am.cpp 59 KB

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