ICommonStateGetter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using System;
  6. using System.Collections.Generic;
  7. using static Ryujinx.HLE.HOS.ErrorCode;
  8. namespace Ryujinx.HLE.HOS.Services.Am
  9. {
  10. class ICommonStateGetter : IpcService
  11. {
  12. private Dictionary<int, ServiceProcessRequest> _commands;
  13. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  14. private KEvent _displayResolutionChangeEvent;
  15. public ICommonStateGetter(Horizon system)
  16. {
  17. _commands = new Dictionary<int, ServiceProcessRequest>
  18. {
  19. { 0, GetEventHandle },
  20. { 1, ReceiveMessage },
  21. { 5, GetOperationMode },
  22. { 6, GetPerformanceMode },
  23. { 8, GetBootMode },
  24. { 9, GetCurrentFocusState },
  25. { 60, GetDefaultDisplayResolution },
  26. { 61, GetDefaultDisplayResolutionChangeEvent }
  27. };
  28. _displayResolutionChangeEvent = new KEvent(system);
  29. }
  30. public long GetEventHandle(ServiceCtx context)
  31. {
  32. KEvent Event = context.Device.System.AppletState.MessageEvent;
  33. if (context.Process.HandleTable.GenerateHandle(Event.ReadableEvent, out int handle) != KernelResult.Success)
  34. {
  35. throw new InvalidOperationException("Out of handles!");
  36. }
  37. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  38. return 0;
  39. }
  40. public long ReceiveMessage(ServiceCtx context)
  41. {
  42. if (!context.Device.System.AppletState.TryDequeueMessage(out MessageInfo message))
  43. {
  44. return MakeError(ErrorModule.Am, AmErr.NoMessages);
  45. }
  46. context.ResponseData.Write((int)message);
  47. return 0;
  48. }
  49. public long GetOperationMode(ServiceCtx context)
  50. {
  51. OperationMode mode = context.Device.System.State.DockedMode
  52. ? OperationMode.Docked
  53. : OperationMode.Handheld;
  54. context.ResponseData.Write((byte)mode);
  55. return 0;
  56. }
  57. public long GetPerformanceMode(ServiceCtx context)
  58. {
  59. Apm.PerformanceMode mode = context.Device.System.State.DockedMode
  60. ? Apm.PerformanceMode.Docked
  61. : Apm.PerformanceMode.Handheld;
  62. context.ResponseData.Write((int)mode);
  63. return 0;
  64. }
  65. public long GetBootMode(ServiceCtx context)
  66. {
  67. context.ResponseData.Write((byte)0); //Unknown value.
  68. Logger.PrintStub(LogClass.ServiceAm);
  69. return 0;
  70. }
  71. public long GetCurrentFocusState(ServiceCtx context)
  72. {
  73. context.ResponseData.Write((byte)context.Device.System.AppletState.FocusState);
  74. return 0;
  75. }
  76. public long GetDefaultDisplayResolution(ServiceCtx context)
  77. {
  78. context.ResponseData.Write(1280);
  79. context.ResponseData.Write(720);
  80. return 0;
  81. }
  82. public long GetDefaultDisplayResolutionChangeEvent(ServiceCtx context)
  83. {
  84. if (context.Process.HandleTable.GenerateHandle(_displayResolutionChangeEvent.ReadableEvent, out int handle) != KernelResult.Success)
  85. {
  86. throw new InvalidOperationException("Out of handles!");
  87. }
  88. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  89. Logger.PrintStub(LogClass.ServiceAm);
  90. return 0;
  91. }
  92. }
  93. }