am.cpp 70 KB

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