ILibraryAppletAccessor.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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(100)]
  72. // PushInData(object<nn::am::service::IStorage>)
  73. public ResultCode PushInData(ServiceCtx context)
  74. {
  75. IStorage data = GetObject<IStorage>(context, 0);
  76. _normalSession.Push(data.Data);
  77. return ResultCode.Success;
  78. }
  79. [Command(101)]
  80. // PopOutData() -> object<nn::am::service::IStorage>
  81. public ResultCode PopOutData(ServiceCtx context)
  82. {
  83. if(_normalSession.TryPop(out byte[] data))
  84. {
  85. MakeObject(context, new IStorage(data));
  86. _normalOutDataEvent.WritableEvent.Clear();
  87. return ResultCode.Success;
  88. }
  89. return ResultCode.NotAvailable;
  90. }
  91. [Command(103)]
  92. // PushInteractiveInData(object<nn::am::service::IStorage>)
  93. public ResultCode PushInteractiveInData(ServiceCtx context)
  94. {
  95. IStorage data = GetObject<IStorage>(context, 0);
  96. _interactiveSession.Push(data.Data);
  97. return ResultCode.Success;
  98. }
  99. [Command(104)]
  100. // PopInteractiveOutData() -> object<nn::am::service::IStorage>
  101. public ResultCode PopInteractiveOutData(ServiceCtx context)
  102. {
  103. if(_interactiveSession.TryPop(out byte[] data))
  104. {
  105. MakeObject(context, new IStorage(data));
  106. _interactiveOutDataEvent.WritableEvent.Clear();
  107. return ResultCode.Success;
  108. }
  109. return ResultCode.NotAvailable;
  110. }
  111. [Command(105)]
  112. // GetPopOutDataEvent() -> handle<copy>
  113. public ResultCode GetPopOutDataEvent(ServiceCtx context)
  114. {
  115. if (_normalOutDataEventHandle == 0)
  116. {
  117. if (context.Process.HandleTable.GenerateHandle(_normalOutDataEvent.ReadableEvent, out _normalOutDataEventHandle) != KernelResult.Success)
  118. {
  119. throw new InvalidOperationException("Out of handles!");
  120. }
  121. }
  122. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_normalOutDataEventHandle);
  123. return ResultCode.Success;
  124. }
  125. [Command(106)]
  126. // GetPopInteractiveOutDataEvent() -> handle<copy>
  127. public ResultCode GetPopInteractiveOutDataEvent(ServiceCtx context)
  128. {
  129. if (_interactiveOutDataEventHandle == 0)
  130. {
  131. if (context.Process.HandleTable.GenerateHandle(_interactiveOutDataEvent.ReadableEvent, out _interactiveOutDataEventHandle) != KernelResult.Success)
  132. {
  133. throw new InvalidOperationException("Out of handles!");
  134. }
  135. }
  136. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_interactiveOutDataEventHandle);
  137. return ResultCode.Success;
  138. }
  139. [Command(110)]
  140. // NeedsToExitProcess()
  141. public ResultCode NeedsToExitProcess(ServiceCtx context)
  142. {
  143. return ResultCode.Stubbed;
  144. }
  145. [Command(150)]
  146. // RequestForAppletToGetForeground()
  147. public ResultCode RequestForAppletToGetForeground(ServiceCtx context)
  148. {
  149. return ResultCode.Stubbed;
  150. }
  151. [Command(160)] // 2.0.0+
  152. // GetIndirectLayerConsumerHandle() -> u64 indirect_layer_consumer_handle
  153. public ResultCode GetIndirectLayerConsumerHandle(ServiceCtx context)
  154. {
  155. /*
  156. if (indirectLayerConsumer == null)
  157. {
  158. return ResultCode.ObjectInvalid;
  159. }
  160. */
  161. // TODO: Official sw uses this during LibraryApplet creation when LibraryAppletMode is 0x3.
  162. // Since we don't support IndirectLayer and the handle couldn't be 0, it's fine to return 1.
  163. ulong indirectLayerConsumerHandle = 1;
  164. context.ResponseData.Write(indirectLayerConsumerHandle);
  165. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { indirectLayerConsumerHandle });
  166. return ResultCode.Success;
  167. }
  168. }
  169. }