SoftwareKeyboardApplet.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
  2. using Ryujinx.HLE.HOS.Services.Am.AppletAE;
  3. using System;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Ryujinx.HLE.HOS.Applets
  8. {
  9. internal class SoftwareKeyboardApplet : IApplet
  10. {
  11. private const string DefaultNumb = "1";
  12. private const string DefaultText = "Ryujinx";
  13. private const int StandardBufferSize = 0x7D8;
  14. private const int InteractiveBufferSize = 0x7D4;
  15. private SoftwareKeyboardState _state = SoftwareKeyboardState.Uninitialized;
  16. private AppletSession _normalSession;
  17. private AppletSession _interactiveSession;
  18. private SoftwareKeyboardConfig _keyboardConfig;
  19. private string _textValue = DefaultText;
  20. private Encoding _encoding = Encoding.Unicode;
  21. public event EventHandler AppletStateChanged;
  22. public SoftwareKeyboardApplet(Horizon system) { }
  23. public ResultCode Start(AppletSession normalSession,
  24. AppletSession interactiveSession)
  25. {
  26. _normalSession = normalSession;
  27. _interactiveSession = interactiveSession;
  28. _interactiveSession.DataAvailable += OnInteractiveData;
  29. var launchParams = _normalSession.Pop();
  30. var keyboardConfig = _normalSession.Pop();
  31. var transferMemory = _normalSession.Pop();
  32. _keyboardConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
  33. if (_keyboardConfig.UseUtf8)
  34. {
  35. _encoding = Encoding.UTF8;
  36. }
  37. _state = SoftwareKeyboardState.Ready;
  38. Execute();
  39. return ResultCode.Success;
  40. }
  41. public ResultCode GetResult()
  42. {
  43. return ResultCode.Success;
  44. }
  45. private void Execute()
  46. {
  47. // If the keyboard type is numbers only, we swap to a default
  48. // text that only contains numbers.
  49. if (_keyboardConfig.Mode == KeyboardMode.NumbersOnly)
  50. {
  51. _textValue = DefaultNumb;
  52. }
  53. // If the max string length is 0, we set it to a large default
  54. // length.
  55. if (_keyboardConfig.StringLengthMax == 0)
  56. {
  57. _keyboardConfig.StringLengthMax = 100;
  58. }
  59. // If the game requests a string with a minimum length less
  60. // than our default text, repeat our default text until we meet
  61. // the minimum length requirement.
  62. // This should always be done before the text truncation step.
  63. while (_textValue.Length < _keyboardConfig.StringLengthMin)
  64. {
  65. _textValue = String.Join(" ", _textValue, _textValue);
  66. }
  67. // If our default text is longer than the allowed length,
  68. // we truncate it.
  69. if (_textValue.Length > _keyboardConfig.StringLengthMax)
  70. {
  71. _textValue = _textValue.Substring(0, (int)_keyboardConfig.StringLengthMax);
  72. }
  73. // Does the application want to validate the text itself?
  74. if (_keyboardConfig.CheckText)
  75. {
  76. // The application needs to validate the response, so we
  77. // submit it to the interactive output buffer, and poll it
  78. // for validation. Once validated, the application will submit
  79. // back a validation status, which is handled in OnInteractiveDataPushIn.
  80. _state = SoftwareKeyboardState.ValidationPending;
  81. _interactiveSession.Push(BuildResponse(_textValue, true));
  82. }
  83. else
  84. {
  85. // If the application doesn't need to validate the response,
  86. // we push the data to the non-interactive output buffer
  87. // and poll it for completion.
  88. _state = SoftwareKeyboardState.Complete;
  89. _normalSession.Push(BuildResponse(_textValue, false));
  90. AppletStateChanged?.Invoke(this, null);
  91. }
  92. }
  93. private void OnInteractiveData(object sender, EventArgs e)
  94. {
  95. // Obtain the validation status response,
  96. var data = _interactiveSession.Pop();
  97. if (_state == SoftwareKeyboardState.ValidationPending)
  98. {
  99. // TODO(jduncantor):
  100. // If application rejects our "attempt", submit another attempt,
  101. // and put the applet back in PendingValidation state.
  102. // For now we assume success, so we push the final result
  103. // to the standard output buffer and carry on our merry way.
  104. _normalSession.Push(BuildResponse(_textValue, false));
  105. AppletStateChanged?.Invoke(this, null);
  106. _state = SoftwareKeyboardState.Complete;
  107. }
  108. else if(_state == SoftwareKeyboardState.Complete)
  109. {
  110. // If we have already completed, we push the result text
  111. // back on the output buffer and poll the application.
  112. _normalSession.Push(BuildResponse(_textValue, false));
  113. AppletStateChanged?.Invoke(this, null);
  114. }
  115. else
  116. {
  117. // We shouldn't be able to get here through standard swkbd execution.
  118. throw new InvalidOperationException("Software Keyboard is in an invalid state.");
  119. }
  120. }
  121. private byte[] BuildResponse(string text, bool interactive)
  122. {
  123. int bufferSize = interactive ? InteractiveBufferSize : StandardBufferSize;
  124. using (MemoryStream stream = new MemoryStream(new byte[bufferSize]))
  125. using (BinaryWriter writer = new BinaryWriter(stream))
  126. {
  127. byte[] output = _encoding.GetBytes(text);
  128. if (!interactive)
  129. {
  130. // Result Code
  131. writer.Write((uint)0);
  132. }
  133. else
  134. {
  135. // In interactive mode, we write the length of the text as a long, rather than
  136. // a result code. This field is inclusive of the 64-bit size.
  137. writer.Write((long)output.Length + 8);
  138. }
  139. writer.Write(output);
  140. return stream.ToArray();
  141. }
  142. }
  143. private static T ReadStruct<T>(byte[] data)
  144. where T : struct
  145. {
  146. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  147. try
  148. {
  149. return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
  150. }
  151. finally
  152. {
  153. handle.Free();
  154. }
  155. }
  156. }
  157. }