SoftwareKeyboardApplet.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. using Ryujinx.Common.Configuration.Hid;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
  4. using Ryujinx.HLE.HOS.Services.Am.AppletAE;
  5. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Npad;
  6. using Ryujinx.HLE.Ui;
  7. using Ryujinx.HLE.Ui.Input;
  8. using Ryujinx.Memory;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. namespace Ryujinx.HLE.HOS.Applets
  15. {
  16. internal class SoftwareKeyboardApplet : IApplet
  17. {
  18. private const string DefaultInputText = "Ryujinx";
  19. private const int StandardBufferSize = 0x7D8;
  20. private const int InteractiveBufferSize = 0x7D4;
  21. private const int MaxUserWords = 0x1388;
  22. private const int MaxUiTextSize = 100;
  23. private const Key CycleInputModesKey = Key.F6;
  24. private readonly Switch _device;
  25. private SoftwareKeyboardState _foregroundState = SoftwareKeyboardState.Uninitialized;
  26. private volatile InlineKeyboardState _backgroundState = InlineKeyboardState.Uninitialized;
  27. private bool _isBackground = false;
  28. private AppletSession _normalSession;
  29. private AppletSession _interactiveSession;
  30. // Configuration for foreground mode.
  31. private SoftwareKeyboardConfig _keyboardForegroundConfig;
  32. // Configuration for background (inline) mode.
  33. private SoftwareKeyboardInitialize _keyboardBackgroundInitialize;
  34. private SoftwareKeyboardCustomizeDic _keyboardBackgroundDic;
  35. private SoftwareKeyboardDictSet _keyboardBackgroundDictSet;
  36. private SoftwareKeyboardUserWord[] _keyboardBackgroundUserWords;
  37. private byte[] _transferMemory;
  38. private string _textValue = "";
  39. private int _cursorBegin = 0;
  40. private Encoding _encoding = Encoding.Unicode;
  41. private KeyboardResult _lastResult = KeyboardResult.NotSet;
  42. private IDynamicTextInputHandler _dynamicTextInputHandler = null;
  43. private SoftwareKeyboardRenderer _keyboardRenderer = null;
  44. private NpadReader _npads = null;
  45. private bool _canAcceptController = false;
  46. private KeyboardInputMode _inputMode = KeyboardInputMode.ControllerAndKeyboard;
  47. private object _lock = new object();
  48. public event EventHandler AppletStateChanged;
  49. public SoftwareKeyboardApplet(Horizon system)
  50. {
  51. _device = system.Device;
  52. }
  53. public ResultCode Start(AppletSession normalSession, AppletSession interactiveSession)
  54. {
  55. lock (_lock)
  56. {
  57. _normalSession = normalSession;
  58. _interactiveSession = interactiveSession;
  59. _interactiveSession.DataAvailable += OnInteractiveData;
  60. var launchParams = _normalSession.Pop();
  61. var keyboardConfig = _normalSession.Pop();
  62. _isBackground = keyboardConfig.Length == Marshal.SizeOf<SoftwareKeyboardInitialize>();
  63. if (_isBackground)
  64. {
  65. // Initialize the keyboard applet in background mode.
  66. _keyboardBackgroundInitialize = ReadStruct<SoftwareKeyboardInitialize>(keyboardConfig);
  67. _backgroundState = InlineKeyboardState.Uninitialized;
  68. if (_device.UiHandler == null)
  69. {
  70. Logger.Error?.Print(LogClass.ServiceAm, "GUI Handler is not set, software keyboard applet will not work properly");
  71. }
  72. else
  73. {
  74. // Create a text handler that converts keyboard strokes to strings.
  75. _dynamicTextInputHandler = _device.UiHandler.CreateDynamicTextInputHandler();
  76. _dynamicTextInputHandler.TextChangedEvent += HandleTextChangedEvent;
  77. _dynamicTextInputHandler.KeyPressedEvent += HandleKeyPressedEvent;
  78. _npads = new NpadReader(_device);
  79. _npads.NpadButtonDownEvent += HandleNpadButtonDownEvent;
  80. _npads.NpadButtonUpEvent += HandleNpadButtonUpEvent;
  81. _keyboardRenderer = new SoftwareKeyboardRenderer(_device.UiHandler.HostUiTheme);
  82. }
  83. return ResultCode.Success;
  84. }
  85. else
  86. {
  87. // Initialize the keyboard applet in foreground mode.
  88. if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
  89. {
  90. Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
  91. }
  92. else
  93. {
  94. _keyboardForegroundConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
  95. }
  96. if (!_normalSession.TryPop(out _transferMemory))
  97. {
  98. Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
  99. }
  100. if (_keyboardForegroundConfig.UseUtf8)
  101. {
  102. _encoding = Encoding.UTF8;
  103. }
  104. _foregroundState = SoftwareKeyboardState.Ready;
  105. ExecuteForegroundKeyboard();
  106. return ResultCode.Success;
  107. }
  108. }
  109. }
  110. public ResultCode GetResult()
  111. {
  112. return ResultCode.Success;
  113. }
  114. private bool IsKeyboardActive()
  115. {
  116. return _backgroundState >= InlineKeyboardState.Appearing && _backgroundState < InlineKeyboardState.Disappearing;
  117. }
  118. private bool InputModeControllerEnabled()
  119. {
  120. return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
  121. _inputMode == KeyboardInputMode.ControllerOnly;
  122. }
  123. private bool InputModeTypingEnabled()
  124. {
  125. return _inputMode == KeyboardInputMode.ControllerAndKeyboard ||
  126. _inputMode == KeyboardInputMode.KeyboardOnly;
  127. }
  128. private void AdvanceInputMode()
  129. {
  130. _inputMode = (KeyboardInputMode)((int)(_inputMode + 1) % (int)KeyboardInputMode.Count);
  131. }
  132. public bool DrawTo(RenderingSurfaceInfo surfaceInfo, IVirtualMemoryManager destination, ulong position)
  133. {
  134. _npads?.Update();
  135. return _keyboardRenderer?.DrawTo(surfaceInfo, destination, position) ?? false;
  136. }
  137. private void ExecuteForegroundKeyboard()
  138. {
  139. string initialText = null;
  140. // Initial Text is always encoded as a UTF-16 string in the work buffer (passed as transfer memory)
  141. // InitialStringOffset points to the memory offset and InitialStringLength is the number of UTF-16 characters
  142. if (_transferMemory != null && _keyboardForegroundConfig.InitialStringLength > 0)
  143. {
  144. initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardForegroundConfig.InitialStringOffset,
  145. 2 * _keyboardForegroundConfig.InitialStringLength);
  146. }
  147. // If the max string length is 0, we set it to a large default
  148. // length.
  149. if (_keyboardForegroundConfig.StringLengthMax == 0)
  150. {
  151. _keyboardForegroundConfig.StringLengthMax = 100;
  152. }
  153. if (_device.UiHandler == null)
  154. {
  155. Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
  156. _textValue = DefaultInputText;
  157. _lastResult = KeyboardResult.Accept;
  158. }
  159. else
  160. {
  161. // Call the configured GUI handler to get user's input.
  162. var args = new SoftwareKeyboardUiArgs
  163. {
  164. HeaderText = _keyboardForegroundConfig.HeaderText,
  165. SubtitleText = _keyboardForegroundConfig.SubtitleText,
  166. GuideText = _keyboardForegroundConfig.GuideText,
  167. SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
  168. _keyboardForegroundConfig.SubmitText : "OK"),
  169. StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
  170. StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
  171. InitialText = initialText
  172. };
  173. _lastResult = _device.UiHandler.DisplayInputDialog(args, out _textValue) ? KeyboardResult.Accept : KeyboardResult.Cancel;
  174. _textValue ??= initialText ?? DefaultInputText;
  175. }
  176. // If the game requests a string with a minimum length less
  177. // than our default text, repeat our default text until we meet
  178. // the minimum length requirement.
  179. // This should always be done before the text truncation step.
  180. while (_textValue.Length < _keyboardForegroundConfig.StringLengthMin)
  181. {
  182. _textValue = String.Join(" ", _textValue, _textValue);
  183. }
  184. // If our default text is longer than the allowed length,
  185. // we truncate it.
  186. if (_textValue.Length > _keyboardForegroundConfig.StringLengthMax)
  187. {
  188. _textValue = _textValue.Substring(0, _keyboardForegroundConfig.StringLengthMax);
  189. }
  190. // Does the application want to validate the text itself?
  191. if (_keyboardForegroundConfig.CheckText)
  192. {
  193. // The application needs to validate the response, so we
  194. // submit it to the interactive output buffer, and poll it
  195. // for validation. Once validated, the application will submit
  196. // back a validation status, which is handled in OnInteractiveDataPushIn.
  197. _foregroundState = SoftwareKeyboardState.ValidationPending;
  198. PushForegroundResponse(true);
  199. }
  200. else
  201. {
  202. // If the application doesn't need to validate the response,
  203. // we push the data to the non-interactive output buffer
  204. // and poll it for completion.
  205. _foregroundState = SoftwareKeyboardState.Complete;
  206. PushForegroundResponse(false);
  207. AppletStateChanged?.Invoke(this, null);
  208. }
  209. }
  210. private void OnInteractiveData(object sender, EventArgs e)
  211. {
  212. // Obtain the validation status response.
  213. var data = _interactiveSession.Pop();
  214. if (_isBackground)
  215. {
  216. lock (_lock)
  217. {
  218. OnBackgroundInteractiveData(data);
  219. }
  220. }
  221. else
  222. {
  223. OnForegroundInteractiveData(data);
  224. }
  225. }
  226. private void OnForegroundInteractiveData(byte[] data)
  227. {
  228. if (_foregroundState == SoftwareKeyboardState.ValidationPending)
  229. {
  230. // TODO(jduncantor):
  231. // If application rejects our "attempt", submit another attempt,
  232. // and put the applet back in PendingValidation state.
  233. // For now we assume success, so we push the final result
  234. // to the standard output buffer and carry on our merry way.
  235. PushForegroundResponse(false);
  236. AppletStateChanged?.Invoke(this, null);
  237. _foregroundState = SoftwareKeyboardState.Complete;
  238. }
  239. else if(_foregroundState == SoftwareKeyboardState.Complete)
  240. {
  241. // If we have already completed, we push the result text
  242. // back on the output buffer and poll the application.
  243. PushForegroundResponse(false);
  244. AppletStateChanged?.Invoke(this, null);
  245. }
  246. else
  247. {
  248. // We shouldn't be able to get here through standard swkbd execution.
  249. throw new InvalidOperationException("Software Keyboard is in an invalid state.");
  250. }
  251. }
  252. private void OnBackgroundInteractiveData(byte[] data)
  253. {
  254. // WARNING: Only invoke applet state changes after an explicit finalization
  255. // request from the game, this is because the inline keyboard is expected to
  256. // keep running in the background sending data by itself.
  257. using (MemoryStream stream = new MemoryStream(data))
  258. using (BinaryReader reader = new BinaryReader(stream))
  259. {
  260. var request = (InlineKeyboardRequest)reader.ReadUInt32();
  261. long remaining;
  262. Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {_backgroundState}");
  263. switch (request)
  264. {
  265. case InlineKeyboardRequest.UseChangedStringV2:
  266. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseChangedStringV2");
  267. break;
  268. case InlineKeyboardRequest.UseMovedCursorV2:
  269. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseMovedCursorV2");
  270. break;
  271. case InlineKeyboardRequest.SetUserWordInfo:
  272. // Read the user word info data.
  273. remaining = stream.Length - stream.Position;
  274. if (remaining < sizeof(int))
  275. {
  276. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info of {remaining} bytes");
  277. }
  278. else
  279. {
  280. int wordsCount = reader.ReadInt32();
  281. int wordSize = Marshal.SizeOf<SoftwareKeyboardUserWord>();
  282. remaining = stream.Length - stream.Position;
  283. if (wordsCount > MaxUserWords)
  284. {
  285. Logger.Warning?.Print(LogClass.ServiceAm, $"Received {wordsCount} User Words but the maximum is {MaxUserWords}");
  286. }
  287. else if (wordsCount * wordSize != remaining)
  288. {
  289. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info data of {remaining} bytes for {wordsCount} words");
  290. }
  291. else
  292. {
  293. _keyboardBackgroundUserWords = new SoftwareKeyboardUserWord[wordsCount];
  294. for (int word = 0; word < wordsCount; word++)
  295. {
  296. byte[] wordData = reader.ReadBytes(wordSize);
  297. _keyboardBackgroundUserWords[word] = ReadStruct<SoftwareKeyboardUserWord>(wordData);
  298. }
  299. }
  300. }
  301. _interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(_backgroundState));
  302. break;
  303. case InlineKeyboardRequest.SetCustomizeDic:
  304. // Read the custom dic data.
  305. remaining = stream.Length - stream.Position;
  306. if (remaining != Marshal.SizeOf<SoftwareKeyboardCustomizeDic>())
  307. {
  308. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Customize Dic of {remaining} bytes");
  309. }
  310. else
  311. {
  312. var keyboardDicData = reader.ReadBytes((int)remaining);
  313. _keyboardBackgroundDic = ReadStruct<SoftwareKeyboardCustomizeDic>(keyboardDicData);
  314. }
  315. break;
  316. case InlineKeyboardRequest.SetCustomizedDictionaries:
  317. // Read the custom dictionaries data.
  318. remaining = stream.Length - stream.Position;
  319. if (remaining != Marshal.SizeOf<SoftwareKeyboardDictSet>())
  320. {
  321. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes");
  322. }
  323. else
  324. {
  325. var keyboardDictData = reader.ReadBytes((int)remaining);
  326. _keyboardBackgroundDictSet = ReadStruct<SoftwareKeyboardDictSet>(keyboardDictData);
  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. private static T ReadStruct<T>(byte[] data)
  628. where T : struct
  629. {
  630. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  631. try
  632. {
  633. return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
  634. }
  635. finally
  636. {
  637. handle.Free();
  638. }
  639. }
  640. }
  641. }