| 1 |
import ctypes |
| 2 |
import logging as _logging |
| 3 |
from ctypes import wintypes |
| 4 |
|
| 5 |
_log = _logging.getLogger('pywebview') |
| 6 |
|
| 7 |
|
| 8 |
_user32 = ctypes.windll.user32 |
| 9 |
|
| 10 |
|
| 11 |
# DEVMODE structure for EnumDisplaySettings |
| 12 |
class DEVMODE(ctypes.Structure): |
| 13 |
_fields_ = [ |
| 14 |
('dmDeviceName', wintypes.WCHAR * 32), |
| 15 |
('dmSpecVersion', wintypes.WORD), |
| 16 |
('dmDriverVersion', wintypes.WORD), |
| 17 |
('dmSize', wintypes.WORD), |
| 18 |
('dmDriverExtra', wintypes.WORD), |
| 19 |
('dmFields', wintypes.DWORD), |
| 20 |
('dmPositionX', wintypes.LONG), |
| 21 |
('dmPositionY', wintypes.LONG), |
| 22 |
('dmDisplayOrientation', wintypes.DWORD), |
| 23 |
('dmDisplayFixedOutput', wintypes.DWORD), |
| 24 |
('dmColor', wintypes.SHORT), |
| 25 |
('dmDuplex', wintypes.SHORT), |
| 26 |
('dmYResolution', wintypes.SHORT), |
| 27 |
('dmTTOption', wintypes.SHORT), |
| 28 |
('dmCollate', wintypes.SHORT), |
| 29 |
('dmFormName', wintypes.WCHAR * 32), |
| 30 |
('dmLogPixels', wintypes.WORD), |
| 31 |
('dmBitsPerPel', wintypes.DWORD), |
| 32 |
('dmPelsWidth', wintypes.DWORD), |
| 33 |
('dmPelsHeight', wintypes.DWORD), |
| 34 |
('dmDisplayFlags', wintypes.DWORD), |
| 35 |
('dmDisplayFrequency', wintypes.DWORD), |
| 36 |
('dmICMMethod', wintypes.DWORD), |
| 37 |
('dmICMIntent', wintypes.DWORD), |
| 38 |
('dmMediaType', wintypes.DWORD), |
| 39 |
('dmDitherType', wintypes.DWORD), |
| 40 |
('dmReserved1', wintypes.DWORD), |
| 41 |
('dmReserved2', wintypes.DWORD), |
| 42 |
('dmPanningWidth', wintypes.DWORD), |
| 43 |
('dmPanningHeight', wintypes.DWORD), |
| 44 |
] |
| 45 |
|
| 46 |
|
| 47 |
# DISPLAY_DEVICEW structure for EnumDisplayDevices |
| 48 |
class DISPLAY_DEVICEW(ctypes.Structure): |
| 49 |
_fields_ = [ |
| 50 |
('cb', wintypes.DWORD), |
| 51 |
('DeviceName', wintypes.WCHAR * 32), |
| 52 |
('DeviceString', wintypes.WCHAR * 128), |
| 53 |
('StateFlags', wintypes.DWORD), |
| 54 |
('DeviceID', wintypes.WCHAR * 128), |
| 55 |
('DeviceKey', wintypes.WCHAR * 128), |
| 56 |
] |
| 57 |
|
| 58 |
|
| 59 |
def get_screen_scale(device_name: str, logical_width: int, logical_height: int) -> float: |
| 60 |
""" |
| 61 |
Calculate the DPI scale factor for a screen. |
| 62 |
|
| 63 |
Args: |
| 64 |
device_name: The device name (e.g., "\\\\.\\DISPLAY1") |
| 65 |
logical_width: The logical width in pixels (DPI-scaled) |
| 66 |
logical_height: The logical height in pixels (DPI-scaled) |
| 67 |
|
| 68 |
Returns: |
| 69 |
The scale factor (e.g., 1.0, 1.5, 2.0, etc.) |
| 70 |
""" |
| 71 |
try: |
| 72 |
dm = DEVMODE() |
| 73 |
dm.dmSize = ctypes.sizeof(DEVMODE) |
| 74 |
|
| 75 |
# ENUM_CURRENT_SETTINGS = -1 |
| 76 |
if _user32.EnumDisplaySettingsW(device_name, -1, ctypes.byref(dm)): |
| 77 |
physical_width = dm.dmPelsWidth |
| 78 |
else: |
| 79 |
# Fallback to logical size if EnumDisplaySettings fails |
| 80 |
return 1.0 |
| 81 |
|
| 82 |
# Calculate scale from the ratio |
| 83 |
if logical_width > 0 and logical_height > 0: |
| 84 |
return physical_width / logical_width |
| 85 |
else: |
| 86 |
return 1.0 |
| 87 |
|
| 88 |
except Exception as e: |
| 89 |
_log.debug(f'Failed to get display settings: {e}') |
| 90 |
return 1.0 |