am.cpp 71 KB

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