IApplicationFunctions.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 ulong _defaultSaveDataSize = 200000000;
  29. private ulong _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)SystemState.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(SystemState.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, u64 save_size, u64 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. ulong saveDataSize = context.RequestData.ReadUInt64();
  169. ulong journalSize = context.RequestData.ReadUInt64();
  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) -> (u64 save_size, u64 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(30)]
  211. // BeginBlockingHomeButtonShortAndLongPressed()
  212. public ResultCode BeginBlockingHomeButtonShortAndLongPressed(ServiceCtx context)
  213. {
  214. // NOTE: This set two internal fields at offsets 0x89 and 0x8B to value 1 then it signals an internal event.
  215. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  216. return ResultCode.Success;
  217. }
  218. [CommandHipc(31)]
  219. // EndBlockingHomeButtonShortAndLongPressed()
  220. public ResultCode EndBlockingHomeButtonShortAndLongPressed(ServiceCtx context)
  221. {
  222. // NOTE: This set two internal fields at offsets 0x89 and 0x8B to value 0 then it signals an internal event.
  223. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  224. return ResultCode.Success;
  225. }
  226. [CommandHipc(32)] // 2.0.0+
  227. // BeginBlockingHomeButton(u64 nano_second)
  228. public ResultCode BeginBlockingHomeButton(ServiceCtx context)
  229. {
  230. ulong nanoSeconds = context.RequestData.ReadUInt64();
  231. // NOTE: This set two internal fields at offsets 0x89 to value 1 and 0x90 to value of "nanoSeconds" then it signals an internal event.
  232. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { nanoSeconds });
  233. return ResultCode.Success;
  234. }
  235. [CommandHipc(33)] // 2.0.0+
  236. // EndBlockingHomeButton()
  237. public ResultCode EndBlockingHomeButton(ServiceCtx context)
  238. {
  239. // NOTE: This set two internal fields at offsets 0x89 and 0x90 to value 0 then it signals an internal event.
  240. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  241. return ResultCode.Success;
  242. }
  243. [CommandHipc(40)]
  244. // NotifyRunning() -> b8
  245. public ResultCode NotifyRunning(ServiceCtx context)
  246. {
  247. context.ResponseData.Write(true);
  248. return ResultCode.Success;
  249. }
  250. [CommandHipc(50)] // 2.0.0+
  251. // GetPseudoDeviceId() -> nn::util::Uuid
  252. public ResultCode GetPseudoDeviceId(ServiceCtx context)
  253. {
  254. context.ResponseData.Write(0L);
  255. context.ResponseData.Write(0L);
  256. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  257. return ResultCode.Success;
  258. }
  259. [CommandHipc(60)] // 2.0.0+
  260. // SetMediaPlaybackStateForApplication(bool enabled)
  261. public ResultCode SetMediaPlaybackStateForApplication(ServiceCtx context)
  262. {
  263. bool enabled = context.RequestData.ReadBoolean();
  264. // NOTE: Service stores the "enabled" value in a private field, when enabled is false, it stores nn::os::GetSystemTick() too.
  265. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { enabled });
  266. return ResultCode.Success;
  267. }
  268. [CommandHipc(65)] // 3.0.0+
  269. // IsGamePlayRecordingSupported() -> u8
  270. public ResultCode IsGamePlayRecordingSupported(ServiceCtx context)
  271. {
  272. context.ResponseData.Write(_gamePlayRecordingState);
  273. return ResultCode.Success;
  274. }
  275. [CommandHipc(66)] // 3.0.0+
  276. // InitializeGamePlayRecording(u64, handle<copy>)
  277. public ResultCode InitializeGamePlayRecording(ServiceCtx context)
  278. {
  279. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  280. return ResultCode.Success;
  281. }
  282. [CommandHipc(67)] // 3.0.0+
  283. // SetGamePlayRecordingState(u32)
  284. public ResultCode SetGamePlayRecordingState(ServiceCtx context)
  285. {
  286. _gamePlayRecordingState = context.RequestData.ReadInt32() != 0;
  287. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { _gamePlayRecordingState });
  288. return ResultCode.Success;
  289. }
  290. [CommandHipc(90)] // 4.0.0+
  291. // EnableApplicationCrashReport(u8)
  292. public ResultCode EnableApplicationCrashReport(ServiceCtx context)
  293. {
  294. bool applicationCrashReportEnabled = context.RequestData.ReadBoolean();
  295. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { applicationCrashReportEnabled });
  296. return ResultCode.Success;
  297. }
  298. [CommandHipc(100)] // 5.0.0+
  299. // InitializeApplicationCopyrightFrameBuffer(s32 width, s32 height, handle<copy, transfer_memory> transfer_memory, u64 transfer_memory_size)
  300. public ResultCode InitializeApplicationCopyrightFrameBuffer(ServiceCtx context)
  301. {
  302. int width = context.RequestData.ReadInt32();
  303. int height = context.RequestData.ReadInt32();
  304. ulong transferMemorySize = context.RequestData.ReadUInt64();
  305. int transferMemoryHandle = context.Request.HandleDesc.ToCopy[0];
  306. ulong transferMemoryAddress = context.Process.HandleTable.GetObject<KTransferMemory>(transferMemoryHandle).Address;
  307. ResultCode resultCode = ResultCode.InvalidParameters;
  308. if (((transferMemorySize & 0x3FFFF) == 0) && width <= 1280 && height <= 720)
  309. {
  310. resultCode = InitializeApplicationCopyrightFrameBufferImpl(transferMemoryAddress, transferMemorySize, width, height);
  311. }
  312. if (transferMemoryHandle != 0)
  313. {
  314. context.Device.System.KernelContext.Syscall.CloseHandle(transferMemoryHandle);
  315. }
  316. return resultCode;
  317. }
  318. private ResultCode InitializeApplicationCopyrightFrameBufferImpl(ulong transferMemoryAddress, ulong transferMemorySize, int width, int height)
  319. {
  320. if ((transferMemorySize & 0x3FFFF) != 0)
  321. {
  322. return ResultCode.InvalidParameters;
  323. }
  324. ResultCode resultCode;
  325. // if (_copyrightBuffer == null)
  326. {
  327. // TODO: Initialize buffer and object.
  328. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { transferMemoryAddress, transferMemorySize, width, height });
  329. resultCode = ResultCode.Success;
  330. }
  331. return resultCode;
  332. }
  333. [CommandHipc(101)] // 5.0.0+
  334. // SetApplicationCopyrightImage(buffer<bytes, 0x45> frame_buffer, s32 x, s32 y, s32 width, s32 height, s32 window_origin_mode)
  335. public ResultCode SetApplicationCopyrightImage(ServiceCtx context)
  336. {
  337. ulong frameBufferPos = context.Request.SendBuff[0].Position;
  338. ulong frameBufferSize = context.Request.SendBuff[0].Size;
  339. int x = context.RequestData.ReadInt32();
  340. int y = context.RequestData.ReadInt32();
  341. int width = context.RequestData.ReadInt32();
  342. int height = context.RequestData.ReadInt32();
  343. uint windowOriginMode = context.RequestData.ReadUInt32();
  344. ResultCode resultCode = ResultCode.InvalidParameters;
  345. if (((y | x) >= 0) && width >= 1 && height >= 1)
  346. {
  347. ResultCode result = SetApplicationCopyrightImageImpl(x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode);
  348. if (result != ResultCode.Success)
  349. {
  350. resultCode = result;
  351. }
  352. else
  353. {
  354. resultCode = ResultCode.Success;
  355. }
  356. }
  357. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { frameBufferPos, frameBufferSize, x, y, width, height, windowOriginMode });
  358. return resultCode;
  359. }
  360. private ResultCode SetApplicationCopyrightImageImpl(int x, int y, int width, int height, ulong frameBufferPos, ulong frameBufferSize, uint windowOriginMode)
  361. {
  362. /*
  363. if (_copyrightBuffer == null)
  364. {
  365. return ResultCode.NullCopyrightObject;
  366. }
  367. */
  368. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode });
  369. return ResultCode.Success;
  370. }
  371. [CommandHipc(102)] // 5.0.0+
  372. // SetApplicationCopyrightVisibility(bool visible)
  373. public ResultCode SetApplicationCopyrightVisibility(ServiceCtx context)
  374. {
  375. bool visible = context.RequestData.ReadBoolean();
  376. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { visible });
  377. // NOTE: It sets an internal field and return ResultCode.Success in all case.
  378. return ResultCode.Success;
  379. }
  380. [CommandHipc(110)] // 5.0.0+
  381. // QueryApplicationPlayStatistics(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  382. public ResultCode QueryApplicationPlayStatistics(ServiceCtx context)
  383. {
  384. // TODO: Call pdm:qry cmd 13 when IPC call between services will be implemented.
  385. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context);
  386. }
  387. [CommandHipc(111)] // 6.0.0+
  388. // QueryApplicationPlayStatisticsByUid(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  389. public ResultCode QueryApplicationPlayStatisticsByUid(ServiceCtx context)
  390. {
  391. // TODO: Call pdm:qry cmd 16 when IPC call between services will be implemented.
  392. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context, true);
  393. }
  394. [CommandHipc(120)] // 5.0.0+
  395. // ExecuteProgram(ProgramSpecifyKind kind, u64 value)
  396. public ResultCode ExecuteProgram(ServiceCtx context)
  397. {
  398. ProgramSpecifyKind kind = (ProgramSpecifyKind)context.RequestData.ReadUInt32();
  399. // padding
  400. context.RequestData.ReadUInt32();
  401. ulong value = context.RequestData.ReadUInt64();
  402. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { kind, value });
  403. context.Device.UiHandler.ExecuteProgram(context.Device, kind, value);
  404. return ResultCode.Success;
  405. }
  406. [CommandHipc(121)] // 5.0.0+
  407. // ClearUserChannel()
  408. public ResultCode ClearUserChannel(ServiceCtx context)
  409. {
  410. context.Device.Configuration.UserChannelPersistence.Clear();
  411. return ResultCode.Success;
  412. }
  413. [CommandHipc(122)] // 5.0.0+
  414. // UnpopToUserChannel(object<nn::am::service::IStorage> input_storage)
  415. public ResultCode UnpopToUserChannel(ServiceCtx context)
  416. {
  417. AppletAE.IStorage data = GetObject<AppletAE.IStorage>(context, 0);
  418. context.Device.Configuration.UserChannelPersistence.Push(data.Data);
  419. return ResultCode.Success;
  420. }
  421. [CommandHipc(123)] // 5.0.0+
  422. // GetPreviousProgramIndex() -> s32 program_index
  423. public ResultCode GetPreviousProgramIndex(ServiceCtx context)
  424. {
  425. int previousProgramIndex = context.Device.Configuration.UserChannelPersistence.PreviousIndex;
  426. context.ResponseData.Write(previousProgramIndex);
  427. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { previousProgramIndex });
  428. return ResultCode.Success;
  429. }
  430. [CommandHipc(130)] // 8.0.0+
  431. // GetGpuErrorDetectedSystemEvent() -> handle<copy>
  432. public ResultCode GetGpuErrorDetectedSystemEvent(ServiceCtx context)
  433. {
  434. if (_gpuErrorDetectedSystemEventHandle == 0)
  435. {
  436. if (context.Process.HandleTable.GenerateHandle(_gpuErrorDetectedSystemEvent.ReadableEvent, out _gpuErrorDetectedSystemEventHandle) != KernelResult.Success)
  437. {
  438. throw new InvalidOperationException("Out of handles!");
  439. }
  440. }
  441. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_gpuErrorDetectedSystemEventHandle);
  442. // NOTE: This is used by "sdk" NSO during applet-application initialization.
  443. // A separate thread is setup where event-waiting is handled.
  444. // When the Event is signaled, official sw will assert.
  445. return ResultCode.Success;
  446. }
  447. [CommandHipc(140)] // 9.0.0+
  448. // GetFriendInvitationStorageChannelEvent() -> handle<copy>
  449. public ResultCode GetFriendInvitationStorageChannelEvent(ServiceCtx context)
  450. {
  451. if (_friendInvitationStorageChannelEventHandle == 0)
  452. {
  453. if (context.Process.HandleTable.GenerateHandle(_friendInvitationStorageChannelEvent.ReadableEvent, out _friendInvitationStorageChannelEventHandle) != KernelResult.Success)
  454. {
  455. throw new InvalidOperationException("Out of handles!");
  456. }
  457. }
  458. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_friendInvitationStorageChannelEventHandle);
  459. return ResultCode.Success;
  460. }
  461. [CommandHipc(141)] // 9.0.0+
  462. // TryPopFromFriendInvitationStorageChannel() -> object<nn::am::service::IStorage>
  463. public ResultCode TryPopFromFriendInvitationStorageChannel(ServiceCtx context)
  464. {
  465. // NOTE: IStorage are pushed in the channel with IApplicationAccessor PushToFriendInvitationStorageChannel
  466. // If _friendInvitationStorageChannelEvent is signaled, the event is cleared.
  467. // If an IStorage is available, returns it with ResultCode.Success.
  468. // If not, just returns ResultCode.NotAvailable. Since we don't support friend feature for now, it's fine to do the same.
  469. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  470. return ResultCode.NotAvailable;
  471. }
  472. [CommandHipc(150)] // 9.0.0+
  473. // GetNotificationStorageChannelEvent() -> handle<copy>
  474. public ResultCode GetNotificationStorageChannelEvent(ServiceCtx context)
  475. {
  476. if (_notificationStorageChannelEventHandle == 0)
  477. {
  478. if (context.Process.HandleTable.GenerateHandle(_notificationStorageChannelEvent.ReadableEvent, out _notificationStorageChannelEventHandle) != KernelResult.Success)
  479. {
  480. throw new InvalidOperationException("Out of handles!");
  481. }
  482. }
  483. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_notificationStorageChannelEventHandle);
  484. return ResultCode.Success;
  485. }
  486. [CommandHipc(160)] // 9.0.0+
  487. // GetHealthWarningDisappearedSystemEvent() -> handle<copy>
  488. public ResultCode GetHealthWarningDisappearedSystemEvent(ServiceCtx context)
  489. {
  490. if (_healthWarningDisappearedSystemEventHandle == 0)
  491. {
  492. if (context.Process.HandleTable.GenerateHandle(_healthWarningDisappearedSystemEvent.ReadableEvent, out _healthWarningDisappearedSystemEventHandle) != KernelResult.Success)
  493. {
  494. throw new InvalidOperationException("Out of handles!");
  495. }
  496. }
  497. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_healthWarningDisappearedSystemEventHandle);
  498. return ResultCode.Success;
  499. }
  500. [CommandHipc(1001)] // 10.0.0+
  501. // PrepareForJit()
  502. public ResultCode PrepareForJit(ServiceCtx context)
  503. {
  504. if (Interlocked.Exchange(ref _jitLoaded, 1) == 0)
  505. {
  506. string jitPath = context.Device.System.ContentManager.GetInstalledContentPath(0x010000000000003B, StorageId.BuiltInSystem, NcaContentType.Program);
  507. string filePath = context.Device.FileSystem.SwitchPathToSystemPath(jitPath);
  508. if (string.IsNullOrWhiteSpace(filePath))
  509. {
  510. 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)");
  511. }
  512. context.Device.Application.LoadServiceNca(filePath);
  513. // FIXME: Most likely not how this should be done?
  514. while (!context.Device.System.SmRegistry.IsServiceRegistered("jit:u"))
  515. {
  516. context.Device.System.SmRegistry.WaitForServiceRegistration();
  517. }
  518. }
  519. return ResultCode.Success;
  520. }
  521. }
  522. }