am.cpp 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <cinttypes>
  6. #include <cstring>
  7. #include "common/settings.h"
  8. #include "common/settings_enums.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/kernel/k_event.h"
  15. #include "core/hle/kernel/k_transfer_memory.h"
  16. #include "core/hle/result.h"
  17. #include "core/hle/service/acc/profile_manager.h"
  18. #include "core/hle/service/am/am.h"
  19. #include "core/hle/service/am/applet_ae.h"
  20. #include "core/hle/service/am/applet_oe.h"
  21. #include "core/hle/service/am/applets/applet_mii_edit_types.h"
  22. #include "core/hle/service/am/applets/applet_profile_select.h"
  23. #include "core/hle/service/am/applets/applet_web_browser.h"
  24. #include "core/hle/service/am/applets/applets.h"
  25. #include "core/hle/service/am/idle.h"
  26. #include "core/hle/service/am/omm.h"
  27. #include "core/hle/service/am/spsm.h"
  28. #include "core/hle/service/apm/apm_controller.h"
  29. #include "core/hle/service/apm/apm_interface.h"
  30. #include "core/hle/service/bcat/backend/backend.h"
  31. #include "core/hle/service/caps/caps.h"
  32. #include "core/hle/service/filesystem/filesystem.h"
  33. #include "core/hle/service/ipc_helpers.h"
  34. #include "core/hle/service/ns/ns.h"
  35. #include "core/hle/service/nvnflinger/nvnflinger.h"
  36. #include "core/hle/service/pm/pm.h"
  37. #include "core/hle/service/server_manager.h"
  38. #include "core/hle/service/sm/sm.h"
  39. #include "core/hle/service/vi/vi.h"
  40. #include "core/memory.h"
  41. namespace Service::AM {
  42. constexpr Result ResultNoDataInChannel{ErrorModule::AM, 2};
  43. constexpr Result ResultNoMessages{ErrorModule::AM, 3};
  44. constexpr Result ResultInvalidOffset{ErrorModule::AM, 503};
  45. enum class LaunchParameterKind : u32 {
  46. UserChannel = 1,
  47. AccountPreselectedUser = 2,
  48. };
  49. constexpr u32 LAUNCH_PARAMETER_ACCOUNT_PRESELECTED_USER_MAGIC = 0xC79497CA;
  50. struct LaunchParameterAccountPreselectedUser {
  51. u32_le magic;
  52. u32_le is_account_selected;
  53. Common::UUID current_user;
  54. INSERT_PADDING_BYTES(0x70);
  55. };
  56. static_assert(sizeof(LaunchParameterAccountPreselectedUser) == 0x88);
  57. IWindowController::IWindowController(Core::System& system_)
  58. : ServiceFramework{system_, "IWindowController"} {
  59. // clang-format off
  60. static const FunctionInfo functions[] = {
  61. {0, nullptr, "CreateWindow"},
  62. {1, &IWindowController::GetAppletResourceUserId, "GetAppletResourceUserId"},
  63. {2, nullptr, "GetAppletResourceUserIdOfCallerApplet"},
  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(HLERequestContext& ctx) {
  75. const u64 process_id = system.ApplicationProcess()->GetProcessId();
  76. LOG_DEBUG(Service_AM, "called. Process ID=0x{:016X}", process_id);
  77. IPC::ResponseBuilder rb{ctx, 4};
  78. rb.Push(ResultSuccess);
  79. rb.Push<u64>(process_id);
  80. }
  81. void IWindowController::AcquireForegroundRights(HLERequestContext& ctx) {
  82. LOG_WARNING(Service_AM, "(STUBBED) called");
  83. IPC::ResponseBuilder rb{ctx, 2};
  84. rb.Push(ResultSuccess);
  85. }
  86. IAudioController::IAudioController(Core::System& system_)
  87. : ServiceFramework{system_, "IAudioController"} {
  88. // clang-format off
  89. static const FunctionInfo functions[] = {
  90. {0, &IAudioController::SetExpectedMasterVolume, "SetExpectedMasterVolume"},
  91. {1, &IAudioController::GetMainAppletExpectedMasterVolume, "GetMainAppletExpectedMasterVolume"},
  92. {2, &IAudioController::GetLibraryAppletExpectedMasterVolume, "GetLibraryAppletExpectedMasterVolume"},
  93. {3, &IAudioController::ChangeMainAppletMasterVolume, "ChangeMainAppletMasterVolume"},
  94. {4, &IAudioController::SetTransparentAudioRate, "SetTransparentVolumeRate"},
  95. };
  96. // clang-format on
  97. RegisterHandlers(functions);
  98. }
  99. IAudioController::~IAudioController() = default;
  100. void IAudioController::SetExpectedMasterVolume(HLERequestContext& ctx) {
  101. IPC::RequestParser rp{ctx};
  102. const float main_applet_volume_tmp = rp.Pop<float>();
  103. const float library_applet_volume_tmp = rp.Pop<float>();
  104. LOG_DEBUG(Service_AM, "called. main_applet_volume={}, library_applet_volume={}",
  105. main_applet_volume_tmp, library_applet_volume_tmp);
  106. // Ensure the volume values remain within the 0-100% range
  107. main_applet_volume = std::clamp(main_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  108. library_applet_volume =
  109. std::clamp(library_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  110. IPC::ResponseBuilder rb{ctx, 2};
  111. rb.Push(ResultSuccess);
  112. }
  113. void IAudioController::GetMainAppletExpectedMasterVolume(HLERequestContext& ctx) {
  114. LOG_DEBUG(Service_AM, "called. main_applet_volume={}", main_applet_volume);
  115. IPC::ResponseBuilder rb{ctx, 3};
  116. rb.Push(ResultSuccess);
  117. rb.Push(main_applet_volume);
  118. }
  119. void IAudioController::GetLibraryAppletExpectedMasterVolume(HLERequestContext& ctx) {
  120. LOG_DEBUG(Service_AM, "called. library_applet_volume={}", library_applet_volume);
  121. IPC::ResponseBuilder rb{ctx, 3};
  122. rb.Push(ResultSuccess);
  123. rb.Push(library_applet_volume);
  124. }
  125. void IAudioController::ChangeMainAppletMasterVolume(HLERequestContext& ctx) {
  126. struct Parameters {
  127. float volume;
  128. s64 fade_time_ns;
  129. };
  130. static_assert(sizeof(Parameters) == 16);
  131. IPC::RequestParser rp{ctx};
  132. const auto parameters = rp.PopRaw<Parameters>();
  133. LOG_DEBUG(Service_AM, "called. volume={}, fade_time_ns={}", parameters.volume,
  134. parameters.fade_time_ns);
  135. main_applet_volume = std::clamp(parameters.volume, min_allowed_volume, max_allowed_volume);
  136. fade_time_ns = std::chrono::nanoseconds{parameters.fade_time_ns};
  137. IPC::ResponseBuilder rb{ctx, 2};
  138. rb.Push(ResultSuccess);
  139. }
  140. void IAudioController::SetTransparentAudioRate(HLERequestContext& ctx) {
  141. IPC::RequestParser rp{ctx};
  142. const float transparent_volume_rate_tmp = rp.Pop<float>();
  143. LOG_DEBUG(Service_AM, "called. transparent_volume_rate={}", transparent_volume_rate_tmp);
  144. // Clamp volume range to 0-100%.
  145. transparent_volume_rate =
  146. std::clamp(transparent_volume_rate_tmp, min_allowed_volume, max_allowed_volume);
  147. IPC::ResponseBuilder rb{ctx, 2};
  148. rb.Push(ResultSuccess);
  149. }
  150. IDisplayController::IDisplayController(Core::System& system_)
  151. : ServiceFramework{system_, "IDisplayController"} {
  152. // clang-format off
  153. static const FunctionInfo functions[] = {
  154. {0, nullptr, "GetLastForegroundCaptureImage"},
  155. {1, nullptr, "UpdateLastForegroundCaptureImage"},
  156. {2, nullptr, "GetLastApplicationCaptureImage"},
  157. {3, nullptr, "GetCallerAppletCaptureImage"},
  158. {4, nullptr, "UpdateCallerAppletCaptureImage"},
  159. {5, nullptr, "GetLastForegroundCaptureImageEx"},
  160. {6, nullptr, "GetLastApplicationCaptureImageEx"},
  161. {7, nullptr, "GetCallerAppletCaptureImageEx"},
  162. {8, &IDisplayController::TakeScreenShotOfOwnLayer, "TakeScreenShotOfOwnLayer"},
  163. {9, nullptr, "CopyBetweenCaptureBuffers"},
  164. {10, nullptr, "AcquireLastApplicationCaptureBuffer"},
  165. {11, nullptr, "ReleaseLastApplicationCaptureBuffer"},
  166. {12, nullptr, "AcquireLastForegroundCaptureBuffer"},
  167. {13, nullptr, "ReleaseLastForegroundCaptureBuffer"},
  168. {14, nullptr, "AcquireCallerAppletCaptureBuffer"},
  169. {15, nullptr, "ReleaseCallerAppletCaptureBuffer"},
  170. {16, nullptr, "AcquireLastApplicationCaptureBufferEx"},
  171. {17, nullptr, "AcquireLastForegroundCaptureBufferEx"},
  172. {18, nullptr, "AcquireCallerAppletCaptureBufferEx"},
  173. {20, nullptr, "ClearCaptureBuffer"},
  174. {21, nullptr, "ClearAppletTransitionBuffer"},
  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. {28, nullptr, "TakeScreenShotOfOwnLayerEx"},
  182. };
  183. // clang-format on
  184. RegisterHandlers(functions);
  185. }
  186. IDisplayController::~IDisplayController() = default;
  187. void IDisplayController::TakeScreenShotOfOwnLayer(HLERequestContext& ctx) {
  188. LOG_WARNING(Service_AM, "(STUBBED) called");
  189. IPC::ResponseBuilder rb{ctx, 2};
  190. rb.Push(ResultSuccess);
  191. }
  192. IDebugFunctions::IDebugFunctions(Core::System& system_)
  193. : ServiceFramework{system_, "IDebugFunctions"} {
  194. // clang-format off
  195. static const FunctionInfo functions[] = {
  196. {0, nullptr, "NotifyMessageToHomeMenuForDebug"},
  197. {1, nullptr, "OpenMainApplication"},
  198. {10, nullptr, "PerformSystemButtonPressing"},
  199. {20, nullptr, "InvalidateTransitionLayer"},
  200. {30, nullptr, "RequestLaunchApplicationWithUserAndArgumentForDebug"},
  201. {31, nullptr, "RequestLaunchApplicationByApplicationLaunchInfoForDebug"},
  202. {40, nullptr, "GetAppletResourceUsageInfo"},
  203. {50, nullptr, "AddSystemProgramIdAndAppletIdForDebug"},
  204. {51, nullptr, "AddOperationConfirmedLibraryAppletIdForDebug"},
  205. {100, nullptr, "SetCpuBoostModeForApplet"},
  206. {101, nullptr, "CancelCpuBoostModeForApplet"},
  207. {110, nullptr, "PushToAppletBoundChannelForDebug"},
  208. {111, nullptr, "TryPopFromAppletBoundChannelForDebug"},
  209. {120, nullptr, "AlarmSettingNotificationEnableAppEventReserve"},
  210. {121, nullptr, "AlarmSettingNotificationDisableAppEventReserve"},
  211. {122, nullptr, "AlarmSettingNotificationPushAppEventNotify"},
  212. {130, nullptr, "FriendInvitationSetApplicationParameter"},
  213. {131, nullptr, "FriendInvitationClearApplicationParameter"},
  214. {132, nullptr, "FriendInvitationPushApplicationParameter"},
  215. {140, nullptr, "RestrictPowerOperationForSecureLaunchModeForDebug"},
  216. {200, nullptr, "CreateFloatingLibraryAppletAccepterForDebug"},
  217. {300, nullptr, "TerminateAllRunningApplicationsForDebug"},
  218. {900, nullptr, "GetGrcProcessLaunchedSystemEvent"},
  219. };
  220. // clang-format on
  221. RegisterHandlers(functions);
  222. }
  223. IDebugFunctions::~IDebugFunctions() = default;
  224. ISelfController::ISelfController(Core::System& system_, Nvnflinger::Nvnflinger& nvnflinger_)
  225. : ServiceFramework{system_, "ISelfController"}, nvnflinger{nvnflinger_},
  226. service_context{system, "ISelfController"} {
  227. // clang-format off
  228. static const FunctionInfo functions[] = {
  229. {0, &ISelfController::Exit, "Exit"},
  230. {1, &ISelfController::LockExit, "LockExit"},
  231. {2, &ISelfController::UnlockExit, "UnlockExit"},
  232. {3, &ISelfController::EnterFatalSection, "EnterFatalSection"},
  233. {4, &ISelfController::LeaveFatalSection, "LeaveFatalSection"},
  234. {9, &ISelfController::GetLibraryAppletLaunchableEvent, "GetLibraryAppletLaunchableEvent"},
  235. {10, &ISelfController::SetScreenShotPermission, "SetScreenShotPermission"},
  236. {11, &ISelfController::SetOperationModeChangedNotification, "SetOperationModeChangedNotification"},
  237. {12, &ISelfController::SetPerformanceModeChangedNotification, "SetPerformanceModeChangedNotification"},
  238. {13, &ISelfController::SetFocusHandlingMode, "SetFocusHandlingMode"},
  239. {14, &ISelfController::SetRestartMessageEnabled, "SetRestartMessageEnabled"},
  240. {15, nullptr, "SetScreenShotAppletIdentityInfo"},
  241. {16, &ISelfController::SetOutOfFocusSuspendingEnabled, "SetOutOfFocusSuspendingEnabled"},
  242. {17, nullptr, "SetControllerFirmwareUpdateSection"},
  243. {18, nullptr, "SetRequiresCaptureButtonShortPressedMessage"},
  244. {19, &ISelfController::SetAlbumImageOrientation, "SetAlbumImageOrientation"},
  245. {20, nullptr, "SetDesirableKeyboardLayout"},
  246. {21, nullptr, "GetScreenShotProgramId"},
  247. {40, &ISelfController::CreateManagedDisplayLayer, "CreateManagedDisplayLayer"},
  248. {41, nullptr, "IsSystemBufferSharingEnabled"},
  249. {42, nullptr, "GetSystemSharedLayerHandle"},
  250. {43, nullptr, "GetSystemSharedBufferHandle"},
  251. {44, &ISelfController::CreateManagedDisplaySeparableLayer, "CreateManagedDisplaySeparableLayer"},
  252. {45, nullptr, "SetManagedDisplayLayerSeparationMode"},
  253. {46, nullptr, "SetRecordingLayerCompositionEnabled"},
  254. {50, &ISelfController::SetHandlesRequestToDisplay, "SetHandlesRequestToDisplay"},
  255. {51, nullptr, "ApproveToDisplay"},
  256. {60, nullptr, "OverrideAutoSleepTimeAndDimmingTime"},
  257. {61, nullptr, "SetMediaPlaybackState"},
  258. {62, &ISelfController::SetIdleTimeDetectionExtension, "SetIdleTimeDetectionExtension"},
  259. {63, &ISelfController::GetIdleTimeDetectionExtension, "GetIdleTimeDetectionExtension"},
  260. {64, nullptr, "SetInputDetectionSourceSet"},
  261. {65, &ISelfController::ReportUserIsActive, "ReportUserIsActive"},
  262. {66, nullptr, "GetCurrentIlluminance"},
  263. {67, nullptr, "IsIlluminanceAvailable"},
  264. {68, &ISelfController::SetAutoSleepDisabled, "SetAutoSleepDisabled"},
  265. {69, &ISelfController::IsAutoSleepDisabled, "IsAutoSleepDisabled"},
  266. {70, nullptr, "ReportMultimediaError"},
  267. {71, nullptr, "GetCurrentIlluminanceEx"},
  268. {72, nullptr, "SetInputDetectionPolicy"},
  269. {80, nullptr, "SetWirelessPriorityMode"},
  270. {90, &ISelfController::GetAccumulatedSuspendedTickValue, "GetAccumulatedSuspendedTickValue"},
  271. {91, &ISelfController::GetAccumulatedSuspendedTickChangedEvent, "GetAccumulatedSuspendedTickChangedEvent"},
  272. {100, &ISelfController::SetAlbumImageTakenNotificationEnabled, "SetAlbumImageTakenNotificationEnabled"},
  273. {110, nullptr, "SetApplicationAlbumUserData"},
  274. {120, &ISelfController::SaveCurrentScreenshot, "SaveCurrentScreenshot"},
  275. {130, &ISelfController::SetRecordVolumeMuted, "SetRecordVolumeMuted"},
  276. {1000, nullptr, "GetDebugStorageChannel"},
  277. };
  278. // clang-format on
  279. RegisterHandlers(functions);
  280. launchable_event = service_context.CreateEvent("ISelfController:LaunchableEvent");
  281. // This event is created by AM on the first time GetAccumulatedSuspendedTickChangedEvent() is
  282. // called. Yuzu can just create it unconditionally, since it doesn't need to support multiple
  283. // ISelfControllers. The event is signaled on creation, and on transition from suspended -> not
  284. // suspended if the event has previously been created by a call to
  285. // GetAccumulatedSuspendedTickChangedEvent.
  286. accumulated_suspended_tick_changed_event =
  287. service_context.CreateEvent("ISelfController:AccumulatedSuspendedTickChangedEvent");
  288. accumulated_suspended_tick_changed_event->Signal();
  289. }
  290. ISelfController::~ISelfController() {
  291. service_context.CloseEvent(launchable_event);
  292. service_context.CloseEvent(accumulated_suspended_tick_changed_event);
  293. }
  294. void ISelfController::Exit(HLERequestContext& ctx) {
  295. LOG_DEBUG(Service_AM, "called");
  296. IPC::ResponseBuilder rb{ctx, 2};
  297. rb.Push(ResultSuccess);
  298. system.Exit();
  299. }
  300. void ISelfController::LockExit(HLERequestContext& ctx) {
  301. LOG_DEBUG(Service_AM, "called");
  302. system.SetExitLocked(true);
  303. IPC::ResponseBuilder rb{ctx, 2};
  304. rb.Push(ResultSuccess);
  305. }
  306. void ISelfController::UnlockExit(HLERequestContext& ctx) {
  307. LOG_DEBUG(Service_AM, "called");
  308. system.SetExitLocked(false);
  309. IPC::ResponseBuilder rb{ctx, 2};
  310. rb.Push(ResultSuccess);
  311. if (system.GetExitRequested()) {
  312. system.Exit();
  313. }
  314. }
  315. void ISelfController::EnterFatalSection(HLERequestContext& ctx) {
  316. ++num_fatal_sections_entered;
  317. LOG_DEBUG(Service_AM, "called. Num fatal sections entered: {}", num_fatal_sections_entered);
  318. IPC::ResponseBuilder rb{ctx, 2};
  319. rb.Push(ResultSuccess);
  320. }
  321. void ISelfController::LeaveFatalSection(HLERequestContext& ctx) {
  322. LOG_DEBUG(Service_AM, "called.");
  323. // Entry and exit of fatal sections must be balanced.
  324. if (num_fatal_sections_entered == 0) {
  325. IPC::ResponseBuilder rb{ctx, 2};
  326. rb.Push(Result{ErrorModule::AM, 512});
  327. return;
  328. }
  329. --num_fatal_sections_entered;
  330. IPC::ResponseBuilder rb{ctx, 2};
  331. rb.Push(ResultSuccess);
  332. }
  333. void ISelfController::GetLibraryAppletLaunchableEvent(HLERequestContext& ctx) {
  334. LOG_WARNING(Service_AM, "(STUBBED) called");
  335. launchable_event->Signal();
  336. IPC::ResponseBuilder rb{ctx, 2, 1};
  337. rb.Push(ResultSuccess);
  338. rb.PushCopyObjects(launchable_event->GetReadableEvent());
  339. }
  340. void ISelfController::SetScreenShotPermission(HLERequestContext& ctx) {
  341. IPC::RequestParser rp{ctx};
  342. const auto permission = rp.PopEnum<ScreenshotPermission>();
  343. LOG_DEBUG(Service_AM, "called, permission={}", permission);
  344. screenshot_permission = permission;
  345. IPC::ResponseBuilder rb{ctx, 2};
  346. rb.Push(ResultSuccess);
  347. }
  348. void ISelfController::SetOperationModeChangedNotification(HLERequestContext& ctx) {
  349. IPC::RequestParser rp{ctx};
  350. bool flag = rp.Pop<bool>();
  351. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  352. IPC::ResponseBuilder rb{ctx, 2};
  353. rb.Push(ResultSuccess);
  354. }
  355. void ISelfController::SetPerformanceModeChangedNotification(HLERequestContext& ctx) {
  356. IPC::RequestParser rp{ctx};
  357. bool flag = rp.Pop<bool>();
  358. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  359. IPC::ResponseBuilder rb{ctx, 2};
  360. rb.Push(ResultSuccess);
  361. }
  362. void ISelfController::SetFocusHandlingMode(HLERequestContext& ctx) {
  363. // Takes 3 input u8s with each field located immediately after the previous
  364. // u8, these are bool flags. No output.
  365. IPC::RequestParser rp{ctx};
  366. struct FocusHandlingModeParams {
  367. u8 unknown0;
  368. u8 unknown1;
  369. u8 unknown2;
  370. };
  371. const auto flags = rp.PopRaw<FocusHandlingModeParams>();
  372. LOG_WARNING(Service_AM, "(STUBBED) called. unknown0={}, unknown1={}, unknown2={}",
  373. flags.unknown0, flags.unknown1, flags.unknown2);
  374. IPC::ResponseBuilder rb{ctx, 2};
  375. rb.Push(ResultSuccess);
  376. }
  377. void ISelfController::SetRestartMessageEnabled(HLERequestContext& ctx) {
  378. LOG_WARNING(Service_AM, "(STUBBED) called");
  379. IPC::ResponseBuilder rb{ctx, 2};
  380. rb.Push(ResultSuccess);
  381. }
  382. void ISelfController::SetOutOfFocusSuspendingEnabled(HLERequestContext& ctx) {
  383. // Takes 3 input u8s with each field located immediately after the previous
  384. // u8, these are bool flags. No output.
  385. IPC::RequestParser rp{ctx};
  386. bool enabled = rp.Pop<bool>();
  387. LOG_WARNING(Service_AM, "(STUBBED) called enabled={}", enabled);
  388. IPC::ResponseBuilder rb{ctx, 2};
  389. rb.Push(ResultSuccess);
  390. }
  391. void ISelfController::SetAlbumImageOrientation(HLERequestContext& ctx) {
  392. LOG_WARNING(Service_AM, "(STUBBED) called");
  393. IPC::ResponseBuilder rb{ctx, 2};
  394. rb.Push(ResultSuccess);
  395. }
  396. void ISelfController::CreateManagedDisplayLayer(HLERequestContext& ctx) {
  397. LOG_WARNING(Service_AM, "(STUBBED) called");
  398. // TODO(Subv): Find out how AM determines the display to use, for now just
  399. // create the layer in the Default display.
  400. const auto display_id = nvnflinger.OpenDisplay("Default");
  401. const auto layer_id = nvnflinger.CreateLayer(*display_id);
  402. IPC::ResponseBuilder rb{ctx, 4};
  403. rb.Push(ResultSuccess);
  404. rb.Push(*layer_id);
  405. }
  406. void ISelfController::CreateManagedDisplaySeparableLayer(HLERequestContext& ctx) {
  407. LOG_WARNING(Service_AM, "(STUBBED) called");
  408. // TODO(Subv): Find out how AM determines the display to use, for now just
  409. // create the layer in the Default display.
  410. // This calls nn::vi::CreateRecordingLayer() which creates another layer.
  411. // Currently we do not support more than 1 layer per display, output 1 layer id for now.
  412. // Outputting 1 layer id instead of the expected 2 has not been observed to cause any adverse
  413. // side effects.
  414. // TODO: Support multiple layers
  415. const auto display_id = nvnflinger.OpenDisplay("Default");
  416. const auto layer_id = nvnflinger.CreateLayer(*display_id);
  417. IPC::ResponseBuilder rb{ctx, 4};
  418. rb.Push(ResultSuccess);
  419. rb.Push(*layer_id);
  420. }
  421. void ISelfController::SetHandlesRequestToDisplay(HLERequestContext& ctx) {
  422. LOG_WARNING(Service_AM, "(STUBBED) called");
  423. IPC::ResponseBuilder rb{ctx, 2};
  424. rb.Push(ResultSuccess);
  425. }
  426. void ISelfController::SetIdleTimeDetectionExtension(HLERequestContext& ctx) {
  427. IPC::RequestParser rp{ctx};
  428. idle_time_detection_extension = rp.Pop<u32>();
  429. LOG_DEBUG(Service_AM, "(STUBBED) called idle_time_detection_extension={}",
  430. idle_time_detection_extension);
  431. IPC::ResponseBuilder rb{ctx, 2};
  432. rb.Push(ResultSuccess);
  433. }
  434. void ISelfController::GetIdleTimeDetectionExtension(HLERequestContext& ctx) {
  435. LOG_WARNING(Service_AM, "(STUBBED) called");
  436. IPC::ResponseBuilder rb{ctx, 3};
  437. rb.Push(ResultSuccess);
  438. rb.Push<u32>(idle_time_detection_extension);
  439. }
  440. void ISelfController::ReportUserIsActive(HLERequestContext& ctx) {
  441. LOG_WARNING(Service_AM, "(STUBBED) called");
  442. IPC::ResponseBuilder rb{ctx, 2};
  443. rb.Push(ResultSuccess);
  444. }
  445. void ISelfController::SetAutoSleepDisabled(HLERequestContext& ctx) {
  446. IPC::RequestParser rp{ctx};
  447. is_auto_sleep_disabled = rp.Pop<bool>();
  448. // On the system itself, if the previous state of is_auto_sleep_disabled
  449. // differed from the current value passed in, it'd signify the internal
  450. // window manager to update (and also increment some statistics like update counts)
  451. //
  452. // It'd also indicate this change to an idle handling context.
  453. //
  454. // However, given we're emulating this behavior, most of this can be ignored
  455. // and it's sufficient to simply set the member variable for querying via
  456. // IsAutoSleepDisabled().
  457. LOG_DEBUG(Service_AM, "called. is_auto_sleep_disabled={}", is_auto_sleep_disabled);
  458. IPC::ResponseBuilder rb{ctx, 2};
  459. rb.Push(ResultSuccess);
  460. }
  461. void ISelfController::IsAutoSleepDisabled(HLERequestContext& ctx) {
  462. LOG_DEBUG(Service_AM, "called.");
  463. IPC::ResponseBuilder rb{ctx, 3};
  464. rb.Push(ResultSuccess);
  465. rb.Push(is_auto_sleep_disabled);
  466. }
  467. void ISelfController::GetAccumulatedSuspendedTickValue(HLERequestContext& ctx) {
  468. LOG_DEBUG(Service_AM, "called.");
  469. // This command returns the total number of system ticks since ISelfController creation
  470. // where the game was suspended. Since Yuzu doesn't implement game suspension, this command
  471. // can just always return 0 ticks.
  472. IPC::ResponseBuilder rb{ctx, 4};
  473. rb.Push(ResultSuccess);
  474. rb.Push<u64>(0);
  475. }
  476. void ISelfController::GetAccumulatedSuspendedTickChangedEvent(HLERequestContext& ctx) {
  477. LOG_DEBUG(Service_AM, "called.");
  478. IPC::ResponseBuilder rb{ctx, 2, 1};
  479. rb.Push(ResultSuccess);
  480. rb.PushCopyObjects(accumulated_suspended_tick_changed_event->GetReadableEvent());
  481. }
  482. void ISelfController::SetAlbumImageTakenNotificationEnabled(HLERequestContext& ctx) {
  483. IPC::RequestParser rp{ctx};
  484. // This service call sets an internal flag whether a notification is shown when an image is
  485. // captured. Currently we do not support capturing images via the capture button, so this can be
  486. // stubbed for now.
  487. const bool album_image_taken_notification_enabled = rp.Pop<bool>();
  488. LOG_WARNING(Service_AM, "(STUBBED) called. album_image_taken_notification_enabled={}",
  489. album_image_taken_notification_enabled);
  490. IPC::ResponseBuilder rb{ctx, 2};
  491. rb.Push(ResultSuccess);
  492. }
  493. void ISelfController::SaveCurrentScreenshot(HLERequestContext& ctx) {
  494. IPC::RequestParser rp{ctx};
  495. const auto album_report_option = rp.PopEnum<Capture::AlbumReportOption>();
  496. LOG_WARNING(Service_AM, "(STUBBED) called. album_report_option={}", album_report_option);
  497. IPC::ResponseBuilder rb{ctx, 2};
  498. rb.Push(ResultSuccess);
  499. }
  500. void ISelfController::SetRecordVolumeMuted(HLERequestContext& ctx) {
  501. IPC::RequestParser rp{ctx};
  502. const auto is_record_volume_muted = rp.Pop<bool>();
  503. LOG_WARNING(Service_AM, "(STUBBED) called. is_record_volume_muted={}", is_record_volume_muted);
  504. IPC::ResponseBuilder rb{ctx, 2};
  505. rb.Push(ResultSuccess);
  506. }
  507. AppletMessageQueue::AppletMessageQueue(Core::System& system)
  508. : service_context{system, "AppletMessageQueue"} {
  509. on_new_message = service_context.CreateEvent("AMMessageQueue:OnMessageReceived");
  510. on_operation_mode_changed = service_context.CreateEvent("AMMessageQueue:OperationModeChanged");
  511. }
  512. AppletMessageQueue::~AppletMessageQueue() {
  513. service_context.CloseEvent(on_new_message);
  514. service_context.CloseEvent(on_operation_mode_changed);
  515. }
  516. Kernel::KReadableEvent& AppletMessageQueue::GetMessageReceiveEvent() {
  517. return on_new_message->GetReadableEvent();
  518. }
  519. Kernel::KReadableEvent& AppletMessageQueue::GetOperationModeChangedEvent() {
  520. return on_operation_mode_changed->GetReadableEvent();
  521. }
  522. void AppletMessageQueue::PushMessage(AppletMessage msg) {
  523. messages.push(msg);
  524. on_new_message->Signal();
  525. }
  526. AppletMessageQueue::AppletMessage AppletMessageQueue::PopMessage() {
  527. if (messages.empty()) {
  528. on_new_message->Clear();
  529. return AppletMessage::None;
  530. }
  531. auto msg = messages.front();
  532. messages.pop();
  533. if (messages.empty()) {
  534. on_new_message->Clear();
  535. }
  536. return msg;
  537. }
  538. std::size_t AppletMessageQueue::GetMessageCount() const {
  539. return messages.size();
  540. }
  541. void AppletMessageQueue::RequestExit() {
  542. PushMessage(AppletMessage::Exit);
  543. }
  544. void AppletMessageQueue::RequestResume() {
  545. PushMessage(AppletMessage::Resume);
  546. }
  547. void AppletMessageQueue::FocusStateChanged() {
  548. PushMessage(AppletMessage::FocusStateChanged);
  549. }
  550. void AppletMessageQueue::OperationModeChanged() {
  551. PushMessage(AppletMessage::OperationModeChanged);
  552. PushMessage(AppletMessage::PerformanceModeChanged);
  553. on_operation_mode_changed->Signal();
  554. }
  555. ICommonStateGetter::ICommonStateGetter(Core::System& system_,
  556. std::shared_ptr<AppletMessageQueue> msg_queue_)
  557. : ServiceFramework{system_, "ICommonStateGetter"}, msg_queue{std::move(msg_queue_)} {
  558. // clang-format off
  559. static const FunctionInfo functions[] = {
  560. {0, &ICommonStateGetter::GetEventHandle, "GetEventHandle"},
  561. {1, &ICommonStateGetter::ReceiveMessage, "ReceiveMessage"},
  562. {2, nullptr, "GetThisAppletKind"},
  563. {3, nullptr, "AllowToEnterSleep"},
  564. {4, nullptr, "DisallowToEnterSleep"},
  565. {5, &ICommonStateGetter::GetOperationMode, "GetOperationMode"},
  566. {6, &ICommonStateGetter::GetPerformanceMode, "GetPerformanceMode"},
  567. {7, nullptr, "GetCradleStatus"},
  568. {8, &ICommonStateGetter::GetBootMode, "GetBootMode"},
  569. {9, &ICommonStateGetter::GetCurrentFocusState, "GetCurrentFocusState"},
  570. {10, nullptr, "RequestToAcquireSleepLock"},
  571. {11, nullptr, "ReleaseSleepLock"},
  572. {12, nullptr, "ReleaseSleepLockTransiently"},
  573. {13, nullptr, "GetAcquiredSleepLockEvent"},
  574. {14, nullptr, "GetWakeupCount"},
  575. {20, nullptr, "PushToGeneralChannel"},
  576. {30, nullptr, "GetHomeButtonReaderLockAccessor"},
  577. {31, nullptr, "GetReaderLockAccessorEx"},
  578. {32, nullptr, "GetWriterLockAccessorEx"},
  579. {40, nullptr, "GetCradleFwVersion"},
  580. {50, &ICommonStateGetter::IsVrModeEnabled, "IsVrModeEnabled"},
  581. {51, &ICommonStateGetter::SetVrModeEnabled, "SetVrModeEnabled"},
  582. {52, &ICommonStateGetter::SetLcdBacklighOffEnabled, "SetLcdBacklighOffEnabled"},
  583. {53, &ICommonStateGetter::BeginVrModeEx, "BeginVrModeEx"},
  584. {54, &ICommonStateGetter::EndVrModeEx, "EndVrModeEx"},
  585. {55, nullptr, "IsInControllerFirmwareUpdateSection"},
  586. {59, nullptr, "SetVrPositionForDebug"},
  587. {60, &ICommonStateGetter::GetDefaultDisplayResolution, "GetDefaultDisplayResolution"},
  588. {61, &ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent, "GetDefaultDisplayResolutionChangeEvent"},
  589. {62, nullptr, "GetHdcpAuthenticationState"},
  590. {63, nullptr, "GetHdcpAuthenticationStateChangeEvent"},
  591. {64, nullptr, "SetTvPowerStateMatchingMode"},
  592. {65, nullptr, "GetApplicationIdByContentActionName"},
  593. {66, &ICommonStateGetter::SetCpuBoostMode, "SetCpuBoostMode"},
  594. {67, nullptr, "CancelCpuBoostMode"},
  595. {68, nullptr, "GetBuiltInDisplayType"},
  596. {80, &ICommonStateGetter::PerformSystemButtonPressingIfInFocus, "PerformSystemButtonPressingIfInFocus"},
  597. {90, nullptr, "SetPerformanceConfigurationChangedNotification"},
  598. {91, nullptr, "GetCurrentPerformanceConfiguration"},
  599. {100, nullptr, "SetHandlingHomeButtonShortPressedEnabled"},
  600. {110, nullptr, "OpenMyGpuErrorHandler"},
  601. {120, nullptr, "GetAppletLaunchedHistory"},
  602. {200, nullptr, "GetOperationModeSystemInfo"},
  603. {300, &ICommonStateGetter::GetSettingsPlatformRegion, "GetSettingsPlatformRegion"},
  604. {400, nullptr, "ActivateMigrationService"},
  605. {401, nullptr, "DeactivateMigrationService"},
  606. {500, nullptr, "DisableSleepTillShutdown"},
  607. {501, nullptr, "SuppressDisablingSleepTemporarily"},
  608. {502, nullptr, "IsSleepEnabled"},
  609. {503, nullptr, "IsDisablingSleepSuppressed"},
  610. {900, &ICommonStateGetter::SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled, "SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled"},
  611. };
  612. // clang-format on
  613. RegisterHandlers(functions);
  614. // Configure applets to be in foreground state
  615. msg_queue->PushMessage(AppletMessageQueue::AppletMessage::FocusStateChanged);
  616. msg_queue->PushMessage(AppletMessageQueue::AppletMessage::ChangeIntoForeground);
  617. }
  618. ICommonStateGetter::~ICommonStateGetter() = default;
  619. void ICommonStateGetter::GetBootMode(HLERequestContext& ctx) {
  620. LOG_DEBUG(Service_AM, "called");
  621. IPC::ResponseBuilder rb{ctx, 3};
  622. rb.Push(ResultSuccess);
  623. rb.Push<u8>(static_cast<u8>(Service::PM::SystemBootMode::Normal)); // Normal boot mode
  624. }
  625. void ICommonStateGetter::GetEventHandle(HLERequestContext& ctx) {
  626. LOG_DEBUG(Service_AM, "called");
  627. IPC::ResponseBuilder rb{ctx, 2, 1};
  628. rb.Push(ResultSuccess);
  629. rb.PushCopyObjects(msg_queue->GetMessageReceiveEvent());
  630. }
  631. void ICommonStateGetter::ReceiveMessage(HLERequestContext& ctx) {
  632. LOG_DEBUG(Service_AM, "called");
  633. const auto message = msg_queue->PopMessage();
  634. IPC::ResponseBuilder rb{ctx, 3};
  635. if (message == AppletMessageQueue::AppletMessage::None) {
  636. LOG_ERROR(Service_AM, "Message queue is empty");
  637. rb.Push(AM::ResultNoMessages);
  638. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  639. return;
  640. }
  641. rb.Push(ResultSuccess);
  642. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  643. }
  644. void ICommonStateGetter::GetCurrentFocusState(HLERequestContext& ctx) {
  645. LOG_DEBUG(Service_AM, "(STUBBED) called");
  646. IPC::ResponseBuilder rb{ctx, 3};
  647. rb.Push(ResultSuccess);
  648. rb.Push(static_cast<u8>(FocusState::InFocus));
  649. }
  650. void ICommonStateGetter::IsVrModeEnabled(HLERequestContext& ctx) {
  651. LOG_DEBUG(Service_AM, "called");
  652. IPC::ResponseBuilder rb{ctx, 3};
  653. rb.Push(ResultSuccess);
  654. rb.Push(vr_mode_state);
  655. }
  656. void ICommonStateGetter::SetVrModeEnabled(HLERequestContext& ctx) {
  657. IPC::RequestParser rp{ctx};
  658. vr_mode_state = rp.Pop<bool>();
  659. LOG_WARNING(Service_AM, "VR Mode is {}", vr_mode_state ? "on" : "off");
  660. IPC::ResponseBuilder rb{ctx, 2};
  661. rb.Push(ResultSuccess);
  662. }
  663. void ICommonStateGetter::SetLcdBacklighOffEnabled(HLERequestContext& ctx) {
  664. IPC::RequestParser rp{ctx};
  665. const auto is_lcd_backlight_off_enabled = rp.Pop<bool>();
  666. LOG_WARNING(Service_AM, "(STUBBED) called. is_lcd_backlight_off_enabled={}",
  667. is_lcd_backlight_off_enabled);
  668. IPC::ResponseBuilder rb{ctx, 2};
  669. rb.Push(ResultSuccess);
  670. }
  671. void ICommonStateGetter::BeginVrModeEx(HLERequestContext& ctx) {
  672. LOG_WARNING(Service_AM, "(STUBBED) called");
  673. IPC::ResponseBuilder rb{ctx, 2};
  674. rb.Push(ResultSuccess);
  675. }
  676. void ICommonStateGetter::EndVrModeEx(HLERequestContext& ctx) {
  677. LOG_WARNING(Service_AM, "(STUBBED) called");
  678. IPC::ResponseBuilder rb{ctx, 2};
  679. rb.Push(ResultSuccess);
  680. }
  681. void ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent(HLERequestContext& ctx) {
  682. LOG_DEBUG(Service_AM, "called");
  683. IPC::ResponseBuilder rb{ctx, 2, 1};
  684. rb.Push(ResultSuccess);
  685. rb.PushCopyObjects(msg_queue->GetOperationModeChangedEvent());
  686. }
  687. void ICommonStateGetter::GetDefaultDisplayResolution(HLERequestContext& ctx) {
  688. LOG_DEBUG(Service_AM, "called");
  689. IPC::ResponseBuilder rb{ctx, 4};
  690. rb.Push(ResultSuccess);
  691. if (Settings::IsDockedMode()) {
  692. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedWidth));
  693. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedHeight));
  694. } else {
  695. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedWidth));
  696. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedHeight));
  697. }
  698. }
  699. void ICommonStateGetter::SetCpuBoostMode(HLERequestContext& ctx) {
  700. LOG_DEBUG(Service_AM, "called, forwarding to APM:SYS");
  701. const auto& sm = system.ServiceManager();
  702. const auto apm_sys = sm.GetService<APM::APM_Sys>("apm:sys");
  703. ASSERT(apm_sys != nullptr);
  704. apm_sys->SetCpuBoostMode(ctx);
  705. }
  706. void ICommonStateGetter::PerformSystemButtonPressingIfInFocus(HLERequestContext& ctx) {
  707. IPC::RequestParser rp{ctx};
  708. const auto system_button{rp.PopEnum<SystemButtonType>()};
  709. LOG_WARNING(Service_AM, "(STUBBED) called, system_button={}", system_button);
  710. IPC::ResponseBuilder rb{ctx, 2};
  711. rb.Push(ResultSuccess);
  712. }
  713. void ICommonStateGetter::GetSettingsPlatformRegion(HLERequestContext& ctx) {
  714. LOG_WARNING(Service_AM, "(STUBBED) called");
  715. IPC::ResponseBuilder rb{ctx, 3};
  716. rb.Push(ResultSuccess);
  717. rb.PushEnum(SysPlatformRegion::Global);
  718. }
  719. void ICommonStateGetter::SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled(
  720. HLERequestContext& ctx) {
  721. LOG_WARNING(Service_AM, "(STUBBED) called");
  722. IPC::ResponseBuilder rb{ctx, 2};
  723. rb.Push(ResultSuccess);
  724. }
  725. IStorageImpl::~IStorageImpl() = default;
  726. class StorageDataImpl final : public IStorageImpl {
  727. public:
  728. explicit StorageDataImpl(std::vector<u8>&& buffer_) : buffer{std::move(buffer_)} {}
  729. std::vector<u8>& GetData() override {
  730. return buffer;
  731. }
  732. const std::vector<u8>& GetData() const override {
  733. return buffer;
  734. }
  735. std::size_t GetSize() const override {
  736. return buffer.size();
  737. }
  738. private:
  739. std::vector<u8> buffer;
  740. };
  741. IStorage::IStorage(Core::System& system_, std::vector<u8>&& buffer)
  742. : ServiceFramework{system_, "IStorage"}, impl{std::make_shared<StorageDataImpl>(
  743. std::move(buffer))} {
  744. Register();
  745. }
  746. void IStorage::Register() {
  747. // clang-format off
  748. static const FunctionInfo functions[] = {
  749. {0, &IStorage::Open, "Open"},
  750. {1, nullptr, "OpenTransferStorage"},
  751. };
  752. // clang-format on
  753. RegisterHandlers(functions);
  754. }
  755. IStorage::~IStorage() = default;
  756. void IStorage::Open(HLERequestContext& ctx) {
  757. LOG_DEBUG(Service_AM, "called");
  758. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  759. rb.Push(ResultSuccess);
  760. rb.PushIpcInterface<IStorageAccessor>(system, *this);
  761. }
  762. void ICommonStateGetter::GetOperationMode(HLERequestContext& ctx) {
  763. const bool use_docked_mode{Settings::IsDockedMode()};
  764. LOG_DEBUG(Service_AM, "called, use_docked_mode={}", use_docked_mode);
  765. IPC::ResponseBuilder rb{ctx, 3};
  766. rb.Push(ResultSuccess);
  767. rb.Push(static_cast<u8>(use_docked_mode ? OperationMode::Docked : OperationMode::Handheld));
  768. }
  769. void ICommonStateGetter::GetPerformanceMode(HLERequestContext& ctx) {
  770. LOG_DEBUG(Service_AM, "called");
  771. IPC::ResponseBuilder rb{ctx, 3};
  772. rb.Push(ResultSuccess);
  773. rb.PushEnum(system.GetAPMController().GetCurrentPerformanceMode());
  774. }
  775. class ILibraryAppletAccessor final : public ServiceFramework<ILibraryAppletAccessor> {
  776. public:
  777. explicit ILibraryAppletAccessor(Core::System& system_, std::shared_ptr<Applets::Applet> applet_)
  778. : ServiceFramework{system_, "ILibraryAppletAccessor"}, applet{std::move(applet_)} {
  779. // clang-format off
  780. static const FunctionInfo functions[] = {
  781. {0, &ILibraryAppletAccessor::GetAppletStateChangedEvent, "GetAppletStateChangedEvent"},
  782. {1, &ILibraryAppletAccessor::IsCompleted, "IsCompleted"},
  783. {10, &ILibraryAppletAccessor::Start, "Start"},
  784. {20, &ILibraryAppletAccessor::RequestExit, "RequestExit"},
  785. {25, nullptr, "Terminate"},
  786. {30, &ILibraryAppletAccessor::GetResult, "GetResult"},
  787. {50, nullptr, "SetOutOfFocusApplicationSuspendingEnabled"},
  788. {60, &ILibraryAppletAccessor::PresetLibraryAppletGpuTimeSliceZero, "PresetLibraryAppletGpuTimeSliceZero"},
  789. {100, &ILibraryAppletAccessor::PushInData, "PushInData"},
  790. {101, &ILibraryAppletAccessor::PopOutData, "PopOutData"},
  791. {102, nullptr, "PushExtraStorage"},
  792. {103, &ILibraryAppletAccessor::PushInteractiveInData, "PushInteractiveInData"},
  793. {104, &ILibraryAppletAccessor::PopInteractiveOutData, "PopInteractiveOutData"},
  794. {105, &ILibraryAppletAccessor::GetPopOutDataEvent, "GetPopOutDataEvent"},
  795. {106, &ILibraryAppletAccessor::GetPopInteractiveOutDataEvent, "GetPopInteractiveOutDataEvent"},
  796. {110, nullptr, "NeedsToExitProcess"},
  797. {120, nullptr, "GetLibraryAppletInfo"},
  798. {150, nullptr, "RequestForAppletToGetForeground"},
  799. {160, &ILibraryAppletAccessor::GetIndirectLayerConsumerHandle, "GetIndirectLayerConsumerHandle"},
  800. };
  801. // clang-format on
  802. RegisterHandlers(functions);
  803. }
  804. private:
  805. void GetAppletStateChangedEvent(HLERequestContext& ctx) {
  806. LOG_DEBUG(Service_AM, "called");
  807. IPC::ResponseBuilder rb{ctx, 2, 1};
  808. rb.Push(ResultSuccess);
  809. rb.PushCopyObjects(applet->GetBroker().GetStateChangedEvent());
  810. }
  811. void IsCompleted(HLERequestContext& ctx) {
  812. LOG_DEBUG(Service_AM, "called");
  813. IPC::ResponseBuilder rb{ctx, 3};
  814. rb.Push(ResultSuccess);
  815. rb.Push<u32>(applet->TransactionComplete());
  816. }
  817. void GetResult(HLERequestContext& ctx) {
  818. LOG_DEBUG(Service_AM, "called");
  819. IPC::ResponseBuilder rb{ctx, 2};
  820. rb.Push(applet->GetStatus());
  821. }
  822. void PresetLibraryAppletGpuTimeSliceZero(HLERequestContext& ctx) {
  823. LOG_WARNING(Service_AM, "(STUBBED) called");
  824. IPC::ResponseBuilder rb{ctx, 2};
  825. rb.Push(ResultSuccess);
  826. }
  827. void Start(HLERequestContext& ctx) {
  828. LOG_DEBUG(Service_AM, "called");
  829. ASSERT(applet != nullptr);
  830. applet->Initialize();
  831. applet->Execute();
  832. IPC::ResponseBuilder rb{ctx, 2};
  833. rb.Push(ResultSuccess);
  834. }
  835. void RequestExit(HLERequestContext& ctx) {
  836. LOG_DEBUG(Service_AM, "called");
  837. ASSERT(applet != nullptr);
  838. IPC::ResponseBuilder rb{ctx, 2};
  839. rb.Push(applet->RequestExit());
  840. }
  841. void PushInData(HLERequestContext& ctx) {
  842. LOG_DEBUG(Service_AM, "called");
  843. IPC::RequestParser rp{ctx};
  844. applet->GetBroker().PushNormalDataFromGame(rp.PopIpcInterface<IStorage>().lock());
  845. IPC::ResponseBuilder rb{ctx, 2};
  846. rb.Push(ResultSuccess);
  847. }
  848. void PopOutData(HLERequestContext& ctx) {
  849. LOG_DEBUG(Service_AM, "called");
  850. auto storage = applet->GetBroker().PopNormalDataToGame();
  851. if (storage == nullptr) {
  852. LOG_DEBUG(Service_AM,
  853. "storage is a nullptr. There is no data in the current normal channel");
  854. IPC::ResponseBuilder rb{ctx, 2};
  855. rb.Push(AM::ResultNoDataInChannel);
  856. return;
  857. }
  858. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  859. rb.Push(ResultSuccess);
  860. rb.PushIpcInterface<IStorage>(std::move(storage));
  861. }
  862. void PushInteractiveInData(HLERequestContext& ctx) {
  863. LOG_DEBUG(Service_AM, "called");
  864. IPC::RequestParser rp{ctx};
  865. applet->GetBroker().PushInteractiveDataFromGame(rp.PopIpcInterface<IStorage>().lock());
  866. ASSERT(applet->IsInitialized());
  867. applet->ExecuteInteractive();
  868. applet->Execute();
  869. IPC::ResponseBuilder rb{ctx, 2};
  870. rb.Push(ResultSuccess);
  871. }
  872. void PopInteractiveOutData(HLERequestContext& ctx) {
  873. LOG_DEBUG(Service_AM, "called");
  874. auto storage = applet->GetBroker().PopInteractiveDataToGame();
  875. if (storage == nullptr) {
  876. LOG_DEBUG(Service_AM,
  877. "storage is a nullptr. There is no data in the current interactive channel");
  878. IPC::ResponseBuilder rb{ctx, 2};
  879. rb.Push(AM::ResultNoDataInChannel);
  880. return;
  881. }
  882. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  883. rb.Push(ResultSuccess);
  884. rb.PushIpcInterface<IStorage>(std::move(storage));
  885. }
  886. void GetPopOutDataEvent(HLERequestContext& ctx) {
  887. LOG_DEBUG(Service_AM, "called");
  888. IPC::ResponseBuilder rb{ctx, 2, 1};
  889. rb.Push(ResultSuccess);
  890. rb.PushCopyObjects(applet->GetBroker().GetNormalDataEvent());
  891. }
  892. void GetPopInteractiveOutDataEvent(HLERequestContext& ctx) {
  893. LOG_DEBUG(Service_AM, "called");
  894. IPC::ResponseBuilder rb{ctx, 2, 1};
  895. rb.Push(ResultSuccess);
  896. rb.PushCopyObjects(applet->GetBroker().GetInteractiveDataEvent());
  897. }
  898. void GetIndirectLayerConsumerHandle(HLERequestContext& ctx) {
  899. LOG_WARNING(Service_AM, "(STUBBED) called");
  900. // We require a non-zero handle to be valid. Using 0xdeadbeef allows us to trace if this is
  901. // actually used anywhere
  902. constexpr u64 handle = 0xdeadbeef;
  903. IPC::ResponseBuilder rb{ctx, 4};
  904. rb.Push(ResultSuccess);
  905. rb.Push(handle);
  906. }
  907. std::shared_ptr<Applets::Applet> applet;
  908. };
  909. IStorageAccessor::IStorageAccessor(Core::System& system_, IStorage& backing_)
  910. : ServiceFramework{system_, "IStorageAccessor"}, backing{backing_} {
  911. // clang-format off
  912. static const FunctionInfo functions[] = {
  913. {0, &IStorageAccessor::GetSize, "GetSize"},
  914. {10, &IStorageAccessor::Write, "Write"},
  915. {11, &IStorageAccessor::Read, "Read"},
  916. };
  917. // clang-format on
  918. RegisterHandlers(functions);
  919. }
  920. IStorageAccessor::~IStorageAccessor() = default;
  921. void IStorageAccessor::GetSize(HLERequestContext& ctx) {
  922. LOG_DEBUG(Service_AM, "called");
  923. IPC::ResponseBuilder rb{ctx, 4};
  924. rb.Push(ResultSuccess);
  925. rb.Push(static_cast<u64>(backing.GetSize()));
  926. }
  927. void IStorageAccessor::Write(HLERequestContext& ctx) {
  928. IPC::RequestParser rp{ctx};
  929. const u64 offset{rp.Pop<u64>()};
  930. const auto data{ctx.ReadBuffer()};
  931. const std::size_t size{std::min<u64>(data.size(), backing.GetSize() - offset)};
  932. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  933. if (offset > backing.GetSize()) {
  934. LOG_ERROR(Service_AM,
  935. "offset is out of bounds, backing_buffer_sz={}, data_size={}, offset={}",
  936. backing.GetSize(), size, offset);
  937. IPC::ResponseBuilder rb{ctx, 2};
  938. rb.Push(AM::ResultInvalidOffset);
  939. return;
  940. }
  941. std::memcpy(backing.GetData().data() + offset, data.data(), size);
  942. IPC::ResponseBuilder rb{ctx, 2};
  943. rb.Push(ResultSuccess);
  944. }
  945. void IStorageAccessor::Read(HLERequestContext& ctx) {
  946. IPC::RequestParser rp{ctx};
  947. const u64 offset{rp.Pop<u64>()};
  948. const std::size_t size{std::min<u64>(ctx.GetWriteBufferSize(), backing.GetSize() - offset)};
  949. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  950. if (offset > backing.GetSize()) {
  951. LOG_ERROR(Service_AM, "offset is out of bounds, backing_buffer_sz={}, size={}, offset={}",
  952. backing.GetSize(), size, offset);
  953. IPC::ResponseBuilder rb{ctx, 2};
  954. rb.Push(AM::ResultInvalidOffset);
  955. return;
  956. }
  957. ctx.WriteBuffer(backing.GetData().data() + offset, size);
  958. IPC::ResponseBuilder rb{ctx, 2};
  959. rb.Push(ResultSuccess);
  960. }
  961. ILibraryAppletCreator::ILibraryAppletCreator(Core::System& system_)
  962. : ServiceFramework{system_, "ILibraryAppletCreator"} {
  963. static const FunctionInfo functions[] = {
  964. {0, &ILibraryAppletCreator::CreateLibraryApplet, "CreateLibraryApplet"},
  965. {1, nullptr, "TerminateAllLibraryApplets"},
  966. {2, nullptr, "AreAnyLibraryAppletsLeft"},
  967. {10, &ILibraryAppletCreator::CreateStorage, "CreateStorage"},
  968. {11, &ILibraryAppletCreator::CreateTransferMemoryStorage, "CreateTransferMemoryStorage"},
  969. {12, &ILibraryAppletCreator::CreateHandleStorage, "CreateHandleStorage"},
  970. };
  971. RegisterHandlers(functions);
  972. }
  973. ILibraryAppletCreator::~ILibraryAppletCreator() = default;
  974. void ILibraryAppletCreator::CreateLibraryApplet(HLERequestContext& ctx) {
  975. IPC::RequestParser rp{ctx};
  976. const auto applet_id = rp.PopRaw<Applets::AppletId>();
  977. const auto applet_mode = rp.PopRaw<Applets::LibraryAppletMode>();
  978. LOG_DEBUG(Service_AM, "called with applet_id={:08X}, applet_mode={:08X}", applet_id,
  979. applet_mode);
  980. const auto& applet_manager{system.GetAppletManager()};
  981. const auto applet = applet_manager.GetApplet(applet_id, applet_mode);
  982. if (applet == nullptr) {
  983. LOG_ERROR(Service_AM, "Applet doesn't exist! applet_id={}", applet_id);
  984. IPC::ResponseBuilder rb{ctx, 2};
  985. rb.Push(ResultUnknown);
  986. return;
  987. }
  988. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  989. rb.Push(ResultSuccess);
  990. rb.PushIpcInterface<ILibraryAppletAccessor>(system, applet);
  991. }
  992. void ILibraryAppletCreator::CreateStorage(HLERequestContext& ctx) {
  993. IPC::RequestParser rp{ctx};
  994. const s64 size{rp.Pop<s64>()};
  995. LOG_DEBUG(Service_AM, "called, size={}", size);
  996. if (size <= 0) {
  997. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  998. IPC::ResponseBuilder rb{ctx, 2};
  999. rb.Push(ResultUnknown);
  1000. return;
  1001. }
  1002. std::vector<u8> buffer(size);
  1003. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1004. rb.Push(ResultSuccess);
  1005. rb.PushIpcInterface<IStorage>(system, std::move(buffer));
  1006. }
  1007. void ILibraryAppletCreator::CreateTransferMemoryStorage(HLERequestContext& ctx) {
  1008. IPC::RequestParser rp{ctx};
  1009. struct Parameters {
  1010. u8 permissions;
  1011. s64 size;
  1012. };
  1013. const auto parameters{rp.PopRaw<Parameters>()};
  1014. const auto handle{ctx.GetCopyHandle(0)};
  1015. LOG_DEBUG(Service_AM, "called, permissions={}, size={}, handle={:08X}", parameters.permissions,
  1016. parameters.size, handle);
  1017. if (parameters.size <= 0) {
  1018. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  1019. IPC::ResponseBuilder rb{ctx, 2};
  1020. rb.Push(ResultUnknown);
  1021. return;
  1022. }
  1023. auto transfer_mem =
  1024. system.ApplicationProcess()->GetHandleTable().GetObject<Kernel::KTransferMemory>(handle);
  1025. if (transfer_mem.IsNull()) {
  1026. LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle);
  1027. IPC::ResponseBuilder rb{ctx, 2};
  1028. rb.Push(ResultUnknown);
  1029. return;
  1030. }
  1031. std::vector<u8> memory(transfer_mem->GetSize());
  1032. system.ApplicationMemory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(),
  1033. memory.size());
  1034. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1035. rb.Push(ResultSuccess);
  1036. rb.PushIpcInterface<IStorage>(system, std::move(memory));
  1037. }
  1038. void ILibraryAppletCreator::CreateHandleStorage(HLERequestContext& ctx) {
  1039. IPC::RequestParser rp{ctx};
  1040. const s64 size{rp.Pop<s64>()};
  1041. const auto handle{ctx.GetCopyHandle(0)};
  1042. LOG_DEBUG(Service_AM, "called, size={}, handle={:08X}", size, handle);
  1043. if (size <= 0) {
  1044. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  1045. IPC::ResponseBuilder rb{ctx, 2};
  1046. rb.Push(ResultUnknown);
  1047. return;
  1048. }
  1049. auto transfer_mem =
  1050. system.ApplicationProcess()->GetHandleTable().GetObject<Kernel::KTransferMemory>(handle);
  1051. if (transfer_mem.IsNull()) {
  1052. LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle);
  1053. IPC::ResponseBuilder rb{ctx, 2};
  1054. rb.Push(ResultUnknown);
  1055. return;
  1056. }
  1057. std::vector<u8> memory(transfer_mem->GetSize());
  1058. system.ApplicationMemory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(),
  1059. memory.size());
  1060. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1061. rb.Push(ResultSuccess);
  1062. rb.PushIpcInterface<IStorage>(system, std::move(memory));
  1063. }
  1064. ILibraryAppletSelfAccessor::ILibraryAppletSelfAccessor(Core::System& system_)
  1065. : ServiceFramework{system_, "ILibraryAppletSelfAccessor"} {
  1066. // clang-format off
  1067. static const FunctionInfo functions[] = {
  1068. {0, &ILibraryAppletSelfAccessor::PopInData, "PopInData"},
  1069. {1, &ILibraryAppletSelfAccessor::PushOutData, "PushOutData"},
  1070. {2, nullptr, "PopInteractiveInData"},
  1071. {3, nullptr, "PushInteractiveOutData"},
  1072. {5, nullptr, "GetPopInDataEvent"},
  1073. {6, nullptr, "GetPopInteractiveInDataEvent"},
  1074. {10, &ILibraryAppletSelfAccessor::ExitProcessAndReturn, "ExitProcessAndReturn"},
  1075. {11, &ILibraryAppletSelfAccessor::GetLibraryAppletInfo, "GetLibraryAppletInfo"},
  1076. {12, nullptr, "GetMainAppletIdentityInfo"},
  1077. {13, nullptr, "CanUseApplicationCore"},
  1078. {14, &ILibraryAppletSelfAccessor::GetCallerAppletIdentityInfo, "GetCallerAppletIdentityInfo"},
  1079. {15, nullptr, "GetMainAppletApplicationControlProperty"},
  1080. {16, nullptr, "GetMainAppletStorageId"},
  1081. {17, nullptr, "GetCallerAppletIdentityInfoStack"},
  1082. {18, nullptr, "GetNextReturnDestinationAppletIdentityInfo"},
  1083. {19, nullptr, "GetDesirableKeyboardLayout"},
  1084. {20, nullptr, "PopExtraStorage"},
  1085. {25, nullptr, "GetPopExtraStorageEvent"},
  1086. {30, nullptr, "UnpopInData"},
  1087. {31, nullptr, "UnpopExtraStorage"},
  1088. {40, nullptr, "GetIndirectLayerProducerHandle"},
  1089. {50, nullptr, "ReportVisibleError"},
  1090. {51, nullptr, "ReportVisibleErrorWithErrorContext"},
  1091. {60, nullptr, "GetMainAppletApplicationDesiredLanguage"},
  1092. {70, nullptr, "GetCurrentApplicationId"},
  1093. {80, nullptr, "RequestExitToSelf"},
  1094. {90, nullptr, "CreateApplicationAndPushAndRequestToLaunch"},
  1095. {100, nullptr, "CreateGameMovieTrimmer"},
  1096. {101, nullptr, "ReserveResourceForMovieOperation"},
  1097. {102, nullptr, "UnreserveResourceForMovieOperation"},
  1098. {110, nullptr, "GetMainAppletAvailableUsers"},
  1099. {120, nullptr, "GetLaunchStorageInfoForDebug"},
  1100. {130, nullptr, "GetGpuErrorDetectedSystemEvent"},
  1101. {140, nullptr, "SetApplicationMemoryReservation"},
  1102. {150, nullptr, "ShouldSetGpuTimeSliceManually"},
  1103. };
  1104. // clang-format on
  1105. RegisterHandlers(functions);
  1106. PushInShowMiiEditData();
  1107. }
  1108. ILibraryAppletSelfAccessor::~ILibraryAppletSelfAccessor() = default;
  1109. void ILibraryAppletSelfAccessor::PopInData(HLERequestContext& ctx) {
  1110. LOG_INFO(Service_AM, "called");
  1111. if (queue_data.empty()) {
  1112. IPC::ResponseBuilder rb{ctx, 2};
  1113. rb.Push(ResultNoDataInChannel);
  1114. return;
  1115. }
  1116. auto data = queue_data.front();
  1117. queue_data.pop_front();
  1118. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1119. rb.Push(ResultSuccess);
  1120. rb.PushIpcInterface<IStorage>(system, std::move(data));
  1121. }
  1122. void ILibraryAppletSelfAccessor::PushOutData(HLERequestContext& ctx) {
  1123. LOG_WARNING(Service_AM, "(STUBBED) called");
  1124. IPC::ResponseBuilder rb{ctx, 2};
  1125. rb.Push(ResultSuccess);
  1126. }
  1127. void ILibraryAppletSelfAccessor::ExitProcessAndReturn(HLERequestContext& ctx) {
  1128. LOG_WARNING(Service_AM, "(STUBBED) called");
  1129. system.Exit();
  1130. IPC::ResponseBuilder rb{ctx, 2};
  1131. rb.Push(ResultSuccess);
  1132. }
  1133. void ILibraryAppletSelfAccessor::GetLibraryAppletInfo(HLERequestContext& ctx) {
  1134. struct LibraryAppletInfo {
  1135. Applets::AppletId applet_id;
  1136. Applets::LibraryAppletMode library_applet_mode;
  1137. };
  1138. LOG_WARNING(Service_AM, "(STUBBED) called");
  1139. const LibraryAppletInfo applet_info{
  1140. .applet_id = Applets::AppletId::MiiEdit,
  1141. .library_applet_mode = Applets::LibraryAppletMode::AllForeground,
  1142. };
  1143. IPC::ResponseBuilder rb{ctx, 4};
  1144. rb.Push(ResultSuccess);
  1145. rb.PushRaw(applet_info);
  1146. }
  1147. void ILibraryAppletSelfAccessor::GetCallerAppletIdentityInfo(HLERequestContext& ctx) {
  1148. struct AppletIdentityInfo {
  1149. Applets::AppletId applet_id;
  1150. INSERT_PADDING_BYTES(0x4);
  1151. u64 application_id;
  1152. };
  1153. LOG_WARNING(Service_AM, "(STUBBED) called");
  1154. const AppletIdentityInfo applet_info{
  1155. .applet_id = Applets::AppletId::QLaunch,
  1156. .application_id = 0x0100000000001000ull,
  1157. };
  1158. IPC::ResponseBuilder rb{ctx, 6};
  1159. rb.Push(ResultSuccess);
  1160. rb.PushRaw(applet_info);
  1161. }
  1162. void ILibraryAppletSelfAccessor::PushInShowMiiEditData() {
  1163. struct MiiEditV3 {
  1164. Applets::MiiEditAppletInputCommon common;
  1165. Applets::MiiEditAppletInputV3 input;
  1166. };
  1167. static_assert(sizeof(MiiEditV3) == 0x100, "MiiEditV3 has incorrect size.");
  1168. MiiEditV3 mii_arguments{
  1169. .common =
  1170. {
  1171. .version = Applets::MiiEditAppletVersion::Version3,
  1172. .applet_mode = Applets::MiiEditAppletMode::ShowMiiEdit,
  1173. },
  1174. .input{},
  1175. };
  1176. std::vector<u8> argument_data(sizeof(mii_arguments));
  1177. std::memcpy(argument_data.data(), &mii_arguments, sizeof(mii_arguments));
  1178. queue_data.emplace_back(std::move(argument_data));
  1179. }
  1180. IAppletCommonFunctions::IAppletCommonFunctions(Core::System& system_)
  1181. : ServiceFramework{system_, "IAppletCommonFunctions"} {
  1182. // clang-format off
  1183. static const FunctionInfo functions[] = {
  1184. {0, nullptr, "SetTerminateResult"},
  1185. {10, nullptr, "ReadThemeStorage"},
  1186. {11, nullptr, "WriteThemeStorage"},
  1187. {20, nullptr, "PushToAppletBoundChannel"},
  1188. {21, nullptr, "TryPopFromAppletBoundChannel"},
  1189. {40, nullptr, "GetDisplayLogicalResolution"},
  1190. {42, nullptr, "SetDisplayMagnification"},
  1191. {50, nullptr, "SetHomeButtonDoubleClickEnabled"},
  1192. {51, nullptr, "GetHomeButtonDoubleClickEnabled"},
  1193. {52, nullptr, "IsHomeButtonShortPressedBlocked"},
  1194. {60, nullptr, "IsVrModeCurtainRequired"},
  1195. {61, nullptr, "IsSleepRequiredByHighTemperature"},
  1196. {62, nullptr, "IsSleepRequiredByLowBattery"},
  1197. {70, &IAppletCommonFunctions::SetCpuBoostRequestPriority, "SetCpuBoostRequestPriority"},
  1198. {80, nullptr, "SetHandlingCaptureButtonShortPressedMessageEnabledForApplet"},
  1199. {81, nullptr, "SetHandlingCaptureButtonLongPressedMessageEnabledForApplet"},
  1200. {90, nullptr, "OpenNamedChannelAsParent"},
  1201. {91, nullptr, "OpenNamedChannelAsChild"},
  1202. {100, nullptr, "SetApplicationCoreUsageMode"},
  1203. };
  1204. // clang-format on
  1205. RegisterHandlers(functions);
  1206. }
  1207. IAppletCommonFunctions::~IAppletCommonFunctions() = default;
  1208. void IAppletCommonFunctions::SetCpuBoostRequestPriority(HLERequestContext& ctx) {
  1209. LOG_WARNING(Service_AM, "(STUBBED) called");
  1210. IPC::ResponseBuilder rb{ctx, 2};
  1211. rb.Push(ResultSuccess);
  1212. }
  1213. IApplicationFunctions::IApplicationFunctions(Core::System& system_)
  1214. : ServiceFramework{system_, "IApplicationFunctions"}, service_context{system,
  1215. "IApplicationFunctions"} {
  1216. // clang-format off
  1217. static const FunctionInfo functions[] = {
  1218. {1, &IApplicationFunctions::PopLaunchParameter, "PopLaunchParameter"},
  1219. {10, nullptr, "CreateApplicationAndPushAndRequestToStart"},
  1220. {11, nullptr, "CreateApplicationAndPushAndRequestToStartForQuest"},
  1221. {12, nullptr, "CreateApplicationAndRequestToStart"},
  1222. {13, &IApplicationFunctions::CreateApplicationAndRequestToStartForQuest, "CreateApplicationAndRequestToStartForQuest"},
  1223. {14, nullptr, "CreateApplicationWithAttributeAndPushAndRequestToStartForQuest"},
  1224. {15, nullptr, "CreateApplicationWithAttributeAndRequestToStartForQuest"},
  1225. {20, &IApplicationFunctions::EnsureSaveData, "EnsureSaveData"},
  1226. {21, &IApplicationFunctions::GetDesiredLanguage, "GetDesiredLanguage"},
  1227. {22, &IApplicationFunctions::SetTerminateResult, "SetTerminateResult"},
  1228. {23, &IApplicationFunctions::GetDisplayVersion, "GetDisplayVersion"},
  1229. {24, nullptr, "GetLaunchStorageInfoForDebug"},
  1230. {25, &IApplicationFunctions::ExtendSaveData, "ExtendSaveData"},
  1231. {26, &IApplicationFunctions::GetSaveDataSize, "GetSaveDataSize"},
  1232. {27, &IApplicationFunctions::CreateCacheStorage, "CreateCacheStorage"},
  1233. {28, &IApplicationFunctions::GetSaveDataSizeMax, "GetSaveDataSizeMax"},
  1234. {29, nullptr, "GetCacheStorageMax"},
  1235. {30, &IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed, "BeginBlockingHomeButtonShortAndLongPressed"},
  1236. {31, &IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed, "EndBlockingHomeButtonShortAndLongPressed"},
  1237. {32, &IApplicationFunctions::BeginBlockingHomeButton, "BeginBlockingHomeButton"},
  1238. {33, &IApplicationFunctions::EndBlockingHomeButton, "EndBlockingHomeButton"},
  1239. {34, nullptr, "SelectApplicationLicense"},
  1240. {35, nullptr, "GetDeviceSaveDataSizeMax"},
  1241. {36, nullptr, "GetLimitedApplicationLicense"},
  1242. {37, nullptr, "GetLimitedApplicationLicenseUpgradableEvent"},
  1243. {40, &IApplicationFunctions::NotifyRunning, "NotifyRunning"},
  1244. {50, &IApplicationFunctions::GetPseudoDeviceId, "GetPseudoDeviceId"},
  1245. {60, nullptr, "SetMediaPlaybackStateForApplication"},
  1246. {65, &IApplicationFunctions::IsGamePlayRecordingSupported, "IsGamePlayRecordingSupported"},
  1247. {66, &IApplicationFunctions::InitializeGamePlayRecording, "InitializeGamePlayRecording"},
  1248. {67, &IApplicationFunctions::SetGamePlayRecordingState, "SetGamePlayRecordingState"},
  1249. {68, nullptr, "RequestFlushGamePlayingMovieForDebug"},
  1250. {70, nullptr, "RequestToShutdown"},
  1251. {71, nullptr, "RequestToReboot"},
  1252. {72, nullptr, "RequestToSleep"},
  1253. {80, nullptr, "ExitAndRequestToShowThanksMessage"},
  1254. {90, &IApplicationFunctions::EnableApplicationCrashReport, "EnableApplicationCrashReport"},
  1255. {100, &IApplicationFunctions::InitializeApplicationCopyrightFrameBuffer, "InitializeApplicationCopyrightFrameBuffer"},
  1256. {101, &IApplicationFunctions::SetApplicationCopyrightImage, "SetApplicationCopyrightImage"},
  1257. {102, &IApplicationFunctions::SetApplicationCopyrightVisibility, "SetApplicationCopyrightVisibility"},
  1258. {110, &IApplicationFunctions::QueryApplicationPlayStatistics, "QueryApplicationPlayStatistics"},
  1259. {111, &IApplicationFunctions::QueryApplicationPlayStatisticsByUid, "QueryApplicationPlayStatisticsByUid"},
  1260. {120, &IApplicationFunctions::ExecuteProgram, "ExecuteProgram"},
  1261. {121, &IApplicationFunctions::ClearUserChannel, "ClearUserChannel"},
  1262. {122, &IApplicationFunctions::UnpopToUserChannel, "UnpopToUserChannel"},
  1263. {123, &IApplicationFunctions::GetPreviousProgramIndex, "GetPreviousProgramIndex"},
  1264. {124, nullptr, "EnableApplicationAllThreadDumpOnCrash"},
  1265. {130, &IApplicationFunctions::GetGpuErrorDetectedSystemEvent, "GetGpuErrorDetectedSystemEvent"},
  1266. {131, nullptr, "SetDelayTimeToAbortOnGpuError"},
  1267. {140, &IApplicationFunctions::GetFriendInvitationStorageChannelEvent, "GetFriendInvitationStorageChannelEvent"},
  1268. {141, &IApplicationFunctions::TryPopFromFriendInvitationStorageChannel, "TryPopFromFriendInvitationStorageChannel"},
  1269. {150, &IApplicationFunctions::GetNotificationStorageChannelEvent, "GetNotificationStorageChannelEvent"},
  1270. {151, nullptr, "TryPopFromNotificationStorageChannel"},
  1271. {160, &IApplicationFunctions::GetHealthWarningDisappearedSystemEvent, "GetHealthWarningDisappearedSystemEvent"},
  1272. {170, nullptr, "SetHdcpAuthenticationActivated"},
  1273. {180, nullptr, "GetLaunchRequiredVersion"},
  1274. {181, nullptr, "UpgradeLaunchRequiredVersion"},
  1275. {190, nullptr, "SendServerMaintenanceOverlayNotification"},
  1276. {200, nullptr, "GetLastApplicationExitReason"},
  1277. {500, nullptr, "StartContinuousRecordingFlushForDebug"},
  1278. {1000, nullptr, "CreateMovieMaker"},
  1279. {1001, &IApplicationFunctions::PrepareForJit, "PrepareForJit"},
  1280. };
  1281. // clang-format on
  1282. RegisterHandlers(functions);
  1283. gpu_error_detected_event =
  1284. service_context.CreateEvent("IApplicationFunctions:GpuErrorDetectedSystemEvent");
  1285. friend_invitation_storage_channel_event =
  1286. service_context.CreateEvent("IApplicationFunctions:FriendInvitationStorageChannelEvent");
  1287. notification_storage_channel_event =
  1288. service_context.CreateEvent("IApplicationFunctions:NotificationStorageChannelEvent");
  1289. health_warning_disappeared_system_event =
  1290. service_context.CreateEvent("IApplicationFunctions:HealthWarningDisappearedSystemEvent");
  1291. }
  1292. IApplicationFunctions::~IApplicationFunctions() {
  1293. service_context.CloseEvent(gpu_error_detected_event);
  1294. service_context.CloseEvent(friend_invitation_storage_channel_event);
  1295. service_context.CloseEvent(notification_storage_channel_event);
  1296. service_context.CloseEvent(health_warning_disappeared_system_event);
  1297. }
  1298. void IApplicationFunctions::EnableApplicationCrashReport(HLERequestContext& ctx) {
  1299. LOG_WARNING(Service_AM, "(STUBBED) called");
  1300. IPC::ResponseBuilder rb{ctx, 2};
  1301. rb.Push(ResultSuccess);
  1302. }
  1303. void IApplicationFunctions::InitializeApplicationCopyrightFrameBuffer(HLERequestContext& ctx) {
  1304. LOG_WARNING(Service_AM, "(STUBBED) called");
  1305. IPC::ResponseBuilder rb{ctx, 2};
  1306. rb.Push(ResultSuccess);
  1307. }
  1308. void IApplicationFunctions::SetApplicationCopyrightImage(HLERequestContext& ctx) {
  1309. LOG_WARNING(Service_AM, "(STUBBED) called");
  1310. IPC::ResponseBuilder rb{ctx, 2};
  1311. rb.Push(ResultSuccess);
  1312. }
  1313. void IApplicationFunctions::SetApplicationCopyrightVisibility(HLERequestContext& ctx) {
  1314. IPC::RequestParser rp{ctx};
  1315. const auto is_visible = rp.Pop<bool>();
  1316. LOG_WARNING(Service_AM, "(STUBBED) called, is_visible={}", is_visible);
  1317. IPC::ResponseBuilder rb{ctx, 2};
  1318. rb.Push(ResultSuccess);
  1319. }
  1320. void IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed(HLERequestContext& ctx) {
  1321. LOG_WARNING(Service_AM, "(STUBBED) called");
  1322. IPC::ResponseBuilder rb{ctx, 2};
  1323. rb.Push(ResultSuccess);
  1324. }
  1325. void IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed(HLERequestContext& ctx) {
  1326. LOG_WARNING(Service_AM, "(STUBBED) called");
  1327. IPC::ResponseBuilder rb{ctx, 2};
  1328. rb.Push(ResultSuccess);
  1329. }
  1330. void IApplicationFunctions::BeginBlockingHomeButton(HLERequestContext& ctx) {
  1331. LOG_WARNING(Service_AM, "(STUBBED) called");
  1332. IPC::ResponseBuilder rb{ctx, 2};
  1333. rb.Push(ResultSuccess);
  1334. }
  1335. void IApplicationFunctions::EndBlockingHomeButton(HLERequestContext& ctx) {
  1336. LOG_WARNING(Service_AM, "(STUBBED) called");
  1337. IPC::ResponseBuilder rb{ctx, 2};
  1338. rb.Push(ResultSuccess);
  1339. }
  1340. void IApplicationFunctions::PopLaunchParameter(HLERequestContext& ctx) {
  1341. IPC::RequestParser rp{ctx};
  1342. const auto kind = rp.PopEnum<LaunchParameterKind>();
  1343. LOG_INFO(Service_AM, "called, kind={:08X}", kind);
  1344. if (kind == LaunchParameterKind::UserChannel) {
  1345. auto channel = system.GetUserChannel();
  1346. if (channel.empty()) {
  1347. LOG_ERROR(Service_AM, "Attempted to load launch parameter but none was found!");
  1348. IPC::ResponseBuilder rb{ctx, 2};
  1349. rb.Push(AM::ResultNoDataInChannel);
  1350. return;
  1351. }
  1352. auto data = channel.back();
  1353. channel.pop_back();
  1354. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1355. rb.Push(ResultSuccess);
  1356. rb.PushIpcInterface<IStorage>(system, std::move(data));
  1357. } else if (kind == LaunchParameterKind::AccountPreselectedUser &&
  1358. !launch_popped_account_preselect) {
  1359. // TODO: Verify this is hw-accurate
  1360. LaunchParameterAccountPreselectedUser params{};
  1361. params.magic = LAUNCH_PARAMETER_ACCOUNT_PRESELECTED_USER_MAGIC;
  1362. params.is_account_selected = 1;
  1363. Account::ProfileManager profile_manager{};
  1364. const auto uuid = profile_manager.GetUser(static_cast<s32>(Settings::values.current_user));
  1365. ASSERT(uuid.has_value() && uuid->IsValid());
  1366. params.current_user = *uuid;
  1367. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1368. rb.Push(ResultSuccess);
  1369. std::vector<u8> buffer(sizeof(LaunchParameterAccountPreselectedUser));
  1370. std::memcpy(buffer.data(), &params, buffer.size());
  1371. rb.PushIpcInterface<IStorage>(system, std::move(buffer));
  1372. launch_popped_account_preselect = true;
  1373. } else {
  1374. LOG_ERROR(Service_AM, "Unknown launch parameter kind.");
  1375. IPC::ResponseBuilder rb{ctx, 2};
  1376. rb.Push(AM::ResultNoDataInChannel);
  1377. }
  1378. }
  1379. void IApplicationFunctions::CreateApplicationAndRequestToStartForQuest(HLERequestContext& ctx) {
  1380. LOG_WARNING(Service_AM, "(STUBBED) called");
  1381. IPC::ResponseBuilder rb{ctx, 2};
  1382. rb.Push(ResultSuccess);
  1383. }
  1384. void IApplicationFunctions::EnsureSaveData(HLERequestContext& ctx) {
  1385. IPC::RequestParser rp{ctx};
  1386. u128 user_id = rp.PopRaw<u128>();
  1387. LOG_DEBUG(Service_AM, "called, uid={:016X}{:016X}", user_id[1], user_id[0]);
  1388. FileSys::SaveDataAttribute attribute{};
  1389. attribute.title_id = system.GetApplicationProcessProgramID();
  1390. attribute.user_id = user_id;
  1391. attribute.type = FileSys::SaveDataType::SaveData;
  1392. FileSys::VirtualDir save_data{};
  1393. const auto res = system.GetFileSystemController().CreateSaveData(
  1394. &save_data, FileSys::SaveDataSpaceId::NandUser, attribute);
  1395. IPC::ResponseBuilder rb{ctx, 4};
  1396. rb.Push(res);
  1397. rb.Push<u64>(0);
  1398. }
  1399. void IApplicationFunctions::SetTerminateResult(HLERequestContext& ctx) {
  1400. // Takes an input u32 Result, no output.
  1401. // For example, in some cases official apps use this with error 0x2A2 then
  1402. // uses svcBreak.
  1403. IPC::RequestParser rp{ctx};
  1404. u32 result = rp.Pop<u32>();
  1405. LOG_WARNING(Service_AM, "(STUBBED) called, result=0x{:08X}", result);
  1406. IPC::ResponseBuilder rb{ctx, 2};
  1407. rb.Push(ResultSuccess);
  1408. }
  1409. void IApplicationFunctions::GetDisplayVersion(HLERequestContext& ctx) {
  1410. LOG_DEBUG(Service_AM, "called");
  1411. std::array<u8, 0x10> version_string{};
  1412. const auto res = [this] {
  1413. const auto title_id = system.GetApplicationProcessProgramID();
  1414. const FileSys::PatchManager pm{title_id, system.GetFileSystemController(),
  1415. system.GetContentProvider()};
  1416. auto metadata = pm.GetControlMetadata();
  1417. if (metadata.first != nullptr) {
  1418. return metadata;
  1419. }
  1420. const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(title_id),
  1421. system.GetFileSystemController(),
  1422. system.GetContentProvider()};
  1423. return pm_update.GetControlMetadata();
  1424. }();
  1425. if (res.first != nullptr) {
  1426. const auto& version = res.first->GetVersionString();
  1427. std::copy(version.begin(), version.end(), version_string.begin());
  1428. } else {
  1429. static constexpr char default_version[]{"1.0.0"};
  1430. std::memcpy(version_string.data(), default_version, sizeof(default_version));
  1431. }
  1432. IPC::ResponseBuilder rb{ctx, 6};
  1433. rb.Push(ResultSuccess);
  1434. rb.PushRaw(version_string);
  1435. }
  1436. void IApplicationFunctions::GetDesiredLanguage(HLERequestContext& ctx) {
  1437. // TODO(bunnei): This should be configurable
  1438. LOG_DEBUG(Service_AM, "called");
  1439. // Get supported languages from NACP, if possible
  1440. // Default to 0 (all languages supported)
  1441. u32 supported_languages = 0;
  1442. const auto res = [this] {
  1443. const auto title_id = system.GetApplicationProcessProgramID();
  1444. const FileSys::PatchManager pm{title_id, system.GetFileSystemController(),
  1445. system.GetContentProvider()};
  1446. auto metadata = pm.GetControlMetadata();
  1447. if (metadata.first != nullptr) {
  1448. return metadata;
  1449. }
  1450. const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(title_id),
  1451. system.GetFileSystemController(),
  1452. system.GetContentProvider()};
  1453. return pm_update.GetControlMetadata();
  1454. }();
  1455. if (res.first != nullptr) {
  1456. supported_languages = res.first->GetSupportedLanguages();
  1457. }
  1458. // Call IApplicationManagerInterface implementation.
  1459. auto& service_manager = system.ServiceManager();
  1460. auto ns_am2 = service_manager.GetService<NS::NS>("ns:am2");
  1461. auto app_man = ns_am2->GetApplicationManagerInterface();
  1462. // Get desired application language
  1463. u8 desired_language{};
  1464. const auto res_lang =
  1465. app_man->GetApplicationDesiredLanguage(&desired_language, supported_languages);
  1466. if (res_lang != ResultSuccess) {
  1467. IPC::ResponseBuilder rb{ctx, 2};
  1468. rb.Push(res_lang);
  1469. return;
  1470. }
  1471. // Convert to settings language code.
  1472. u64 language_code{};
  1473. const auto res_code =
  1474. app_man->ConvertApplicationLanguageToLanguageCode(&language_code, desired_language);
  1475. if (res_code != ResultSuccess) {
  1476. IPC::ResponseBuilder rb{ctx, 2};
  1477. rb.Push(res_code);
  1478. return;
  1479. }
  1480. LOG_DEBUG(Service_AM, "got desired_language={:016X}", language_code);
  1481. IPC::ResponseBuilder rb{ctx, 4};
  1482. rb.Push(ResultSuccess);
  1483. rb.Push(language_code);
  1484. }
  1485. void IApplicationFunctions::IsGamePlayRecordingSupported(HLERequestContext& ctx) {
  1486. LOG_WARNING(Service_AM, "(STUBBED) called");
  1487. constexpr bool gameplay_recording_supported = false;
  1488. IPC::ResponseBuilder rb{ctx, 3};
  1489. rb.Push(ResultSuccess);
  1490. rb.Push(gameplay_recording_supported);
  1491. }
  1492. void IApplicationFunctions::InitializeGamePlayRecording(HLERequestContext& ctx) {
  1493. LOG_WARNING(Service_AM, "(STUBBED) called");
  1494. IPC::ResponseBuilder rb{ctx, 2};
  1495. rb.Push(ResultSuccess);
  1496. }
  1497. void IApplicationFunctions::SetGamePlayRecordingState(HLERequestContext& ctx) {
  1498. LOG_WARNING(Service_AM, "(STUBBED) called");
  1499. IPC::ResponseBuilder rb{ctx, 2};
  1500. rb.Push(ResultSuccess);
  1501. }
  1502. void IApplicationFunctions::NotifyRunning(HLERequestContext& ctx) {
  1503. LOG_WARNING(Service_AM, "(STUBBED) called");
  1504. IPC::ResponseBuilder rb{ctx, 3};
  1505. rb.Push(ResultSuccess);
  1506. rb.Push<u8>(0); // Unknown, seems to be ignored by official processes
  1507. }
  1508. void IApplicationFunctions::GetPseudoDeviceId(HLERequestContext& ctx) {
  1509. LOG_WARNING(Service_AM, "(STUBBED) called");
  1510. IPC::ResponseBuilder rb{ctx, 6};
  1511. rb.Push(ResultSuccess);
  1512. // Returns a 128-bit UUID
  1513. rb.Push<u64>(0);
  1514. rb.Push<u64>(0);
  1515. }
  1516. void IApplicationFunctions::ExtendSaveData(HLERequestContext& ctx) {
  1517. struct Parameters {
  1518. FileSys::SaveDataType type;
  1519. u128 user_id;
  1520. u64 new_normal_size;
  1521. u64 new_journal_size;
  1522. };
  1523. static_assert(sizeof(Parameters) == 40);
  1524. IPC::RequestParser rp{ctx};
  1525. const auto [type, user_id, new_normal_size, new_journal_size] = rp.PopRaw<Parameters>();
  1526. LOG_DEBUG(Service_AM,
  1527. "called with type={:02X}, user_id={:016X}{:016X}, new_normal={:016X}, "
  1528. "new_journal={:016X}",
  1529. static_cast<u8>(type), user_id[1], user_id[0], new_normal_size, new_journal_size);
  1530. system.GetFileSystemController().WriteSaveDataSize(
  1531. type, system.GetApplicationProcessProgramID(), user_id,
  1532. {new_normal_size, new_journal_size});
  1533. IPC::ResponseBuilder rb{ctx, 4};
  1534. rb.Push(ResultSuccess);
  1535. // The following value is used upon failure to help the system recover.
  1536. // Since we always succeed, this should be 0.
  1537. rb.Push<u64>(0);
  1538. }
  1539. void IApplicationFunctions::GetSaveDataSize(HLERequestContext& ctx) {
  1540. struct Parameters {
  1541. FileSys::SaveDataType type;
  1542. u128 user_id;
  1543. };
  1544. static_assert(sizeof(Parameters) == 24);
  1545. IPC::RequestParser rp{ctx};
  1546. const auto [type, user_id] = rp.PopRaw<Parameters>();
  1547. LOG_DEBUG(Service_AM, "called with type={:02X}, user_id={:016X}{:016X}", type, user_id[1],
  1548. user_id[0]);
  1549. const auto size = system.GetFileSystemController().ReadSaveDataSize(
  1550. type, system.GetApplicationProcessProgramID(), user_id);
  1551. IPC::ResponseBuilder rb{ctx, 6};
  1552. rb.Push(ResultSuccess);
  1553. rb.Push(size.normal);
  1554. rb.Push(size.journal);
  1555. }
  1556. void IApplicationFunctions::CreateCacheStorage(HLERequestContext& ctx) {
  1557. struct InputParameters {
  1558. u16 index;
  1559. s64 size;
  1560. s64 journal_size;
  1561. };
  1562. static_assert(sizeof(InputParameters) == 24);
  1563. struct OutputParameters {
  1564. u32 storage_target;
  1565. u64 required_size;
  1566. };
  1567. static_assert(sizeof(OutputParameters) == 16);
  1568. IPC::RequestParser rp{ctx};
  1569. const auto params = rp.PopRaw<InputParameters>();
  1570. LOG_WARNING(Service_AM, "(STUBBED) called with index={}, size={:#x}, journal_size={:#x}",
  1571. params.index, params.size, params.journal_size);
  1572. const OutputParameters resp{
  1573. .storage_target = 1,
  1574. .required_size = 0,
  1575. };
  1576. IPC::ResponseBuilder rb{ctx, 6};
  1577. rb.Push(ResultSuccess);
  1578. rb.PushRaw(resp);
  1579. }
  1580. void IApplicationFunctions::GetSaveDataSizeMax(HLERequestContext& ctx) {
  1581. LOG_WARNING(Service_AM, "(STUBBED) called");
  1582. constexpr u64 size_max_normal = 0xFFFFFFF;
  1583. constexpr u64 size_max_journal = 0xFFFFFFF;
  1584. IPC::ResponseBuilder rb{ctx, 6};
  1585. rb.Push(ResultSuccess);
  1586. rb.Push(size_max_normal);
  1587. rb.Push(size_max_journal);
  1588. }
  1589. void IApplicationFunctions::QueryApplicationPlayStatistics(HLERequestContext& ctx) {
  1590. LOG_WARNING(Service_AM, "(STUBBED) called");
  1591. IPC::ResponseBuilder rb{ctx, 3};
  1592. rb.Push(ResultSuccess);
  1593. rb.Push<u32>(0);
  1594. }
  1595. void IApplicationFunctions::QueryApplicationPlayStatisticsByUid(HLERequestContext& ctx) {
  1596. LOG_WARNING(Service_AM, "(STUBBED) called");
  1597. IPC::ResponseBuilder rb{ctx, 3};
  1598. rb.Push(ResultSuccess);
  1599. rb.Push<u32>(0);
  1600. }
  1601. void IApplicationFunctions::ExecuteProgram(HLERequestContext& ctx) {
  1602. LOG_WARNING(Service_AM, "(STUBBED) called");
  1603. IPC::RequestParser rp{ctx};
  1604. [[maybe_unused]] const auto unk_1 = rp.Pop<u32>();
  1605. [[maybe_unused]] const auto unk_2 = rp.Pop<u32>();
  1606. const auto program_index = rp.Pop<u64>();
  1607. IPC::ResponseBuilder rb{ctx, 2};
  1608. rb.Push(ResultSuccess);
  1609. system.ExecuteProgram(program_index);
  1610. }
  1611. void IApplicationFunctions::ClearUserChannel(HLERequestContext& ctx) {
  1612. LOG_DEBUG(Service_AM, "called");
  1613. system.GetUserChannel().clear();
  1614. IPC::ResponseBuilder rb{ctx, 2};
  1615. rb.Push(ResultSuccess);
  1616. }
  1617. void IApplicationFunctions::UnpopToUserChannel(HLERequestContext& ctx) {
  1618. LOG_DEBUG(Service_AM, "called");
  1619. IPC::RequestParser rp{ctx};
  1620. const auto storage = rp.PopIpcInterface<IStorage>().lock();
  1621. if (storage) {
  1622. system.GetUserChannel().push_back(storage->GetData());
  1623. }
  1624. IPC::ResponseBuilder rb{ctx, 2};
  1625. rb.Push(ResultSuccess);
  1626. }
  1627. void IApplicationFunctions::GetPreviousProgramIndex(HLERequestContext& ctx) {
  1628. LOG_WARNING(Service_AM, "(STUBBED) called");
  1629. IPC::ResponseBuilder rb{ctx, 3};
  1630. rb.Push(ResultSuccess);
  1631. rb.Push<s32>(previous_program_index);
  1632. }
  1633. void IApplicationFunctions::GetGpuErrorDetectedSystemEvent(HLERequestContext& ctx) {
  1634. LOG_WARNING(Service_AM, "(STUBBED) called");
  1635. IPC::ResponseBuilder rb{ctx, 2, 1};
  1636. rb.Push(ResultSuccess);
  1637. rb.PushCopyObjects(gpu_error_detected_event->GetReadableEvent());
  1638. }
  1639. void IApplicationFunctions::GetFriendInvitationStorageChannelEvent(HLERequestContext& ctx) {
  1640. LOG_DEBUG(Service_AM, "called");
  1641. IPC::ResponseBuilder rb{ctx, 2, 1};
  1642. rb.Push(ResultSuccess);
  1643. rb.PushCopyObjects(friend_invitation_storage_channel_event->GetReadableEvent());
  1644. }
  1645. void IApplicationFunctions::TryPopFromFriendInvitationStorageChannel(HLERequestContext& ctx) {
  1646. LOG_DEBUG(Service_AM, "(STUBBED) called");
  1647. IPC::ResponseBuilder rb{ctx, 2};
  1648. rb.Push(AM::ResultNoDataInChannel);
  1649. }
  1650. void IApplicationFunctions::GetNotificationStorageChannelEvent(HLERequestContext& ctx) {
  1651. LOG_DEBUG(Service_AM, "called");
  1652. IPC::ResponseBuilder rb{ctx, 2, 1};
  1653. rb.Push(ResultSuccess);
  1654. rb.PushCopyObjects(notification_storage_channel_event->GetReadableEvent());
  1655. }
  1656. void IApplicationFunctions::GetHealthWarningDisappearedSystemEvent(HLERequestContext& ctx) {
  1657. LOG_DEBUG(Service_AM, "called");
  1658. IPC::ResponseBuilder rb{ctx, 2, 1};
  1659. rb.Push(ResultSuccess);
  1660. rb.PushCopyObjects(health_warning_disappeared_system_event->GetReadableEvent());
  1661. }
  1662. void IApplicationFunctions::PrepareForJit(HLERequestContext& ctx) {
  1663. LOG_WARNING(Service_AM, "(STUBBED) called");
  1664. IPC::ResponseBuilder rb{ctx, 2};
  1665. rb.Push(ResultSuccess);
  1666. }
  1667. void LoopProcess(Nvnflinger::Nvnflinger& nvnflinger, Core::System& system) {
  1668. auto message_queue = std::make_shared<AppletMessageQueue>(system);
  1669. // Needed on game boot
  1670. message_queue->PushMessage(AppletMessageQueue::AppletMessage::FocusStateChanged);
  1671. auto server_manager = std::make_unique<ServerManager>(system);
  1672. server_manager->RegisterNamedService(
  1673. "appletAE", std::make_shared<AppletAE>(nvnflinger, message_queue, system));
  1674. server_manager->RegisterNamedService(
  1675. "appletOE", std::make_shared<AppletOE>(nvnflinger, message_queue, system));
  1676. server_manager->RegisterNamedService("idle:sys", std::make_shared<IdleSys>(system));
  1677. server_manager->RegisterNamedService("omm", std::make_shared<OMM>(system));
  1678. server_manager->RegisterNamedService("spsm", std::make_shared<SPSM>(system));
  1679. ServerManager::RunServer(std::move(server_manager));
  1680. }
  1681. IHomeMenuFunctions::IHomeMenuFunctions(Core::System& system_)
  1682. : ServiceFramework{system_, "IHomeMenuFunctions"}, service_context{system,
  1683. "IHomeMenuFunctions"} {
  1684. // clang-format off
  1685. static const FunctionInfo functions[] = {
  1686. {10, &IHomeMenuFunctions::RequestToGetForeground, "RequestToGetForeground"},
  1687. {11, nullptr, "LockForeground"},
  1688. {12, nullptr, "UnlockForeground"},
  1689. {20, nullptr, "PopFromGeneralChannel"},
  1690. {21, &IHomeMenuFunctions::GetPopFromGeneralChannelEvent, "GetPopFromGeneralChannelEvent"},
  1691. {30, nullptr, "GetHomeButtonWriterLockAccessor"},
  1692. {31, nullptr, "GetWriterLockAccessorEx"},
  1693. {40, nullptr, "IsSleepEnabled"},
  1694. {41, nullptr, "IsRebootEnabled"},
  1695. {50, nullptr, "LaunchSystemApplet"},
  1696. {51, nullptr, "LaunchStarter"},
  1697. {100, nullptr, "PopRequestLaunchApplicationForDebug"},
  1698. {110, nullptr, "IsForceTerminateApplicationDisabledForDebug"},
  1699. {200, nullptr, "LaunchDevMenu"},
  1700. {1000, nullptr, "SetLastApplicationExitReason"},
  1701. };
  1702. // clang-format on
  1703. RegisterHandlers(functions);
  1704. pop_from_general_channel_event =
  1705. service_context.CreateEvent("IHomeMenuFunctions:PopFromGeneralChannelEvent");
  1706. }
  1707. IHomeMenuFunctions::~IHomeMenuFunctions() {
  1708. service_context.CloseEvent(pop_from_general_channel_event);
  1709. }
  1710. void IHomeMenuFunctions::RequestToGetForeground(HLERequestContext& ctx) {
  1711. LOG_WARNING(Service_AM, "(STUBBED) called");
  1712. IPC::ResponseBuilder rb{ctx, 2};
  1713. rb.Push(ResultSuccess);
  1714. }
  1715. void IHomeMenuFunctions::GetPopFromGeneralChannelEvent(HLERequestContext& ctx) {
  1716. LOG_WARNING(Service_AM, "(STUBBED) called");
  1717. IPC::ResponseBuilder rb{ctx, 2, 1};
  1718. rb.Push(ResultSuccess);
  1719. rb.PushCopyObjects(pop_from_general_channel_event->GetReadableEvent());
  1720. }
  1721. IGlobalStateController::IGlobalStateController(Core::System& system_)
  1722. : ServiceFramework{system_, "IGlobalStateController"} {
  1723. // clang-format off
  1724. static const FunctionInfo functions[] = {
  1725. {0, nullptr, "RequestToEnterSleep"},
  1726. {1, nullptr, "EnterSleep"},
  1727. {2, nullptr, "StartSleepSequence"},
  1728. {3, nullptr, "StartShutdownSequence"},
  1729. {4, nullptr, "StartRebootSequence"},
  1730. {9, nullptr, "IsAutoPowerDownRequested"},
  1731. {10, nullptr, "LoadAndApplyIdlePolicySettings"},
  1732. {11, nullptr, "NotifyCecSettingsChanged"},
  1733. {12, nullptr, "SetDefaultHomeButtonLongPressTime"},
  1734. {13, nullptr, "UpdateDefaultDisplayResolution"},
  1735. {14, nullptr, "ShouldSleepOnBoot"},
  1736. {15, nullptr, "GetHdcpAuthenticationFailedEvent"},
  1737. {30, nullptr, "OpenCradleFirmwareUpdater"},
  1738. };
  1739. // clang-format on
  1740. RegisterHandlers(functions);
  1741. }
  1742. IGlobalStateController::~IGlobalStateController() = default;
  1743. IApplicationCreator::IApplicationCreator(Core::System& system_)
  1744. : ServiceFramework{system_, "IApplicationCreator"} {
  1745. // clang-format off
  1746. static const FunctionInfo functions[] = {
  1747. {0, nullptr, "CreateApplication"},
  1748. {1, nullptr, "PopLaunchRequestedApplication"},
  1749. {10, nullptr, "CreateSystemApplication"},
  1750. {100, nullptr, "PopFloatingApplicationForDevelopment"},
  1751. };
  1752. // clang-format on
  1753. RegisterHandlers(functions);
  1754. }
  1755. IApplicationCreator::~IApplicationCreator() = default;
  1756. IProcessWindingController::IProcessWindingController(Core::System& system_)
  1757. : ServiceFramework{system_, "IProcessWindingController"} {
  1758. // clang-format off
  1759. static const FunctionInfo functions[] = {
  1760. {0, &IProcessWindingController::GetLaunchReason, "GetLaunchReason"},
  1761. {11, &IProcessWindingController::OpenCallingLibraryApplet, "OpenCallingLibraryApplet"},
  1762. {21, nullptr, "PushContext"},
  1763. {22, nullptr, "PopContext"},
  1764. {23, nullptr, "CancelWindingReservation"},
  1765. {30, nullptr, "WindAndDoReserved"},
  1766. {40, nullptr, "ReserveToStartAndWaitAndUnwindThis"},
  1767. {41, nullptr, "ReserveToStartAndWait"},
  1768. };
  1769. // clang-format on
  1770. RegisterHandlers(functions);
  1771. }
  1772. IProcessWindingController::~IProcessWindingController() = default;
  1773. void IProcessWindingController::GetLaunchReason(HLERequestContext& ctx) {
  1774. LOG_WARNING(Service_AM, "(STUBBED) called");
  1775. struct AppletProcessLaunchReason {
  1776. u8 flag;
  1777. INSERT_PADDING_BYTES(3);
  1778. };
  1779. static_assert(sizeof(AppletProcessLaunchReason) == 0x4,
  1780. "AppletProcessLaunchReason is an invalid size");
  1781. AppletProcessLaunchReason reason{
  1782. .flag = 0,
  1783. };
  1784. IPC::ResponseBuilder rb{ctx, 3};
  1785. rb.Push(ResultSuccess);
  1786. rb.PushRaw(reason);
  1787. }
  1788. void IProcessWindingController::OpenCallingLibraryApplet(HLERequestContext& ctx) {
  1789. const auto applet_id = Applets::AppletId::MiiEdit;
  1790. const auto applet_mode = Applets::LibraryAppletMode::AllForeground;
  1791. LOG_WARNING(Service_AM, "(STUBBED) called with applet_id={:08X}, applet_mode={:08X}", applet_id,
  1792. applet_mode);
  1793. const auto& applet_manager{system.GetAppletManager()};
  1794. const auto applet = applet_manager.GetApplet(applet_id, applet_mode);
  1795. if (applet == nullptr) {
  1796. LOG_ERROR(Service_AM, "Applet doesn't exist! applet_id={}", applet_id);
  1797. IPC::ResponseBuilder rb{ctx, 2};
  1798. rb.Push(ResultUnknown);
  1799. return;
  1800. }
  1801. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1802. rb.Push(ResultSuccess);
  1803. rb.PushIpcInterface<ILibraryAppletAccessor>(system, applet);
  1804. }
  1805. } // namespace Service::AM