am.cpp 66 KB

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