2022-11-24 15:26:29 +01:00
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using Ryujinx.Common.Configuration.Hid;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-08-03 03:30:58 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Am.AppletAE;
|
2021-10-12 21:54:21 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Npad;
|
|
|
|
|
using Ryujinx.HLE.Ui;
|
|
|
|
|
using Ryujinx.HLE.Ui.Input;
|
|
|
|
|
using Ryujinx.Memory;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
using System;
|
2021-10-12 21:54:21 +02:00
|
|
|
|
using System.Diagnostics;
|
2022-12-12 15:10:05 +01:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
using System.IO;
|
2022-11-24 15:26:29 +01:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Applets
|
|
|
|
|
{
|
|
|
|
|
internal class SoftwareKeyboardApplet : IApplet
|
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private const string DefaultInputText = "Ryujinx";
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-13 17:20:48 +02:00
|
|
|
|
private const int StandardBufferSize = 0x7D8;
|
|
|
|
|
private const int InteractiveBufferSize = 0x7D4;
|
|
|
|
|
private const int MaxUserWords = 0x1388;
|
|
|
|
|
private const int MaxUiTextSize = 100;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private const Key CycleInputModesKey = Key.F6;
|
2020-08-03 03:30:58 +02:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private readonly Switch _device;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private SoftwareKeyboardState _foregroundState = SoftwareKeyboardState.Uninitialized;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
private volatile InlineKeyboardState _backgroundState = InlineKeyboardState.Uninitialized;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
private bool _isBackground = false;
|
|
|
|
|
|
2019-11-18 12:16:26 +01:00
|
|
|
|
private AppletSession _normalSession;
|
|
|
|
|
private AppletSession _interactiveSession;
|
|
|
|
|
|
2021-02-11 01:28:44 +01:00
|
|
|
|
// Configuration for foreground mode.
|
|
|
|
|
private SoftwareKeyboardConfig _keyboardForegroundConfig;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
|
2021-02-11 01:28:44 +01:00
|
|
|
|
// Configuration for background (inline) mode.
|
|
|
|
|
private SoftwareKeyboardInitialize _keyboardBackgroundInitialize;
|
|
|
|
|
private SoftwareKeyboardCustomizeDic _keyboardBackgroundDic;
|
|
|
|
|
private SoftwareKeyboardDictSet _keyboardBackgroundDictSet;
|
|
|
|
|
private SoftwareKeyboardUserWord[] _keyboardBackgroundUserWords;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
private byte[] _transferMemory;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private string _textValue = "";
|
|
|
|
|
private int _cursorBegin = 0;
|
|
|
|
|
private Encoding _encoding = Encoding.Unicode;
|
|
|
|
|
private KeyboardResult _lastResult = KeyboardResult.NotSet;
|
|
|
|
|
|
|
|
|
|
private IDynamicTextInputHandler _dynamicTextInputHandler = null;
|
|
|
|
|
private SoftwareKeyboardRenderer _keyboardRenderer = null;
|
|
|
|
|
private NpadReader _npads = null;
|
|
|
|
|
private bool _canAcceptController = false;
|
|
|
|
|
private KeyboardInputMode _inputMode = KeyboardInputMode.ControllerAndKeyboard;
|
|
|
|
|
|
|
|
|
|
private object _lock = new object();
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
public event EventHandler AppletStateChanged;
|
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
public SoftwareKeyboardApplet(Horizon system)
|
|
|
|
|
{
|
|
|
|
|
_device = system.Device;
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-11-01 23:38:13 +01:00
|
|
|
|
public ResultCode Start(AppletSession normalSession, AppletSession interactiveSession)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
lock (_lock)
|
2020-08-03 03:30:58 +02:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_normalSession = normalSession;
|
|
|
|
|
_interactiveSession = interactiveSession;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_interactiveSession.DataAvailable += OnInteractiveData;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
var launchParams = _normalSession.Pop();
|
|
|
|
|
var keyboardConfig = _normalSession.Pop();
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2022-11-24 15:26:29 +01:00
|
|
|
|
_isBackground = keyboardConfig.Length == Unsafe.SizeOf<SoftwareKeyboardInitialize>();
|
2020-08-03 03:30:58 +02:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (_isBackground)
|
2021-01-11 19:27:55 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
// Initialize the keyboard applet in background mode.
|
|
|
|
|
|
2022-11-24 15:26:29 +01:00
|
|
|
|
_keyboardBackgroundInitialize = MemoryMarshal.Read<SoftwareKeyboardInitialize>(keyboardConfig);
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_backgroundState = InlineKeyboardState.Uninitialized;
|
|
|
|
|
|
|
|
|
|
if (_device.UiHandler == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, "GUI Handler is not set, software keyboard applet will not work properly");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Create a text handler that converts keyboard strokes to strings.
|
|
|
|
|
_dynamicTextInputHandler = _device.UiHandler.CreateDynamicTextInputHandler();
|
|
|
|
|
_dynamicTextInputHandler.TextChangedEvent += HandleTextChangedEvent;
|
|
|
|
|
_dynamicTextInputHandler.KeyPressedEvent += HandleKeyPressedEvent;
|
|
|
|
|
|
|
|
|
|
_npads = new NpadReader(_device);
|
|
|
|
|
_npads.NpadButtonDownEvent += HandleNpadButtonDownEvent;
|
|
|
|
|
_npads.NpadButtonUpEvent += HandleNpadButtonUpEvent;
|
|
|
|
|
|
|
|
|
|
_keyboardRenderer = new SoftwareKeyboardRenderer(_device.UiHandler.HostUiTheme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
// Initialize the keyboard applet in foreground mode.
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_keyboardForegroundConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
|
|
|
|
|
}
|
2019-12-03 05:00:56 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (!_normalSession.TryPop(out _transferMemory))
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (_keyboardForegroundConfig.UseUtf8)
|
|
|
|
|
{
|
|
|
|
|
_encoding = Encoding.UTF8;
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.Ready;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
ExecuteForegroundKeyboard();
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
2021-01-11 19:27:55 +01:00
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode GetResult()
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private bool IsKeyboardActive()
|
|
|
|
|
{
|
|
|
|
|
return _backgroundState >= InlineKeyboardState.Appearing && _backgroundState < InlineKeyboardState.Disappearing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool InputModeControllerEnabled()
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
|
|
|
|
|
_inputMode == KeyboardInputMode.ControllerOnly;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private bool InputModeTypingEnabled()
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
|
|
|
|
|
_inputMode == KeyboardInputMode.KeyboardOnly;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AdvanceInputMode()
|
|
|
|
|
{
|
|
|
|
|
_inputMode = (KeyboardInputMode)((int)(_inputMode + 1) % (int)KeyboardInputMode.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DrawTo(RenderingSurfaceInfo surfaceInfo, IVirtualMemoryManager destination, ulong position)
|
|
|
|
|
{
|
|
|
|
|
_npads?.Update();
|
|
|
|
|
|
2021-11-28 21:24:17 +01:00
|
|
|
|
_keyboardRenderer?.SetSurfaceInfo(surfaceInfo);
|
|
|
|
|
|
|
|
|
|
return _keyboardRenderer?.DrawTo(destination, position) ?? false;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
private void ExecuteForegroundKeyboard()
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2020-08-03 03:30:58 +02:00
|
|
|
|
string initialText = null;
|
|
|
|
|
|
|
|
|
|
// Initial Text is always encoded as a UTF-16 string in the work buffer (passed as transfer memory)
|
|
|
|
|
// InitialStringOffset points to the memory offset and InitialStringLength is the number of UTF-16 characters
|
2021-02-11 01:28:44 +01:00
|
|
|
|
if (_transferMemory != null && _keyboardForegroundConfig.InitialStringLength > 0)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-02-11 01:28:44 +01:00
|
|
|
|
initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardForegroundConfig.InitialStringOffset,
|
|
|
|
|
2 * _keyboardForegroundConfig.InitialStringLength);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the max string length is 0, we set it to a large default
|
|
|
|
|
// length.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
if (_keyboardForegroundConfig.StringLengthMax == 0)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-02-11 01:28:44 +01:00
|
|
|
|
_keyboardForegroundConfig.StringLengthMax = 100;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
if (_device.UiHandler == null)
|
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
|
2021-10-12 21:54:21 +02:00
|
|
|
|
|
|
|
|
|
_textValue = DefaultInputText;
|
|
|
|
|
_lastResult = KeyboardResult.Accept;
|
2020-08-03 03:30:58 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
// Call the configured GUI handler to get user's input.
|
|
|
|
|
var args = new SoftwareKeyboardUiArgs
|
|
|
|
|
{
|
2022-11-16 23:53:17 +01:00
|
|
|
|
HeaderText = StripUnicodeControlCodes(_keyboardForegroundConfig.HeaderText),
|
|
|
|
|
SubtitleText = StripUnicodeControlCodes(_keyboardForegroundConfig.SubtitleText),
|
|
|
|
|
GuideText = StripUnicodeControlCodes(_keyboardForegroundConfig.GuideText),
|
2021-10-12 21:54:21 +02:00
|
|
|
|
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
|
|
|
|
|
_keyboardForegroundConfig.SubmitText : "OK"),
|
|
|
|
|
StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
|
|
|
|
|
StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
|
|
|
|
|
InitialText = initialText
|
|
|
|
|
};
|
2020-08-03 03:30:58 +02:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_lastResult = _device.UiHandler.DisplayInputDialog(args, out _textValue) ? KeyboardResult.Accept : KeyboardResult.Cancel;
|
|
|
|
|
_textValue ??= initialText ?? DefaultInputText;
|
|
|
|
|
}
|
2020-08-03 03:30:58 +02:00
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
// If the game requests a string with a minimum length less
|
|
|
|
|
// than our default text, repeat our default text until we meet
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// the minimum length requirement.
|
2019-12-03 05:00:56 +01:00
|
|
|
|
// This should always be done before the text truncation step.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
while (_textValue.Length < _keyboardForegroundConfig.StringLengthMin)
|
2019-12-03 05:00:56 +01:00
|
|
|
|
{
|
|
|
|
|
_textValue = String.Join(" ", _textValue, _textValue);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 12:16:26 +01:00
|
|
|
|
// If our default text is longer than the allowed length,
|
|
|
|
|
// we truncate it.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
if (_textValue.Length > _keyboardForegroundConfig.StringLengthMax)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_textValue = _textValue.Substring(0, _keyboardForegroundConfig.StringLengthMax);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
// Does the application want to validate the text itself?
|
2021-02-11 01:28:44 +01:00
|
|
|
|
if (_keyboardForegroundConfig.CheckText)
|
2019-12-03 05:00:56 +01:00
|
|
|
|
{
|
|
|
|
|
// The application needs to validate the response, so we
|
|
|
|
|
// submit it to the interactive output buffer, and poll it
|
|
|
|
|
// for validation. Once validated, the application will submit
|
|
|
|
|
// back a validation status, which is handled in OnInteractiveDataPushIn.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.ValidationPending;
|
2019-12-03 05:00:56 +01:00
|
|
|
|
|
2021-10-13 17:20:48 +02:00
|
|
|
|
PushForegroundResponse(true);
|
2019-12-03 05:00:56 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
|
|
|
|
// If the application doesn't need to validate the response,
|
|
|
|
|
// we push the data to the non-interactive output buffer
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// and poll it for completion.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.Complete;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-13 17:20:48 +02:00
|
|
|
|
PushForegroundResponse(false);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnInteractiveData(object sender, EventArgs e)
|
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// Obtain the validation status response.
|
2019-11-18 12:16:26 +01:00
|
|
|
|
var data = _interactiveSession.Pop();
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (_isBackground)
|
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
OnBackgroundInteractiveData(data);
|
|
|
|
|
}
|
2021-01-11 19:27:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnForegroundInteractiveData(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnForegroundInteractiveData(byte[] data)
|
|
|
|
|
{
|
2021-02-11 01:28:44 +01:00
|
|
|
|
if (_foregroundState == SoftwareKeyboardState.ValidationPending)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
|
|
|
|
// TODO(jduncantor):
|
|
|
|
|
// If application rejects our "attempt", submit another attempt,
|
|
|
|
|
// and put the applet back in PendingValidation state.
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// For now we assume success, so we push the final result
|
2019-11-18 12:16:26 +01:00
|
|
|
|
// to the standard output buffer and carry on our merry way.
|
2021-10-13 17:20:48 +02:00
|
|
|
|
PushForegroundResponse(false);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
|
2021-02-11 01:28:44 +01:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.Complete;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
else if(_foregroundState == SoftwareKeyboardState.Complete)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
|
|
|
|
// If we have already completed, we push the result text
|
|
|
|
|
// back on the output buffer and poll the application.
|
2021-10-13 17:20:48 +02:00
|
|
|
|
PushForegroundResponse(false);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// We shouldn't be able to get here through standard swkbd execution.
|
|
|
|
|
throw new InvalidOperationException("Software Keyboard is in an invalid state.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
private void OnBackgroundInteractiveData(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
// WARNING: Only invoke applet state changes after an explicit finalization
|
|
|
|
|
// request from the game, this is because the inline keyboard is expected to
|
|
|
|
|
// keep running in the background sending data by itself.
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(data))
|
|
|
|
|
using (BinaryReader reader = new BinaryReader(stream))
|
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
var request = (InlineKeyboardRequest)reader.ReadUInt32();
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
long remaining;
|
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {_backgroundState}");
|
2021-01-11 19:27:55 +01:00
|
|
|
|
|
|
|
|
|
switch (request)
|
|
|
|
|
{
|
|
|
|
|
case InlineKeyboardRequest.UseChangedStringV2:
|
2021-10-12 21:54:21 +02:00
|
|
|
|
Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseChangedStringV2");
|
2021-01-11 19:27:55 +01:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.UseMovedCursorV2:
|
2021-10-12 21:54:21 +02:00
|
|
|
|
Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseMovedCursorV2");
|
2021-02-11 01:28:44 +01:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.SetUserWordInfo:
|
|
|
|
|
// Read the user word info data.
|
|
|
|
|
remaining = stream.Length - stream.Position;
|
|
|
|
|
if (remaining < sizeof(int))
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info of {remaining} bytes");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int wordsCount = reader.ReadInt32();
|
2022-11-24 15:26:29 +01:00
|
|
|
|
int wordSize = Unsafe.SizeOf<SoftwareKeyboardUserWord>();
|
2021-02-11 01:28:44 +01:00
|
|
|
|
remaining = stream.Length - stream.Position;
|
|
|
|
|
|
|
|
|
|
if (wordsCount > MaxUserWords)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Received {wordsCount} User Words but the maximum is {MaxUserWords}");
|
|
|
|
|
}
|
|
|
|
|
else if (wordsCount * wordSize != remaining)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info data of {remaining} bytes for {wordsCount} words");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_keyboardBackgroundUserWords = new SoftwareKeyboardUserWord[wordsCount];
|
|
|
|
|
|
|
|
|
|
for (int word = 0; word < wordsCount; word++)
|
|
|
|
|
{
|
2022-11-24 15:26:29 +01:00
|
|
|
|
_keyboardBackgroundUserWords[word] = reader.ReadStruct<SoftwareKeyboardUserWord>();
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(_backgroundState));
|
2021-01-11 19:27:55 +01:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.SetCustomizeDic:
|
2021-02-11 01:28:44 +01:00
|
|
|
|
// Read the custom dic data.
|
|
|
|
|
remaining = stream.Length - stream.Position;
|
2022-11-24 15:26:29 +01:00
|
|
|
|
if (remaining != Unsafe.SizeOf<SoftwareKeyboardCustomizeDic>())
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Customize Dic of {remaining} bytes");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-24 15:26:29 +01:00
|
|
|
|
_keyboardBackgroundDic = reader.ReadStruct<SoftwareKeyboardCustomizeDic>();
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.SetCustomizedDictionaries:
|
|
|
|
|
// Read the custom dictionaries data.
|
2021-01-11 19:27:55 +01:00
|
|
|
|
remaining = stream.Length - stream.Position;
|
2022-11-24 15:26:29 +01:00
|
|
|
|
if (remaining != Unsafe.SizeOf<SoftwareKeyboardDictSet>())
|
2021-01-11 19:27:55 +01:00
|
|
|
|
{
|
2021-02-11 01:28:44 +01:00
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes");
|
2021-01-11 19:27:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-24 15:26:29 +01:00
|
|
|
|
_keyboardBackgroundDictSet = reader.ReadStruct<SoftwareKeyboardDictSet>();
|
2021-01-11 19:27:55 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.Calc:
|
2021-10-12 21:54:21 +02:00
|
|
|
|
// The Calc request is used to communicate configuration changes and commands to the keyboard.
|
|
|
|
|
// Fields in the Calc struct and operations are masked by the Flags field.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
|
|
|
|
// Read the Calc data.
|
2021-10-12 21:54:21 +02:00
|
|
|
|
SoftwareKeyboardCalcEx newCalc;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
remaining = stream.Length - stream.Position;
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (remaining == Marshal.SizeOf<SoftwareKeyboardCalc>())
|
2021-01-11 19:27:55 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
var keyboardCalcData = reader.ReadBytes((int)remaining);
|
|
|
|
|
var keyboardCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
|
|
|
|
|
|
|
|
|
|
newCalc = keyboardCalc.ToExtended();
|
2021-01-11 19:27:55 +01:00
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
else if (remaining == Marshal.SizeOf<SoftwareKeyboardCalcEx>() || remaining == SoftwareKeyboardCalcEx.AlternativeSize)
|
2021-01-11 19:27:55 +01:00
|
|
|
|
{
|
|
|
|
|
var keyboardCalcData = reader.ReadBytes((int)remaining);
|
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
newCalc = ReadStruct<SoftwareKeyboardCalcEx>(keyboardCalcData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes");
|
2021-01-11 19:27:55 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
newCalc = new SoftwareKeyboardCalcEx();
|
2021-01-11 19:27:55 +01:00
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
|
|
|
|
|
// Process each individual operation specified in the flags.
|
|
|
|
|
|
|
|
|
|
bool updateText = false;
|
|
|
|
|
|
|
|
|
|
if ((newCalc.Flags & KeyboardCalcFlags.Initialize) != 0)
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.FinishedInitialize(_backgroundState));
|
|
|
|
|
|
|
|
|
|
_backgroundState = InlineKeyboardState.Initialized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((newCalc.Flags & KeyboardCalcFlags.SetCursorPos) != 0)
|
|
|
|
|
{
|
|
|
|
|
_cursorBegin = newCalc.CursorPos;
|
|
|
|
|
updateText = true;
|
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Cursor position set to {_cursorBegin}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((newCalc.Flags & KeyboardCalcFlags.SetInputText) != 0)
|
|
|
|
|
{
|
|
|
|
|
_textValue = newCalc.InputText;
|
|
|
|
|
updateText = true;
|
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Input text set to {_textValue}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((newCalc.Flags & KeyboardCalcFlags.SetUtf8Mode) != 0)
|
|
|
|
|
{
|
|
|
|
|
_encoding = newCalc.UseUtf8 ? Encoding.UTF8 : Encoding.Default;
|
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Encoding set to {_encoding}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (updateText)
|
|
|
|
|
{
|
|
|
|
|
_dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
|
|
|
|
|
_keyboardRenderer.UpdateTextState(_textValue, _cursorBegin, _cursorBegin, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((newCalc.Flags & KeyboardCalcFlags.MustShow) != 0)
|
|
|
|
|
{
|
|
|
|
|
ActivateFrontend();
|
|
|
|
|
|
|
|
|
|
_backgroundState = InlineKeyboardState.Shown;
|
|
|
|
|
|
|
|
|
|
PushChangedString(_textValue, (uint)_cursorBegin, _backgroundState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the response to the Calc
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
|
2021-01-11 19:27:55 +01:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.Finalize:
|
2021-10-12 21:54:21 +02:00
|
|
|
|
// Destroy the frontend.
|
|
|
|
|
DestroyFrontend();
|
2021-02-28 11:26:00 +01:00
|
|
|
|
// The calling application wants to close the keyboard applet and will wait for a state change.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
_backgroundState = InlineKeyboardState.Uninitialized;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// We shouldn't be able to get here through standard swkbd execution.
|
2021-02-11 01:28:44 +01:00
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Invalid Software Keyboard request {request} during state {_backgroundState}");
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
|
2021-01-11 19:27:55 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private void ActivateFrontend()
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Activating software keyboard frontend");
|
|
|
|
|
|
|
|
|
|
_inputMode = KeyboardInputMode.ControllerAndKeyboard;
|
|
|
|
|
|
|
|
|
|
_npads.Update(true);
|
|
|
|
|
|
|
|
|
|
NpadButton buttons = _npads.GetCurrentButtonsOfAllNpads();
|
|
|
|
|
|
|
|
|
|
// Block the input if the current accept key is pressed so the applet won't be instantly closed.
|
|
|
|
|
_canAcceptController = (buttons & NpadButton.A) == 0;
|
|
|
|
|
|
|
|
|
|
_dynamicTextInputHandler.TextProcessingEnabled = true;
|
|
|
|
|
|
|
|
|
|
_keyboardRenderer.UpdateCommandState(null, null, true);
|
|
|
|
|
_keyboardRenderer.UpdateTextState(null, null, null, null, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DeactivateFrontend()
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Deactivating software keyboard frontend");
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_inputMode = KeyboardInputMode.ControllerAndKeyboard;
|
|
|
|
|
_canAcceptController = false;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_dynamicTextInputHandler.TextProcessingEnabled = false;
|
|
|
|
|
_dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private void DestroyFrontend()
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Destroying software keyboard frontend");
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_keyboardRenderer?.Dispose();
|
|
|
|
|
_keyboardRenderer = null;
|
|
|
|
|
|
|
|
|
|
if (_dynamicTextInputHandler != null)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_dynamicTextInputHandler.TextChangedEvent -= HandleTextChangedEvent;
|
|
|
|
|
_dynamicTextInputHandler.KeyPressedEvent -= HandleKeyPressedEvent;
|
|
|
|
|
_dynamicTextInputHandler.Dispose();
|
|
|
|
|
_dynamicTextInputHandler = null;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (_npads != null)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_npads.NpadButtonDownEvent -= HandleNpadButtonDownEvent;
|
|
|
|
|
_npads.NpadButtonUpEvent -= HandleNpadButtonUpEvent;
|
|
|
|
|
_npads = null;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool HandleKeyPressedEvent(Key key)
|
|
|
|
|
{
|
|
|
|
|
if (key == CycleInputModesKey)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
lock (_lock)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (IsKeyboardActive())
|
|
|
|
|
{
|
|
|
|
|
AdvanceInputMode();
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
bool typingEnabled = InputModeTypingEnabled();
|
|
|
|
|
bool controllerEnabled = InputModeControllerEnabled();
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_dynamicTextInputHandler.TextProcessingEnabled = typingEnabled;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_keyboardRenderer.UpdateTextState(null, null, null, null, typingEnabled);
|
|
|
|
|
_keyboardRenderer.UpdateCommandState(null, null, controllerEnabled);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleTextChangedEvent(string text, int cursorBegin, int cursorEnd, bool overwriteMode)
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
// Text processing should not run with typing disabled.
|
|
|
|
|
Debug.Assert(InputModeTypingEnabled());
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (text.Length > MaxUiTextSize)
|
|
|
|
|
{
|
|
|
|
|
// Limit the text size and change it back.
|
|
|
|
|
text = text.Substring(0, MaxUiTextSize);
|
|
|
|
|
cursorBegin = Math.Min(cursorBegin, MaxUiTextSize);
|
|
|
|
|
cursorEnd = Math.Min(cursorEnd, MaxUiTextSize);
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_dynamicTextInputHandler.SetText(text, cursorBegin, cursorEnd);
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
_textValue = text;
|
|
|
|
|
_cursorBegin = cursorBegin;
|
|
|
|
|
_keyboardRenderer.UpdateTextState(text, cursorBegin, cursorEnd, overwriteMode, null);
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
PushUpdatedState(text, cursorBegin, KeyboardResult.NotSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private void HandleNpadButtonDownEvent(int npadIndex, NpadButton button)
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
if (!IsKeyboardActive())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
switch (button)
|
|
|
|
|
{
|
|
|
|
|
case NpadButton.A:
|
|
|
|
|
_keyboardRenderer.UpdateCommandState(_canAcceptController, null, null);
|
|
|
|
|
break;
|
|
|
|
|
case NpadButton.B:
|
|
|
|
|
_keyboardRenderer.UpdateCommandState(null, _canAcceptController, null);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 21:54:21 +02:00
|
|
|
|
private void HandleNpadButtonUpEvent(int npadIndex, NpadButton button)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
lock (_lock)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
KeyboardResult result = KeyboardResult.NotSet;
|
|
|
|
|
|
|
|
|
|
switch (button)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
case NpadButton.A:
|
|
|
|
|
result = KeyboardResult.Accept;
|
|
|
|
|
_keyboardRenderer.UpdateCommandState(false, null, null);
|
|
|
|
|
break;
|
|
|
|
|
case NpadButton.B:
|
|
|
|
|
result = KeyboardResult.Cancel;
|
|
|
|
|
_keyboardRenderer.UpdateCommandState(null, false, null);
|
|
|
|
|
break;
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
|
|
|
|
|
if (IsKeyboardActive())
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
if (!_canAcceptController)
|
|
|
|
|
{
|
|
|
|
|
_canAcceptController = true;
|
|
|
|
|
}
|
|
|
|
|
else if (InputModeControllerEnabled())
|
|
|
|
|
{
|
|
|
|
|
PushUpdatedState(_textValue, _cursorBegin, result);
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PushUpdatedState(string text, int cursorBegin, KeyboardResult result)
|
|
|
|
|
{
|
|
|
|
|
_lastResult = result;
|
|
|
|
|
_textValue = text;
|
|
|
|
|
|
|
|
|
|
bool cancel = result == KeyboardResult.Cancel;
|
|
|
|
|
bool accept = result == KeyboardResult.Accept;
|
|
|
|
|
|
|
|
|
|
if (!IsKeyboardActive())
|
|
|
|
|
{
|
|
|
|
|
// Keyboard is not active.
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (accept == false && cancel == false)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Updating keyboard text to {text} and cursor position to {cursorBegin}");
|
|
|
|
|
|
|
|
|
|
PushChangedString(text, (uint)cursorBegin, _backgroundState);
|
|
|
|
|
}
|
2021-02-11 01:28:44 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
// Disable the frontend.
|
|
|
|
|
DeactivateFrontend();
|
|
|
|
|
|
|
|
|
|
// The 'Complete' state indicates the Calc request has been fulfilled by the applet.
|
|
|
|
|
_backgroundState = InlineKeyboardState.Disappearing;
|
|
|
|
|
|
|
|
|
|
if (accept)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Sending keyboard OK with text {text}");
|
|
|
|
|
|
|
|
|
|
DecidedEnter(text, _backgroundState);
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
else if (cancel)
|
2021-02-11 01:28:44 +01:00
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard Cancel");
|
|
|
|
|
|
|
|
|
|
DecidedCancel(_backgroundState);
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
2021-10-12 21:54:21 +02:00
|
|
|
|
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
|
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Resetting state of the keyboard to {_backgroundState}");
|
|
|
|
|
|
|
|
|
|
// Set the state of the applet to 'Initialized' as it is the only known state so far
|
|
|
|
|
// that does not soft-lock the keyboard after use.
|
|
|
|
|
|
|
|
|
|
_backgroundState = InlineKeyboardState.Initialized;
|
|
|
|
|
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default(_backgroundState));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PushChangedString(string text, uint cursor, InlineKeyboardState state)
|
|
|
|
|
{
|
|
|
|
|
// TODO (Caian): The *V2 methods are not supported because the applications that request
|
|
|
|
|
// them do not seem to accept them. The regular methods seem to work just fine in all cases.
|
|
|
|
|
|
|
|
|
|
if (_encoding == Encoding.UTF8)
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.ChangedStringUtf8(text, cursor, state));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.ChangedString(text, cursor, state));
|
2021-02-11 01:28:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DecidedEnter(string text, InlineKeyboardState state)
|
|
|
|
|
{
|
|
|
|
|
if (_encoding == Encoding.UTF8)
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.DecidedEnterUtf8(text, state));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.DecidedEnter(text, state));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DecidedCancel(InlineKeyboardState state)
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.DecidedCancel(state));
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 17:20:48 +02:00
|
|
|
|
private void PushForegroundResponse(bool interactive)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-10-13 17:20:48 +02:00
|
|
|
|
int bufferSize = interactive ? InteractiveBufferSize : StandardBufferSize;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[bufferSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2021-10-12 21:54:21 +02:00
|
|
|
|
byte[] output = _encoding.GetBytes(_textValue);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-10-13 17:20:48 +02:00
|
|
|
|
if (!interactive)
|
|
|
|
|
{
|
|
|
|
|
// Result Code.
|
|
|
|
|
writer.Write(_lastResult == KeyboardResult.Accept ? 0U : 1U);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// In interactive mode, we write the length of the text as a long, rather than
|
|
|
|
|
// a result code. This field is inclusive of the 64-bit size.
|
|
|
|
|
writer.Write((long)output.Length + 8);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 12:16:26 +01:00
|
|
|
|
writer.Write(output);
|
|
|
|
|
|
2021-10-13 17:20:48 +02:00
|
|
|
|
if (!interactive)
|
|
|
|
|
{
|
|
|
|
|
_normalSession.Push(stream.ToArray());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(stream.ToArray());
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-03 08:53:06 +02:00
|
|
|
|
|
2022-11-16 23:53:17 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes all Unicode control code characters from the input string.
|
|
|
|
|
/// This includes CR/LF, tabs, null characters, escape characters,
|
|
|
|
|
/// and special control codes which are used for formatting by the real keyboard applet.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Some games send special control codes (such as 0x13 "Device Control 3") as part of the string.
|
|
|
|
|
/// Future implementations of the emulated keyboard applet will need to handle these as well.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="input">The input string to sanitize (may be null).</param>
|
|
|
|
|
/// <returns>The sanitized string.</returns>
|
|
|
|
|
internal static string StripUnicodeControlCodes(string input)
|
|
|
|
|
{
|
|
|
|
|
if (input is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(capacity: input.Length);
|
|
|
|
|
foreach (char c in input)
|
|
|
|
|
{
|
|
|
|
|
if (!char.IsControl(c))
|
|
|
|
|
{
|
|
|
|
|
sb.Append(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
private static T ReadStruct<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(byte[] data)
|
2020-04-03 08:53:06 +02:00
|
|
|
|
where T : struct
|
|
|
|
|
{
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
|
|
|
|
|
|
|
|
|
|
try
|
2021-01-11 19:27:55 +01:00
|
|
|
|
{
|
2020-04-03 08:53:06 +02:00
|
|
|
|
return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
handle.Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|