SoftwareKeyboardApplet.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. _keyboardRenderer?.SetSurfaceInfo(surfaceInfo);
  136. return _keyboardRenderer?.DrawTo(destination, position) ?? false;
  137. }
  138. private void ExecuteForegroundKeyboard()
  139. {
  140. string initialText = null;
  141. // Initial Text is always encoded as a UTF-16 string in the work buffer (passed as transfer memory)
  142. // InitialStringOffset points to the memory offset and InitialStringLength is the number of UTF-16 characters
  143. if (_transferMemory != null && _keyboardForegroundConfig.InitialStringLength > 0)
  144. {
  145. initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardForegroundConfig.InitialStringOffset,
  146. 2 * _keyboardForegroundConfig.InitialStringLength);
  147. }
  148. // If the max string length is 0, we set it to a large default
  149. // length.
  150. if (_keyboardForegroundConfig.StringLengthMax == 0)
  151. {
  152. _keyboardForegroundConfig.StringLengthMax = 100;
  153. }
  154. if (_device.UiHandler == null)
  155. {
  156. Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
  157. _textValue = DefaultInputText;
  158. _lastResult = KeyboardResult.Accept;
  159. }
  160. else
  161. {
  162. // Call the configured GUI handler to get user's input.
  163. var args = new SoftwareKeyboardUiArgs
  164. {
  165. HeaderText = _keyboardForegroundConfig.HeaderText,
  166. SubtitleText = _keyboardForegroundConfig.SubtitleText,
  167. GuideText = _keyboardForegroundConfig.GuideText,
  168. SubmitText = (!string.IsNullOrWhiteSpace(_keyboardForegroundConfig.SubmitText) ?
  169. _keyboardForegroundConfig.SubmitText : "OK"),
  170. StringLengthMin = _keyboardForegroundConfig.StringLengthMin,
  171. StringLengthMax = _keyboardForegroundConfig.StringLengthMax,
  172. InitialText = initialText
  173. };
  174. _lastResult = _device.UiHandler.DisplayInputDialog(args, out _textValue) ? KeyboardResult.Accept : KeyboardResult.Cancel;
  175. _textValue ??= initialText ?? DefaultInputText;
  176. }
  177. // If the game requests a string with a minimum length less
  178. // than our default text, repeat our default text until we meet
  179. // the minimum length requirement.
  180. // This should always be done before the text truncation step.
  181. while (_textValue.Length < _keyboardForegroundConfig.StringLengthMin)
  182. {
  183. _textValue = String.Join(" ", _textValue, _textValue);
  184. }
  185. // If our default text is longer than the allowed length,
  186. // we truncate it.
  187. if (_textValue.Length > _keyboardForegroundConfig.StringLengthMax)
  188. {
  189. _textValue = _textValue.Substring(0, _keyboardForegroundConfig.StringLengthMax);
  190. }
  191. // Does the application want to validate the text itself?
  192. if (_keyboardForegroundConfig.CheckText)
  193. {
  194. // The application needs to validate the response, so we
  195. // submit it to the interactive output buffer, and poll it
  196. // for validation. Once validated, the application will submit
  197. // back a validation status, which is handled in OnInteractiveDataPushIn.
  198. _foregroundState = SoftwareKeyboardState.ValidationPending;
  199. PushForegroundResponse(true);
  200. }
  201. else
  202. {
  203. // If the application doesn't need to validate the response,
  204. // we push the data to the non-interactive output buffer
  205. // and poll it for completion.
  206. _foregroundState = SoftwareKeyboardState.Complete;
  207. PushForegroundResponse(false);
  208. AppletStateChanged?.Invoke(this, null);
  209. }
  210. }
  211. private void OnInteractiveData(object sender, EventArgs e)
  212. {
  213. // Obtain the validation status response.
  214. var data = _interactiveSession.Pop();
  215. if (_isBackground)
  216. {
  217. lock (_lock)
  218. {
  219. OnBackgroundInteractiveData(data);
  220. }
  221. }
  222. else
  223. {
  224. OnForegroundInteractiveData(data);
  225. }
  226. }
  227. private void OnForegroundInteractiveData(byte[] data)
  228. {
  229. if (_foregroundState == SoftwareKeyboardState.ValidationPending)
  230. {
  231. // TODO(jduncantor):
  232. // If application rejects our "attempt", submit another attempt,
  233. // and put the applet back in PendingValidation state.
  234. // For now we assume success, so we push the final result
  235. // to the standard output buffer and carry on our merry way.
  236. PushForegroundResponse(false);
  237. AppletStateChanged?.Invoke(this, null);
  238. _foregroundState = SoftwareKeyboardState.Complete;
  239. }
  240. else if(_foregroundState == SoftwareKeyboardState.Complete)
  241. {
  242. // If we have already completed, we push the result text
  243. // back on the output buffer and poll the application.
  244. PushForegroundResponse(false);
  245. AppletStateChanged?.Invoke(this, null);
  246. }
  247. else
  248. {
  249. // We shouldn't be able to get here through standard swkbd execution.
  250. throw new InvalidOperationException("Software Keyboard is in an invalid state.");
  251. }
  252. }
  253. private void OnBackgroundInteractiveData(byte[] data)
  254. {
  255. // WARNING: Only invoke applet state changes after an explicit finalization
  256. // request from the game, this is because the inline keyboard is expected to
  257. // keep running in the background sending data by itself.
  258. using (MemoryStream stream = new MemoryStream(data))
  259. using (BinaryReader reader = new BinaryReader(stream))
  260. {
  261. var request = (InlineKeyboardRequest)reader.ReadUInt32();
  262. long remaining;
  263. Logger.Debug?.Print(LogClass.ServiceAm, $"Keyboard received command {request} in state {_backgroundState}");
  264. switch (request)
  265. {
  266. case InlineKeyboardRequest.UseChangedStringV2:
  267. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseChangedStringV2");
  268. break;
  269. case InlineKeyboardRequest.UseMovedCursorV2:
  270. Logger.Stub?.Print(LogClass.ServiceAm, "Inline keyboard request UseMovedCursorV2");
  271. break;
  272. case InlineKeyboardRequest.SetUserWordInfo:
  273. // Read the user word info data.
  274. remaining = stream.Length - stream.Position;
  275. if (remaining < sizeof(int))
  276. {
  277. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info of {remaining} bytes");
  278. }
  279. else
  280. {
  281. int wordsCount = reader.ReadInt32();
  282. int wordSize = Marshal.SizeOf<SoftwareKeyboardUserWord>();
  283. remaining = stream.Length - stream.Position;
  284. if (wordsCount > MaxUserWords)
  285. {
  286. Logger.Warning?.Print(LogClass.ServiceAm, $"Received {wordsCount} User Words but the maximum is {MaxUserWords}");
  287. }
  288. else if (wordsCount * wordSize != remaining)
  289. {
  290. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard User Word Info data of {remaining} bytes for {wordsCount} words");
  291. }
  292. else
  293. {
  294. _keyboardBackgroundUserWords = new SoftwareKeyboardUserWord[wordsCount];
  295. for (int word = 0; word < wordsCount; word++)
  296. {
  297. byte[] wordData = reader.ReadBytes(wordSize);
  298. _keyboardBackgroundUserWords[word] = ReadStruct<SoftwareKeyboardUserWord>(wordData);
  299. }
  300. }
  301. }
  302. _interactiveSession.Push(InlineResponses.ReleasedUserWordInfo(_backgroundState));
  303. break;
  304. case InlineKeyboardRequest.SetCustomizeDic:
  305. // Read the custom dic data.
  306. remaining = stream.Length - stream.Position;
  307. if (remaining != Marshal.SizeOf<SoftwareKeyboardCustomizeDic>())
  308. {
  309. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Customize Dic of {remaining} bytes");
  310. }
  311. else
  312. {
  313. var keyboardDicData = reader.ReadBytes((int)remaining);
  314. _keyboardBackgroundDic = ReadStruct<SoftwareKeyboardCustomizeDic>(keyboardDicData);
  315. }
  316. break;
  317. case InlineKeyboardRequest.SetCustomizedDictionaries:
  318. // Read the custom dictionaries data.
  319. remaining = stream.Length - stream.Position;
  320. if (remaining != Marshal.SizeOf<SoftwareKeyboardDictSet>())
  321. {
  322. Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes");
  323. }
  324. else
  325. {
  326. var keyboardDictData = reader.ReadBytes((int)remaining);
  327. _keyboardBackgroundDictSet = ReadStruct<SoftwareKeyboardDictSet>(keyboardDictData);
  328. }
  329. break;
  330. case InlineKeyboardRequest.Calc:
  331. // The Calc request is used to communicate configuration changes and commands to the keyboard.
  332. // Fields in the Calc struct and operations are masked by the Flags field.
  333. // Read the Calc data.
  334. SoftwareKeyboardCalcEx newCalc;
  335. remaining = stream.Length - stream.Position;
  336. if (remaining == Marshal.SizeOf<SoftwareKeyboardCalc>())
  337. {
  338. var keyboardCalcData = reader.ReadBytes((int)remaining);
  339. var keyboardCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
  340. newCalc = keyboardCalc.ToExtended();
  341. }
  342. else if (remaining == Marshal.SizeOf<SoftwareKeyboardCalcEx>() || remaining == SoftwareKeyboardCalcEx.AlternativeSize)
  343. {
  344. var keyboardCalcData = reader.ReadBytes((int)remaining);
  345. newCalc = ReadStruct<SoftwareKeyboardCalcEx>(keyboardCalcData);
  346. }
  347. else
  348. {
  349. Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes");
  350. newCalc = new SoftwareKeyboardCalcEx();
  351. }
  352. // Process each individual operation specified in the flags.
  353. bool updateText = false;
  354. if ((newCalc.Flags & KeyboardCalcFlags.Initialize) != 0)
  355. {
  356. _interactiveSession.Push(InlineResponses.FinishedInitialize(_backgroundState));
  357. _backgroundState = InlineKeyboardState.Initialized;
  358. }
  359. if ((newCalc.Flags & KeyboardCalcFlags.SetCursorPos) != 0)
  360. {
  361. _cursorBegin = newCalc.CursorPos;
  362. updateText = true;
  363. Logger.Debug?.Print(LogClass.ServiceAm, $"Cursor position set to {_cursorBegin}");
  364. }
  365. if ((newCalc.Flags & KeyboardCalcFlags.SetInputText) != 0)
  366. {
  367. _textValue = newCalc.InputText;
  368. updateText = true;
  369. Logger.Debug?.Print(LogClass.ServiceAm, $"Input text set to {_textValue}");
  370. }
  371. if ((newCalc.Flags & KeyboardCalcFlags.SetUtf8Mode) != 0)
  372. {
  373. _encoding = newCalc.UseUtf8 ? Encoding.UTF8 : Encoding.Default;
  374. Logger.Debug?.Print(LogClass.ServiceAm, $"Encoding set to {_encoding}");
  375. }
  376. if (updateText)
  377. {
  378. _dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
  379. _keyboardRenderer.UpdateTextState(_textValue, _cursorBegin, _cursorBegin, null, null);
  380. }
  381. if ((newCalc.Flags & KeyboardCalcFlags.MustShow) != 0)
  382. {
  383. ActivateFrontend();
  384. _backgroundState = InlineKeyboardState.Shown;
  385. PushChangedString(_textValue, (uint)_cursorBegin, _backgroundState);
  386. }
  387. // Send the response to the Calc
  388. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  389. break;
  390. case InlineKeyboardRequest.Finalize:
  391. // Destroy the frontend.
  392. DestroyFrontend();
  393. // The calling application wants to close the keyboard applet and will wait for a state change.
  394. _backgroundState = InlineKeyboardState.Uninitialized;
  395. AppletStateChanged?.Invoke(this, null);
  396. break;
  397. default:
  398. // We shouldn't be able to get here through standard swkbd execution.
  399. Logger.Warning?.Print(LogClass.ServiceAm, $"Invalid Software Keyboard request {request} during state {_backgroundState}");
  400. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  401. break;
  402. }
  403. }
  404. }
  405. private void ActivateFrontend()
  406. {
  407. Logger.Debug?.Print(LogClass.ServiceAm, $"Activating software keyboard frontend");
  408. _inputMode = KeyboardInputMode.ControllerAndKeyboard;
  409. _npads.Update(true);
  410. NpadButton buttons = _npads.GetCurrentButtonsOfAllNpads();
  411. // Block the input if the current accept key is pressed so the applet won't be instantly closed.
  412. _canAcceptController = (buttons & NpadButton.A) == 0;
  413. _dynamicTextInputHandler.TextProcessingEnabled = true;
  414. _keyboardRenderer.UpdateCommandState(null, null, true);
  415. _keyboardRenderer.UpdateTextState(null, null, null, null, true);
  416. }
  417. private void DeactivateFrontend()
  418. {
  419. Logger.Debug?.Print(LogClass.ServiceAm, $"Deactivating software keyboard frontend");
  420. _inputMode = KeyboardInputMode.ControllerAndKeyboard;
  421. _canAcceptController = false;
  422. _dynamicTextInputHandler.TextProcessingEnabled = false;
  423. _dynamicTextInputHandler.SetText(_textValue, _cursorBegin);
  424. }
  425. private void DestroyFrontend()
  426. {
  427. Logger.Debug?.Print(LogClass.ServiceAm, $"Destroying software keyboard frontend");
  428. _keyboardRenderer?.Dispose();
  429. _keyboardRenderer = null;
  430. if (_dynamicTextInputHandler != null)
  431. {
  432. _dynamicTextInputHandler.TextChangedEvent -= HandleTextChangedEvent;
  433. _dynamicTextInputHandler.KeyPressedEvent -= HandleKeyPressedEvent;
  434. _dynamicTextInputHandler.Dispose();
  435. _dynamicTextInputHandler = null;
  436. }
  437. if (_npads != null)
  438. {
  439. _npads.NpadButtonDownEvent -= HandleNpadButtonDownEvent;
  440. _npads.NpadButtonUpEvent -= HandleNpadButtonUpEvent;
  441. _npads = null;
  442. }
  443. }
  444. private bool HandleKeyPressedEvent(Key key)
  445. {
  446. if (key == CycleInputModesKey)
  447. {
  448. lock (_lock)
  449. {
  450. if (IsKeyboardActive())
  451. {
  452. AdvanceInputMode();
  453. bool typingEnabled = InputModeTypingEnabled();
  454. bool controllerEnabled = InputModeControllerEnabled();
  455. _dynamicTextInputHandler.TextProcessingEnabled = typingEnabled;
  456. _keyboardRenderer.UpdateTextState(null, null, null, null, typingEnabled);
  457. _keyboardRenderer.UpdateCommandState(null, null, controllerEnabled);
  458. }
  459. }
  460. }
  461. return true;
  462. }
  463. private void HandleTextChangedEvent(string text, int cursorBegin, int cursorEnd, bool overwriteMode)
  464. {
  465. lock (_lock)
  466. {
  467. // Text processing should not run with typing disabled.
  468. Debug.Assert(InputModeTypingEnabled());
  469. if (text.Length > MaxUiTextSize)
  470. {
  471. // Limit the text size and change it back.
  472. text = text.Substring(0, MaxUiTextSize);
  473. cursorBegin = Math.Min(cursorBegin, MaxUiTextSize);
  474. cursorEnd = Math.Min(cursorEnd, MaxUiTextSize);
  475. _dynamicTextInputHandler.SetText(text, cursorBegin, cursorEnd);
  476. }
  477. _textValue = text;
  478. _cursorBegin = cursorBegin;
  479. _keyboardRenderer.UpdateTextState(text, cursorBegin, cursorEnd, overwriteMode, null);
  480. PushUpdatedState(text, cursorBegin, KeyboardResult.NotSet);
  481. }
  482. }
  483. private void HandleNpadButtonDownEvent(int npadIndex, NpadButton button)
  484. {
  485. lock (_lock)
  486. {
  487. if (!IsKeyboardActive())
  488. {
  489. return;
  490. }
  491. switch (button)
  492. {
  493. case NpadButton.A:
  494. _keyboardRenderer.UpdateCommandState(_canAcceptController, null, null);
  495. break;
  496. case NpadButton.B:
  497. _keyboardRenderer.UpdateCommandState(null, _canAcceptController, null);
  498. break;
  499. }
  500. }
  501. }
  502. private void HandleNpadButtonUpEvent(int npadIndex, NpadButton button)
  503. {
  504. lock (_lock)
  505. {
  506. KeyboardResult result = KeyboardResult.NotSet;
  507. switch (button)
  508. {
  509. case NpadButton.A:
  510. result = KeyboardResult.Accept;
  511. _keyboardRenderer.UpdateCommandState(false, null, null);
  512. break;
  513. case NpadButton.B:
  514. result = KeyboardResult.Cancel;
  515. _keyboardRenderer.UpdateCommandState(null, false, null);
  516. break;
  517. }
  518. if (IsKeyboardActive())
  519. {
  520. if (!_canAcceptController)
  521. {
  522. _canAcceptController = true;
  523. }
  524. else if (InputModeControllerEnabled())
  525. {
  526. PushUpdatedState(_textValue, _cursorBegin, result);
  527. }
  528. }
  529. }
  530. }
  531. private void PushUpdatedState(string text, int cursorBegin, KeyboardResult result)
  532. {
  533. _lastResult = result;
  534. _textValue = text;
  535. bool cancel = result == KeyboardResult.Cancel;
  536. bool accept = result == KeyboardResult.Accept;
  537. if (!IsKeyboardActive())
  538. {
  539. // Keyboard is not active.
  540. return;
  541. }
  542. if (accept == false && cancel == false)
  543. {
  544. Logger.Debug?.Print(LogClass.ServiceAm, $"Updating keyboard text to {text} and cursor position to {cursorBegin}");
  545. PushChangedString(text, (uint)cursorBegin, _backgroundState);
  546. }
  547. else
  548. {
  549. // Disable the frontend.
  550. DeactivateFrontend();
  551. // The 'Complete' state indicates the Calc request has been fulfilled by the applet.
  552. _backgroundState = InlineKeyboardState.Disappearing;
  553. if (accept)
  554. {
  555. Logger.Debug?.Print(LogClass.ServiceAm, $"Sending keyboard OK with text {text}");
  556. DecidedEnter(text, _backgroundState);
  557. }
  558. else if (cancel)
  559. {
  560. Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard Cancel");
  561. DecidedCancel(_backgroundState);
  562. }
  563. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  564. Logger.Debug?.Print(LogClass.ServiceAm, $"Resetting state of the keyboard to {_backgroundState}");
  565. // Set the state of the applet to 'Initialized' as it is the only known state so far
  566. // that does not soft-lock the keyboard after use.
  567. _backgroundState = InlineKeyboardState.Initialized;
  568. _interactiveSession.Push(InlineResponses.Default(_backgroundState));
  569. }
  570. }
  571. private void PushChangedString(string text, uint cursor, InlineKeyboardState state)
  572. {
  573. // TODO (Caian): The *V2 methods are not supported because the applications that request
  574. // them do not seem to accept them. The regular methods seem to work just fine in all cases.
  575. if (_encoding == Encoding.UTF8)
  576. {
  577. _interactiveSession.Push(InlineResponses.ChangedStringUtf8(text, cursor, state));
  578. }
  579. else
  580. {
  581. _interactiveSession.Push(InlineResponses.ChangedString(text, cursor, state));
  582. }
  583. }
  584. private void DecidedEnter(string text, InlineKeyboardState state)
  585. {
  586. if (_encoding == Encoding.UTF8)
  587. {
  588. _interactiveSession.Push(InlineResponses.DecidedEnterUtf8(text, state));
  589. }
  590. else
  591. {
  592. _interactiveSession.Push(InlineResponses.DecidedEnter(text, state));
  593. }
  594. }
  595. private void DecidedCancel(InlineKeyboardState state)
  596. {
  597. _interactiveSession.Push(InlineResponses.DecidedCancel(state));
  598. }
  599. private void PushForegroundResponse(bool interactive)
  600. {
  601. int bufferSize = interactive ? InteractiveBufferSize : StandardBufferSize;
  602. using (MemoryStream stream = new MemoryStream(new byte[bufferSize]))
  603. using (BinaryWriter writer = new BinaryWriter(stream))
  604. {
  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. }
  628. private static T ReadStruct<T>(byte[] data)
  629. where T : struct
  630. {
  631. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  632. try
  633. {
  634. return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
  635. }
  636. finally
  637. {
  638. handle.Free();
  639. }
  640. }
  641. }
  642. }