ForceDpiAware.cs 3.5 KB

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