ILibraryAppletAccessor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Applets;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Threading;
  6. using System;
  7. namespace Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.LibraryAppletCreator
  8. {
  9. class ILibraryAppletAccessor : IpcService
  10. {
  11. private IApplet _applet;
  12. private AppletSession _normalSession;
  13. private AppletSession _interactiveSession;
  14. private KEvent _stateChangedEvent;
  15. private KEvent _normalOutDataEvent;
  16. private KEvent _interactiveOutDataEvent;
  17. private int _stateChangedEventHandle;
  18. private int _normalOutDataEventHandle;
  19. private int _interactiveOutDataEventHandle;
  20. public ILibraryAppletAccessor(AppletId appletId, Horizon system)
  21. {
  22. _stateChangedEvent = new KEvent(system.KernelContext);
  23. _normalOutDataEvent = new KEvent(system.KernelContext);
  24. _interactiveOutDataEvent = new KEvent(system.KernelContext);
  25. _applet = AppletManager.Create(appletId, system);
  26. _normalSession = new AppletSession();
  27. _interactiveSession = new AppletSession();
  28. _applet.AppletStateChanged += OnAppletStateChanged;
  29. _normalSession.DataAvailable += OnNormalOutData;
  30. _interactiveSession.DataAvailable += OnInteractiveOutData;
  31. Logger.Info?.Print(LogClass.ServiceAm, $"Applet '{appletId}' created.");
  32. }
  33. private void OnAppletStateChanged(object sender, EventArgs e)
  34. {
  35. _stateChangedEvent.WritableEvent.Signal();
  36. }
  37. private void OnNormalOutData(object sender, EventArgs e)
  38. {
  39. _normalOutDataEvent.WritableEvent.Signal();
  40. }
  41. private void OnInteractiveOutData(object sender, EventArgs e)
  42. {
  43. _interactiveOutDataEvent.WritableEvent.Signal();
  44. }
  45. [Command(0)]
  46. // GetAppletStateChangedEvent() -> handle<copy>
  47. public ResultCode GetAppletStateChangedEvent(ServiceCtx context)
  48. {
  49. if (_stateChangedEventHandle == 0)
  50. {
  51. if (context.Process.HandleTable.GenerateHandle(_stateChangedEvent.ReadableEvent, out _stateChangedEventHandle) != KernelResult.Success)
  52. {
  53. throw new InvalidOperationException("Out of handles!");
  54. }
  55. }
  56. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_stateChangedEventHandle);
  57. return ResultCode.Success;
  58. }
  59. [Command(10)]
  60. // Start()
  61. public ResultCode Start(ServiceCtx context)
  62. {
  63. return (ResultCode)_applet.Start(_normalSession.GetConsumer(), _interactiveSession.GetConsumer());
  64. }
  65. [Command(30)]
  66. // GetResult()
  67. public ResultCode GetResult(ServiceCtx context)
  68. {
  69. return (ResultCode)_applet.GetResult();
  70. }
  71. [Command(60)]
  72. // PresetLibraryAppletGpuTimeSliceZero()
  73. public ResultCode PresetLibraryAppletGpuTimeSliceZero(ServiceCtx context)
  74. {
  75. // NOTE: This call reset two internal fields to 0 and one internal field to "true".
  76. // It seems to be used only with software keyboard inline.
  77. // Since we doesn't support applets for now, it's fine to stub it.
  78. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  79. return ResultCode.Success;
  80. }
  81. [Command(100)]
  82. // PushInData(object<nn::am::service::IStorage>)
  83. public ResultCode PushInData(ServiceCtx context)
  84. {
  85. IStorage data = GetObject<IStorage>(context, 0);
  86. _normalSession.Push(data.Data);
  87. return ResultCode.Success;
  88. }
  89. [Command(101)]
  90. // PopOutData() -> object<nn::am::service::IStorage>
  91. public ResultCode PopOutData(ServiceCtx context)
  92. {
  93. if(_normalSession.TryPop(out byte[] data))
  94. {
  95. MakeObject(context, new IStorage(data));
  96. _normalOutDataEvent.WritableEvent.Clear();
  97. return ResultCode.Success;
  98. }
  99. return ResultCode.NotAvailable;
  100. }
  101. [Command(103)]
  102. // PushInteractiveInData(object<nn::am::service::IStorage>)
  103. public ResultCode PushInteractiveInData(ServiceCtx context)
  104. {
  105. IStorage data = GetObject<IStorage>(context, 0);
  106. _interactiveSession.Push(data.Data);
  107. return ResultCode.Success;
  108. }
  109. [Command(104)]
  110. // PopInteractiveOutData() -> object<nn::am::service::IStorage>
  111. public ResultCode PopInteractiveOutData(ServiceCtx context)
  112. {
  113. if(_interactiveSession.TryPop(out byte[] data))
  114. {
  115. MakeObject(context, new IStorage(data));
  116. _interactiveOutDataEvent.WritableEvent.Clear();
  117. return ResultCode.Success;
  118. }
  119. return ResultCode.NotAvailable;
  120. }
  121. [Command(105)]
  122. // GetPopOutDataEvent() -> handle<copy>
  123. public ResultCode GetPopOutDataEvent(ServiceCtx context)
  124. {
  125. if (_normalOutDataEventHandle == 0)
  126. {
  127. if (context.Process.HandleTable.GenerateHandle(_normalOutDataEvent.ReadableEvent, out _normalOutDataEventHandle) != KernelResult.Success)
  128. {
  129. throw new InvalidOperationException("Out of handles!");
  130. }
  131. }
  132. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_normalOutDataEventHandle);
  133. return ResultCode.Success;
  134. }
  135. [Command(106)]
  136. // GetPopInteractiveOutDataEvent() -> handle<copy>
  137. public ResultCode GetPopInteractiveOutDataEvent(ServiceCtx context)
  138. {
  139. if (_interactiveOutDataEventHandle == 0)
  140. {
  141. if (context.Process.HandleTable.GenerateHandle(_interactiveOutDataEvent.ReadableEvent, out _interactiveOutDataEventHandle) != KernelResult.Success)
  142. {
  143. throw new InvalidOperationException("Out of handles!");
  144. }
  145. }
  146. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_interactiveOutDataEventHandle);
  147. return ResultCode.Success;
  148. }
  149. [Command(110)]
  150. // NeedsToExitProcess()
  151. public ResultCode NeedsToExitProcess(ServiceCtx context)
  152. {
  153. return ResultCode.Stubbed;
  154. }
  155. [Command(150)]
  156. // RequestForAppletToGetForeground()
  157. public ResultCode RequestForAppletToGetForeground(ServiceCtx context)
  158. {
  159. return ResultCode.Stubbed;
  160. }
  161. [Command(160)] // 2.0.0+
  162. // GetIndirectLayerConsumerHandle() -> u64 indirect_layer_consumer_handle
  163. public ResultCode GetIndirectLayerConsumerHandle(ServiceCtx context)
  164. {
  165. /*
  166. if (indirectLayerConsumer == null)
  167. {
  168. return ResultCode.ObjectInvalid;
  169. }
  170. */
  171. // TODO: Official sw uses this during LibraryApplet creation when LibraryAppletMode is 0x3.
  172. // Since we don't support IndirectLayer and the handle couldn't be 0, it's fine to return 1.
  173. ulong indirectLayerConsumerHandle = 1;
  174. context.ResponseData.Write(indirectLayerConsumerHandle);
  175. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { indirectLayerConsumerHandle });
  176. return ResultCode.Success;
  177. }
  178. }
  179. }