SoftwareKeyboardApplet.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Configuration.Hid;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
  5. using Ryujinx.HLE.HOS.Services.Am.AppletAE;
  6. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Npad;
  7. using Ryujinx.HLE.Ui;
  8. using Ryujinx.HLE.Ui.Input;
  9. using Ryujinx.Memory;
  10. using System;
  11. using System.Diagnostics;
  12. using System.Diagnostics.CodeAnalysis;
  13. using System.IO;
  14. using System.Runtime.CompilerServices;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. namespace Ryujinx.HLE.HOS.Applets
  18. {
  19. internal class SoftwareKeyboardApplet : IApplet
  20. {
  21. private const string DefaultInputText = "Ryujinx";
  22. private const int StandardBufferSize = 0x7D8;
  23. private const int InteractiveBufferSize = 0x7D4;
  24. private const int MaxUserWords = 0x1388;
  25. private const int MaxUiTextSize = 100;
  26. private const Key CycleInputModesKey = Key.F6;
  27. private readonly Switch _device;
  28. private SoftwareKeyboardState _foregroundState = SoftwareKeyboardState.Uninitialized;
  29. private volatile InlineKeyboardState _backgroundState = InlineKeyboardState.Uninitialized;
  30. private bool _isBackground = false;
  31. private AppletSession _normalSession;
  32. private AppletSession _interactiveSession;
  33. // Configuration for foreground mode.
  34. private SoftwareKeyboardConfig _keyboardForegroundConfig;
  35. // Configuration for background (inline) mode.
  36. private SoftwareKeyboardInitialize _keyboardBackgroundInitialize;
  37. private SoftwareKeyboardCustomizeDic _keyboardBackgroundDic;
  38. private SoftwareKeyboardDictSet _keyboardBackgroundDictSet;
  39. private SoftwareKeyboardUserWord[] _keyboardBackgroundUserWords;
  40. private byte[] _transferMemory;
  41. private string _textValue = "";
  42. private int _cursorBegin = 0;
  43. private Encoding _encoding = Encoding.Unicode;
  44. private KeyboardResult _lastResult = KeyboardResult.NotSet;
  45. private IDynamicTextInputHandler _dynamicTextInputHandler = null;
  46. private SoftwareKeyboardRenderer _keyboardRenderer = null;
  47. private NpadReader _npads = null;
  48. private bool _canAcceptController = false;
  49. private KeyboardInputMode _inputMode = KeyboardInputMode.ControllerAndKeyboard;
  50. private readonly object _lock = new();
  51. public event EventHandler AppletStateChanged;
  52. public SoftwareKeyboardApplet(Horizon system)
  53. {
  54. _device = system.Device;
  55. }
  56. public ResultCode Start(AppletSession normalSession, AppletSession interactiveSession)
  57. {
  58. lock (_lock)
  59. {
  60. _normalSession = normalSession;
  61. _interactiveSession = interactiveSession;
  62. _interactiveSession.DataAvailable += OnInteractiveData;
  63. var launchParams = _normalSession.Pop();
  64. var keyboardConfig = _normalSession.Pop();
  65. _isBackground = keyboardConfig.Length == Unsafe.SizeOf<SoftwareKeyboardInitialize>();
  66. if (_isBackground)
  67. {
  68. // Initialize the keyboard applet in background mode.
  69. _keyboardBackgroundInitialize = MemoryMarshal.Read<SoftwareKeyboardInitialize>(keyboardConfig);
  70. _backgroundState = InlineKeyboardState.Uninitialized;
  71. if (_device.UiHandler == null)
  72. {
  73. Logger.Error?.Print(LogClass.ServiceAm, "GUI Handler is not set, software keyboard applet will not work properly");
  74. }
  75. else
  76. {
  77. // Create a text handler that converts keyboard strokes to strings.
  78. _dynamicTextInputHandler = _device.UiHandler.CreateDynamicTextInputHandler();
  79. _dynamicTextInputHandler.TextChangedEvent += HandleTextChangedEvent;
  80. _dynamicTextInputHandler.KeyPressedEvent += HandleKeyPressedEvent;
  81. _npads = new NpadReader(_device);
  82. _npads.NpadButtonDownEvent += HandleNpadButtonDownEvent;
  83. _npads.NpadButtonUpEvent += HandleNpadButtonUpEvent;
  84. _keyboardRenderer = new SoftwareKeyboardRenderer(_device.UiHandler.HostUiTheme);
  85. }
  86. return ResultCode.Success;
  87. }
  88. else
  89. {
  90. // Initialize the keyboard applet in foreground mode.
  91. if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
  92. {
  93. Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
  94. }
  95. else
  96. {
  97. _keyboardForegroundConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
  98. }
  99. if (!_normalSession.TryPop(out _transferMemory))
  100. {
  101. Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
  102. }
  103. if (_keyboardForegroundConfig.UseUtf8)
  104. {
  105. _encoding = Encoding.UTF8;
  106. }
  107. _foregroundState = SoftwareKeyboardState.Ready;
  108. ExecuteForegroundKeyboard();
  109. return ResultCode.Success;
  110. }
  111. }
  112. }
  113. public ResultCode GetResult()
  114. {
  115. return ResultCode.Success;
  116. }
  117. private bool IsKeyboardActive()
  118. {
  119. return _backgroundState >= InlineKeyboardState.Appearing && _backgroundState < InlineKeyboardState.Disappearing;
  120. }
  121. private bool InputModeControllerEnabled()
  122. {
  123. return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
  124. _inputMode == KeyboardInputMode.ControllerOnly;
  125. }
  126. private bool InputModeTypingEnabled()
  127. {
  128. return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
  129. _inputMode == KeyboardInputMode.KeyboardOnly;
  130. }
  131. private void AdvanceInputMode()
  132. {
  133. _inputMode = (KeyboardInputMode)((int)(_inputMode + 1) % (int)KeyboardInputMode.Count);
  134. }
  135. public bool DrawTo(RenderingSurfaceInfo surfaceInfo, IVirtualMemoryManager destination, ulong position)
  136. {
  137. _npads?.Update();
  138. _keyboardRenderer?.SetSurfaceInfo(surfaceInfo);
  139. return _keyboardRenderer?.DrawTo(destination, position) ?? false;
  140. }
  141. private void ExecuteForegroundKeyboard()
  142. {
  143. string initialText = null;
  144. // Initial Text is always encoded as a UTF-16 string in the work buffer (passed as transfer memory)
  145. // InitialStringOffset points to the memory offset and InitialStringLength is the number of UTF-16 characters
  146. if (_transferMemory != null && _keyboardForegroundConfig.InitialStringLength > 0)
  147. {
  148. initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardForegroundConfig.InitialStringOffset,
  149. 2 * _keyboardForegroundConfig.InitialStringLength);
  150. }
  151. // If the max string length is 0, we set it to a large default
  152. // length.
  153. if (_keyboardForegroundConfig.StringLengthMax == 0)
  154. {
  155. _keyboardForegroundConfig.StringLengthMax = 100;
  156. }
  157. if (_device.UiHandler == null)
  158. {
  159. Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
  160. _textValue = DefaultInputText;
  161. _lastResult = KeyboardResult.Accept;
  162. }
  163. else
  164. {
  165. // Call the configured GUI handler to get user's input.
  166. var args = new SoftwareKeyboardUiArgs
  167. {
  168. KeyboardMode = _keyboardForegroundConfig.Mode,
  169. HeaderText = StripUnicodeControlCodes(_keyboardForegroundConfig.HeaderText),
  170. SubtitleText = StripUnicodeControlCodes(_keyboardForegroundConfig.SubtitleText),
  171. GuideText = StripUnicodeControlCodes(_keyboardForegroundConfig.GuideText),
  172. SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
  173. _keyboardForegroundConfig.SubmitText : "OK"),
  174. StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
  175. StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
  176. InitialText = initialText
  177. };
  178. _lastResult = _device.UiHandler.DisplayInputDialog(args, out _textValue) ? KeyboardResult.Accept : KeyboardResult.Cancel;
  179. _textValue ??= initialText ?? DefaultInputText;
  180. }
  181. // If the game requests a string with a minimum length less
  182. // than our default text, repeat our default text until we meet
  183. // the minimum length requirement.
  184. // This should always be done before the text truncation step.
  185. while (_textValue.Length < _keyboardForegroundConfig.StringLengthMin)
  186. {
  187. _textValue = String.Join(" ", _textValue, _textValue);
  188. }
  189. // If our default text is longer than the allowed length,
  190. // we truncate it.
  191. if (_textValue.Length > _keyboardForegroundConfig.StringLengthMax)
  192. {
  193. _textValue = _textValue.Substring(0, _keyboardForegroundConfig.StringLengthMax);
  194. }
  195. // Does the application want to validate the text itself?
  196. if (_keyboardForegroundConfig.CheckText)
  197. {
  198. // The application needs to validate the response, so we
  199. // submit it to the interactive output buffer, and poll it
  200. // for validation. Once validated, the application will submit
  201. // back a validation status, which is handled in OnInteractiveDataPushIn.
  202. _foregroundState = SoftwareKeyboardState.ValidationPending;
  203. PushForegroundResponse(true);
  204. }
  205. else
  206. {
  207. // If the application doesn't need to validate the response,
  208. // we push the data to the non-interactive output buffer
  209. // and poll it for completion.
  210. _foregroundState = SoftwareKeyboardState.Complete;
  211. PushForegroundResponse(false);
  212. AppletStateChanged?.Invoke(this, null);
  213. }
  214. }
  215. private void OnInteractiveData(object sender, EventArgs e)
  216. {
  217. // Obtain the validation status response.
  218. var data = _interactiveSession.Pop();
  219. if (_isBackground)
  220. {
  221. lock (_lock)
  222. {
  223. OnBackgroundInteractiveData(data);
  224. }
  225. }
  226. else
  227. {
  228. OnForegroundInteractiveData(data);
  229. }
  230. }
  231. private void OnForegroundInteractiveData(byte[] data)
  232. {
  233. if (_foregroundState == SoftwareKeyboardState.ValidationPending)
  234. {
  235. // TODO(jduncantor):
  236. // If application rejects our "attempt", submit another attempt,
  237. // and put the applet back in PendingValidation state.
  238. // For now we assume success, so we push the final result
  239. // to the standard output buffer and carry on our merry way.
  240. PushForegroundResponse(false);
  241. AppletStateChanged?.Invoke(this, null);
  242. _foregroundState = SoftwareKeyboardState.Complete;
  243. }
  244. else if (_foregroundState == SoftwareKeyboardState.Complete)
  245. {
  246. // If we have already completed, we push the result text
  247. // back on the output buffer and poll the application.
  248. PushForegroundResponse(false);
  249. AppletStateChanged?.Invoke(this, null);
  250. }
  251. else
  252. {
  253. // We shouldn't be able to get here through standard swkbd execution.
  254. throw new InvalidOperationException("Software Keyboard is in an invalid state.");
  255. }
  256. }
  257. private void OnBackgroundInteractiveData(byte[] data)
  258. {
  259. // WARNING: Only invoke applet state changes after an explicit finalization
  260. // request from the game, this is because the inline keyboard is expected to
  261. // keep running in the background sending data by itself.
  262. using (MemoryStream stream = new MemoryStream(data))
  263. using (BinaryReader reader = new BinaryReader(stream))
  264. {
  265. var request = (InlineKeyboardRequest)reader.ReadUInt32();
  266. long remaining;
  267. Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {_backgroundState}");
  268. switch (request)
  269. {
  270. case InlineKeyboardRequest.UseChangedStringV2:
  271. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseChangedStringV2");
  272. break;
  273. case InlineKeyboardRequest.UseMovedCursorV2:
  274. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseMovedCursorV2");
  275. break;
  276. case InlineKeyboardRequest.SetUserWordInfo:
  277. // Read the user word info data.
  278. remaining = stream.Length - stream.Position;
  279. if (remaining < sizeof(int))
  280. {
  281. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info of {remaining} bytes");
  282. }
  283. else
  284. {
  285. int wordsCount = reader.ReadInt32();
  286. int wordSize = Unsafe.SizeOf<SoftwareKeyboardUserWord>();
  287. remaining = stream.Length - stream.Position;
  288. if (wordsCount > MaxUserWords)
  289. {
  290. Logger.Warning?.Print(LogClass.ServiceAm, $"Received {wordsCount} User Words but the maximum is {MaxUserWords}");
  291. }
  292. else if (wordsCount * wordSize != remaining)
  293. {
  294. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info data of {remaining} bytes for {wordsCount} words");
  295. }
  296. else
  297. {
  298. _keyboardBackgroundUserWords = new SoftwareKeyboardUserWord[wordsCount];
  299. for (int word = 0; word < wordsCount; word++)
  300. {
  301. _keyboardBackgroundUserWords[word] = reader.ReadStruct<SoftwareKeyboardUserWord>();
  302. }
  303. }
  304. }
  305. _interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(_backgroundState));
  306. break;
  307. case InlineKeyboardRequest.SetCustomizeDic:
  308. // Read the custom dic data.
  309. remaining = stream.Length - stream.Position;
  310. if (remaining != Unsafe.SizeOf<SoftwareKeyboardCustomizeDic>())
  311. {
  312. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Customize Dic of {remaining} bytes");
  313. }
  314. else
  315. {
  316. _keyboardBackgroundDic = reader.ReadStruct<SoftwareKeyboardCustomizeDic>();
  317. }
  318. break;
  319. case InlineKeyboardRequest.SetCustomizedDictionaries:
  320. // Read the custom dictionaries data.
  321. remaining = stream.Length - stream.Position;
  322. if (remaining != Unsafe.SizeOf<SoftwareKeyboardDictSet>())
  323. {
  324. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes");
  325. }
  326. else
  327. {
  328. _keyboardBackgroundDictSet = reader.ReadStruct<SoftwareKeyboardDictSet>();
  329. }
  330. break;
  331. case InlineKeyboardRequest.Calc:
  332. // The Calc request is used to communicate configuration changes and commands to the keyboard.
  333. // Fields in the Calc struct and operations are masked by the Flags field.
  334. // Read the Calc data.
  335. SoftwareKeyboardCalcEx newCalc;
  336. remaining = stream.Length - stream.Position;
  337. if (remaining == Marshal.SizeOf<SoftwareKeyboardCalc>())
  338. {
  339. var keyboardCalcData = reader.ReadBytes((int)remaining);
  340. var keyboardCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
  341. newCalc = keyboardCalc.ToExtended();
  342. }
  343. else if (remaining == Marshal.SizeOf<SoftwareKeyboardCalcEx>() || remaining == SoftwareKeyboardCalcEx.AlternativeSize)
  344. {
  345. var keyboardCalcData = reader.ReadBytes((int)remaining);
  346. newCalc = ReadStruct<SoftwareKeyboardCalcEx>(keyboardCalcData);
  347. }
  348. else
  349. {
  350. Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes");
  351. newCalc = new SoftwareKeyboardCalcEx();
  352. }
  353. // Process each individual operation specified in the flags.
  354. bool updateText = false;
  355. if ((newCalc.Flags & KeyboardCalcFlags.Initialize) != 0)
  356. {
  357. _interactiveSession.Push(InlineResponses.FinishedInitialize(_backgroundState));
  358. _backgroundState = InlineKeyboardState.Initialized;
  359. }
  360. if ((newCalc.Flags & KeyboardCalcFlags.SetCursorPos) != 0)
  361. {
  362. _cursorBegin = newCalc.CursorPos;
  363. updateText = true;
  364. Logger.Debug?.Print(LogClass.ServiceAm, $"Cursor position set to {_cursorBegin}");
  365. }
  366. if ((newCalc.Flags & KeyboardCalcFlags.SetInputText) != 0)
  367. {
  368. _textValue = newCalc.InputText;
  369. updateText = true;
  370. Logger.Debug?.Print(LogClass.ServiceAm, $"Input text set to {_textValue}");
  371. }
  372. if ((newCalc.Flags & KeyboardCalcFlags.SetUtf8Mode) != 0)
  373. {
  374. _encoding = newCalc.UseUtf8 ? Encoding.UTF8 : Encoding.Default;
  375. Logger.Debug?.Print(LogClass.ServiceAm, $"Encoding set to {_encoding}");
  376. }
  377. if (updateText)
  378. {
  379. _dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
  380. _keyboardRenderer.UpdateTextState(_textValue, _cursorBegin, _cursorBegin, null, null);
  381. }
  382. if ((newCalc.Flags & KeyboardCalcFlags.MustShow) != 0)
  383. {
  384. ActivateFrontend();
  385. _backgroundState = InlineKeyboardState.Shown;
  386. PushChangedString(_textValue, (uint)_cursorBegin, _backgroundState);
  387. }
  388. // Send the response to the Calc
  389. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  390. break;
  391. case InlineKeyboardRequest.Finalize:
  392. // Destroy the frontend.
  393. DestroyFrontend();
  394. // The calling application wants to close the keyboard applet and will wait for a state change.
  395. _backgroundState = InlineKeyboardState.Uninitialized;
  396. AppletStateChanged?.Invoke(this, null);
  397. break;
  398. default:
  399. // We shouldn't be able to get here through standard swkbd execution.
  400. Logger.Warning?.Print(LogClass.ServiceAm, $"Invalid Software Keyboard request {request} during state {_backgroundState}");
  401. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  402. break;
  403. }
  404. }
  405. }
  406. private void ActivateFrontend()
  407. {
  408. Logger.Debug?.Print(LogClass.ServiceAm, $"Activating software keyboard frontend");
  409. _inputMode = KeyboardInputMode.ControllerAndKeyboard;
  410. _npads.Update(true);
  411. NpadButton buttons = _npads.GetCurrentButtonsOfAllNpads();
  412. // Block the input if the current accept key is pressed so the applet won't be instantly closed.
  413. _canAcceptController = (buttons & NpadButton.A) == 0;
  414. _dynamicTextInputHandler.TextProcessingEnabled = true;
  415. _keyboardRenderer.UpdateCommandState(null, null, true);
  416. _keyboardRenderer.UpdateTextState(null, null, null, null, true);
  417. }
  418. private void DeactivateFrontend()
  419. {
  420. Logger.Debug?.Print(LogClass.ServiceAm, $"Deactivating software keyboard frontend");
  421. _inputMode = KeyboardInputMode.ControllerAndKeyboard;
  422. _canAcceptController = false;
  423. _dynamicTextInputHandler.TextProcessingEnabled = false;
  424. _dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
  425. }
  426. private void DestroyFrontend()
  427. {
  428. Logger.Debug?.Print(LogClass.ServiceAm, $"Destroying software keyboard frontend");
  429. _keyboardRenderer?.Dispose();
  430. _keyboardRenderer = null;
  431. if (_dynamicTextInputHandler != null)
  432. {
  433. _dynamicTextInputHandler.TextChangedEvent -= HandleTextChangedEvent;
  434. _dynamicTextInputHandler.KeyPressedEvent -= HandleKeyPressedEvent;
  435. _dynamicTextInputHandler.Dispose();
  436. _dynamicTextInputHandler = null;
  437. }
  438. if (_npads != null)
  439. {
  440. _npads.NpadButtonDownEvent -= HandleNpadButtonDownEvent;
  441. _npads.NpadButtonUpEvent -= HandleNpadButtonUpEvent;
  442. _npads = null;
  443. }
  444. }
  445. private bool HandleKeyPressedEvent(Key key)
  446. {
  447. if (key == CycleInputModesKey)
  448. {
  449. lock (_lock)
  450. {
  451. if (IsKeyboardActive())
  452. {
  453. AdvanceInputMode();
  454. bool typingEnabled = InputModeTypingEnabled();
  455. bool controllerEnabled = InputModeControllerEnabled();
  456. _dynamicTextInputHandler.TextProcessingEnabled = typingEnabled;
  457. _keyboardRenderer.UpdateTextState(null, null, null, null, typingEnabled);
  458. _keyboardRenderer.UpdateCommandState(null, null, controllerEnabled);
  459. }
  460. }
  461. }
  462. return true;
  463. }
  464. private void HandleTextChangedEvent(string text, int cursorBegin, int cursorEnd, bool overwriteMode)
  465. {
  466. lock (_lock)
  467. {
  468. // Text processing should not run with typing disabled.
  469. Debug.Assert(InputModeTypingEnabled());
  470. if (text.Length > MaxUiTextSize)
  471. {
  472. // Limit the text size and change it back.
  473. text = text.Substring(0, MaxUiTextSize);
  474. cursorBegin = Math.Min(cursorBegin, MaxUiTextSize);
  475. cursorEnd = Math.Min(cursorEnd, MaxUiTextSize);
  476. _dynamicTextInputHandler.SetText(text, cursorBegin, cursorEnd);
  477. }
  478. _textValue = text;
  479. _cursorBegin = cursorBegin;
  480. _keyboardRenderer.UpdateTextState(text, cursorBegin, cursorEnd, overwriteMode, null);
  481. PushUpdatedState(text, cursorBegin, KeyboardResult.NotSet);
  482. }
  483. }
  484. private void HandleNpadButtonDownEvent(int npadIndex, NpadButton button)
  485. {
  486. lock (_lock)
  487. {
  488. if (!IsKeyboardActive())
  489. {
  490. return;
  491. }
  492. switch (button)
  493. {
  494. case NpadButton.A:
  495. _keyboardRenderer.UpdateCommandState(_canAcceptController, null, null);
  496. break;
  497. case NpadButton.B:
  498. _keyboardRenderer.UpdateCommandState(null, _canAcceptController, null);
  499. break;
  500. }
  501. }
  502. }
  503. private void HandleNpadButtonUpEvent(int npadIndex, NpadButton button)
  504. {
  505. lock (_lock)
  506. {
  507. KeyboardResult result = KeyboardResult.NotSet;
  508. switch (button)
  509. {
  510. case NpadButton.A:
  511. result = KeyboardResult.Accept;
  512. _keyboardRenderer.UpdateCommandState(false, null, null);
  513. break;
  514. case NpadButton.B:
  515. result = KeyboardResult.Cancel;
  516. _keyboardRenderer.UpdateCommandState(null, false, null);
  517. break;
  518. }
  519. if (IsKeyboardActive())
  520. {
  521. if (!_canAcceptController)
  522. {
  523. _canAcceptController = true;
  524. }
  525. else if (InputModeControllerEnabled())
  526. {
  527. PushUpdatedState(_textValue, _cursorBegin, result);
  528. }
  529. }
  530. }
  531. }
  532. private void PushUpdatedState(string text, int cursorBegin, KeyboardResult result)
  533. {
  534. _lastResult = result;
  535. _textValue = text;
  536. bool cancel = result == KeyboardResult.Cancel;
  537. bool accept = result == KeyboardResult.Accept;
  538. if (!IsKeyboardActive())
  539. {
  540. // Keyboard is not active.
  541. return;
  542. }
  543. if (accept == false && cancel == false)
  544. {
  545. Logger.Debug?.Print(LogClass.ServiceAm, $"Updating keyboard text to {text} and cursor position to {cursorBegin}");
  546. PushChangedString(text, (uint)cursorBegin, _backgroundState);
  547. }
  548. else
  549. {
  550. // Disable the frontend.
  551. DeactivateFrontend();
  552. // The 'Complete' state indicates the Calc request has been fulfilled by the applet.
  553. _backgroundState = InlineKeyboardState.Disappearing;
  554. if (accept)
  555. {
  556. Logger.Debug?.Print(LogClass.ServiceAm, $"Sending keyboard OK with text {text}");
  557. DecidedEnter(text, _backgroundState);
  558. }
  559. else if (cancel)
  560. {
  561. Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard Cancel");
  562. DecidedCancel(_backgroundState);
  563. }
  564. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  565. Logger.Debug?.Print(LogClass.ServiceAm, $"Resetting state of the keyboard to {_backgroundState}");
  566. // Set the state of the applet to 'Initialized' as it is the only known state so far
  567. // that does not soft-lock the keyboard after use.
  568. _backgroundState = InlineKeyboardState.Initialized;
  569. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  570. }
  571. }
  572. private void PushChangedString(string text, uint cursor, InlineKeyboardState state)
  573. {
  574. // TODO (Caian): The *V2 methods are not supported because the applications that request
  575. // them do not seem to accept them. The regular methods seem to work just fine in all cases.
  576. if (_encoding == Encoding.UTF8)
  577. {
  578. _interactiveSession.Push(InlineResponses.ChangedStringUtf8(text, cursor, state));
  579. }
  580. else
  581. {
  582. _interactiveSession.Push(InlineResponses.ChangedString(text, cursor, state));
  583. }
  584. }
  585. private void DecidedEnter(string text, InlineKeyboardState state)
  586. {
  587. if (_encoding == Encoding.UTF8)
  588. {
  589. _interactiveSession.Push(InlineResponses.DecidedEnterUtf8(text, state));
  590. }
  591. else
  592. {
  593. _interactiveSession.Push(InlineResponses.DecidedEnter(text, state));
  594. }
  595. }
  596. private void DecidedCancel(InlineKeyboardState state)
  597. {
  598. _interactiveSession.Push(InlineResponses.DecidedCancel(state));
  599. }
  600. private void PushForegroundResponse(bool interactive)
  601. {
  602. int bufferSize = interactive ? InteractiveBufferSize : StandardBufferSize;
  603. using (MemoryStream stream = new MemoryStream(new byte[bufferSize]))
  604. using (BinaryWriter writer = new BinaryWriter(stream))
  605. {
  606. byte[] output = _encoding.GetBytes(_textValue);
  607. if (!interactive)
  608. {
  609. // Result Code.
  610. writer.Write(_lastResult == KeyboardResult.Accept ? 0U : 1U);
  611. }
  612. else
  613. {
  614. // In interactive mode, we write the length of the text as a long, rather than
  615. // a result code. This field is inclusive of the 64-bit size.
  616. writer.Write((long)output.Length + 8);
  617. }
  618. writer.Write(output);
  619. if (!interactive)
  620. {
  621. _normalSession.Push(stream.ToArray());
  622. }
  623. else
  624. {
  625. _interactiveSession.Push(stream.ToArray());
  626. }
  627. }
  628. }
  629. /// <summary>
  630. /// Removes all Unicode control code characters from the input string.
  631. /// This includes CR/LF, tabs, null characters, escape characters,
  632. /// and special control codes which are used for formatting by the real keyboard applet.
  633. /// </summary>
  634. /// <remarks>
  635. /// Some games send special control codes (such as 0x13 "Device Control 3") as part of the string.
  636. /// Future implementations of the emulated keyboard applet will need to handle these as well.
  637. /// </remarks>
  638. /// <param name="input">The input string to sanitize (may be null).</param>
  639. /// <returns>The sanitized string.</returns>
  640. internal static string StripUnicodeControlCodes(string input)
  641. {
  642. if (input is null)
  643. {
  644. return null;
  645. }
  646. if (input.Length == 0)
  647. {
  648. return string.Empty;
  649. }
  650. StringBuilder sb = new StringBuilder(capacity: input.Length);
  651. foreach (char c in input)
  652. {
  653. if (!char.IsControl(c))
  654. {
  655. sb.Append(c);
  656. }
  657. }
  658. return sb.ToString();
  659. }
  660. private static T ReadStruct<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(byte[] data)
  661. where T : struct
  662. {
  663. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  664. try
  665. {
  666. return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
  667. }
  668. finally
  669. {
  670. handle.Free();
  671. }
  672. }
  673. }
  674. }