ISelfController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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(ServiceCtx context, long pid)
  31. {
  32. _libraryAppletLaunchableEvent = new KEvent(context.Device.System.KernelContext);
  33. _pid = pid;
  34. }
  35. [CommandHipc(0)]
  36. // Exit()
  37. public ResultCode Exit(ServiceCtx context)
  38. {
  39. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  40. return ResultCode.Success;
  41. }
  42. [CommandHipc(1)]
  43. // LockExit()
  44. public ResultCode LockExit(ServiceCtx context)
  45. {
  46. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  47. return ResultCode.Success;
  48. }
  49. [CommandHipc(2)]
  50. // UnlockExit()
  51. public ResultCode UnlockExit(ServiceCtx context)
  52. {
  53. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  54. return ResultCode.Success;
  55. }
  56. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(40)]
  165. // CreateManagedDisplayLayer() -> u64
  166. public ResultCode CreateManagedDisplayLayer(ServiceCtx context)
  167. {
  168. context.Device.System.SurfaceFlinger.CreateLayer(_pid, out long layerId);
  169. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  170. context.ResponseData.Write(layerId);
  171. return ResultCode.Success;
  172. }
  173. [CommandHipc(41)] // 4.0.0+
  174. // IsSystemBufferSharingEnabled()
  175. public ResultCode IsSystemBufferSharingEnabled(ServiceCtx context)
  176. {
  177. // NOTE: Service checks a private field and return an error if the SystemBufferSharing is disabled.
  178. return ResultCode.NotImplemented;
  179. }
  180. [CommandHipc(44)] // 10.0.0+
  181. // CreateManagedDisplaySeparableLayer() -> (u64, u64)
  182. public ResultCode CreateManagedDisplaySeparableLayer(ServiceCtx context)
  183. {
  184. context.Device.System.SurfaceFlinger.CreateLayer(_pid, out long displayLayerId);
  185. context.Device.System.SurfaceFlinger.CreateLayer(_pid, out long recordingLayerId);
  186. context.Device.System.SurfaceFlinger.SetRenderLayer(displayLayerId);
  187. context.ResponseData.Write(displayLayerId);
  188. context.ResponseData.Write(recordingLayerId);
  189. return ResultCode.Success;
  190. }
  191. [CommandHipc(50)]
  192. // SetHandlesRequestToDisplay(b8)
  193. public ResultCode SetHandlesRequestToDisplay(ServiceCtx context)
  194. {
  195. bool handlesRequestToDisplay = context.RequestData.ReadBoolean();
  196. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { handlesRequestToDisplay });
  197. _handlesRequestToDisplay = handlesRequestToDisplay;
  198. return ResultCode.Success;
  199. }
  200. [CommandHipc(62)]
  201. // SetIdleTimeDetectionExtension(u32)
  202. public ResultCode SetIdleTimeDetectionExtension(ServiceCtx context)
  203. {
  204. uint idleTimeDetectionExtension = context.RequestData.ReadUInt32();
  205. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { idleTimeDetectionExtension });
  206. _idleTimeDetectionExtension = idleTimeDetectionExtension;
  207. return ResultCode.Success;
  208. }
  209. [CommandHipc(63)]
  210. // GetIdleTimeDetectionExtension() -> u32
  211. public ResultCode GetIdleTimeDetectionExtension(ServiceCtx context)
  212. {
  213. context.ResponseData.Write(_idleTimeDetectionExtension);
  214. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { _idleTimeDetectionExtension });
  215. return ResultCode.Success;
  216. }
  217. [CommandHipc(65)]
  218. // ReportUserIsActive()
  219. public ResultCode ReportUserIsActive(ServiceCtx context)
  220. {
  221. // TODO: Call idle:sys ReportUserIsActive when implemented.
  222. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  223. return ResultCode.Success;
  224. }
  225. [CommandHipc(68)]
  226. // SetAutoSleepDisabled(u8)
  227. public ResultCode SetAutoSleepDisabled(ServiceCtx context)
  228. {
  229. bool autoSleepDisabled = context.RequestData.ReadBoolean();
  230. _autoSleepDisabled = autoSleepDisabled;
  231. return ResultCode.Success;
  232. }
  233. [CommandHipc(69)]
  234. // IsAutoSleepDisabled() -> u8
  235. public ResultCode IsAutoSleepDisabled(ServiceCtx context)
  236. {
  237. context.ResponseData.Write(_autoSleepDisabled);
  238. return ResultCode.Success;
  239. }
  240. [CommandHipc(90)] // 6.0.0+
  241. // GetAccumulatedSuspendedTickValue() -> u64
  242. public ResultCode GetAccumulatedSuspendedTickValue(ServiceCtx context)
  243. {
  244. context.ResponseData.Write(_accumulatedSuspendedTickValue);
  245. return ResultCode.Success;
  246. }
  247. [CommandHipc(91)] // 6.0.0+
  248. // GetAccumulatedSuspendedTickChangedEvent() -> handle<copy>
  249. public ResultCode GetAccumulatedSuspendedTickChangedEvent(ServiceCtx context)
  250. {
  251. if (_accumulatedSuspendedTickChangedEventHandle == 0)
  252. {
  253. _accumulatedSuspendedTickChangedEvent = new KEvent(context.Device.System.KernelContext);
  254. _accumulatedSuspendedTickChangedEvent.ReadableEvent.Signal();
  255. if (context.Process.HandleTable.GenerateHandle(_accumulatedSuspendedTickChangedEvent.ReadableEvent, out _accumulatedSuspendedTickChangedEventHandle) != KernelResult.Success)
  256. {
  257. throw new InvalidOperationException("Out of handles!");
  258. }
  259. }
  260. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_accumulatedSuspendedTickChangedEventHandle);
  261. return ResultCode.Success;
  262. }
  263. [CommandHipc(100)] // 7.0.0+
  264. // SetAlbumImageTakenNotificationEnabled(u8)
  265. public ResultCode SetAlbumImageTakenNotificationEnabled(ServiceCtx context)
  266. {
  267. bool albumImageTakenNotificationEnabled = context.RequestData.ReadBoolean();
  268. _albumImageTakenNotificationEnabled = albumImageTakenNotificationEnabled;
  269. return ResultCode.Success;
  270. }
  271. }
  272. }