IApplicationFunctions.cs 28 KB

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