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