am.cpp 62 KB

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