ISelfController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.SystemAppletProxy.Types;
  6. using System;
  7. namespace Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.SystemAppletProxy
  8. {
  9. class ISelfController : IpcService
  10. {
  11. private readonly ulong _pid;
  12. private KEvent _libraryAppletLaunchableEvent;
  13. private int _libraryAppletLaunchableEventHandle;
  14. private KEvent _accumulatedSuspendedTickChangedEvent;
  15. private int _accumulatedSuspendedTickChangedEventHandle;
  16. private object _fatalSectionLock = new object();
  17. private int _fatalSectionCount;
  18. // 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.
  19. private ulong _accumulatedSuspendedTickValue = 0;
  20. // TODO: Determine where those fields are used.
  21. private bool _screenShotPermission = false;
  22. private bool _operationModeChangedNotification = false;
  23. private bool _performanceModeChangedNotification = false;
  24. private bool _restartMessageEnabled = false;
  25. private bool _outOfFocusSuspendingEnabled = false;
  26. private bool _handlesRequestToDisplay = false;
  27. private bool _autoSleepDisabled = false;
  28. private bool _albumImageTakenNotificationEnabled = false;
  29. private uint _screenShotImageOrientation = 0;
  30. private uint _idleTimeDetectionExtension = 0;
  31. public ISelfController(ServiceCtx context, ulong pid)
  32. {
  33. _libraryAppletLaunchableEvent = new KEvent(context.Device.System.KernelContext);
  34. _pid = pid;
  35. }
  36. [CommandHipc(0)]
  37. // Exit()
  38. public ResultCode Exit(ServiceCtx context)
  39. {
  40. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  41. return ResultCode.Success;
  42. }
  43. [CommandHipc(1)]
  44. // LockExit()
  45. public ResultCode LockExit(ServiceCtx context)
  46. {
  47. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  48. return ResultCode.Success;
  49. }
  50. [CommandHipc(2)]
  51. // UnlockExit()
  52. public ResultCode UnlockExit(ServiceCtx context)
  53. {
  54. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  55. return ResultCode.Success;
  56. }
  57. [CommandHipc(3)] // 2.0.0+
  58. // EnterFatalSection()
  59. public ResultCode EnterFatalSection(ServiceCtx context)
  60. {
  61. lock (_fatalSectionLock)
  62. {
  63. _fatalSectionCount++;
  64. }
  65. return ResultCode.Success;
  66. }
  67. [CommandHipc(4)] // 2.0.0+
  68. // LeaveFatalSection()
  69. public ResultCode LeaveFatalSection(ServiceCtx context)
  70. {
  71. ResultCode result = ResultCode.Success;
  72. lock (_fatalSectionLock)
  73. {
  74. if (_fatalSectionCount != 0)
  75. {
  76. _fatalSectionCount--;
  77. }
  78. else
  79. {
  80. result = ResultCode.UnbalancedFatalSection;
  81. }
  82. }
  83. return result;
  84. }
  85. [CommandHipc(9)]
  86. // GetLibraryAppletLaunchableEvent() -> handle<copy>
  87. public ResultCode GetLibraryAppletLaunchableEvent(ServiceCtx context)
  88. {
  89. _libraryAppletLaunchableEvent.ReadableEvent.Signal();
  90. if (_libraryAppletLaunchableEventHandle == 0)
  91. {
  92. if (context.Process.HandleTable.GenerateHandle(_libraryAppletLaunchableEvent.ReadableEvent, out _libraryAppletLaunchableEventHandle) != KernelResult.Success)
  93. {
  94. throw new InvalidOperationException("Out of handles!");
  95. }
  96. }
  97. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_libraryAppletLaunchableEventHandle);
  98. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  99. return ResultCode.Success;
  100. }
  101. [CommandHipc(10)]
  102. // SetScreenShotPermission(u32)
  103. public ResultCode SetScreenShotPermission(ServiceCtx context)
  104. {
  105. bool screenShotPermission = context.RequestData.ReadBoolean();
  106. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { screenShotPermission });
  107. _screenShotPermission = screenShotPermission;
  108. return ResultCode.Success;
  109. }
  110. [CommandHipc(11)]
  111. // SetOperationModeChangedNotification(b8)
  112. public ResultCode SetOperationModeChangedNotification(ServiceCtx context)
  113. {
  114. bool operationModeChangedNotification = context.RequestData.ReadBoolean();
  115. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { operationModeChangedNotification });
  116. _operationModeChangedNotification = operationModeChangedNotification;
  117. return ResultCode.Success;
  118. }
  119. [CommandHipc(12)]
  120. // SetPerformanceModeChangedNotification(b8)
  121. public ResultCode SetPerformanceModeChangedNotification(ServiceCtx context)
  122. {
  123. bool performanceModeChangedNotification = context.RequestData.ReadBoolean();
  124. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { performanceModeChangedNotification });
  125. _performanceModeChangedNotification = performanceModeChangedNotification;
  126. return ResultCode.Success;
  127. }
  128. [CommandHipc(13)]
  129. // SetFocusHandlingMode(b8, b8, b8)
  130. public ResultCode SetFocusHandlingMode(ServiceCtx context)
  131. {
  132. bool unknownFlag1 = context.RequestData.ReadBoolean();
  133. bool unknownFlag2 = context.RequestData.ReadBoolean();
  134. bool unknownFlag3 = context.RequestData.ReadBoolean();
  135. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { unknownFlag1, unknownFlag2, unknownFlag3 });
  136. return ResultCode.Success;
  137. }
  138. [CommandHipc(14)]
  139. // SetRestartMessageEnabled(b8)
  140. public ResultCode SetRestartMessageEnabled(ServiceCtx context)
  141. {
  142. bool restartMessageEnabled = context.RequestData.ReadBoolean();
  143. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { restartMessageEnabled });
  144. _restartMessageEnabled = restartMessageEnabled;
  145. return ResultCode.Success;
  146. }
  147. [CommandHipc(16)] // 2.0.0+
  148. // SetOutOfFocusSuspendingEnabled(b8)
  149. public ResultCode SetOutOfFocusSuspendingEnabled(ServiceCtx context)
  150. {
  151. bool outOfFocusSuspendingEnabled = context.RequestData.ReadBoolean();
  152. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { outOfFocusSuspendingEnabled });
  153. _outOfFocusSuspendingEnabled = outOfFocusSuspendingEnabled;
  154. return ResultCode.Success;
  155. }
  156. [CommandHipc(19)] // 3.0.0+
  157. // SetScreenShotImageOrientation(u32)
  158. public ResultCode SetScreenShotImageOrientation(ServiceCtx context)
  159. {
  160. uint screenShotImageOrientation = context.RequestData.ReadUInt32();
  161. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { screenShotImageOrientation });
  162. _screenShotImageOrientation = screenShotImageOrientation;
  163. return ResultCode.Success;
  164. }
  165. [CommandHipc(40)]
  166. // CreateManagedDisplayLayer() -> u64
  167. public ResultCode CreateManagedDisplayLayer(ServiceCtx context)
  168. {
  169. context.Device.System.SurfaceFlinger.CreateLayer(out long layerId, _pid);
  170. context.Device.System.SurfaceFlinger.SetRenderLayer(layerId);
  171. context.ResponseData.Write(layerId);
  172. return ResultCode.Success;
  173. }
  174. [CommandHipc(41)] // 4.0.0+
  175. // IsSystemBufferSharingEnabled()
  176. public ResultCode IsSystemBufferSharingEnabled(ServiceCtx context)
  177. {
  178. // NOTE: Service checks a private field and return an error if the SystemBufferSharing is disabled.
  179. return ResultCode.NotImplemented;
  180. }
  181. [CommandHipc(44)] // 10.0.0+
  182. // CreateManagedDisplaySeparableLayer() -> (u64, u64)
  183. public ResultCode CreateManagedDisplaySeparableLayer(ServiceCtx context)
  184. {
  185. context.Device.System.SurfaceFlinger.CreateLayer(out long displayLayerId, _pid);
  186. context.Device.System.SurfaceFlinger.CreateLayer(out long recordingLayerId, _pid);
  187. context.Device.System.SurfaceFlinger.SetRenderLayer(displayLayerId);
  188. context.ResponseData.Write(displayLayerId);
  189. context.ResponseData.Write(recordingLayerId);
  190. return ResultCode.Success;
  191. }
  192. [CommandHipc(50)]
  193. // SetHandlesRequestToDisplay(b8)
  194. public ResultCode SetHandlesRequestToDisplay(ServiceCtx context)
  195. {
  196. bool handlesRequestToDisplay = context.RequestData.ReadBoolean();
  197. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { handlesRequestToDisplay });
  198. _handlesRequestToDisplay = handlesRequestToDisplay;
  199. return ResultCode.Success;
  200. }
  201. [CommandHipc(62)]
  202. // SetIdleTimeDetectionExtension(u32)
  203. public ResultCode SetIdleTimeDetectionExtension(ServiceCtx context)
  204. {
  205. uint idleTimeDetectionExtension = context.RequestData.ReadUInt32();
  206. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { idleTimeDetectionExtension });
  207. _idleTimeDetectionExtension = idleTimeDetectionExtension;
  208. return ResultCode.Success;
  209. }
  210. [CommandHipc(63)]
  211. // GetIdleTimeDetectionExtension() -> u32
  212. public ResultCode GetIdleTimeDetectionExtension(ServiceCtx context)
  213. {
  214. context.ResponseData.Write(_idleTimeDetectionExtension);
  215. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { _idleTimeDetectionExtension });
  216. return ResultCode.Success;
  217. }
  218. [CommandHipc(65)]
  219. // ReportUserIsActive()
  220. public ResultCode ReportUserIsActive(ServiceCtx context)
  221. {
  222. // TODO: Call idle:sys ReportUserIsActive when implemented.
  223. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  224. return ResultCode.Success;
  225. }
  226. [CommandHipc(68)]
  227. // SetAutoSleepDisabled(u8)
  228. public ResultCode SetAutoSleepDisabled(ServiceCtx context)
  229. {
  230. bool autoSleepDisabled = context.RequestData.ReadBoolean();
  231. _autoSleepDisabled = autoSleepDisabled;
  232. return ResultCode.Success;
  233. }
  234. [CommandHipc(69)]
  235. // IsAutoSleepDisabled() -> u8
  236. public ResultCode IsAutoSleepDisabled(ServiceCtx context)
  237. {
  238. context.ResponseData.Write(_autoSleepDisabled);
  239. return ResultCode.Success;
  240. }
  241. [CommandHipc(80)] // 4.0.0+
  242. // SetWirelessPriorityMode(s32 wireless_priority_mode)
  243. public ResultCode SetWirelessPriorityMode(ServiceCtx context)
  244. {
  245. WirelessPriorityMode wirelessPriorityMode = (WirelessPriorityMode)context.RequestData.ReadInt32();
  246. if (wirelessPriorityMode > WirelessPriorityMode.Unknown2)
  247. {
  248. return ResultCode.InvalidParameters;
  249. }
  250. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { wirelessPriorityMode });
  251. return ResultCode.Success;
  252. }
  253. [CommandHipc(90)] // 6.0.0+
  254. // GetAccumulatedSuspendedTickValue() -> u64
  255. public ResultCode GetAccumulatedSuspendedTickValue(ServiceCtx context)
  256. {
  257. context.ResponseData.Write(_accumulatedSuspendedTickValue);
  258. return ResultCode.Success;
  259. }
  260. [CommandHipc(91)] // 6.0.0+
  261. // GetAccumulatedSuspendedTickChangedEvent() -> handle<copy>
  262. public ResultCode GetAccumulatedSuspendedTickChangedEvent(ServiceCtx context)
  263. {
  264. if (_accumulatedSuspendedTickChangedEventHandle == 0)
  265. {
  266. _accumulatedSuspendedTickChangedEvent = new KEvent(context.Device.System.KernelContext);
  267. _accumulatedSuspendedTickChangedEvent.ReadableEvent.Signal();
  268. if (context.Process.HandleTable.GenerateHandle(_accumulatedSuspendedTickChangedEvent.ReadableEvent, out _accumulatedSuspendedTickChangedEventHandle) != KernelResult.Success)
  269. {
  270. throw new InvalidOperationException("Out of handles!");
  271. }
  272. }
  273. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_accumulatedSuspendedTickChangedEventHandle);
  274. return ResultCode.Success;
  275. }
  276. [CommandHipc(100)] // 7.0.0+
  277. // SetAlbumImageTakenNotificationEnabled(u8)
  278. public ResultCode SetAlbumImageTakenNotificationEnabled(ServiceCtx context)
  279. {
  280. bool albumImageTakenNotificationEnabled = context.RequestData.ReadBoolean();
  281. _albumImageTakenNotificationEnabled = albumImageTakenNotificationEnabled;
  282. return ResultCode.Success;
  283. }
  284. [CommandHipc(120)] // 11.0.0+
  285. // SaveCurrentScreenshot(s32 album_report_option)
  286. public ResultCode SaveCurrentScreenshot(ServiceCtx context)
  287. {
  288. AlbumReportOption albumReportOption = (AlbumReportOption)context.RequestData.ReadInt32();
  289. if (albumReportOption > AlbumReportOption.Unknown3)
  290. {
  291. return ResultCode.InvalidParameters;
  292. }
  293. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { albumReportOption });
  294. return ResultCode.Success;
  295. }
  296. }
  297. }