From dd72e2b5731c5fad5821d023c24bf6a7b9dd3314 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Thu, 17 Aug 2023 20:28:48 +0200 Subject: [PATCH] Screen detection fix --- app/Display/ScreenNative.cs | 68 ++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/app/Display/ScreenNative.cs b/app/Display/ScreenNative.cs index 39b256a5..cf7bd359 100644 --- a/app/Display/ScreenNative.cs +++ b/app/Display/ScreenNative.cs @@ -1,10 +1,27 @@ using System.Collections; using System.Runtime.InteropServices; +using System.Windows.Forms; using static GHelper.Display.ScreenInterrogatory; namespace GHelper.Display { + class DeviceComparer : IComparer + { + public int Compare(object x, object y) + { + uint displayX = ((DISPLAYCONFIG_TARGET_DEVICE_NAME)x).connectorInstance; + uint displayY = ((DISPLAYCONFIG_TARGET_DEVICE_NAME)y).connectorInstance; + + if (displayX > displayY) + return 1; + if (displayX < displayY) + return -1; + else + return 0; + } + } + class ScreenComparer : IComparer { public int Compare(object x, object y) @@ -129,38 +146,19 @@ namespace GHelper.Display public const int ENUM_CURRENT_SETTINGS = -1; public const string defaultDevice = @"\\.\DISPLAY1"; - static bool? _ultimate = null; - - static bool isUltimate - { - get - { - if (_ultimate is null) _ultimate = (Program.acpi.DeviceGet(AsusACPI.GPUMux) == 0); - return (bool)_ultimate; - } - } - public static string? FindLaptopScreen(bool log = false) { string? laptopScreen = null; var screens = Screen.AllScreens; - /* - if (!isUltimate) - { - foreach (var screen in screens ) - { - if (log) Logger.WriteLine(screen.DeviceName); - if (screen.DeviceName == defaultDevice) return defaultDevice; - } - } - */ - try { var devices = GetAllDevices().ToArray(); - int count = 0, displayNum = -1; + Array.Sort(devices, new DeviceComparer()); + Array.Sort(screens, new ScreenComparer()); + + int count = 0; string internalName = AppConfig.GetString("internal_display"); @@ -170,23 +168,23 @@ namespace GHelper.Display device.outputTechnology == DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EMBEDDED || device.monitorFriendlyDeviceName == internalName) { - displayNum = count; AppConfig.Set("internal_display", device.monitorFriendlyDeviceName); + + if (count < screens.Length) + { + laptopScreen = screens[count].DeviceName; + } + else + { + laptopScreen = defaultDevice; + } } - if (log) Logger.WriteLine(device.monitorFriendlyDeviceName + ":" + device.outputTechnology.ToString() + ": " + ((count < screens.Length) ? screens[count].DeviceName : "")); + + + if (log) Logger.WriteLine(device.monitorFriendlyDeviceName + ":" + device.outputTechnology.ToString() + device.connectorInstance.ToString() + ": " + ((count < screens.Length) ? screens[count].DeviceName : "")); count++; } - Array.Sort(screens, new ScreenComparer()); - - count = 0; - foreach (var screen in screens) - { - if (count == displayNum) laptopScreen = screen.DeviceName; - count++; - } - - if (displayNum > 0 && count == 0) laptopScreen = defaultDevice; } catch (Exception ex) {