2020-08-03 03:30:58 +02:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Am.AppletAE;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Applets
|
|
|
|
|
{
|
|
|
|
|
internal class SoftwareKeyboardApplet : IApplet
|
|
|
|
|
{
|
2019-12-03 05:00:56 +01:00
|
|
|
|
private const string DefaultText = "Ryujinx";
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
private readonly Switch _device;
|
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
private const int StandardBufferSize = 0x7D8;
|
|
|
|
|
private const int InteractiveBufferSize = 0x7D4;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
private SoftwareKeyboardState _state = SoftwareKeyboardState.Uninitialized;
|
|
|
|
|
|
|
|
|
|
private AppletSession _normalSession;
|
|
|
|
|
private AppletSession _interactiveSession;
|
|
|
|
|
|
|
|
|
|
private SoftwareKeyboardConfig _keyboardConfig;
|
2020-08-03 03:30:58 +02:00
|
|
|
|
private byte[] _transferMemory;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
private string _textValue = null;
|
|
|
|
|
private bool _okPressed = false;
|
2019-12-03 05:00:56 +01:00
|
|
|
|
private Encoding _encoding = Encoding.Unicode;
|
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
|
|
|
|
|
|
|
|
|
public ResultCode Start(AppletSession normalSession,
|
|
|
|
|
AppletSession interactiveSession)
|
|
|
|
|
{
|
|
|
|
|
_normalSession = normalSession;
|
|
|
|
|
_interactiveSession = interactiveSession;
|
|
|
|
|
|
|
|
|
|
_interactiveSession.DataAvailable += OnInteractiveData;
|
|
|
|
|
|
|
|
|
|
var launchParams = _normalSession.Pop();
|
|
|
|
|
var keyboardConfig = _normalSession.Pop();
|
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
|
2020-08-03 03:30:58 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_keyboardConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_normalSession.TryPop(out _transferMemory))
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
|
2020-08-03 03:30:58 +02:00
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
if (_keyboardConfig.UseUtf8)
|
|
|
|
|
{
|
|
|
|
|
_encoding = Encoding.UTF8;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 12:16:26 +01:00
|
|
|
|
_state = SoftwareKeyboardState.Ready;
|
|
|
|
|
|
|
|
|
|
Execute();
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode GetResult()
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Execute()
|
|
|
|
|
{
|
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
|
|
|
|
|
if (_transferMemory != null && _keyboardConfig.InitialStringLength > 0)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2020-08-03 03:30:58 +02:00
|
|
|
|
initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardConfig.InitialStringOffset, 2 * _keyboardConfig.InitialStringLength);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the max string length is 0, we set it to a large default
|
|
|
|
|
// length.
|
|
|
|
|
if (_keyboardConfig.StringLengthMax == 0)
|
|
|
|
|
{
|
|
|
|
|
_keyboardConfig.StringLengthMax = 100;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
var args = new SoftwareKeyboardUiArgs
|
|
|
|
|
{
|
|
|
|
|
HeaderText = _keyboardConfig.HeaderText,
|
|
|
|
|
SubtitleText = _keyboardConfig.SubtitleText,
|
|
|
|
|
GuideText = _keyboardConfig.GuideText,
|
|
|
|
|
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardConfig.SubmitText) ? _keyboardConfig.SubmitText : "OK"),
|
|
|
|
|
StringLengthMin = _keyboardConfig.StringLengthMin,
|
|
|
|
|
StringLengthMax = _keyboardConfig.StringLengthMax,
|
|
|
|
|
InitialText = initialText
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Call the configured GUI handler to get user's input
|
|
|
|
|
if (_device.UiHandler == null)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Warning?.Print(LogClass.Application, $"GUI Handler is not set. Falling back to default");
|
2020-08-03 03:30:58 +02:00
|
|
|
|
_okPressed = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_okPressed = _device.UiHandler.DisplayInputDialog(args, out _textValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_textValue ??= initialText ?? DefaultText;
|
|
|
|
|
|
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
|
|
|
|
|
// the minimum length requirement.
|
|
|
|
|
// This should always be done before the text truncation step.
|
|
|
|
|
while (_textValue.Length < _keyboardConfig.StringLengthMin)
|
|
|
|
|
{
|
|
|
|
|
_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.
|
|
|
|
|
if (_textValue.Length > _keyboardConfig.StringLengthMax)
|
|
|
|
|
{
|
|
|
|
|
_textValue = _textValue.Substring(0, (int)_keyboardConfig.StringLengthMax);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
// Does the application want to validate the text itself?
|
|
|
|
|
if (_keyboardConfig.CheckText)
|
|
|
|
|
{
|
|
|
|
|
// 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.
|
|
|
|
|
_state = SoftwareKeyboardState.ValidationPending;
|
|
|
|
|
|
|
|
|
|
_interactiveSession.Push(BuildResponse(_textValue, true));
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
// and poll it for completion.
|
|
|
|
|
_state = SoftwareKeyboardState.Complete;
|
|
|
|
|
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnInteractiveData(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Obtain the validation status response,
|
|
|
|
|
var data = _interactiveSession.Pop();
|
|
|
|
|
|
|
|
|
|
if (_state == SoftwareKeyboardState.ValidationPending)
|
|
|
|
|
{
|
|
|
|
|
// TODO(jduncantor):
|
|
|
|
|
// If application rejects our "attempt", submit another attempt,
|
|
|
|
|
// and put the applet back in PendingValidation state.
|
|
|
|
|
|
|
|
|
|
// For now we assume success, so we push the final result
|
|
|
|
|
// to the standard output buffer and carry on our merry way.
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
|
|
|
|
|
_state = SoftwareKeyboardState.Complete;
|
|
|
|
|
}
|
|
|
|
|
else if(_state == SoftwareKeyboardState.Complete)
|
|
|
|
|
{
|
|
|
|
|
// 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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] BuildResponse(string text, bool interactive)
|
|
|
|
|
{
|
2019-12-03 05:00:56 +01: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))
|
|
|
|
|
{
|
2019-12-03 05:00:56 +01:00
|
|
|
|
byte[] output = _encoding.GetBytes(text);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
if (!interactive)
|
|
|
|
|
{
|
|
|
|
|
// Result Code
|
2020-08-03 03:30:58 +02:00
|
|
|
|
writer.Write(_okPressed ? 0U : 1U);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-26 06:14:21 +01: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 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.Write(output);
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-03 08:53:06 +02:00
|
|
|
|
|
|
|
|
|
private static T ReadStruct<T>(byte[] data)
|
|
|
|
|
where T : struct
|
|
|
|
|
{
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
handle.Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|