perf: reduce portable build size ~40MB — replace winsdk/wmi, migrate cv2 to Pillow
Lint & Test / test (push) Failing after 19s
Lint & Test / test (push) Failing after 19s
- Replace winsdk (~35MB) with winrt packages (~2.5MB) for OS notification listener. API is identical, 93% size reduction. - Replace wmi (~3-5MB) with ctypes for monitor names (EnumDisplayDevicesW) and camera names (SetupAPI). Zero external dependency. - Migrate cv2.resize/imencode/LUT to Pillow/numpy in 5 files (filters, preview helpers, kc_target_processor). OpenCV only needed for camera and video stream now. - Fix DefWindowProcW ctypes overflow on 64-bit Python (pre-existing bug in platform_detector display power listener). - Fix openLightbox import in streams-capture-templates.ts (was using broken window cast instead of direct import). - Add mandatory data migration policy to CLAUDE.md after silent data loss incident from storage file rename without migration.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Utility functions for retrieving friendly monitor/display names."""
|
||||
|
||||
import ctypes
|
||||
import sys
|
||||
from typing import Dict
|
||||
|
||||
@@ -11,7 +12,8 @@ logger = get_logger(__name__)
|
||||
def get_monitor_names() -> Dict[int, str]:
|
||||
"""Get friendly names for connected monitors.
|
||||
|
||||
On Windows, attempts to retrieve monitor names from WMI.
|
||||
On Windows, enumerates display adapters and their monitors via
|
||||
``EnumDisplayDevicesW`` (pure ctypes, no third-party dependencies).
|
||||
On other platforms, returns empty dict (will fall back to generic names).
|
||||
|
||||
Returns:
|
||||
@@ -22,47 +24,68 @@ def get_monitor_names() -> Dict[int, str]:
|
||||
return {}
|
||||
|
||||
try:
|
||||
import wmi
|
||||
from ctypes import wintypes
|
||||
|
||||
w = wmi.WMI(namespace="wmi")
|
||||
monitors = w.WmiMonitorID()
|
||||
class DISPLAY_DEVICEW(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("cb", wintypes.DWORD),
|
||||
("DeviceName", ctypes.c_wchar * 32),
|
||||
("DeviceString", ctypes.c_wchar * 128),
|
||||
("StateFlags", wintypes.DWORD),
|
||||
("DeviceID", ctypes.c_wchar * 128),
|
||||
("DeviceKey", ctypes.c_wchar * 128),
|
||||
]
|
||||
|
||||
monitor_names = {}
|
||||
user32 = ctypes.windll.user32
|
||||
|
||||
for idx, monitor in enumerate(monitors):
|
||||
try:
|
||||
# Extract manufacturer name
|
||||
manufacturer = ""
|
||||
if monitor.ManufacturerName:
|
||||
manufacturer = "".join(chr(c) for c in monitor.ManufacturerName if c != 0)
|
||||
DISPLAY_DEVICE_ACTIVE = 0x00000001
|
||||
DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 0x00000002
|
||||
|
||||
# Extract user-friendly name
|
||||
user_name = ""
|
||||
if monitor.UserFriendlyName:
|
||||
user_name = "".join(chr(c) for c in monitor.UserFriendlyName if c != 0)
|
||||
monitor_names: Dict[int, str] = {}
|
||||
monitor_idx = 0
|
||||
|
||||
# Build friendly name
|
||||
if user_name:
|
||||
friendly_name = user_name.strip()
|
||||
elif manufacturer:
|
||||
friendly_name = f"{manufacturer.strip()} Monitor"
|
||||
else:
|
||||
friendly_name = f"Display {idx}"
|
||||
# Enumerate display adapters (GPUs / virtual outputs)
|
||||
adapter_idx = 0
|
||||
while adapter_idx < 16: # safety limit
|
||||
adapter = DISPLAY_DEVICEW()
|
||||
adapter.cb = ctypes.sizeof(DISPLAY_DEVICEW)
|
||||
|
||||
monitor_names[idx] = friendly_name
|
||||
logger.debug(f"Monitor {idx}: {friendly_name}")
|
||||
if not user32.EnumDisplayDevicesW(None, adapter_idx, ctypes.byref(adapter), 0):
|
||||
break
|
||||
adapter_idx += 1
|
||||
|
||||
except Exception as e:
|
||||
logger.debug(f"Failed to parse monitor {idx} name: {e}")
|
||||
monitor_names[idx] = f"Display {idx}"
|
||||
# Skip adapters not attached to the desktop
|
||||
if not (adapter.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP):
|
||||
continue
|
||||
|
||||
# Enumerate monitors attached to this adapter
|
||||
child_idx = 0
|
||||
while child_idx < 16: # safety limit
|
||||
monitor = DISPLAY_DEVICEW()
|
||||
monitor.cb = ctypes.sizeof(DISPLAY_DEVICEW)
|
||||
|
||||
if not user32.EnumDisplayDevicesW(
|
||||
adapter.DeviceName, child_idx, ctypes.byref(monitor), 0
|
||||
):
|
||||
break
|
||||
child_idx += 1
|
||||
|
||||
if not (monitor.StateFlags & DISPLAY_DEVICE_ACTIVE):
|
||||
continue
|
||||
|
||||
# DeviceString contains the friendly name (e.g. "DELL U2718Q")
|
||||
friendly_name = monitor.DeviceString.strip()
|
||||
if not friendly_name:
|
||||
friendly_name = f"Display {monitor_idx}"
|
||||
|
||||
monitor_names[monitor_idx] = friendly_name
|
||||
logger.debug(f"Monitor {monitor_idx}: {friendly_name}")
|
||||
monitor_idx += 1
|
||||
|
||||
return monitor_names
|
||||
|
||||
except ImportError:
|
||||
logger.debug("WMI library not available - install with: pip install wmi")
|
||||
return {}
|
||||
except Exception as e:
|
||||
logger.debug(f"Failed to retrieve monitor names via WMI: {e}")
|
||||
logger.debug(f"Failed to retrieve monitor names via EnumDisplayDevices: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user