Skip to content

Commit

Permalink
DisplayManager create a hash incase DeviceId not available using bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
rocksdanister committed Apr 28, 2024
1 parent c626c81 commit b704d4c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
Expand Up @@ -115,7 +115,7 @@
</Grid>
<!-- Labels -->
<TextBlock
Margin="2.5,-2.5,0,0"
Margin="5,0,0,0"
FontSize="18"
Text="{Binding Screen.Index, Mode=OneWay}" />
</Grid>
Expand Down
56 changes: 31 additions & 25 deletions src/Lively/Lively/Core/Display/DisplayManager.cs
Expand Up @@ -13,23 +13,29 @@
using System.Linq;
using System.Runtime.InteropServices;

// Ref:
// https://github.com/micdenny/WpfScreenHelper
// http://referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/winforms/Managed/System/WinForms/Screen.cs
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd145072.aspx
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd183314.aspx
namespace Lively.Core.Display
{
public class DisplayManager : ObservableObject, IDisplayManager //,IWndProcHookHandler
{
private const int PRIMARY_MONITOR = unchecked((int)0xBAADF00D);
public event EventHandler DisplayUpdated;

public ObservableCollection<DisplayMonitor> DisplayMonitors { get; } = new ObservableCollection<DisplayMonitor>();

// This identifier is just for us, so that we don't try to call the multimon
// functions if we just need the primary monitor... this is safer for
// non-multimon OSes.
private const int PRIMARY_MONITOR = unchecked((int)0xBAADF00D);
private const int MONITORINFOF_PRIMARY = 0x00000001;
private const int MONITOR_DEFAULTTONEAREST = 0x00000002;

private static bool multiMonitorSupport;
private const string defaultDisplayDeviceName = "DISPLAY";

//public static DisplayManager Instance { get; private set; }

public event EventHandler DisplayUpdated;

public ObservableCollection<DisplayMonitor> DisplayMonitors { get; } = new ObservableCollection<DisplayMonitor>();
// Indicates if we have more than one monitor.
private bool multiMonitorSupport;

private Rectangle virtualScreenBounds = Rectangle.Empty;

Expand All @@ -47,13 +53,6 @@ public DisplayManager()
RefreshDisplayMonitorList();
}

/*
public static void Initialize()
{
Instance = new DisplayManager();
}
*/

public uint OnHwndCreated(IntPtr hWnd, out bool register)
{
register = false;
Expand Down Expand Up @@ -142,7 +141,7 @@ private DisplayMonitor GetDisplayMonitorFromHMonitor(IntPtr hMonitor)
}

displayMonitor.Bounds = GetVirtualScreenBounds();
displayMonitor.DeviceId = GetDefaultDisplayDeviceId();
displayMonitor.DeviceId = string.Empty;
displayMonitor.DisplayName = "Display";
displayMonitor.HMonitor = hMonitor;
displayMonitor.IsPrimary = true;
Expand All @@ -152,7 +151,7 @@ private DisplayMonitor GetDisplayMonitorFromHMonitor(IntPtr hMonitor)
}
else
{
var info = new NativeMethods.MONITORINFOEX();// MONITORINFOEX();
var info = new NativeMethods.MONITORINFOEX();
NativeMethods.GetMonitorInfo(new HandleRef(null, hMonitor), info);

string deviceName = new string(info.szDevice).TrimEnd((char)0);
Expand All @@ -166,6 +165,9 @@ private DisplayMonitor GetDisplayMonitorFromHMonitor(IntPtr hMonitor)
UpdateDisplayMonitor(displayMonitor, info);
}

if (string.IsNullOrWhiteSpace(displayMonitor.DeviceId))
displayMonitor.DeviceId = GetDefaultDisplayDeviceId(displayMonitor.Bounds);

return displayMonitor;
}

Expand Down Expand Up @@ -246,17 +248,21 @@ private static NativeMethods.DISPLAY_DEVICE GetDisplayDevice(string deviceName)
}
catch { }

if (string.IsNullOrEmpty(result.DeviceID)
|| string.IsNullOrWhiteSpace(result.DeviceID))
{
result.DeviceID = GetDefaultDisplayDeviceId();
}

return result;
}

private static string GetDefaultDisplayDeviceId() => NativeMethods.GetSystemMetrics((int)NativeMethods.SystemMetric.SM_REMOTESESSION) != 0 ?
"\\\\?\\DISPLAY#REMOTEDISPLAY#" : "\\\\?\\DISPLAY#LOCALDISPLAY#";
private static string GetDefaultDisplayDeviceId(Rectangle displayBounds)
{
var isRemote = NativeMethods.GetSystemMetrics((int)NativeMethods.SystemMetric.SM_REMOTESESSION) != 0;
var boundsString = $"{displayBounds.X}-{displayBounds.Y}-{displayBounds.Width}-{displayBounds.Height}";

// Calculate SHA-256 hash
var hashBytes = System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(boundsString));
var hashString = BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLowerInvariant();
var prefix = isRemote ? "\\\\?\\DISPLAY#REMOTEDISPLAY#" : "\\\\?\\DISPLAY#LOCALDISPLAY#";

return $"{prefix}{hashString}";
}

private static Rectangle GetVirtualScreenBounds()
{
Expand Down

0 comments on commit b704d4c

Please sign in to comment.