am.cpp 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <cinttypes>
  6. #include <cstring>
  7. #include "common/settings.h"
  8. #include "common/settings_enums.h"
  9. #include "core/core.h"
  10. #include "core/core_timing.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/kernel/k_event.h"
  16. #include "core/hle/kernel/k_transfer_memory.h"
  17. #include "core/hle/result.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_cabinet.h"
  23. #include "core/hle/service/am/applets/applet_controller.h"
  24. #include "core/hle/service/am/applets/applet_mii_edit_types.h"
  25. #include "core/hle/service/am/applets/applet_profile_select.h"
  26. #include "core/hle/service/am/applets/applet_software_keyboard_types.h"
  27. #include "core/hle/service/am/applets/applet_web_browser.h"
  28. #include "core/hle/service/am/applets/applets.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/apm/apm_controller.h"
  33. #include "core/hle/service/apm/apm_interface.h"
  34. #include "core/hle/service/bcat/backend/backend.h"
  35. #include "core/hle/service/caps/caps_su.h"
  36. #include "core/hle/service/caps/caps_types.h"
  37. #include "core/hle/service/filesystem/filesystem.h"
  38. #include "core/hle/service/filesystem/save_data_controller.h"
  39. #include "core/hle/service/ipc_helpers.h"
  40. #include "core/hle/service/ns/ns.h"
  41. #include "core/hle/service/nvnflinger/fb_share_buffer_manager.h"
  42. #include "core/hle/service/nvnflinger/nvnflinger.h"
  43. #include "core/hle/service/pm/pm.h"
  44. #include "core/hle/service/server_manager.h"
  45. #include "core/hle/service/sm/sm.h"
  46. #include "core/hle/service/vi/vi.h"
  47. #include "core/hle/service/vi/vi_results.h"
  48. #include "core/memory.h"
  49. #include "hid_core/hid_types.h"
  50. #include "hid_core/resources/npad/npad.h"
  51. namespace Service::AM {
  52. constexpr Result ResultNoDataInChannel{ErrorModule::AM, 2};
  53. constexpr Result ResultNoMessages{ErrorModule::AM, 3};
  54. constexpr Result ResultInvalidOffset{ErrorModule::AM, 503};
  55. enum class LaunchParameterKind : u32 {
  56. UserChannel = 1,
  57. AccountPreselectedUser = 2,
  58. };
  59. constexpr u32 LAUNCH_PARAMETER_ACCOUNT_PRESELECTED_USER_MAGIC = 0xC79497CA;
  60. struct LaunchParameterAccountPreselectedUser {
  61. u32_le magic;
  62. u32_le is_account_selected;
  63. Common::UUID current_user;
  64. INSERT_PADDING_BYTES(0x70);
  65. };
  66. static_assert(sizeof(LaunchParameterAccountPreselectedUser) == 0x88);
  67. IWindowController::IWindowController(Core::System& system_)
  68. : ServiceFramework{system_, "IWindowController"} {
  69. // clang-format off
  70. static const FunctionInfo functions[] = {
  71. {0, nullptr, "CreateWindow"},
  72. {1, &IWindowController::GetAppletResourceUserId, "GetAppletResourceUserId"},
  73. {2, &IWindowController::GetAppletResourceUserIdOfCallerApplet, "GetAppletResourceUserIdOfCallerApplet"},
  74. {10, &IWindowController::AcquireForegroundRights, "AcquireForegroundRights"},
  75. {11, nullptr, "ReleaseForegroundRights"},
  76. {12, nullptr, "RejectToChangeIntoBackground"},
  77. {20, nullptr, "SetAppletWindowVisibility"},
  78. {21, nullptr, "SetAppletGpuTimeSlice"},
  79. };
  80. // clang-format on
  81. RegisterHandlers(functions);
  82. }
  83. IWindowController::~IWindowController() = default;
  84. void IWindowController::GetAppletResourceUserId(HLERequestContext& ctx) {
  85. const u64 process_id = system.ApplicationProcess()->GetProcessId();
  86. LOG_DEBUG(Service_AM, "called. Process ID=0x{:016X}", process_id);
  87. IPC::ResponseBuilder rb{ctx, 4};
  88. rb.Push(ResultSuccess);
  89. rb.Push<u64>(process_id);
  90. }
  91. void IWindowController::GetAppletResourceUserIdOfCallerApplet(HLERequestContext& ctx) {
  92. const u64 process_id = 0;
  93. LOG_WARNING(Service_AM, "(STUBBED) called");
  94. IPC::ResponseBuilder rb{ctx, 4};
  95. rb.Push(ResultSuccess);
  96. rb.Push<u64>(process_id);
  97. }
  98. void IWindowController::AcquireForegroundRights(HLERequestContext& ctx) {
  99. LOG_WARNING(Service_AM, "(STUBBED) called");
  100. IPC::ResponseBuilder rb{ctx, 2};
  101. rb.Push(ResultSuccess);
  102. }
  103. IAudioController::IAudioController(Core::System& system_)
  104. : ServiceFramework{system_, "IAudioController"} {
  105. // clang-format off
  106. static const FunctionInfo functions[] = {
  107. {0, &IAudioController::SetExpectedMasterVolume, "SetExpectedMasterVolume"},
  108. {1, &IAudioController::GetMainAppletExpectedMasterVolume, "GetMainAppletExpectedMasterVolume"},
  109. {2, &IAudioController::GetLibraryAppletExpectedMasterVolume, "GetLibraryAppletExpectedMasterVolume"},
  110. {3, &IAudioController::ChangeMainAppletMasterVolume, "ChangeMainAppletMasterVolume"},
  111. {4, &IAudioController::SetTransparentAudioRate, "SetTransparentVolumeRate"},
  112. };
  113. // clang-format on
  114. RegisterHandlers(functions);
  115. }
  116. IAudioController::~IAudioController() = default;
  117. void IAudioController::SetExpectedMasterVolume(HLERequestContext& ctx) {
  118. IPC::RequestParser rp{ctx};
  119. const float main_applet_volume_tmp = rp.Pop<float>();
  120. const float library_applet_volume_tmp = rp.Pop<float>();
  121. LOG_DEBUG(Service_AM, "called. main_applet_volume={}, library_applet_volume={}",
  122. main_applet_volume_tmp, library_applet_volume_tmp);
  123. // Ensure the volume values remain within the 0-100% range
  124. main_applet_volume = std::clamp(main_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  125. library_applet_volume =
  126. std::clamp(library_applet_volume_tmp, min_allowed_volume, max_allowed_volume);
  127. IPC::ResponseBuilder rb{ctx, 2};
  128. rb.Push(ResultSuccess);
  129. }
  130. void IAudioController::GetMainAppletExpectedMasterVolume(HLERequestContext& ctx) {
  131. LOG_DEBUG(Service_AM, "called. main_applet_volume={}", main_applet_volume);
  132. IPC::ResponseBuilder rb{ctx, 3};
  133. rb.Push(ResultSuccess);
  134. rb.Push(main_applet_volume);
  135. }
  136. void IAudioController::GetLibraryAppletExpectedMasterVolume(HLERequestContext& ctx) {
  137. LOG_DEBUG(Service_AM, "called. library_applet_volume={}", library_applet_volume);
  138. IPC::ResponseBuilder rb{ctx, 3};
  139. rb.Push(ResultSuccess);
  140. rb.Push(library_applet_volume);
  141. }
  142. void IAudioController::ChangeMainAppletMasterVolume(HLERequestContext& ctx) {
  143. struct Parameters {
  144. float volume;
  145. s64 fade_time_ns;
  146. };
  147. static_assert(sizeof(Parameters) == 16);
  148. IPC::RequestParser rp{ctx};
  149. const auto parameters = rp.PopRaw<Parameters>();
  150. LOG_DEBUG(Service_AM, "called. volume={}, fade_time_ns={}", parameters.volume,
  151. parameters.fade_time_ns);
  152. main_applet_volume = std::clamp(parameters.volume, min_allowed_volume, max_allowed_volume);
  153. fade_time_ns = std::chrono::nanoseconds{parameters.fade_time_ns};
  154. IPC::ResponseBuilder rb{ctx, 2};
  155. rb.Push(ResultSuccess);
  156. }
  157. void IAudioController::SetTransparentAudioRate(HLERequestContext& ctx) {
  158. IPC::RequestParser rp{ctx};
  159. const float transparent_volume_rate_tmp = rp.Pop<float>();
  160. LOG_DEBUG(Service_AM, "called. transparent_volume_rate={}", transparent_volume_rate_tmp);
  161. // Clamp volume range to 0-100%.
  162. transparent_volume_rate =
  163. std::clamp(transparent_volume_rate_tmp, min_allowed_volume, max_allowed_volume);
  164. IPC::ResponseBuilder rb{ctx, 2};
  165. rb.Push(ResultSuccess);
  166. }
  167. IDisplayController::IDisplayController(Core::System& system_)
  168. : ServiceFramework{system_, "IDisplayController"} {
  169. // clang-format off
  170. static const FunctionInfo functions[] = {
  171. {0, nullptr, "GetLastForegroundCaptureImage"},
  172. {1, nullptr, "UpdateLastForegroundCaptureImage"},
  173. {2, nullptr, "GetLastApplicationCaptureImage"},
  174. {3, nullptr, "GetCallerAppletCaptureImage"},
  175. {4, nullptr, "UpdateCallerAppletCaptureImage"},
  176. {5, nullptr, "GetLastForegroundCaptureImageEx"},
  177. {6, nullptr, "GetLastApplicationCaptureImageEx"},
  178. {7, &IDisplayController::GetCallerAppletCaptureImageEx, "GetCallerAppletCaptureImageEx"},
  179. {8, &IDisplayController::TakeScreenShotOfOwnLayer, "TakeScreenShotOfOwnLayer"},
  180. {9, nullptr, "CopyBetweenCaptureBuffers"},
  181. {10, nullptr, "AcquireLastApplicationCaptureBuffer"},
  182. {11, nullptr, "ReleaseLastApplicationCaptureBuffer"},
  183. {12, nullptr, "AcquireLastForegroundCaptureBuffer"},
  184. {13, nullptr, "ReleaseLastForegroundCaptureBuffer"},
  185. {14, nullptr, "AcquireCallerAppletCaptureBuffer"},
  186. {15, nullptr, "ReleaseCallerAppletCaptureBuffer"},
  187. {16, nullptr, "AcquireLastApplicationCaptureBufferEx"},
  188. {17, nullptr, "AcquireLastForegroundCaptureBufferEx"},
  189. {18, nullptr, "AcquireCallerAppletCaptureBufferEx"},
  190. {20, nullptr, "ClearCaptureBuffer"},
  191. {21, nullptr, "ClearAppletTransitionBuffer"},
  192. {22, nullptr, "AcquireLastApplicationCaptureSharedBuffer"},
  193. {23, nullptr, "ReleaseLastApplicationCaptureSharedBuffer"},
  194. {24, &IDisplayController::AcquireLastForegroundCaptureSharedBuffer, "AcquireLastForegroundCaptureSharedBuffer"},
  195. {25, &IDisplayController::ReleaseLastForegroundCaptureSharedBuffer, "ReleaseLastForegroundCaptureSharedBuffer"},
  196. {26, &IDisplayController::AcquireCallerAppletCaptureSharedBuffer, "AcquireCallerAppletCaptureSharedBuffer"},
  197. {27, &IDisplayController::ReleaseCallerAppletCaptureSharedBuffer, "ReleaseCallerAppletCaptureSharedBuffer"},
  198. {28, nullptr, "TakeScreenShotOfOwnLayerEx"},
  199. };
  200. // clang-format on
  201. RegisterHandlers(functions);
  202. }
  203. IDisplayController::~IDisplayController() = default;
  204. void IDisplayController::GetCallerAppletCaptureImageEx(HLERequestContext& ctx) {
  205. LOG_WARNING(Service_AM, "(STUBBED) called");
  206. IPC::ResponseBuilder rb{ctx, 4};
  207. rb.Push(ResultSuccess);
  208. rb.Push(1u);
  209. rb.Push(0);
  210. }
  211. void IDisplayController::TakeScreenShotOfOwnLayer(HLERequestContext& ctx) {
  212. LOG_WARNING(Service_AM, "(STUBBED) called");
  213. IPC::ResponseBuilder rb{ctx, 2};
  214. rb.Push(ResultSuccess);
  215. }
  216. void IDisplayController::AcquireLastForegroundCaptureSharedBuffer(HLERequestContext& ctx) {
  217. LOG_WARNING(Service_AM, "(STUBBED) called");
  218. IPC::ResponseBuilder rb{ctx, 4};
  219. rb.Push(ResultSuccess);
  220. rb.Push(1U);
  221. rb.Push(0);
  222. }
  223. void IDisplayController::ReleaseLastForegroundCaptureSharedBuffer(HLERequestContext& ctx) {
  224. LOG_WARNING(Service_AM, "(STUBBED) called");
  225. IPC::ResponseBuilder rb{ctx, 2};
  226. rb.Push(ResultSuccess);
  227. }
  228. void IDisplayController::AcquireCallerAppletCaptureSharedBuffer(HLERequestContext& ctx) {
  229. LOG_WARNING(Service_AM, "(STUBBED) called");
  230. IPC::ResponseBuilder rb{ctx, 4};
  231. rb.Push(ResultSuccess);
  232. rb.Push(1U);
  233. rb.Push(0);
  234. }
  235. void IDisplayController::ReleaseCallerAppletCaptureSharedBuffer(HLERequestContext& ctx) {
  236. LOG_WARNING(Service_AM, "(STUBBED) called");
  237. IPC::ResponseBuilder rb{ctx, 2};
  238. rb.Push(ResultSuccess);
  239. }
  240. IDebugFunctions::IDebugFunctions(Core::System& system_)
  241. : ServiceFramework{system_, "IDebugFunctions"} {
  242. // clang-format off
  243. static const FunctionInfo functions[] = {
  244. {0, nullptr, "NotifyMessageToHomeMenuForDebug"},
  245. {1, nullptr, "OpenMainApplication"},
  246. {10, nullptr, "PerformSystemButtonPressing"},
  247. {20, nullptr, "InvalidateTransitionLayer"},
  248. {30, nullptr, "RequestLaunchApplicationWithUserAndArgumentForDebug"},
  249. {31, nullptr, "RequestLaunchApplicationByApplicationLaunchInfoForDebug"},
  250. {40, nullptr, "GetAppletResourceUsageInfo"},
  251. {50, nullptr, "AddSystemProgramIdAndAppletIdForDebug"},
  252. {51, nullptr, "AddOperationConfirmedLibraryAppletIdForDebug"},
  253. {100, nullptr, "SetCpuBoostModeForApplet"},
  254. {101, nullptr, "CancelCpuBoostModeForApplet"},
  255. {110, nullptr, "PushToAppletBoundChannelForDebug"},
  256. {111, nullptr, "TryPopFromAppletBoundChannelForDebug"},
  257. {120, nullptr, "AlarmSettingNotificationEnableAppEventReserve"},
  258. {121, nullptr, "AlarmSettingNotificationDisableAppEventReserve"},
  259. {122, nullptr, "AlarmSettingNotificationPushAppEventNotify"},
  260. {130, nullptr, "FriendInvitationSetApplicationParameter"},
  261. {131, nullptr, "FriendInvitationClearApplicationParameter"},
  262. {132, nullptr, "FriendInvitationPushApplicationParameter"},
  263. {140, nullptr, "RestrictPowerOperationForSecureLaunchModeForDebug"},
  264. {200, nullptr, "CreateFloatingLibraryAppletAccepterForDebug"},
  265. {300, nullptr, "TerminateAllRunningApplicationsForDebug"},
  266. {900, nullptr, "GetGrcProcessLaunchedSystemEvent"},
  267. };
  268. // clang-format on
  269. RegisterHandlers(functions);
  270. }
  271. IDebugFunctions::~IDebugFunctions() = default;
  272. ISelfController::ISelfController(Core::System& system_, Nvnflinger::Nvnflinger& nvnflinger_)
  273. : ServiceFramework{system_, "ISelfController"}, nvnflinger{nvnflinger_},
  274. service_context{system, "ISelfController"} {
  275. // clang-format off
  276. static const FunctionInfo functions[] = {
  277. {0, &ISelfController::Exit, "Exit"},
  278. {1, &ISelfController::LockExit, "LockExit"},
  279. {2, &ISelfController::UnlockExit, "UnlockExit"},
  280. {3, &ISelfController::EnterFatalSection, "EnterFatalSection"},
  281. {4, &ISelfController::LeaveFatalSection, "LeaveFatalSection"},
  282. {9, &ISelfController::GetLibraryAppletLaunchableEvent, "GetLibraryAppletLaunchableEvent"},
  283. {10, &ISelfController::SetScreenShotPermission, "SetScreenShotPermission"},
  284. {11, &ISelfController::SetOperationModeChangedNotification, "SetOperationModeChangedNotification"},
  285. {12, &ISelfController::SetPerformanceModeChangedNotification, "SetPerformanceModeChangedNotification"},
  286. {13, &ISelfController::SetFocusHandlingMode, "SetFocusHandlingMode"},
  287. {14, &ISelfController::SetRestartMessageEnabled, "SetRestartMessageEnabled"},
  288. {15, nullptr, "SetScreenShotAppletIdentityInfo"},
  289. {16, &ISelfController::SetOutOfFocusSuspendingEnabled, "SetOutOfFocusSuspendingEnabled"},
  290. {17, nullptr, "SetControllerFirmwareUpdateSection"},
  291. {18, nullptr, "SetRequiresCaptureButtonShortPressedMessage"},
  292. {19, &ISelfController::SetAlbumImageOrientation, "SetAlbumImageOrientation"},
  293. {20, nullptr, "SetDesirableKeyboardLayout"},
  294. {21, nullptr, "GetScreenShotProgramId"},
  295. {40, &ISelfController::CreateManagedDisplayLayer, "CreateManagedDisplayLayer"},
  296. {41, &ISelfController::IsSystemBufferSharingEnabled, "IsSystemBufferSharingEnabled"},
  297. {42, &ISelfController::GetSystemSharedLayerHandle, "GetSystemSharedLayerHandle"},
  298. {43, &ISelfController::GetSystemSharedBufferHandle, "GetSystemSharedBufferHandle"},
  299. {44, &ISelfController::CreateManagedDisplaySeparableLayer, "CreateManagedDisplaySeparableLayer"},
  300. {45, nullptr, "SetManagedDisplayLayerSeparationMode"},
  301. {46, nullptr, "SetRecordingLayerCompositionEnabled"},
  302. {50, &ISelfController::SetHandlesRequestToDisplay, "SetHandlesRequestToDisplay"},
  303. {51, &ISelfController::ApproveToDisplay, "ApproveToDisplay"},
  304. {60, nullptr, "OverrideAutoSleepTimeAndDimmingTime"},
  305. {61, nullptr, "SetMediaPlaybackState"},
  306. {62, &ISelfController::SetIdleTimeDetectionExtension, "SetIdleTimeDetectionExtension"},
  307. {63, &ISelfController::GetIdleTimeDetectionExtension, "GetIdleTimeDetectionExtension"},
  308. {64, nullptr, "SetInputDetectionSourceSet"},
  309. {65, &ISelfController::ReportUserIsActive, "ReportUserIsActive"},
  310. {66, nullptr, "GetCurrentIlluminance"},
  311. {67, nullptr, "IsIlluminanceAvailable"},
  312. {68, &ISelfController::SetAutoSleepDisabled, "SetAutoSleepDisabled"},
  313. {69, &ISelfController::IsAutoSleepDisabled, "IsAutoSleepDisabled"},
  314. {70, nullptr, "ReportMultimediaError"},
  315. {71, nullptr, "GetCurrentIlluminanceEx"},
  316. {72, nullptr, "SetInputDetectionPolicy"},
  317. {80, nullptr, "SetWirelessPriorityMode"},
  318. {90, &ISelfController::GetAccumulatedSuspendedTickValue, "GetAccumulatedSuspendedTickValue"},
  319. {91, &ISelfController::GetAccumulatedSuspendedTickChangedEvent, "GetAccumulatedSuspendedTickChangedEvent"},
  320. {100, &ISelfController::SetAlbumImageTakenNotificationEnabled, "SetAlbumImageTakenNotificationEnabled"},
  321. {110, nullptr, "SetApplicationAlbumUserData"},
  322. {120, &ISelfController::SaveCurrentScreenshot, "SaveCurrentScreenshot"},
  323. {130, &ISelfController::SetRecordVolumeMuted, "SetRecordVolumeMuted"},
  324. {1000, nullptr, "GetDebugStorageChannel"},
  325. };
  326. // clang-format on
  327. RegisterHandlers(functions);
  328. launchable_event = service_context.CreateEvent("ISelfController:LaunchableEvent");
  329. // This event is created by AM on the first time GetAccumulatedSuspendedTickChangedEvent() is
  330. // called. Yuzu can just create it unconditionally, since it doesn't need to support multiple
  331. // ISelfControllers. The event is signaled on creation, and on transition from suspended -> not
  332. // suspended if the event has previously been created by a call to
  333. // GetAccumulatedSuspendedTickChangedEvent.
  334. accumulated_suspended_tick_changed_event =
  335. service_context.CreateEvent("ISelfController:AccumulatedSuspendedTickChangedEvent");
  336. accumulated_suspended_tick_changed_event->Signal();
  337. }
  338. ISelfController::~ISelfController() {
  339. service_context.CloseEvent(launchable_event);
  340. service_context.CloseEvent(accumulated_suspended_tick_changed_event);
  341. }
  342. void ISelfController::Exit(HLERequestContext& ctx) {
  343. LOG_DEBUG(Service_AM, "called");
  344. IPC::ResponseBuilder rb{ctx, 2};
  345. rb.Push(ResultSuccess);
  346. system.Exit();
  347. }
  348. void ISelfController::LockExit(HLERequestContext& ctx) {
  349. LOG_DEBUG(Service_AM, "called");
  350. system.SetExitLocked(true);
  351. IPC::ResponseBuilder rb{ctx, 2};
  352. rb.Push(ResultSuccess);
  353. }
  354. void ISelfController::UnlockExit(HLERequestContext& ctx) {
  355. LOG_DEBUG(Service_AM, "called");
  356. system.SetExitLocked(false);
  357. IPC::ResponseBuilder rb{ctx, 2};
  358. rb.Push(ResultSuccess);
  359. if (system.GetExitRequested()) {
  360. system.Exit();
  361. }
  362. }
  363. void ISelfController::EnterFatalSection(HLERequestContext& ctx) {
  364. ++num_fatal_sections_entered;
  365. LOG_DEBUG(Service_AM, "called. Num fatal sections entered: {}", num_fatal_sections_entered);
  366. IPC::ResponseBuilder rb{ctx, 2};
  367. rb.Push(ResultSuccess);
  368. }
  369. void ISelfController::LeaveFatalSection(HLERequestContext& ctx) {
  370. LOG_DEBUG(Service_AM, "called.");
  371. // Entry and exit of fatal sections must be balanced.
  372. if (num_fatal_sections_entered == 0) {
  373. IPC::ResponseBuilder rb{ctx, 2};
  374. rb.Push(Result{ErrorModule::AM, 512});
  375. return;
  376. }
  377. --num_fatal_sections_entered;
  378. IPC::ResponseBuilder rb{ctx, 2};
  379. rb.Push(ResultSuccess);
  380. }
  381. void ISelfController::GetLibraryAppletLaunchableEvent(HLERequestContext& ctx) {
  382. LOG_WARNING(Service_AM, "(STUBBED) called");
  383. launchable_event->Signal();
  384. IPC::ResponseBuilder rb{ctx, 2, 1};
  385. rb.Push(ResultSuccess);
  386. rb.PushCopyObjects(launchable_event->GetReadableEvent());
  387. }
  388. void ISelfController::SetScreenShotPermission(HLERequestContext& ctx) {
  389. IPC::RequestParser rp{ctx};
  390. const auto permission = rp.PopEnum<ScreenshotPermission>();
  391. LOG_DEBUG(Service_AM, "called, permission={}", permission);
  392. screenshot_permission = permission;
  393. IPC::ResponseBuilder rb{ctx, 2};
  394. rb.Push(ResultSuccess);
  395. }
  396. void ISelfController::SetOperationModeChangedNotification(HLERequestContext& ctx) {
  397. IPC::RequestParser rp{ctx};
  398. bool flag = rp.Pop<bool>();
  399. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  400. IPC::ResponseBuilder rb{ctx, 2};
  401. rb.Push(ResultSuccess);
  402. }
  403. void ISelfController::SetPerformanceModeChangedNotification(HLERequestContext& ctx) {
  404. IPC::RequestParser rp{ctx};
  405. bool flag = rp.Pop<bool>();
  406. LOG_WARNING(Service_AM, "(STUBBED) called flag={}", flag);
  407. IPC::ResponseBuilder rb{ctx, 2};
  408. rb.Push(ResultSuccess);
  409. }
  410. void ISelfController::SetFocusHandlingMode(HLERequestContext& ctx) {
  411. // Takes 3 input u8s with each field located immediately after the previous
  412. // u8, these are bool flags. No output.
  413. IPC::RequestParser rp{ctx};
  414. struct FocusHandlingModeParams {
  415. u8 unknown0;
  416. u8 unknown1;
  417. u8 unknown2;
  418. };
  419. const auto flags = rp.PopRaw<FocusHandlingModeParams>();
  420. LOG_WARNING(Service_AM, "(STUBBED) called. unknown0={}, unknown1={}, unknown2={}",
  421. flags.unknown0, flags.unknown1, flags.unknown2);
  422. IPC::ResponseBuilder rb{ctx, 2};
  423. rb.Push(ResultSuccess);
  424. }
  425. void ISelfController::SetRestartMessageEnabled(HLERequestContext& ctx) {
  426. LOG_WARNING(Service_AM, "(STUBBED) called");
  427. IPC::ResponseBuilder rb{ctx, 2};
  428. rb.Push(ResultSuccess);
  429. }
  430. void ISelfController::SetOutOfFocusSuspendingEnabled(HLERequestContext& ctx) {
  431. // Takes 3 input u8s with each field located immediately after the previous
  432. // u8, these are bool flags. No output.
  433. IPC::RequestParser rp{ctx};
  434. bool enabled = rp.Pop<bool>();
  435. LOG_WARNING(Service_AM, "(STUBBED) called enabled={}", enabled);
  436. IPC::ResponseBuilder rb{ctx, 2};
  437. rb.Push(ResultSuccess);
  438. }
  439. void ISelfController::SetAlbumImageOrientation(HLERequestContext& ctx) {
  440. LOG_WARNING(Service_AM, "(STUBBED) called");
  441. IPC::ResponseBuilder rb{ctx, 2};
  442. rb.Push(ResultSuccess);
  443. }
  444. void ISelfController::CreateManagedDisplayLayer(HLERequestContext& ctx) {
  445. LOG_WARNING(Service_AM, "(STUBBED) called");
  446. // TODO(Subv): Find out how AM determines the display to use, for now just
  447. // create the layer in the Default display.
  448. const auto display_id = nvnflinger.OpenDisplay("Default");
  449. const auto layer_id = nvnflinger.CreateLayer(*display_id);
  450. IPC::ResponseBuilder rb{ctx, 4};
  451. rb.Push(ResultSuccess);
  452. rb.Push(*layer_id);
  453. }
  454. void ISelfController::IsSystemBufferSharingEnabled(HLERequestContext& ctx) {
  455. LOG_WARNING(Service_AM, "(STUBBED) called");
  456. IPC::ResponseBuilder rb{ctx, 2};
  457. rb.Push(this->EnsureBufferSharingEnabled());
  458. }
  459. void ISelfController::GetSystemSharedLayerHandle(HLERequestContext& ctx) {
  460. LOG_WARNING(Service_AM, "(STUBBED) called");
  461. IPC::ResponseBuilder rb{ctx, 6};
  462. rb.Push(this->EnsureBufferSharingEnabled());
  463. rb.Push<s64>(system_shared_buffer_id);
  464. rb.Push<s64>(system_shared_layer_id);
  465. }
  466. void ISelfController::GetSystemSharedBufferHandle(HLERequestContext& ctx) {
  467. LOG_WARNING(Service_AM, "(STUBBED) called");
  468. IPC::ResponseBuilder rb{ctx, 4};
  469. rb.Push(this->EnsureBufferSharingEnabled());
  470. rb.Push<s64>(system_shared_buffer_id);
  471. }
  472. Result ISelfController::EnsureBufferSharingEnabled() {
  473. if (buffer_sharing_enabled) {
  474. return ResultSuccess;
  475. }
  476. if (system.GetAppletManager().GetCurrentAppletId() <= Applets::AppletId::Application) {
  477. return VI::ResultOperationFailed;
  478. }
  479. const auto display_id = nvnflinger.OpenDisplay("Default");
  480. const auto result = nvnflinger.GetSystemBufferManager().Initialize(
  481. &system_shared_buffer_id, &system_shared_layer_id, *display_id);
  482. if (result.IsSuccess()) {
  483. buffer_sharing_enabled = true;
  484. }
  485. return result;
  486. }
  487. void ISelfController::CreateManagedDisplaySeparableLayer(HLERequestContext& ctx) {
  488. LOG_WARNING(Service_AM, "(STUBBED) called");
  489. // TODO(Subv): Find out how AM determines the display to use, for now just
  490. // create the layer in the Default display.
  491. // This calls nn::vi::CreateRecordingLayer() which creates another layer.
  492. // Currently we do not support more than 1 layer per display, output 1 layer id for now.
  493. // Outputting 1 layer id instead of the expected 2 has not been observed to cause any adverse
  494. // side effects.
  495. // TODO: Support multiple layers
  496. const auto display_id = nvnflinger.OpenDisplay("Default");
  497. const auto layer_id = nvnflinger.CreateLayer(*display_id);
  498. IPC::ResponseBuilder rb{ctx, 4};
  499. rb.Push(ResultSuccess);
  500. rb.Push(*layer_id);
  501. }
  502. void ISelfController::SetHandlesRequestToDisplay(HLERequestContext& ctx) {
  503. LOG_WARNING(Service_AM, "(STUBBED) called");
  504. IPC::ResponseBuilder rb{ctx, 2};
  505. rb.Push(ResultSuccess);
  506. }
  507. void ISelfController::ApproveToDisplay(HLERequestContext& ctx) {
  508. LOG_WARNING(Service_AM, "(STUBBED) called");
  509. IPC::ResponseBuilder rb{ctx, 2};
  510. rb.Push(ResultSuccess);
  511. }
  512. void ISelfController::SetIdleTimeDetectionExtension(HLERequestContext& ctx) {
  513. IPC::RequestParser rp{ctx};
  514. idle_time_detection_extension = rp.Pop<u32>();
  515. LOG_DEBUG(Service_AM, "(STUBBED) called idle_time_detection_extension={}",
  516. idle_time_detection_extension);
  517. IPC::ResponseBuilder rb{ctx, 2};
  518. rb.Push(ResultSuccess);
  519. }
  520. void ISelfController::GetIdleTimeDetectionExtension(HLERequestContext& ctx) {
  521. LOG_WARNING(Service_AM, "(STUBBED) called");
  522. IPC::ResponseBuilder rb{ctx, 3};
  523. rb.Push(ResultSuccess);
  524. rb.Push<u32>(idle_time_detection_extension);
  525. }
  526. void ISelfController::ReportUserIsActive(HLERequestContext& ctx) {
  527. LOG_WARNING(Service_AM, "(STUBBED) called");
  528. IPC::ResponseBuilder rb{ctx, 2};
  529. rb.Push(ResultSuccess);
  530. }
  531. void ISelfController::SetAutoSleepDisabled(HLERequestContext& ctx) {
  532. IPC::RequestParser rp{ctx};
  533. is_auto_sleep_disabled = rp.Pop<bool>();
  534. // On the system itself, if the previous state of is_auto_sleep_disabled
  535. // differed from the current value passed in, it'd signify the internal
  536. // window manager to update (and also increment some statistics like update counts)
  537. //
  538. // It'd also indicate this change to an idle handling context.
  539. //
  540. // However, given we're emulating this behavior, most of this can be ignored
  541. // and it's sufficient to simply set the member variable for querying via
  542. // IsAutoSleepDisabled().
  543. LOG_DEBUG(Service_AM, "called. is_auto_sleep_disabled={}", is_auto_sleep_disabled);
  544. IPC::ResponseBuilder rb{ctx, 2};
  545. rb.Push(ResultSuccess);
  546. }
  547. void ISelfController::IsAutoSleepDisabled(HLERequestContext& ctx) {
  548. LOG_DEBUG(Service_AM, "called.");
  549. IPC::ResponseBuilder rb{ctx, 3};
  550. rb.Push(ResultSuccess);
  551. rb.Push(is_auto_sleep_disabled);
  552. }
  553. void ISelfController::GetAccumulatedSuspendedTickValue(HLERequestContext& ctx) {
  554. LOG_DEBUG(Service_AM, "called.");
  555. // This command returns the total number of system ticks since ISelfController creation
  556. // where the game was suspended. Since Yuzu doesn't implement game suspension, this command
  557. // can just always return 0 ticks.
  558. IPC::ResponseBuilder rb{ctx, 4};
  559. rb.Push(ResultSuccess);
  560. rb.Push<u64>(0);
  561. }
  562. void ISelfController::GetAccumulatedSuspendedTickChangedEvent(HLERequestContext& ctx) {
  563. LOG_DEBUG(Service_AM, "called.");
  564. IPC::ResponseBuilder rb{ctx, 2, 1};
  565. rb.Push(ResultSuccess);
  566. rb.PushCopyObjects(accumulated_suspended_tick_changed_event->GetReadableEvent());
  567. }
  568. void ISelfController::SetAlbumImageTakenNotificationEnabled(HLERequestContext& ctx) {
  569. IPC::RequestParser rp{ctx};
  570. // This service call sets an internal flag whether a notification is shown when an image is
  571. // captured. Currently we do not support capturing images via the capture button, so this can be
  572. // stubbed for now.
  573. const bool album_image_taken_notification_enabled = rp.Pop<bool>();
  574. LOG_WARNING(Service_AM, "(STUBBED) called. album_image_taken_notification_enabled={}",
  575. album_image_taken_notification_enabled);
  576. IPC::ResponseBuilder rb{ctx, 2};
  577. rb.Push(ResultSuccess);
  578. }
  579. void ISelfController::SaveCurrentScreenshot(HLERequestContext& ctx) {
  580. IPC::RequestParser rp{ctx};
  581. const auto report_option = rp.PopEnum<Capture::AlbumReportOption>();
  582. LOG_INFO(Service_AM, "called, report_option={}", report_option);
  583. const auto screenshot_service =
  584. system.ServiceManager().GetService<Service::Capture::IScreenShotApplicationService>(
  585. "caps:su");
  586. if (screenshot_service) {
  587. screenshot_service->CaptureAndSaveScreenshot(report_option);
  588. }
  589. IPC::ResponseBuilder rb{ctx, 2};
  590. rb.Push(ResultSuccess);
  591. }
  592. void ISelfController::SetRecordVolumeMuted(HLERequestContext& ctx) {
  593. IPC::RequestParser rp{ctx};
  594. const auto is_record_volume_muted = rp.Pop<bool>();
  595. LOG_WARNING(Service_AM, "(STUBBED) called. is_record_volume_muted={}", is_record_volume_muted);
  596. IPC::ResponseBuilder rb{ctx, 2};
  597. rb.Push(ResultSuccess);
  598. }
  599. AppletMessageQueue::AppletMessageQueue(Core::System& system)
  600. : service_context{system, "AppletMessageQueue"} {
  601. on_new_message = service_context.CreateEvent("AMMessageQueue:OnMessageReceived");
  602. on_operation_mode_changed = service_context.CreateEvent("AMMessageQueue:OperationModeChanged");
  603. }
  604. AppletMessageQueue::~AppletMessageQueue() {
  605. service_context.CloseEvent(on_new_message);
  606. service_context.CloseEvent(on_operation_mode_changed);
  607. }
  608. Kernel::KReadableEvent& AppletMessageQueue::GetMessageReceiveEvent() {
  609. return on_new_message->GetReadableEvent();
  610. }
  611. Kernel::KReadableEvent& AppletMessageQueue::GetOperationModeChangedEvent() {
  612. return on_operation_mode_changed->GetReadableEvent();
  613. }
  614. void AppletMessageQueue::PushMessage(AppletMessage msg) {
  615. messages.push(msg);
  616. on_new_message->Signal();
  617. }
  618. AppletMessageQueue::AppletMessage AppletMessageQueue::PopMessage() {
  619. if (messages.empty()) {
  620. on_new_message->Clear();
  621. return AppletMessage::None;
  622. }
  623. auto msg = messages.front();
  624. messages.pop();
  625. if (messages.empty()) {
  626. on_new_message->Clear();
  627. }
  628. return msg;
  629. }
  630. std::size_t AppletMessageQueue::GetMessageCount() const {
  631. return messages.size();
  632. }
  633. void AppletMessageQueue::RequestExit() {
  634. PushMessage(AppletMessage::Exit);
  635. }
  636. void AppletMessageQueue::RequestResume() {
  637. PushMessage(AppletMessage::Resume);
  638. }
  639. void AppletMessageQueue::FocusStateChanged() {
  640. PushMessage(AppletMessage::FocusStateChanged);
  641. }
  642. void AppletMessageQueue::OperationModeChanged() {
  643. PushMessage(AppletMessage::OperationModeChanged);
  644. PushMessage(AppletMessage::PerformanceModeChanged);
  645. on_operation_mode_changed->Signal();
  646. }
  647. ILockAccessor::ILockAccessor(Core::System& system_)
  648. : ServiceFramework{system_, "ILockAccessor"}, service_context{system_, "ILockAccessor"} {
  649. // clang-format off
  650. static const FunctionInfo functions[] = {
  651. {1, &ILockAccessor::TryLock, "TryLock"},
  652. {2, &ILockAccessor::Unlock, "Unlock"},
  653. {3, &ILockAccessor::GetEvent, "GetEvent"},
  654. {4,&ILockAccessor::IsLocked, "IsLocked"},
  655. };
  656. // clang-format on
  657. RegisterHandlers(functions);
  658. lock_event = service_context.CreateEvent("ILockAccessor::LockEvent");
  659. }
  660. ILockAccessor::~ILockAccessor() {
  661. service_context.CloseEvent(lock_event);
  662. };
  663. void ILockAccessor::TryLock(HLERequestContext& ctx) {
  664. IPC::RequestParser rp{ctx};
  665. const auto return_handle = rp.Pop<bool>();
  666. LOG_WARNING(Service_AM, "(STUBBED) called, return_handle={}", return_handle);
  667. // TODO: When return_handle is true this function should return the lock handle
  668. is_locked = true;
  669. IPC::ResponseBuilder rb{ctx, 3};
  670. rb.Push(ResultSuccess);
  671. rb.Push<u8>(is_locked);
  672. }
  673. void ILockAccessor::Unlock(HLERequestContext& ctx) {
  674. LOG_INFO(Service_AM, "called");
  675. is_locked = false;
  676. IPC::ResponseBuilder rb{ctx, 2};
  677. rb.Push(ResultSuccess);
  678. }
  679. void ILockAccessor::GetEvent(HLERequestContext& ctx) {
  680. LOG_INFO(Service_AM, "called");
  681. lock_event->Signal();
  682. IPC::ResponseBuilder rb{ctx, 2, 1};
  683. rb.Push(ResultSuccess);
  684. rb.PushCopyObjects(lock_event->GetReadableEvent());
  685. }
  686. void ILockAccessor::IsLocked(HLERequestContext& ctx) {
  687. LOG_INFO(Service_AM, "called");
  688. IPC::ResponseBuilder rb{ctx, 2};
  689. rb.Push(ResultSuccess);
  690. rb.Push<u8>(is_locked);
  691. }
  692. ICommonStateGetter::ICommonStateGetter(Core::System& system_,
  693. std::shared_ptr<AppletMessageQueue> msg_queue_)
  694. : ServiceFramework{system_, "ICommonStateGetter"}, msg_queue{std::move(msg_queue_)},
  695. service_context{system_, "ICommonStateGetter"} {
  696. // clang-format off
  697. static const FunctionInfo functions[] = {
  698. {0, &ICommonStateGetter::GetEventHandle, "GetEventHandle"},
  699. {1, &ICommonStateGetter::ReceiveMessage, "ReceiveMessage"},
  700. {2, nullptr, "GetThisAppletKind"},
  701. {3, nullptr, "AllowToEnterSleep"},
  702. {4, nullptr, "DisallowToEnterSleep"},
  703. {5, &ICommonStateGetter::GetOperationMode, "GetOperationMode"},
  704. {6, &ICommonStateGetter::GetPerformanceMode, "GetPerformanceMode"},
  705. {7, nullptr, "GetCradleStatus"},
  706. {8, &ICommonStateGetter::GetBootMode, "GetBootMode"},
  707. {9, &ICommonStateGetter::GetCurrentFocusState, "GetCurrentFocusState"},
  708. {10, &ICommonStateGetter::RequestToAcquireSleepLock, "RequestToAcquireSleepLock"},
  709. {11, nullptr, "ReleaseSleepLock"},
  710. {12, nullptr, "ReleaseSleepLockTransiently"},
  711. {13, &ICommonStateGetter::GetAcquiredSleepLockEvent, "GetAcquiredSleepLockEvent"},
  712. {14, nullptr, "GetWakeupCount"},
  713. {20, nullptr, "PushToGeneralChannel"},
  714. {30, nullptr, "GetHomeButtonReaderLockAccessor"},
  715. {31, &ICommonStateGetter::GetReaderLockAccessorEx, "GetReaderLockAccessorEx"},
  716. {32, nullptr, "GetWriterLockAccessorEx"},
  717. {40, nullptr, "GetCradleFwVersion"},
  718. {50, &ICommonStateGetter::IsVrModeEnabled, "IsVrModeEnabled"},
  719. {51, &ICommonStateGetter::SetVrModeEnabled, "SetVrModeEnabled"},
  720. {52, &ICommonStateGetter::SetLcdBacklighOffEnabled, "SetLcdBacklighOffEnabled"},
  721. {53, &ICommonStateGetter::BeginVrModeEx, "BeginVrModeEx"},
  722. {54, &ICommonStateGetter::EndVrModeEx, "EndVrModeEx"},
  723. {55, nullptr, "IsInControllerFirmwareUpdateSection"},
  724. {59, nullptr, "SetVrPositionForDebug"},
  725. {60, &ICommonStateGetter::GetDefaultDisplayResolution, "GetDefaultDisplayResolution"},
  726. {61, &ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent, "GetDefaultDisplayResolutionChangeEvent"},
  727. {62, nullptr, "GetHdcpAuthenticationState"},
  728. {63, nullptr, "GetHdcpAuthenticationStateChangeEvent"},
  729. {64, nullptr, "SetTvPowerStateMatchingMode"},
  730. {65, nullptr, "GetApplicationIdByContentActionName"},
  731. {66, &ICommonStateGetter::SetCpuBoostMode, "SetCpuBoostMode"},
  732. {67, nullptr, "CancelCpuBoostMode"},
  733. {68, &ICommonStateGetter::GetBuiltInDisplayType, "GetBuiltInDisplayType"},
  734. {80, &ICommonStateGetter::PerformSystemButtonPressingIfInFocus, "PerformSystemButtonPressingIfInFocus"},
  735. {90, nullptr, "SetPerformanceConfigurationChangedNotification"},
  736. {91, nullptr, "GetCurrentPerformanceConfiguration"},
  737. {100, nullptr, "SetHandlingHomeButtonShortPressedEnabled"},
  738. {110, nullptr, "OpenMyGpuErrorHandler"},
  739. {120, nullptr, "GetAppletLaunchedHistory"},
  740. {200, nullptr, "GetOperationModeSystemInfo"},
  741. {300, &ICommonStateGetter::GetSettingsPlatformRegion, "GetSettingsPlatformRegion"},
  742. {400, nullptr, "ActivateMigrationService"},
  743. {401, nullptr, "DeactivateMigrationService"},
  744. {500, nullptr, "DisableSleepTillShutdown"},
  745. {501, nullptr, "SuppressDisablingSleepTemporarily"},
  746. {502, nullptr, "IsSleepEnabled"},
  747. {503, nullptr, "IsDisablingSleepSuppressed"},
  748. {900, &ICommonStateGetter::SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled, "SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled"},
  749. };
  750. // clang-format on
  751. RegisterHandlers(functions);
  752. sleep_lock_event = service_context.CreateEvent("ICommonStateGetter::SleepLockEvent");
  753. // Configure applets to be in foreground state
  754. msg_queue->PushMessage(AppletMessageQueue::AppletMessage::ChangeIntoForeground);
  755. msg_queue->PushMessage(AppletMessageQueue::AppletMessage::FocusStateChanged);
  756. }
  757. ICommonStateGetter::~ICommonStateGetter() {
  758. service_context.CloseEvent(sleep_lock_event);
  759. };
  760. void ICommonStateGetter::GetBootMode(HLERequestContext& ctx) {
  761. LOG_DEBUG(Service_AM, "called");
  762. IPC::ResponseBuilder rb{ctx, 3};
  763. rb.Push(ResultSuccess);
  764. rb.Push<u8>(static_cast<u8>(Service::PM::SystemBootMode::Normal)); // Normal boot mode
  765. }
  766. void ICommonStateGetter::GetEventHandle(HLERequestContext& ctx) {
  767. LOG_DEBUG(Service_AM, "called");
  768. IPC::ResponseBuilder rb{ctx, 2, 1};
  769. rb.Push(ResultSuccess);
  770. rb.PushCopyObjects(msg_queue->GetMessageReceiveEvent());
  771. }
  772. void ICommonStateGetter::ReceiveMessage(HLERequestContext& ctx) {
  773. LOG_DEBUG(Service_AM, "called");
  774. const auto message = msg_queue->PopMessage();
  775. IPC::ResponseBuilder rb{ctx, 3};
  776. if (message == AppletMessageQueue::AppletMessage::None) {
  777. LOG_ERROR(Service_AM, "Message queue is empty");
  778. rb.Push(AM::ResultNoMessages);
  779. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  780. return;
  781. }
  782. rb.Push(ResultSuccess);
  783. rb.PushEnum<AppletMessageQueue::AppletMessage>(message);
  784. }
  785. void ICommonStateGetter::GetCurrentFocusState(HLERequestContext& ctx) {
  786. LOG_DEBUG(Service_AM, "(STUBBED) called");
  787. IPC::ResponseBuilder rb{ctx, 3};
  788. rb.Push(ResultSuccess);
  789. rb.Push(static_cast<u8>(FocusState::InFocus));
  790. }
  791. void ICommonStateGetter::RequestToAcquireSleepLock(HLERequestContext& ctx) {
  792. LOG_WARNING(Service_AM, "(STUBBED) called");
  793. // Sleep lock is acquired immediately.
  794. sleep_lock_event->Signal();
  795. IPC::ResponseBuilder rb{ctx, 2};
  796. rb.Push(ResultSuccess);
  797. }
  798. void ICommonStateGetter::GetReaderLockAccessorEx(HLERequestContext& ctx) {
  799. IPC::RequestParser rp{ctx};
  800. const auto unknown = rp.Pop<u32>();
  801. LOG_INFO(Service_AM, "called, unknown={}", unknown);
  802. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  803. rb.Push(ResultSuccess);
  804. rb.PushIpcInterface<ILockAccessor>(system);
  805. }
  806. void ICommonStateGetter::GetAcquiredSleepLockEvent(HLERequestContext& ctx) {
  807. LOG_WARNING(Service_AM, "called");
  808. IPC::ResponseBuilder rb{ctx, 2, 1};
  809. rb.Push(ResultSuccess);
  810. rb.PushCopyObjects(sleep_lock_event->GetReadableEvent());
  811. }
  812. void ICommonStateGetter::IsVrModeEnabled(HLERequestContext& ctx) {
  813. LOG_DEBUG(Service_AM, "called");
  814. IPC::ResponseBuilder rb{ctx, 3};
  815. rb.Push(ResultSuccess);
  816. rb.Push(vr_mode_state);
  817. }
  818. void ICommonStateGetter::SetVrModeEnabled(HLERequestContext& ctx) {
  819. IPC::RequestParser rp{ctx};
  820. vr_mode_state = rp.Pop<bool>();
  821. LOG_WARNING(Service_AM, "VR Mode is {}", vr_mode_state ? "on" : "off");
  822. IPC::ResponseBuilder rb{ctx, 2};
  823. rb.Push(ResultSuccess);
  824. }
  825. void ICommonStateGetter::SetLcdBacklighOffEnabled(HLERequestContext& ctx) {
  826. IPC::RequestParser rp{ctx};
  827. const auto is_lcd_backlight_off_enabled = rp.Pop<bool>();
  828. LOG_WARNING(Service_AM, "(STUBBED) called. is_lcd_backlight_off_enabled={}",
  829. is_lcd_backlight_off_enabled);
  830. IPC::ResponseBuilder rb{ctx, 2};
  831. rb.Push(ResultSuccess);
  832. }
  833. void ICommonStateGetter::BeginVrModeEx(HLERequestContext& ctx) {
  834. LOG_WARNING(Service_AM, "(STUBBED) called");
  835. IPC::ResponseBuilder rb{ctx, 2};
  836. rb.Push(ResultSuccess);
  837. }
  838. void ICommonStateGetter::EndVrModeEx(HLERequestContext& ctx) {
  839. LOG_WARNING(Service_AM, "(STUBBED) called");
  840. IPC::ResponseBuilder rb{ctx, 2};
  841. rb.Push(ResultSuccess);
  842. }
  843. void ICommonStateGetter::GetDefaultDisplayResolutionChangeEvent(HLERequestContext& ctx) {
  844. LOG_DEBUG(Service_AM, "called");
  845. IPC::ResponseBuilder rb{ctx, 2, 1};
  846. rb.Push(ResultSuccess);
  847. rb.PushCopyObjects(msg_queue->GetOperationModeChangedEvent());
  848. }
  849. void ICommonStateGetter::GetDefaultDisplayResolution(HLERequestContext& ctx) {
  850. LOG_DEBUG(Service_AM, "called");
  851. IPC::ResponseBuilder rb{ctx, 4};
  852. rb.Push(ResultSuccess);
  853. if (Settings::IsDockedMode()) {
  854. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedWidth));
  855. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedHeight));
  856. } else {
  857. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedWidth));
  858. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedHeight));
  859. }
  860. }
  861. void ICommonStateGetter::SetCpuBoostMode(HLERequestContext& ctx) {
  862. LOG_DEBUG(Service_AM, "called, forwarding to APM:SYS");
  863. const auto& sm = system.ServiceManager();
  864. const auto apm_sys = sm.GetService<APM::APM_Sys>("apm:sys");
  865. ASSERT(apm_sys != nullptr);
  866. apm_sys->SetCpuBoostMode(ctx);
  867. }
  868. void ICommonStateGetter::GetBuiltInDisplayType(HLERequestContext& ctx) {
  869. LOG_WARNING(Service_AM, "(STUBBED) called");
  870. IPC::ResponseBuilder rb{ctx, 3};
  871. rb.Push(ResultSuccess);
  872. rb.Push(0);
  873. }
  874. void ICommonStateGetter::PerformSystemButtonPressingIfInFocus(HLERequestContext& ctx) {
  875. IPC::RequestParser rp{ctx};
  876. const auto system_button{rp.PopEnum<SystemButtonType>()};
  877. LOG_WARNING(Service_AM, "(STUBBED) called, system_button={}", system_button);
  878. IPC::ResponseBuilder rb{ctx, 2};
  879. rb.Push(ResultSuccess);
  880. }
  881. void ICommonStateGetter::GetSettingsPlatformRegion(HLERequestContext& ctx) {
  882. LOG_WARNING(Service_AM, "(STUBBED) called");
  883. IPC::ResponseBuilder rb{ctx, 3};
  884. rb.Push(ResultSuccess);
  885. rb.PushEnum(SysPlatformRegion::Global);
  886. }
  887. void ICommonStateGetter::SetRequestExitToLibraryAppletAtExecuteNextProgramEnabled(
  888. HLERequestContext& ctx) {
  889. LOG_WARNING(Service_AM, "(STUBBED) called");
  890. IPC::ResponseBuilder rb{ctx, 2};
  891. rb.Push(ResultSuccess);
  892. }
  893. IStorageImpl::~IStorageImpl() = default;
  894. class StorageDataImpl final : public IStorageImpl {
  895. public:
  896. explicit StorageDataImpl(std::vector<u8>&& buffer_) : buffer{std::move(buffer_)} {}
  897. std::vector<u8>& GetData() override {
  898. return buffer;
  899. }
  900. const std::vector<u8>& GetData() const override {
  901. return buffer;
  902. }
  903. std::size_t GetSize() const override {
  904. return buffer.size();
  905. }
  906. private:
  907. std::vector<u8> buffer;
  908. };
  909. IStorage::IStorage(Core::System& system_, std::vector<u8>&& buffer)
  910. : ServiceFramework{system_, "IStorage"}, impl{std::make_shared<StorageDataImpl>(
  911. std::move(buffer))} {
  912. Register();
  913. }
  914. void IStorage::Register() {
  915. // clang-format off
  916. static const FunctionInfo functions[] = {
  917. {0, &IStorage::Open, "Open"},
  918. {1, nullptr, "OpenTransferStorage"},
  919. };
  920. // clang-format on
  921. RegisterHandlers(functions);
  922. }
  923. IStorage::~IStorage() = default;
  924. void IStorage::Open(HLERequestContext& ctx) {
  925. LOG_DEBUG(Service_AM, "called");
  926. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  927. rb.Push(ResultSuccess);
  928. rb.PushIpcInterface<IStorageAccessor>(system, *this);
  929. }
  930. void ICommonStateGetter::GetOperationMode(HLERequestContext& ctx) {
  931. const bool use_docked_mode{Settings::IsDockedMode()};
  932. LOG_DEBUG(Service_AM, "called, use_docked_mode={}", use_docked_mode);
  933. IPC::ResponseBuilder rb{ctx, 3};
  934. rb.Push(ResultSuccess);
  935. rb.Push(static_cast<u8>(use_docked_mode ? OperationMode::Docked : OperationMode::Handheld));
  936. }
  937. void ICommonStateGetter::GetPerformanceMode(HLERequestContext& ctx) {
  938. LOG_DEBUG(Service_AM, "called");
  939. IPC::ResponseBuilder rb{ctx, 3};
  940. rb.Push(ResultSuccess);
  941. rb.PushEnum(system.GetAPMController().GetCurrentPerformanceMode());
  942. }
  943. class ILibraryAppletAccessor final : public ServiceFramework<ILibraryAppletAccessor> {
  944. public:
  945. explicit ILibraryAppletAccessor(Core::System& system_, std::shared_ptr<Applets::Applet> applet_)
  946. : ServiceFramework{system_, "ILibraryAppletAccessor"}, applet{std::move(applet_)} {
  947. // clang-format off
  948. static const FunctionInfo functions[] = {
  949. {0, &ILibraryAppletAccessor::GetAppletStateChangedEvent, "GetAppletStateChangedEvent"},
  950. {1, &ILibraryAppletAccessor::IsCompleted, "IsCompleted"},
  951. {10, &ILibraryAppletAccessor::Start, "Start"},
  952. {20, &ILibraryAppletAccessor::RequestExit, "RequestExit"},
  953. {25, nullptr, "Terminate"},
  954. {30, &ILibraryAppletAccessor::GetResult, "GetResult"},
  955. {50, nullptr, "SetOutOfFocusApplicationSuspendingEnabled"},
  956. {60, &ILibraryAppletAccessor::PresetLibraryAppletGpuTimeSliceZero, "PresetLibraryAppletGpuTimeSliceZero"},
  957. {100, &ILibraryAppletAccessor::PushInData, "PushInData"},
  958. {101, &ILibraryAppletAccessor::PopOutData, "PopOutData"},
  959. {102, nullptr, "PushExtraStorage"},
  960. {103, &ILibraryAppletAccessor::PushInteractiveInData, "PushInteractiveInData"},
  961. {104, &ILibraryAppletAccessor::PopInteractiveOutData, "PopInteractiveOutData"},
  962. {105, &ILibraryAppletAccessor::GetPopOutDataEvent, "GetPopOutDataEvent"},
  963. {106, &ILibraryAppletAccessor::GetPopInteractiveOutDataEvent, "GetPopInteractiveOutDataEvent"},
  964. {110, nullptr, "NeedsToExitProcess"},
  965. {120, nullptr, "GetLibraryAppletInfo"},
  966. {150, nullptr, "RequestForAppletToGetForeground"},
  967. {160, &ILibraryAppletAccessor::GetIndirectLayerConsumerHandle, "GetIndirectLayerConsumerHandle"},
  968. };
  969. // clang-format on
  970. RegisterHandlers(functions);
  971. }
  972. private:
  973. void GetAppletStateChangedEvent(HLERequestContext& ctx) {
  974. LOG_DEBUG(Service_AM, "called");
  975. IPC::ResponseBuilder rb{ctx, 2, 1};
  976. rb.Push(ResultSuccess);
  977. rb.PushCopyObjects(applet->GetBroker().GetStateChangedEvent());
  978. }
  979. void IsCompleted(HLERequestContext& ctx) {
  980. LOG_DEBUG(Service_AM, "called");
  981. IPC::ResponseBuilder rb{ctx, 3};
  982. rb.Push(ResultSuccess);
  983. rb.Push<u32>(applet->TransactionComplete());
  984. }
  985. void GetResult(HLERequestContext& ctx) {
  986. LOG_DEBUG(Service_AM, "called");
  987. IPC::ResponseBuilder rb{ctx, 2};
  988. rb.Push(applet->GetStatus());
  989. }
  990. void PresetLibraryAppletGpuTimeSliceZero(HLERequestContext& ctx) {
  991. LOG_WARNING(Service_AM, "(STUBBED) called");
  992. IPC::ResponseBuilder rb{ctx, 2};
  993. rb.Push(ResultSuccess);
  994. }
  995. void Start(HLERequestContext& ctx) {
  996. LOG_DEBUG(Service_AM, "called");
  997. ASSERT(applet != nullptr);
  998. applet->Initialize();
  999. applet->Execute();
  1000. IPC::ResponseBuilder rb{ctx, 2};
  1001. rb.Push(ResultSuccess);
  1002. }
  1003. void RequestExit(HLERequestContext& ctx) {
  1004. LOG_DEBUG(Service_AM, "called");
  1005. ASSERT(applet != nullptr);
  1006. IPC::ResponseBuilder rb{ctx, 2};
  1007. rb.Push(applet->RequestExit());
  1008. }
  1009. void PushInData(HLERequestContext& ctx) {
  1010. LOG_DEBUG(Service_AM, "called");
  1011. IPC::RequestParser rp{ctx};
  1012. applet->GetBroker().PushNormalDataFromGame(rp.PopIpcInterface<IStorage>().lock());
  1013. IPC::ResponseBuilder rb{ctx, 2};
  1014. rb.Push(ResultSuccess);
  1015. }
  1016. void PopOutData(HLERequestContext& ctx) {
  1017. LOG_DEBUG(Service_AM, "called");
  1018. auto storage = applet->GetBroker().PopNormalDataToGame();
  1019. if (storage == nullptr) {
  1020. LOG_DEBUG(Service_AM,
  1021. "storage is a nullptr. There is no data in the current normal channel");
  1022. IPC::ResponseBuilder rb{ctx, 2};
  1023. rb.Push(AM::ResultNoDataInChannel);
  1024. return;
  1025. }
  1026. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1027. rb.Push(ResultSuccess);
  1028. rb.PushIpcInterface<IStorage>(std::move(storage));
  1029. }
  1030. void PushInteractiveInData(HLERequestContext& ctx) {
  1031. LOG_DEBUG(Service_AM, "called");
  1032. IPC::RequestParser rp{ctx};
  1033. applet->GetBroker().PushInteractiveDataFromGame(rp.PopIpcInterface<IStorage>().lock());
  1034. ASSERT(applet->IsInitialized());
  1035. applet->ExecuteInteractive();
  1036. applet->Execute();
  1037. IPC::ResponseBuilder rb{ctx, 2};
  1038. rb.Push(ResultSuccess);
  1039. }
  1040. void PopInteractiveOutData(HLERequestContext& ctx) {
  1041. LOG_DEBUG(Service_AM, "called");
  1042. auto storage = applet->GetBroker().PopInteractiveDataToGame();
  1043. if (storage == nullptr) {
  1044. LOG_DEBUG(Service_AM,
  1045. "storage is a nullptr. There is no data in the current interactive channel");
  1046. IPC::ResponseBuilder rb{ctx, 2};
  1047. rb.Push(AM::ResultNoDataInChannel);
  1048. return;
  1049. }
  1050. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1051. rb.Push(ResultSuccess);
  1052. rb.PushIpcInterface<IStorage>(std::move(storage));
  1053. }
  1054. void GetPopOutDataEvent(HLERequestContext& ctx) {
  1055. LOG_DEBUG(Service_AM, "called");
  1056. IPC::ResponseBuilder rb{ctx, 2, 1};
  1057. rb.Push(ResultSuccess);
  1058. rb.PushCopyObjects(applet->GetBroker().GetNormalDataEvent());
  1059. }
  1060. void GetPopInteractiveOutDataEvent(HLERequestContext& ctx) {
  1061. LOG_DEBUG(Service_AM, "called");
  1062. IPC::ResponseBuilder rb{ctx, 2, 1};
  1063. rb.Push(ResultSuccess);
  1064. rb.PushCopyObjects(applet->GetBroker().GetInteractiveDataEvent());
  1065. }
  1066. void GetIndirectLayerConsumerHandle(HLERequestContext& ctx) {
  1067. LOG_WARNING(Service_AM, "(STUBBED) called");
  1068. // We require a non-zero handle to be valid. Using 0xdeadbeef allows us to trace if this is
  1069. // actually used anywhere
  1070. constexpr u64 handle = 0xdeadbeef;
  1071. IPC::ResponseBuilder rb{ctx, 4};
  1072. rb.Push(ResultSuccess);
  1073. rb.Push(handle);
  1074. }
  1075. std::shared_ptr<Applets::Applet> applet;
  1076. };
  1077. IStorageAccessor::IStorageAccessor(Core::System& system_, IStorage& backing_)
  1078. : ServiceFramework{system_, "IStorageAccessor"}, backing{backing_} {
  1079. // clang-format off
  1080. static const FunctionInfo functions[] = {
  1081. {0, &IStorageAccessor::GetSize, "GetSize"},
  1082. {10, &IStorageAccessor::Write, "Write"},
  1083. {11, &IStorageAccessor::Read, "Read"},
  1084. };
  1085. // clang-format on
  1086. RegisterHandlers(functions);
  1087. }
  1088. IStorageAccessor::~IStorageAccessor() = default;
  1089. void IStorageAccessor::GetSize(HLERequestContext& ctx) {
  1090. LOG_DEBUG(Service_AM, "called");
  1091. IPC::ResponseBuilder rb{ctx, 4};
  1092. rb.Push(ResultSuccess);
  1093. rb.Push(static_cast<u64>(backing.GetSize()));
  1094. }
  1095. void IStorageAccessor::Write(HLERequestContext& ctx) {
  1096. IPC::RequestParser rp{ctx};
  1097. const u64 offset{rp.Pop<u64>()};
  1098. const auto data{ctx.ReadBuffer()};
  1099. const std::size_t size{std::min<u64>(data.size(), backing.GetSize() - offset)};
  1100. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  1101. if (offset > backing.GetSize()) {
  1102. LOG_ERROR(Service_AM,
  1103. "offset is out of bounds, backing_buffer_sz={}, data_size={}, offset={}",
  1104. backing.GetSize(), size, offset);
  1105. IPC::ResponseBuilder rb{ctx, 2};
  1106. rb.Push(AM::ResultInvalidOffset);
  1107. return;
  1108. }
  1109. std::memcpy(backing.GetData().data() + offset, data.data(), size);
  1110. IPC::ResponseBuilder rb{ctx, 2};
  1111. rb.Push(ResultSuccess);
  1112. }
  1113. void IStorageAccessor::Read(HLERequestContext& ctx) {
  1114. IPC::RequestParser rp{ctx};
  1115. const u64 offset{rp.Pop<u64>()};
  1116. const std::size_t size{std::min<u64>(ctx.GetWriteBufferSize(), backing.GetSize() - offset)};
  1117. LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size);
  1118. if (offset > backing.GetSize()) {
  1119. LOG_ERROR(Service_AM, "offset is out of bounds, backing_buffer_sz={}, size={}, offset={}",
  1120. backing.GetSize(), size, offset);
  1121. IPC::ResponseBuilder rb{ctx, 2};
  1122. rb.Push(AM::ResultInvalidOffset);
  1123. return;
  1124. }
  1125. ctx.WriteBuffer(backing.GetData().data() + offset, size);
  1126. IPC::ResponseBuilder rb{ctx, 2};
  1127. rb.Push(ResultSuccess);
  1128. }
  1129. ILibraryAppletCreator::ILibraryAppletCreator(Core::System& system_)
  1130. : ServiceFramework{system_, "ILibraryAppletCreator"} {
  1131. static const FunctionInfo functions[] = {
  1132. {0, &ILibraryAppletCreator::CreateLibraryApplet, "CreateLibraryApplet"},
  1133. {1, nullptr, "TerminateAllLibraryApplets"},
  1134. {2, nullptr, "AreAnyLibraryAppletsLeft"},
  1135. {10, &ILibraryAppletCreator::CreateStorage, "CreateStorage"},
  1136. {11, &ILibraryAppletCreator::CreateTransferMemoryStorage, "CreateTransferMemoryStorage"},
  1137. {12, &ILibraryAppletCreator::CreateHandleStorage, "CreateHandleStorage"},
  1138. };
  1139. RegisterHandlers(functions);
  1140. }
  1141. ILibraryAppletCreator::~ILibraryAppletCreator() = default;
  1142. void ILibraryAppletCreator::CreateLibraryApplet(HLERequestContext& ctx) {
  1143. IPC::RequestParser rp{ctx};
  1144. const auto applet_id = rp.PopRaw<Applets::AppletId>();
  1145. const auto applet_mode = rp.PopRaw<Applets::LibraryAppletMode>();
  1146. LOG_DEBUG(Service_AM, "called with applet_id={:08X}, applet_mode={:08X}", applet_id,
  1147. applet_mode);
  1148. const auto& applet_manager{system.GetAppletManager()};
  1149. const auto applet = applet_manager.GetApplet(applet_id, applet_mode);
  1150. if (applet == nullptr) {
  1151. LOG_ERROR(Service_AM, "Applet doesn't exist! applet_id={}", applet_id);
  1152. IPC::ResponseBuilder rb{ctx, 2};
  1153. rb.Push(ResultUnknown);
  1154. return;
  1155. }
  1156. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1157. rb.Push(ResultSuccess);
  1158. rb.PushIpcInterface<ILibraryAppletAccessor>(system, applet);
  1159. }
  1160. void ILibraryAppletCreator::CreateStorage(HLERequestContext& ctx) {
  1161. IPC::RequestParser rp{ctx};
  1162. const s64 size{rp.Pop<s64>()};
  1163. LOG_DEBUG(Service_AM, "called, size={}", size);
  1164. if (size <= 0) {
  1165. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  1166. IPC::ResponseBuilder rb{ctx, 2};
  1167. rb.Push(ResultUnknown);
  1168. return;
  1169. }
  1170. std::vector<u8> buffer(size);
  1171. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1172. rb.Push(ResultSuccess);
  1173. rb.PushIpcInterface<IStorage>(system, std::move(buffer));
  1174. }
  1175. void ILibraryAppletCreator::CreateTransferMemoryStorage(HLERequestContext& ctx) {
  1176. IPC::RequestParser rp{ctx};
  1177. struct Parameters {
  1178. u8 permissions;
  1179. s64 size;
  1180. };
  1181. const auto parameters{rp.PopRaw<Parameters>()};
  1182. const auto handle{ctx.GetCopyHandle(0)};
  1183. LOG_DEBUG(Service_AM, "called, permissions={}, size={}, handle={:08X}", parameters.permissions,
  1184. parameters.size, handle);
  1185. if (parameters.size <= 0) {
  1186. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  1187. IPC::ResponseBuilder rb{ctx, 2};
  1188. rb.Push(ResultUnknown);
  1189. return;
  1190. }
  1191. auto transfer_mem = ctx.GetObjectFromHandle<Kernel::KTransferMemory>(handle);
  1192. if (transfer_mem.IsNull()) {
  1193. LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle);
  1194. IPC::ResponseBuilder rb{ctx, 2};
  1195. rb.Push(ResultUnknown);
  1196. return;
  1197. }
  1198. std::vector<u8> memory(transfer_mem->GetSize());
  1199. ctx.GetMemory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(), memory.size());
  1200. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1201. rb.Push(ResultSuccess);
  1202. rb.PushIpcInterface<IStorage>(system, std::move(memory));
  1203. }
  1204. void ILibraryAppletCreator::CreateHandleStorage(HLERequestContext& ctx) {
  1205. IPC::RequestParser rp{ctx};
  1206. const s64 size{rp.Pop<s64>()};
  1207. const auto handle{ctx.GetCopyHandle(0)};
  1208. LOG_DEBUG(Service_AM, "called, size={}, handle={:08X}", size, handle);
  1209. if (size <= 0) {
  1210. LOG_ERROR(Service_AM, "size is less than or equal to 0");
  1211. IPC::ResponseBuilder rb{ctx, 2};
  1212. rb.Push(ResultUnknown);
  1213. return;
  1214. }
  1215. auto transfer_mem = ctx.GetObjectFromHandle<Kernel::KTransferMemory>(handle);
  1216. if (transfer_mem.IsNull()) {
  1217. LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle);
  1218. IPC::ResponseBuilder rb{ctx, 2};
  1219. rb.Push(ResultUnknown);
  1220. return;
  1221. }
  1222. std::vector<u8> memory(transfer_mem->GetSize());
  1223. ctx.GetMemory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(), memory.size());
  1224. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1225. rb.Push(ResultSuccess);
  1226. rb.PushIpcInterface<IStorage>(system, std::move(memory));
  1227. }
  1228. ILibraryAppletSelfAccessor::ILibraryAppletSelfAccessor(Core::System& system_)
  1229. : ServiceFramework{system_, "ILibraryAppletSelfAccessor"} {
  1230. // clang-format off
  1231. static const FunctionInfo functions[] = {
  1232. {0, &ILibraryAppletSelfAccessor::PopInData, "PopInData"},
  1233. {1, &ILibraryAppletSelfAccessor::PushOutData, "PushOutData"},
  1234. {2, nullptr, "PopInteractiveInData"},
  1235. {3, nullptr, "PushInteractiveOutData"},
  1236. {5, nullptr, "GetPopInDataEvent"},
  1237. {6, nullptr, "GetPopInteractiveInDataEvent"},
  1238. {10, &ILibraryAppletSelfAccessor::ExitProcessAndReturn, "ExitProcessAndReturn"},
  1239. {11, &ILibraryAppletSelfAccessor::GetLibraryAppletInfo, "GetLibraryAppletInfo"},
  1240. {12, &ILibraryAppletSelfAccessor::GetMainAppletIdentityInfo, "GetMainAppletIdentityInfo"},
  1241. {13, nullptr, "CanUseApplicationCore"},
  1242. {14, &ILibraryAppletSelfAccessor::GetCallerAppletIdentityInfo, "GetCallerAppletIdentityInfo"},
  1243. {15, nullptr, "GetMainAppletApplicationControlProperty"},
  1244. {16, nullptr, "GetMainAppletStorageId"},
  1245. {17, nullptr, "GetCallerAppletIdentityInfoStack"},
  1246. {18, nullptr, "GetNextReturnDestinationAppletIdentityInfo"},
  1247. {19, &ILibraryAppletSelfAccessor::GetDesirableKeyboardLayout, "GetDesirableKeyboardLayout"},
  1248. {20, nullptr, "PopExtraStorage"},
  1249. {25, nullptr, "GetPopExtraStorageEvent"},
  1250. {30, nullptr, "UnpopInData"},
  1251. {31, nullptr, "UnpopExtraStorage"},
  1252. {40, nullptr, "GetIndirectLayerProducerHandle"},
  1253. {50, nullptr, "ReportVisibleError"},
  1254. {51, nullptr, "ReportVisibleErrorWithErrorContext"},
  1255. {60, nullptr, "GetMainAppletApplicationDesiredLanguage"},
  1256. {70, nullptr, "GetCurrentApplicationId"},
  1257. {80, nullptr, "RequestExitToSelf"},
  1258. {90, nullptr, "CreateApplicationAndPushAndRequestToLaunch"},
  1259. {100, nullptr, "CreateGameMovieTrimmer"},
  1260. {101, nullptr, "ReserveResourceForMovieOperation"},
  1261. {102, nullptr, "UnreserveResourceForMovieOperation"},
  1262. {110, &ILibraryAppletSelfAccessor::GetMainAppletAvailableUsers, "GetMainAppletAvailableUsers"},
  1263. {120, nullptr, "GetLaunchStorageInfoForDebug"},
  1264. {130, nullptr, "GetGpuErrorDetectedSystemEvent"},
  1265. {140, nullptr, "SetApplicationMemoryReservation"},
  1266. {150, &ILibraryAppletSelfAccessor::ShouldSetGpuTimeSliceManually, "ShouldSetGpuTimeSliceManually"},
  1267. };
  1268. // clang-format on
  1269. RegisterHandlers(functions);
  1270. switch (system.GetAppletManager().GetCurrentAppletId()) {
  1271. case Applets::AppletId::Cabinet:
  1272. PushInShowCabinetData();
  1273. break;
  1274. case Applets::AppletId::MiiEdit:
  1275. PushInShowMiiEditData();
  1276. break;
  1277. case Applets::AppletId::PhotoViewer:
  1278. PushInShowAlbum();
  1279. break;
  1280. case Applets::AppletId::SoftwareKeyboard:
  1281. PushInShowSoftwareKeyboard();
  1282. break;
  1283. case Applets::AppletId::Controller:
  1284. PushInShowController();
  1285. break;
  1286. default:
  1287. break;
  1288. }
  1289. }
  1290. ILibraryAppletSelfAccessor::~ILibraryAppletSelfAccessor() = default;
  1291. void ILibraryAppletSelfAccessor::PopInData(HLERequestContext& ctx) {
  1292. LOG_INFO(Service_AM, "called");
  1293. if (queue_data.empty()) {
  1294. IPC::ResponseBuilder rb{ctx, 2};
  1295. rb.Push(ResultNoDataInChannel);
  1296. return;
  1297. }
  1298. auto data = queue_data.front();
  1299. queue_data.pop_front();
  1300. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1301. rb.Push(ResultSuccess);
  1302. rb.PushIpcInterface<IStorage>(system, std::move(data));
  1303. }
  1304. void ILibraryAppletSelfAccessor::PushOutData(HLERequestContext& ctx) {
  1305. LOG_WARNING(Service_AM, "(STUBBED) called");
  1306. IPC::ResponseBuilder rb{ctx, 2};
  1307. rb.Push(ResultSuccess);
  1308. }
  1309. void ILibraryAppletSelfAccessor::ExitProcessAndReturn(HLERequestContext& ctx) {
  1310. LOG_WARNING(Service_AM, "(STUBBED) called");
  1311. system.Exit();
  1312. IPC::ResponseBuilder rb{ctx, 2};
  1313. rb.Push(ResultSuccess);
  1314. }
  1315. void ILibraryAppletSelfAccessor::GetLibraryAppletInfo(HLERequestContext& ctx) {
  1316. struct LibraryAppletInfo {
  1317. Applets::AppletId applet_id;
  1318. Applets::LibraryAppletMode library_applet_mode;
  1319. };
  1320. LOG_WARNING(Service_AM, "(STUBBED) called");
  1321. const LibraryAppletInfo applet_info{
  1322. .applet_id = system.GetAppletManager().GetCurrentAppletId(),
  1323. .library_applet_mode = Applets::LibraryAppletMode::AllForeground,
  1324. };
  1325. IPC::ResponseBuilder rb{ctx, 4};
  1326. rb.Push(ResultSuccess);
  1327. rb.PushRaw(applet_info);
  1328. }
  1329. void ILibraryAppletSelfAccessor::GetMainAppletIdentityInfo(HLERequestContext& ctx) {
  1330. struct AppletIdentityInfo {
  1331. Applets::AppletId applet_id;
  1332. INSERT_PADDING_BYTES(0x4);
  1333. u64 application_id;
  1334. };
  1335. static_assert(sizeof(AppletIdentityInfo) == 0x10, "AppletIdentityInfo has incorrect size.");
  1336. LOG_WARNING(Service_AM, "(STUBBED) called");
  1337. const AppletIdentityInfo applet_info{
  1338. .applet_id = Applets::AppletId::QLaunch,
  1339. .application_id = 0x0100000000001000ull,
  1340. };
  1341. IPC::ResponseBuilder rb{ctx, 6};
  1342. rb.Push(ResultSuccess);
  1343. rb.PushRaw(applet_info);
  1344. }
  1345. void ILibraryAppletSelfAccessor::GetCallerAppletIdentityInfo(HLERequestContext& ctx) {
  1346. struct AppletIdentityInfo {
  1347. Applets::AppletId applet_id;
  1348. INSERT_PADDING_BYTES(0x4);
  1349. u64 application_id;
  1350. };
  1351. static_assert(sizeof(AppletIdentityInfo) == 0x10, "AppletIdentityInfo has incorrect size.");
  1352. LOG_WARNING(Service_AM, "(STUBBED) called");
  1353. const AppletIdentityInfo applet_info{
  1354. .applet_id = Applets::AppletId::QLaunch,
  1355. .application_id = 0x0100000000001000ull,
  1356. };
  1357. IPC::ResponseBuilder rb{ctx, 6};
  1358. rb.Push(ResultSuccess);
  1359. rb.PushRaw(applet_info);
  1360. }
  1361. void ILibraryAppletSelfAccessor::GetDesirableKeyboardLayout(HLERequestContext& ctx) {
  1362. LOG_WARNING(Service_AM, "(STUBBED) called");
  1363. IPC::ResponseBuilder rb{ctx, 3};
  1364. rb.Push(ResultSuccess);
  1365. rb.Push<u32>(0);
  1366. }
  1367. void ILibraryAppletSelfAccessor::GetMainAppletAvailableUsers(HLERequestContext& ctx) {
  1368. const Service::Account::ProfileManager manager{};
  1369. bool is_empty{true};
  1370. s32 user_count{-1};
  1371. LOG_INFO(Service_AM, "called");
  1372. if (manager.GetUserCount() > 0) {
  1373. is_empty = false;
  1374. user_count = static_cast<s32>(manager.GetUserCount());
  1375. ctx.WriteBuffer(manager.GetAllUsers());
  1376. }
  1377. IPC::ResponseBuilder rb{ctx, 4};
  1378. rb.Push(ResultSuccess);
  1379. rb.Push<u8>(is_empty);
  1380. rb.Push(user_count);
  1381. }
  1382. void ILibraryAppletSelfAccessor::ShouldSetGpuTimeSliceManually(HLERequestContext& ctx) {
  1383. LOG_WARNING(Service_AM, "(STUBBED) called");
  1384. IPC::ResponseBuilder rb{ctx, 2};
  1385. rb.Push(ResultSuccess);
  1386. rb.Push<u8>(0);
  1387. }
  1388. void ILibraryAppletSelfAccessor::PushInShowAlbum() {
  1389. const Applets::CommonArguments arguments{
  1390. .arguments_version = Applets::CommonArgumentVersion::Version3,
  1391. .size = Applets::CommonArgumentSize::Version3,
  1392. .library_version = 1,
  1393. .theme_color = Applets::ThemeColor::BasicBlack,
  1394. .play_startup_sound = true,
  1395. .system_tick = system.CoreTiming().GetClockTicks(),
  1396. };
  1397. std::vector<u8> argument_data(sizeof(arguments));
  1398. std::vector<u8> settings_data{2};
  1399. std::memcpy(argument_data.data(), &arguments, sizeof(arguments));
  1400. queue_data.emplace_back(std::move(argument_data));
  1401. queue_data.emplace_back(std::move(settings_data));
  1402. }
  1403. void ILibraryAppletSelfAccessor::PushInShowController() {
  1404. const Applets::CommonArguments common_args = {
  1405. .arguments_version = Applets::CommonArgumentVersion::Version3,
  1406. .size = Applets::CommonArgumentSize::Version3,
  1407. .library_version = static_cast<u32>(Applets::ControllerAppletVersion::Version8),
  1408. .theme_color = Applets::ThemeColor::BasicBlack,
  1409. .play_startup_sound = true,
  1410. .system_tick = system.CoreTiming().GetClockTicks(),
  1411. };
  1412. Applets::ControllerSupportArgNew user_args = {
  1413. .header = {.player_count_min = 1,
  1414. .player_count_max = 4,
  1415. .enable_take_over_connection = true,
  1416. .enable_left_justify = false,
  1417. .enable_permit_joy_dual = true,
  1418. .enable_single_mode = false,
  1419. .enable_identification_color = false},
  1420. .identification_colors = {},
  1421. .enable_explain_text = false,
  1422. .explain_text = {},
  1423. };
  1424. Applets::ControllerSupportArgPrivate private_args = {
  1425. .arg_private_size = sizeof(Applets::ControllerSupportArgPrivate),
  1426. .arg_size = sizeof(Applets::ControllerSupportArgNew),
  1427. .is_home_menu = true,
  1428. .flag_1 = true,
  1429. .mode = Applets::ControllerSupportMode::ShowControllerSupport,
  1430. .caller = Applets::ControllerSupportCaller::
  1431. Application, // switchbrew: Always zero except with
  1432. // ShowControllerFirmwareUpdateForSystem/ShowControllerKeyRemappingForSystem,
  1433. // which sets this to the input param
  1434. .style_set = Core::HID::NpadStyleSet::None,
  1435. .joy_hold_type = 0,
  1436. };
  1437. std::vector<u8> common_args_data(sizeof(common_args));
  1438. std::vector<u8> private_args_data(sizeof(private_args));
  1439. std::vector<u8> user_args_data(sizeof(user_args));
  1440. std::memcpy(common_args_data.data(), &common_args, sizeof(common_args));
  1441. std::memcpy(private_args_data.data(), &private_args, sizeof(private_args));
  1442. std::memcpy(user_args_data.data(), &user_args, sizeof(user_args));
  1443. queue_data.emplace_back(std::move(common_args_data));
  1444. queue_data.emplace_back(std::move(private_args_data));
  1445. queue_data.emplace_back(std::move(user_args_data));
  1446. }
  1447. void ILibraryAppletSelfAccessor::PushInShowCabinetData() {
  1448. const Applets::CommonArguments arguments{
  1449. .arguments_version = Applets::CommonArgumentVersion::Version3,
  1450. .size = Applets::CommonArgumentSize::Version3,
  1451. .library_version = static_cast<u32>(Applets::CabinetAppletVersion::Version1),
  1452. .theme_color = Applets::ThemeColor::BasicBlack,
  1453. .play_startup_sound = true,
  1454. .system_tick = system.CoreTiming().GetClockTicks(),
  1455. };
  1456. const Applets::StartParamForAmiiboSettings amiibo_settings{
  1457. .param_1 = 0,
  1458. .applet_mode = system.GetAppletManager().GetCabinetMode(),
  1459. .flags = Applets::CabinetFlags::None,
  1460. .amiibo_settings_1 = 0,
  1461. .device_handle = 0,
  1462. .tag_info{},
  1463. .register_info{},
  1464. .amiibo_settings_3{},
  1465. };
  1466. std::vector<u8> argument_data(sizeof(arguments));
  1467. std::vector<u8> settings_data(sizeof(amiibo_settings));
  1468. std::memcpy(argument_data.data(), &arguments, sizeof(arguments));
  1469. std::memcpy(settings_data.data(), &amiibo_settings, sizeof(amiibo_settings));
  1470. queue_data.emplace_back(std::move(argument_data));
  1471. queue_data.emplace_back(std::move(settings_data));
  1472. }
  1473. void ILibraryAppletSelfAccessor::PushInShowMiiEditData() {
  1474. struct MiiEditV3 {
  1475. Applets::MiiEditAppletInputCommon common;
  1476. Applets::MiiEditAppletInputV3 input;
  1477. };
  1478. static_assert(sizeof(MiiEditV3) == 0x100, "MiiEditV3 has incorrect size.");
  1479. MiiEditV3 mii_arguments{
  1480. .common =
  1481. {
  1482. .version = Applets::MiiEditAppletVersion::Version3,
  1483. .applet_mode = Applets::MiiEditAppletMode::ShowMiiEdit,
  1484. },
  1485. .input{},
  1486. };
  1487. std::vector<u8> argument_data(sizeof(mii_arguments));
  1488. std::memcpy(argument_data.data(), &mii_arguments, sizeof(mii_arguments));
  1489. queue_data.emplace_back(std::move(argument_data));
  1490. }
  1491. void ILibraryAppletSelfAccessor::PushInShowSoftwareKeyboard() {
  1492. const Applets::CommonArguments arguments{
  1493. .arguments_version = Applets::CommonArgumentVersion::Version3,
  1494. .size = Applets::CommonArgumentSize::Version3,
  1495. .library_version = static_cast<u32>(Applets::SwkbdAppletVersion::Version524301),
  1496. .theme_color = Applets::ThemeColor::BasicBlack,
  1497. .play_startup_sound = true,
  1498. .system_tick = system.CoreTiming().GetClockTicks(),
  1499. };
  1500. std::vector<char16_t> initial_string(0);
  1501. const Applets::SwkbdConfigCommon swkbd_config{
  1502. .type = Applets::SwkbdType::Qwerty,
  1503. .ok_text{},
  1504. .left_optional_symbol_key{},
  1505. .right_optional_symbol_key{},
  1506. .use_prediction = false,
  1507. .key_disable_flags{},
  1508. .initial_cursor_position = Applets::SwkbdInitialCursorPosition::Start,
  1509. .header_text{},
  1510. .sub_text{},
  1511. .guide_text{},
  1512. .max_text_length = 500,
  1513. .min_text_length = 0,
  1514. .password_mode = Applets::SwkbdPasswordMode::Disabled,
  1515. .text_draw_type = Applets::SwkbdTextDrawType::Box,
  1516. .enable_return_button = true,
  1517. .use_utf8 = false,
  1518. .use_blur_background = true,
  1519. .initial_string_offset{},
  1520. .initial_string_length = static_cast<u32>(initial_string.size()),
  1521. .user_dictionary_offset{},
  1522. .user_dictionary_entries{},
  1523. .use_text_check = false,
  1524. };
  1525. Applets::SwkbdConfigNew swkbd_config_new{};
  1526. std::vector<u8> argument_data(sizeof(arguments));
  1527. std::vector<u8> swkbd_data(sizeof(swkbd_config) + sizeof(swkbd_config_new));
  1528. std::vector<u8> work_buffer(swkbd_config.initial_string_length * sizeof(char16_t));
  1529. std::memcpy(argument_data.data(), &arguments, sizeof(arguments));
  1530. std::memcpy(swkbd_data.data(), &swkbd_config, sizeof(swkbd_config));
  1531. std::memcpy(swkbd_data.data() + sizeof(swkbd_config), &swkbd_config_new,
  1532. sizeof(Applets::SwkbdConfigNew));
  1533. std::memcpy(work_buffer.data(), initial_string.data(),
  1534. swkbd_config.initial_string_length * sizeof(char16_t));
  1535. queue_data.emplace_back(std::move(argument_data));
  1536. queue_data.emplace_back(std::move(swkbd_data));
  1537. queue_data.emplace_back(std::move(work_buffer));
  1538. }
  1539. IAppletCommonFunctions::IAppletCommonFunctions(Core::System& system_)
  1540. : ServiceFramework{system_, "IAppletCommonFunctions"} {
  1541. // clang-format off
  1542. static const FunctionInfo functions[] = {
  1543. {0, nullptr, "SetTerminateResult"},
  1544. {10, nullptr, "ReadThemeStorage"},
  1545. {11, nullptr, "WriteThemeStorage"},
  1546. {20, nullptr, "PushToAppletBoundChannel"},
  1547. {21, nullptr, "TryPopFromAppletBoundChannel"},
  1548. {40, nullptr, "GetDisplayLogicalResolution"},
  1549. {42, nullptr, "SetDisplayMagnification"},
  1550. {50, nullptr, "SetHomeButtonDoubleClickEnabled"},
  1551. {51, nullptr, "GetHomeButtonDoubleClickEnabled"},
  1552. {52, nullptr, "IsHomeButtonShortPressedBlocked"},
  1553. {60, nullptr, "IsVrModeCurtainRequired"},
  1554. {61, nullptr, "IsSleepRequiredByHighTemperature"},
  1555. {62, nullptr, "IsSleepRequiredByLowBattery"},
  1556. {70, &IAppletCommonFunctions::SetCpuBoostRequestPriority, "SetCpuBoostRequestPriority"},
  1557. {80, nullptr, "SetHandlingCaptureButtonShortPressedMessageEnabledForApplet"},
  1558. {81, nullptr, "SetHandlingCaptureButtonLongPressedMessageEnabledForApplet"},
  1559. {90, nullptr, "OpenNamedChannelAsParent"},
  1560. {91, nullptr, "OpenNamedChannelAsChild"},
  1561. {100, nullptr, "SetApplicationCoreUsageMode"},
  1562. };
  1563. // clang-format on
  1564. RegisterHandlers(functions);
  1565. }
  1566. IAppletCommonFunctions::~IAppletCommonFunctions() = default;
  1567. void IAppletCommonFunctions::SetCpuBoostRequestPriority(HLERequestContext& ctx) {
  1568. LOG_WARNING(Service_AM, "(STUBBED) called");
  1569. IPC::ResponseBuilder rb{ctx, 2};
  1570. rb.Push(ResultSuccess);
  1571. }
  1572. IApplicationFunctions::IApplicationFunctions(Core::System& system_)
  1573. : ServiceFramework{system_, "IApplicationFunctions"}, service_context{system,
  1574. "IApplicationFunctions"} {
  1575. // clang-format off
  1576. static const FunctionInfo functions[] = {
  1577. {1, &IApplicationFunctions::PopLaunchParameter, "PopLaunchParameter"},
  1578. {10, nullptr, "CreateApplicationAndPushAndRequestToStart"},
  1579. {11, nullptr, "CreateApplicationAndPushAndRequestToStartForQuest"},
  1580. {12, nullptr, "CreateApplicationAndRequestToStart"},
  1581. {13, &IApplicationFunctions::CreateApplicationAndRequestToStartForQuest, "CreateApplicationAndRequestToStartForQuest"},
  1582. {14, nullptr, "CreateApplicationWithAttributeAndPushAndRequestToStartForQuest"},
  1583. {15, nullptr, "CreateApplicationWithAttributeAndRequestToStartForQuest"},
  1584. {20, &IApplicationFunctions::EnsureSaveData, "EnsureSaveData"},
  1585. {21, &IApplicationFunctions::GetDesiredLanguage, "GetDesiredLanguage"},
  1586. {22, &IApplicationFunctions::SetTerminateResult, "SetTerminateResult"},
  1587. {23, &IApplicationFunctions::GetDisplayVersion, "GetDisplayVersion"},
  1588. {24, nullptr, "GetLaunchStorageInfoForDebug"},
  1589. {25, &IApplicationFunctions::ExtendSaveData, "ExtendSaveData"},
  1590. {26, &IApplicationFunctions::GetSaveDataSize, "GetSaveDataSize"},
  1591. {27, &IApplicationFunctions::CreateCacheStorage, "CreateCacheStorage"},
  1592. {28, &IApplicationFunctions::GetSaveDataSizeMax, "GetSaveDataSizeMax"},
  1593. {29, nullptr, "GetCacheStorageMax"},
  1594. {30, &IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed, "BeginBlockingHomeButtonShortAndLongPressed"},
  1595. {31, &IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed, "EndBlockingHomeButtonShortAndLongPressed"},
  1596. {32, &IApplicationFunctions::BeginBlockingHomeButton, "BeginBlockingHomeButton"},
  1597. {33, &IApplicationFunctions::EndBlockingHomeButton, "EndBlockingHomeButton"},
  1598. {34, nullptr, "SelectApplicationLicense"},
  1599. {35, nullptr, "GetDeviceSaveDataSizeMax"},
  1600. {36, nullptr, "GetLimitedApplicationLicense"},
  1601. {37, nullptr, "GetLimitedApplicationLicenseUpgradableEvent"},
  1602. {40, &IApplicationFunctions::NotifyRunning, "NotifyRunning"},
  1603. {50, &IApplicationFunctions::GetPseudoDeviceId, "GetPseudoDeviceId"},
  1604. {60, nullptr, "SetMediaPlaybackStateForApplication"},
  1605. {65, &IApplicationFunctions::IsGamePlayRecordingSupported, "IsGamePlayRecordingSupported"},
  1606. {66, &IApplicationFunctions::InitializeGamePlayRecording, "InitializeGamePlayRecording"},
  1607. {67, &IApplicationFunctions::SetGamePlayRecordingState, "SetGamePlayRecordingState"},
  1608. {68, nullptr, "RequestFlushGamePlayingMovieForDebug"},
  1609. {70, nullptr, "RequestToShutdown"},
  1610. {71, nullptr, "RequestToReboot"},
  1611. {72, nullptr, "RequestToSleep"},
  1612. {80, nullptr, "ExitAndRequestToShowThanksMessage"},
  1613. {90, &IApplicationFunctions::EnableApplicationCrashReport, "EnableApplicationCrashReport"},
  1614. {100, &IApplicationFunctions::InitializeApplicationCopyrightFrameBuffer, "InitializeApplicationCopyrightFrameBuffer"},
  1615. {101, &IApplicationFunctions::SetApplicationCopyrightImage, "SetApplicationCopyrightImage"},
  1616. {102, &IApplicationFunctions::SetApplicationCopyrightVisibility, "SetApplicationCopyrightVisibility"},
  1617. {110, &IApplicationFunctions::QueryApplicationPlayStatistics, "QueryApplicationPlayStatistics"},
  1618. {111, &IApplicationFunctions::QueryApplicationPlayStatisticsByUid, "QueryApplicationPlayStatisticsByUid"},
  1619. {120, &IApplicationFunctions::ExecuteProgram, "ExecuteProgram"},
  1620. {121, &IApplicationFunctions::ClearUserChannel, "ClearUserChannel"},
  1621. {122, &IApplicationFunctions::UnpopToUserChannel, "UnpopToUserChannel"},
  1622. {123, &IApplicationFunctions::GetPreviousProgramIndex, "GetPreviousProgramIndex"},
  1623. {124, nullptr, "EnableApplicationAllThreadDumpOnCrash"},
  1624. {130, &IApplicationFunctions::GetGpuErrorDetectedSystemEvent, "GetGpuErrorDetectedSystemEvent"},
  1625. {131, nullptr, "SetDelayTimeToAbortOnGpuError"},
  1626. {140, &IApplicationFunctions::GetFriendInvitationStorageChannelEvent, "GetFriendInvitationStorageChannelEvent"},
  1627. {141, &IApplicationFunctions::TryPopFromFriendInvitationStorageChannel, "TryPopFromFriendInvitationStorageChannel"},
  1628. {150, &IApplicationFunctions::GetNotificationStorageChannelEvent, "GetNotificationStorageChannelEvent"},
  1629. {151, nullptr, "TryPopFromNotificationStorageChannel"},
  1630. {160, &IApplicationFunctions::GetHealthWarningDisappearedSystemEvent, "GetHealthWarningDisappearedSystemEvent"},
  1631. {170, nullptr, "SetHdcpAuthenticationActivated"},
  1632. {180, nullptr, "GetLaunchRequiredVersion"},
  1633. {181, nullptr, "UpgradeLaunchRequiredVersion"},
  1634. {190, nullptr, "SendServerMaintenanceOverlayNotification"},
  1635. {200, nullptr, "GetLastApplicationExitReason"},
  1636. {500, nullptr, "StartContinuousRecordingFlushForDebug"},
  1637. {1000, nullptr, "CreateMovieMaker"},
  1638. {1001, &IApplicationFunctions::PrepareForJit, "PrepareForJit"},
  1639. };
  1640. // clang-format on
  1641. RegisterHandlers(functions);
  1642. gpu_error_detected_event =
  1643. service_context.CreateEvent("IApplicationFunctions:GpuErrorDetectedSystemEvent");
  1644. friend_invitation_storage_channel_event =
  1645. service_context.CreateEvent("IApplicationFunctions:FriendInvitationStorageChannelEvent");
  1646. notification_storage_channel_event =
  1647. service_context.CreateEvent("IApplicationFunctions:NotificationStorageChannelEvent");
  1648. health_warning_disappeared_system_event =
  1649. service_context.CreateEvent("IApplicationFunctions:HealthWarningDisappearedSystemEvent");
  1650. }
  1651. IApplicationFunctions::~IApplicationFunctions() {
  1652. service_context.CloseEvent(gpu_error_detected_event);
  1653. service_context.CloseEvent(friend_invitation_storage_channel_event);
  1654. service_context.CloseEvent(notification_storage_channel_event);
  1655. service_context.CloseEvent(health_warning_disappeared_system_event);
  1656. }
  1657. void IApplicationFunctions::EnableApplicationCrashReport(HLERequestContext& ctx) {
  1658. LOG_WARNING(Service_AM, "(STUBBED) called");
  1659. IPC::ResponseBuilder rb{ctx, 2};
  1660. rb.Push(ResultSuccess);
  1661. }
  1662. void IApplicationFunctions::InitializeApplicationCopyrightFrameBuffer(HLERequestContext& ctx) {
  1663. LOG_WARNING(Service_AM, "(STUBBED) called");
  1664. IPC::ResponseBuilder rb{ctx, 2};
  1665. rb.Push(ResultSuccess);
  1666. }
  1667. void IApplicationFunctions::SetApplicationCopyrightImage(HLERequestContext& ctx) {
  1668. LOG_WARNING(Service_AM, "(STUBBED) called");
  1669. IPC::ResponseBuilder rb{ctx, 2};
  1670. rb.Push(ResultSuccess);
  1671. }
  1672. void IApplicationFunctions::SetApplicationCopyrightVisibility(HLERequestContext& ctx) {
  1673. IPC::RequestParser rp{ctx};
  1674. const auto is_visible = rp.Pop<bool>();
  1675. LOG_WARNING(Service_AM, "(STUBBED) called, is_visible={}", is_visible);
  1676. IPC::ResponseBuilder rb{ctx, 2};
  1677. rb.Push(ResultSuccess);
  1678. }
  1679. void IApplicationFunctions::BeginBlockingHomeButtonShortAndLongPressed(HLERequestContext& ctx) {
  1680. LOG_WARNING(Service_AM, "(STUBBED) called");
  1681. IPC::ResponseBuilder rb{ctx, 2};
  1682. rb.Push(ResultSuccess);
  1683. }
  1684. void IApplicationFunctions::EndBlockingHomeButtonShortAndLongPressed(HLERequestContext& ctx) {
  1685. LOG_WARNING(Service_AM, "(STUBBED) called");
  1686. IPC::ResponseBuilder rb{ctx, 2};
  1687. rb.Push(ResultSuccess);
  1688. }
  1689. void IApplicationFunctions::BeginBlockingHomeButton(HLERequestContext& ctx) {
  1690. LOG_WARNING(Service_AM, "(STUBBED) called");
  1691. IPC::ResponseBuilder rb{ctx, 2};
  1692. rb.Push(ResultSuccess);
  1693. }
  1694. void IApplicationFunctions::EndBlockingHomeButton(HLERequestContext& ctx) {
  1695. LOG_WARNING(Service_AM, "(STUBBED) called");
  1696. IPC::ResponseBuilder rb{ctx, 2};
  1697. rb.Push(ResultSuccess);
  1698. }
  1699. void IApplicationFunctions::PopLaunchParameter(HLERequestContext& ctx) {
  1700. IPC::RequestParser rp{ctx};
  1701. const auto kind = rp.PopEnum<LaunchParameterKind>();
  1702. LOG_INFO(Service_AM, "called, kind={:08X}", kind);
  1703. if (kind == LaunchParameterKind::UserChannel) {
  1704. auto channel = system.GetUserChannel();
  1705. if (channel.empty()) {
  1706. LOG_ERROR(Service_AM, "Attempted to load launch parameter but none was found!");
  1707. IPC::ResponseBuilder rb{ctx, 2};
  1708. rb.Push(AM::ResultNoDataInChannel);
  1709. return;
  1710. }
  1711. auto data = channel.back();
  1712. channel.pop_back();
  1713. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1714. rb.Push(ResultSuccess);
  1715. rb.PushIpcInterface<IStorage>(system, std::move(data));
  1716. } else if (kind == LaunchParameterKind::AccountPreselectedUser &&
  1717. !launch_popped_account_preselect) {
  1718. // TODO: Verify this is hw-accurate
  1719. LaunchParameterAccountPreselectedUser params{};
  1720. params.magic = LAUNCH_PARAMETER_ACCOUNT_PRESELECTED_USER_MAGIC;
  1721. params.is_account_selected = 1;
  1722. Account::ProfileManager profile_manager{};
  1723. const auto uuid = profile_manager.GetUser(static_cast<s32>(Settings::values.current_user));
  1724. ASSERT(uuid.has_value() && uuid->IsValid());
  1725. params.current_user = *uuid;
  1726. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1727. rb.Push(ResultSuccess);
  1728. std::vector<u8> buffer(sizeof(LaunchParameterAccountPreselectedUser));
  1729. std::memcpy(buffer.data(), &params, buffer.size());
  1730. rb.PushIpcInterface<IStorage>(system, std::move(buffer));
  1731. launch_popped_account_preselect = true;
  1732. } else {
  1733. LOG_ERROR(Service_AM, "Unknown launch parameter kind.");
  1734. IPC::ResponseBuilder rb{ctx, 2};
  1735. rb.Push(AM::ResultNoDataInChannel);
  1736. }
  1737. }
  1738. void IApplicationFunctions::CreateApplicationAndRequestToStartForQuest(HLERequestContext& ctx) {
  1739. LOG_WARNING(Service_AM, "(STUBBED) called");
  1740. IPC::ResponseBuilder rb{ctx, 2};
  1741. rb.Push(ResultSuccess);
  1742. }
  1743. void IApplicationFunctions::EnsureSaveData(HLERequestContext& ctx) {
  1744. IPC::RequestParser rp{ctx};
  1745. u128 user_id = rp.PopRaw<u128>();
  1746. LOG_DEBUG(Service_AM, "called, uid={:016X}{:016X}", user_id[1], user_id[0]);
  1747. FileSys::SaveDataAttribute attribute{};
  1748. attribute.title_id = system.GetApplicationProcessProgramID();
  1749. attribute.user_id = user_id;
  1750. attribute.type = FileSys::SaveDataType::SaveData;
  1751. FileSys::VirtualDir save_data{};
  1752. const auto res = system.GetFileSystemController().OpenSaveDataController()->CreateSaveData(
  1753. &save_data, FileSys::SaveDataSpaceId::NandUser, attribute);
  1754. IPC::ResponseBuilder rb{ctx, 4};
  1755. rb.Push(res);
  1756. rb.Push<u64>(0);
  1757. }
  1758. void IApplicationFunctions::SetTerminateResult(HLERequestContext& ctx) {
  1759. // Takes an input u32 Result, no output.
  1760. // For example, in some cases official apps use this with error 0x2A2 then
  1761. // uses svcBreak.
  1762. IPC::RequestParser rp{ctx};
  1763. u32 result = rp.Pop<u32>();
  1764. LOG_WARNING(Service_AM, "(STUBBED) called, result=0x{:08X}", result);
  1765. IPC::ResponseBuilder rb{ctx, 2};
  1766. rb.Push(ResultSuccess);
  1767. }
  1768. void IApplicationFunctions::GetDisplayVersion(HLERequestContext& ctx) {
  1769. LOG_DEBUG(Service_AM, "called");
  1770. std::array<u8, 0x10> version_string{};
  1771. const auto res = [this] {
  1772. const auto title_id = system.GetApplicationProcessProgramID();
  1773. const FileSys::PatchManager pm{title_id, system.GetFileSystemController(),
  1774. system.GetContentProvider()};
  1775. auto metadata = pm.GetControlMetadata();
  1776. if (metadata.first != nullptr) {
  1777. return metadata;
  1778. }
  1779. const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(title_id),
  1780. system.GetFileSystemController(),
  1781. system.GetContentProvider()};
  1782. return pm_update.GetControlMetadata();
  1783. }();
  1784. if (res.first != nullptr) {
  1785. const auto& version = res.first->GetVersionString();
  1786. std::copy(version.begin(), version.end(), version_string.begin());
  1787. } else {
  1788. static constexpr char default_version[]{"1.0.0"};
  1789. std::memcpy(version_string.data(), default_version, sizeof(default_version));
  1790. }
  1791. IPC::ResponseBuilder rb{ctx, 6};
  1792. rb.Push(ResultSuccess);
  1793. rb.PushRaw(version_string);
  1794. }
  1795. void IApplicationFunctions::GetDesiredLanguage(HLERequestContext& ctx) {
  1796. // TODO(bunnei): This should be configurable
  1797. LOG_DEBUG(Service_AM, "called");
  1798. // Get supported languages from NACP, if possible
  1799. // Default to 0 (all languages supported)
  1800. u32 supported_languages = 0;
  1801. const auto res = [this] {
  1802. const auto title_id = system.GetApplicationProcessProgramID();
  1803. const FileSys::PatchManager pm{title_id, system.GetFileSystemController(),
  1804. system.GetContentProvider()};
  1805. auto metadata = pm.GetControlMetadata();
  1806. if (metadata.first != nullptr) {
  1807. return metadata;
  1808. }
  1809. const FileSys::PatchManager pm_update{FileSys::GetUpdateTitleID(title_id),
  1810. system.GetFileSystemController(),
  1811. system.GetContentProvider()};
  1812. return pm_update.GetControlMetadata();
  1813. }();
  1814. if (res.first != nullptr) {
  1815. supported_languages = res.first->GetSupportedLanguages();
  1816. }
  1817. // Call IApplicationManagerInterface implementation.
  1818. auto& service_manager = system.ServiceManager();
  1819. auto ns_am2 = service_manager.GetService<NS::NS>("ns:am2");
  1820. auto app_man = ns_am2->GetApplicationManagerInterface();
  1821. // Get desired application language
  1822. u8 desired_language{};
  1823. const auto res_lang =
  1824. app_man->GetApplicationDesiredLanguage(&desired_language, supported_languages);
  1825. if (res_lang != ResultSuccess) {
  1826. IPC::ResponseBuilder rb{ctx, 2};
  1827. rb.Push(res_lang);
  1828. return;
  1829. }
  1830. // Convert to settings language code.
  1831. u64 language_code{};
  1832. const auto res_code =
  1833. app_man->ConvertApplicationLanguageToLanguageCode(&language_code, desired_language);
  1834. if (res_code != ResultSuccess) {
  1835. IPC::ResponseBuilder rb{ctx, 2};
  1836. rb.Push(res_code);
  1837. return;
  1838. }
  1839. LOG_DEBUG(Service_AM, "got desired_language={:016X}", language_code);
  1840. IPC::ResponseBuilder rb{ctx, 4};
  1841. rb.Push(ResultSuccess);
  1842. rb.Push(language_code);
  1843. }
  1844. void IApplicationFunctions::IsGamePlayRecordingSupported(HLERequestContext& ctx) {
  1845. LOG_WARNING(Service_AM, "(STUBBED) called");
  1846. constexpr bool gameplay_recording_supported = false;
  1847. IPC::ResponseBuilder rb{ctx, 3};
  1848. rb.Push(ResultSuccess);
  1849. rb.Push(gameplay_recording_supported);
  1850. }
  1851. void IApplicationFunctions::InitializeGamePlayRecording(HLERequestContext& ctx) {
  1852. LOG_WARNING(Service_AM, "(STUBBED) called");
  1853. IPC::ResponseBuilder rb{ctx, 2};
  1854. rb.Push(ResultSuccess);
  1855. }
  1856. void IApplicationFunctions::SetGamePlayRecordingState(HLERequestContext& ctx) {
  1857. LOG_WARNING(Service_AM, "(STUBBED) called");
  1858. IPC::ResponseBuilder rb{ctx, 2};
  1859. rb.Push(ResultSuccess);
  1860. }
  1861. void IApplicationFunctions::NotifyRunning(HLERequestContext& ctx) {
  1862. LOG_WARNING(Service_AM, "(STUBBED) called");
  1863. IPC::ResponseBuilder rb{ctx, 3};
  1864. rb.Push(ResultSuccess);
  1865. rb.Push<u8>(0); // Unknown, seems to be ignored by official processes
  1866. }
  1867. void IApplicationFunctions::GetPseudoDeviceId(HLERequestContext& ctx) {
  1868. LOG_WARNING(Service_AM, "(STUBBED) called");
  1869. IPC::ResponseBuilder rb{ctx, 6};
  1870. rb.Push(ResultSuccess);
  1871. // Returns a 128-bit UUID
  1872. rb.Push<u64>(0);
  1873. rb.Push<u64>(0);
  1874. }
  1875. void IApplicationFunctions::ExtendSaveData(HLERequestContext& ctx) {
  1876. struct Parameters {
  1877. FileSys::SaveDataType type;
  1878. u128 user_id;
  1879. u64 new_normal_size;
  1880. u64 new_journal_size;
  1881. };
  1882. static_assert(sizeof(Parameters) == 40);
  1883. IPC::RequestParser rp{ctx};
  1884. const auto [type, user_id, new_normal_size, new_journal_size] = rp.PopRaw<Parameters>();
  1885. LOG_DEBUG(Service_AM,
  1886. "called with type={:02X}, user_id={:016X}{:016X}, new_normal={:016X}, "
  1887. "new_journal={:016X}",
  1888. static_cast<u8>(type), user_id[1], user_id[0], new_normal_size, new_journal_size);
  1889. system.GetFileSystemController().OpenSaveDataController()->WriteSaveDataSize(
  1890. type, system.GetApplicationProcessProgramID(), user_id,
  1891. {new_normal_size, new_journal_size});
  1892. IPC::ResponseBuilder rb{ctx, 4};
  1893. rb.Push(ResultSuccess);
  1894. // The following value is used upon failure to help the system recover.
  1895. // Since we always succeed, this should be 0.
  1896. rb.Push<u64>(0);
  1897. }
  1898. void IApplicationFunctions::GetSaveDataSize(HLERequestContext& ctx) {
  1899. struct Parameters {
  1900. FileSys::SaveDataType type;
  1901. u128 user_id;
  1902. };
  1903. static_assert(sizeof(Parameters) == 24);
  1904. IPC::RequestParser rp{ctx};
  1905. const auto [type, user_id] = rp.PopRaw<Parameters>();
  1906. LOG_DEBUG(Service_AM, "called with type={:02X}, user_id={:016X}{:016X}", type, user_id[1],
  1907. user_id[0]);
  1908. const auto size = system.GetFileSystemController().OpenSaveDataController()->ReadSaveDataSize(
  1909. type, system.GetApplicationProcessProgramID(), user_id);
  1910. IPC::ResponseBuilder rb{ctx, 6};
  1911. rb.Push(ResultSuccess);
  1912. rb.Push(size.normal);
  1913. rb.Push(size.journal);
  1914. }
  1915. void IApplicationFunctions::CreateCacheStorage(HLERequestContext& ctx) {
  1916. struct InputParameters {
  1917. u16 index;
  1918. s64 size;
  1919. s64 journal_size;
  1920. };
  1921. static_assert(sizeof(InputParameters) == 24);
  1922. struct OutputParameters {
  1923. u32 storage_target;
  1924. u64 required_size;
  1925. };
  1926. static_assert(sizeof(OutputParameters) == 16);
  1927. IPC::RequestParser rp{ctx};
  1928. const auto params = rp.PopRaw<InputParameters>();
  1929. LOG_WARNING(Service_AM, "(STUBBED) called with index={}, size={:#x}, journal_size={:#x}",
  1930. params.index, params.size, params.journal_size);
  1931. const OutputParameters resp{
  1932. .storage_target = 1,
  1933. .required_size = 0,
  1934. };
  1935. IPC::ResponseBuilder rb{ctx, 6};
  1936. rb.Push(ResultSuccess);
  1937. rb.PushRaw(resp);
  1938. }
  1939. void IApplicationFunctions::GetSaveDataSizeMax(HLERequestContext& ctx) {
  1940. LOG_WARNING(Service_AM, "(STUBBED) called");
  1941. constexpr u64 size_max_normal = 0xFFFFFFF;
  1942. constexpr u64 size_max_journal = 0xFFFFFFF;
  1943. IPC::ResponseBuilder rb{ctx, 6};
  1944. rb.Push(ResultSuccess);
  1945. rb.Push(size_max_normal);
  1946. rb.Push(size_max_journal);
  1947. }
  1948. void IApplicationFunctions::QueryApplicationPlayStatistics(HLERequestContext& ctx) {
  1949. LOG_WARNING(Service_AM, "(STUBBED) called");
  1950. IPC::ResponseBuilder rb{ctx, 3};
  1951. rb.Push(ResultSuccess);
  1952. rb.Push<u32>(0);
  1953. }
  1954. void IApplicationFunctions::QueryApplicationPlayStatisticsByUid(HLERequestContext& ctx) {
  1955. LOG_WARNING(Service_AM, "(STUBBED) called");
  1956. IPC::ResponseBuilder rb{ctx, 3};
  1957. rb.Push(ResultSuccess);
  1958. rb.Push<u32>(0);
  1959. }
  1960. void IApplicationFunctions::ExecuteProgram(HLERequestContext& ctx) {
  1961. LOG_WARNING(Service_AM, "(STUBBED) called");
  1962. IPC::RequestParser rp{ctx};
  1963. [[maybe_unused]] const auto unk_1 = rp.Pop<u32>();
  1964. [[maybe_unused]] const auto unk_2 = rp.Pop<u32>();
  1965. const auto program_index = rp.Pop<u64>();
  1966. IPC::ResponseBuilder rb{ctx, 2};
  1967. rb.Push(ResultSuccess);
  1968. system.ExecuteProgram(program_index);
  1969. }
  1970. void IApplicationFunctions::ClearUserChannel(HLERequestContext& ctx) {
  1971. LOG_DEBUG(Service_AM, "called");
  1972. system.GetUserChannel().clear();
  1973. IPC::ResponseBuilder rb{ctx, 2};
  1974. rb.Push(ResultSuccess);
  1975. }
  1976. void IApplicationFunctions::UnpopToUserChannel(HLERequestContext& ctx) {
  1977. LOG_DEBUG(Service_AM, "called");
  1978. IPC::RequestParser rp{ctx};
  1979. const auto storage = rp.PopIpcInterface<IStorage>().lock();
  1980. if (storage) {
  1981. system.GetUserChannel().push_back(storage->GetData());
  1982. }
  1983. IPC::ResponseBuilder rb{ctx, 2};
  1984. rb.Push(ResultSuccess);
  1985. }
  1986. void IApplicationFunctions::GetPreviousProgramIndex(HLERequestContext& ctx) {
  1987. LOG_WARNING(Service_AM, "(STUBBED) called");
  1988. IPC::ResponseBuilder rb{ctx, 3};
  1989. rb.Push(ResultSuccess);
  1990. rb.Push<s32>(previous_program_index);
  1991. }
  1992. void IApplicationFunctions::GetGpuErrorDetectedSystemEvent(HLERequestContext& ctx) {
  1993. LOG_WARNING(Service_AM, "(STUBBED) called");
  1994. IPC::ResponseBuilder rb{ctx, 2, 1};
  1995. rb.Push(ResultSuccess);
  1996. rb.PushCopyObjects(gpu_error_detected_event->GetReadableEvent());
  1997. }
  1998. void IApplicationFunctions::GetFriendInvitationStorageChannelEvent(HLERequestContext& ctx) {
  1999. LOG_DEBUG(Service_AM, "called");
  2000. IPC::ResponseBuilder rb{ctx, 2, 1};
  2001. rb.Push(ResultSuccess);
  2002. rb.PushCopyObjects(friend_invitation_storage_channel_event->GetReadableEvent());
  2003. }
  2004. void IApplicationFunctions::TryPopFromFriendInvitationStorageChannel(HLERequestContext& ctx) {
  2005. LOG_DEBUG(Service_AM, "(STUBBED) called");
  2006. IPC::ResponseBuilder rb{ctx, 2};
  2007. rb.Push(AM::ResultNoDataInChannel);
  2008. }
  2009. void IApplicationFunctions::GetNotificationStorageChannelEvent(HLERequestContext& ctx) {
  2010. LOG_DEBUG(Service_AM, "called");
  2011. IPC::ResponseBuilder rb{ctx, 2, 1};
  2012. rb.Push(ResultSuccess);
  2013. rb.PushCopyObjects(notification_storage_channel_event->GetReadableEvent());
  2014. }
  2015. void IApplicationFunctions::GetHealthWarningDisappearedSystemEvent(HLERequestContext& ctx) {
  2016. LOG_DEBUG(Service_AM, "called");
  2017. IPC::ResponseBuilder rb{ctx, 2, 1};
  2018. rb.Push(ResultSuccess);
  2019. rb.PushCopyObjects(health_warning_disappeared_system_event->GetReadableEvent());
  2020. }
  2021. void IApplicationFunctions::PrepareForJit(HLERequestContext& ctx) {
  2022. LOG_WARNING(Service_AM, "(STUBBED) called");
  2023. IPC::ResponseBuilder rb{ctx, 2};
  2024. rb.Push(ResultSuccess);
  2025. }
  2026. void LoopProcess(Nvnflinger::Nvnflinger& nvnflinger, Core::System& system) {
  2027. auto message_queue = std::make_shared<AppletMessageQueue>(system);
  2028. auto server_manager = std::make_unique<ServerManager>(system);
  2029. server_manager->RegisterNamedService(
  2030. "appletAE", std::make_shared<AppletAE>(nvnflinger, message_queue, system));
  2031. server_manager->RegisterNamedService(
  2032. "appletOE", std::make_shared<AppletOE>(nvnflinger, message_queue, system));
  2033. server_manager->RegisterNamedService("idle:sys", std::make_shared<IdleSys>(system));
  2034. server_manager->RegisterNamedService("omm", std::make_shared<OMM>(system));
  2035. server_manager->RegisterNamedService("spsm", std::make_shared<SPSM>(system));
  2036. ServerManager::RunServer(std::move(server_manager));
  2037. }
  2038. IHomeMenuFunctions::IHomeMenuFunctions(Core::System& system_)
  2039. : ServiceFramework{system_, "IHomeMenuFunctions"}, service_context{system,
  2040. "IHomeMenuFunctions"} {
  2041. // clang-format off
  2042. static const FunctionInfo functions[] = {
  2043. {10, &IHomeMenuFunctions::RequestToGetForeground, "RequestToGetForeground"},
  2044. {11, nullptr, "LockForeground"},
  2045. {12, nullptr, "UnlockForeground"},
  2046. {20, nullptr, "PopFromGeneralChannel"},
  2047. {21, &IHomeMenuFunctions::GetPopFromGeneralChannelEvent, "GetPopFromGeneralChannelEvent"},
  2048. {30, nullptr, "GetHomeButtonWriterLockAccessor"},
  2049. {31, nullptr, "GetWriterLockAccessorEx"},
  2050. {40, nullptr, "IsSleepEnabled"},
  2051. {41, nullptr, "IsRebootEnabled"},
  2052. {50, nullptr, "LaunchSystemApplet"},
  2053. {51, nullptr, "LaunchStarter"},
  2054. {100, nullptr, "PopRequestLaunchApplicationForDebug"},
  2055. {110, nullptr, "IsForceTerminateApplicationDisabledForDebug"},
  2056. {200, nullptr, "LaunchDevMenu"},
  2057. {1000, nullptr, "SetLastApplicationExitReason"},
  2058. };
  2059. // clang-format on
  2060. RegisterHandlers(functions);
  2061. pop_from_general_channel_event =
  2062. service_context.CreateEvent("IHomeMenuFunctions:PopFromGeneralChannelEvent");
  2063. }
  2064. IHomeMenuFunctions::~IHomeMenuFunctions() {
  2065. service_context.CloseEvent(pop_from_general_channel_event);
  2066. }
  2067. void IHomeMenuFunctions::RequestToGetForeground(HLERequestContext& ctx) {
  2068. LOG_WARNING(Service_AM, "(STUBBED) called");
  2069. IPC::ResponseBuilder rb{ctx, 2};
  2070. rb.Push(ResultSuccess);
  2071. }
  2072. void IHomeMenuFunctions::GetPopFromGeneralChannelEvent(HLERequestContext& ctx) {
  2073. LOG_WARNING(Service_AM, "(STUBBED) called");
  2074. IPC::ResponseBuilder rb{ctx, 2, 1};
  2075. rb.Push(ResultSuccess);
  2076. rb.PushCopyObjects(pop_from_general_channel_event->GetReadableEvent());
  2077. }
  2078. IGlobalStateController::IGlobalStateController(Core::System& system_)
  2079. : ServiceFramework{system_, "IGlobalStateController"} {
  2080. // clang-format off
  2081. static const FunctionInfo functions[] = {
  2082. {0, nullptr, "RequestToEnterSleep"},
  2083. {1, nullptr, "EnterSleep"},
  2084. {2, nullptr, "StartSleepSequence"},
  2085. {3, nullptr, "StartShutdownSequence"},
  2086. {4, nullptr, "StartRebootSequence"},
  2087. {9, nullptr, "IsAutoPowerDownRequested"},
  2088. {10, nullptr, "LoadAndApplyIdlePolicySettings"},
  2089. {11, nullptr, "NotifyCecSettingsChanged"},
  2090. {12, nullptr, "SetDefaultHomeButtonLongPressTime"},
  2091. {13, nullptr, "UpdateDefaultDisplayResolution"},
  2092. {14, nullptr, "ShouldSleepOnBoot"},
  2093. {15, nullptr, "GetHdcpAuthenticationFailedEvent"},
  2094. {30, nullptr, "OpenCradleFirmwareUpdater"},
  2095. };
  2096. // clang-format on
  2097. RegisterHandlers(functions);
  2098. }
  2099. IGlobalStateController::~IGlobalStateController() = default;
  2100. IApplicationCreator::IApplicationCreator(Core::System& system_)
  2101. : ServiceFramework{system_, "IApplicationCreator"} {
  2102. // clang-format off
  2103. static const FunctionInfo functions[] = {
  2104. {0, nullptr, "CreateApplication"},
  2105. {1, nullptr, "PopLaunchRequestedApplication"},
  2106. {10, nullptr, "CreateSystemApplication"},
  2107. {100, nullptr, "PopFloatingApplicationForDevelopment"},
  2108. };
  2109. // clang-format on
  2110. RegisterHandlers(functions);
  2111. }
  2112. IApplicationCreator::~IApplicationCreator() = default;
  2113. IProcessWindingController::IProcessWindingController(Core::System& system_)
  2114. : ServiceFramework{system_, "IProcessWindingController"} {
  2115. // clang-format off
  2116. static const FunctionInfo functions[] = {
  2117. {0, &IProcessWindingController::GetLaunchReason, "GetLaunchReason"},
  2118. {11, &IProcessWindingController::OpenCallingLibraryApplet, "OpenCallingLibraryApplet"},
  2119. {21, nullptr, "PushContext"},
  2120. {22, nullptr, "PopContext"},
  2121. {23, nullptr, "CancelWindingReservation"},
  2122. {30, nullptr, "WindAndDoReserved"},
  2123. {40, nullptr, "ReserveToStartAndWaitAndUnwindThis"},
  2124. {41, nullptr, "ReserveToStartAndWait"},
  2125. };
  2126. // clang-format on
  2127. RegisterHandlers(functions);
  2128. }
  2129. IProcessWindingController::~IProcessWindingController() = default;
  2130. void IProcessWindingController::GetLaunchReason(HLERequestContext& ctx) {
  2131. LOG_WARNING(Service_AM, "(STUBBED) called");
  2132. struct AppletProcessLaunchReason {
  2133. u8 flag;
  2134. INSERT_PADDING_BYTES(3);
  2135. };
  2136. static_assert(sizeof(AppletProcessLaunchReason) == 0x4,
  2137. "AppletProcessLaunchReason is an invalid size");
  2138. AppletProcessLaunchReason reason{
  2139. .flag = 0,
  2140. };
  2141. IPC::ResponseBuilder rb{ctx, 3};
  2142. rb.Push(ResultSuccess);
  2143. rb.PushRaw(reason);
  2144. }
  2145. void IProcessWindingController::OpenCallingLibraryApplet(HLERequestContext& ctx) {
  2146. const auto applet_id = system.GetAppletManager().GetCurrentAppletId();
  2147. const auto applet_mode = Applets::LibraryAppletMode::AllForeground;
  2148. LOG_WARNING(Service_AM, "(STUBBED) called with applet_id={:08X}, applet_mode={:08X}", applet_id,
  2149. applet_mode);
  2150. const auto& applet_manager{system.GetAppletManager()};
  2151. const auto applet = applet_manager.GetApplet(applet_id, applet_mode);
  2152. if (applet == nullptr) {
  2153. LOG_ERROR(Service_AM, "Applet doesn't exist! applet_id={}", applet_id);
  2154. IPC::ResponseBuilder rb{ctx, 2};
  2155. rb.Push(ResultUnknown);
  2156. return;
  2157. }
  2158. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  2159. rb.Push(ResultSuccess);
  2160. rb.PushIpcInterface<ILibraryAppletAccessor>(system, applet);
  2161. }
  2162. } // namespace Service::AM