2021-01-11 18:27:55 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
|
|
|
|
{
|
|
|
|
|
internal class InlineResponses
|
|
|
|
|
{
|
|
|
|
|
private const uint MaxStrLenUTF8 = 0x7D4;
|
|
|
|
|
private const uint MaxStrLenUTF16 = 0x3EC;
|
|
|
|
|
|
|
|
|
|
private static void BeginResponse(InlineKeyboardState state, InlineKeyboardResponse resCode, BinaryWriter writer)
|
|
|
|
|
{
|
|
|
|
|
writer.Write((uint)state);
|
|
|
|
|
writer.Write((uint)resCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static uint WriteString(string text, BinaryWriter writer, uint maxSize, Encoding encoding)
|
|
|
|
|
{
|
|
|
|
|
// Ensure the text fits in the buffer, but do not straight cut the bytes because
|
|
|
|
|
// this may corrupt the encoding. Search for a cut in the source string that fits.
|
|
|
|
|
|
|
|
|
|
byte[] bytes = null;
|
|
|
|
|
|
|
|
|
|
for (int maxStr = text.Length; maxStr >= 0; maxStr--)
|
|
|
|
|
{
|
|
|
|
|
// This loop will probably will run only once.
|
2023-01-18 22:25:16 +00:00
|
|
|
|
bytes = encoding.GetBytes(text, 0, maxStr);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
if (bytes.Length <= maxSize)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.Write(bytes);
|
|
|
|
|
writer.Seek((int)maxSize - bytes.Length, SeekOrigin.Current);
|
|
|
|
|
writer.Write((uint)text.Length); // String size
|
|
|
|
|
|
|
|
|
|
return (uint)text.Length; // Return the cursor position at the end of the text
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
private static void WriteStringWithCursor(string text, uint cursor, BinaryWriter writer, uint maxSize, Encoding encoding, bool padMiddle)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
2021-10-12 19:54:21 +00:00
|
|
|
|
uint length = WriteString(text, writer, maxSize, encoding);
|
|
|
|
|
|
|
|
|
|
if (cursor > length)
|
|
|
|
|
{
|
|
|
|
|
cursor = length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (padMiddle)
|
|
|
|
|
{
|
|
|
|
|
writer.Write((int)-1); // ?
|
|
|
|
|
writer.Write((int)-1); // ?
|
|
|
|
|
}
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
writer.Write(cursor); // Cursor position
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] FinishedInitialize(InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 2 * sizeof(uint) + 0x1;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.FinishedInitialize, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
writer.Write((byte)1); // Data (ignored by the program)
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] Default(InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 2 * sizeof(uint);
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.Default, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] ChangedString(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF16;
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.ChangedString, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, true);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] MovedCursor(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.MovedCursor, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, false);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] MovedTab(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
// Should be the same as MovedCursor.
|
|
|
|
|
|
|
|
|
|
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.MovedTab, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, false);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] DecidedEnter(string text, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 3 * sizeof(uint) + MaxStrLenUTF16;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.DecidedEnter, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
WriteString(text, writer, MaxStrLenUTF16, Encoding.Unicode);
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] DecidedCancel(InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 2 * sizeof(uint);
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.DecidedCancel, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] ChangedStringUtf8(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF8;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.ChangedStringUtf8, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, true);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] MovedCursorUtf8(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF8;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.MovedCursorUtf8, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, false);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] DecidedEnterUtf8(string text, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 3 * sizeof(uint) + MaxStrLenUTF8;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.DecidedEnterUtf8, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
WriteString(text, writer, MaxStrLenUTF8, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] UnsetCustomizeDic(InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 2 * sizeof(uint);
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.UnsetCustomizeDic, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] ReleasedUserWordInfo(InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 2 * sizeof(uint);
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.ReleasedUserWordInfo, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 00:28:44 +00:00
|
|
|
|
public static byte[] UnsetCustomizedDictionaries(InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 2 * sizeof(uint);
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.UnsetCustomizedDictionaries, writer);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] ChangedStringV2(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF16 + 0x1;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.ChangedStringV2, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, true);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
writer.Write((byte)0); // Flag == 0
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] MovedCursorV2(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16 + 0x1;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.MovedCursorV2, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF16, Encoding.Unicode, false);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
writer.Write((byte)0); // Flag == 0
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] ChangedStringUtf8V2(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 6 * sizeof(uint) + MaxStrLenUTF8 + 0x1;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.ChangedStringUtf8V2, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, true);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
writer.Write((byte)0); // Flag == 0
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:54:21 +00:00
|
|
|
|
public static byte[] MovedCursorUtf8V2(string text, uint cursor, InlineKeyboardState state)
|
2021-01-11 18:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
uint resSize = 4 * sizeof(uint) + MaxStrLenUTF8 + 0x1;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[resSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-02-11 00:28:44 +00:00
|
|
|
|
BeginResponse(state, InlineKeyboardResponse.MovedCursorUtf8V2, writer);
|
2021-10-12 19:54:21 +00:00
|
|
|
|
WriteStringWithCursor(text, cursor, writer, MaxStrLenUTF8, Encoding.UTF8, false);
|
2021-01-11 18:27:55 +00:00
|
|
|
|
writer.Write((byte)0); // Flag == 0
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|