2021-02-10 19:28:44 -05:00
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-08-02 21:30:58 -04:00
|
|
|
|
using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Am.AppletAE;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Applets
|
|
|
|
|
{
|
|
|
|
|
internal class SoftwareKeyboardApplet : IApplet
|
|
|
|
|
{
|
2019-12-02 23:00:56 -05:00
|
|
|
|
private const string DefaultText = "Ryujinx";
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
private const long DebounceTimeMillis = 200;
|
|
|
|
|
private const int ResetDelayMillis = 500;
|
|
|
|
|
|
2020-08-02 21:30:58 -04:00
|
|
|
|
private readonly Switch _device;
|
|
|
|
|
|
2019-12-02 23:00:56 -05:00
|
|
|
|
private const int StandardBufferSize = 0x7D8;
|
|
|
|
|
private const int InteractiveBufferSize = 0x7D4;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
private const int MaxUserWords = 0x1388;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
private SoftwareKeyboardState _foregroundState = SoftwareKeyboardState.Uninitialized;
|
|
|
|
|
private volatile InlineKeyboardState _backgroundState = InlineKeyboardState.Uninitialized;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
private bool _isBackground = false;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
private bool _alreadyShown = false;
|
|
|
|
|
private volatile bool _useChangedStringV2 = false;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
|
2019-11-18 06:16:26 -05:00
|
|
|
|
private AppletSession _normalSession;
|
|
|
|
|
private AppletSession _interactiveSession;
|
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// Configuration for foreground mode.
|
|
|
|
|
private SoftwareKeyboardConfig _keyboardForegroundConfig;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// Configuration for background (inline) mode.
|
|
|
|
|
private SoftwareKeyboardInitialize _keyboardBackgroundInitialize;
|
|
|
|
|
private SoftwareKeyboardCalc _keyboardBackgroundCalc;
|
|
|
|
|
private SoftwareKeyboardCustomizeDic _keyboardBackgroundDic;
|
|
|
|
|
private SoftwareKeyboardDictSet _keyboardBackgroundDictSet;
|
|
|
|
|
private SoftwareKeyboardUserWord[] _keyboardBackgroundUserWords;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
|
2020-08-02 21:30:58 -04:00
|
|
|
|
private byte[] _transferMemory;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
private string _textValue = "";
|
2020-08-02 21:30:58 -04:00
|
|
|
|
private bool _okPressed = false;
|
2019-12-02 23:00:56 -05:00
|
|
|
|
private Encoding _encoding = Encoding.Unicode;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
private long _lastTextSetMillis = 0;
|
2021-02-28 05:26:00 -05:00
|
|
|
|
private bool _lastWasHidden = false;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
|
|
|
|
public event EventHandler AppletStateChanged;
|
|
|
|
|
|
2020-08-02 21:30:58 -04:00
|
|
|
|
public SoftwareKeyboardApplet(Horizon system)
|
|
|
|
|
{
|
|
|
|
|
_device = system.Device;
|
|
|
|
|
}
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
|
|
|
|
public ResultCode Start(AppletSession normalSession,
|
|
|
|
|
AppletSession interactiveSession)
|
|
|
|
|
{
|
|
|
|
|
_normalSession = normalSession;
|
|
|
|
|
_interactiveSession = interactiveSession;
|
|
|
|
|
|
|
|
|
|
_interactiveSession.DataAvailable += OnInteractiveData;
|
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_alreadyShown = false;
|
|
|
|
|
_useChangedStringV2 = false;
|
|
|
|
|
|
2019-11-18 06:16:26 -05:00
|
|
|
|
var launchParams = _normalSession.Pop();
|
|
|
|
|
var keyboardConfig = _normalSession.Pop();
|
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
if (keyboardConfig.Length == Marshal.SizeOf<SoftwareKeyboardInitialize>())
|
2020-08-02 21:30:58 -04:00
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// Initialize the keyboard applet in background mode.
|
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
_isBackground = true;
|
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_keyboardBackgroundInitialize = ReadStruct<SoftwareKeyboardInitialize>(keyboardConfig);
|
|
|
|
|
_backgroundState = InlineKeyboardState.Uninitialized;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2020-08-02 21:30:58 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// Initialize the keyboard applet in foreground mode.
|
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
_isBackground = false;
|
2020-08-02 21:30:58 -04:00
|
|
|
|
|
2021-01-11 13:27:55 -05: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
|
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_keyboardForegroundConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
|
2021-01-11 13:27:55 -05:00
|
|
|
|
}
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
if (!_normalSession.TryPop(out _transferMemory))
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
|
|
|
|
|
}
|
2019-12-02 23:00:56 -05:00
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
if (_keyboardForegroundConfig.UseUtf8)
|
2021-01-11 13:27:55 -05:00
|
|
|
|
{
|
|
|
|
|
_encoding = Encoding.UTF8;
|
|
|
|
|
}
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.Ready;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
ExecuteForegroundKeyboard();
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
2019-11-18 06:16:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode GetResult()
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
private InlineKeyboardState GetInlineState()
|
|
|
|
|
{
|
|
|
|
|
return _backgroundState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetInlineState(InlineKeyboardState state)
|
|
|
|
|
{
|
|
|
|
|
_backgroundState = state;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
private void ExecuteForegroundKeyboard()
|
2019-11-18 06:16:26 -05:00
|
|
|
|
{
|
2020-08-02 21:30:58 -04: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-10 19:28:44 -05:00
|
|
|
|
if (_transferMemory != null && _keyboardForegroundConfig.InitialStringLength > 0)
|
2019-11-18 06:16:26 -05:00
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardForegroundConfig.InitialStringOffset,
|
|
|
|
|
2 * _keyboardForegroundConfig.InitialStringLength);
|
2019-11-18 06:16:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the max string length is 0, we set it to a large default
|
|
|
|
|
// length.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
if (_keyboardForegroundConfig.StringLengthMax == 0)
|
2019-11-18 06:16:26 -05:00
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_keyboardForegroundConfig.StringLengthMax = 100;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 21:30:58 -04:00
|
|
|
|
var args = new SoftwareKeyboardUiArgs
|
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
HeaderText = _keyboardForegroundConfig.HeaderText,
|
|
|
|
|
SubtitleText = _keyboardForegroundConfig.SubtitleText,
|
|
|
|
|
GuideText = _keyboardForegroundConfig.GuideText,
|
|
|
|
|
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
|
|
|
|
|
_keyboardForegroundConfig.SubmitText : "OK"),
|
|
|
|
|
StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
|
|
|
|
|
StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
|
2020-08-02 21:30:58 -04:00
|
|
|
|
InitialText = initialText
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Call the configured GUI handler to get user's input
|
|
|
|
|
if (_device.UiHandler == null)
|
|
|
|
|
{
|
2021-01-11 13:27:55 -05:00
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
|
2020-08-02 21:30:58 -04:00
|
|
|
|
_okPressed = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_okPressed = _device.UiHandler.DisplayInputDialog(args, out _textValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_textValue ??= initialText ?? DefaultText;
|
|
|
|
|
|
2019-12-02 23:00:56 -05: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 13:27:55 -05:00
|
|
|
|
// the minimum length requirement.
|
2019-12-02 23:00:56 -05:00
|
|
|
|
// This should always be done before the text truncation step.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
while (_textValue.Length < _keyboardForegroundConfig.StringLengthMin)
|
2019-12-02 23:00:56 -05:00
|
|
|
|
{
|
|
|
|
|
_textValue = String.Join(" ", _textValue, _textValue);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 06:16:26 -05:00
|
|
|
|
// If our default text is longer than the allowed length,
|
|
|
|
|
// we truncate it.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
if (_textValue.Length > _keyboardForegroundConfig.StringLengthMax)
|
2019-11-18 06:16:26 -05:00
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_textValue = _textValue.Substring(0, (int)_keyboardForegroundConfig.StringLengthMax);
|
2019-11-18 06:16:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 23:00:56 -05:00
|
|
|
|
// Does the application want to validate the text itself?
|
2021-02-10 19:28:44 -05:00
|
|
|
|
if (_keyboardForegroundConfig.CheckText)
|
2019-12-02 23:00:56 -05: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-10 19:28:44 -05:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.ValidationPending;
|
2019-12-02 23:00:56 -05:00
|
|
|
|
|
|
|
|
|
_interactiveSession.Push(BuildResponse(_textValue, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-11-18 06:16:26 -05:00
|
|
|
|
{
|
|
|
|
|
// If the application doesn't need to validate the response,
|
|
|
|
|
// we push the data to the non-interactive output buffer
|
2021-01-11 13:27:55 -05:00
|
|
|
|
// and poll it for completion.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.Complete;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnInteractiveData(object sender, EventArgs e)
|
|
|
|
|
{
|
2021-01-11 13:27:55 -05:00
|
|
|
|
// Obtain the validation status response.
|
2019-11-18 06:16:26 -05:00
|
|
|
|
var data = _interactiveSession.Pop();
|
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
if (_isBackground)
|
|
|
|
|
{
|
|
|
|
|
OnBackgroundInteractiveData(data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnForegroundInteractiveData(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnForegroundInteractiveData(byte[] data)
|
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
if (_foregroundState == SoftwareKeyboardState.ValidationPending)
|
2019-11-18 06:16:26 -05:00
|
|
|
|
{
|
|
|
|
|
// TODO(jduncantor):
|
|
|
|
|
// If application rejects our "attempt", submit another attempt,
|
|
|
|
|
// and put the applet back in PendingValidation state.
|
|
|
|
|
|
2021-01-11 13:27:55 -05:00
|
|
|
|
// For now we assume success, so we push the final result
|
2019-11-18 06:16:26 -05:00
|
|
|
|
// to the standard output buffer and carry on our merry way.
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_foregroundState = SoftwareKeyboardState.Complete;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
}
|
2021-02-10 19:28:44 -05:00
|
|
|
|
else if(_foregroundState == SoftwareKeyboardState.Complete)
|
2019-11-18 06:16:26 -05:00
|
|
|
|
{
|
|
|
|
|
// If we have already completed, we push the result text
|
|
|
|
|
// back on the output buffer and poll the application.
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
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 13:27:55 -05: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-02-10 19:28:44 -05:00
|
|
|
|
InlineKeyboardRequest request = (InlineKeyboardRequest)reader.ReadUInt32();
|
|
|
|
|
InlineKeyboardState state = GetInlineState();
|
2021-01-11 13:27:55 -05:00
|
|
|
|
long remaining;
|
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {state}");
|
2021-01-11 13:27:55 -05:00
|
|
|
|
|
|
|
|
|
switch (request)
|
|
|
|
|
{
|
|
|
|
|
case InlineKeyboardRequest.UseChangedStringV2:
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_useChangedStringV2 = true;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.UseMovedCursorV2:
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// Not used because we only reply with the final string.
|
|
|
|
|
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();
|
|
|
|
|
int wordSize = Marshal.SizeOf<SoftwareKeyboardUserWord>();
|
|
|
|
|
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++)
|
|
|
|
|
{
|
|
|
|
|
byte[] wordData = reader.ReadBytes(wordSize);
|
|
|
|
|
_keyboardBackgroundUserWords[word] = ReadStruct<SoftwareKeyboardUserWord>(wordData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(state));
|
2021-01-11 13:27:55 -05:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.SetCustomizeDic:
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// Read the custom dic data.
|
|
|
|
|
remaining = stream.Length - stream.Position;
|
|
|
|
|
if (remaining != Marshal.SizeOf<SoftwareKeyboardCustomizeDic>())
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Customize Dic of {remaining} bytes");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var keyboardDicData = reader.ReadBytes((int)remaining);
|
|
|
|
|
_keyboardBackgroundDic = ReadStruct<SoftwareKeyboardCustomizeDic>(keyboardDicData);
|
|
|
|
|
}
|
|
|
|
|
_interactiveSession.Push(InlineResponses.UnsetCustomizeDic(state));
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.SetCustomizedDictionaries:
|
|
|
|
|
// Read the custom dictionaries data.
|
2021-01-11 13:27:55 -05:00
|
|
|
|
remaining = stream.Length - stream.Position;
|
|
|
|
|
if (remaining != Marshal.SizeOf<SoftwareKeyboardDictSet>())
|
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes");
|
2021-01-11 13:27:55 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var keyboardDictData = reader.ReadBytes((int)remaining);
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_keyboardBackgroundDictSet = ReadStruct<SoftwareKeyboardDictSet>(keyboardDictData);
|
2021-01-11 13:27:55 -05:00
|
|
|
|
}
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_interactiveSession.Push(InlineResponses.UnsetCustomizedDictionaries(state));
|
2021-01-11 13:27:55 -05:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.Calc:
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// The Calc request tells the Applet to enter the main input handling loop, which will end
|
|
|
|
|
// with either a text being submitted or a cancel request from the user.
|
|
|
|
|
|
2021-02-28 05:26:00 -05:00
|
|
|
|
// NOTE: Some Calc requests happen early in the application and are not meant to be shown. This possibly
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// happens because the game has complete control over when the inline keyboard is drawn, but here it
|
|
|
|
|
// would cause a dialog to pop in the emulator, which is inconvenient. An algorithm is applied to
|
|
|
|
|
// decide whether it is a dummy Calc or not, but regardless of the result, the dummy Calc appears to
|
|
|
|
|
// never happen twice, so the keyboard will always show if it has already been shown before.
|
2021-02-28 05:26:00 -05:00
|
|
|
|
bool shouldShowKeyboard = _alreadyShown;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_alreadyShown = true;
|
|
|
|
|
|
|
|
|
|
// Read the Calc data.
|
2021-01-11 13:27:55 -05:00
|
|
|
|
remaining = stream.Length - stream.Position;
|
|
|
|
|
if (remaining != Marshal.SizeOf<SoftwareKeyboardCalc>())
|
|
|
|
|
{
|
2021-02-10 19:28:44 -05:00
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes");
|
2021-01-11 13:27:55 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var keyboardCalcData = reader.ReadBytes((int)remaining);
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_keyboardBackgroundCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
|
2021-01-11 13:27:55 -05:00
|
|
|
|
|
2021-02-10 19:28:44 -05:00
|
|
|
|
// Check if the application expects UTF8 encoding instead of UTF16.
|
|
|
|
|
if (_keyboardBackgroundCalc.UseUtf8)
|
2021-01-11 13:27:55 -05:00
|
|
|
|
{
|
|
|
|
|
_encoding = Encoding.UTF8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Force showing the keyboard regardless of the state, an unwanted
|
|
|
|
|
// input dialog may show, but it is better than a soft lock.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
if (_keyboardBackgroundCalc.Appear.ShouldBeHidden == 0)
|
2021-01-11 13:27:55 -05:00
|
|
|
|
{
|
2021-02-28 05:26:00 -05:00
|
|
|
|
shouldShowKeyboard = true;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Send an initialization finished signal.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
state = InlineKeyboardState.Ready;
|
|
|
|
|
SetInlineState(state);
|
|
|
|
|
_interactiveSession.Push(InlineResponses.FinishedInitialize(state));
|
2021-01-11 13:27:55 -05:00
|
|
|
|
// Start a task with the GUI handler to get user's input.
|
2021-02-28 05:26:00 -05:00
|
|
|
|
new Task(() => { GetInputTextAndSend(shouldShowKeyboard, state); }).Start();
|
2021-01-11 13:27:55 -05:00
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.Finalize:
|
2021-02-28 05:26:00 -05:00
|
|
|
|
// The calling application wants to close the keyboard applet and will wait for a state change.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
_backgroundState = InlineKeyboardState.Uninitialized;
|
2021-01-11 13:27:55 -05:00
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// We shouldn't be able to get here through standard swkbd execution.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceAm, $"Invalid Software Keyboard request {request} during state {_backgroundState}");
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default(state));
|
2021-01-11 13:27:55 -05:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 05:26:00 -05:00
|
|
|
|
private void GetInputTextAndSend(bool shouldShowKeyboard, InlineKeyboardState oldState)
|
2021-02-10 19:28:44 -05:00
|
|
|
|
{
|
|
|
|
|
bool submit = true;
|
|
|
|
|
|
|
|
|
|
// Use the text specified by the Calc if it is available, otherwise use the default one.
|
|
|
|
|
string inputText = (!string.IsNullOrWhiteSpace(_keyboardBackgroundCalc.InputText) ?
|
|
|
|
|
_keyboardBackgroundCalc.InputText : DefaultText);
|
|
|
|
|
|
|
|
|
|
// Compute the elapsed time for the debouncing algorithm.
|
|
|
|
|
long currentMillis = PerformanceCounter.ElapsedMilliseconds;
|
|
|
|
|
long inputElapsedMillis = currentMillis - _lastTextSetMillis;
|
|
|
|
|
|
|
|
|
|
// Reset the input text before submitting the final result, that's because some games do not expect
|
|
|
|
|
// consecutive submissions to abruptly shrink and they will crash if it happens. Changing the string
|
|
|
|
|
// before the final submission prevents that.
|
|
|
|
|
InlineKeyboardState newState = InlineKeyboardState.DataAvailable;
|
|
|
|
|
SetInlineState(newState);
|
|
|
|
|
ChangedString("", newState);
|
|
|
|
|
|
2021-02-28 05:26:00 -05:00
|
|
|
|
if (!_lastWasHidden && (inputElapsedMillis < DebounceTimeMillis))
|
2021-02-10 19:28:44 -05:00
|
|
|
|
{
|
|
|
|
|
// A repeated Calc request has been received without player interaction, after the input has been
|
|
|
|
|
// sent. This behavior happens in some games, so instead of showing another dialog, just apply a
|
|
|
|
|
// time-based debouncing algorithm and repeat the last submission, either a value or a cancel.
|
2021-02-28 05:26:00 -05:00
|
|
|
|
// It is also possible that the first Calc request was hidden by accident, in this case use the
|
|
|
|
|
// debouncing as an oportunity to properly ask for input.
|
2021-02-10 19:28:44 -05:00
|
|
|
|
inputText = _textValue;
|
|
|
|
|
submit = _textValue != null;
|
2021-02-28 05:26:00 -05:00
|
|
|
|
_lastWasHidden = false;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "Debouncing repeated keyboard request");
|
|
|
|
|
}
|
2021-02-28 05:26:00 -05:00
|
|
|
|
else if (!shouldShowKeyboard)
|
2021-02-10 19:28:44 -05:00
|
|
|
|
{
|
|
|
|
|
// Submit the default text to avoid soft locking if the keyboard was ignored by
|
|
|
|
|
// accident. It's better to change the name than being locked out of the game.
|
|
|
|
|
inputText = DefaultText;
|
2021-02-28 05:26:00 -05:00
|
|
|
|
_lastWasHidden = true;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.Application, "Received a dummy Calc, keyboard will not be shown");
|
|
|
|
|
}
|
|
|
|
|
else if (_device.UiHandler == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
|
2021-02-28 05:26:00 -05:00
|
|
|
|
_lastWasHidden = false;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Call the configured GUI handler to get user's input.
|
|
|
|
|
var args = new SoftwareKeyboardUiArgs
|
|
|
|
|
{
|
|
|
|
|
HeaderText = "", // The inline keyboard lacks these texts
|
|
|
|
|
SubtitleText = "",
|
|
|
|
|
GuideText = "",
|
|
|
|
|
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardBackgroundCalc.Appear.OkText) ?
|
|
|
|
|
_keyboardBackgroundCalc.Appear.OkText : "OK"),
|
|
|
|
|
StringLengthMin = 0,
|
|
|
|
|
StringLengthMax = 100,
|
|
|
|
|
InitialText = inputText
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
submit = _device.UiHandler.DisplayInputDialog(args, out inputText);
|
|
|
|
|
inputText = submit ? inputText : null;
|
2021-02-28 05:26:00 -05:00
|
|
|
|
_lastWasHidden = false;
|
2021-02-10 19:28:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The 'Complete' state indicates the Calc request has been fulfilled by the applet.
|
|
|
|
|
newState = InlineKeyboardState.Complete;
|
|
|
|
|
|
|
|
|
|
if (submit)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard OK");
|
|
|
|
|
DecidedEnter(inputText, newState);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard Cancel");
|
|
|
|
|
DecidedCancel(newState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default(newState));
|
|
|
|
|
|
|
|
|
|
// The constant calls to PopInteractiveData suggest that the keyboard applet continuously reports
|
|
|
|
|
// data back to the application and this can also be time-sensitive. Pushing a state reset right
|
|
|
|
|
// after the data has been sent does not work properly and the application will soft-lock. This
|
|
|
|
|
// delay gives time for the application to catch up with the data and properly process the state
|
|
|
|
|
// reset.
|
|
|
|
|
Thread.Sleep(ResetDelayMillis);
|
|
|
|
|
|
|
|
|
|
// 'Initialized' is the only known state so far that does not soft-lock the keyboard after use.
|
|
|
|
|
newState = InlineKeyboardState.Initialized;
|
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, $"Resetting state of the keyboard to {newState}");
|
|
|
|
|
|
|
|
|
|
SetInlineState(newState);
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default(newState));
|
|
|
|
|
|
|
|
|
|
// Keep the text and the timestamp of the input for the debouncing algorithm.
|
|
|
|
|
_textValue = inputText;
|
|
|
|
|
_lastTextSetMillis = PerformanceCounter.ElapsedMilliseconds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChangedString(string text, InlineKeyboardState state)
|
|
|
|
|
{
|
|
|
|
|
if (_encoding == Encoding.UTF8)
|
|
|
|
|
{
|
|
|
|
|
if (_useChangedStringV2)
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.ChangedStringUtf8V2(text, state));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.ChangedStringUtf8(text, state));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (_useChangedStringV2)
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.ChangedStringV2(text, state));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.ChangedString(text, state));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 06:16:26 -05:00
|
|
|
|
private byte[] BuildResponse(string text, bool interactive)
|
|
|
|
|
{
|
2019-12-02 23:00:56 -05:00
|
|
|
|
int bufferSize = interactive ? InteractiveBufferSize : StandardBufferSize;
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[bufferSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2019-12-02 23:00:56 -05:00
|
|
|
|
byte[] output = _encoding.GetBytes(text);
|
2019-11-18 06:16:26 -05:00
|
|
|
|
|
|
|
|
|
if (!interactive)
|
|
|
|
|
{
|
|
|
|
|
// Result Code
|
2020-08-02 21:30:58 -04:00
|
|
|
|
writer.Write(_okPressed ? 0U : 1U);
|
2019-11-18 06:16:26 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-26 00:14:21 -05:00
|
|
|
|
// 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 06:16:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.Write(output);
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-03 02:53:06 -04:00
|
|
|
|
|
|
|
|
|
private static T ReadStruct<T>(byte[] data)
|
|
|
|
|
where T : struct
|
|
|
|
|
{
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
|
|
|
|
|
|
|
|
|
|
try
|
2021-01-11 13:27:55 -05:00
|
|
|
|
{
|
2020-04-03 02:53:06 -04:00
|
|
|
|
return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
handle.Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 06:16:26 -05:00
|
|
|
|
}
|
|
|
|
|
}
|