ICommonStateGetter.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.Settings.Types;
  6. using Ryujinx.HLE.HOS.Services.Vi.RootService.ApplicationDisplayService;
  7. using Ryujinx.HLE.HOS.SystemState;
  8. using System;
  9. namespace Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.SystemAppletProxy
  10. {
  11. class ICommonStateGetter : IpcService
  12. {
  13. private Apm.ManagerServer _apmManagerServer;
  14. private Apm.SystemManagerServer _apmSystemManagerServer;
  15. private Lbl.LblControllerServer _lblControllerServer;
  16. private bool _vrModeEnabled;
  17. #pragma warning disable CS0414
  18. private bool _lcdBacklighOffEnabled;
  19. private bool _requestExitToLibraryAppletAtExecuteNextProgramEnabled;
  20. #pragma warning restore CS0414
  21. private int _messageEventHandle;
  22. private int _displayResolutionChangedEventHandle;
  23. public ICommonStateGetter(ServiceCtx context)
  24. {
  25. _apmManagerServer = new Apm.ManagerServer(context);
  26. _apmSystemManagerServer = new Apm.SystemManagerServer(context);
  27. _lblControllerServer = new Lbl.LblControllerServer(context);
  28. }
  29. [CommandHipc(0)]
  30. // GetEventHandle() -> handle<copy>
  31. public ResultCode GetEventHandle(ServiceCtx context)
  32. {
  33. KEvent messageEvent = context.Device.System.AppletState.MessageEvent;
  34. if (_messageEventHandle == 0)
  35. {
  36. if (context.Process.HandleTable.GenerateHandle(messageEvent.ReadableEvent, out _messageEventHandle) != KernelResult.Success)
  37. {
  38. throw new InvalidOperationException("Out of handles!");
  39. }
  40. }
  41. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_messageEventHandle);
  42. return ResultCode.Success;
  43. }
  44. [CommandHipc(1)]
  45. // ReceiveMessage() -> nn::am::AppletMessage
  46. public ResultCode ReceiveMessage(ServiceCtx context)
  47. {
  48. if (!context.Device.System.AppletState.Messages.TryDequeue(out AppletMessage message))
  49. {
  50. return ResultCode.NoMessages;
  51. }
  52. KEvent messageEvent = context.Device.System.AppletState.MessageEvent;
  53. // NOTE: Service checks if current states are different than the stored ones.
  54. // Since we don't support any states for now, it's fine to check if there is still messages available.
  55. if (context.Device.System.AppletState.Messages.IsEmpty)
  56. {
  57. messageEvent.ReadableEvent.Clear();
  58. }
  59. else
  60. {
  61. messageEvent.ReadableEvent.Signal();
  62. }
  63. context.ResponseData.Write((int)message);
  64. return ResultCode.Success;
  65. }
  66. [CommandHipc(5)]
  67. // GetOperationMode() -> u8
  68. public ResultCode GetOperationMode(ServiceCtx context)
  69. {
  70. OperationMode mode = context.Device.System.State.DockedMode
  71. ? OperationMode.Docked
  72. : OperationMode.Handheld;
  73. context.ResponseData.Write((byte)mode);
  74. return ResultCode.Success;
  75. }
  76. [CommandHipc(6)]
  77. // GetPerformanceMode() -> nn::apm::PerformanceMode
  78. public ResultCode GetPerformanceMode(ServiceCtx context)
  79. {
  80. return (ResultCode)_apmManagerServer.GetPerformanceMode(context);
  81. }
  82. [CommandHipc(8)]
  83. // GetBootMode() -> u8
  84. public ResultCode GetBootMode(ServiceCtx context)
  85. {
  86. context.ResponseData.Write((byte)0); //Unknown value.
  87. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  88. return ResultCode.Success;
  89. }
  90. [CommandHipc(9)]
  91. // GetCurrentFocusState() -> u8
  92. public ResultCode GetCurrentFocusState(ServiceCtx context)
  93. {
  94. context.ResponseData.Write((byte)context.Device.System.AppletState.FocusState);
  95. return ResultCode.Success;
  96. }
  97. [CommandHipc(50)] // 3.0.0+
  98. // IsVrModeEnabled() -> b8
  99. public ResultCode IsVrModeEnabled(ServiceCtx context)
  100. {
  101. context.ResponseData.Write(_vrModeEnabled);
  102. return ResultCode.Success;
  103. }
  104. [CommandHipc(51)] // 3.0.0+
  105. // SetVrModeEnabled(b8)
  106. public ResultCode SetVrModeEnabled(ServiceCtx context)
  107. {
  108. bool vrModeEnabled = context.RequestData.ReadBoolean();
  109. UpdateVrMode(vrModeEnabled);
  110. return ResultCode.Success;
  111. }
  112. [CommandHipc(52)] // 4.0.0+
  113. // SetLcdBacklighOffEnabled(b8)
  114. public ResultCode SetLcdBacklighOffEnabled(ServiceCtx context)
  115. {
  116. // NOTE: Service sets a private field here, maybe this field is used somewhere else to turned off the backlight.
  117. // Since we don't support backlight, it's fine to do nothing.
  118. _lcdBacklighOffEnabled = context.RequestData.ReadBoolean();
  119. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  120. return ResultCode.Success;
  121. }
  122. [CommandHipc(53)] // 7.0.0+
  123. // BeginVrModeEx()
  124. public ResultCode BeginVrModeEx(ServiceCtx context)
  125. {
  126. UpdateVrMode(true);
  127. return ResultCode.Success;
  128. }
  129. [CommandHipc(54)] // 7.0.0+
  130. // EndVrModeEx()
  131. public ResultCode EndVrModeEx(ServiceCtx context)
  132. {
  133. UpdateVrMode(false);
  134. return ResultCode.Success;
  135. }
  136. private void UpdateVrMode(bool vrModeEnabled)
  137. {
  138. if (_vrModeEnabled == vrModeEnabled)
  139. {
  140. return;
  141. }
  142. _vrModeEnabled = vrModeEnabled;
  143. if (vrModeEnabled)
  144. {
  145. _lblControllerServer.EnableVrMode();
  146. }
  147. else
  148. {
  149. _lblControllerServer.DisableVrMode();
  150. }
  151. // TODO: It signals an internal event of ICommonStateGetter. We have to determine where this event is used.
  152. }
  153. [CommandHipc(60)] // 3.0.0+
  154. // GetDefaultDisplayResolution() -> (u32, u32)
  155. public ResultCode GetDefaultDisplayResolution(ServiceCtx context)
  156. {
  157. // NOTE: Original service calls IOperationModeManager::GetDefaultDisplayResolution of omm service.
  158. // IOperationModeManager::GetDefaultDisplayResolution of omm service call IManagerDisplayService::GetDisplayResolution of vi service.
  159. (ulong width, ulong height) = AndroidSurfaceComposerClient.GetDisplayInfo(context);
  160. context.ResponseData.Write((uint)width);
  161. context.ResponseData.Write((uint)height);
  162. return ResultCode.Success;
  163. }
  164. [CommandHipc(61)] // 3.0.0+
  165. // GetDefaultDisplayResolutionChangeEvent() -> handle<copy>
  166. public ResultCode GetDefaultDisplayResolutionChangeEvent(ServiceCtx context)
  167. {
  168. // NOTE: Original service calls IOperationModeManager::GetDefaultDisplayResolutionChangeEvent of omm service.
  169. if (_displayResolutionChangedEventHandle == 0)
  170. {
  171. if (context.Process.HandleTable.GenerateHandle(context.Device.System.DisplayResolutionChangeEvent.ReadableEvent, out _displayResolutionChangedEventHandle) != KernelResult.Success)
  172. {
  173. throw new InvalidOperationException("Out of handles!");
  174. }
  175. }
  176. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_displayResolutionChangedEventHandle);
  177. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  178. return ResultCode.Success;
  179. }
  180. [CommandHipc(66)] // 6.0.0+
  181. // SetCpuBoostMode(u32 cpu_boost_mode)
  182. public ResultCode SetCpuBoostMode(ServiceCtx context)
  183. {
  184. uint cpuBoostMode = context.RequestData.ReadUInt32();
  185. if (cpuBoostMode > 1)
  186. {
  187. return ResultCode.InvalidParameters;
  188. }
  189. _apmSystemManagerServer.SetCpuBoostMode((Apm.CpuBoostMode)cpuBoostMode);
  190. // TODO: It signals an internal event of ICommonStateGetter. We have to determine where this event is used.
  191. return ResultCode.Success;
  192. }
  193. [CommandHipc(91)] // 7.0.0+
  194. // GetCurrentPerformanceConfiguration() -> nn::apm::PerformanceConfiguration
  195. public ResultCode GetCurrentPerformanceConfiguration(ServiceCtx context)
  196. {
  197. return (ResultCode)_apmSystemManagerServer.GetCurrentPerformanceConfiguration(context);
  198. }
  199. [CommandHipc(300)] // 9.0.0+
  200. // GetSettingsPlatformRegion() -> u8
  201. public ResultCode GetSettingsPlatformRegion(ServiceCtx context)
  202. {
  203. PlatformRegion platformRegion = context.Device.System.State.DesiredRegionCode == (uint)RegionCode.China ? PlatformRegion.China : PlatformRegion.Global;
  204. // FIXME: Call set:sys GetPlatformRegion
  205. context.ResponseData.Write((byte)platformRegion);
  206. return ResultCode.Success;
  207. }
  208. [CommandHipc(900)] // 11.0.0+
  209. // SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled()
  210. public ResultCode SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled(ServiceCtx context)
  211. {
  212. // TODO : Find where the field is used.
  213. _requestExitToLibraryAppletAtExecuteNextProgramEnabled = true;
  214. return ResultCode.Success;
  215. }
  216. }
  217. }