WindowsMultimediaTimerResolution.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Common.System
  6. {
  7. /// <summary>
  8. /// Handle Windows Multimedia timer resolution.
  9. /// </summary>
  10. public class WindowsMultimediaTimerResolution : IDisposable
  11. {
  12. [StructLayout(LayoutKind.Sequential)]
  13. public struct TimeCaps
  14. {
  15. public uint wPeriodMin;
  16. public uint wPeriodMax;
  17. };
  18. [DllImport("winmm.dll", EntryPoint = "timeGetDevCaps", SetLastError = true)]
  19. private static extern uint TimeGetDevCaps(ref TimeCaps timeCaps, uint sizeTimeCaps);
  20. [DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")]
  21. private static extern uint TimeBeginPeriod(uint uMilliseconds);
  22. [DllImport("winmm.dll", EntryPoint = "timeEndPeriod")]
  23. private static extern uint TimeEndPeriod(uint uMilliseconds);
  24. private uint _targetResolutionInMilliseconds;
  25. private bool _isActive;
  26. /// <summary>
  27. /// Create a new <see cref="WindowsMultimediaTimerResolution"/> and activate the given resolution.
  28. /// </summary>
  29. /// <param name="targetResolutionInMilliseconds"></param>
  30. public WindowsMultimediaTimerResolution(uint targetResolutionInMilliseconds)
  31. {
  32. _targetResolutionInMilliseconds = targetResolutionInMilliseconds;
  33. EnsureResolutionSupport();
  34. Activate();
  35. }
  36. private void EnsureResolutionSupport()
  37. {
  38. TimeCaps timeCaps = default;
  39. uint result = TimeGetDevCaps(ref timeCaps, (uint)Unsafe.SizeOf<TimeCaps>());
  40. if (result != 0)
  41. {
  42. Logger.Notice.Print(LogClass.Application, $"timeGetDevCaps failed with result: {result}");
  43. }
  44. else
  45. {
  46. uint supportedTargetResolutionInMilliseconds = Math.Min(Math.Max(timeCaps.wPeriodMin, _targetResolutionInMilliseconds), timeCaps.wPeriodMax);
  47. if (supportedTargetResolutionInMilliseconds != _targetResolutionInMilliseconds)
  48. {
  49. Logger.Notice.Print(LogClass.Application, $"Target resolution isn't supported by OS, using closest resolution: {supportedTargetResolutionInMilliseconds}ms");
  50. _targetResolutionInMilliseconds = supportedTargetResolutionInMilliseconds;
  51. }
  52. }
  53. }
  54. private void Activate()
  55. {
  56. uint result = TimeBeginPeriod(_targetResolutionInMilliseconds);
  57. if (result != 0)
  58. {
  59. Logger.Notice.Print(LogClass.Application, $"timeBeginPeriod failed with result: {result}");
  60. }
  61. else
  62. {
  63. _isActive = true;
  64. }
  65. }
  66. private void Disable()
  67. {
  68. if (_isActive)
  69. {
  70. uint result = TimeEndPeriod(_targetResolutionInMilliseconds);
  71. if (result != 0)
  72. {
  73. Logger.Notice.Print(LogClass.Application, $"timeEndPeriod failed with result: {result}");
  74. }
  75. else
  76. {
  77. _isActive = false;
  78. }
  79. }
  80. }
  81. public void Dispose()
  82. {
  83. Dispose(true);
  84. GC.SuppressFinalize(this);
  85. }
  86. protected virtual void Dispose(bool disposing)
  87. {
  88. if (disposing)
  89. {
  90. Disable();
  91. }
  92. }
  93. }
  94. }