ryujinx/src/Ryujinx.Common/SystemInterop/ForceDpiAware.cs
TSRBerry fc20d9b925
[Ryujinx.Common] Address dotnet-format issues (#5358)
* dotnet format style --severity info

Some changes were manually reverted.

* dotnet format analyzers --serverity info

Some changes have been minimally adapted.

* Restore a few unused methods and variables

* Silence dotnet format IDE0060 warnings

* Silence dotnet format IDE0059 warnings

* Address or silence dotnet format IDE1006 warnings

* Address dotnet format CA1816 warnings

* Address or silence dotnet format CA2211 warnings

* Silence CA1806 and CA1834 issues

* Fix formatting for switch expressions

* Address most dotnet format whitespace warnings

* Apply dotnet format whitespace formatting

A few of them have been manually reverted and the corresponding warning was silenced

* Revert formatting changes for while and for-loops

* Format if-blocks correctly

* Run dotnet format whitespace after rebase

* Run dotnet format style after rebase

* Run dotnet format analyzers after rebase

* Run dotnet format after rebase and remove unused usings

- analyzers
- style
- whitespace

* Add comments to disabled warnings

* Remove a few unused parameters

* Replace MmeShadowScratch with Array256<uint>

* Simplify properties and array initialization, Use const when possible, Remove trailing commas

* Run dotnet format after rebase

* Address IDE0251 warnings

* Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas"

This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e.

* dotnet format whitespace after rebase

* First dotnet format pass

* Second dotnet format pass

* Fix build issues

* Fix StructArrayHelpers.cs

* Apply suggestions from code review

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Fix return statements

* Fix naming rule violations

* Update src/Ryujinx.Common/Utilities/StreamUtils.cs

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Add trailing commas

* Address review feedback

* Address review feedback

* Rename remaining type parameters to TKey and TValue

* Fix manual formatting for logging levels

* Fix spacing before comments

---------

Co-authored-by: Ac_K <Acoustik666@gmail.com>
2023-06-28 18:41:38 +02:00

96 lines
3.6 KiB
C#

using Ryujinx.Common.Logging;
using System;
using System.Globalization;
using System.Runtime.InteropServices;
namespace Ryujinx.Common.SystemInterop
{
public static partial class ForceDpiAware
{
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetProcessDPIAware();
private const string X11LibraryName = "libX11.so.6";
[LibraryImport(X11LibraryName)]
private static partial IntPtr XOpenDisplay([MarshalAs(UnmanagedType.LPStr)] string display);
[LibraryImport(X11LibraryName)]
private static partial IntPtr XGetDefault(IntPtr display, [MarshalAs(UnmanagedType.LPStr)] string program, [MarshalAs(UnmanagedType.LPStr)] string option);
[LibraryImport(X11LibraryName)]
private static partial int XDisplayWidth(IntPtr display, int screenNumber);
[LibraryImport(X11LibraryName)]
private static partial int XDisplayWidthMM(IntPtr display, int screenNumber);
[LibraryImport(X11LibraryName)]
private static partial int XCloseDisplay(IntPtr display);
private static readonly double _standardDpiScale = 96.0;
private static readonly double _maxScaleFactor = 1.25;
/// <summary>
/// Marks the application as DPI-Aware when running on the Windows operating system.
/// </summary>
public static void Windows()
{
// Make process DPI aware for proper window sizing on high-res screens.
if (OperatingSystem.IsWindowsVersionAtLeast(6))
{
SetProcessDPIAware();
}
}
public static double GetActualScaleFactor()
{
double userDpiScale = 96.0;
try
{
if (OperatingSystem.IsWindows())
{
userDpiScale = GdiPlusHelper.GetDpiX(IntPtr.Zero);
}
else if (OperatingSystem.IsLinux())
{
string xdgSessionType = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE")?.ToLower();
if (xdgSessionType == null || xdgSessionType == "x11")
{
IntPtr display = XOpenDisplay(null);
string dpiString = Marshal.PtrToStringAnsi(XGetDefault(display, "Xft", "dpi"));
if (dpiString == null || !double.TryParse(dpiString, NumberStyles.Any, CultureInfo.InvariantCulture, out userDpiScale))
{
userDpiScale = XDisplayWidth(display, 0) * 25.4 / XDisplayWidthMM(display, 0);
}
_ = XCloseDisplay(display);
}
else if (xdgSessionType == "wayland")
{
// TODO
Logger.Warning?.Print(LogClass.Application, "Couldn't determine monitor DPI: Wayland not yet supported");
}
else
{
Logger.Warning?.Print(LogClass.Application, $"Couldn't determine monitor DPI: Unrecognised XDG_SESSION_TYPE: {xdgSessionType}");
}
}
}
catch (Exception e)
{
Logger.Warning?.Print(LogClass.Application, $"Couldn't determine monitor DPI: {e.Message}");
}
return userDpiScale;
}
public static double GetWindowScaleFactor()
{
double userDpiScale = GetActualScaleFactor();
return Math.Min(userDpiScale / _standardDpiScale, _maxScaleFactor);
}
}
}