am.cpp 62 KB

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