am.cpp 70 KB

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