feat(e2e): change the mode of a display on the Windows guest

display.ps1 lists the display devices and changes the mode of one (the
logic is in C# because PowerShell cannot pass the structs by reference),
and WindowsPlatform.resize_display uses it. Only the QXL display lists
modes; the first one is the basic adapter, which has one.
This commit is contained in:
LGUG2Z committed 2026-10-30 18:22:44 -07:00
1 parent 8d5106f732
commit be9e6eb36f
2 files changed
+116

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
# usage: powershell -File display.ps1 list
# powershell -File display.ps1 set DEVICE WIDTH HEIGHT
# `list` prints each display device, its current mode and the modes it lists;
# `set` changes the mode of DEVICE (for example \\.\DISPLAY2) for this session.
param([string]$Command = "list", [string]$Device = "", [int]$Width = 0, [int]$Height = 0)
Add-Type @"
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
public static class Display {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DEVMODE {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 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 = 32)] public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel; public int dmPelsWidth; public int dmPelsHeight;
public int dmDisplayFlags; 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;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DISPLAY_DEVICE {
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceString;
public int StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceKey;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern bool EnumDisplayDevices(string device, int n, ref DISPLAY_DEVICE dd, int flags);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern bool EnumDisplaySettings(string device, int mode, ref DEVMODE dm);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern int ChangeDisplaySettingsEx(string device, ref DEVMODE dm, IntPtr hwnd, int flags, IntPtr lparam);
static DEVMODE NewMode() {
var dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
return dm;
}
public static string List() {
var sb = new StringBuilder();
for (int n = 0; ; n++) {
var dd = new DISPLAY_DEVICE();
dd.cb = Marshal.SizeOf(typeof(DISPLAY_DEVICE));
if (!EnumDisplayDevices(null, n, ref dd, 0)) break;
sb.AppendLine("device " + dd.DeviceName + " [" + dd.DeviceString + "] flags 0x" + dd.StateFlags.ToString("x"));
var cur = NewMode();
bool ok = EnumDisplaySettings(dd.DeviceName, -1, ref cur);
sb.AppendLine(" current ok=" + ok + " " + cur.dmPelsWidth + "x" + cur.dmPelsHeight + " at " + cur.dmPositionX + "," + cur.dmPositionY);
var seen = new SortedSet<string>();
for (int i = 0; ; i++) {
var m = NewMode();
if (!EnumDisplaySettings(dd.DeviceName, i, ref m)) break;
seen.Add(m.dmPelsWidth + "x" + m.dmPelsHeight);
}
sb.AppendLine(" modes: " + string.Join(" ", seen));
}
return sb.ToString();
}
// 0 is DISP_CHANGE_SUCCESSFUL
public static int Set(string device, int width, int height) {
var dm = NewMode();
if (!EnumDisplaySettings(device, -1, ref dm)) return -100;
dm.dmPelsWidth = width;
dm.dmPelsHeight = height;
dm.dmFields = 0x80000 | 0x100000;
return ChangeDisplaySettingsEx(device, ref dm, IntPtr.Zero, 0, IntPtr.Zero);
}
}
"@
if ($Command -eq "list") {
[Display]::List()
} elseif ($Command -eq "set") {
"set $Device ${Width}x${Height}: " + [Display]::Set($Device, $Width, $Height)
}
@@ -39,6 +39,7 @@ LAUNCHES = 3
POLICY_SYSTEM = "HKLM\\SOFTWARE\\Policies\\Microsoft\\Windows\\System"
# the account the image logs in to
PASSWORD = "komorebi"
DISPLAY_SCRIPT = "C:\\e2e\\display.ps1"
CONFIG_PATH = "C:\\e2e\\komorebi.json"
@@ -98,6 +99,7 @@ class WindowsPlatform:
self._agent = agent
self._out = out
self._scale = scale
self._display_script = False
self._daemon_pid: int | None = None
self.start_notes: list[str] = []
self._pointer = WindowsPointer(qmp, agent)
@@ -220,6 +222,29 @@ class WindowsPlatform:
# the daemon watches the file it was started with and reads it again
self._agent.put_file(local, CONFIG_PATH)
def resize_display(self, index: int, width: int, height: int) -> None:
"""Changes the mode of display `index` and returns when the operating
system has it. Only a display of a QXL device lists modes: the first
one is the basic display adapter, which has the one."""
if not self._display_script:
self._agent.exec("cmd", "/c", "mkdir C:\\e2e 2>nul")
self._agent.put_file(
Path(__file__).parent / "display.ps1", DISPLAY_SCRIPT
)
self._display_script = True
result = self._agent.exec(
"powershell", "-NoProfile", "-ExecutionPolicy", "Bypass",
"-File", DISPLAY_SCRIPT, "set", f"\\\\.\\DISPLAY{index + 1}",
str(width), str(height),
)
if not result["stdout"].strip().endswith(": 0"):
raise RuntimeError(f"could not change the display: {result}")
if not self.wait_for(
lambda: (d := self.displays())[index].w == width
and d[index].h == height
):
raise RuntimeError("the display did not take the mode")
def exe_id(self, program: str) -> str:
# kfw names the exe of a window with its extension
return f"{program}.exe"