IApplicationFunctions.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. using LibHac.Account;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Ncm;
  5. using LibHac.Ns;
  6. using LibHac.Tools.FsSystem.NcaUtils;
  7. using Ryujinx.Common;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.HLE.Exceptions;
  10. using Ryujinx.HLE.HOS.Ipc;
  11. using Ryujinx.HLE.HOS.Kernel.Memory;
  12. using Ryujinx.HLE.HOS.Kernel.Threading;
  13. using Ryujinx.HLE.HOS.Services.Am.AppletAE.Storage;
  14. using Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy.Types;
  15. using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService;
  16. using Ryujinx.HLE.HOS.SystemState;
  17. using Ryujinx.Horizon.Common;
  18. using System;
  19. using System.Numerics;
  20. using System.Threading;
  21. using AccountUid = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
  22. using ApplicationId = LibHac.Ncm.ApplicationId;
  23. namespace Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy
  24. {
  25. class IApplicationFunctions : IpcService
  26. {
  27. private long _defaultSaveDataSize = 200000000;
  28. private long _defaultJournalSaveDataSize = 200000000;
  29. private KEvent _gpuErrorDetectedSystemEvent;
  30. private KEvent _friendInvitationStorageChannelEvent;
  31. private KEvent _notificationStorageChannelEvent;
  32. private KEvent _healthWarningDisappearedSystemEvent;
  33. private int _gpuErrorDetectedSystemEventHandle;
  34. private int _friendInvitationStorageChannelEventHandle;
  35. private int _notificationStorageChannelEventHandle;
  36. private int _healthWarningDisappearedSystemEventHandle;
  37. private bool _gamePlayRecordingState;
  38. private int _jitLoaded;
  39. private LibHac.HorizonClient _horizon;
  40. public IApplicationFunctions(Horizon system)
  41. {
  42. // TODO: Find where they are signaled.
  43. _gpuErrorDetectedSystemEvent = new KEvent(system.KernelContext);
  44. _friendInvitationStorageChannelEvent = new KEvent(system.KernelContext);
  45. _notificationStorageChannelEvent = new KEvent(system.KernelContext);
  46. _healthWarningDisappearedSystemEvent = new KEvent(system.KernelContext);
  47. _horizon = system.LibHacHorizonManager.AmClient;
  48. }
  49. [CommandHipc(1)]
  50. // PopLaunchParameter(LaunchParameterKind kind) -> object<nn::am::service::IStorage>
  51. public ResultCode PopLaunchParameter(ServiceCtx context)
  52. {
  53. LaunchParameterKind kind = (LaunchParameterKind)context.RequestData.ReadUInt32();
  54. byte[] storageData;
  55. switch (kind)
  56. {
  57. case LaunchParameterKind.UserChannel:
  58. storageData = context.Device.Configuration.UserChannelPersistence.Pop();
  59. break;
  60. case LaunchParameterKind.PreselectedUser:
  61. // Only the first 0x18 bytes of the Data seems to be actually used.
  62. storageData = StorageHelper.MakeLaunchParams(context.Device.System.AccountManager.LastOpenedUser);
  63. break;
  64. case LaunchParameterKind.Unknown:
  65. throw new NotImplementedException("Unknown LaunchParameterKind.");
  66. default:
  67. return ResultCode.ObjectInvalid;
  68. }
  69. if (storageData == null)
  70. {
  71. return ResultCode.NotAvailable;
  72. }
  73. MakeObject(context, new AppletAE.IStorage(storageData));
  74. return ResultCode.Success;
  75. }
  76. [CommandHipc(12)] // 4.0.0+
  77. // CreateApplicationAndRequestToStart(u64 title_id)
  78. public ResultCode CreateApplicationAndRequestToStart(ServiceCtx context)
  79. {
  80. ulong titleId = context.RequestData.ReadUInt64();
  81. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { titleId });
  82. if (titleId == 0)
  83. {
  84. context.Device.UiHandler.ExecuteProgram(context.Device, ProgramSpecifyKind.RestartProgram, titleId);
  85. }
  86. else
  87. {
  88. throw new NotImplementedException();
  89. }
  90. return ResultCode.Success;
  91. }
  92. [CommandHipc(20)]
  93. // EnsureSaveData(nn::account::Uid) -> u64
  94. public ResultCode EnsureSaveData(ServiceCtx context)
  95. {
  96. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  97. // Mask out the low nibble of the program ID to get the application ID
  98. ApplicationId applicationId = new ApplicationId(context.Device.Application.TitleId & ~0xFul);
  99. BlitStruct<ApplicationControlProperty> controlHolder = context.Device.Application.ControlData;
  100. ref ApplicationControlProperty control = ref controlHolder.Value;
  101. if (LibHac.Common.Utilities.IsZeros(controlHolder.ByteSpan))
  102. {
  103. // If the current application doesn't have a loaded control property, create a dummy one
  104. // and set the savedata sizes so a user savedata will be created.
  105. control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
  106. // The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
  107. control.UserAccountSaveDataSize = 0x4000;
  108. control.UserAccountSaveDataJournalSize = 0x4000;
  109. Logger.Warning?.Print(LogClass.ServiceAm,
  110. "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
  111. }
  112. LibHac.HorizonClient hos = context.Device.System.LibHacHorizonManager.AmClient;
  113. LibHac.Result result = hos.Fs.EnsureApplicationSaveData(out long requiredSize, applicationId, in control, in userId);
  114. context.ResponseData.Write(requiredSize);
  115. return (ResultCode)result.Value;
  116. }
  117. [CommandHipc(21)]
  118. // GetDesiredLanguage() -> nn::settings::LanguageCode
  119. public ResultCode GetDesiredLanguage(ServiceCtx context)
  120. {
  121. // This seems to be calling ns:am GetApplicationDesiredLanguage followed by ConvertApplicationLanguageToLanguageCode
  122. // Calls are from a IReadOnlyApplicationControlDataInterface object
  123. // ConvertApplicationLanguageToLanguageCode compares language code strings and returns the index
  124. // TODO: When above calls are implemented, switch to using ns:am
  125. long desiredLanguageCode = context.Device.System.State.DesiredLanguageCode;
  126. int supportedLanguages = (int)context.Device.Application.ControlData.Value.SupportedLanguageFlag;
  127. int firstSupported = BitOperations.TrailingZeroCount(supportedLanguages);
  128. if (firstSupported > (int)TitleLanguage.BrazilianPortuguese)
  129. {
  130. Logger.Warning?.Print(LogClass.ServiceAm, "Application has zero supported languages");
  131. context.ResponseData.Write(desiredLanguageCode);
  132. return ResultCode.Success;
  133. }
  134. // If desired language is not supported by application, use first supported language from TitleLanguage.
  135. // TODO: In the future, a GUI could enable user-specified search priority
  136. if (((1 << (int)context.Device.System.State.DesiredTitleLanguage) & supportedLanguages) == 0)
  137. {
  138. SystemLanguage newLanguage = Enum.Parse<SystemLanguage>(Enum.GetName(typeof(TitleLanguage), firstSupported));
  139. desiredLanguageCode = SystemStateMgr.GetLanguageCode((int)newLanguage);
  140. Logger.Info?.Print(LogClass.ServiceAm, $"Application doesn't support configured language. Using {newLanguage}");
  141. }
  142. context.ResponseData.Write(desiredLanguageCode);
  143. return ResultCode.Success;
  144. }
  145. [CommandHipc(22)]
  146. // SetTerminateResult(u32)
  147. public ResultCode SetTerminateResult(ServiceCtx context)
  148. {
  149. LibHac.Result result = new LibHac.Result(context.RequestData.ReadUInt32());
  150. Logger.Info?.Print(LogClass.ServiceAm, $"Result = 0x{result.Value:x8} ({result.ToStringWithName()}).");
  151. return ResultCode.Success;
  152. }
  153. [CommandHipc(23)]
  154. // GetDisplayVersion() -> nn::oe::DisplayVersion
  155. public ResultCode GetDisplayVersion(ServiceCtx context)
  156. {
  157. // If an NACP isn't found, the buffer will be all '\0' which seems to be the correct implementation.
  158. context.ResponseData.Write(context.Device.Application.ControlData.Value.DisplayVersion);
  159. return ResultCode.Success;
  160. }
  161. [CommandHipc(25)] // 3.0.0+
  162. // ExtendSaveData(u8 save_data_type, nn::account::Uid, s64 save_size, s64 journal_size) -> u64 result_code
  163. public ResultCode ExtendSaveData(ServiceCtx context)
  164. {
  165. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadUInt64();
  166. Uid userId = context.RequestData.ReadStruct<Uid>();
  167. long saveDataSize = context.RequestData.ReadInt64();
  168. long journalSize = context.RequestData.ReadInt64();
  169. // NOTE: Service calls nn::fs::ExtendApplicationSaveData.
  170. // Since LibHac currently doesn't support this method, we can stub it for now.
  171. _defaultSaveDataSize = saveDataSize;
  172. _defaultJournalSaveDataSize = journalSize;
  173. context.ResponseData.Write((uint)ResultCode.Success);
  174. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { saveDataType, userId, saveDataSize, journalSize });
  175. return ResultCode.Success;
  176. }
  177. [CommandHipc(26)] // 3.0.0+
  178. // GetSaveDataSize(u8 save_data_type, nn::account::Uid) -> (s64 save_size, s64 journal_size)
  179. public ResultCode GetSaveDataSize(ServiceCtx context)
  180. {
  181. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadUInt64();
  182. Uid userId = context.RequestData.ReadStruct<Uid>();
  183. // NOTE: Service calls nn::fs::FindSaveDataWithFilter with SaveDataType = 1 hardcoded.
  184. // Then it calls nn::fs::GetSaveDataAvailableSize and nn::fs::GetSaveDataJournalSize to get the sizes.
  185. // Since LibHac currently doesn't support the 2 last methods, we can hardcode the values to 200mb.
  186. context.ResponseData.Write(_defaultSaveDataSize);
  187. context.ResponseData.Write(_defaultJournalSaveDataSize);
  188. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { saveDataType, userId });
  189. return ResultCode.Success;
  190. }
  191. [CommandHipc(27)] // 5.0.0+
  192. // CreateCacheStorage(u16 index, s64 save_size, s64 journal_size) -> (u32 storageTarget, u64 requiredSize)
  193. public ResultCode CreateCacheStorage(ServiceCtx context)
  194. {
  195. ushort index = (ushort)context.RequestData.ReadUInt64();
  196. long saveSize = context.RequestData.ReadInt64();
  197. long journalSize = context.RequestData.ReadInt64();
  198. // Mask out the low nibble of the program ID to get the application ID
  199. ApplicationId applicationId = new ApplicationId(context.Device.Application.TitleId & ~0xFul);
  200. BlitStruct<ApplicationControlProperty> controlHolder = context.Device.Application.ControlData;
  201. LibHac.Result result = _horizon.Fs.CreateApplicationCacheStorage(out long requiredSize,
  202. out CacheStorageTargetMedia storageTarget, applicationId, in controlHolder.Value, index, saveSize,
  203. journalSize);
  204. if (result.IsFailure()) return (ResultCode)result.Value;
  205. context.ResponseData.Write((ulong)storageTarget);
  206. context.ResponseData.Write(requiredSize);
  207. return ResultCode.Success;
  208. }
  209. [CommandHipc(28)] // 11.0.0+
  210. // GetSaveDataSizeMax() -> (s64 save_size_max, s64 journal_size_max)
  211. public ResultCode GetSaveDataSizeMax(ServiceCtx context)
  212. {
  213. // NOTE: We are currently using a stub for GetSaveDataSize() which returns the default values.
  214. // For this method we shouldn't return anything lower than that, but since we aren't interacting
  215. // with fs to get the actual sizes, we return the default values here as well.
  216. // This also helps in case ExtendSaveData() has been executed and the default values were modified.
  217. context.ResponseData.Write(_defaultSaveDataSize);
  218. context.ResponseData.Write(_defaultJournalSaveDataSize);
  219. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  220. return ResultCode.Success;
  221. }
  222. [CommandHipc(30)]
  223. // BeginBlockingHomeButtonShortAndLongPressed()
  224. public ResultCode BeginBlockingHomeButtonShortAndLongPressed(ServiceCtx context)
  225. {
  226. // NOTE: This set two internal fields at offsets 0x89 and 0x8B to value 1 then it signals an internal event.
  227. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  228. return ResultCode.Success;
  229. }
  230. [CommandHipc(31)]
  231. // EndBlockingHomeButtonShortAndLongPressed()
  232. public ResultCode EndBlockingHomeButtonShortAndLongPressed(ServiceCtx context)
  233. {
  234. // NOTE: This set two internal fields at offsets 0x89 and 0x8B to value 0 then it signals an internal event.
  235. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  236. return ResultCode.Success;
  237. }
  238. [CommandHipc(32)] // 2.0.0+
  239. // BeginBlockingHomeButton(u64 nano_second)
  240. public ResultCode BeginBlockingHomeButton(ServiceCtx context)
  241. {
  242. ulong nanoSeconds = context.RequestData.ReadUInt64();
  243. // NOTE: This set two internal fields at offsets 0x89 to value 1 and 0x90 to value of "nanoSeconds" then it signals an internal event.
  244. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { nanoSeconds });
  245. return ResultCode.Success;
  246. }
  247. [CommandHipc(33)] // 2.0.0+
  248. // EndBlockingHomeButton()
  249. public ResultCode EndBlockingHomeButton(ServiceCtx context)
  250. {
  251. // NOTE: This set two internal fields at offsets 0x89 and 0x90 to value 0 then it signals an internal event.
  252. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  253. return ResultCode.Success;
  254. }
  255. [CommandHipc(40)]
  256. // NotifyRunning() -> b8
  257. public ResultCode NotifyRunning(ServiceCtx context)
  258. {
  259. context.ResponseData.Write(true);
  260. return ResultCode.Success;
  261. }
  262. [CommandHipc(50)] // 2.0.0+
  263. // GetPseudoDeviceId() -> nn::util::Uuid
  264. public ResultCode GetPseudoDeviceId(ServiceCtx context)
  265. {
  266. context.ResponseData.Write(0L);
  267. context.ResponseData.Write(0L);
  268. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  269. return ResultCode.Success;
  270. }
  271. [CommandHipc(60)] // 2.0.0+
  272. // SetMediaPlaybackStateForApplication(bool enabled)
  273. public ResultCode SetMediaPlaybackStateForApplication(ServiceCtx context)
  274. {
  275. bool enabled = context.RequestData.ReadBoolean();
  276. // NOTE: Service stores the "enabled" value in a private field, when enabled is false, it stores nn::os::GetSystemTick() too.
  277. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { enabled });
  278. return ResultCode.Success;
  279. }
  280. [CommandHipc(65)] // 3.0.0+
  281. // IsGamePlayRecordingSupported() -> u8
  282. public ResultCode IsGamePlayRecordingSupported(ServiceCtx context)
  283. {
  284. context.ResponseData.Write(_gamePlayRecordingState);
  285. return ResultCode.Success;
  286. }
  287. [CommandHipc(66)] // 3.0.0+
  288. // InitializeGamePlayRecording(u64, handle<copy>)
  289. public ResultCode InitializeGamePlayRecording(ServiceCtx context)
  290. {
  291. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  292. return ResultCode.Success;
  293. }
  294. [CommandHipc(67)] // 3.0.0+
  295. // SetGamePlayRecordingState(u32)
  296. public ResultCode SetGamePlayRecordingState(ServiceCtx context)
  297. {
  298. _gamePlayRecordingState = context.RequestData.ReadInt32() != 0;
  299. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { _gamePlayRecordingState });
  300. return ResultCode.Success;
  301. }
  302. [CommandHipc(90)] // 4.0.0+
  303. // EnableApplicationCrashReport(u8)
  304. public ResultCode EnableApplicationCrashReport(ServiceCtx context)
  305. {
  306. bool applicationCrashReportEnabled = context.RequestData.ReadBoolean();
  307. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { applicationCrashReportEnabled });
  308. return ResultCode.Success;
  309. }
  310. [CommandHipc(100)] // 5.0.0+
  311. // InitializeApplicationCopyrightFrameBuffer(s32 width, s32 height, handle<copy, transfer_memory> transfer_memory, u64 transfer_memory_size)
  312. public ResultCode InitializeApplicationCopyrightFrameBuffer(ServiceCtx context)
  313. {
  314. int width = context.RequestData.ReadInt32();
  315. int height = context.RequestData.ReadInt32();
  316. ulong transferMemorySize = context.RequestData.ReadUInt64();
  317. int transferMemoryHandle = context.Request.HandleDesc.ToCopy[0];
  318. ulong transferMemoryAddress = context.Process.HandleTable.GetObject<KTransferMemory>(transferMemoryHandle).Address;
  319. ResultCode resultCode = ResultCode.InvalidParameters;
  320. if (((transferMemorySize & 0x3FFFF) == 0) && width <= 1280 && height <= 720)
  321. {
  322. resultCode = InitializeApplicationCopyrightFrameBufferImpl(transferMemoryAddress, transferMemorySize, width, height);
  323. }
  324. if (transferMemoryHandle != 0)
  325. {
  326. context.Device.System.KernelContext.Syscall.CloseHandle(transferMemoryHandle);
  327. }
  328. return resultCode;
  329. }
  330. private ResultCode InitializeApplicationCopyrightFrameBufferImpl(ulong transferMemoryAddress, ulong transferMemorySize, int width, int height)
  331. {
  332. if ((transferMemorySize & 0x3FFFF) != 0)
  333. {
  334. return ResultCode.InvalidParameters;
  335. }
  336. ResultCode resultCode;
  337. // if (_copyrightBuffer == null)
  338. {
  339. // TODO: Initialize buffer and object.
  340. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { transferMemoryAddress, transferMemorySize, width, height });
  341. resultCode = ResultCode.Success;
  342. }
  343. return resultCode;
  344. }
  345. [CommandHipc(101)] // 5.0.0+
  346. // SetApplicationCopyrightImage(buffer<bytes, 0x45> frame_buffer, s32 x, s32 y, s32 width, s32 height, s32 window_origin_mode)
  347. public ResultCode SetApplicationCopyrightImage(ServiceCtx context)
  348. {
  349. ulong frameBufferPos = context.Request.SendBuff[0].Position;
  350. ulong frameBufferSize = context.Request.SendBuff[0].Size;
  351. int x = context.RequestData.ReadInt32();
  352. int y = context.RequestData.ReadInt32();
  353. int width = context.RequestData.ReadInt32();
  354. int height = context.RequestData.ReadInt32();
  355. uint windowOriginMode = context.RequestData.ReadUInt32();
  356. ResultCode resultCode = ResultCode.InvalidParameters;
  357. if (((y | x) >= 0) && width >= 1 && height >= 1)
  358. {
  359. ResultCode result = SetApplicationCopyrightImageImpl(x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode);
  360. if (result != ResultCode.Success)
  361. {
  362. resultCode = result;
  363. }
  364. else
  365. {
  366. resultCode = ResultCode.Success;
  367. }
  368. }
  369. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { frameBufferPos, frameBufferSize, x, y, width, height, windowOriginMode });
  370. return resultCode;
  371. }
  372. private ResultCode SetApplicationCopyrightImageImpl(int x, int y, int width, int height, ulong frameBufferPos, ulong frameBufferSize, uint windowOriginMode)
  373. {
  374. /*
  375. if (_copyrightBuffer == null)
  376. {
  377. return ResultCode.NullCopyrightObject;
  378. }
  379. */
  380. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode });
  381. return ResultCode.Success;
  382. }
  383. [CommandHipc(102)] // 5.0.0+
  384. // SetApplicationCopyrightVisibility(bool visible)
  385. public ResultCode SetApplicationCopyrightVisibility(ServiceCtx context)
  386. {
  387. bool visible = context.RequestData.ReadBoolean();
  388. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { visible });
  389. // NOTE: It sets an internal field and return ResultCode.Success in all case.
  390. return ResultCode.Success;
  391. }
  392. [CommandHipc(110)] // 5.0.0+
  393. // QueryApplicationPlayStatistics(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  394. public ResultCode QueryApplicationPlayStatistics(ServiceCtx context)
  395. {
  396. // TODO: Call pdm:qry cmd 13 when IPC call between services will be implemented.
  397. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context);
  398. }
  399. [CommandHipc(111)] // 6.0.0+
  400. // QueryApplicationPlayStatisticsByUid(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  401. public ResultCode QueryApplicationPlayStatisticsByUid(ServiceCtx context)
  402. {
  403. // TODO: Call pdm:qry cmd 16 when IPC call between services will be implemented.
  404. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context, true);
  405. }
  406. [CommandHipc(120)] // 5.0.0+
  407. // ExecuteProgram(ProgramSpecifyKind kind, u64 value)
  408. public ResultCode ExecuteProgram(ServiceCtx context)
  409. {
  410. ProgramSpecifyKind kind = (ProgramSpecifyKind)context.RequestData.ReadUInt32();
  411. // padding
  412. context.RequestData.ReadUInt32();
  413. ulong value = context.RequestData.ReadUInt64();
  414. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { kind, value });
  415. context.Device.UiHandler.ExecuteProgram(context.Device, kind, value);
  416. return ResultCode.Success;
  417. }
  418. [CommandHipc(121)] // 5.0.0+
  419. // ClearUserChannel()
  420. public ResultCode ClearUserChannel(ServiceCtx context)
  421. {
  422. context.Device.Configuration.UserChannelPersistence.Clear();
  423. return ResultCode.Success;
  424. }
  425. [CommandHipc(122)] // 5.0.0+
  426. // UnpopToUserChannel(object<nn::am::service::IStorage> input_storage)
  427. public ResultCode UnpopToUserChannel(ServiceCtx context)
  428. {
  429. AppletAE.IStorage data = GetObject<AppletAE.IStorage>(context, 0);
  430. context.Device.Configuration.UserChannelPersistence.Push(data.Data);
  431. return ResultCode.Success;
  432. }
  433. [CommandHipc(123)] // 5.0.0+
  434. // GetPreviousProgramIndex() -> s32 program_index
  435. public ResultCode GetPreviousProgramIndex(ServiceCtx context)
  436. {
  437. int previousProgramIndex = context.Device.Configuration.UserChannelPersistence.PreviousIndex;
  438. context.ResponseData.Write(previousProgramIndex);
  439. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { previousProgramIndex });
  440. return ResultCode.Success;
  441. }
  442. [CommandHipc(130)] // 8.0.0+
  443. // GetGpuErrorDetectedSystemEvent() -> handle<copy>
  444. public ResultCode GetGpuErrorDetectedSystemEvent(ServiceCtx context)
  445. {
  446. if (_gpuErrorDetectedSystemEventHandle == 0)
  447. {
  448. if (context.Process.HandleTable.GenerateHandle(_gpuErrorDetectedSystemEvent.ReadableEvent, out _gpuErrorDetectedSystemEventHandle) != Result.Success)
  449. {
  450. throw new InvalidOperationException("Out of handles!");
  451. }
  452. }
  453. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_gpuErrorDetectedSystemEventHandle);
  454. // NOTE: This is used by "sdk" NSO during applet-application initialization.
  455. // A separate thread is setup where event-waiting is handled.
  456. // When the Event is signaled, official sw will assert.
  457. return ResultCode.Success;
  458. }
  459. [CommandHipc(140)] // 9.0.0+
  460. // GetFriendInvitationStorageChannelEvent() -> handle<copy>
  461. public ResultCode GetFriendInvitationStorageChannelEvent(ServiceCtx context)
  462. {
  463. if (_friendInvitationStorageChannelEventHandle == 0)
  464. {
  465. if (context.Process.HandleTable.GenerateHandle(_friendInvitationStorageChannelEvent.ReadableEvent, out _friendInvitationStorageChannelEventHandle) != Result.Success)
  466. {
  467. throw new InvalidOperationException("Out of handles!");
  468. }
  469. }
  470. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_friendInvitationStorageChannelEventHandle);
  471. return ResultCode.Success;
  472. }
  473. [CommandHipc(141)] // 9.0.0+
  474. // TryPopFromFriendInvitationStorageChannel() -> object<nn::am::service::IStorage>
  475. public ResultCode TryPopFromFriendInvitationStorageChannel(ServiceCtx context)
  476. {
  477. // NOTE: IStorage are pushed in the channel with IApplicationAccessor PushToFriendInvitationStorageChannel
  478. // If _friendInvitationStorageChannelEvent is signaled, the event is cleared.
  479. // If an IStorage is available, returns it with ResultCode.Success.
  480. // If not, just returns ResultCode.NotAvailable. Since we don't support friend feature for now, it's fine to do the same.
  481. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  482. return ResultCode.NotAvailable;
  483. }
  484. [CommandHipc(150)] // 9.0.0+
  485. // GetNotificationStorageChannelEvent() -> handle<copy>
  486. public ResultCode GetNotificationStorageChannelEvent(ServiceCtx context)
  487. {
  488. if (_notificationStorageChannelEventHandle == 0)
  489. {
  490. if (context.Process.HandleTable.GenerateHandle(_notificationStorageChannelEvent.ReadableEvent, out _notificationStorageChannelEventHandle) != Result.Success)
  491. {
  492. throw new InvalidOperationException("Out of handles!");
  493. }
  494. }
  495. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_notificationStorageChannelEventHandle);
  496. return ResultCode.Success;
  497. }
  498. [CommandHipc(160)] // 9.0.0+
  499. // GetHealthWarningDisappearedSystemEvent() -> handle<copy>
  500. public ResultCode GetHealthWarningDisappearedSystemEvent(ServiceCtx context)
  501. {
  502. if (_healthWarningDisappearedSystemEventHandle == 0)
  503. {
  504. if (context.Process.HandleTable.GenerateHandle(_healthWarningDisappearedSystemEvent.ReadableEvent, out _healthWarningDisappearedSystemEventHandle) != Result.Success)
  505. {
  506. throw new InvalidOperationException("Out of handles!");
  507. }
  508. }
  509. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_healthWarningDisappearedSystemEventHandle);
  510. return ResultCode.Success;
  511. }
  512. [CommandHipc(1001)] // 10.0.0+
  513. // PrepareForJit()
  514. public ResultCode PrepareForJit(ServiceCtx context)
  515. {
  516. if (Interlocked.Exchange(ref _jitLoaded, 1) == 0)
  517. {
  518. string jitPath = context.Device.System.ContentManager.GetInstalledContentPath(0x010000000000003B, StorageId.BuiltInSystem, NcaContentType.Program);
  519. string filePath = context.Device.FileSystem.SwitchPathToSystemPath(jitPath);
  520. if (string.IsNullOrWhiteSpace(filePath))
  521. {
  522. throw new InvalidSystemResourceException($"JIT (010000000000003B) system title not found! The JIT will not work, provide the system archive to fix this error. (See https://github.com/Ryujinx/Ryujinx#requirements for more information)");
  523. }
  524. context.Device.Application.LoadServiceNca(filePath);
  525. // FIXME: Most likely not how this should be done?
  526. while (!context.Device.System.SmRegistry.IsServiceRegistered("jit:u"))
  527. {
  528. context.Device.System.SmRegistry.WaitForServiceRegistration();
  529. }
  530. }
  531. return ResultCode.Success;
  532. }
  533. }
  534. }