Fix display grabbing

This commit is contained in:
Pat Hartl 2024-08-08 20:00:21 -05:00
parent 968f7da4cf
commit c1e9db798e
3 changed files with 129 additions and 25 deletions

View file

@ -250,11 +250,12 @@ namespace LANCommander.Launcher.Services
var process = new Process();
var settings = SettingService.GetSettings();
/*var monitor = Window.Monitors.First(m => m.MonitorArea.X == 0 && m.MonitorArea.Y == 0);
var screen = DisplayHelper.GetScreen();
Client.Actions.AddVariable("DisplayWidth", monitor.MonitorArea.Width.ToString());
Client.Actions.AddVariable("DisplayHeight", monitor.MonitorArea.Height.ToString());*/
// Client.Actions.AddVariable("DisplayRefreshRate", ((int)DeviceDisplay.Current.MainDisplayInfo.RefreshRate).ToString());
Client.Actions.AddVariable("DisplayWidth", screen.Width.ToString());
Client.Actions.AddVariable("DisplayHeight", screen.Height.ToString());
Client.Actions.AddVariable("DisplayRefreshRate", screen.RefreshRate.ToString());
Client.Actions.AddVariable("DisplayBitDepth", screen.BitsPerPixel.ToString());
process.StartInfo.Arguments = Client.Actions.ExpandVariables(action.Arguments, game.InstallDirectory, skipSlashes: true);
process.StartInfo.FileName = Client.Actions.ExpandVariables(action.Path, game.InstallDirectory);

View file

@ -0,0 +1,122 @@
using LANCommander.SDK.PowerShell.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace LANCommander.SDK.Helpers
{
public class DisplayHelper
{
public static DEVMODE GetDeviceMode()
{
DEVMODE devMode = new DEVMODE();
devMode.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode);
return devMode;
}
[DllImport("user32.dll")]
private static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
private const int ENUM_CURRENT_SETTINGS = -1;
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 32;
private const int CCHFORMNAME = 32;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmNup;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
public static Screen GetScreen()
{
var screen = new Screen();
var bounds = new Bounds();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var devMode = GetDeviceMode();
bounds.Width = devMode.dmPelsWidth;
bounds.Height = devMode.dmPelsHeight;
screen.RefreshRate = devMode.dmDisplayFrequency;
screen.BitsPerPixel = devMode.dmBitsPerPel;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "xrandr";
process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
var match = Regex.Match(output, @"^\s+(\d+)x(\d+)\s+(\d+\.?\d+)\*\+$");
var w = match.Groups[1].Value;
var h = match.Groups[2].Value;
var r = match.Groups[3].Value;
bounds.Width = int.Parse(w);
bounds.Height = int.Parse(h);
screen.RefreshRate = (int)float.Parse(r);
}
screen.Primary = true;
screen.Bounds = bounds;
screen.Width = bounds.Width;
screen.Height = bounds.Height;
return screen;
}
}
}

View file

@ -1,4 +1,5 @@
using LANCommander.SDK.Extensions;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.PowerShell.Models;
using System.Linq;
using System.Management.Automation;
@ -11,29 +12,9 @@ namespace LANCommander.SDK.PowerShell.Cmdlets
{
protected override void ProcessRecord()
{
var bounds = GetBounds();
var screen = new Screen
{
Bounds = bounds,
Width = bounds.Width,
Height = bounds.Height,
Primary = true,
RefreshRate = 60,
BitsPerPixel = 32
};
var screen = DisplayHelper.GetScreen();
WriteObject(screen);
}
private Bounds GetBounds()
{
Bounds bounds = new Bounds();
bounds.Width = 1920;
bounds.Height = 1080;
return bounds;
}
}
}