am.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  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. // clang-format off
  220. static const FunctionInfo functions[] = {
  221. {0, &ISelfController::Exit, "Exit"},
  222. {1, &ISelfController::LockExit, "LockExit"},
  223. {2, &ISelfController::UnlockExit, "UnlockExit"},
  224. {3, &ISelfController::EnterFatalSection, "EnterFatalSection"},
  225. {4, &ISelfController::LeaveFatalSection, "LeaveFatalSection"},
  226. {9, &ISelfController::GetLibraryAppletLaunchableEvent, "GetLibraryAppletLaunchableEvent"},
  227. {10, &ISelfController::SetScreenShotPermission, "SetScreenShotPermission"},
  228. {11, &ISelfController::SetOperationModeChangedNotification, "SetOperationModeChangedNotification"},
  229. {12, &ISelfController::SetPerformanceModeChangedNotification, "SetPerformanceModeChangedNotification"},
  230. {13, &ISelfController::SetFocusHandlingMode, "SetFocusHandlingMode"},
  231. {14, &ISelfController::SetRestartMessageEnabled, "SetRestartMessageEnabled"},
  232. {15, nullptr, "SetScreenShotAppletIdentityInfo"},
  233. {16, &ISelfController::SetOutOfFocusSuspendingEnabled, "SetOutOfFocusSuspendingEnabled"},
  234. {17, nullptr, "SetControllerFirmwareUpdateSection"},
  235. {18, nullptr, "SetRequiresCaptureButtonShortPressedMessage"},
  236. {19, &ISelfController::SetAlbumImageOrientation, "SetAlbumImageOrientation"},
  237. {20, nullptr, "SetDesirableKeyboardLayout"},
  238. {40, &ISelfController::CreateManagedDisplayLayer, "CreateManagedDisplayLayer"},
  239. {41, nullptr, "IsSystemBufferSharingEnabled"},
  240. {42, nullptr, "GetSystemSharedLayerHandle"},
  241. {43, nullptr, "GetSystemSharedBufferHandle"},
  242. {44, &ISelfController::CreateManagedDisplaySeparableLayer, "CreateManagedDisplaySeparableLayer"},
  243. {45, nullptr, "SetManagedDisplayLayerSeparationMode"},
  244. {50, &ISelfController::SetHandlesRequestToDisplay, "SetHandlesRequestToDisplay"},
  245. {51, nullptr, "ApproveToDisplay"},
  246. {60, nullptr, "OverrideAutoSleepTimeAndDimmingTime"},
  247. {61, nullptr, "SetMediaPlaybackState"},
  248. {62, &ISelfController::SetIdleTimeDetectionExtension, "SetIdleTimeDetectionExtension"},
  249. {63, &ISelfController::GetIdleTimeDetectionExtension, "GetIdleTimeDetectionExtension"},
  250. {64, nullptr, "SetInputDetectionSourceSet"},
  251. {65, nullptr, "ReportUserIsActive"},
  252. {66, nullptr, "GetCurrentIlluminance"},
  253. {67, nullptr, "IsIlluminanceAvailable"},
  254. {68, &ISelfController::SetAutoSleepDisabled, "SetAutoSleepDisabled"},
  255. {69, &ISelfController::IsAutoSleepDisabled, "IsAutoSleepDisabled"},
  256. {70, nullptr, "ReportMultimediaError"},
  257. {71, nullptr, "GetCurrentIlluminanceEx"},
  258. {72, nullptr, "SetInputDetectionPolicy"},
  259. {80, nullptr, "SetWirelessPriorityMode"},
  260. {90, &ISelfController::GetAccumulatedSuspendedTickValue, "GetAccumulatedSuspendedTickValue"},
  261. {91, &ISelfController::GetAccumulatedSuspendedTickChangedEvent, "GetAccumulatedSuspendedTickChangedEvent"},
  262. {100, &ISelfController::SetAlbumImageTakenNotificationEnabled, "SetAlbumImageTakenNotificationEnabled"},
  263. {110, nullptr, "SetApplicationAlbumUserData"},
  264. {120, nullptr, "SaveCurrentScreenshot"},
  265. {1000, nullptr, "GetDebugStorageChannel"},
  266. };
  267. // clang-format on
  268. RegisterHandlers(functions);
  269. auto& kernel = system.Kernel();
  270. launchable_event = Kernel::KEvent::Create(kernel, "ISelfController:LaunchableEvent");
  271. launchable_event->Initialize();
  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. accumulated_suspended_tick_changed_event =
  278. Kernel::KEvent::Create(kernel, "ISelfController:AccumulatedSuspendedTickChangedEvent");
  279. accumulated_suspended_tick_changed_event->Initialize();
  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::KEvent::Create(kernel, "AMMessageQueue:OnMessageReceived");
  476. on_new_message->Initialize();
  477. on_operation_mode_changed =
  478. Kernel::KEvent::Create(kernel, "AMMessageQueue:OperationModeChanged");
  479. on_operation_mode_changed->Initialize();
  480. }
  481. AppletMessageQueue::~AppletMessageQueue() = default;
  482. const std::shared_ptr<Kernel::KReadableEvent>& AppletMessageQueue::GetMessageReceiveEvent() const {
  483. return on_new_message->GetReadableEvent();
  484. }
  485. const std::shared_ptr<Kernel::KReadableEvent>& AppletMessageQueue::GetOperationModeChangedEvent()
  486. const {
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  630. }
  631. void ICommonStateGetter::BeginVrModeEx(Kernel::HLERequestContext& ctx) {
  632. LOG_WARNING(Service_AM, "(STUBBED) called");
  633. IPC::ResponseBuilder rb{ctx, 2};
  634. rb.Push(RESULT_SUCCESS);
  635. }
  636. void ICommonStateGetter::EndVrModeEx(Kernel::HLERequestContext& ctx) {
  637. LOG_WARNING(Service_AM, "(STUBBED) called");
  638. IPC::ResponseBuilder rb{ctx, 2};
  639. rb.Push(RESULT_SUCCESS);
  640. }
  641. void ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent(Kernel::HLERequestContext& ctx) {
  642. LOG_DEBUG(Service_AM, "called");
  643. IPC::ResponseBuilder rb{ctx, 2, 1};
  644. rb.Push(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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(RESULT_SUCCESS);
  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. const auto event = applet->GetBroker().GetStateChangedEvent();
  759. IPC::ResponseBuilder rb{ctx, 2, 1};
  760. rb.Push(RESULT_SUCCESS);
  761. rb.PushCopyObjects(event);
  762. }
  763. void IsCompleted(Kernel::HLERequestContext& ctx) {
  764. LOG_DEBUG(Service_AM, "called");
  765. IPC::ResponseBuilder rb{ctx, 3};
  766. rb.Push(RESULT_SUCCESS);
  767. rb.Push<u32>(applet->TransactionComplete());
  768. }
  769. void GetResult(Kernel::HLERequestContext& ctx) {
  770. LOG_DEBUG(Service_AM, "called");
  771. IPC::ResponseBuilder rb{ctx, 2};
  772. rb.Push(applet->GetStatus());
  773. }
  774. void PresetLibraryAppletGpuTimeSliceZero(Kernel::HLERequestContext& ctx) {
  775. LOG_WARNING(Service_AM, "(STUBBED) called");
  776. IPC::ResponseBuilder rb{ctx, 2};
  777. rb.Push(RESULT_SUCCESS);
  778. }
  779. void Start(Kernel::HLERequestContext& ctx) {
  780. LOG_DEBUG(Service_AM, "called");
  781. ASSERT(applet != nullptr);
  782. applet->Initialize();
  783. applet->Execute();
  784. IPC::ResponseBuilder rb{ctx, 2};
  785. rb.Push(RESULT_SUCCESS);
  786. }
  787. void PushInData(Kernel::HLERequestContext& ctx) {
  788. LOG_DEBUG(Service_AM, "called");
  789. IPC::RequestParser rp{ctx};
  790. applet->GetBroker().PushNormalDataFromGame(rp.PopIpcInterface<IStorage>());
  791. IPC::ResponseBuilder rb{ctx, 2};
  792. rb.Push(RESULT_SUCCESS);
  793. }
  794. void PopOutData(Kernel::HLERequestContext& ctx) {
  795. LOG_DEBUG(Service_AM, "called");
  796. auto storage = applet->GetBroker().PopNormalDataToGame();
  797. if (storage == nullptr) {
  798. LOG_DEBUG(Service_AM,
  799. "storage is a nullptr. There is no data in the current normal channel");
  800. IPC::ResponseBuilder rb{ctx, 2};
  801. rb.Push(ERR_NO_DATA_IN_CHANNEL);
  802. return;
  803. }
  804. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  805. rb.Push(RESULT_SUCCESS);
  806. rb.PushIpcInterface<IStorage>(std::move(storage));
  807. }
  808. void PushInteractiveInData(Kernel::HLERequestContext& ctx) {
  809. LOG_DEBUG(Service_AM, "called");
  810. IPC::RequestParser rp{ctx};
  811. applet->GetBroker().PushInteractiveDataFromGame(rp.PopIpcInterface<IStorage>());
  812. ASSERT(applet->IsInitialized());
  813. applet->ExecuteInteractive();
  814. applet->Execute();
  815. IPC::ResponseBuilder rb{ctx, 2};
  816. rb.Push(RESULT_SUCCESS);
  817. }
  818. void PopInteractiveOutData(Kernel::HLERequestContext& ctx) {
  819. LOG_DEBUG(Service_AM, "called");
  820. auto storage = applet->GetBroker().PopInteractiveDataToGame();
  821. if (storage == nullptr) {
  822. LOG_DEBUG(Service_AM,
  823. "storage is a nullptr. There is no data in the current interactive channel");
  824. IPC::ResponseBuilder rb{ctx, 2};
  825. rb.Push(ERR_NO_DATA_IN_CHANNEL);
  826. return;
  827. }
  828. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  829. rb.Push(RESULT_SUCCESS);
  830. rb.PushIpcInterface<IStorage>(std::move(storage));
  831. }
  832. void GetPopOutDataEvent(Kernel::HLERequestContext& ctx) {
  833. LOG_DEBUG(Service_AM, "called");
  834. IPC::ResponseBuilder rb{ctx, 2, 1};
  835. rb.Push(RESULT_SUCCESS);
  836. rb.PushCopyObjects(applet->GetBroker().GetNormalDataEvent());
  837. }
  838. void GetPopInteractiveOutDataEvent(Kernel::HLERequestContext& ctx) {
  839. LOG_DEBUG(Service_AM, "called");
  840. IPC::ResponseBuilder rb{ctx, 2, 1};
  841. rb.Push(RESULT_SUCCESS);
  842. rb.PushCopyObjects(applet->GetBroker().GetInteractiveDataEvent());
  843. }
  844. void GetIndirectLayerConsumerHandle(Kernel::HLERequestContext& ctx) {
  845. LOG_WARNING(Service_AM, "(STUBBED) called");
  846. // We require a non-zero handle to be valid. Using 0xdeadbeef allows us to trace if this is
  847. // actually used anywhere
  848. constexpr u64 handle = 0xdeadbeef;
  849. IPC::ResponseBuilder rb{ctx, 4};
  850. rb.Push(RESULT_SUCCESS);
  851. rb.Push(handle);
  852. }
  853. std::shared_ptr<Applets::Applet> applet;
  854. };
  855. IStorageAccessor::IStorageAccessor(Core::System& system_, IStorage& backing_)
  856. : ServiceFramework{system_, "IStorageAccessor"}, backing{backing_} {
  857. // clang-format off
  858. static const FunctionInfo functions[] = {
  859. {0, &IStorageAccessor::GetSize, "GetSize"},
  860. {10, &IStorageAccessor::Write, "Write"},
  861. {11, &IStorageAccessor::Read, "Read"},
  862. };
  863. // clang-format on
  864. RegisterHandlers(functions);
  865. }
  866. IStorageAccessor::~IStorageAccessor() = default;
  867. void IStorageAccessor::GetSize(Kernel::HLERequestContext& ctx) {
  868. LOG_DEBUG(Service_AM, "called");
  869. IPC::ResponseBuilder rb{ctx, 4};
  870. rb.Push(RESULT_SUCCESS);
  871. rb.Push(static_cast<u64>(backing.GetSize()));
  872. }
  873. void IStorageAccessor::Write(Kernel::HLERequestContext& ctx) {
  874. IPC::RequestParser rp{ctx};
  875. const u64 offset{rp.Pop<u64>()};
  876. const std::vector<u8> data{ctx.ReadBuffer()};
  877. const std::size_t size{std::min(data.size(), backing.GetSize() - offset)};
  878. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  879. if (offset > backing.GetSize()) {
  880. LOG_ERROR(Service_AM,
  881. "offset is out of bounds, backing_buffer_sz={}, data_size={}, offset={}",
  882. backing.GetSize(), size, offset);
  883. IPC::ResponseBuilder rb{ctx, 2};
  884. rb.Push(ERR_SIZE_OUT_OF_BOUNDS);
  885. return;
  886. }
  887. std::memcpy(backing.GetData().data() + offset, data.data(), size);
  888. IPC::ResponseBuilder rb{ctx, 2};
  889. rb.Push(RESULT_SUCCESS);
  890. }
  891. void IStorageAccessor::Read(Kernel::HLERequestContext& ctx) {
  892. IPC::RequestParser rp{ctx};
  893. const u64 offset{rp.Pop<u64>()};
  894. const std::size_t size{std::min(ctx.GetWriteBufferSize(), backing.GetSize() - offset)};
  895. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  896. if (offset > backing.GetSize()) {
  897. LOG_ERROR(Service_AM, "offset is out of bounds, backing_buffer_sz={}, size={}, offset={}",
  898. backing.GetSize(), size, offset);
  899. IPC::ResponseBuilder rb{ctx, 2};
  900. rb.Push(ERR_SIZE_OUT_OF_BOUNDS);
  901. return;
  902. }
  903. ctx.WriteBuffer(backing.GetData().data() + offset, size);
  904. IPC::ResponseBuilder rb{ctx, 2};
  905. rb.Push(RESULT_SUCCESS);
  906. }
  907. ILibraryAppletCreator::ILibraryAppletCreator(Core::System& system_)
  908. : ServiceFramework{system_, "ILibraryAppletCreator"} {
  909. static const FunctionInfo functions[] = {
  910. {0, &ILibraryAppletCreator::CreateLibraryApplet, "CreateLibraryApplet"},
  911. {1, nullptr, "TerminateAllLibraryApplets"},
  912. {2, nullptr, "AreAnyLibraryAppletsLeft"},
  913. {10, &ILibraryAppletCreator::CreateStorage, "CreateStorage"},
  914. {11, &ILibraryAppletCreator::CreateTransferMemoryStorage, "CreateTransferMemoryStorage"},
  915. {12, &ILibraryAppletCreator::CreateHandleStorage, "CreateHandleStorage"},
  916. };
  917. RegisterHandlers(functions);
  918. }
  919. ILibraryAppletCreator::~ILibraryAppletCreator() = default;
  920. void ILibraryAppletCreator::CreateLibraryApplet(Kernel::HLERequestContext& ctx) {
  921. IPC::RequestParser rp{ctx};
  922. const auto applet_id = rp.PopRaw<Applets::AppletId>();
  923. const auto applet_mode = rp.PopRaw<Applets::LibraryAppletMode>();
  924. LOG_DEBUG(Service_AM, "called with applet_id={:08X}, applet_mode={:08X}", applet_id,
  925. applet_mode);
  926. const auto& applet_manager{system.GetAppletManager()};
  927. const auto applet = applet_manager.GetApplet(applet_id, applet_mode);
  928. if (applet == nullptr) {
  929. LOG_ERROR(Service_AM, "Applet doesn't exist! applet_id={}", applet_id);
  930. IPC::ResponseBuilder rb{ctx, 2};
  931. rb.Push(RESULT_UNKNOWN);
  932. return;
  933. }
  934. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  935. rb.Push(RESULT_SUCCESS);
  936. rb.PushIpcInterface<ILibraryAppletAccessor>(system, applet);
  937. }
  938. void ILibraryAppletCreator::CreateStorage(Kernel::HLERequestContext& ctx) {
  939. IPC::RequestParser rp{ctx};
  940. const s64 size{rp.Pop<s64>()};
  941. LOG_DEBUG(Service_AM, "called, size={}", size);
  942. if (size <= 0) {
  943. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  944. IPC::ResponseBuilder rb{ctx, 2};
  945. rb.Push(RESULT_UNKNOWN);
  946. return;
  947. }
  948. std::vector<u8> buffer(size);
  949. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  950. rb.Push(RESULT_SUCCESS);
  951. rb.PushIpcInterface<IStorage>(system, std::move(buffer));
  952. }
  953. void ILibraryAppletCreator::CreateTransferMemoryStorage(Kernel::HLERequestContext& ctx) {
  954. IPC::RequestParser rp{ctx};
  955. struct Parameters {
  956. u8 permissions;
  957. s64 size;
  958. };
  959. const auto parameters{rp.PopRaw<Parameters>()};
  960. const auto handle{ctx.GetCopyHandle(0)};
  961. LOG_DEBUG(Service_AM, "called, permissions={}, size={}, handle={:08X}", parameters.permissions,
  962. parameters.size, handle);
  963. if (parameters.size <= 0) {
  964. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  965. IPC::ResponseBuilder rb{ctx, 2};
  966. rb.Push(RESULT_UNKNOWN);
  967. return;
  968. }
  969. auto transfer_mem =
  970. system.CurrentProcess()->GetHandleTable().Get<Kernel::TransferMemory>(handle);
  971. if (transfer_mem == nullptr) {
  972. LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle);
  973. IPC::ResponseBuilder rb{ctx, 2};
  974. rb.Push(RESULT_UNKNOWN);
  975. return;
  976. }
  977. const u8* const mem_begin = transfer_mem->GetPointer();
  978. const u8* const mem_end = mem_begin + transfer_mem->GetSize();
  979. std::vector<u8> memory{mem_begin, mem_end};
  980. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  981. rb.Push(RESULT_SUCCESS);
  982. rb.PushIpcInterface<IStorage>(system, std::move(memory));
  983. }
  984. void ILibraryAppletCreator::CreateHandleStorage(Kernel::HLERequestContext& ctx) {
  985. IPC::RequestParser rp{ctx};
  986. const s64 size{rp.Pop<s64>()};
  987. const auto handle{ctx.GetCopyHandle(0)};
  988. LOG_DEBUG(Service_AM, "called, size={}, handle={:08X}", size, handle);
  989. if (size <= 0) {
  990. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  991. IPC::ResponseBuilder rb{ctx, 2};
  992. rb.Push(RESULT_UNKNOWN);
  993. return;
  994. }
  995. auto transfer_mem =
  996. system.CurrentProcess()->GetHandleTable().Get<Kernel::TransferMemory>(handle);
  997. if (transfer_mem == nullptr) {
  998. LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle);
  999. IPC::ResponseBuilder rb{ctx, 2};
  1000. rb.Push(RESULT_UNKNOWN);
  1001. return;
  1002. }
  1003. const u8* const mem_begin = transfer_mem->GetPointer();
  1004. const u8* const mem_end = mem_begin + transfer_mem->GetSize();
  1005. std::vector<u8> memory{mem_begin, mem_end};
  1006. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1007. rb.Push(RESULT_SUCCESS);
  1008. rb.PushIpcInterface<IStorage>(system, std::move(memory));
  1009. }
  1010. IApplicationFunctions::IApplicationFunctions(Core::System& system_)
  1011. : ServiceFramework{system_, "IApplicationFunctions"} {
  1012. // clang-format off
  1013. static const FunctionInfo functions[] = {
  1014. {1, &IApplicationFunctions::PopLaunchParameter, "PopLaunchParameter"},
  1015. {10, nullptr, "CreateApplicationAndPushAndRequestToStart"},
  1016. {11, nullptr, "CreateApplicationAndPushAndRequestToStartForQuest"},
  1017. {12, nullptr, "CreateApplicationAndRequestToStart"},
  1018. {13, &IApplicationFunctions::CreateApplicationAndRequestToStartForQuest, "CreateApplicationAndRequestToStartForQuest"},
  1019. {14, nullptr, "CreateApplicationWithAttributeAndPushAndRequestToStartForQuest"},
  1020. {15, nullptr, "CreateApplicationWithAttributeAndRequestToStartForQuest"},
  1021. {20, &IApplicationFunctions::EnsureSaveData, "EnsureSaveData"},
  1022. {21, &IApplicationFunctions::GetDesiredLanguage, "GetDesiredLanguage"},
  1023. {22, &IApplicationFunctions::SetTerminateResult, "SetTerminateResult"},
  1024. {23, &IApplicationFunctions::GetDisplayVersion, "GetDisplayVersion"},
  1025. {24, nullptr, "GetLaunchStorageInfoForDebug"},
  1026. {25, &IApplicationFunctions::ExtendSaveData, "ExtendSaveData"},
  1027. {26, &IApplicationFunctions::GetSaveDataSize, "GetSaveDataSize"},
  1028. {27, nullptr, "CreateCacheStorage"},
  1029. {28, nullptr, "GetSaveDataSizeMax"},
  1030. {29, nullptr, "GetCacheStorageMax"},
  1031. {30, &IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed, "BeginBlockingHomeButtonShortAndLongPressed"},
  1032. {31, &IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed, "EndBlockingHomeButtonShortAndLongPressed"},
  1033. {32, &IApplicationFunctions::BeginBlockingHomeButton, "BeginBlockingHomeButton"},
  1034. {33, &IApplicationFunctions::EndBlockingHomeButton, "EndBlockingHomeButton"},
  1035. {34, nullptr, "SelectApplicationLicense"},
  1036. {35, nullptr, "GetDeviceSaveDataSizeMax"},
  1037. {40, &IApplicationFunctions::NotifyRunning, "NotifyRunning"},
  1038. {50, &IApplicationFunctions::GetPseudoDeviceId, "GetPseudoDeviceId"},
  1039. {60, nullptr, "SetMediaPlaybackStateForApplication"},
  1040. {65, &IApplicationFunctions::IsGamePlayRecordingSupported, "IsGamePlayRecordingSupported"},
  1041. {66, &IApplicationFunctions::InitializeGamePlayRecording, "InitializeGamePlayRecording"},
  1042. {67, &IApplicationFunctions::SetGamePlayRecordingState, "SetGamePlayRecordingState"},
  1043. {68, nullptr, "RequestFlushGamePlayingMovieForDebug"},
  1044. {70, nullptr, "RequestToShutdown"},
  1045. {71, nullptr, "RequestToReboot"},
  1046. {72, nullptr, "RequestToSleep"},
  1047. {80, nullptr, "ExitAndRequestToShowThanksMessage"},
  1048. {90, &IApplicationFunctions::EnableApplicationCrashReport, "EnableApplicationCrashReport"},
  1049. {100, &IApplicationFunctions::InitializeApplicationCopyrightFrameBuffer, "InitializeApplicationCopyrightFrameBuffer"},
  1050. {101, &IApplicationFunctions::SetApplicationCopyrightImage, "SetApplicationCopyrightImage"},
  1051. {102, &IApplicationFunctions::SetApplicationCopyrightVisibility, "SetApplicationCopyrightVisibility"},
  1052. {110, &IApplicationFunctions::QueryApplicationPlayStatistics, "QueryApplicationPlayStatistics"},
  1053. {111, &IApplicationFunctions::QueryApplicationPlayStatisticsByUid, "QueryApplicationPlayStatisticsByUid"},
  1054. {120, &IApplicationFunctions::ExecuteProgram, "ExecuteProgram"},
  1055. {121, &IApplicationFunctions::ClearUserChannel, "ClearUserChannel"},
  1056. {122, &IApplicationFunctions::UnpopToUserChannel, "UnpopToUserChannel"},
  1057. {123, &IApplicationFunctions::GetPreviousProgramIndex, "GetPreviousProgramIndex"},
  1058. {124, nullptr, "EnableApplicationAllThreadDumpOnCrash"},
  1059. {130, &IApplicationFunctions::GetGpuErrorDetectedSystemEvent, "GetGpuErrorDetectedSystemEvent"},
  1060. {131, nullptr, "SetDelayTimeToAbortOnGpuError"},
  1061. {140, &IApplicationFunctions::GetFriendInvitationStorageChannelEvent, "GetFriendInvitationStorageChannelEvent"},
  1062. {141, &IApplicationFunctions::TryPopFromFriendInvitationStorageChannel, "TryPopFromFriendInvitationStorageChannel"},
  1063. {150, nullptr, "GetNotificationStorageChannelEvent"},
  1064. {151, nullptr, "TryPopFromNotificationStorageChannel"},
  1065. {160, &IApplicationFunctions::GetHealthWarningDisappearedSystemEvent, "GetHealthWarningDisappearedSystemEvent"},
  1066. {170, nullptr, "SetHdcpAuthenticationActivated"},
  1067. {180, nullptr, "GetLaunchRequiredVersion"},
  1068. {181, nullptr, "UpgradeLaunchRequiredVersion"},
  1069. {190, nullptr, "SendServerMaintenanceOverlayNotification"},
  1070. {200, nullptr, "GetLastApplicationExitReason"},
  1071. {500, nullptr, "StartContinuousRecordingFlushForDebug"},
  1072. {1000, nullptr, "CreateMovieMaker"},
  1073. {1001, nullptr, "PrepareForJit"},
  1074. };
  1075. // clang-format on
  1076. RegisterHandlers(functions);
  1077. auto& kernel = system.Kernel();
  1078. gpu_error_detected_event =
  1079. Kernel::KEvent::Create(kernel, "IApplicationFunctions:GpuErrorDetectedSystemEvent");
  1080. gpu_error_detected_event->Initialize();
  1081. friend_invitation_storage_channel_event =
  1082. Kernel::KEvent::Create(kernel, "IApplicationFunctions:FriendInvitationStorageChannelEvent");
  1083. friend_invitation_storage_channel_event->Initialize();
  1084. health_warning_disappeared_system_event =
  1085. Kernel::KEvent::Create(kernel, "IApplicationFunctions:HealthWarningDisappearedSystemEvent");
  1086. health_warning_disappeared_system_event->Initialize();
  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"} {
  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. pop_from_general_channel_event =
  1441. Kernel::KEvent::Create(system.Kernel(), "IHomeMenuFunctions:PopFromGeneralChannelEvent");
  1442. pop_from_general_channel_event->Initialize();
  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