IApplicationFunctions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using LibHac;
  2. using LibHac.Account;
  3. using LibHac.Common;
  4. using LibHac.Fs;
  5. using LibHac.Ncm;
  6. using LibHac.Ns;
  7. using Ryujinx.Common;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.HLE.HOS.Ipc;
  10. using Ryujinx.HLE.HOS.Kernel.Common;
  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.Sdb.Pdm.QueryService;
  15. using System;
  16. using static LibHac.Fs.ApplicationSaveDataManagement;
  17. using AccountUid = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
  18. namespace Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy
  19. {
  20. class IApplicationFunctions : IpcService
  21. {
  22. private KEvent _gpuErrorDetectedSystemEvent;
  23. private KEvent _friendInvitationStorageChannelEvent;
  24. private KEvent _notificationStorageChannelEvent;
  25. public IApplicationFunctions(Horizon system)
  26. {
  27. _gpuErrorDetectedSystemEvent = new KEvent(system.KernelContext);
  28. _friendInvitationStorageChannelEvent = new KEvent(system.KernelContext);
  29. _notificationStorageChannelEvent = new KEvent(system.KernelContext);
  30. }
  31. [Command(1)]
  32. // PopLaunchParameter(u32) -> object<nn::am::service::IStorage>
  33. public ResultCode PopLaunchParameter(ServiceCtx context)
  34. {
  35. // Only the first 0x18 bytes of the Data seems to be actually used.
  36. MakeObject(context, new AppletAE.IStorage(StorageHelper.MakeLaunchParams(context.Device.System.State.Account.LastOpenedUser)));
  37. return ResultCode.Success;
  38. }
  39. [Command(20)]
  40. // EnsureSaveData(nn::account::Uid) -> u64
  41. public ResultCode EnsureSaveData(ServiceCtx context)
  42. {
  43. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  44. TitleId titleId = new TitleId(context.Process.TitleId);
  45. BlitStruct<ApplicationControlProperty> controlHolder = context.Device.System.ControlData;
  46. ref ApplicationControlProperty control = ref controlHolder.Value;
  47. if (Util.IsEmpty(controlHolder.ByteSpan))
  48. {
  49. // If the current application doesn't have a loaded control property, create a dummy one
  50. // and set the savedata sizes so a user savedata will be created.
  51. control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
  52. // The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
  53. control.UserAccountSaveDataSize = 0x4000;
  54. control.UserAccountSaveDataJournalSize = 0x4000;
  55. Logger.PrintWarning(LogClass.ServiceAm,
  56. "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
  57. }
  58. Result result = EnsureApplicationSaveData(context.Device.FileSystem.FsClient, out long requiredSize, titleId,
  59. ref control, ref userId);
  60. context.ResponseData.Write(requiredSize);
  61. return (ResultCode)result.Value;
  62. }
  63. [Command(21)]
  64. // GetDesiredLanguage() -> nn::settings::LanguageCode
  65. public ResultCode GetDesiredLanguage(ServiceCtx context)
  66. {
  67. context.ResponseData.Write(context.Device.System.State.DesiredLanguageCode);
  68. return ResultCode.Success;
  69. }
  70. [Command(22)]
  71. // SetTerminateResult(u32)
  72. public ResultCode SetTerminateResult(ServiceCtx context)
  73. {
  74. Result result = new Result(context.RequestData.ReadUInt32());
  75. Logger.PrintInfo(LogClass.ServiceAm, $"Result = 0x{result.Value:x8} ({result.ToStringWithName()}).");
  76. return ResultCode.Success;
  77. }
  78. [Command(23)]
  79. // GetDisplayVersion() -> nn::oe::DisplayVersion
  80. public ResultCode GetDisplayVersion(ServiceCtx context)
  81. {
  82. // FIXME: Need to check correct version on a switch.
  83. context.ResponseData.Write(1L);
  84. context.ResponseData.Write(0L);
  85. return ResultCode.Success;
  86. }
  87. // GetSaveDataSize(u8, nn::account::Uid) -> (u64, u64)
  88. [Command(26)] // 3.0.0+
  89. public ResultCode GetSaveDataSize(ServiceCtx context)
  90. {
  91. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
  92. context.RequestData.BaseStream.Seek(7, System.IO.SeekOrigin.Current);
  93. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  94. // TODO: We return a size of 2GB as we use a directory based save system. This should be enough for most of the games.
  95. context.ResponseData.Write(2000000000u);
  96. Logger.PrintStub(LogClass.ServiceAm, new { saveDataType, userId });
  97. return ResultCode.Success;
  98. }
  99. [Command(40)]
  100. // NotifyRunning() -> b8
  101. public ResultCode NotifyRunning(ServiceCtx context)
  102. {
  103. context.ResponseData.Write(1);
  104. return ResultCode.Success;
  105. }
  106. [Command(50)] // 2.0.0+
  107. // GetPseudoDeviceId() -> nn::util::Uuid
  108. public ResultCode GetPseudoDeviceId(ServiceCtx context)
  109. {
  110. context.ResponseData.Write(0L);
  111. context.ResponseData.Write(0L);
  112. Logger.PrintStub(LogClass.ServiceAm);
  113. return ResultCode.Success;
  114. }
  115. [Command(66)] // 3.0.0+
  116. // InitializeGamePlayRecording(u64, handle<copy>)
  117. public ResultCode InitializeGamePlayRecording(ServiceCtx context)
  118. {
  119. Logger.PrintStub(LogClass.ServiceAm);
  120. return ResultCode.Success;
  121. }
  122. [Command(67)] // 3.0.0+
  123. // SetGamePlayRecordingState(u32)
  124. public ResultCode SetGamePlayRecordingState(ServiceCtx context)
  125. {
  126. int state = context.RequestData.ReadInt32();
  127. Logger.PrintStub(LogClass.ServiceAm, new { state });
  128. return ResultCode.Success;
  129. }
  130. [Command(100)] // 5.0.0+
  131. // InitializeApplicationCopyrightFrameBuffer(s32 width, s32 height, handle<copy, transfer_memory> transfer_memory, u64 transfer_memory_size)
  132. public ResultCode InitializeApplicationCopyrightFrameBuffer(ServiceCtx context)
  133. {
  134. int width = context.RequestData.ReadInt32();
  135. int height = context.RequestData.ReadInt32();
  136. ulong transferMemorySize = context.RequestData.ReadUInt64();
  137. int transferMemoryHandle = context.Request.HandleDesc.ToCopy[0];
  138. ulong transferMemoryAddress = context.Process.HandleTable.GetObject<KTransferMemory>(transferMemoryHandle).Address;
  139. ResultCode resultCode = ResultCode.InvalidParameters;
  140. if (((transferMemorySize & 0x3FFFF) == 0) && width <= 1280 && height <= 720)
  141. {
  142. resultCode = InitializeApplicationCopyrightFrameBufferImpl(transferMemoryAddress, transferMemorySize, width, height);
  143. }
  144. /*
  145. if (transferMemoryHandle)
  146. {
  147. svcCloseHandle(transferMemoryHandle);
  148. }
  149. */
  150. return resultCode;
  151. }
  152. private ResultCode InitializeApplicationCopyrightFrameBufferImpl(ulong transferMemoryAddress, ulong transferMemorySize, int width, int height)
  153. {
  154. ResultCode resultCode = ResultCode.ObjectInvalid;
  155. if ((transferMemorySize & 0x3FFFF) != 0)
  156. {
  157. return ResultCode.InvalidParameters;
  158. }
  159. // if (_copyrightBuffer == null)
  160. {
  161. // TODO: Initialize buffer and object.
  162. Logger.PrintStub(LogClass.ServiceAm, new { transferMemoryAddress, transferMemorySize, width, height });
  163. resultCode = ResultCode.Success;
  164. }
  165. return resultCode;
  166. }
  167. [Command(101)] // 5.0.0+
  168. // SetApplicationCopyrightImage(buffer<bytes, 0x45> frame_buffer, s32 x, s32 y, s32 width, s32 height, s32 window_origin_mode)
  169. public ResultCode SetApplicationCopyrightImage(ServiceCtx context)
  170. {
  171. long frameBufferPos = context.Request.SendBuff[0].Position;
  172. long frameBufferSize = context.Request.SendBuff[0].Size;
  173. int x = context.RequestData.ReadInt32();
  174. int y = context.RequestData.ReadInt32();
  175. int width = context.RequestData.ReadInt32();
  176. int height = context.RequestData.ReadInt32();
  177. uint windowOriginMode = context.RequestData.ReadUInt32();
  178. ResultCode resultCode = ResultCode.InvalidParameters;
  179. if (((y | x) >= 0) && width >= 1 && height >= 1)
  180. {
  181. ResultCode result = SetApplicationCopyrightImageImpl(x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode);
  182. if (resultCode != ResultCode.Success)
  183. {
  184. resultCode = result;
  185. }
  186. else
  187. {
  188. resultCode = ResultCode.Success;
  189. }
  190. }
  191. Logger.PrintStub(LogClass.ServiceAm, new { frameBufferPos, frameBufferSize, x, y, width, height, windowOriginMode });
  192. return resultCode;
  193. }
  194. private ResultCode SetApplicationCopyrightImageImpl(int x, int y, int width, int height, long frameBufferPos, long frameBufferSize, uint windowOriginMode)
  195. {
  196. /*
  197. if (_copyrightBuffer == null)
  198. {
  199. return ResultCode.NullCopyrightObject;
  200. }
  201. */
  202. Logger.PrintStub(LogClass.ServiceAm, new { x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode });
  203. return ResultCode.Success;
  204. }
  205. [Command(102)] // 5.0.0+
  206. // SetApplicationCopyrightVisibility(bool visible)
  207. public ResultCode SetApplicationCopyrightVisibility(ServiceCtx context)
  208. {
  209. bool visible = context.RequestData.ReadBoolean();
  210. Logger.PrintStub(LogClass.ServiceAm, new { visible });
  211. // NOTE: It sets an internal field and return ResultCode.Success in all case.
  212. return ResultCode.Success;
  213. }
  214. [Command(110)] // 5.0.0+
  215. // QueryApplicationPlayStatistics(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  216. public ResultCode QueryApplicationPlayStatistics(ServiceCtx context)
  217. {
  218. // TODO: Call pdm:qry cmd 13 when IPC call between services will be implemented.
  219. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context);
  220. }
  221. [Command(111)] // 6.0.0+
  222. // QueryApplicationPlayStatisticsByUid(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  223. public ResultCode QueryApplicationPlayStatisticsByUid(ServiceCtx context)
  224. {
  225. // TODO: Call pdm:qry cmd 16 when IPC call between services will be implemented.
  226. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context, true);
  227. }
  228. [Command(130)] // 8.0.0+
  229. // GetGpuErrorDetectedSystemEvent() -> handle<copy>
  230. public ResultCode GetGpuErrorDetectedSystemEvent(ServiceCtx context)
  231. {
  232. if (context.Process.HandleTable.GenerateHandle(_gpuErrorDetectedSystemEvent.ReadableEvent, out int gpuErrorDetectedSystemEventHandle) != KernelResult.Success)
  233. {
  234. throw new InvalidOperationException("Out of handles!");
  235. }
  236. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(gpuErrorDetectedSystemEventHandle);
  237. // NOTE: This is used by "sdk" NSO during applet-application initialization.
  238. // A seperate thread is setup where event-waiting is handled.
  239. // When the Event is signaled, official sw will assert.
  240. return ResultCode.Success;
  241. }
  242. [Command(140)] // 9.0.0+
  243. // GetFriendInvitationStorageChannelEvent() -> handle<copy>
  244. public ResultCode GetFriendInvitationStorageChannelEvent(ServiceCtx context)
  245. {
  246. if (context.Process.HandleTable.GenerateHandle(_friendInvitationStorageChannelEvent.ReadableEvent, out int friendInvitationStorageChannelEventHandle) != KernelResult.Success)
  247. {
  248. throw new InvalidOperationException("Out of handles!");
  249. }
  250. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(friendInvitationStorageChannelEventHandle);
  251. return ResultCode.Success;
  252. }
  253. [Command(150)] // 9.0.0+
  254. // GetNotificationStorageChannelEvent() -> handle<copy>
  255. public ResultCode GetNotificationStorageChannelEvent(ServiceCtx context)
  256. {
  257. if (context.Process.HandleTable.GenerateHandle(_notificationStorageChannelEvent.ReadableEvent, out int notificationStorageChannelEventHandle) != KernelResult.Success)
  258. {
  259. throw new InvalidOperationException("Out of handles!");
  260. }
  261. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(notificationStorageChannelEventHandle);
  262. return ResultCode.Success;
  263. }
  264. }
  265. }