From 311d32d4364fce5e8233b5de8bb25e39797fd3d9 Mon Sep 17 00:00:00 2001 From: Hoang Hong Quan Date: Wed, 16 Oct 2024 00:37:18 +0700 Subject: [PATCH] Update code for gathering monitor information --- Scripts/platforms/windows.py | 74 ++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/Scripts/platforms/windows.py b/Scripts/platforms/windows.py index 518b378..94cd5da 100644 --- a/Scripts/platforms/windows.py +++ b/Scripts/platforms/windows.py @@ -249,33 +249,29 @@ class WindowsHardwareInfo: def monitor(self): monitor_info = {} + monitor_properties = sorted(self.devices_by_class.get("Monitor"), key=lambda x: x.PNPDeviceID) + try: wmi_service = wmi.WMI(namespace="root\\wmi") monitor_ids = sorted(wmi_service.WmiMonitorID(), key=lambda x: x.InstanceName) connection_params = sorted(wmi_service.WmiMonitorConnectionParams(), key=lambda x: x.InstanceName) source_modes = sorted(wmi_service.WmiMonitorListedSupportedSourceModes(), key=lambda x: x.InstanceName) - monitor_properties = sorted(self.devices_by_class.get("Monitor"), key=lambda x: x.PNPDeviceID) + except: + monitor_ids = connection_params = source_modes = [] - for monitor_id, connection_param, source_modes, monitor_property in zip(monitor_ids, connection_params, source_modes, monitor_properties): - try: - user_friendly_name = monitor_id.UserFriendlyName - monitor_name = bytes(user_friendly_name).decode('ascii').rstrip('\x00') - except: - monitor_name = monitor_id.InstanceName.split("\\")[1] + for monitor_property in monitor_properties: + try: + monitor_id = next((monitor_id for monitor_id in monitor_ids if monitor_id.InstanceName == monitor_property.PNPDeviceID), None) + user_friendly_name = monitor_id.UserFriendlyName + monitor_name = bytes(user_friendly_name).decode('ascii').rstrip('\x00') + except: + monitor_name = monitor_property.PNPDeviceID.split("\\")[1] + + try: + connection_param = next((connection_param for connection_param in connection_params if connection_param.InstanceName == monitor_property.PNPDeviceID), None) video_output_type = connection_param.VideoOutputTechnology - monitor_source_modes = source_modes.MonitorSourceModes - connected_gpu = None - parent_device_id = monitor_property.GetDeviceProperties(["DEVPKEY_Device_Parent"])[0][0].Data - for device in self.devices_by_class.get("Display"): - device_name = device.Name or "Unknown" - pnp_device_id = device.PNPDeviceID - - if pnp_device_id and pnp_device_id.startswith("PCI") and pnp_device_id.upper() == parent_device_id.upper(): - connected_gpu = device_name - break - if video_output_type == 0: video_output_type = "VGA" elif video_output_type == 4: @@ -292,24 +288,38 @@ class WindowsHardwareInfo: video_output_type = "Internal" else: video_output_type = "Uninitialized" + except: + video_output_type = "Uninitialized" + + try: + source_mode = next((source_mode for source_mode in source_modes if source_mode.InstanceName == monitor_property.PNPDeviceID), None) + monitor_source_modes = source_mode.MonitorSourceModes - max_h_active = 0 - max_v_active = 0 - for monitor_source_mode in monitor_source_modes: max_h_active = max(max_h_active, monitor_source_mode.HorizontalActivePixels) max_v_active = max(max_v_active, monitor_source_mode.VerticalActivePixels) - - unique_monitor_name = self.utils.get_unique_key(monitor_name, monitor_info) - monitor_info[unique_monitor_name] = { - "Connector Type": video_output_type, - "Resolution": "{}x{}".format(max_h_active, max_v_active) - } - if connected_gpu: - monitor_info[unique_monitor_name]["Connected GPU"] = connected_gpu - except: - pass - + except: + max_h_active = 0 + max_v_active = 0 + + connected_gpu = None + parent_device_id = monitor_property.GetDeviceProperties(["DEVPKEY_Device_Parent"])[0][0].Data + for device in self.devices_by_class.get("Display"): + device_name = device.Name or "Unknown" + pnp_device_id = device.PNPDeviceID + + if pnp_device_id and pnp_device_id.startswith("PCI") and pnp_device_id.upper() == parent_device_id.upper(): + connected_gpu = device_name + break + + unique_monitor_name = self.utils.get_unique_key(monitor_name, monitor_info) + monitor_info[unique_monitor_name] = { + "Connector Type": video_output_type, + "Resolution": "{}x{}".format(max_h_active, max_v_active) + } + if connected_gpu: + monitor_info[unique_monitor_name]["Connected GPU"] = connected_gpu + return monitor_info def network(self):