am.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <chrono>
  5. #include <memory>
  6. #include <queue>
  7. #include "core/hle/service/kernel_helpers.h"
  8. #include "core/hle/service/service.h"
  9. namespace Kernel {
  10. class KernelCore;
  11. class KReadableEvent;
  12. class KTransferMemory;
  13. } // namespace Kernel
  14. namespace Service::Nvnflinger {
  15. class Nvnflinger;
  16. }
  17. namespace Service::AM {
  18. class AppletMessageQueue {
  19. public:
  20. // This is nn::am::AppletMessage
  21. enum class AppletMessage : u32 {
  22. None = 0,
  23. ChangeIntoForeground = 1,
  24. ChangeIntoBackground = 2,
  25. Exit = 4,
  26. ApplicationExited = 6,
  27. FocusStateChanged = 15,
  28. Resume = 16,
  29. DetectShortPressingHomeButton = 20,
  30. DetectLongPressingHomeButton = 21,
  31. DetectShortPressingPowerButton = 22,
  32. DetectMiddlePressingPowerButton = 23,
  33. DetectLongPressingPowerButton = 24,
  34. RequestToPrepareSleep = 25,
  35. FinishedSleepSequence = 26,
  36. SleepRequiredByHighTemperature = 27,
  37. SleepRequiredByLowBattery = 28,
  38. AutoPowerDown = 29,
  39. OperationModeChanged = 30,
  40. PerformanceModeChanged = 31,
  41. DetectReceivingCecSystemStandby = 32,
  42. SdCardRemoved = 33,
  43. LaunchApplicationRequested = 50,
  44. RequestToDisplay = 51,
  45. ShowApplicationLogo = 55,
  46. HideApplicationLogo = 56,
  47. ForceHideApplicationLogo = 57,
  48. FloatingApplicationDetected = 60,
  49. DetectShortPressingCaptureButton = 90,
  50. AlbumScreenShotTaken = 92,
  51. AlbumRecordingSaved = 93,
  52. };
  53. explicit AppletMessageQueue(Core::System& system);
  54. ~AppletMessageQueue();
  55. Kernel::KReadableEvent& GetMessageReceiveEvent();
  56. Kernel::KReadableEvent& GetOperationModeChangedEvent();
  57. void PushMessage(AppletMessage msg);
  58. AppletMessage PopMessage();
  59. std::size_t GetMessageCount() const;
  60. void RequestExit();
  61. void RequestResume();
  62. void FocusStateChanged();
  63. void OperationModeChanged();
  64. private:
  65. KernelHelpers::ServiceContext service_context;
  66. Kernel::KEvent* on_new_message;
  67. Kernel::KEvent* on_operation_mode_changed;
  68. std::queue<AppletMessage> messages;
  69. };
  70. class IWindowController final : public ServiceFramework<IWindowController> {
  71. public:
  72. explicit IWindowController(Core::System& system_);
  73. ~IWindowController() override;
  74. private:
  75. void GetAppletResourceUserId(HLERequestContext& ctx);
  76. void AcquireForegroundRights(HLERequestContext& ctx);
  77. };
  78. class IAudioController final : public ServiceFramework<IAudioController> {
  79. public:
  80. explicit IAudioController(Core::System& system_);
  81. ~IAudioController() override;
  82. private:
  83. void SetExpectedMasterVolume(HLERequestContext& ctx);
  84. void GetMainAppletExpectedMasterVolume(HLERequestContext& ctx);
  85. void GetLibraryAppletExpectedMasterVolume(HLERequestContext& ctx);
  86. void ChangeMainAppletMasterVolume(HLERequestContext& ctx);
  87. void SetTransparentAudioRate(HLERequestContext& ctx);
  88. static constexpr float min_allowed_volume = 0.0f;
  89. static constexpr float max_allowed_volume = 1.0f;
  90. float main_applet_volume{0.25f};
  91. float library_applet_volume{max_allowed_volume};
  92. float transparent_volume_rate{min_allowed_volume};
  93. // Volume transition fade time in nanoseconds.
  94. // e.g. If the main applet volume was 0% and was changed to 50%
  95. // with a fade of 50ns, then over the course of 50ns,
  96. // the volume will gradually fade up to 50%
  97. std::chrono::nanoseconds fade_time_ns{0};
  98. };
  99. class IDisplayController final : public ServiceFramework<IDisplayController> {
  100. public:
  101. explicit IDisplayController(Core::System& system_);
  102. ~IDisplayController() override;
  103. private:
  104. void GetCallerAppletCaptureImageEx(HLERequestContext& ctx);
  105. void TakeScreenShotOfOwnLayer(HLERequestContext& ctx);
  106. void AcquireCallerAppletCaptureSharedBuffer(HLERequestContext& ctx);
  107. void ReleaseCallerAppletCaptureSharedBuffer(HLERequestContext& ctx);
  108. };
  109. class IDebugFunctions final : public ServiceFramework<IDebugFunctions> {
  110. public:
  111. explicit IDebugFunctions(Core::System& system_);
  112. ~IDebugFunctions() override;
  113. };
  114. class ISelfController final : public ServiceFramework<ISelfController> {
  115. public:
  116. explicit ISelfController(Core::System& system_, Nvnflinger::Nvnflinger& nvnflinger_);
  117. ~ISelfController() override;
  118. private:
  119. void Exit(HLERequestContext& ctx);
  120. void LockExit(HLERequestContext& ctx);
  121. void UnlockExit(HLERequestContext& ctx);
  122. void EnterFatalSection(HLERequestContext& ctx);
  123. void LeaveFatalSection(HLERequestContext& ctx);
  124. void GetLibraryAppletLaunchableEvent(HLERequestContext& ctx);
  125. void SetScreenShotPermission(HLERequestContext& ctx);
  126. void SetOperationModeChangedNotification(HLERequestContext& ctx);
  127. void SetPerformanceModeChangedNotification(HLERequestContext& ctx);
  128. void SetFocusHandlingMode(HLERequestContext& ctx);
  129. void SetRestartMessageEnabled(HLERequestContext& ctx);
  130. void SetOutOfFocusSuspendingEnabled(HLERequestContext& ctx);
  131. void SetAlbumImageOrientation(HLERequestContext& ctx);
  132. void IsSystemBufferSharingEnabled(HLERequestContext& ctx);
  133. void GetSystemSharedBufferHandle(HLERequestContext& ctx);
  134. void GetSystemSharedLayerHandle(HLERequestContext& ctx);
  135. void CreateManagedDisplayLayer(HLERequestContext& ctx);
  136. void CreateManagedDisplaySeparableLayer(HLERequestContext& ctx);
  137. void SetHandlesRequestToDisplay(HLERequestContext& ctx);
  138. void ApproveToDisplay(HLERequestContext& ctx);
  139. void SetIdleTimeDetectionExtension(HLERequestContext& ctx);
  140. void GetIdleTimeDetectionExtension(HLERequestContext& ctx);
  141. void ReportUserIsActive(HLERequestContext& ctx);
  142. void SetAutoSleepDisabled(HLERequestContext& ctx);
  143. void IsAutoSleepDisabled(HLERequestContext& ctx);
  144. void GetAccumulatedSuspendedTickValue(HLERequestContext& ctx);
  145. void GetAccumulatedSuspendedTickChangedEvent(HLERequestContext& ctx);
  146. void SetAlbumImageTakenNotificationEnabled(HLERequestContext& ctx);
  147. void SaveCurrentScreenshot(HLERequestContext& ctx);
  148. void SetRecordVolumeMuted(HLERequestContext& ctx);
  149. Result EnsureBufferSharingEnabled();
  150. enum class ScreenshotPermission : u32 {
  151. Inherit = 0,
  152. Enable = 1,
  153. Disable = 2,
  154. };
  155. Nvnflinger::Nvnflinger& nvnflinger;
  156. KernelHelpers::ServiceContext service_context;
  157. Kernel::KEvent* launchable_event;
  158. Kernel::KEvent* accumulated_suspended_tick_changed_event;
  159. u32 idle_time_detection_extension = 0;
  160. u64 num_fatal_sections_entered = 0;
  161. u64 system_shared_buffer_id = 0;
  162. u64 system_shared_layer_id = 0;
  163. bool is_auto_sleep_disabled = false;
  164. bool buffer_sharing_enabled = false;
  165. ScreenshotPermission screenshot_permission = ScreenshotPermission::Inherit;
  166. };
  167. class ICommonStateGetter final : public ServiceFramework<ICommonStateGetter> {
  168. public:
  169. explicit ICommonStateGetter(Core::System& system_,
  170. std::shared_ptr<AppletMessageQueue> msg_queue_);
  171. ~ICommonStateGetter() override;
  172. private:
  173. // This is nn::oe::FocusState
  174. enum class FocusState : u8 {
  175. InFocus = 1,
  176. NotInFocus = 2,
  177. Background = 3,
  178. };
  179. // This is nn::oe::OperationMode
  180. enum class OperationMode : u8 {
  181. Handheld = 0,
  182. Docked = 1,
  183. };
  184. // This is nn::am::service::SystemButtonType
  185. enum class SystemButtonType {
  186. None,
  187. HomeButtonShortPressing,
  188. HomeButtonLongPressing,
  189. PowerButtonShortPressing,
  190. PowerButtonLongPressing,
  191. ShutdownSystem,
  192. CaptureButtonShortPressing,
  193. CaptureButtonLongPressing,
  194. };
  195. enum class SysPlatformRegion : s32 {
  196. Global = 1,
  197. Terra = 2,
  198. };
  199. void GetEventHandle(HLERequestContext& ctx);
  200. void ReceiveMessage(HLERequestContext& ctx);
  201. void GetCurrentFocusState(HLERequestContext& ctx);
  202. void RequestToAcquireSleepLock(HLERequestContext& ctx);
  203. void GetAcquiredSleepLockEvent(HLERequestContext& ctx);
  204. void GetDefaultDisplayResolutionChangeEvent(HLERequestContext& ctx);
  205. void GetOperationMode(HLERequestContext& ctx);
  206. void GetPerformanceMode(HLERequestContext& ctx);
  207. void GetBootMode(HLERequestContext& ctx);
  208. void IsVrModeEnabled(HLERequestContext& ctx);
  209. void SetVrModeEnabled(HLERequestContext& ctx);
  210. void SetLcdBacklighOffEnabled(HLERequestContext& ctx);
  211. void BeginVrModeEx(HLERequestContext& ctx);
  212. void EndVrModeEx(HLERequestContext& ctx);
  213. void GetDefaultDisplayResolution(HLERequestContext& ctx);
  214. void SetCpuBoostMode(HLERequestContext& ctx);
  215. void PerformSystemButtonPressingIfInFocus(HLERequestContext& ctx);
  216. void GetSettingsPlatformRegion(HLERequestContext& ctx);
  217. void SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled(HLERequestContext& ctx);
  218. std::shared_ptr<AppletMessageQueue> msg_queue;
  219. bool vr_mode_state{};
  220. Kernel::KEvent* sleep_lock_event;
  221. KernelHelpers::ServiceContext service_context;
  222. };
  223. class IStorageImpl {
  224. public:
  225. virtual ~IStorageImpl();
  226. virtual std::vector<u8>& GetData() = 0;
  227. virtual const std::vector<u8>& GetData() const = 0;
  228. virtual std::size_t GetSize() const = 0;
  229. };
  230. class IStorage final : public ServiceFramework<IStorage> {
  231. public:
  232. explicit IStorage(Core::System& system_, std::vector<u8>&& buffer);
  233. ~IStorage() override;
  234. std::vector<u8>& GetData() {
  235. return impl->GetData();
  236. }
  237. const std::vector<u8>& GetData() const {
  238. return impl->GetData();
  239. }
  240. std::size_t GetSize() const {
  241. return impl->GetSize();
  242. }
  243. private:
  244. void Register();
  245. void Open(HLERequestContext& ctx);
  246. std::shared_ptr<IStorageImpl> impl;
  247. };
  248. class IStorageAccessor final : public ServiceFramework<IStorageAccessor> {
  249. public:
  250. explicit IStorageAccessor(Core::System& system_, IStorage& backing_);
  251. ~IStorageAccessor() override;
  252. private:
  253. void GetSize(HLERequestContext& ctx);
  254. void Write(HLERequestContext& ctx);
  255. void Read(HLERequestContext& ctx);
  256. IStorage& backing;
  257. };
  258. class ILibraryAppletCreator final : public ServiceFramework<ILibraryAppletCreator> {
  259. public:
  260. explicit ILibraryAppletCreator(Core::System& system_);
  261. ~ILibraryAppletCreator() override;
  262. private:
  263. void CreateLibraryApplet(HLERequestContext& ctx);
  264. void CreateStorage(HLERequestContext& ctx);
  265. void CreateTransferMemoryStorage(HLERequestContext& ctx);
  266. void CreateHandleStorage(HLERequestContext& ctx);
  267. };
  268. class ILibraryAppletSelfAccessor final : public ServiceFramework<ILibraryAppletSelfAccessor> {
  269. public:
  270. explicit ILibraryAppletSelfAccessor(Core::System& system_);
  271. ~ILibraryAppletSelfAccessor() override;
  272. private:
  273. void PopInData(HLERequestContext& ctx);
  274. void PushOutData(HLERequestContext& ctx);
  275. void GetLibraryAppletInfo(HLERequestContext& ctx);
  276. void ExitProcessAndReturn(HLERequestContext& ctx);
  277. void GetCallerAppletIdentityInfo(HLERequestContext& ctx);
  278. void PushInShowAlbum();
  279. void PushInShowCabinetData();
  280. void PushInShowMiiEditData();
  281. std::deque<std::vector<u8>> queue_data;
  282. };
  283. class IAppletCommonFunctions final : public ServiceFramework<IAppletCommonFunctions> {
  284. public:
  285. explicit IAppletCommonFunctions(Core::System& system_);
  286. ~IAppletCommonFunctions() override;
  287. private:
  288. void SetCpuBoostRequestPriority(HLERequestContext& ctx);
  289. };
  290. class IApplicationFunctions final : public ServiceFramework<IApplicationFunctions> {
  291. public:
  292. explicit IApplicationFunctions(Core::System& system_);
  293. ~IApplicationFunctions() override;
  294. private:
  295. void PopLaunchParameter(HLERequestContext& ctx);
  296. void CreateApplicationAndRequestToStartForQuest(HLERequestContext& ctx);
  297. void EnsureSaveData(HLERequestContext& ctx);
  298. void SetTerminateResult(HLERequestContext& ctx);
  299. void GetDisplayVersion(HLERequestContext& ctx);
  300. void GetDesiredLanguage(HLERequestContext& ctx);
  301. void IsGamePlayRecordingSupported(HLERequestContext& ctx);
  302. void InitializeGamePlayRecording(HLERequestContext& ctx);
  303. void SetGamePlayRecordingState(HLERequestContext& ctx);
  304. void NotifyRunning(HLERequestContext& ctx);
  305. void GetPseudoDeviceId(HLERequestContext& ctx);
  306. void ExtendSaveData(HLERequestContext& ctx);
  307. void GetSaveDataSize(HLERequestContext& ctx);
  308. void CreateCacheStorage(HLERequestContext& ctx);
  309. void GetSaveDataSizeMax(HLERequestContext& ctx);
  310. void BeginBlockingHomeButtonShortAndLongPressed(HLERequestContext& ctx);
  311. void EndBlockingHomeButtonShortAndLongPressed(HLERequestContext& ctx);
  312. void BeginBlockingHomeButton(HLERequestContext& ctx);
  313. void EndBlockingHomeButton(HLERequestContext& ctx);
  314. void EnableApplicationCrashReport(HLERequestContext& ctx);
  315. void InitializeApplicationCopyrightFrameBuffer(HLERequestContext& ctx);
  316. void SetApplicationCopyrightImage(HLERequestContext& ctx);
  317. void SetApplicationCopyrightVisibility(HLERequestContext& ctx);
  318. void QueryApplicationPlayStatistics(HLERequestContext& ctx);
  319. void QueryApplicationPlayStatisticsByUid(HLERequestContext& ctx);
  320. void ExecuteProgram(HLERequestContext& ctx);
  321. void ClearUserChannel(HLERequestContext& ctx);
  322. void UnpopToUserChannel(HLERequestContext& ctx);
  323. void GetPreviousProgramIndex(HLERequestContext& ctx);
  324. void GetGpuErrorDetectedSystemEvent(HLERequestContext& ctx);
  325. void GetFriendInvitationStorageChannelEvent(HLERequestContext& ctx);
  326. void TryPopFromFriendInvitationStorageChannel(HLERequestContext& ctx);
  327. void GetNotificationStorageChannelEvent(HLERequestContext& ctx);
  328. void GetHealthWarningDisappearedSystemEvent(HLERequestContext& ctx);
  329. void PrepareForJit(HLERequestContext& ctx);
  330. KernelHelpers::ServiceContext service_context;
  331. bool launch_popped_account_preselect = false;
  332. s32 previous_program_index{-1};
  333. Kernel::KEvent* gpu_error_detected_event;
  334. Kernel::KEvent* friend_invitation_storage_channel_event;
  335. Kernel::KEvent* notification_storage_channel_event;
  336. Kernel::KEvent* health_warning_disappeared_system_event;
  337. };
  338. class IHomeMenuFunctions final : public ServiceFramework<IHomeMenuFunctions> {
  339. public:
  340. explicit IHomeMenuFunctions(Core::System& system_);
  341. ~IHomeMenuFunctions() override;
  342. private:
  343. void RequestToGetForeground(HLERequestContext& ctx);
  344. void GetPopFromGeneralChannelEvent(HLERequestContext& ctx);
  345. KernelHelpers::ServiceContext service_context;
  346. Kernel::KEvent* pop_from_general_channel_event;
  347. };
  348. class IGlobalStateController final : public ServiceFramework<IGlobalStateController> {
  349. public:
  350. explicit IGlobalStateController(Core::System& system_);
  351. ~IGlobalStateController() override;
  352. };
  353. class IApplicationCreator final : public ServiceFramework<IApplicationCreator> {
  354. public:
  355. explicit IApplicationCreator(Core::System& system_);
  356. ~IApplicationCreator() override;
  357. };
  358. class IProcessWindingController final : public ServiceFramework<IProcessWindingController> {
  359. public:
  360. explicit IProcessWindingController(Core::System& system_);
  361. ~IProcessWindingController() override;
  362. private:
  363. void GetLaunchReason(HLERequestContext& ctx);
  364. void OpenCallingLibraryApplet(HLERequestContext& ctx);
  365. };
  366. void LoopProcess(Nvnflinger::Nvnflinger& nvnflinger, Core::System& system);
  367. } // namespace Service::AM