ISelfController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using System;
  6. namespace Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.SystemAppletProxy
  7. {
  8. class ISelfController : IpcService
  9. {
  10. private readonly long _pid;
  11. private KEvent _libraryAppletLaunchableEvent;
  12. private int _libraryAppletLaunchableEventHandle;
  13. private KEvent _accumulatedSuspendedTickChangedEvent;
  14. private int _accumulatedSuspendedTickChangedEventHandle;
  15. private object _fatalSectionLock = new object();
  16. private int _fatalSectionCount;
  17. // TODO: Set this when the game goes in suspension (go back to home menu ect), we currently don't support that so we can keep it set to 0.
  18. private ulong _accumulatedSuspendedTickValue = 0;
  19. // TODO: Determine where those fields are used.
  20. private bool _screenShotPermission = false;
  21. private bool _operationModeChangedNotification = false;
  22. private bool _performanceModeChangedNotification = false;
  23. private bool _restartMessageEnabled = false;
  24. private bool _outOfFocusSuspendingEnabled = false;
  25. private bool _handlesRequestToDisplay = false;
  26. private bool _autoSleepDisabled = false;
  27. private bool _albumImageTakenNotificationEnabled = false;
  28. private uint _screenShotImageOrientation = 0;
  29. private uint _idleTimeDetectionExtension = 0;
  30. public ISelfController(Horizon system, long pid)
  31. {
  32. _libraryAppletLaunchableEvent = new KEvent(system.KernelContext);
  33. _pid = pid;
  34. }
  35. [Command(0)]
  36. // Exit()
  37. public ResultCode Exit(ServiceCtx context)
  38. {
  39. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  40. return ResultCode.Success;
  41. }
  42. [Command(1)]
  43. // LockExit()
  44. public ResultCode LockExit(ServiceCtx context)
  45. {
  46. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  47. return ResultCode.Success;
  48. }
  49. [Command(2)]
  50. // UnlockExit()
  51. public ResultCode UnlockExit(ServiceCtx context)
  52. {
  53. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  54. return ResultCode.Success;
  55. }
  56. [Command(3)] // 2.0.0+
  57. // EnterFatalSection()
  58. public ResultCode EnterFatalSection(ServiceCtx context)
  59. {
  60. lock (_fatalSectionLock)
  61. {
  62. _fatalSectionCount++;
  63. }
  64. return ResultCode.Success;
  65. }
  66. [Command(4)] // 2.0.0+
  67. // LeaveFatalSection()
  68. public ResultCode LeaveFatalSection(ServiceCtx context)
  69. {
  70. ResultCode result = ResultCode.Success;
  71. lock (_fatalSectionLock)
  72. {
  73. if (_fatalSectionCount != 0)
  74. {
  75. _fatalSectionCount--;
  76. }
  77. else
  78. {
  79. result = ResultCode.UnbalancedFatalSection;
  80. }
  81. }
  82. return result;
  83. }
  84. [Command(9)]
  85. // GetLibraryAppletLaunchableEvent() -> handle<copy>
  86. public ResultCode GetLibraryAppletLaunchableEvent(ServiceCtx context)
  87. {
  88. _libraryAppletLaunchableEvent.ReadableEvent.Signal();
  89. if (_libraryAppletLaunchableEventHandle == 0)
  90. {
  91. if (context.Process.HandleTable.GenerateHandle(_libraryAppletLaunchableEvent.ReadableEvent, out _libraryAppletLaunchableEventHandle) != KernelResult.Success)
  92. {
  93. throw new InvalidOperationException("Out of handles!");
  94. }
  95. }
  96. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_libraryAppletLaunchableEventHandle);
  97. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  98. return ResultCode.Success;
  99. }
  100. [Command(10)]
  101. // SetScreenShotPermission(u32)
  102. public ResultCode SetScreenShotPermission(ServiceCtx context)
  103. {
  104. bool screenShotPermission = context.RequestData.ReadBoolean();
  105. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { screenShotPermission });
  106. _screenShotPermission = screenShotPermission;
  107. return ResultCode.Success;
  108. }
  109. [Command(11)]
  110. // SetOperationModeChangedNotification(b8)
  111. public ResultCode SetOperationModeChangedNotification(ServiceCtx context)
  112. {
  113. bool operationModeChangedNotification = context.RequestData.ReadBoolean();
  114. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { operationModeChangedNotification });
  115. _operationModeChangedNotification = operationModeChangedNotification;
  116. return ResultCode.Success;
  117. }
  118. [Command(12)]
  119. // SetPerformanceModeChangedNotification(b8)
  120. public ResultCode SetPerformanceModeChangedNotification(ServiceCtx context)
  121. {
  122. bool performanceModeChangedNotification = context.RequestData.ReadBoolean();
  123. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { performanceModeChangedNotification });
  124. _performanceModeChangedNotification = performanceModeChangedNotification;
  125. return ResultCode.Success;
  126. }
  127. [Command(13)]
  128. // SetFocusHandlingMode(b8, b8, b8)
  129. public ResultCode SetFocusHandlingMode(ServiceCtx context)
  130. {
  131. bool unknownFlag1 = context.RequestData.ReadBoolean();
  132. bool unknownFlag2 = context.RequestData.ReadBoolean();
  133. bool unknownFlag3 = context.RequestData.ReadBoolean();
  134. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { unknownFlag1, unknownFlag2, unknownFlag3 });
  135. return ResultCode.Success;
  136. }
  137. [Command(14)]
  138. // SetRestartMessageEnabled(b8)
  139. public ResultCode SetRestartMessageEnabled(ServiceCtx context)
  140. {
  141. bool restartMessageEnabled = context.RequestData.ReadBoolean();
  142. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { restartMessageEnabled });
  143. _restartMessageEnabled = restartMessageEnabled;
  144. return ResultCode.Success;
  145. }
  146. [Command(16)] // 2.0.0+
  147. // SetOutOfFocusSuspendingEnabled(b8)
  148. public ResultCode SetOutOfFocusSuspendingEnabled(ServiceCtx context)
  149. {
  150. bool outOfFocusSuspendingEnabled = context.RequestData.ReadBoolean();
  151. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { outOfFocusSuspendingEnabled });
  152. _outOfFocusSuspendingEnabled = outOfFocusSuspendingEnabled;
  153. return ResultCode.Success;
  154. }
  155. [Command(19)] // 3.0.0+
  156. // SetScreenShotImageOrientation(u32)
  157. public ResultCode SetScreenShotImageOrientation(ServiceCtx context)
  158. {
  159. uint screenShotImageOrientation = context.RequestData.ReadUInt32();
  160. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { screenShotImageOrientation });
  161. _screenShotImageOrientation = screenShotImageOrientation;
  162. return ResultCode.Success;
  163. }
  164. [Command(40)]
  165. // CreateManagedDisplayLayer() -> u64
  166. public ResultCode CreateManagedDisplayLayer(ServiceCtx context)
  167. {
  168. context.Device.System.SurfaceFlinger.CreateLayer(_pid, out long layerId);
  169. context.ResponseData.Write(layerId);
  170. return ResultCode.Success;
  171. }
  172. [Command(44)] // 10.0.0+
  173. // CreateManagedDisplaySeparableLayer() -> (u64, u64)
  174. public ResultCode CreateManagedDisplaySeparableLayer(ServiceCtx context)
  175. {
  176. // NOTE: first create the recoding layer and then the display one because right now Surface Flinger only use the last id.
  177. context.Device.System.SurfaceFlinger.CreateLayer(_pid, out long recordingLayerId);
  178. context.Device.System.SurfaceFlinger.CreateLayer(_pid, out long displayLayerId);
  179. context.ResponseData.Write(displayLayerId);
  180. context.ResponseData.Write(recordingLayerId);
  181. return ResultCode.Success;
  182. }
  183. [Command(50)]
  184. // SetHandlesRequestToDisplay(b8)
  185. public ResultCode SetHandlesRequestToDisplay(ServiceCtx context)
  186. {
  187. bool handlesRequestToDisplay = context.RequestData.ReadBoolean();
  188. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { handlesRequestToDisplay });
  189. _handlesRequestToDisplay = handlesRequestToDisplay;
  190. return ResultCode.Success;
  191. }
  192. [Command(62)]
  193. // SetIdleTimeDetectionExtension(u32)
  194. public ResultCode SetIdleTimeDetectionExtension(ServiceCtx context)
  195. {
  196. uint idleTimeDetectionExtension = context.RequestData.ReadUInt32();
  197. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { idleTimeDetectionExtension });
  198. _idleTimeDetectionExtension = idleTimeDetectionExtension;
  199. return ResultCode.Success;
  200. }
  201. [Command(63)]
  202. // GetIdleTimeDetectionExtension() -> u32
  203. public ResultCode GetIdleTimeDetectionExtension(ServiceCtx context)
  204. {
  205. context.ResponseData.Write(_idleTimeDetectionExtension);
  206. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { _idleTimeDetectionExtension });
  207. return ResultCode.Success;
  208. }
  209. [Command(68)]
  210. // SetAutoSleepDisabled(u8)
  211. public ResultCode SetAutoSleepDisabled(ServiceCtx context)
  212. {
  213. bool autoSleepDisabled = context.RequestData.ReadBoolean();
  214. _autoSleepDisabled = autoSleepDisabled;
  215. return ResultCode.Success;
  216. }
  217. [Command(69)]
  218. // IsAutoSleepDisabled() -> u8
  219. public ResultCode IsAutoSleepDisabled(ServiceCtx context)
  220. {
  221. context.ResponseData.Write(_autoSleepDisabled);
  222. return ResultCode.Success;
  223. }
  224. [Command(90)] // 6.0.0+
  225. // GetAccumulatedSuspendedTickValue() -> u64
  226. public ResultCode GetAccumulatedSuspendedTickValue(ServiceCtx context)
  227. {
  228. context.ResponseData.Write(_accumulatedSuspendedTickValue);
  229. return ResultCode.Success;
  230. }
  231. [Command(91)] // 6.0.0+
  232. // GetAccumulatedSuspendedTickChangedEvent() -> handle<copy>
  233. public ResultCode GetAccumulatedSuspendedTickChangedEvent(ServiceCtx context)
  234. {
  235. if (_accumulatedSuspendedTickChangedEventHandle == 0)
  236. {
  237. _accumulatedSuspendedTickChangedEvent = new KEvent(context.Device.System.KernelContext);
  238. _accumulatedSuspendedTickChangedEvent.ReadableEvent.Signal();
  239. if (context.Process.HandleTable.GenerateHandle(_accumulatedSuspendedTickChangedEvent.ReadableEvent, out _accumulatedSuspendedTickChangedEventHandle) != KernelResult.Success)
  240. {
  241. throw new InvalidOperationException("Out of handles!");
  242. }
  243. }
  244. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_accumulatedSuspendedTickChangedEventHandle);
  245. return ResultCode.Success;
  246. }
  247. [Command(100)] // 7.0.0+
  248. // SetAlbumImageTakenNotificationEnabled(u8)
  249. public ResultCode SetAlbumImageTakenNotificationEnabled(ServiceCtx context)
  250. {
  251. bool albumImageTakenNotificationEnabled = context.RequestData.ReadBoolean();
  252. _albumImageTakenNotificationEnabled = albumImageTakenNotificationEnabled;
  253. return ResultCode.Success;
  254. }
  255. }
  256. }