am.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cinttypes>
  7. #include <cstring>
  8. #include "audio_core/audio_renderer.h"
  9. #include "core/core.h"
  10. #include "core/file_sys/control_metadata.h"
  11. #include "core/file_sys/patch_manager.h"
  12. #include "core/file_sys/savedata_factory.h"
  13. #include "core/hle/ipc_helpers.h"
  14. #include "core/hle/kernel/kernel.h"
  15. #include "core/hle/kernel/process.h"
  16. #include "core/hle/kernel/readable_event.h"
  17. #include "core/hle/kernel/transfer_memory.h"
  18. #include "core/hle/kernel/writable_event.h"
  19. #include "core/hle/service/acc/profile_manager.h"
  20. #include "core/hle/service/am/am.h"
  21. #include "core/hle/service/am/applet_ae.h"
  22. #include "core/hle/service/am/applet_oe.h"
  23. #include "core/hle/service/am/applets/applets.h"
  24. #include "core/hle/service/am/applets/profile_select.h"
  25. #include "core/hle/service/am/applets/software_keyboard.h"
  26. #include "core/hle/service/am/applets/web_browser.h"
  27. #include "core/hle/service/am/idle.h"
  28. #include "core/hle/service/am/omm.h"
  29. #include "core/hle/service/am/spsm.h"
  30. #include "core/hle/service/am/tcap.h"
  31. #include "core/hle/service/apm/controller.h"
  32. #include "core/hle/service/apm/interface.h"
  33. #include "core/hle/service/bcat/backend/backend.h"
  34. #include "core/hle/service/filesystem/filesystem.h"
  35. #include "core/hle/service/ns/ns.h"
  36. #include "core/hle/service/nvflinger/nvflinger.h"
  37. #include "core/hle/service/pm/pm.h"
  38. #include "core/hle/service/set/set.h"
  39. #include "core/hle/service/sm/sm.h"
  40. #include "core/hle/service/vi/vi.h"
  41. #include "core/settings.h"
  42. namespace Service::AM {
  43. constexpr ResultCode ERR_NO_DATA_IN_CHANNEL{ErrorModule::AM, 2};
  44. constexpr ResultCode ERR_NO_MESSAGES{ErrorModule::AM, 3};
  45. constexpr ResultCode ERR_SIZE_OUT_OF_BOUNDS{ErrorModule::AM, 503};
  46. enum class LaunchParameterKind : u32 {
  47. ApplicationSpecific = 1,
  48. AccountPreselectedUser = 2,
  49. };
  50. constexpr u32 LAUNCH_PARAMETER_ACCOUNT_PRESELECTED_USER_MAGIC = 0xC79497CA;
  51. struct LaunchParameterAccountPreselectedUser {
  52. u32_le magic;
  53. u32_le is_account_selected;
  54. u128 current_user;
  55. INSERT_PADDING_BYTES(0x70);
  56. };
  57. static_assert(sizeof(LaunchParameterAccountPreselectedUser) == 0x88);
  58. IWindowController::IWindowController(Core::System& system_)
  59. : ServiceFramework("IWindowController"), system{system_} {
  60. // clang-format off
  61. static const FunctionInfo functions[] = {
  62. {0, nullptr, "CreateWindow"},
  63. {1, &IWindowController::GetAppletResourceUserId, "GetAppletResourceUserId"},
  64. {10, &IWindowController::AcquireForegroundRights, "AcquireForegroundRights"},
  65. {11, nullptr, "ReleaseForegroundRights"},
  66. {12, nullptr, "RejectToChangeIntoBackground"},
  67. {20, nullptr, "SetAppletWindowVisibility"},
  68. {21, nullptr, "SetAppletGpuTimeSlice"},
  69. };
  70. // clang-format on
  71. RegisterHandlers(functions);
  72. }
  73. IWindowController::~IWindowController() = default;
  74. void IWindowController::GetAppletResourceUserId(Kernel::HLERequestContext& ctx) {
  75. const u64 process_id = system.CurrentProcess()->GetProcessID();
  76. LOG_DEBUG(Service_AM, "called. Process ID=0x{:016X}", process_id);
  77. IPC::ResponseBuilder rb{ctx, 4};
  78. rb.Push(RESULT_SUCCESS);
  79. rb.Push<u64>(process_id);
  80. }
  81. void IWindowController::AcquireForegroundRights(Kernel::HLERequestContext& ctx) {
  82. LOG_WARNING(Service_AM, "(STUBBED) called");
  83. IPC::ResponseBuilder rb{ctx, 2};
  84. rb.Push(RESULT_SUCCESS);
  85. }
  86. IAudioController::IAudioController() : ServiceFramework("IAudioController") {
  87. // clang-format off
  88. static const FunctionInfo functions[] = {
  89. {0, &IAudioController::SetExpectedMasterVolume, "SetExpectedMasterVolume"},
  90. {1, &IAudioController::GetMainAppletExpectedMasterVolume, "GetMainAppletExpectedMasterVolume"},
  91. {2, &IAudioController::GetLibraryAppletExpectedMasterVolume, "GetLibraryAppletExpectedMasterVolume"},
  92. {3, &IAudioController::ChangeMainAppletMasterVolume, "ChangeMainAppletMasterVolume"},
  93. {4, &IAudioController::SetTransparentAudioRate, "SetTransparentVolumeRate"},
  94. };
  95. // clang-format on
  96. RegisterHandlers(functions);
  97. }
  98. IAudioController::~IAudioController() = default;
  99. void IAudioController::SetExpectedMasterVolume(Kernel::HLERequestContext& ctx) {
  100. IPC::RequestParser rp{ctx};
  101. const float main_applet_volume_tmp = rp.Pop<float>();
  102. const float library_applet_volume_tmp = rp.Pop<float>();
  103. LOG_DEBUG(Service_AM, "called. main_applet_volume={}, library_applet_volume={}",
  104. main_applet_volume_tmp, library_applet_volume_tmp);
  105. // Ensure the volume values remain within the 0-100% range
  106. main_applet_volume = std::clamp(main_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  107. library_applet_volume =
  108. std::clamp(library_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  109. IPC::ResponseBuilder rb{ctx, 2};
  110. rb.Push(RESULT_SUCCESS);
  111. }
  112. void IAudioController::GetMainAppletExpectedMasterVolume(Kernel::HLERequestContext& ctx) {
  113. LOG_DEBUG(Service_AM, "called. main_applet_volume={}", main_applet_volume);
  114. IPC::ResponseBuilder rb{ctx, 3};
  115. rb.Push(RESULT_SUCCESS);
  116. rb.Push(main_applet_volume);
  117. }
  118. void IAudioController::GetLibraryAppletExpectedMasterVolume(Kernel::HLERequestContext& ctx) {
  119. LOG_DEBUG(Service_AM, "called. library_applet_volume={}", library_applet_volume);
  120. IPC::ResponseBuilder rb{ctx, 3};
  121. rb.Push(RESULT_SUCCESS);
  122. rb.Push(library_applet_volume);
  123. }
  124. void IAudioController::ChangeMainAppletMasterVolume(Kernel::HLERequestContext& ctx) {
  125. struct Parameters {
  126. float volume;
  127. s64 fade_time_ns;
  128. };
  129. static_assert(sizeof(Parameters) == 16);
  130. IPC::RequestParser rp{ctx};
  131. const auto parameters = rp.PopRaw<Parameters>();
  132. LOG_DEBUG(Service_AM, "called. volume={}, fade_time_ns={}", parameters.volume,
  133. parameters.fade_time_ns);
  134. main_applet_volume = std::clamp(parameters.volume, min_allowed_volume, max_allowed_volume);
  135. fade_time_ns = std::chrono::nanoseconds{parameters.fade_time_ns};
  136. IPC::ResponseBuilder rb{ctx, 2};
  137. rb.Push(RESULT_SUCCESS);
  138. }
  139. void IAudioController::SetTransparentAudioRate(Kernel::HLERequestContext& ctx) {
  140. IPC::RequestParser rp{ctx};
  141. const float transparent_volume_rate_tmp = rp.Pop<float>();
  142. LOG_DEBUG(Service_AM, "called. transparent_volume_rate={}", transparent_volume_rate_tmp);
  143. // Clamp volume range to 0-100%.
  144. transparent_volume_rate =
  145. std::clamp(transparent_volume_rate_tmp, min_allowed_volume, max_allowed_volume);
  146. IPC::ResponseBuilder rb{ctx, 2};
  147. rb.Push(RESULT_SUCCESS);
  148. }
  149. IDisplayController::IDisplayController() : ServiceFramework("IDisplayController") {
  150. // clang-format off
  151. static const FunctionInfo functions[] = {
  152. {0, nullptr, "GetLastForegroundCaptureImage"},
  153. {1, nullptr, "UpdateLastForegroundCaptureImage"},
  154. {2, nullptr, "GetLastApplicationCaptureImage"},
  155. {3, nullptr, "GetCallerAppletCaptureImage"},
  156. {4, nullptr, "UpdateCallerAppletCaptureImage"},
  157. {5, nullptr, "GetLastForegroundCaptureImageEx"},
  158. {6, nullptr, "GetLastApplicationCaptureImageEx"},
  159. {7, nullptr, "GetCallerAppletCaptureImageEx"},
  160. {8, nullptr, "TakeScreenShotOfOwnLayer"}, // 2.0.0+
  161. {9, nullptr, "CopyBetweenCaptureBuffers"}, // 5.0.0+
  162. {10, nullptr, "AcquireLastApplicationCaptureBuffer"},
  163. {11, nullptr, "ReleaseLastApplicationCaptureBuffer"},
  164. {12, nullptr, "AcquireLastForegroundCaptureBuffer"},
  165. {13, nullptr, "ReleaseLastForegroundCaptureBuffer"},
  166. {14, nullptr, "AcquireCallerAppletCaptureBuffer"},
  167. {15, nullptr, "ReleaseCallerAppletCaptureBuffer"},
  168. {16, nullptr, "AcquireLastApplicationCaptureBufferEx"},
  169. {17, nullptr, "AcquireLastForegroundCaptureBufferEx"},
  170. {18, nullptr, "AcquireCallerAppletCaptureBufferEx"},
  171. // 2.0.0+
  172. {20, nullptr, "ClearCaptureBuffer"},
  173. {21, nullptr, "ClearAppletTransitionBuffer"},
  174. // 4.0.0+
  175. {22, nullptr, "AcquireLastApplicationCaptureSharedBuffer"},
  176. {23, nullptr, "ReleaseLastApplicationCaptureSharedBuffer"},
  177. {24, nullptr, "AcquireLastForegroundCaptureSharedBuffer"},
  178. {25, nullptr, "ReleaseLastForegroundCaptureSharedBuffer"},
  179. {26, nullptr, "AcquireCallerAppletCaptureSharedBuffer"},
  180. {27, nullptr, "ReleaseCallerAppletCaptureSharedBuffer"},
  181. // 6.0.0+
  182. {28, nullptr, "TakeScreenShotOfOwnLayerEx"},
  183. };
  184. // clang-format on
  185. RegisterHandlers(functions);
  186. }
  187. IDisplayController::~IDisplayController() = default;
  188. IDebugFunctions::IDebugFunctions() : ServiceFramework{"IDebugFunctions"} {
  189. // clang-format off
  190. static const FunctionInfo functions[] = {
  191. {0, nullptr, "NotifyMessageToHomeMenuForDebug"},
  192. {1, nullptr, "OpenMainApplication"},
  193. {10, nullptr, "EmulateButtonEvent"},
  194. {20, nullptr, "InvalidateTransitionLayer"},
  195. {30, nullptr, "RequestLaunchApplicationWithUserAndArgumentForDebug"},
  196. {40, nullptr, "GetAppletResourceUsageInfo"},
  197. {100, nullptr, "SetCpuBoostModeForApplet"},
  198. {101, nullptr, "CancelCpuBoostModeForApplet"},
  199. {110, nullptr, "PushToAppletBoundChannelForDebug"},
  200. {111, nullptr, "TryPopFromAppletBoundChannelForDebug"},
  201. {120, nullptr, "AlarmSettingNotificationEnableAppEventReserve"},
  202. {121, nullptr, "AlarmSettingNotificationDisableAppEventReserve"},
  203. {122, nullptr, "AlarmSettingNotificationPushAppEventNotify"},
  204. {130, nullptr, "FriendInvitationSetApplicationParameter"},
  205. {131, nullptr, "FriendInvitationClearApplicationParameter"},
  206. {132, nullptr, "FriendInvitationPushApplicationParameter"},
  207. };
  208. // clang-format on
  209. RegisterHandlers(functions);
  210. }
  211. IDebugFunctions::~IDebugFunctions() = default;
  212. ISelfController::ISelfController(Core::System& system,
  213. std::shared_ptr<NVFlinger::NVFlinger> nvflinger)
  214. : ServiceFramework("ISelfController"), system(system), nvflinger(std::move(nvflinger)) {
  215. // clang-format off
  216. static const FunctionInfo functions[] = {
  217. {0, &ISelfController::Exit, "Exit"},
  218. {1, &ISelfController::LockExit, "LockExit"},
  219. {2, &ISelfController::UnlockExit, "UnlockExit"},
  220. {3, &ISelfController::EnterFatalSection, "EnterFatalSection"},
  221. {4, &ISelfController::LeaveFatalSection, "LeaveFatalSection"},
  222. {9, &ISelfController::GetLibraryAppletLaunchableEvent, "GetLibraryAppletLaunchableEvent"},
  223. {10, &ISelfController::SetScreenShotPermission, "SetScreenShotPermission"},
  224. {11, &ISelfController::SetOperationModeChangedNotification, "SetOperationModeChangedNotification"},
  225. {12, &ISelfController::SetPerformanceModeChangedNotification, "SetPerformanceModeChangedNotification"},
  226. {13, &ISelfController::SetFocusHandlingMode, "SetFocusHandlingMode"},
  227. {14, &ISelfController::SetRestartMessageEnabled, "SetRestartMessageEnabled"},
  228. {15, nullptr, "SetScreenShotAppletIdentityInfo"},
  229. {16, &ISelfController::SetOutOfFocusSuspendingEnabled, "SetOutOfFocusSuspendingEnabled"},
  230. {17, nullptr, "SetControllerFirmwareUpdateSection"},
  231. {18, nullptr, "SetRequiresCaptureButtonShortPressedMessage"},
  232. {19, &ISelfController::SetScreenShotImageOrientation, "SetScreenShotImageOrientation"},
  233. {20, nullptr, "SetDesirableKeyboardLayout"},
  234. {40, &ISelfController::CreateManagedDisplayLayer, "CreateManagedDisplayLayer"},
  235. {41, nullptr, "IsSystemBufferSharingEnabled"},
  236. {42, nullptr, "GetSystemSharedLayerHandle"},
  237. {43, nullptr, "GetSystemSharedBufferHandle"},
  238. {44, nullptr, "CreateManagedDisplaySeparableLayer"},
  239. {45, nullptr, "SetManagedDisplayLayerSeparationMode"},
  240. {50, &ISelfController::SetHandlesRequestToDisplay, "SetHandlesRequestToDisplay"},
  241. {51, nullptr, "ApproveToDisplay"},
  242. {60, nullptr, "OverrideAutoSleepTimeAndDimmingTime"},
  243. {61, nullptr, "SetMediaPlaybackState"},
  244. {62, &ISelfController::SetIdleTimeDetectionExtension, "SetIdleTimeDetectionExtension"},
  245. {63, &ISelfController::GetIdleTimeDetectionExtension, "GetIdleTimeDetectionExtension"},
  246. {64, nullptr, "SetInputDetectionSourceSet"},
  247. {65, nullptr, "ReportUserIsActive"},
  248. {66, nullptr, "GetCurrentIlluminance"},
  249. {67, nullptr, "IsIlluminanceAvailable"},
  250. {68, &ISelfController::SetAutoSleepDisabled, "SetAutoSleepDisabled"},
  251. {69, &ISelfController::IsAutoSleepDisabled, "IsAutoSleepDisabled"},
  252. {70, nullptr, "ReportMultimediaError"},
  253. {71, nullptr, "GetCurrentIlluminanceEx"},
  254. {72, nullptr, "SetInputDetectionPolicy"},
  255. {80, nullptr, "SetWirelessPriorityMode"},
  256. {90, &ISelfController::GetAccumulatedSuspendedTickValue, "GetAccumulatedSuspendedTickValue"},
  257. {91, &ISelfController::GetAccumulatedSuspendedTickChangedEvent, "GetAccumulatedSuspendedTickChangedEvent"},
  258. {100, nullptr, "SetAlbumImageTakenNotificationEnabled"},
  259. {110, nullptr, "SetApplicationAlbumUserData"},
  260. {1000, nullptr, "GetDebugStorageChannel"},
  261. };
  262. // clang-format on
  263. RegisterHandlers(functions);
  264. auto& kernel = system.Kernel();
  265. launchable_event =
  266. Kernel::WritableEvent::CreateEventPair(kernel, "ISelfController:LaunchableEvent");
  267. // This event is created by AM on the first time GetAccumulatedSuspendedTickChangedEvent() is
  268. // called. Yuzu can just create it unconditionally, since it doesn't need to support multiple
  269. // ISelfControllers. The event is signaled on creation, and on transition from suspended -> not
  270. // suspended if the event has previously been created by a call to
  271. // GetAccumulatedSuspendedTickChangedEvent.
  272. accumulated_suspended_tick_changed_event = Kernel::WritableEvent::CreateEventPair(
  273. kernel, "ISelfController:AccumulatedSuspendedTickChangedEvent");
  274. accumulated_suspended_tick_changed_event.writable->Signal();
  275. }
  276. ISelfController::~ISelfController() = default;
  277. void ISelfController::Exit(Kernel::HLERequestContext& ctx) {
  278. LOG_DEBUG(Service_AM, "called");
  279. system.Shutdown();
  280. IPC::ResponseBuilder rb{ctx, 2};
  281. rb.Push(RESULT_SUCCESS);
  282. }
  283. void ISelfController::LockExit(Kernel::HLERequestContext& ctx) {
  284. LOG_DEBUG(Service_AM, "called");
  285. system.SetExitLock(true);
  286. IPC::ResponseBuilder rb{ctx, 2};
  287. rb.Push(RESULT_SUCCESS);
  288. }
  289. void ISelfController::UnlockExit(Kernel::HLERequestContext& ctx) {
  290. LOG_DEBUG(Service_AM, "called");
  291. system.SetExitLock(false);
  292. IPC::ResponseBuilder rb{ctx, 2};
  293. rb.Push(RESULT_SUCCESS);
  294. }
  295. void ISelfController::EnterFatalSection(Kernel::HLERequestContext& ctx) {
  296. ++num_fatal_sections_entered;
  297. LOG_DEBUG(Service_AM, "called. Num fatal sections entered: {}", num_fatal_sections_entered);
  298. IPC::ResponseBuilder rb{ctx, 2};
  299. rb.Push(RESULT_SUCCESS);
  300. }
  301. void ISelfController::LeaveFatalSection(Kernel::HLERequestContext& ctx) {
  302. LOG_DEBUG(Service_AM, "called.");
  303. // Entry and exit of fatal sections must be balanced.
  304. if (num_fatal_sections_entered == 0) {
  305. IPC::ResponseBuilder rb{ctx, 2};
  306. rb.Push(ResultCode{ErrorModule::AM, 512});
  307. return;
  308. }
  309. --num_fatal_sections_entered;
  310. IPC::ResponseBuilder rb{ctx, 2};
  311. rb.Push(RESULT_SUCCESS);
  312. }
  313. void ISelfController::GetLibraryAppletLaunchableEvent(Kernel::HLERequestContext& ctx) {
  314. LOG_WARNING(Service_AM, "(STUBBED) called");
  315. launchable_event.writable->Signal();
  316. IPC::ResponseBuilder rb{ctx, 2, 1};
  317. rb.Push(RESULT_SUCCESS);
  318. rb.PushCopyObjects(launchable_event.readable);
  319. }
  320. void ISelfController::SetScreenShotPermission(Kernel::HLERequestContext& ctx) {
  321. LOG_WARNING(Service_AM, "(STUBBED) called");
  322. IPC::ResponseBuilder rb{ctx, 2};
  323. rb.Push(RESULT_SUCCESS);
  324. }
  325. void ISelfController::SetOperationModeChangedNotification(Kernel::HLERequestContext& ctx) {
  326. IPC::RequestParser rp{ctx};
  327. bool flag = rp.Pop<bool>();
  328. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  329. IPC::ResponseBuilder rb{ctx, 2};
  330. rb.Push(RESULT_SUCCESS);
  331. }
  332. void ISelfController::SetPerformanceModeChangedNotification(Kernel::HLERequestContext& ctx) {
  333. IPC::RequestParser rp{ctx};
  334. bool flag = rp.Pop<bool>();
  335. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  336. IPC::ResponseBuilder rb{ctx, 2};
  337. rb.Push(RESULT_SUCCESS);
  338. }
  339. void ISelfController::SetFocusHandlingMode(Kernel::HLERequestContext& ctx) {
  340. // Takes 3 input u8s with each field located immediately after the previous
  341. // u8, these are bool flags. No output.
  342. IPC::RequestParser rp{ctx};
  343. struct FocusHandlingModeParams {
  344. u8 unknown0;
  345. u8 unknown1;
  346. u8 unknown2;
  347. };
  348. const auto flags = rp.PopRaw<FocusHandlingModeParams>();
  349. LOG_WARNING(Service_AM, "(STUBBED) called. unknown0={}, unknown1={}, unknown2={}",
  350. flags.unknown0, flags.unknown1, flags.unknown2);
  351. IPC::ResponseBuilder rb{ctx, 2};
  352. rb.Push(RESULT_SUCCESS);
  353. }
  354. void ISelfController::SetRestartMessageEnabled(Kernel::HLERequestContext& ctx) {
  355. LOG_WARNING(Service_AM, "(STUBBED) called");
  356. IPC::ResponseBuilder rb{ctx, 2};
  357. rb.Push(RESULT_SUCCESS);
  358. }
  359. void ISelfController::SetOutOfFocusSuspendingEnabled(Kernel::HLERequestContext& ctx) {
  360. // Takes 3 input u8s with each field located immediately after the previous
  361. // u8, these are bool flags. No output.
  362. IPC::RequestParser rp{ctx};
  363. bool enabled = rp.Pop<bool>();
  364. LOG_WARNING(Service_AM, "(STUBBED) called enabled={}", enabled);
  365. IPC::ResponseBuilder rb{ctx, 2};
  366. rb.Push(RESULT_SUCCESS);
  367. }
  368. void ISelfController::SetScreenShotImageOrientation(Kernel::HLERequestContext& ctx) {
  369. LOG_WARNING(Service_AM, "(STUBBED) called");
  370. IPC::ResponseBuilder rb{ctx, 2};
  371. rb.Push(RESULT_SUCCESS);
  372. }
  373. void ISelfController::CreateManagedDisplayLayer(Kernel::HLERequestContext& ctx) {
  374. LOG_WARNING(Service_AM, "(STUBBED) called");
  375. // TODO(Subv): Find out how AM determines the display to use, for now just
  376. // create the layer in the Default display.
  377. const auto display_id = nvflinger->OpenDisplay("Default");
  378. const auto layer_id = nvflinger->CreateLayer(*display_id);
  379. IPC::ResponseBuilder rb{ctx, 4};
  380. rb.Push(RESULT_SUCCESS);
  381. rb.Push(*layer_id);
  382. }
  383. void ISelfController::SetHandlesRequestToDisplay(Kernel::HLERequestContext& ctx) {
  384. LOG_WARNING(Service_AM, "(STUBBED) called");
  385. IPC::ResponseBuilder rb{ctx, 2};
  386. rb.Push(RESULT_SUCCESS);
  387. }
  388. void ISelfController::SetIdleTimeDetectionExtension(Kernel::HLERequestContext& ctx) {
  389. IPC::RequestParser rp{ctx};
  390. idle_time_detection_extension = rp.Pop<u32>();
  391. LOG_WARNING(Service_AM, "(STUBBED) called idle_time_detection_extension={}",
  392. idle_time_detection_extension);
  393. IPC::ResponseBuilder rb{ctx, 2};
  394. rb.Push(RESULT_SUCCESS);
  395. }
  396. void ISelfController::GetIdleTimeDetectionExtension(Kernel::HLERequestContext& ctx) {
  397. LOG_WARNING(Service_AM, "(STUBBED) called");
  398. IPC::ResponseBuilder rb{ctx, 3};
  399. rb.Push(RESULT_SUCCESS);
  400. rb.Push<u32>(idle_time_detection_extension);
  401. }
  402. void ISelfController::SetAutoSleepDisabled(Kernel::HLERequestContext& ctx) {
  403. IPC::RequestParser rp{ctx};
  404. is_auto_sleep_disabled = rp.Pop<bool>();
  405. // On the system itself, if the previous state of is_auto_sleep_disabled
  406. // differed from the current value passed in, it'd signify the internal
  407. // window manager to update (and also increment some statistics like update counts)
  408. //
  409. // It'd also indicate this change to an idle handling context.
  410. //
  411. // However, given we're emulating this behavior, most of this can be ignored
  412. // and it's sufficient to simply set the member variable for querying via
  413. // IsAutoSleepDisabled().
  414. LOG_DEBUG(Service_AM, "called. is_auto_sleep_disabled={}", is_auto_sleep_disabled);
  415. IPC::ResponseBuilder rb{ctx, 2};
  416. rb.Push(RESULT_SUCCESS);
  417. }
  418. void ISelfController::IsAutoSleepDisabled(Kernel::HLERequestContext& ctx) {
  419. LOG_DEBUG(Service_AM, "called.");
  420. IPC::ResponseBuilder rb{ctx, 3};
  421. rb.Push(RESULT_SUCCESS);
  422. rb.Push(is_auto_sleep_disabled);
  423. }
  424. void ISelfController::GetAccumulatedSuspendedTickValue(Kernel::HLERequestContext& ctx) {
  425. LOG_DEBUG(Service_AM, "called.");
  426. // This command returns the total number of system ticks since ISelfController creation
  427. // where the game was suspended. Since Yuzu doesn't implement game suspension, this command
  428. // can just always return 0 ticks.
  429. IPC::ResponseBuilder rb{ctx, 4};
  430. rb.Push(RESULT_SUCCESS);
  431. rb.Push<u64>(0);
  432. }
  433. void ISelfController::GetAccumulatedSuspendedTickChangedEvent(Kernel::HLERequestContext& ctx) {
  434. LOG_DEBUG(Service_AM, "called.");
  435. IPC::ResponseBuilder rb{ctx, 2, 1};
  436. rb.Push(RESULT_SUCCESS);
  437. rb.PushCopyObjects(accumulated_suspended_tick_changed_event.readable);
  438. }
  439. AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) {
  440. on_new_message =
  441. Kernel::WritableEvent::CreateEventPair(kernel, "AMMessageQueue:OnMessageRecieved");
  442. on_operation_mode_changed =
  443. Kernel::WritableEvent::CreateEventPair(kernel, "AMMessageQueue:OperationModeChanged");
  444. }
  445. AppletMessageQueue::~AppletMessageQueue() = default;
  446. const std::shared_ptr<Kernel::ReadableEvent>& AppletMessageQueue::GetMesssageRecieveEvent() const {
  447. return on_new_message.readable;
  448. }
  449. const std::shared_ptr<Kernel::ReadableEvent>& AppletMessageQueue::GetOperationModeChangedEvent()
  450. const {
  451. return on_operation_mode_changed.readable;
  452. }
  453. void AppletMessageQueue::PushMessage(AppletMessage msg) {
  454. messages.push(msg);
  455. on_new_message.writable->Signal();
  456. }
  457. AppletMessageQueue::AppletMessage AppletMessageQueue::PopMessage() {
  458. if (messages.empty()) {
  459. on_new_message.writable->Clear();
  460. return AppletMessage::NoMessage;
  461. }
  462. auto msg = messages.front();
  463. messages.pop();
  464. if (messages.empty()) {
  465. on_new_message.writable->Clear();
  466. }
  467. return msg;
  468. }
  469. std::size_t AppletMessageQueue::GetMessageCount() const {
  470. return messages.size();
  471. }
  472. void AppletMessageQueue::OperationModeChanged() {
  473. PushMessage(AppletMessage::OperationModeChanged);
  474. PushMessage(AppletMessage::PerformanceModeChanged);
  475. on_operation_mode_changed.writable->Signal();
  476. }
  477. void AppletMessageQueue::RequestExit() {
  478. PushMessage(AppletMessage::ExitRequested);
  479. }
  480. ICommonStateGetter::ICommonStateGetter(Core::System& system,
  481. std::shared_ptr<AppletMessageQueue> msg_queue)
  482. : ServiceFramework("ICommonStateGetter"), system(system), msg_queue(std::move(msg_queue)) {
  483. // clang-format off
  484. static const FunctionInfo functions[] = {
  485. {0, &ICommonStateGetter::GetEventHandle, "GetEventHandle"},
  486. {1, &ICommonStateGetter::ReceiveMessage, "ReceiveMessage"},
  487. {2, nullptr, "GetThisAppletKind"},
  488. {3, nullptr, "AllowToEnterSleep"},
  489. {4, nullptr, "DisallowToEnterSleep"},
  490. {5, &ICommonStateGetter::GetOperationMode, "GetOperationMode"},
  491. {6, &ICommonStateGetter::GetPerformanceMode, "GetPerformanceMode"},
  492. {7, nullptr, "GetCradleStatus"},
  493. {8, &ICommonStateGetter::GetBootMode, "GetBootMode"},
  494. {9, &ICommonStateGetter::GetCurrentFocusState, "GetCurrentFocusState"},
  495. {10, nullptr, "RequestToAcquireSleepLock"},
  496. {11, nullptr, "ReleaseSleepLock"},
  497. {12, nullptr, "ReleaseSleepLockTransiently"},
  498. {13, nullptr, "GetAcquiredSleepLockEvent"},
  499. {20, nullptr, "PushToGeneralChannel"},
  500. {30, nullptr, "GetHomeButtonReaderLockAccessor"},
  501. {31, nullptr, "GetReaderLockAccessorEx"},
  502. {40, nullptr, "GetCradleFwVersion"},
  503. {50, &ICommonStateGetter::IsVrModeEnabled, "IsVrModeEnabled"},
  504. {51, &ICommonStateGetter::SetVrModeEnabled, "SetVrModeEnabled"},
  505. {52, &ICommonStateGetter::SetLcdBacklighOffEnabled, "SetLcdBacklighOffEnabled"},
  506. {53, nullptr, "BeginVrModeEx"},
  507. {54, &ICommonStateGetter::EndVrModeEx, "EndVrModeEx"},
  508. {55, nullptr, "IsInControllerFirmwareUpdateSection"},
  509. {60, &ICommonStateGetter::GetDefaultDisplayResolution, "GetDefaultDisplayResolution"},
  510. {61, &ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent, "GetDefaultDisplayResolutionChangeEvent"},
  511. {62, nullptr, "GetHdcpAuthenticationState"},
  512. {63, nullptr, "GetHdcpAuthenticationStateChangeEvent"},
  513. {64, nullptr, "SetTvPowerStateMatchingMode"},
  514. {65, nullptr, "GetApplicationIdByContentActionName"},
  515. {66, &ICommonStateGetter::SetCpuBoostMode, "SetCpuBoostMode"},
  516. {67, nullptr, "CancelCpuBoostMode"},
  517. {80, nullptr, "PerformSystemButtonPressingIfInFocus"},
  518. {90, nullptr, "SetPerformanceConfigurationChangedNotification"},
  519. {91, nullptr, "GetCurrentPerformanceConfiguration"},
  520. {100, nullptr, "SetHandlingHomeButtonShortPressedEnabled"},
  521. {200, nullptr, "GetOperationModeSystemInfo"},
  522. {300, nullptr, "GetSettingsPlatformRegion"},
  523. {400, nullptr, "ActivateMigrationService"},
  524. {401, nullptr, "DeactivateMigrationService"},
  525. };
  526. // clang-format on
  527. RegisterHandlers(functions);
  528. }
  529. ICommonStateGetter::~ICommonStateGetter() = default;
  530. void ICommonStateGetter::GetBootMode(Kernel::HLERequestContext& ctx) {
  531. LOG_DEBUG(Service_AM, "called");
  532. IPC::ResponseBuilder rb{ctx, 3};
  533. rb.Push(RESULT_SUCCESS);
  534. rb.Push<u8>(static_cast<u8>(Service::PM::SystemBootMode::Normal)); // Normal boot mode
  535. }
  536. void ICommonStateGetter::GetEventHandle(Kernel::HLERequestContext& ctx) {
  537. LOG_DEBUG(Service_AM, "called");
  538. IPC::ResponseBuilder rb{ctx, 2, 1};
  539. rb.Push(RESULT_SUCCESS);
  540. rb.PushCopyObjects(msg_queue->GetMesssageRecieveEvent());
  541. }
  542. void ICommonStateGetter::ReceiveMessage(Kernel::HLERequestContext& ctx) {
  543. LOG_DEBUG(Service_AM, "called");
  544. const auto message = msg_queue->PopMessage();
  545. IPC::ResponseBuilder rb{ctx, 3};
  546. if (message == AppletMessageQueue::AppletMessage::NoMessage) {
  547. LOG_ERROR(Service_AM, "Message queue is empty");
  548. rb.Push(ERR_NO_MESSAGES);
  549. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  550. return;
  551. }
  552. rb.Push(RESULT_SUCCESS);
  553. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  554. }
  555. void ICommonStateGetter::GetCurrentFocusState(Kernel::HLERequestContext& ctx) {
  556. LOG_WARNING(Service_AM, "(STUBBED) called");
  557. IPC::ResponseBuilder rb{ctx, 3};
  558. rb.Push(RESULT_SUCCESS);
  559. rb.Push(static_cast<u8>(FocusState::InFocus));
  560. }
  561. void ICommonStateGetter::IsVrModeEnabled(Kernel::HLERequestContext& ctx) {
  562. LOG_DEBUG(Service_AM, "called");
  563. IPC::ResponseBuilder rb{ctx, 3};
  564. rb.Push(RESULT_SUCCESS);
  565. rb.Push(vr_mode_state);
  566. }
  567. void ICommonStateGetter::SetVrModeEnabled(Kernel::HLERequestContext& ctx) {
  568. IPC::RequestParser rp{ctx};
  569. vr_mode_state = rp.Pop<bool>();
  570. LOG_WARNING(Service_AM, "VR Mode is {}", vr_mode_state ? "on" : "off");
  571. IPC::ResponseBuilder rb{ctx, 2};
  572. rb.Push(RESULT_SUCCESS);
  573. }
  574. void ICommonStateGetter::SetLcdBacklighOffEnabled(Kernel::HLERequestContext& ctx) {
  575. IPC::RequestParser rp{ctx};
  576. const auto is_lcd_backlight_off_enabled = rp.Pop<bool>();
  577. LOG_WARNING(Service_AM, "(STUBBED) called. is_lcd_backlight_off_enabled={}",
  578. is_lcd_backlight_off_enabled);
  579. IPC::ResponseBuilder rb{ctx, 2};
  580. rb.Push(RESULT_SUCCESS);
  581. }
  582. void ICommonStateGetter::EndVrModeEx(Kernel::HLERequestContext& ctx) {
  583. LOG_WARNING(Service_AM, "(STUBBED) called");
  584. IPC::ResponseBuilder rb{ctx, 2};
  585. rb.Push(RESULT_SUCCESS);
  586. }
  587. void ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent(Kernel::HLERequestContext& ctx) {
  588. LOG_DEBUG(Service_AM, "called");
  589. IPC::ResponseBuilder rb{ctx, 2, 1};
  590. rb.Push(RESULT_SUCCESS);
  591. rb.PushCopyObjects(msg_queue->GetOperationModeChangedEvent());
  592. }
  593. void ICommonStateGetter::GetDefaultDisplayResolution(Kernel::HLERequestContext& ctx) {
  594. LOG_DEBUG(Service_AM, "called");
  595. IPC::ResponseBuilder rb{ctx, 4};
  596. rb.Push(RESULT_SUCCESS);
  597. if (Settings::values.use_docked_mode) {
  598. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedWidth) *
  599. static_cast<u32>(Settings::values.resolution_factor));
  600. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedHeight) *
  601. static_cast<u32>(Settings::values.resolution_factor));
  602. } else {
  603. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedWidth) *
  604. static_cast<u32>(Settings::values.resolution_factor));
  605. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedHeight) *
  606. static_cast<u32>(Settings::values.resolution_factor));
  607. }
  608. }
  609. void ICommonStateGetter::SetCpuBoostMode(Kernel::HLERequestContext& ctx) {
  610. LOG_DEBUG(Service_AM, "called, forwarding to APM:SYS");
  611. const auto& sm = system.ServiceManager();
  612. const auto apm_sys = sm.GetService<APM::APM_Sys>("apm:sys");
  613. ASSERT(apm_sys != nullptr);
  614. apm_sys->SetCpuBoostMode(ctx);
  615. }
  616. IStorageImpl::~IStorageImpl() = default;
  617. class StorageDataImpl final : public IStorageImpl {
  618. public:
  619. explicit StorageDataImpl(std::vector<u8>&& buffer) : buffer{std::move(buffer)} {}
  620. std::vector<u8>& GetData() override {
  621. return buffer;
  622. }
  623. const std::vector<u8>& GetData() const override {
  624. return buffer;
  625. }
  626. std::size_t GetSize() const override {
  627. return buffer.size();
  628. }
  629. private:
  630. std::vector<u8> buffer;
  631. };
  632. IStorage::IStorage(std::vector<u8>&& buffer)
  633. : ServiceFramework("IStorage"), impl{std::make_shared<StorageDataImpl>(std::move(buffer))} {
  634. Register();
  635. }
  636. void IStorage::Register() {
  637. // clang-format off
  638. static const FunctionInfo functions[] = {
  639. {0, &IStorage::Open, "Open"},
  640. {1, nullptr, "OpenTransferStorage"},
  641. };
  642. // clang-format on
  643. RegisterHandlers(functions);
  644. }
  645. IStorage::~IStorage() = default;
  646. void IStorage::Open(Kernel::HLERequestContext& ctx) {
  647. LOG_DEBUG(Service_AM, "called");
  648. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  649. rb.Push(RESULT_SUCCESS);
  650. rb.PushIpcInterface<IStorageAccessor>(*this);
  651. }
  652. void ICommonStateGetter::GetOperationMode(Kernel::HLERequestContext& ctx) {
  653. const bool use_docked_mode{Settings::values.use_docked_mode};
  654. LOG_DEBUG(Service_AM, "called, use_docked_mode={}", use_docked_mode);
  655. IPC::ResponseBuilder rb{ctx, 3};
  656. rb.Push(RESULT_SUCCESS);
  657. rb.Push(static_cast<u8>(use_docked_mode ? OperationMode::Docked : OperationMode::Handheld));
  658. }
  659. void ICommonStateGetter::GetPerformanceMode(Kernel::HLERequestContext& ctx) {
  660. LOG_DEBUG(Service_AM, "called");
  661. IPC::ResponseBuilder rb{ctx, 3};
  662. rb.Push(RESULT_SUCCESS);
  663. rb.PushEnum(system.GetAPMController().GetCurrentPerformanceMode());
  664. }
  665. class ILibraryAppletAccessor final : public ServiceFramework<ILibraryAppletAccessor> {
  666. public:
  667. explicit ILibraryAppletAccessor(std::shared_ptr<Applets::Applet> applet)
  668. : ServiceFramework("ILibraryAppletAccessor"), applet(std::move(applet)) {
  669. // clang-format off
  670. static const FunctionInfo functions[] = {
  671. {0, &ILibraryAppletAccessor::GetAppletStateChangedEvent, "GetAppletStateChangedEvent"},
  672. {1, &ILibraryAppletAccessor::IsCompleted, "IsCompleted"},
  673. {10, &ILibraryAppletAccessor::Start, "Start"},
  674. {20, nullptr, "RequestExit"},
  675. {25, nullptr, "Terminate"},
  676. {30, &ILibraryAppletAccessor::GetResult, "GetResult"},
  677. {50, nullptr, "SetOutOfFocusApplicationSuspendingEnabled"},
  678. {60, nullptr, "PresetLibraryAppletGpuTimeSliceZero"},
  679. {100, &ILibraryAppletAccessor::PushInData, "PushInData"},
  680. {101, &ILibraryAppletAccessor::PopOutData, "PopOutData"},
  681. {102, nullptr, "PushExtraStorage"},
  682. {103, &ILibraryAppletAccessor::PushInteractiveInData, "PushInteractiveInData"},
  683. {104, &ILibraryAppletAccessor::PopInteractiveOutData, "PopInteractiveOutData"},
  684. {105, &ILibraryAppletAccessor::GetPopOutDataEvent, "GetPopOutDataEvent"},
  685. {106, &ILibraryAppletAccessor::GetPopInteractiveOutDataEvent, "GetPopInteractiveOutDataEvent"},
  686. {110, nullptr, "NeedsToExitProcess"},
  687. {120, nullptr, "GetLibraryAppletInfo"},
  688. {150, nullptr, "RequestForAppletToGetForeground"},
  689. {160, nullptr, "GetIndirectLayerConsumerHandle"},
  690. };
  691. // clang-format on
  692. RegisterHandlers(functions);
  693. }
  694. private:
  695. void GetAppletStateChangedEvent(Kernel::HLERequestContext& ctx) {
  696. LOG_DEBUG(Service_AM, "called");
  697. const auto event = applet->GetBroker().GetStateChangedEvent();
  698. IPC::ResponseBuilder rb{ctx, 2, 1};
  699. rb.Push(RESULT_SUCCESS);
  700. rb.PushCopyObjects(event);
  701. }
  702. void IsCompleted(Kernel::HLERequestContext& ctx) {
  703. LOG_DEBUG(Service_AM, "called");
  704. IPC::ResponseBuilder rb{ctx, 3};
  705. rb.Push(RESULT_SUCCESS);
  706. rb.Push<u32>(applet->TransactionComplete());
  707. }
  708. void GetResult(Kernel::HLERequestContext& ctx) {
  709. LOG_DEBUG(Service_AM, "called");
  710. IPC::ResponseBuilder rb{ctx, 2};
  711. rb.Push(applet->GetStatus());
  712. }
  713. void Start(Kernel::HLERequestContext& ctx) {
  714. LOG_DEBUG(Service_AM, "called");
  715. ASSERT(applet != nullptr);
  716. applet->Initialize();
  717. applet->Execute();
  718. IPC::ResponseBuilder rb{ctx, 2};
  719. rb.Push(RESULT_SUCCESS);
  720. }
  721. void PushInData(Kernel::HLERequestContext& ctx) {
  722. LOG_DEBUG(Service_AM, "called");
  723. IPC::RequestParser rp{ctx};
  724. applet->GetBroker().PushNormalDataFromGame(rp.PopIpcInterface<IStorage>());
  725. IPC::ResponseBuilder rb{ctx, 2};
  726. rb.Push(RESULT_SUCCESS);
  727. }
  728. void PopOutData(Kernel::HLERequestContext& ctx) {
  729. LOG_DEBUG(Service_AM, "called");
  730. auto storage = applet->GetBroker().PopNormalDataToGame();
  731. if (storage == nullptr) {
  732. LOG_ERROR(Service_AM,
  733. "storage is a nullptr. There is no data in the current normal channel");
  734. IPC::ResponseBuilder rb{ctx, 2};
  735. rb.Push(ERR_NO_DATA_IN_CHANNEL);
  736. return;
  737. }
  738. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  739. rb.Push(RESULT_SUCCESS);
  740. rb.PushIpcInterface<IStorage>(std::move(storage));
  741. }
  742. void PushInteractiveInData(Kernel::HLERequestContext& ctx) {
  743. LOG_DEBUG(Service_AM, "called");
  744. IPC::RequestParser rp{ctx};
  745. applet->GetBroker().PushInteractiveDataFromGame(rp.PopIpcInterface<IStorage>());
  746. ASSERT(applet->IsInitialized());
  747. applet->ExecuteInteractive();
  748. applet->Execute();
  749. IPC::ResponseBuilder rb{ctx, 2};
  750. rb.Push(RESULT_SUCCESS);
  751. }
  752. void PopInteractiveOutData(Kernel::HLERequestContext& ctx) {
  753. LOG_DEBUG(Service_AM, "called");
  754. auto storage = applet->GetBroker().PopInteractiveDataToGame();
  755. if (storage == nullptr) {
  756. LOG_ERROR(Service_AM,
  757. "storage is a nullptr. There is no data in the current interactive channel");
  758. IPC::ResponseBuilder rb{ctx, 2};
  759. rb.Push(ERR_NO_DATA_IN_CHANNEL);
  760. return;
  761. }
  762. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  763. rb.Push(RESULT_SUCCESS);
  764. rb.PushIpcInterface<IStorage>(std::move(storage));
  765. }
  766. void GetPopOutDataEvent(Kernel::HLERequestContext& ctx) {
  767. LOG_DEBUG(Service_AM, "called");
  768. IPC::ResponseBuilder rb{ctx, 2, 1};
  769. rb.Push(RESULT_SUCCESS);
  770. rb.PushCopyObjects(applet->GetBroker().GetNormalDataEvent());
  771. }
  772. void GetPopInteractiveOutDataEvent(Kernel::HLERequestContext& ctx) {
  773. LOG_DEBUG(Service_AM, "called");
  774. IPC::ResponseBuilder rb{ctx, 2, 1};
  775. rb.Push(RESULT_SUCCESS);
  776. rb.PushCopyObjects(applet->GetBroker().GetInteractiveDataEvent());
  777. }
  778. std::shared_ptr<Applets::Applet> applet;
  779. };
  780. IStorageAccessor::IStorageAccessor(IStorage& storage)
  781. : ServiceFramework("IStorageAccessor"), backing(storage) {
  782. // clang-format off
  783. static const FunctionInfo functions[] = {
  784. {0, &IStorageAccessor::GetSize, "GetSize"},
  785. {10, &IStorageAccessor::Write, "Write"},
  786. {11, &IStorageAccessor::Read, "Read"},
  787. };
  788. // clang-format on
  789. RegisterHandlers(functions);
  790. }
  791. IStorageAccessor::~IStorageAccessor() = default;
  792. void IStorageAccessor::GetSize(Kernel::HLERequestContext& ctx) {
  793. LOG_DEBUG(Service_AM, "called");
  794. IPC::ResponseBuilder rb{ctx, 4};
  795. rb.Push(RESULT_SUCCESS);
  796. rb.Push(static_cast<u64>(backing.GetSize()));
  797. }
  798. void IStorageAccessor::Write(Kernel::HLERequestContext& ctx) {
  799. IPC::RequestParser rp{ctx};
  800. const u64 offset{rp.Pop<u64>()};
  801. const std::vector<u8> data{ctx.ReadBuffer()};
  802. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, data.size());
  803. if (data.size() > backing.GetSize() - offset) {
  804. LOG_ERROR(Service_AM,
  805. "offset is out of bounds, backing_buffer_sz={}, data_size={}, offset={}",
  806. backing.GetSize(), data.size(), offset);
  807. IPC::ResponseBuilder rb{ctx, 2};
  808. rb.Push(ERR_SIZE_OUT_OF_BOUNDS);
  809. return;
  810. }
  811. std::memcpy(backing.GetData().data() + offset, data.data(), data.size());
  812. IPC::ResponseBuilder rb{ctx, 2};
  813. rb.Push(RESULT_SUCCESS);
  814. }
  815. void IStorageAccessor::Read(Kernel::HLERequestContext& ctx) {
  816. IPC::RequestParser rp{ctx};
  817. const u64 offset{rp.Pop<u64>()};
  818. const std::size_t size{ctx.GetWriteBufferSize()};
  819. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  820. if (size > backing.GetSize() - offset) {
  821. LOG_ERROR(Service_AM, "offset is out of bounds, backing_buffer_sz={}, size={}, offset={}",
  822. backing.GetSize(), size, offset);
  823. IPC::ResponseBuilder rb{ctx, 2};
  824. rb.Push(ERR_SIZE_OUT_OF_BOUNDS);
  825. return;
  826. }
  827. ctx.WriteBuffer(backing.GetData().data() + offset, size);
  828. IPC::ResponseBuilder rb{ctx, 2};
  829. rb.Push(RESULT_SUCCESS);
  830. }
  831. ILibraryAppletCreator::ILibraryAppletCreator(Core::System& system_)
  832. : ServiceFramework("ILibraryAppletCreator"), system{system_} {
  833. static const FunctionInfo functions[] = {
  834. {0, &ILibraryAppletCreator::CreateLibraryApplet, "CreateLibraryApplet"},
  835. {1, nullptr, "TerminateAllLibraryApplets"},
  836. {2, nullptr, "AreAnyLibraryAppletsLeft"},
  837. {10, &ILibraryAppletCreator::CreateStorage, "CreateStorage"},
  838. {11, &ILibraryAppletCreator::CreateTransferMemoryStorage, "CreateTransferMemoryStorage"},
  839. {12, nullptr, "CreateHandleStorage"},
  840. };
  841. RegisterHandlers(functions);
  842. }
  843. ILibraryAppletCreator::~ILibraryAppletCreator() = default;
  844. void ILibraryAppletCreator::CreateLibraryApplet(Kernel::HLERequestContext& ctx) {
  845. IPC::RequestParser rp{ctx};
  846. const auto applet_id = rp.PopRaw<Applets::AppletId>();
  847. const auto applet_mode = rp.PopRaw<u32>();
  848. LOG_DEBUG(Service_AM, "called with applet_id={:08X}, applet_mode={:08X}",
  849. static_cast<u32>(applet_id), applet_mode);
  850. const auto& applet_manager{system.GetAppletManager()};
  851. const auto applet = applet_manager.GetApplet(applet_id);
  852. if (applet == nullptr) {
  853. LOG_ERROR(Service_AM, "Applet doesn't exist! applet_id={}", static_cast<u32>(applet_id));
  854. IPC::ResponseBuilder rb{ctx, 2};
  855. rb.Push(RESULT_UNKNOWN);
  856. return;
  857. }
  858. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  859. rb.Push(RESULT_SUCCESS);
  860. rb.PushIpcInterface<AM::ILibraryAppletAccessor>(applet);
  861. }
  862. void ILibraryAppletCreator::CreateStorage(Kernel::HLERequestContext& ctx) {
  863. IPC::RequestParser rp{ctx};
  864. const u64 size{rp.Pop<u64>()};
  865. LOG_DEBUG(Service_AM, "called, size={}", size);
  866. std::vector<u8> buffer(size);
  867. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  868. rb.Push(RESULT_SUCCESS);
  869. rb.PushIpcInterface<AM::IStorage>(std::move(buffer));
  870. }
  871. void ILibraryAppletCreator::CreateTransferMemoryStorage(Kernel::HLERequestContext& ctx) {
  872. LOG_DEBUG(Service_AM, "called");
  873. IPC::RequestParser rp{ctx};
  874. rp.SetCurrentOffset(3);
  875. const auto handle{rp.Pop<Kernel::Handle>()};
  876. auto transfer_mem =
  877. system.CurrentProcess()->GetHandleTable().Get<Kernel::TransferMemory>(handle);
  878. if (transfer_mem == nullptr) {
  879. LOG_ERROR(Service_AM, "shared_mem is a nullpr for handle={:08X}", handle);
  880. IPC::ResponseBuilder rb{ctx, 2};
  881. rb.Push(RESULT_UNKNOWN);
  882. return;
  883. }
  884. const u8* const mem_begin = transfer_mem->GetPointer();
  885. const u8* const mem_end = mem_begin + transfer_mem->GetSize();
  886. std::vector<u8> memory{mem_begin, mem_end};
  887. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  888. rb.Push(RESULT_SUCCESS);
  889. rb.PushIpcInterface<IStorage>(std::move(memory));
  890. }
  891. IApplicationFunctions::IApplicationFunctions(Core::System& system_)
  892. : ServiceFramework("IApplicationFunctions"), system{system_} {
  893. // clang-format off
  894. static const FunctionInfo functions[] = {
  895. {1, &IApplicationFunctions::PopLaunchParameter, "PopLaunchParameter"},
  896. {10, nullptr, "CreateApplicationAndPushAndRequestToStart"},
  897. {11, nullptr, "CreateApplicationAndPushAndRequestToStartForQuest"},
  898. {12, nullptr, "CreateApplicationAndRequestToStart"},
  899. {13, &IApplicationFunctions::CreateApplicationAndRequestToStartForQuest, "CreateApplicationAndRequestToStartForQuest"},
  900. {14, nullptr, "CreateApplicationWithAttributeAndPushAndRequestToStartForQuest"},
  901. {15, nullptr, "CreateApplicationWithAttributeAndRequestToStartForQuest"},
  902. {20, &IApplicationFunctions::EnsureSaveData, "EnsureSaveData"},
  903. {21, &IApplicationFunctions::GetDesiredLanguage, "GetDesiredLanguage"},
  904. {22, &IApplicationFunctions::SetTerminateResult, "SetTerminateResult"},
  905. {23, &IApplicationFunctions::GetDisplayVersion, "GetDisplayVersion"},
  906. {24, nullptr, "GetLaunchStorageInfoForDebug"},
  907. {25, &IApplicationFunctions::ExtendSaveData, "ExtendSaveData"},
  908. {26, &IApplicationFunctions::GetSaveDataSize, "GetSaveDataSize"},
  909. {30, &IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed, "BeginBlockingHomeButtonShortAndLongPressed"},
  910. {31, &IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed, "EndBlockingHomeButtonShortAndLongPressed"},
  911. {32, &IApplicationFunctions::BeginBlockingHomeButton, "BeginBlockingHomeButton"},
  912. {33, &IApplicationFunctions::EndBlockingHomeButton, "EndBlockingHomeButton"},
  913. {34, nullptr, "SelectApplicationLicense"},
  914. {40, &IApplicationFunctions::NotifyRunning, "NotifyRunning"},
  915. {50, &IApplicationFunctions::GetPseudoDeviceId, "GetPseudoDeviceId"},
  916. {60, nullptr, "SetMediaPlaybackStateForApplication"},
  917. {65, nullptr, "IsGamePlayRecordingSupported"},
  918. {66, &IApplicationFunctions::InitializeGamePlayRecording, "InitializeGamePlayRecording"},
  919. {67, &IApplicationFunctions::SetGamePlayRecordingState, "SetGamePlayRecordingState"},
  920. {68, nullptr, "RequestFlushGamePlayingMovieForDebug"},
  921. {70, nullptr, "RequestToShutdown"},
  922. {71, nullptr, "RequestToReboot"},
  923. {72, nullptr, "RequestToSleep"},
  924. {80, nullptr, "ExitAndRequestToShowThanksMessage"},
  925. {90, &IApplicationFunctions::EnableApplicationCrashReport, "EnableApplicationCrashReport"},
  926. {100, &IApplicationFunctions::InitializeApplicationCopyrightFrameBuffer, "InitializeApplicationCopyrightFrameBuffer"},
  927. {101, &IApplicationFunctions::SetApplicationCopyrightImage, "SetApplicationCopyrightImage"},
  928. {102, &IApplicationFunctions::SetApplicationCopyrightVisibility, "SetApplicationCopyrightVisibility"},
  929. {110, &IApplicationFunctions::QueryApplicationPlayStatistics, "QueryApplicationPlayStatistics"},
  930. {111, &IApplicationFunctions::QueryApplicationPlayStatisticsByUid, "QueryApplicationPlayStatisticsByUid"},
  931. {120, nullptr, "ExecuteProgram"},
  932. {121, nullptr, "ClearUserChannel"},
  933. {122, nullptr, "UnpopToUserChannel"},
  934. {130, &IApplicationFunctions::GetGpuErrorDetectedSystemEvent, "GetGpuErrorDetectedSystemEvent"},
  935. {140, &IApplicationFunctions::GetFriendInvitationStorageChannelEvent, "GetFriendInvitationStorageChannelEvent"},
  936. {141, nullptr, "TryPopFromFriendInvitationStorageChannel"},
  937. {150, nullptr, "GetNotificationStorageChannelEvent"},
  938. {151, nullptr, "TryPopFromNotificationStorageChannel"},
  939. {160, nullptr, "GetHealthWarningDisappearedSystemEvent"},
  940. {170, nullptr, "SetHdcpAuthenticationActivated"},
  941. {500, nullptr, "StartContinuousRecordingFlushForDebug"},
  942. {1000, nullptr, "CreateMovieMaker"},
  943. {1001, nullptr, "PrepareForJit"},
  944. };
  945. // clang-format on
  946. RegisterHandlers(functions);
  947. auto& kernel = system.Kernel();
  948. gpu_error_detected_event = Kernel::WritableEvent::CreateEventPair(
  949. kernel, "IApplicationFunctions:GpuErrorDetectedSystemEvent");
  950. friend_invitation_storage_channel_event = Kernel::WritableEvent::CreateEventPair(
  951. kernel, "IApplicationFunctions:FriendInvitationStorageChannelEvent");
  952. }
  953. IApplicationFunctions::~IApplicationFunctions() = default;
  954. void IApplicationFunctions::EnableApplicationCrashReport(Kernel::HLERequestContext& ctx) {
  955. LOG_WARNING(Service_AM, "(STUBBED) called");
  956. IPC::ResponseBuilder rb{ctx, 2};
  957. rb.Push(RESULT_SUCCESS);
  958. }
  959. void IApplicationFunctions::InitializeApplicationCopyrightFrameBuffer(
  960. Kernel::HLERequestContext& ctx) {
  961. LOG_WARNING(Service_AM, "(STUBBED) called");
  962. IPC::ResponseBuilder rb{ctx, 2};
  963. rb.Push(RESULT_SUCCESS);
  964. }
  965. void IApplicationFunctions::SetApplicationCopyrightImage(Kernel::HLERequestContext& ctx) {
  966. LOG_WARNING(Service_AM, "(STUBBED) called");
  967. IPC::ResponseBuilder rb{ctx, 2};
  968. rb.Push(RESULT_SUCCESS);
  969. }
  970. void IApplicationFunctions::SetApplicationCopyrightVisibility(Kernel::HLERequestContext& ctx) {
  971. IPC::RequestParser rp{ctx};
  972. const auto is_visible = rp.Pop<bool>();
  973. LOG_WARNING(Service_AM, "(STUBBED) called, is_visible={}", is_visible);
  974. IPC::ResponseBuilder rb{ctx, 2};
  975. rb.Push(RESULT_SUCCESS);
  976. }
  977. void IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed(
  978. Kernel::HLERequestContext& ctx) {
  979. LOG_WARNING(Service_AM, "(STUBBED) called");
  980. IPC::ResponseBuilder rb{ctx, 2};
  981. rb.Push(RESULT_SUCCESS);
  982. }
  983. void IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed(
  984. Kernel::HLERequestContext& ctx) {
  985. LOG_WARNING(Service_AM, "(STUBBED) called");
  986. IPC::ResponseBuilder rb{ctx, 2};
  987. rb.Push(RESULT_SUCCESS);
  988. }
  989. void IApplicationFunctions::BeginBlockingHomeButton(Kernel::HLERequestContext& ctx) {
  990. LOG_WARNING(Service_AM, "(STUBBED) called");
  991. IPC::ResponseBuilder rb{ctx, 2};
  992. rb.Push(RESULT_SUCCESS);
  993. }
  994. void IApplicationFunctions::EndBlockingHomeButton(Kernel::HLERequestContext& ctx) {
  995. LOG_WARNING(Service_AM, "(STUBBED) called");
  996. IPC::ResponseBuilder rb{ctx, 2};
  997. rb.Push(RESULT_SUCCESS);
  998. }
  999. void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) {
  1000. IPC::RequestParser rp{ctx};
  1001. const auto kind = rp.PopEnum<LaunchParameterKind>();
  1002. LOG_DEBUG(Service_AM, "called, kind={:08X}", static_cast<u8>(kind));
  1003. if (kind == LaunchParameterKind::ApplicationSpecific && !launch_popped_application_specific) {
  1004. const auto backend = BCAT::CreateBackendFromSettings(system, [this](u64 tid) {
  1005. return system.GetFileSystemController().GetBCATDirectory(tid);
  1006. });
  1007. const auto build_id_full = system.GetCurrentProcessBuildID();
  1008. u64 build_id{};
  1009. std::memcpy(&build_id, build_id_full.data(), sizeof(u64));
  1010. auto data = backend->GetLaunchParameter({system.CurrentProcess()->GetTitleID(), build_id});
  1011. if (data.has_value()) {
  1012. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1013. rb.Push(RESULT_SUCCESS);
  1014. rb.PushIpcInterface<IStorage>(std::move(*data));
  1015. launch_popped_application_specific = true;
  1016. return;
  1017. }
  1018. } else if (kind == LaunchParameterKind::AccountPreselectedUser &&
  1019. !launch_popped_account_preselect) {
  1020. LaunchParameterAccountPreselectedUser params{};
  1021. params.magic = LAUNCH_PARAMETER_ACCOUNT_PRESELECTED_USER_MAGIC;
  1022. params.is_account_selected = 1;
  1023. Account::ProfileManager profile_manager{};
  1024. const auto uuid = profile_manager.GetUser(Settings::values.current_user);
  1025. ASSERT(uuid);
  1026. params.current_user = uuid->uuid;
  1027. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1028. rb.Push(RESULT_SUCCESS);
  1029. std::vector<u8> buffer(sizeof(LaunchParameterAccountPreselectedUser));
  1030. std::memcpy(buffer.data(), &params, buffer.size());
  1031. rb.PushIpcInterface<IStorage>(std::move(buffer));
  1032. launch_popped_account_preselect = true;
  1033. return;
  1034. }
  1035. LOG_ERROR(Service_AM, "Attempted to load launch parameter but none was found!");
  1036. IPC::ResponseBuilder rb{ctx, 2};
  1037. rb.Push(ERR_NO_DATA_IN_CHANNEL);
  1038. }
  1039. void IApplicationFunctions::CreateApplicationAndRequestToStartForQuest(
  1040. Kernel::HLERequestContext& ctx) {
  1041. LOG_WARNING(Service_AM, "(STUBBED) called");
  1042. IPC::ResponseBuilder rb{ctx, 2};
  1043. rb.Push(RESULT_SUCCESS);
  1044. }
  1045. void IApplicationFunctions::EnsureSaveData(Kernel::HLERequestContext& ctx) {
  1046. IPC::RequestParser rp{ctx};
  1047. u128 user_id = rp.PopRaw<u128>();
  1048. LOG_DEBUG(Service_AM, "called, uid={:016X}{:016X}", user_id[1], user_id[0]);
  1049. FileSys::SaveDataDescriptor descriptor{};
  1050. descriptor.title_id = system.CurrentProcess()->GetTitleID();
  1051. descriptor.user_id = user_id;
  1052. descriptor.type = FileSys::SaveDataType::SaveData;
  1053. const auto res = system.GetFileSystemController().CreateSaveData(
  1054. FileSys::SaveDataSpaceId::NandUser, descriptor);
  1055. IPC::ResponseBuilder rb{ctx, 4};
  1056. rb.Push(res.Code());
  1057. rb.Push<u64>(0);
  1058. }
  1059. void IApplicationFunctions::SetTerminateResult(Kernel::HLERequestContext& ctx) {
  1060. // Takes an input u32 Result, no output.
  1061. // For example, in some cases official apps use this with error 0x2A2 then
  1062. // uses svcBreak.
  1063. IPC::RequestParser rp{ctx};
  1064. u32 result = rp.Pop<u32>();
  1065. LOG_WARNING(Service_AM, "(STUBBED) called, result=0x{:08X}", result);
  1066. IPC::ResponseBuilder rb{ctx, 2};
  1067. rb.Push(RESULT_SUCCESS);
  1068. }
  1069. void IApplicationFunctions::GetDisplayVersion(Kernel::HLERequestContext& ctx) {
  1070. LOG_WARNING(Service_AM, "(STUBBED) called");
  1071. IPC::ResponseBuilder rb{ctx, 6};
  1072. rb.Push(RESULT_SUCCESS);
  1073. rb.Push<u64>(1);
  1074. rb.Push<u64>(0);
  1075. }
  1076. void IApplicationFunctions::GetDesiredLanguage(Kernel::HLERequestContext& ctx) {
  1077. // TODO(bunnei): This should be configurable
  1078. LOG_DEBUG(Service_AM, "called");
  1079. // Get supported languages from NACP, if possible
  1080. // Default to 0 (all languages supported)
  1081. u32 supported_languages = 0;
  1082. FileSys::PatchManager pm{system.CurrentProcess()->GetTitleID()};
  1083. const auto res = pm.GetControlMetadata();
  1084. if (res.first != nullptr) {
  1085. supported_languages = res.first->GetSupportedLanguages();
  1086. }
  1087. // Call IApplicationManagerInterface implementation.
  1088. auto& service_manager = system.ServiceManager();
  1089. auto ns_am2 = service_manager.GetService<NS::NS>("ns:am2");
  1090. auto app_man = ns_am2->GetApplicationManagerInterface();
  1091. // Get desired application language
  1092. const auto res_lang = app_man->GetApplicationDesiredLanguage(supported_languages);
  1093. if (res_lang.Failed()) {
  1094. IPC::ResponseBuilder rb{ctx, 2};
  1095. rb.Push(res_lang.Code());
  1096. return;
  1097. }
  1098. // Convert to settings language code.
  1099. const auto res_code = app_man->ConvertApplicationLanguageToLanguageCode(*res_lang);
  1100. if (res_code.Failed()) {
  1101. IPC::ResponseBuilder rb{ctx, 2};
  1102. rb.Push(res_code.Code());
  1103. return;
  1104. }
  1105. LOG_DEBUG(Service_AM, "got desired_language={:016X}", *res_code);
  1106. IPC::ResponseBuilder rb{ctx, 4};
  1107. rb.Push(RESULT_SUCCESS);
  1108. rb.Push(*res_code);
  1109. }
  1110. void IApplicationFunctions::InitializeGamePlayRecording(Kernel::HLERequestContext& ctx) {
  1111. LOG_WARNING(Service_AM, "(STUBBED) called");
  1112. IPC::ResponseBuilder rb{ctx, 2};
  1113. rb.Push(RESULT_SUCCESS);
  1114. }
  1115. void IApplicationFunctions::SetGamePlayRecordingState(Kernel::HLERequestContext& ctx) {
  1116. LOG_WARNING(Service_AM, "(STUBBED) called");
  1117. IPC::ResponseBuilder rb{ctx, 2};
  1118. rb.Push(RESULT_SUCCESS);
  1119. }
  1120. void IApplicationFunctions::NotifyRunning(Kernel::HLERequestContext& ctx) {
  1121. LOG_WARNING(Service_AM, "(STUBBED) called");
  1122. IPC::ResponseBuilder rb{ctx, 3};
  1123. rb.Push(RESULT_SUCCESS);
  1124. rb.Push<u8>(0); // Unknown, seems to be ignored by official processes
  1125. }
  1126. void IApplicationFunctions::GetPseudoDeviceId(Kernel::HLERequestContext& ctx) {
  1127. LOG_WARNING(Service_AM, "(STUBBED) called");
  1128. IPC::ResponseBuilder rb{ctx, 6};
  1129. rb.Push(RESULT_SUCCESS);
  1130. // Returns a 128-bit UUID
  1131. rb.Push<u64>(0);
  1132. rb.Push<u64>(0);
  1133. }
  1134. void IApplicationFunctions::ExtendSaveData(Kernel::HLERequestContext& ctx) {
  1135. struct Parameters {
  1136. FileSys::SaveDataType type;
  1137. u128 user_id;
  1138. u64 new_normal_size;
  1139. u64 new_journal_size;
  1140. };
  1141. static_assert(sizeof(Parameters) == 40);
  1142. IPC::RequestParser rp{ctx};
  1143. const auto [type, user_id, new_normal_size, new_journal_size] = rp.PopRaw<Parameters>();
  1144. LOG_DEBUG(Service_AM,
  1145. "called with type={:02X}, user_id={:016X}{:016X}, new_normal={:016X}, "
  1146. "new_journal={:016X}",
  1147. static_cast<u8>(type), user_id[1], user_id[0], new_normal_size, new_journal_size);
  1148. system.GetFileSystemController().WriteSaveDataSize(
  1149. type, system.CurrentProcess()->GetTitleID(), user_id, {new_normal_size, new_journal_size});
  1150. IPC::ResponseBuilder rb{ctx, 4};
  1151. rb.Push(RESULT_SUCCESS);
  1152. // The following value is used upon failure to help the system recover.
  1153. // Since we always succeed, this should be 0.
  1154. rb.Push<u64>(0);
  1155. }
  1156. void IApplicationFunctions::GetSaveDataSize(Kernel::HLERequestContext& ctx) {
  1157. struct Parameters {
  1158. FileSys::SaveDataType type;
  1159. u128 user_id;
  1160. };
  1161. static_assert(sizeof(Parameters) == 24);
  1162. IPC::RequestParser rp{ctx};
  1163. const auto [type, user_id] = rp.PopRaw<Parameters>();
  1164. LOG_DEBUG(Service_AM, "called with type={:02X}, user_id={:016X}{:016X}", static_cast<u8>(type),
  1165. user_id[1], user_id[0]);
  1166. const auto size = system.GetFileSystemController().ReadSaveDataSize(
  1167. type, system.CurrentProcess()->GetTitleID(), user_id);
  1168. IPC::ResponseBuilder rb{ctx, 6};
  1169. rb.Push(RESULT_SUCCESS);
  1170. rb.Push(size.normal);
  1171. rb.Push(size.journal);
  1172. }
  1173. void IApplicationFunctions::QueryApplicationPlayStatistics(Kernel::HLERequestContext& ctx) {
  1174. LOG_WARNING(Service_AM, "(STUBBED) called");
  1175. IPC::ResponseBuilder rb{ctx, 3};
  1176. rb.Push(RESULT_SUCCESS);
  1177. rb.Push<u32>(0);
  1178. }
  1179. void IApplicationFunctions::QueryApplicationPlayStatisticsByUid(Kernel::HLERequestContext& ctx) {
  1180. LOG_WARNING(Service_AM, "(STUBBED) called");
  1181. IPC::ResponseBuilder rb{ctx, 3};
  1182. rb.Push(RESULT_SUCCESS);
  1183. rb.Push<u32>(0);
  1184. }
  1185. void IApplicationFunctions::GetGpuErrorDetectedSystemEvent(Kernel::HLERequestContext& ctx) {
  1186. LOG_WARNING(Service_AM, "(STUBBED) called");
  1187. IPC::ResponseBuilder rb{ctx, 2, 1};
  1188. rb.Push(RESULT_SUCCESS);
  1189. rb.PushCopyObjects(gpu_error_detected_event.readable);
  1190. }
  1191. void IApplicationFunctions::GetFriendInvitationStorageChannelEvent(Kernel::HLERequestContext& ctx) {
  1192. LOG_DEBUG(Service_AM, "called");
  1193. IPC::ResponseBuilder rb{ctx, 2, 1};
  1194. rb.Push(RESULT_SUCCESS);
  1195. rb.PushCopyObjects(friend_invitation_storage_channel_event.readable);
  1196. }
  1197. void InstallInterfaces(SM::ServiceManager& service_manager,
  1198. std::shared_ptr<NVFlinger::NVFlinger> nvflinger, Core::System& system) {
  1199. auto message_queue = std::make_shared<AppletMessageQueue>(system.Kernel());
  1200. // Needed on game boot
  1201. message_queue->PushMessage(AppletMessageQueue::AppletMessage::FocusStateChanged);
  1202. std::make_shared<AppletAE>(nvflinger, message_queue, system)->InstallAsService(service_manager);
  1203. std::make_shared<AppletOE>(nvflinger, message_queue, system)->InstallAsService(service_manager);
  1204. std::make_shared<IdleSys>()->InstallAsService(service_manager);
  1205. std::make_shared<OMM>()->InstallAsService(service_manager);
  1206. std::make_shared<SPSM>()->InstallAsService(service_manager);
  1207. std::make_shared<TCAP>()->InstallAsService(service_manager);
  1208. }
  1209. IHomeMenuFunctions::IHomeMenuFunctions() : ServiceFramework("IHomeMenuFunctions") {
  1210. // clang-format off
  1211. static const FunctionInfo functions[] = {
  1212. {10, &IHomeMenuFunctions::RequestToGetForeground, "RequestToGetForeground"},
  1213. {11, nullptr, "LockForeground"},
  1214. {12, nullptr, "UnlockForeground"},
  1215. {20, nullptr, "PopFromGeneralChannel"},
  1216. {21, nullptr, "GetPopFromGeneralChannelEvent"},
  1217. {30, nullptr, "GetHomeButtonWriterLockAccessor"},
  1218. {31, nullptr, "GetWriterLockAccessorEx"},
  1219. {100, nullptr, "PopRequestLaunchApplicationForDebug"},
  1220. {110, nullptr, "IsForceTerminateApplicationDisabledForDebug"},
  1221. {200, nullptr, "LaunchDevMenu"},
  1222. };
  1223. // clang-format on
  1224. RegisterHandlers(functions);
  1225. }
  1226. IHomeMenuFunctions::~IHomeMenuFunctions() = default;
  1227. void IHomeMenuFunctions::RequestToGetForeground(Kernel::HLERequestContext& ctx) {
  1228. LOG_WARNING(Service_AM, "(STUBBED) called");
  1229. IPC::ResponseBuilder rb{ctx, 2};
  1230. rb.Push(RESULT_SUCCESS);
  1231. }
  1232. IGlobalStateController::IGlobalStateController() : ServiceFramework("IGlobalStateController") {
  1233. // clang-format off
  1234. static const FunctionInfo functions[] = {
  1235. {0, nullptr, "RequestToEnterSleep"},
  1236. {1, nullptr, "EnterSleep"},
  1237. {2, nullptr, "StartSleepSequence"},
  1238. {3, nullptr, "StartShutdownSequence"},
  1239. {4, nullptr, "StartRebootSequence"},
  1240. {9, nullptr, "IsAutoPowerDownRequested"},
  1241. {10, nullptr, "LoadAndApplyIdlePolicySettings"},
  1242. {11, nullptr, "NotifyCecSettingsChanged"},
  1243. {12, nullptr, "SetDefaultHomeButtonLongPressTime"},
  1244. {13, nullptr, "UpdateDefaultDisplayResolution"},
  1245. {14, nullptr, "ShouldSleepOnBoot"},
  1246. {15, nullptr, "GetHdcpAuthenticationFailedEvent"},
  1247. };
  1248. // clang-format on
  1249. RegisterHandlers(functions);
  1250. }
  1251. IGlobalStateController::~IGlobalStateController() = default;
  1252. IApplicationCreator::IApplicationCreator() : ServiceFramework("IApplicationCreator") {
  1253. // clang-format off
  1254. static const FunctionInfo functions[] = {
  1255. {0, nullptr, "CreateApplication"},
  1256. {1, nullptr, "PopLaunchRequestedApplication"},
  1257. {10, nullptr, "CreateSystemApplication"},
  1258. {100, nullptr, "PopFloatingApplicationForDevelopment"},
  1259. };
  1260. // clang-format on
  1261. RegisterHandlers(functions);
  1262. }
  1263. IApplicationCreator::~IApplicationCreator() = default;
  1264. IProcessWindingController::IProcessWindingController()
  1265. : ServiceFramework("IProcessWindingController") {
  1266. // clang-format off
  1267. static const FunctionInfo functions[] = {
  1268. {0, nullptr, "GetLaunchReason"},
  1269. {11, nullptr, "OpenCallingLibraryApplet"},
  1270. {21, nullptr, "PushContext"},
  1271. {22, nullptr, "PopContext"},
  1272. {23, nullptr, "CancelWindingReservation"},
  1273. {30, nullptr, "WindAndDoReserved"},
  1274. {40, nullptr, "ReserveToStartAndWaitAndUnwindThis"},
  1275. {41, nullptr, "ReserveToStartAndWait"},
  1276. };
  1277. // clang-format on
  1278. RegisterHandlers(functions);
  1279. }
  1280. IProcessWindingController::~IProcessWindingController() = default;
  1281. } // namespace Service::AM