IApplicationFunctions.cs 28 KB

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