SoftwareKeyboardApplet.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. #pragma warning disable IDE0052 // Remove unread private member
  37. private SoftwareKeyboardInitialize _keyboardBackgroundInitialize;
  38. private SoftwareKeyboardCustomizeDic _keyboardBackgroundDic;
  39. private SoftwareKeyboardDictSet _keyboardBackgroundDictSet;
  40. #pragma warning restore IDE0052
  41. private SoftwareKeyboardUserWord[] _keyboardBackgroundUserWords;
  42. private byte[] _transferMemory;
  43. private string _textValue = "";
  44. private int _cursorBegin = 0;
  45. private Encoding _encoding = Encoding.Unicode;
  46. private KeyboardResult _lastResult = KeyboardResult.NotSet;
  47. private IDynamicTextInputHandler _dynamicTextInputHandler = null;
  48. private SoftwareKeyboardRenderer _keyboardRenderer = null;
  49. private NpadReader _npads = null;
  50. private bool _canAcceptController = false;
  51. private KeyboardInputMode _inputMode = KeyboardInputMode.ControllerAndKeyboard;
  52. private readonly object _lock = new();
  53. public event EventHandler AppletStateChanged;
  54. public SoftwareKeyboardApplet(Horizon system)
  55. {
  56. _device = system.Device;
  57. }
  58. public ResultCode Start(AppletSession normalSession, AppletSession interactiveSession)
  59. {
  60. lock (_lock)
  61. {
  62. _normalSession = normalSession;
  63. _interactiveSession = interactiveSession;
  64. _interactiveSession.DataAvailable += OnInteractiveData;
  65. var launchParams = _normalSession.Pop();
  66. var keyboardConfig = _normalSession.Pop();
  67. _isBackground = keyboardConfig.Length == Unsafe.SizeOf<SoftwareKeyboardInitialize>();
  68. if (_isBackground)
  69. {
  70. // Initialize the keyboard applet in background mode.
  71. _keyboardBackgroundInitialize = MemoryMarshal.Read<SoftwareKeyboardInitialize>(keyboardConfig);
  72. _backgroundState = InlineKeyboardState.Uninitialized;
  73. if (_device.UIHandler == null)
  74. {
  75. Logger.Error?.Print(LogClass.ServiceAm, "GUI Handler is not set, software keyboard applet will not work properly");
  76. }
  77. else
  78. {
  79. // Create a text handler that converts keyboard strokes to strings.
  80. _dynamicTextInputHandler = _device.UIHandler.CreateDynamicTextInputHandler();
  81. _dynamicTextInputHandler.TextChangedEvent += HandleTextChangedEvent;
  82. _dynamicTextInputHandler.KeyPressedEvent += HandleKeyPressedEvent;
  83. _npads = new NpadReader(_device);
  84. _npads.NpadButtonDownEvent += HandleNpadButtonDownEvent;
  85. _npads.NpadButtonUpEvent += HandleNpadButtonUpEvent;
  86. _keyboardRenderer = new SoftwareKeyboardRenderer(_device.UIHandler.HostUITheme);
  87. }
  88. return ResultCode.Success;
  89. }
  90. else
  91. {
  92. // Initialize the keyboard applet in foreground mode.
  93. if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
  94. {
  95. Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
  96. }
  97. else
  98. {
  99. _keyboardForegroundConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
  100. }
  101. if (!_normalSession.TryPop(out _transferMemory))
  102. {
  103. Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
  104. }
  105. if (_keyboardForegroundConfig.UseUtf8)
  106. {
  107. _encoding = Encoding.UTF8;
  108. }
  109. _foregroundState = SoftwareKeyboardState.Ready;
  110. ExecuteForegroundKeyboard();
  111. return ResultCode.Success;
  112. }
  113. }
  114. }
  115. public ResultCode GetResult()
  116. {
  117. return ResultCode.Success;
  118. }
  119. private bool IsKeyboardActive()
  120. {
  121. return _backgroundState >= InlineKeyboardState.Appearing && _backgroundState < InlineKeyboardState.Disappearing;
  122. }
  123. private bool InputModeControllerEnabled()
  124. {
  125. return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
  126. _inputMode == KeyboardInputMode.ControllerOnly;
  127. }
  128. private bool InputModeTypingEnabled()
  129. {
  130. return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
  131. _inputMode == KeyboardInputMode.KeyboardOnly;
  132. }
  133. private void AdvanceInputMode()
  134. {
  135. _inputMode = (KeyboardInputMode)((int)(_inputMode + 1) % (int)KeyboardInputMode.Count);
  136. }
  137. public bool DrawTo(RenderingSurfaceInfo surfaceInfo, IVirtualMemoryManager destination, ulong position)
  138. {
  139. _npads?.Update();
  140. _keyboardRenderer?.SetSurfaceInfo(surfaceInfo);
  141. return _keyboardRenderer?.DrawTo(destination, position) ?? false;
  142. }
  143. private void ExecuteForegroundKeyboard()
  144. {
  145. string initialText = null;
  146. // Initial Text is always encoded as a UTF-16 string in the work buffer (passed as transfer memory)
  147. // InitialStringOffset points to the memory offset and InitialStringLength is the number of UTF-16 characters
  148. if (_transferMemory != null && _keyboardForegroundConfig.InitialStringLength > 0)
  149. {
  150. initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardForegroundConfig.InitialStringOffset,
  151. 2 * _keyboardForegroundConfig.InitialStringLength);
  152. }
  153. // If the max string length is 0, we set it to a large default
  154. // length.
  155. if (_keyboardForegroundConfig.StringLengthMax == 0)
  156. {
  157. _keyboardForegroundConfig.StringLengthMax = 100;
  158. }
  159. if (_device.UIHandler == null)
  160. {
  161. Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
  162. _textValue = DefaultInputText;
  163. _lastResult = KeyboardResult.Accept;
  164. }
  165. else
  166. {
  167. // Call the configured GUI handler to get user's input.
  168. var args = new SoftwareKeyboardUIArgs
  169. {
  170. KeyboardMode = _keyboardForegroundConfig.Mode,
  171. HeaderText = StripUnicodeControlCodes(_keyboardForegroundConfig.HeaderText),
  172. SubtitleText = StripUnicodeControlCodes(_keyboardForegroundConfig.SubtitleText),
  173. GuideText = StripUnicodeControlCodes(_keyboardForegroundConfig.GuideText),
  174. SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
  175. _keyboardForegroundConfig.SubmitText : "OK"),
  176. StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
  177. StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
  178. InitialText = initialText,
  179. };
  180. _lastResult = _device.UIHandler.DisplayInputDialog(args, out _textValue) ? KeyboardResult.Accept : KeyboardResult.Cancel;
  181. _textValue ??= initialText ?? DefaultInputText;
  182. }
  183. // If the game requests a string with a minimum length less
  184. // than our default text, repeat our default text until we meet
  185. // the minimum length requirement.
  186. // This should always be done before the text truncation step.
  187. while (_textValue.Length < _keyboardForegroundConfig.StringLengthMin)
  188. {
  189. _textValue = String.Join(" ", _textValue, _textValue);
  190. }
  191. // If our default text is longer than the allowed length,
  192. // we truncate it.
  193. if (_textValue.Length > _keyboardForegroundConfig.StringLengthMax)
  194. {
  195. _textValue = _textValue[.._keyboardForegroundConfig.StringLengthMax];
  196. }
  197. // Does the application want to validate the text itself?
  198. if (_keyboardForegroundConfig.CheckText)
  199. {
  200. // The application needs to validate the response, so we
  201. // submit it to the interactive output buffer, and poll it
  202. // for validation. Once validated, the application will submit
  203. // back a validation status, which is handled in OnInteractiveDataPushIn.
  204. _foregroundState = SoftwareKeyboardState.ValidationPending;
  205. PushForegroundResponse(true);
  206. }
  207. else
  208. {
  209. // If the application doesn't need to validate the response,
  210. // we push the data to the non-interactive output buffer
  211. // and poll it for completion.
  212. _foregroundState = SoftwareKeyboardState.Complete;
  213. PushForegroundResponse(false);
  214. AppletStateChanged?.Invoke(this, null);
  215. }
  216. }
  217. private void OnInteractiveData(object sender, EventArgs e)
  218. {
  219. // Obtain the validation status response.
  220. var data = _interactiveSession.Pop();
  221. if (_isBackground)
  222. {
  223. lock (_lock)
  224. {
  225. OnBackgroundInteractiveData(data);
  226. }
  227. }
  228. else
  229. {
  230. OnForegroundInteractiveData(data);
  231. }
  232. }
  233. private void OnForegroundInteractiveData(byte[] data)
  234. {
  235. if (_foregroundState == SoftwareKeyboardState.ValidationPending)
  236. {
  237. // TODO(jduncantor):
  238. // If application rejects our "attempt", submit another attempt,
  239. // and put the applet back in PendingValidation state.
  240. // For now we assume success, so we push the final result
  241. // to the standard output buffer and carry on our merry way.
  242. PushForegroundResponse(false);
  243. AppletStateChanged?.Invoke(this, null);
  244. _foregroundState = SoftwareKeyboardState.Complete;
  245. }
  246. else if (_foregroundState == SoftwareKeyboardState.Complete)
  247. {
  248. // If we have already completed, we push the result text
  249. // back on the output buffer and poll the application.
  250. PushForegroundResponse(false);
  251. AppletStateChanged?.Invoke(this, null);
  252. }
  253. else
  254. {
  255. // We shouldn't be able to get here through standard swkbd execution.
  256. throw new InvalidOperationException("Software Keyboard is in an invalid state.");
  257. }
  258. }
  259. private void OnBackgroundInteractiveData(byte[] data)
  260. {
  261. // WARNING: Only invoke applet state changes after an explicit finalization
  262. // request from the game, this is because the inline keyboard is expected to
  263. // keep running in the background sending data by itself.
  264. using MemoryStream stream = new(data);
  265. using BinaryReader reader = new(stream);
  266. var request = (InlineKeyboardRequest)reader.ReadUInt32();
  267. long remaining;
  268. Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {_backgroundState}");
  269. switch (request)
  270. {
  271. case InlineKeyboardRequest.UseChangedStringV2:
  272. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseChangedStringV2");
  273. break;
  274. case InlineKeyboardRequest.UseMovedCursorV2:
  275. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseMovedCursorV2");
  276. break;
  277. case InlineKeyboardRequest.SetUserWordInfo:
  278. // Read the user word info data.
  279. remaining = stream.Length - stream.Position;
  280. if (remaining < sizeof(int))
  281. {
  282. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info of {remaining} bytes");
  283. }
  284. else
  285. {
  286. int wordsCount = reader.ReadInt32();
  287. int wordSize = Unsafe.SizeOf<SoftwareKeyboardUserWord>();
  288. remaining = stream.Length - stream.Position;
  289. if (wordsCount > MaxUserWords)
  290. {
  291. Logger.Warning?.Print(LogClass.ServiceAm, $"Received {wordsCount} User Words but the maximum is {MaxUserWords}");
  292. }
  293. else if (wordsCount * wordSize != remaining)
  294. {
  295. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info data of {remaining} bytes for {wordsCount} words");
  296. }
  297. else
  298. {
  299. _keyboardBackgroundUserWords = new SoftwareKeyboardUserWord[wordsCount];
  300. for (int word = 0; word < wordsCount; word++)
  301. {
  302. _keyboardBackgroundUserWords[word] = reader.ReadStruct<SoftwareKeyboardUserWord>();
  303. }
  304. }
  305. }
  306. _interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(_backgroundState));
  307. break;
  308. case InlineKeyboardRequest.SetCustomizeDic:
  309. // Read the custom dic data.
  310. remaining = stream.Length - stream.Position;
  311. if (remaining != Unsafe.SizeOf<SoftwareKeyboardCustomizeDic>())
  312. {
  313. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Customize Dic of {remaining} bytes");
  314. }
  315. else
  316. {
  317. _keyboardBackgroundDic = reader.ReadStruct<SoftwareKeyboardCustomizeDic>();
  318. }
  319. break;
  320. case InlineKeyboardRequest.SetCustomizedDictionaries:
  321. // Read the custom dictionaries data.
  322. remaining = stream.Length - stream.Position;
  323. if (remaining != Unsafe.SizeOf<SoftwareKeyboardDictSet>())
  324. {
  325. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes");
  326. }
  327. else
  328. {
  329. _keyboardBackgroundDictSet = reader.ReadStruct<SoftwareKeyboardDictSet>();
  330. }
  331. break;
  332. case InlineKeyboardRequest.Calc:
  333. // The Calc request is used to communicate configuration changes and commands to the keyboard.
  334. // Fields in the Calc struct and operations are masked by the Flags field.
  335. // Read the Calc data.
  336. SoftwareKeyboardCalcEx newCalc;
  337. remaining = stream.Length - stream.Position;
  338. if (remaining == Marshal.SizeOf<SoftwareKeyboardCalc>())
  339. {
  340. var keyboardCalcData = reader.ReadBytes((int)remaining);
  341. var keyboardCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
  342. newCalc = keyboardCalc.ToExtended();
  343. }
  344. else if (remaining == Marshal.SizeOf<SoftwareKeyboardCalcEx>() || remaining == SoftwareKeyboardCalcEx.AlternativeSize)
  345. {
  346. var keyboardCalcData = reader.ReadBytes((int)remaining);
  347. newCalc = ReadStruct<SoftwareKeyboardCalcEx>(keyboardCalcData);
  348. }
  349. else
  350. {
  351. Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes");
  352. newCalc = new SoftwareKeyboardCalcEx();
  353. }
  354. // Process each individual operation specified in the flags.
  355. bool updateText = false;
  356. if ((newCalc.Flags & KeyboardCalcFlags.Initialize) != 0)
  357. {
  358. _interactiveSession.Push(InlineResponses.FinishedInitialize(_backgroundState));
  359. _backgroundState = InlineKeyboardState.Initialized;
  360. }
  361. if ((newCalc.Flags & KeyboardCalcFlags.SetCursorPos) != 0)
  362. {
  363. _cursorBegin = newCalc.CursorPos;
  364. updateText = true;
  365. Logger.Debug?.Print(LogClass.ServiceAm, $"Cursor position set to {_cursorBegin}");
  366. }
  367. if ((newCalc.Flags & KeyboardCalcFlags.SetInputText) != 0)
  368. {
  369. _textValue = newCalc.InputText;
  370. updateText = true;
  371. Logger.Debug?.Print(LogClass.ServiceAm, $"Input text set to {_textValue}");
  372. }
  373. if ((newCalc.Flags & KeyboardCalcFlags.SetUtf8Mode) != 0)
  374. {
  375. _encoding = newCalc.UseUtf8 ? Encoding.UTF8 : Encoding.Default;
  376. Logger.Debug?.Print(LogClass.ServiceAm, $"Encoding set to {_encoding}");
  377. }
  378. if (updateText)
  379. {
  380. _dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
  381. _keyboardRenderer.UpdateTextState(_textValue, _cursorBegin, _cursorBegin, null, null);
  382. }
  383. if ((newCalc.Flags & KeyboardCalcFlags.MustShow) != 0)
  384. {
  385. ActivateFrontend();
  386. _backgroundState = InlineKeyboardState.Shown;
  387. PushChangedString(_textValue, (uint)_cursorBegin, _backgroundState);
  388. }
  389. // Send the response to the Calc
  390. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  391. break;
  392. case InlineKeyboardRequest.Finalize:
  393. // Destroy the frontend.
  394. DestroyFrontend();
  395. // The calling application wants to close the keyboard applet and will wait for a state change.
  396. _backgroundState = InlineKeyboardState.Uninitialized;
  397. AppletStateChanged?.Invoke(this, null);
  398. break;
  399. default:
  400. // We shouldn't be able to get here through standard swkbd execution.
  401. Logger.Warning?.Print(LogClass.ServiceAm, $"Invalid Software Keyboard request {request} during state {_backgroundState}");
  402. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  403. break;
  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[..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(new byte[bufferSize]);
  604. using BinaryWriter writer = new(stream);
  605. byte[] output = _encoding.GetBytes(_textValue);
  606. if (!interactive)
  607. {
  608. // Result Code.
  609. writer.Write(_lastResult == KeyboardResult.Accept ? 0U : 1U);
  610. }
  611. else
  612. {
  613. // In interactive mode, we write the length of the text as a long, rather than
  614. // a result code. This field is inclusive of the 64-bit size.
  615. writer.Write((long)output.Length + 8);
  616. }
  617. writer.Write(output);
  618. if (!interactive)
  619. {
  620. _normalSession.Push(stream.ToArray());
  621. }
  622. else
  623. {
  624. _interactiveSession.Push(stream.ToArray());
  625. }
  626. }
  627. /// <summary>
  628. /// Removes all Unicode control code characters from the input string.
  629. /// This includes CR/LF, tabs, null characters, escape characters,
  630. /// and special control codes which are used for formatting by the real keyboard applet.
  631. /// </summary>
  632. /// <remarks>
  633. /// Some games send special control codes (such as 0x13 "Device Control 3") as part of the string.
  634. /// Future implementations of the emulated keyboard applet will need to handle these as well.
  635. /// </remarks>
  636. /// <param name="input">The input string to sanitize (may be null).</param>
  637. /// <returns>The sanitized string.</returns>
  638. internal static string StripUnicodeControlCodes(string input)
  639. {
  640. if (input is null)
  641. {
  642. return null;
  643. }
  644. if (input.Length == 0)
  645. {
  646. return string.Empty;
  647. }
  648. StringBuilder sb = new(capacity: input.Length);
  649. foreach (char c in input)
  650. {
  651. if (!char.IsControl(c))
  652. {
  653. sb.Append(c);
  654. }
  655. }
  656. return sb.ToString();
  657. }
  658. private static T ReadStruct<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(byte[] data)
  659. where T : struct
  660. {
  661. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  662. try
  663. {
  664. return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
  665. }
  666. finally
  667. {
  668. handle.Free();
  669. }
  670. }
  671. }
  672. }