ForceDpiAware.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Globalization;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Common.SystemInterop
  6. {
  7. public static partial class ForceDpiAware
  8. {
  9. [LibraryImport("user32.dll")]
  10. [return: MarshalAs(UnmanagedType.Bool)]
  11. private static partial bool SetProcessDPIAware();
  12. private const string X11LibraryName = "libX11.so.6";
  13. [LibraryImport(X11LibraryName)]
  14. private static partial IntPtr XOpenDisplay([MarshalAs(UnmanagedType.LPStr)] string display);
  15. [LibraryImport(X11LibraryName)]
  16. private static partial IntPtr XGetDefault(IntPtr display, [MarshalAs(UnmanagedType.LPStr)] string program, [MarshalAs(UnmanagedType.LPStr)] string option);
  17. [LibraryImport(X11LibraryName)]
  18. private static partial int XDisplayWidth(IntPtr display, int screenNumber);
  19. [LibraryImport(X11LibraryName)]
  20. private static partial int XDisplayWidthMM(IntPtr display, int screenNumber);
  21. [LibraryImport(X11LibraryName)]
  22. private static partial int XCloseDisplay(IntPtr display);
  23. private static readonly double _standardDpiScale = 96.0;
  24. private static readonly double _maxScaleFactor = 1.25;
  25. /// <summary>
  26. /// Marks the application as DPI-Aware when running on the Windows operating system.
  27. /// </summary>
  28. public static void Windows()
  29. {
  30. // Make process DPI aware for proper window sizing on high-res screens.
  31. if (OperatingSystem.IsWindowsVersionAtLeast(6))
  32. {
  33. SetProcessDPIAware();
  34. }
  35. }
  36. public static double GetActualScaleFactor()
  37. {
  38. double userDpiScale = 96.0;
  39. try
  40. {
  41. if (OperatingSystem.IsWindows())
  42. {
  43. userDpiScale = GdiPlusHelper.GetDpiX(IntPtr.Zero);
  44. }
  45. else if (OperatingSystem.IsLinux())
  46. {
  47. string xdgSessionType = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE")?.ToLower();
  48. if (xdgSessionType == null || xdgSessionType == "x11")
  49. {
  50. IntPtr display = XOpenDisplay(null);
  51. string dpiString = Marshal.PtrToStringAnsi(XGetDefault(display, "Xft", "dpi"));
  52. if (dpiString == null || !double.TryParse(dpiString, NumberStyles.Any, CultureInfo.InvariantCulture, out userDpiScale))
  53. {
  54. userDpiScale = (double)XDisplayWidth(display, 0) * 25.4 / (double)XDisplayWidthMM(display, 0);
  55. }
  56. XCloseDisplay(display);
  57. }
  58. else if (xdgSessionType == "wayland")
  59. {
  60. // TODO
  61. Logger.Warning?.Print(LogClass.Application, $"Couldn't determine monitor DPI: Wayland not yet supported");
  62. }
  63. else
  64. {
  65. Logger.Warning?.Print(LogClass.Application, $"Couldn't determine monitor DPI: Unrecognised XDG_SESSION_TYPE: {xdgSessionType}");
  66. }
  67. }
  68. }
  69. catch (Exception e)
  70. {
  71. Logger.Warning?.Print(LogClass.Application, $"Couldn't determine monitor DPI: {e.Message}");
  72. }
  73. return userDpiScale;
  74. }
  75. public static double GetWindowScaleFactor()
  76. {
  77. double userDpiScale = GetActualScaleFactor();
  78. return Math.Min(userDpiScale / _standardDpiScale, _maxScaleFactor);
  79. }
  80. }
  81. }