am.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cinttypes>
  7. #include <cstring>
  8. #include "audio_core/audio_renderer.h"
  9. #include "core/core.h"
  10. #include "core/file_sys/control_metadata.h"
  11. #include "core/file_sys/patch_manager.h"
  12. #include "core/file_sys/savedata_factory.h"
  13. #include "core/hle/ipc_helpers.h"
  14. #include "core/hle/kernel/kernel.h"
  15. #include "core/hle/kernel/process.h"
  16. #include "core/hle/kernel/readable_event.h"
  17. #include "core/hle/kernel/transfer_memory.h"
  18. #include "core/hle/kernel/writable_event.h"
  19. #include "core/hle/service/acc/profile_manager.h"
  20. #include "core/hle/service/am/am.h"
  21. #include "core/hle/service/am/applet_ae.h"
  22. #include "core/hle/service/am/applet_oe.h"
  23. #include "core/hle/service/am/applets/applets.h"
  24. #include "core/hle/service/am/applets/profile_select.h"
  25. #include "core/hle/service/am/applets/software_keyboard.h"
  26. #include "core/hle/service/am/applets/web_browser.h"
  27. #include "core/hle/service/am/idle.h"
  28. #include "core/hle/service/am/omm.h"
  29. #include "core/hle/service/am/spsm.h"
  30. #include "core/hle/service/am/tcap.h"
  31. #include "core/hle/service/apm/controller.h"
  32. #include "core/hle/service/apm/interface.h"
  33. #include "core/hle/service/filesystem/filesystem.h"
  34. #include "core/hle/service/ns/ns.h"
  35. #include "core/hle/service/nvflinger/nvflinger.h"
  36. #include "core/hle/service/pm/pm.h"
  37. #include "core/hle/service/set/set.h"
  38. #include "core/hle/service/sm/sm.h"
  39. #include "core/hle/service/vi/vi.h"
  40. #include "core/settings.h"
  41. namespace Service::AM {
  42. constexpr ResultCode ERR_NO_DATA_IN_CHANNEL{ErrorModule::AM, 0x2};
  43. constexpr ResultCode ERR_NO_MESSAGES{ErrorModule::AM, 0x3};
  44. constexpr ResultCode ERR_SIZE_OUT_OF_BOUNDS{ErrorModule::AM, 0x1F7};
  45. constexpr u32 POP_LAUNCH_PARAMETER_MAGIC = 0xC79497CA;
  46. struct LaunchParameters {
  47. u32_le magic;
  48. u32_le is_account_selected;
  49. u128 current_user;
  50. INSERT_PADDING_BYTES(0x70);
  51. };
  52. static_assert(sizeof(LaunchParameters) == 0x88);
  53. IWindowController::IWindowController(Core::System& system_)
  54. : ServiceFramework("IWindowController"), system{system_} {
  55. // clang-format off
  56. static const FunctionInfo functions[] = {
  57. {0, nullptr, "CreateWindow"},
  58. {1, &IWindowController::GetAppletResourceUserId, "GetAppletResourceUserId"},
  59. {10, &IWindowController::AcquireForegroundRights, "AcquireForegroundRights"},
  60. {11, nullptr, "ReleaseForegroundRights"},
  61. {12, nullptr, "RejectToChangeIntoBackground"},
  62. {20, nullptr, "SetAppletWindowVisibility"},
  63. {21, nullptr, "SetAppletGpuTimeSlice"},
  64. };
  65. // clang-format on
  66. RegisterHandlers(functions);
  67. }
  68. IWindowController::~IWindowController() = default;
  69. void IWindowController::GetAppletResourceUserId(Kernel::HLERequestContext& ctx) {
  70. const u64 process_id = system.CurrentProcess()->GetProcessID();
  71. LOG_DEBUG(Service_AM, "called. Process ID=0x{:016X}", process_id);
  72. IPC::ResponseBuilder rb{ctx, 4};
  73. rb.Push(RESULT_SUCCESS);
  74. rb.Push<u64>(process_id);
  75. }
  76. void IWindowController::AcquireForegroundRights(Kernel::HLERequestContext& ctx) {
  77. LOG_WARNING(Service_AM, "(STUBBED) called");
  78. IPC::ResponseBuilder rb{ctx, 2};
  79. rb.Push(RESULT_SUCCESS);
  80. }
  81. IAudioController::IAudioController() : ServiceFramework("IAudioController") {
  82. // clang-format off
  83. static const FunctionInfo functions[] = {
  84. {0, &IAudioController::SetExpectedMasterVolume, "SetExpectedMasterVolume"},
  85. {1, &IAudioController::GetMainAppletExpectedMasterVolume, "GetMainAppletExpectedMasterVolume"},
  86. {2, &IAudioController::GetLibraryAppletExpectedMasterVolume, "GetLibraryAppletExpectedMasterVolume"},
  87. {3, &IAudioController::ChangeMainAppletMasterVolume, "ChangeMainAppletMasterVolume"},
  88. {4, &IAudioController::SetTransparentAudioRate, "SetTransparentVolumeRate"},
  89. };
  90. // clang-format on
  91. RegisterHandlers(functions);
  92. }
  93. IAudioController::~IAudioController() = default;
  94. void IAudioController::SetExpectedMasterVolume(Kernel::HLERequestContext& ctx) {
  95. IPC::RequestParser rp{ctx};
  96. const float main_applet_volume_tmp = rp.Pop<float>();
  97. const float library_applet_volume_tmp = rp.Pop<float>();
  98. LOG_DEBUG(Service_AM, "called. main_applet_volume={}, library_applet_volume={}",
  99. main_applet_volume_tmp, library_applet_volume_tmp);
  100. // Ensure the volume values remain within the 0-100% range
  101. main_applet_volume = std::clamp(main_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  102. library_applet_volume =
  103. std::clamp(library_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  104. IPC::ResponseBuilder rb{ctx, 2};
  105. rb.Push(RESULT_SUCCESS);
  106. }
  107. void IAudioController::GetMainAppletExpectedMasterVolume(Kernel::HLERequestContext& ctx) {
  108. LOG_DEBUG(Service_AM, "called. main_applet_volume={}", main_applet_volume);
  109. IPC::ResponseBuilder rb{ctx, 3};
  110. rb.Push(RESULT_SUCCESS);
  111. rb.Push(main_applet_volume);
  112. }
  113. void IAudioController::GetLibraryAppletExpectedMasterVolume(Kernel::HLERequestContext& ctx) {
  114. LOG_DEBUG(Service_AM, "called. library_applet_volume={}", library_applet_volume);
  115. IPC::ResponseBuilder rb{ctx, 3};
  116. rb.Push(RESULT_SUCCESS);
  117. rb.Push(library_applet_volume);
  118. }
  119. void IAudioController::ChangeMainAppletMasterVolume(Kernel::HLERequestContext& ctx) {
  120. struct Parameters {
  121. float volume;
  122. s64 fade_time_ns;
  123. };
  124. static_assert(sizeof(Parameters) == 16);
  125. IPC::RequestParser rp{ctx};
  126. const auto parameters = rp.PopRaw<Parameters>();
  127. LOG_DEBUG(Service_AM, "called. volume={}, fade_time_ns={}", parameters.volume,
  128. parameters.fade_time_ns);
  129. main_applet_volume = std::clamp(parameters.volume, min_allowed_volume, max_allowed_volume);
  130. fade_time_ns = std::chrono::nanoseconds{parameters.fade_time_ns};
  131. IPC::ResponseBuilder rb{ctx, 2};
  132. rb.Push(RESULT_SUCCESS);
  133. }
  134. void IAudioController::SetTransparentAudioRate(Kernel::HLERequestContext& ctx) {
  135. IPC::RequestParser rp{ctx};
  136. const float transparent_volume_rate_tmp = rp.Pop<float>();
  137. LOG_DEBUG(Service_AM, "called. transparent_volume_rate={}", transparent_volume_rate_tmp);
  138. // Clamp volume range to 0-100%.
  139. transparent_volume_rate =
  140. std::clamp(transparent_volume_rate_tmp, min_allowed_volume, max_allowed_volume);
  141. IPC::ResponseBuilder rb{ctx, 2};
  142. rb.Push(RESULT_SUCCESS);
  143. }
  144. IDisplayController::IDisplayController() : ServiceFramework("IDisplayController") {
  145. // clang-format off
  146. static const FunctionInfo functions[] = {
  147. {0, nullptr, "GetLastForegroundCaptureImage"},
  148. {1, nullptr, "UpdateLastForegroundCaptureImage"},
  149. {2, nullptr, "GetLastApplicationCaptureImage"},
  150. {3, nullptr, "GetCallerAppletCaptureImage"},
  151. {4, nullptr, "UpdateCallerAppletCaptureImage"},
  152. {5, nullptr, "GetLastForegroundCaptureImageEx"},
  153. {6, nullptr, "GetLastApplicationCaptureImageEx"},
  154. {7, nullptr, "GetCallerAppletCaptureImageEx"},
  155. {8, nullptr, "TakeScreenShotOfOwnLayer"}, // 2.0.0+
  156. {9, nullptr, "CopyBetweenCaptureBuffers"}, // 5.0.0+
  157. {10, nullptr, "AcquireLastApplicationCaptureBuffer"},
  158. {11, nullptr, "ReleaseLastApplicationCaptureBuffer"},
  159. {12, nullptr, "AcquireLastForegroundCaptureBuffer"},
  160. {13, nullptr, "ReleaseLastForegroundCaptureBuffer"},
  161. {14, nullptr, "AcquireCallerAppletCaptureBuffer"},
  162. {15, nullptr, "ReleaseCallerAppletCaptureBuffer"},
  163. {16, nullptr, "AcquireLastApplicationCaptureBufferEx"},
  164. {17, nullptr, "AcquireLastForegroundCaptureBufferEx"},
  165. {18, nullptr, "AcquireCallerAppletCaptureBufferEx"},
  166. // 2.0.0+
  167. {20, nullptr, "ClearCaptureBuffer"},
  168. {21, nullptr, "ClearAppletTransitionBuffer"},
  169. // 4.0.0+
  170. {22, nullptr, "AcquireLastApplicationCaptureSharedBuffer"},
  171. {23, nullptr, "ReleaseLastApplicationCaptureSharedBuffer"},
  172. {24, nullptr, "AcquireLastForegroundCaptureSharedBuffer"},
  173. {25, nullptr, "ReleaseLastForegroundCaptureSharedBuffer"},
  174. {26, nullptr, "AcquireCallerAppletCaptureSharedBuffer"},
  175. {27, nullptr, "ReleaseCallerAppletCaptureSharedBuffer"},
  176. // 6.0.0+
  177. {28, nullptr, "TakeScreenShotOfOwnLayerEx"},
  178. };
  179. // clang-format on
  180. RegisterHandlers(functions);
  181. }
  182. IDisplayController::~IDisplayController() = default;
  183. IDebugFunctions::IDebugFunctions() : ServiceFramework{"IDebugFunctions"} {
  184. // clang-format off
  185. static const FunctionInfo functions[] = {
  186. {0, nullptr, "NotifyMessageToHomeMenuForDebug"},
  187. {1, nullptr, "OpenMainApplication"},
  188. {10, nullptr, "EmulateButtonEvent"},
  189. {20, nullptr, "InvalidateTransitionLayer"},
  190. {30, nullptr, "RequestLaunchApplicationWithUserAndArgumentForDebug"},
  191. {40, nullptr, "GetAppletResourceUsageInfo"},
  192. {41, nullptr, "SetCpuBoostModeForApplet"},
  193. };
  194. // clang-format on
  195. RegisterHandlers(functions);
  196. }
  197. IDebugFunctions::~IDebugFunctions() = default;
  198. ISelfController::ISelfController(Core::System& system,
  199. std::shared_ptr<NVFlinger::NVFlinger> nvflinger)
  200. : ServiceFramework("ISelfController"), system(system), nvflinger(std::move(nvflinger)) {
  201. // clang-format off
  202. static const FunctionInfo functions[] = {
  203. {0, &ISelfController::Exit, "Exit"},
  204. {1, &ISelfController::LockExit, "LockExit"},
  205. {2, &ISelfController::UnlockExit, "UnlockExit"},
  206. {3, &ISelfController::EnterFatalSection, "EnterFatalSection"},
  207. {4, &ISelfController::LeaveFatalSection, "LeaveFatalSection"},
  208. {9, &ISelfController::GetLibraryAppletLaunchableEvent, "GetLibraryAppletLaunchableEvent"},
  209. {10, &ISelfController::SetScreenShotPermission, "SetScreenShotPermission"},
  210. {11, &ISelfController::SetOperationModeChangedNotification, "SetOperationModeChangedNotification"},
  211. {12, &ISelfController::SetPerformanceModeChangedNotification, "SetPerformanceModeChangedNotification"},
  212. {13, &ISelfController::SetFocusHandlingMode, "SetFocusHandlingMode"},
  213. {14, &ISelfController::SetRestartMessageEnabled, "SetRestartMessageEnabled"},
  214. {15, nullptr, "SetScreenShotAppletIdentityInfo"},
  215. {16, &ISelfController::SetOutOfFocusSuspendingEnabled, "SetOutOfFocusSuspendingEnabled"},
  216. {17, nullptr, "SetControllerFirmwareUpdateSection"},
  217. {18, nullptr, "SetRequiresCaptureButtonShortPressedMessage"},
  218. {19, &ISelfController::SetScreenShotImageOrientation, "SetScreenShotImageOrientation"},
  219. {20, nullptr, "SetDesirableKeyboardLayout"},
  220. {40, &ISelfController::CreateManagedDisplayLayer, "CreateManagedDisplayLayer"},
  221. {41, nullptr, "IsSystemBufferSharingEnabled"},
  222. {42, nullptr, "GetSystemSharedLayerHandle"},
  223. {43, nullptr, "GetSystemSharedBufferHandle"},
  224. {50, &ISelfController::SetHandlesRequestToDisplay, "SetHandlesRequestToDisplay"},
  225. {51, nullptr, "ApproveToDisplay"},
  226. {60, nullptr, "OverrideAutoSleepTimeAndDimmingTime"},
  227. {61, nullptr, "SetMediaPlaybackState"},
  228. {62, &ISelfController::SetIdleTimeDetectionExtension, "SetIdleTimeDetectionExtension"},
  229. {63, &ISelfController::GetIdleTimeDetectionExtension, "GetIdleTimeDetectionExtension"},
  230. {64, nullptr, "SetInputDetectionSourceSet"},
  231. {65, nullptr, "ReportUserIsActive"},
  232. {66, nullptr, "GetCurrentIlluminance"},
  233. {67, nullptr, "IsIlluminanceAvailable"},
  234. {68, &ISelfController::SetAutoSleepDisabled, "SetAutoSleepDisabled"},
  235. {69, &ISelfController::IsAutoSleepDisabled, "IsAutoSleepDisabled"},
  236. {70, nullptr, "ReportMultimediaError"},
  237. {71, nullptr, "GetCurrentIlluminanceEx"},
  238. {80, nullptr, "SetWirelessPriorityMode"},
  239. {90, &ISelfController::GetAccumulatedSuspendedTickValue, "GetAccumulatedSuspendedTickValue"},
  240. {91, &ISelfController::GetAccumulatedSuspendedTickChangedEvent, "GetAccumulatedSuspendedTickChangedEvent"},
  241. {100, nullptr, "SetAlbumImageTakenNotificationEnabled"},
  242. {1000, nullptr, "GetDebugStorageChannel"},
  243. };
  244. // clang-format on
  245. RegisterHandlers(functions);
  246. auto& kernel = system.Kernel();
  247. launchable_event = Kernel::WritableEvent::CreateEventPair(kernel, Kernel::ResetType::Manual,
  248. "ISelfController:LaunchableEvent");
  249. // This event is created by AM on the first time GetAccumulatedSuspendedTickChangedEvent() is
  250. // called. Yuzu can just create it unconditionally, since it doesn't need to support multiple
  251. // ISelfControllers. The event is signaled on creation, and on transition from suspended -> not
  252. // suspended if the event has previously been created by a call to
  253. // GetAccumulatedSuspendedTickChangedEvent.
  254. accumulated_suspended_tick_changed_event = Kernel::WritableEvent::CreateEventPair(
  255. kernel, Kernel::ResetType::Manual, "ISelfController:AccumulatedSuspendedTickChangedEvent");
  256. accumulated_suspended_tick_changed_event.writable->Signal();
  257. }
  258. ISelfController::~ISelfController() = default;
  259. void ISelfController::Exit(Kernel::HLERequestContext& ctx) {
  260. LOG_DEBUG(Service_AM, "called");
  261. system.Shutdown();
  262. IPC::ResponseBuilder rb{ctx, 2};
  263. rb.Push(RESULT_SUCCESS);
  264. }
  265. void ISelfController::LockExit(Kernel::HLERequestContext& ctx) {
  266. LOG_DEBUG(Service_AM, "called");
  267. system.SetExitLock(true);
  268. IPC::ResponseBuilder rb{ctx, 2};
  269. rb.Push(RESULT_SUCCESS);
  270. }
  271. void ISelfController::UnlockExit(Kernel::HLERequestContext& ctx) {
  272. LOG_DEBUG(Service_AM, "called");
  273. system.SetExitLock(false);
  274. IPC::ResponseBuilder rb{ctx, 2};
  275. rb.Push(RESULT_SUCCESS);
  276. }
  277. void ISelfController::EnterFatalSection(Kernel::HLERequestContext& ctx) {
  278. ++num_fatal_sections_entered;
  279. LOG_DEBUG(Service_AM, "called. Num fatal sections entered: {}", num_fatal_sections_entered);
  280. IPC::ResponseBuilder rb{ctx, 2};
  281. rb.Push(RESULT_SUCCESS);
  282. }
  283. void ISelfController::LeaveFatalSection(Kernel::HLERequestContext& ctx) {
  284. LOG_DEBUG(Service_AM, "called.");
  285. // Entry and exit of fatal sections must be balanced.
  286. if (num_fatal_sections_entered == 0) {
  287. IPC::ResponseBuilder rb{ctx, 2};
  288. rb.Push(ResultCode{ErrorModule::AM, 512});
  289. return;
  290. }
  291. --num_fatal_sections_entered;
  292. IPC::ResponseBuilder rb{ctx, 2};
  293. rb.Push(RESULT_SUCCESS);
  294. }
  295. void ISelfController::GetLibraryAppletLaunchableEvent(Kernel::HLERequestContext& ctx) {
  296. LOG_WARNING(Service_AM, "(STUBBED) called");
  297. launchable_event.writable->Signal();
  298. IPC::ResponseBuilder rb{ctx, 2, 1};
  299. rb.Push(RESULT_SUCCESS);
  300. rb.PushCopyObjects(launchable_event.readable);
  301. }
  302. void ISelfController::SetScreenShotPermission(Kernel::HLERequestContext& ctx) {
  303. LOG_WARNING(Service_AM, "(STUBBED) called");
  304. IPC::ResponseBuilder rb{ctx, 2};
  305. rb.Push(RESULT_SUCCESS);
  306. }
  307. void ISelfController::SetOperationModeChangedNotification(Kernel::HLERequestContext& ctx) {
  308. IPC::RequestParser rp{ctx};
  309. bool flag = rp.Pop<bool>();
  310. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  311. IPC::ResponseBuilder rb{ctx, 2};
  312. rb.Push(RESULT_SUCCESS);
  313. }
  314. void ISelfController::SetPerformanceModeChangedNotification(Kernel::HLERequestContext& ctx) {
  315. IPC::RequestParser rp{ctx};
  316. bool flag = rp.Pop<bool>();
  317. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  318. IPC::ResponseBuilder rb{ctx, 2};
  319. rb.Push(RESULT_SUCCESS);
  320. }
  321. void ISelfController::SetFocusHandlingMode(Kernel::HLERequestContext& ctx) {
  322. // Takes 3 input u8s with each field located immediately after the previous
  323. // u8, these are bool flags. No output.
  324. IPC::RequestParser rp{ctx};
  325. struct FocusHandlingModeParams {
  326. u8 unknown0;
  327. u8 unknown1;
  328. u8 unknown2;
  329. };
  330. const auto flags = rp.PopRaw<FocusHandlingModeParams>();
  331. LOG_WARNING(Service_AM, "(STUBBED) called. unknown0={}, unknown1={}, unknown2={}",
  332. flags.unknown0, flags.unknown1, flags.unknown2);
  333. IPC::ResponseBuilder rb{ctx, 2};
  334. rb.Push(RESULT_SUCCESS);
  335. }
  336. void ISelfController::SetRestartMessageEnabled(Kernel::HLERequestContext& ctx) {
  337. LOG_WARNING(Service_AM, "(STUBBED) called");
  338. IPC::ResponseBuilder rb{ctx, 2};
  339. rb.Push(RESULT_SUCCESS);
  340. }
  341. void ISelfController::SetOutOfFocusSuspendingEnabled(Kernel::HLERequestContext& ctx) {
  342. // Takes 3 input u8s with each field located immediately after the previous
  343. // u8, these are bool flags. No output.
  344. IPC::RequestParser rp{ctx};
  345. bool enabled = rp.Pop<bool>();
  346. LOG_WARNING(Service_AM, "(STUBBED) called enabled={}", enabled);
  347. IPC::ResponseBuilder rb{ctx, 2};
  348. rb.Push(RESULT_SUCCESS);
  349. }
  350. void ISelfController::SetScreenShotImageOrientation(Kernel::HLERequestContext& ctx) {
  351. LOG_WARNING(Service_AM, "(STUBBED) called");
  352. IPC::ResponseBuilder rb{ctx, 2};
  353. rb.Push(RESULT_SUCCESS);
  354. }
  355. void ISelfController::CreateManagedDisplayLayer(Kernel::HLERequestContext& ctx) {
  356. LOG_WARNING(Service_AM, "(STUBBED) called");
  357. // TODO(Subv): Find out how AM determines the display to use, for now just
  358. // create the layer in the Default display.
  359. const auto display_id = nvflinger->OpenDisplay("Default");
  360. const auto layer_id = nvflinger->CreateLayer(*display_id);
  361. IPC::ResponseBuilder rb{ctx, 4};
  362. rb.Push(RESULT_SUCCESS);
  363. rb.Push(*layer_id);
  364. }
  365. void ISelfController::SetHandlesRequestToDisplay(Kernel::HLERequestContext& ctx) {
  366. LOG_WARNING(Service_AM, "(STUBBED) called");
  367. IPC::ResponseBuilder rb{ctx, 2};
  368. rb.Push(RESULT_SUCCESS);
  369. }
  370. void ISelfController::SetIdleTimeDetectionExtension(Kernel::HLERequestContext& ctx) {
  371. IPC::RequestParser rp{ctx};
  372. idle_time_detection_extension = rp.Pop<u32>();
  373. LOG_WARNING(Service_AM, "(STUBBED) called idle_time_detection_extension={}",
  374. idle_time_detection_extension);
  375. IPC::ResponseBuilder rb{ctx, 2};
  376. rb.Push(RESULT_SUCCESS);
  377. }
  378. void ISelfController::GetIdleTimeDetectionExtension(Kernel::HLERequestContext& ctx) {
  379. LOG_WARNING(Service_AM, "(STUBBED) called");
  380. IPC::ResponseBuilder rb{ctx, 3};
  381. rb.Push(RESULT_SUCCESS);
  382. rb.Push<u32>(idle_time_detection_extension);
  383. }
  384. void ISelfController::SetAutoSleepDisabled(Kernel::HLERequestContext& ctx) {
  385. IPC::RequestParser rp{ctx};
  386. is_auto_sleep_disabled = rp.Pop<bool>();
  387. // On the system itself, if the previous state of is_auto_sleep_disabled
  388. // differed from the current value passed in, it'd signify the internal
  389. // window manager to update (and also increment some statistics like update counts)
  390. //
  391. // It'd also indicate this change to an idle handling context.
  392. //
  393. // However, given we're emulating this behavior, most of this can be ignored
  394. // and it's sufficient to simply set the member variable for querying via
  395. // IsAutoSleepDisabled().
  396. LOG_DEBUG(Service_AM, "called. is_auto_sleep_disabled={}", is_auto_sleep_disabled);
  397. IPC::ResponseBuilder rb{ctx, 2};
  398. rb.Push(RESULT_SUCCESS);
  399. }
  400. void ISelfController::IsAutoSleepDisabled(Kernel::HLERequestContext& ctx) {
  401. LOG_DEBUG(Service_AM, "called.");
  402. IPC::ResponseBuilder rb{ctx, 3};
  403. rb.Push(RESULT_SUCCESS);
  404. rb.Push(is_auto_sleep_disabled);
  405. }
  406. void ISelfController::GetAccumulatedSuspendedTickValue(Kernel::HLERequestContext& ctx) {
  407. LOG_DEBUG(Service_AM, "called.");
  408. // This command returns the total number of system ticks since ISelfController creation
  409. // where the game was suspended. Since Yuzu doesn't implement game suspension, this command
  410. // can just always return 0 ticks.
  411. IPC::ResponseBuilder rb{ctx, 4};
  412. rb.Push(RESULT_SUCCESS);
  413. rb.Push<u64>(0);
  414. }
  415. void ISelfController::GetAccumulatedSuspendedTickChangedEvent(Kernel::HLERequestContext& ctx) {
  416. LOG_DEBUG(Service_AM, "called.");
  417. IPC::ResponseBuilder rb{ctx, 2, 1};
  418. rb.Push(RESULT_SUCCESS);
  419. rb.PushCopyObjects(accumulated_suspended_tick_changed_event.readable);
  420. }
  421. AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) {
  422. on_new_message = Kernel::WritableEvent::CreateEventPair(kernel, Kernel::ResetType::Manual,
  423. "AMMessageQueue:OnMessageRecieved");
  424. on_operation_mode_changed = Kernel::WritableEvent::CreateEventPair(
  425. kernel, Kernel::ResetType::Automatic, "AMMessageQueue:OperationModeChanged");
  426. }
  427. AppletMessageQueue::~AppletMessageQueue() = default;
  428. const Kernel::SharedPtr<Kernel::ReadableEvent>& AppletMessageQueue::GetMesssageRecieveEvent()
  429. const {
  430. return on_new_message.readable;
  431. }
  432. const Kernel::SharedPtr<Kernel::ReadableEvent>& AppletMessageQueue::GetOperationModeChangedEvent()
  433. const {
  434. return on_operation_mode_changed.readable;
  435. }
  436. void AppletMessageQueue::PushMessage(AppletMessage msg) {
  437. messages.push(msg);
  438. on_new_message.writable->Signal();
  439. }
  440. AppletMessageQueue::AppletMessage AppletMessageQueue::PopMessage() {
  441. if (messages.empty()) {
  442. on_new_message.writable->Clear();
  443. return AppletMessage::NoMessage;
  444. }
  445. auto msg = messages.front();
  446. messages.pop();
  447. if (messages.empty()) {
  448. on_new_message.writable->Clear();
  449. }
  450. return msg;
  451. }
  452. std::size_t AppletMessageQueue::GetMessageCount() const {
  453. return messages.size();
  454. }
  455. void AppletMessageQueue::OperationModeChanged() {
  456. PushMessage(AppletMessage::OperationModeChanged);
  457. PushMessage(AppletMessage::PerformanceModeChanged);
  458. on_operation_mode_changed.writable->Signal();
  459. }
  460. void AppletMessageQueue::RequestExit() {
  461. PushMessage(AppletMessage::ExitRequested);
  462. }
  463. ICommonStateGetter::ICommonStateGetter(Core::System& system,
  464. std::shared_ptr<AppletMessageQueue> msg_queue)
  465. : ServiceFramework("ICommonStateGetter"), system(system), msg_queue(std::move(msg_queue)) {
  466. // clang-format off
  467. static const FunctionInfo functions[] = {
  468. {0, &ICommonStateGetter::GetEventHandle, "GetEventHandle"},
  469. {1, &ICommonStateGetter::ReceiveMessage, "ReceiveMessage"},
  470. {2, nullptr, "GetThisAppletKind"},
  471. {3, nullptr, "AllowToEnterSleep"},
  472. {4, nullptr, "DisallowToEnterSleep"},
  473. {5, &ICommonStateGetter::GetOperationMode, "GetOperationMode"},
  474. {6, &ICommonStateGetter::GetPerformanceMode, "GetPerformanceMode"},
  475. {7, nullptr, "GetCradleStatus"},
  476. {8, &ICommonStateGetter::GetBootMode, "GetBootMode"},
  477. {9, &ICommonStateGetter::GetCurrentFocusState, "GetCurrentFocusState"},
  478. {10, nullptr, "RequestToAcquireSleepLock"},
  479. {11, nullptr, "ReleaseSleepLock"},
  480. {12, nullptr, "ReleaseSleepLockTransiently"},
  481. {13, nullptr, "GetAcquiredSleepLockEvent"},
  482. {20, nullptr, "PushToGeneralChannel"},
  483. {30, nullptr, "GetHomeButtonReaderLockAccessor"},
  484. {31, nullptr, "GetReaderLockAccessorEx"},
  485. {40, nullptr, "GetCradleFwVersion"},
  486. {50, nullptr, "IsVrModeEnabled"},
  487. {51, nullptr, "SetVrModeEnabled"},
  488. {52, nullptr, "SwitchLcdBacklight"},
  489. {53, nullptr, "BeginVrModeEx"},
  490. {54, nullptr, "EndVrModeEx"},
  491. {55, nullptr, "IsInControllerFirmwareUpdateSection"},
  492. {60, &ICommonStateGetter::GetDefaultDisplayResolution, "GetDefaultDisplayResolution"},
  493. {61, &ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent, "GetDefaultDisplayResolutionChangeEvent"},
  494. {62, nullptr, "GetHdcpAuthenticationState"},
  495. {63, nullptr, "GetHdcpAuthenticationStateChangeEvent"},
  496. {64, nullptr, "SetTvPowerStateMatchingMode"},
  497. {65, nullptr, "GetApplicationIdByContentActionName"},
  498. {66, &ICommonStateGetter::SetCpuBoostMode, "SetCpuBoostMode"},
  499. {80, nullptr, "PerformSystemButtonPressingIfInFocus"},
  500. {90, nullptr, "SetPerformanceConfigurationChangedNotification"},
  501. {91, nullptr, "GetCurrentPerformanceConfiguration"},
  502. {200, nullptr, "GetOperationModeSystemInfo"},
  503. };
  504. // clang-format on
  505. RegisterHandlers(functions);
  506. }
  507. ICommonStateGetter::~ICommonStateGetter() = default;
  508. void ICommonStateGetter::GetBootMode(Kernel::HLERequestContext& ctx) {
  509. LOG_DEBUG(Service_AM, "called");
  510. IPC::ResponseBuilder rb{ctx, 3};
  511. rb.Push(RESULT_SUCCESS);
  512. rb.Push<u8>(static_cast<u8>(Service::PM::SystemBootMode::Normal)); // Normal boot mode
  513. }
  514. void ICommonStateGetter::GetEventHandle(Kernel::HLERequestContext& ctx) {
  515. LOG_DEBUG(Service_AM, "called");
  516. IPC::ResponseBuilder rb{ctx, 2, 1};
  517. rb.Push(RESULT_SUCCESS);
  518. rb.PushCopyObjects(msg_queue->GetMesssageRecieveEvent());
  519. }
  520. void ICommonStateGetter::ReceiveMessage(Kernel::HLERequestContext& ctx) {
  521. LOG_DEBUG(Service_AM, "called");
  522. const auto message = msg_queue->PopMessage();
  523. IPC::ResponseBuilder rb{ctx, 3};
  524. if (message == AppletMessageQueue::AppletMessage::NoMessage) {
  525. LOG_ERROR(Service_AM, "Message queue is empty");
  526. rb.Push(ERR_NO_MESSAGES);
  527. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  528. return;
  529. }
  530. rb.Push(RESULT_SUCCESS);
  531. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  532. }
  533. void ICommonStateGetter::GetCurrentFocusState(Kernel::HLERequestContext& ctx) {
  534. LOG_WARNING(Service_AM, "(STUBBED) called");
  535. IPC::ResponseBuilder rb{ctx, 3};
  536. rb.Push(RESULT_SUCCESS);
  537. rb.Push(static_cast<u8>(FocusState::InFocus));
  538. }
  539. void ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent(Kernel::HLERequestContext& ctx) {
  540. LOG_DEBUG(Service_AM, "called");
  541. IPC::ResponseBuilder rb{ctx, 2, 1};
  542. rb.Push(RESULT_SUCCESS);
  543. rb.PushCopyObjects(msg_queue->GetOperationModeChangedEvent());
  544. }
  545. void ICommonStateGetter::GetDefaultDisplayResolution(Kernel::HLERequestContext& ctx) {
  546. LOG_DEBUG(Service_AM, "called");
  547. IPC::ResponseBuilder rb{ctx, 4};
  548. rb.Push(RESULT_SUCCESS);
  549. if (Settings::values.use_docked_mode) {
  550. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedWidth) *
  551. static_cast<u32>(Settings::values.resolution_factor));
  552. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedHeight) *
  553. static_cast<u32>(Settings::values.resolution_factor));
  554. } else {
  555. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedWidth) *
  556. static_cast<u32>(Settings::values.resolution_factor));
  557. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedHeight) *
  558. static_cast<u32>(Settings::values.resolution_factor));
  559. }
  560. }
  561. void ICommonStateGetter::SetCpuBoostMode(Kernel::HLERequestContext& ctx) {
  562. LOG_DEBUG(Service_AM, "called, forwarding to APM:SYS");
  563. const auto& sm = system.ServiceManager();
  564. const auto apm_sys = sm.GetService<APM::APM_Sys>("apm:sys");
  565. ASSERT(apm_sys != nullptr);
  566. apm_sys->SetCpuBoostMode(ctx);
  567. }
  568. IStorage::IStorage(std::vector<u8> buffer)
  569. : ServiceFramework("IStorage"), buffer(std::move(buffer)) {
  570. // clang-format off
  571. static const FunctionInfo functions[] = {
  572. {0, &IStorage::Open, "Open"},
  573. {1, nullptr, "OpenTransferStorage"},
  574. };
  575. // clang-format on
  576. RegisterHandlers(functions);
  577. }
  578. IStorage::~IStorage() = default;
  579. const std::vector<u8>& IStorage::GetData() const {
  580. return buffer;
  581. }
  582. void ICommonStateGetter::GetOperationMode(Kernel::HLERequestContext& ctx) {
  583. const bool use_docked_mode{Settings::values.use_docked_mode};
  584. LOG_DEBUG(Service_AM, "called, use_docked_mode={}", use_docked_mode);
  585. IPC::ResponseBuilder rb{ctx, 3};
  586. rb.Push(RESULT_SUCCESS);
  587. rb.Push(static_cast<u8>(use_docked_mode ? OperationMode::Docked : OperationMode::Handheld));
  588. }
  589. void ICommonStateGetter::GetPerformanceMode(Kernel::HLERequestContext& ctx) {
  590. LOG_DEBUG(Service_AM, "called");
  591. IPC::ResponseBuilder rb{ctx, 3};
  592. rb.Push(RESULT_SUCCESS);
  593. rb.PushEnum(system.GetAPMController().GetCurrentPerformanceMode());
  594. }
  595. class ILibraryAppletAccessor final : public ServiceFramework<ILibraryAppletAccessor> {
  596. public:
  597. explicit ILibraryAppletAccessor(std::shared_ptr<Applets::Applet> applet)
  598. : ServiceFramework("ILibraryAppletAccessor"), applet(std::move(applet)) {
  599. // clang-format off
  600. static const FunctionInfo functions[] = {
  601. {0, &ILibraryAppletAccessor::GetAppletStateChangedEvent, "GetAppletStateChangedEvent"},
  602. {1, &ILibraryAppletAccessor::IsCompleted, "IsCompleted"},
  603. {10, &ILibraryAppletAccessor::Start, "Start"},
  604. {20, nullptr, "RequestExit"},
  605. {25, nullptr, "Terminate"},
  606. {30, &ILibraryAppletAccessor::GetResult, "GetResult"},
  607. {50, nullptr, "SetOutOfFocusApplicationSuspendingEnabled"},
  608. {100, &ILibraryAppletAccessor::PushInData, "PushInData"},
  609. {101, &ILibraryAppletAccessor::PopOutData, "PopOutData"},
  610. {102, nullptr, "PushExtraStorage"},
  611. {103, &ILibraryAppletAccessor::PushInteractiveInData, "PushInteractiveInData"},
  612. {104, &ILibraryAppletAccessor::PopInteractiveOutData, "PopInteractiveOutData"},
  613. {105, &ILibraryAppletAccessor::GetPopOutDataEvent, "GetPopOutDataEvent"},
  614. {106, &ILibraryAppletAccessor::GetPopInteractiveOutDataEvent, "GetPopInteractiveOutDataEvent"},
  615. {110, nullptr, "NeedsToExitProcess"},
  616. {120, nullptr, "GetLibraryAppletInfo"},
  617. {150, nullptr, "RequestForAppletToGetForeground"},
  618. {160, nullptr, "GetIndirectLayerConsumerHandle"},
  619. };
  620. // clang-format on
  621. RegisterHandlers(functions);
  622. }
  623. private:
  624. void GetAppletStateChangedEvent(Kernel::HLERequestContext& ctx) {
  625. LOG_DEBUG(Service_AM, "called");
  626. const auto event = applet->GetBroker().GetStateChangedEvent();
  627. IPC::ResponseBuilder rb{ctx, 2, 1};
  628. rb.Push(RESULT_SUCCESS);
  629. rb.PushCopyObjects(event);
  630. }
  631. void IsCompleted(Kernel::HLERequestContext& ctx) {
  632. LOG_DEBUG(Service_AM, "called");
  633. IPC::ResponseBuilder rb{ctx, 3};
  634. rb.Push(RESULT_SUCCESS);
  635. rb.Push<u32>(applet->TransactionComplete());
  636. }
  637. void GetResult(Kernel::HLERequestContext& ctx) {
  638. LOG_DEBUG(Service_AM, "called");
  639. IPC::ResponseBuilder rb{ctx, 2};
  640. rb.Push(applet->GetStatus());
  641. }
  642. void Start(Kernel::HLERequestContext& ctx) {
  643. LOG_DEBUG(Service_AM, "called");
  644. ASSERT(applet != nullptr);
  645. applet->Initialize();
  646. applet->Execute();
  647. IPC::ResponseBuilder rb{ctx, 2};
  648. rb.Push(RESULT_SUCCESS);
  649. }
  650. void PushInData(Kernel::HLERequestContext& ctx) {
  651. LOG_DEBUG(Service_AM, "called");
  652. IPC::RequestParser rp{ctx};
  653. applet->GetBroker().PushNormalDataFromGame(*rp.PopIpcInterface<IStorage>());
  654. IPC::ResponseBuilder rb{ctx, 2};
  655. rb.Push(RESULT_SUCCESS);
  656. }
  657. void PopOutData(Kernel::HLERequestContext& ctx) {
  658. LOG_DEBUG(Service_AM, "called");
  659. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  660. const auto storage = applet->GetBroker().PopNormalDataToGame();
  661. if (storage == nullptr) {
  662. LOG_ERROR(Service_AM,
  663. "storage is a nullptr. There is no data in the current normal channel");
  664. rb.Push(ERR_NO_DATA_IN_CHANNEL);
  665. return;
  666. }
  667. rb.Push(RESULT_SUCCESS);
  668. rb.PushIpcInterface<IStorage>(std::move(*storage));
  669. }
  670. void PushInteractiveInData(Kernel::HLERequestContext& ctx) {
  671. LOG_DEBUG(Service_AM, "called");
  672. IPC::RequestParser rp{ctx};
  673. applet->GetBroker().PushInteractiveDataFromGame(*rp.PopIpcInterface<IStorage>());
  674. ASSERT(applet->IsInitialized());
  675. applet->ExecuteInteractive();
  676. applet->Execute();
  677. IPC::ResponseBuilder rb{ctx, 2};
  678. rb.Push(RESULT_SUCCESS);
  679. }
  680. void PopInteractiveOutData(Kernel::HLERequestContext& ctx) {
  681. LOG_DEBUG(Service_AM, "called");
  682. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  683. const auto storage = applet->GetBroker().PopInteractiveDataToGame();
  684. if (storage == nullptr) {
  685. LOG_ERROR(Service_AM,
  686. "storage is a nullptr. There is no data in the current interactive channel");
  687. rb.Push(ERR_NO_DATA_IN_CHANNEL);
  688. return;
  689. }
  690. rb.Push(RESULT_SUCCESS);
  691. rb.PushIpcInterface<IStorage>(std::move(*storage));
  692. }
  693. void GetPopOutDataEvent(Kernel::HLERequestContext& ctx) {
  694. LOG_DEBUG(Service_AM, "called");
  695. IPC::ResponseBuilder rb{ctx, 2, 1};
  696. rb.Push(RESULT_SUCCESS);
  697. rb.PushCopyObjects(applet->GetBroker().GetNormalDataEvent());
  698. }
  699. void GetPopInteractiveOutDataEvent(Kernel::HLERequestContext& ctx) {
  700. LOG_DEBUG(Service_AM, "called");
  701. IPC::ResponseBuilder rb{ctx, 2, 1};
  702. rb.Push(RESULT_SUCCESS);
  703. rb.PushCopyObjects(applet->GetBroker().GetInteractiveDataEvent());
  704. }
  705. std::shared_ptr<Applets::Applet> applet;
  706. };
  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>(*this);
  712. }
  713. IStorageAccessor::IStorageAccessor(IStorage& storage)
  714. : ServiceFramework("IStorageAccessor"), backing(storage) {
  715. // clang-format off
  716. static const FunctionInfo functions[] = {
  717. {0, &IStorageAccessor::GetSize, "GetSize"},
  718. {10, &IStorageAccessor::Write, "Write"},
  719. {11, &IStorageAccessor::Read, "Read"},
  720. };
  721. // clang-format on
  722. RegisterHandlers(functions);
  723. }
  724. IStorageAccessor::~IStorageAccessor() = default;
  725. void IStorageAccessor::GetSize(Kernel::HLERequestContext& ctx) {
  726. LOG_DEBUG(Service_AM, "called");
  727. IPC::ResponseBuilder rb{ctx, 4};
  728. rb.Push(RESULT_SUCCESS);
  729. rb.Push(static_cast<u64>(backing.buffer.size()));
  730. }
  731. void IStorageAccessor::Write(Kernel::HLERequestContext& ctx) {
  732. IPC::RequestParser rp{ctx};
  733. const u64 offset{rp.Pop<u64>()};
  734. const std::vector<u8> data{ctx.ReadBuffer()};
  735. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, data.size());
  736. if (data.size() > backing.buffer.size() - offset) {
  737. LOG_ERROR(Service_AM,
  738. "offset is out of bounds, backing_buffer_sz={}, data_size={}, offset={}",
  739. backing.buffer.size(), data.size(), offset);
  740. IPC::ResponseBuilder rb{ctx, 2};
  741. rb.Push(ERR_SIZE_OUT_OF_BOUNDS);
  742. return;
  743. }
  744. std::memcpy(backing.buffer.data() + offset, data.data(), data.size());
  745. IPC::ResponseBuilder rb{ctx, 2};
  746. rb.Push(RESULT_SUCCESS);
  747. }
  748. void IStorageAccessor::Read(Kernel::HLERequestContext& ctx) {
  749. IPC::RequestParser rp{ctx};
  750. const u64 offset{rp.Pop<u64>()};
  751. const std::size_t size{ctx.GetWriteBufferSize()};
  752. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  753. if (size > backing.buffer.size() - offset) {
  754. LOG_ERROR(Service_AM, "offset is out of bounds, backing_buffer_sz={}, size={}, offset={}",
  755. backing.buffer.size(), size, offset);
  756. IPC::ResponseBuilder rb{ctx, 2};
  757. rb.Push(ERR_SIZE_OUT_OF_BOUNDS);
  758. return;
  759. }
  760. ctx.WriteBuffer(backing.buffer.data() + offset, size);
  761. IPC::ResponseBuilder rb{ctx, 2};
  762. rb.Push(RESULT_SUCCESS);
  763. }
  764. ILibraryAppletCreator::ILibraryAppletCreator(Core::System& system_)
  765. : ServiceFramework("ILibraryAppletCreator"), system{system_} {
  766. static const FunctionInfo functions[] = {
  767. {0, &ILibraryAppletCreator::CreateLibraryApplet, "CreateLibraryApplet"},
  768. {1, nullptr, "TerminateAllLibraryApplets"},
  769. {2, nullptr, "AreAnyLibraryAppletsLeft"},
  770. {10, &ILibraryAppletCreator::CreateStorage, "CreateStorage"},
  771. {11, &ILibraryAppletCreator::CreateTransferMemoryStorage, "CreateTransferMemoryStorage"},
  772. {12, nullptr, "CreateHandleStorage"},
  773. };
  774. RegisterHandlers(functions);
  775. }
  776. ILibraryAppletCreator::~ILibraryAppletCreator() = default;
  777. void ILibraryAppletCreator::CreateLibraryApplet(Kernel::HLERequestContext& ctx) {
  778. IPC::RequestParser rp{ctx};
  779. const auto applet_id = rp.PopRaw<Applets::AppletId>();
  780. const auto applet_mode = rp.PopRaw<u32>();
  781. LOG_DEBUG(Service_AM, "called with applet_id={:08X}, applet_mode={:08X}",
  782. static_cast<u32>(applet_id), applet_mode);
  783. const auto& applet_manager{system.GetAppletManager()};
  784. const auto applet = applet_manager.GetApplet(applet_id);
  785. if (applet == nullptr) {
  786. LOG_ERROR(Service_AM, "Applet doesn't exist! applet_id={}", static_cast<u32>(applet_id));
  787. IPC::ResponseBuilder rb{ctx, 2};
  788. rb.Push(ResultCode(-1));
  789. return;
  790. }
  791. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  792. rb.Push(RESULT_SUCCESS);
  793. rb.PushIpcInterface<AM::ILibraryAppletAccessor>(applet);
  794. }
  795. void ILibraryAppletCreator::CreateStorage(Kernel::HLERequestContext& ctx) {
  796. IPC::RequestParser rp{ctx};
  797. const u64 size{rp.Pop<u64>()};
  798. LOG_DEBUG(Service_AM, "called, size={}", size);
  799. std::vector<u8> buffer(size);
  800. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  801. rb.Push(RESULT_SUCCESS);
  802. rb.PushIpcInterface<AM::IStorage>(std::move(buffer));
  803. }
  804. void ILibraryAppletCreator::CreateTransferMemoryStorage(Kernel::HLERequestContext& ctx) {
  805. LOG_DEBUG(Service_AM, "called");
  806. IPC::RequestParser rp{ctx};
  807. rp.SetCurrentOffset(3);
  808. const auto handle{rp.Pop<Kernel::Handle>()};
  809. const auto transfer_mem =
  810. system.CurrentProcess()->GetHandleTable().Get<Kernel::TransferMemory>(handle);
  811. if (transfer_mem == nullptr) {
  812. LOG_ERROR(Service_AM, "shared_mem is a nullpr for handle={:08X}", handle);
  813. IPC::ResponseBuilder rb{ctx, 2};
  814. rb.Push(ResultCode(-1));
  815. return;
  816. }
  817. const u8* const mem_begin = transfer_mem->GetPointer();
  818. const u8* const mem_end = mem_begin + transfer_mem->GetSize();
  819. std::vector<u8> memory{mem_begin, mem_end};
  820. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  821. rb.Push(RESULT_SUCCESS);
  822. rb.PushIpcInterface(std::make_shared<IStorage>(std::move(memory)));
  823. }
  824. IApplicationFunctions::IApplicationFunctions(Core::System& system_)
  825. : ServiceFramework("IApplicationFunctions"), system{system_} {
  826. // clang-format off
  827. static const FunctionInfo functions[] = {
  828. {1, &IApplicationFunctions::PopLaunchParameter, "PopLaunchParameter"},
  829. {10, nullptr, "CreateApplicationAndPushAndRequestToStart"},
  830. {11, nullptr, "CreateApplicationAndPushAndRequestToStartForQuest"},
  831. {12, nullptr, "CreateApplicationAndRequestToStart"},
  832. {13, &IApplicationFunctions::CreateApplicationAndRequestToStartForQuest, "CreateApplicationAndRequestToStartForQuest"},
  833. {14, nullptr, "CreateApplicationWithAttributeAndPushAndRequestToStartForQuest"},
  834. {15, nullptr, "CreateApplicationWithAttributeAndRequestToStartForQuest"},
  835. {20, &IApplicationFunctions::EnsureSaveData, "EnsureSaveData"},
  836. {21, &IApplicationFunctions::GetDesiredLanguage, "GetDesiredLanguage"},
  837. {22, &IApplicationFunctions::SetTerminateResult, "SetTerminateResult"},
  838. {23, &IApplicationFunctions::GetDisplayVersion, "GetDisplayVersion"},
  839. {24, nullptr, "GetLaunchStorageInfoForDebug"},
  840. {25, &IApplicationFunctions::ExtendSaveData, "ExtendSaveData"},
  841. {26, &IApplicationFunctions::GetSaveDataSize, "GetSaveDataSize"},
  842. {30, &IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed, "BeginBlockingHomeButtonShortAndLongPressed"},
  843. {31, &IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed, "EndBlockingHomeButtonShortAndLongPressed"},
  844. {32, &IApplicationFunctions::BeginBlockingHomeButton, "BeginBlockingHomeButton"},
  845. {33, &IApplicationFunctions::EndBlockingHomeButton, "EndBlockingHomeButton"},
  846. {40, &IApplicationFunctions::NotifyRunning, "NotifyRunning"},
  847. {50, &IApplicationFunctions::GetPseudoDeviceId, "GetPseudoDeviceId"},
  848. {60, nullptr, "SetMediaPlaybackStateForApplication"},
  849. {65, nullptr, "IsGamePlayRecordingSupported"},
  850. {66, &IApplicationFunctions::InitializeGamePlayRecording, "InitializeGamePlayRecording"},
  851. {67, &IApplicationFunctions::SetGamePlayRecordingState, "SetGamePlayRecordingState"},
  852. {68, nullptr, "RequestFlushGamePlayingMovieForDebug"},
  853. {70, nullptr, "RequestToShutdown"},
  854. {71, nullptr, "RequestToReboot"},
  855. {80, nullptr, "ExitAndRequestToShowThanksMessage"},
  856. {90, &IApplicationFunctions::EnableApplicationCrashReport, "EnableApplicationCrashReport"},
  857. {100, nullptr, "InitializeApplicationCopyrightFrameBuffer"},
  858. {101, nullptr, "SetApplicationCopyrightImage"},
  859. {102, nullptr, "SetApplicationCopyrightVisibility"},
  860. {110, nullptr, "QueryApplicationPlayStatistics"},
  861. {120, nullptr, "ExecuteProgram"},
  862. {121, nullptr, "ClearUserChannel"},
  863. {122, nullptr, "UnpopToUserChannel"},
  864. {130, &IApplicationFunctions::GetGpuErrorDetectedSystemEvent, "GetGpuErrorDetectedSystemEvent"},
  865. {500, nullptr, "StartContinuousRecordingFlushForDebug"},
  866. {1000, nullptr, "CreateMovieMaker"},
  867. {1001, nullptr, "PrepareForJit"},
  868. };
  869. // clang-format on
  870. RegisterHandlers(functions);
  871. auto& kernel = Core::System::GetInstance().Kernel();
  872. gpu_error_detected_event = Kernel::WritableEvent::CreateEventPair(
  873. kernel, Kernel::ResetType::Manual, "IApplicationFunctions:GpuErrorDetectedSystemEvent");
  874. }
  875. IApplicationFunctions::~IApplicationFunctions() = default;
  876. void IApplicationFunctions::EnableApplicationCrashReport(Kernel::HLERequestContext& ctx) {
  877. LOG_WARNING(Service_AM, "(STUBBED) called");
  878. IPC::ResponseBuilder rb{ctx, 2};
  879. rb.Push(RESULT_SUCCESS);
  880. }
  881. void IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed(
  882. Kernel::HLERequestContext& ctx) {
  883. LOG_WARNING(Service_AM, "(STUBBED) called");
  884. IPC::ResponseBuilder rb{ctx, 2};
  885. rb.Push(RESULT_SUCCESS);
  886. }
  887. void IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed(
  888. Kernel::HLERequestContext& ctx) {
  889. LOG_WARNING(Service_AM, "(STUBBED) called");
  890. IPC::ResponseBuilder rb{ctx, 2};
  891. rb.Push(RESULT_SUCCESS);
  892. }
  893. void IApplicationFunctions::BeginBlockingHomeButton(Kernel::HLERequestContext& ctx) {
  894. LOG_WARNING(Service_AM, "(STUBBED) called");
  895. IPC::ResponseBuilder rb{ctx, 2};
  896. rb.Push(RESULT_SUCCESS);
  897. }
  898. void IApplicationFunctions::EndBlockingHomeButton(Kernel::HLERequestContext& ctx) {
  899. LOG_WARNING(Service_AM, "(STUBBED) called");
  900. IPC::ResponseBuilder rb{ctx, 2};
  901. rb.Push(RESULT_SUCCESS);
  902. }
  903. void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) {
  904. LOG_DEBUG(Service_AM, "called");
  905. LaunchParameters params{};
  906. params.magic = POP_LAUNCH_PARAMETER_MAGIC;
  907. params.is_account_selected = 1;
  908. Account::ProfileManager profile_manager{};
  909. const auto uuid = profile_manager.GetUser(Settings::values.current_user);
  910. ASSERT(uuid);
  911. params.current_user = uuid->uuid;
  912. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  913. rb.Push(RESULT_SUCCESS);
  914. std::vector<u8> buffer(sizeof(LaunchParameters));
  915. std::memcpy(buffer.data(), &params, buffer.size());
  916. rb.PushIpcInterface<AM::IStorage>(buffer);
  917. }
  918. void IApplicationFunctions::CreateApplicationAndRequestToStartForQuest(
  919. Kernel::HLERequestContext& ctx) {
  920. LOG_WARNING(Service_AM, "(STUBBED) called");
  921. IPC::ResponseBuilder rb{ctx, 2};
  922. rb.Push(RESULT_SUCCESS);
  923. }
  924. void IApplicationFunctions::EnsureSaveData(Kernel::HLERequestContext& ctx) {
  925. IPC::RequestParser rp{ctx};
  926. u128 user_id = rp.PopRaw<u128>();
  927. LOG_DEBUG(Service_AM, "called, uid={:016X}{:016X}", user_id[1], user_id[0]);
  928. FileSys::SaveDataDescriptor descriptor{};
  929. descriptor.title_id = Core::CurrentProcess()->GetTitleID();
  930. descriptor.user_id = user_id;
  931. descriptor.type = FileSys::SaveDataType::SaveData;
  932. const auto res = system.GetFileSystemController().CreateSaveData(
  933. FileSys::SaveDataSpaceId::NandUser, descriptor);
  934. IPC::ResponseBuilder rb{ctx, 4};
  935. rb.Push(res.Code());
  936. rb.Push<u64>(0);
  937. }
  938. void IApplicationFunctions::SetTerminateResult(Kernel::HLERequestContext& ctx) {
  939. // Takes an input u32 Result, no output.
  940. // For example, in some cases official apps use this with error 0x2A2 then
  941. // uses svcBreak.
  942. IPC::RequestParser rp{ctx};
  943. u32 result = rp.Pop<u32>();
  944. LOG_WARNING(Service_AM, "(STUBBED) called, result=0x{:08X}", result);
  945. IPC::ResponseBuilder rb{ctx, 2};
  946. rb.Push(RESULT_SUCCESS);
  947. }
  948. void IApplicationFunctions::GetDisplayVersion(Kernel::HLERequestContext& ctx) {
  949. LOG_WARNING(Service_AM, "(STUBBED) called");
  950. IPC::ResponseBuilder rb{ctx, 6};
  951. rb.Push(RESULT_SUCCESS);
  952. rb.Push<u64>(1);
  953. rb.Push<u64>(0);
  954. }
  955. void IApplicationFunctions::GetDesiredLanguage(Kernel::HLERequestContext& ctx) {
  956. // TODO(bunnei): This should be configurable
  957. LOG_DEBUG(Service_AM, "called");
  958. // Get supported languages from NACP, if possible
  959. // Default to 0 (all languages supported)
  960. u32 supported_languages = 0;
  961. FileSys::PatchManager pm{system.CurrentProcess()->GetTitleID()};
  962. const auto res = pm.GetControlMetadata();
  963. if (res.first != nullptr) {
  964. supported_languages = res.first->GetSupportedLanguages();
  965. }
  966. // Call IApplicationManagerInterface implementation.
  967. auto& service_manager = system.ServiceManager();
  968. auto ns_am2 = service_manager.GetService<NS::NS>("ns:am2");
  969. auto app_man = ns_am2->GetApplicationManagerInterface();
  970. // Get desired application language
  971. const auto res_lang = app_man->GetApplicationDesiredLanguage(supported_languages);
  972. if (res_lang.Failed()) {
  973. IPC::ResponseBuilder rb{ctx, 2};
  974. rb.Push(res_lang.Code());
  975. return;
  976. }
  977. // Convert to settings language code.
  978. const auto res_code = app_man->ConvertApplicationLanguageToLanguageCode(*res_lang);
  979. if (res_code.Failed()) {
  980. IPC::ResponseBuilder rb{ctx, 2};
  981. rb.Push(res_code.Code());
  982. return;
  983. }
  984. LOG_DEBUG(Service_AM, "got desired_language={:016X}", *res_code);
  985. IPC::ResponseBuilder rb{ctx, 4};
  986. rb.Push(RESULT_SUCCESS);
  987. rb.Push(*res_code);
  988. }
  989. void IApplicationFunctions::InitializeGamePlayRecording(Kernel::HLERequestContext& ctx) {
  990. LOG_WARNING(Service_AM, "(STUBBED) called");
  991. IPC::ResponseBuilder rb{ctx, 2};
  992. rb.Push(RESULT_SUCCESS);
  993. }
  994. void IApplicationFunctions::SetGamePlayRecordingState(Kernel::HLERequestContext& ctx) {
  995. LOG_WARNING(Service_AM, "(STUBBED) called");
  996. IPC::ResponseBuilder rb{ctx, 2};
  997. rb.Push(RESULT_SUCCESS);
  998. }
  999. void IApplicationFunctions::NotifyRunning(Kernel::HLERequestContext& ctx) {
  1000. LOG_WARNING(Service_AM, "(STUBBED) called");
  1001. IPC::ResponseBuilder rb{ctx, 3};
  1002. rb.Push(RESULT_SUCCESS);
  1003. rb.Push<u8>(0); // Unknown, seems to be ignored by official processes
  1004. }
  1005. void IApplicationFunctions::GetPseudoDeviceId(Kernel::HLERequestContext& ctx) {
  1006. LOG_WARNING(Service_AM, "(STUBBED) called");
  1007. IPC::ResponseBuilder rb{ctx, 6};
  1008. rb.Push(RESULT_SUCCESS);
  1009. // Returns a 128-bit UUID
  1010. rb.Push<u64>(0);
  1011. rb.Push<u64>(0);
  1012. }
  1013. void IApplicationFunctions::ExtendSaveData(Kernel::HLERequestContext& ctx) {
  1014. IPC::RequestParser rp{ctx};
  1015. const auto type{rp.PopRaw<FileSys::SaveDataType>()};
  1016. rp.Skip(1, false);
  1017. const auto user_id{rp.PopRaw<u128>()};
  1018. const auto new_normal_size{rp.PopRaw<u64>()};
  1019. const auto new_journal_size{rp.PopRaw<u64>()};
  1020. LOG_DEBUG(Service_AM,
  1021. "called with type={:02X}, user_id={:016X}{:016X}, new_normal={:016X}, "
  1022. "new_journal={:016X}",
  1023. static_cast<u8>(type), user_id[1], user_id[0], new_normal_size, new_journal_size);
  1024. system.GetFileSystemController().WriteSaveDataSize(
  1025. type, system.CurrentProcess()->GetTitleID(), user_id, {new_normal_size, new_journal_size});
  1026. IPC::ResponseBuilder rb{ctx, 4};
  1027. rb.Push(RESULT_SUCCESS);
  1028. // The following value is used upon failure to help the system recover.
  1029. // Since we always succeed, this should be 0.
  1030. rb.Push<u64>(0);
  1031. }
  1032. void IApplicationFunctions::GetSaveDataSize(Kernel::HLERequestContext& ctx) {
  1033. IPC::RequestParser rp{ctx};
  1034. const auto type{rp.PopRaw<FileSys::SaveDataType>()};
  1035. rp.Skip(1, false);
  1036. const auto user_id{rp.PopRaw<u128>()};
  1037. LOG_DEBUG(Service_AM, "called with type={:02X}, user_id={:016X}{:016X}", static_cast<u8>(type),
  1038. user_id[1], user_id[0]);
  1039. const auto size = system.GetFileSystemController().ReadSaveDataSize(
  1040. type, system.CurrentProcess()->GetTitleID(), user_id);
  1041. IPC::ResponseBuilder rb{ctx, 6};
  1042. rb.Push(RESULT_SUCCESS);
  1043. rb.Push(size.normal);
  1044. rb.Push(size.journal);
  1045. }
  1046. void IApplicationFunctions::GetGpuErrorDetectedSystemEvent(Kernel::HLERequestContext& ctx) {
  1047. LOG_WARNING(Service_AM, "(STUBBED) called");
  1048. IPC::ResponseBuilder rb{ctx, 2, 1};
  1049. rb.Push(RESULT_SUCCESS);
  1050. rb.PushCopyObjects(gpu_error_detected_event.readable);
  1051. }
  1052. void InstallInterfaces(SM::ServiceManager& service_manager,
  1053. std::shared_ptr<NVFlinger::NVFlinger> nvflinger, Core::System& system) {
  1054. auto message_queue = std::make_shared<AppletMessageQueue>(system.Kernel());
  1055. // Needed on game boot
  1056. message_queue->PushMessage(AppletMessageQueue::AppletMessage::FocusStateChanged);
  1057. std::make_shared<AppletAE>(nvflinger, message_queue, system)->InstallAsService(service_manager);
  1058. std::make_shared<AppletOE>(nvflinger, message_queue, system)->InstallAsService(service_manager);
  1059. std::make_shared<IdleSys>()->InstallAsService(service_manager);
  1060. std::make_shared<OMM>()->InstallAsService(service_manager);
  1061. std::make_shared<SPSM>()->InstallAsService(service_manager);
  1062. std::make_shared<TCAP>()->InstallAsService(service_manager);
  1063. }
  1064. IHomeMenuFunctions::IHomeMenuFunctions() : ServiceFramework("IHomeMenuFunctions") {
  1065. // clang-format off
  1066. static const FunctionInfo functions[] = {
  1067. {10, &IHomeMenuFunctions::RequestToGetForeground, "RequestToGetForeground"},
  1068. {11, nullptr, "LockForeground"},
  1069. {12, nullptr, "UnlockForeground"},
  1070. {20, nullptr, "PopFromGeneralChannel"},
  1071. {21, nullptr, "GetPopFromGeneralChannelEvent"},
  1072. {30, nullptr, "GetHomeButtonWriterLockAccessor"},
  1073. {31, nullptr, "GetWriterLockAccessorEx"},
  1074. {100, nullptr, "PopRequestLaunchApplicationForDebug"},
  1075. };
  1076. // clang-format on
  1077. RegisterHandlers(functions);
  1078. }
  1079. IHomeMenuFunctions::~IHomeMenuFunctions() = default;
  1080. void IHomeMenuFunctions::RequestToGetForeground(Kernel::HLERequestContext& ctx) {
  1081. LOG_WARNING(Service_AM, "(STUBBED) called");
  1082. IPC::ResponseBuilder rb{ctx, 2};
  1083. rb.Push(RESULT_SUCCESS);
  1084. }
  1085. IGlobalStateController::IGlobalStateController() : ServiceFramework("IGlobalStateController") {
  1086. // clang-format off
  1087. static const FunctionInfo functions[] = {
  1088. {0, nullptr, "RequestToEnterSleep"},
  1089. {1, nullptr, "EnterSleep"},
  1090. {2, nullptr, "StartSleepSequence"},
  1091. {3, nullptr, "StartShutdownSequence"},
  1092. {4, nullptr, "StartRebootSequence"},
  1093. {9, nullptr, "IsAutoPowerDownRequested"},
  1094. {10, nullptr, "LoadAndApplyIdlePolicySettings"},
  1095. {11, nullptr, "NotifyCecSettingsChanged"},
  1096. {12, nullptr, "SetDefaultHomeButtonLongPressTime"},
  1097. {13, nullptr, "UpdateDefaultDisplayResolution"},
  1098. {14, nullptr, "ShouldSleepOnBoot"},
  1099. {15, nullptr, "GetHdcpAuthenticationFailedEvent"},
  1100. };
  1101. // clang-format on
  1102. RegisterHandlers(functions);
  1103. }
  1104. IGlobalStateController::~IGlobalStateController() = default;
  1105. IApplicationCreator::IApplicationCreator() : ServiceFramework("IApplicationCreator") {
  1106. // clang-format off
  1107. static const FunctionInfo functions[] = {
  1108. {0, nullptr, "CreateApplication"},
  1109. {1, nullptr, "PopLaunchRequestedApplication"},
  1110. {10, nullptr, "CreateSystemApplication"},
  1111. {100, nullptr, "PopFloatingApplicationForDevelopment"},
  1112. };
  1113. // clang-format on
  1114. RegisterHandlers(functions);
  1115. }
  1116. IApplicationCreator::~IApplicationCreator() = default;
  1117. IProcessWindingController::IProcessWindingController()
  1118. : ServiceFramework("IProcessWindingController") {
  1119. // clang-format off
  1120. static const FunctionInfo functions[] = {
  1121. {0, nullptr, "GetLaunchReason"},
  1122. {11, nullptr, "OpenCallingLibraryApplet"},
  1123. {21, nullptr, "PushContext"},
  1124. {22, nullptr, "PopContext"},
  1125. {23, nullptr, "CancelWindingReservation"},
  1126. {30, nullptr, "WindAndDoReserved"},
  1127. {40, nullptr, "ReserveToStartAndWaitAndUnwindThis"},
  1128. {41, nullptr, "ReserveToStartAndWait"},
  1129. };
  1130. // clang-format on
  1131. RegisterHandlers(functions);
  1132. }
  1133. IProcessWindingController::~IProcessWindingController() = default;
  1134. } // namespace Service::AM