am.cpp 66 KB

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