SoftwareKeyboardApplet.cs 33 KB

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