am.cpp 68 KB

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