-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make index
Unique and Ordered by win32api.EnumDisplayMonitors()
#40
Conversation
Having a unique Regarding Linux, it lacks anything like |
Yes, the primary display is listed first by default. However, after testing by unplugging and replugging the primary display, it comes back as the last in all three lists: Since asleep monitors might be skipped, the order from The main reason I started looking into this indexing issue is because I wanted to use the monitor coordinates from The import win32api, wmi, re
import screen_brightness_control as sbc
def extract_uid(device_id):
"""Extract UID from string."""
match = re.search(r"UID(\d+)", device_id)
return match.group(1) if match else None
for monitor_enum in win32api.EnumDisplayMonitors():
print(monitor_enum)
pyhandle = monitor_enum[0]
monitor_info = win32api.GetMonitorInfo(pyhandle)
device = win32api.EnumDisplayDevices(monitor_info["Device"], 0, 1)
device_id = device.DeviceID
UID = extract_uid(device_id)
print(f"{UID=}\t{device_id=}")
print("-" * 100)
wmi = wmi.WMI(namespace="wmi")
for monitor in wmi.WmiMonitorDescriptorMethods():
instance_name = monitor.InstanceName
UID = extract_uid(instance_name)
print(f"{UID=}\t{instance_name=}")
print("-" * 100)
for monitor in sbc.list_monitors_info(allow_duplicates=True):
print(monitor)
|
Just checked this on my machine and it looks like the current behaviour of the library lists my secondary monitor first anyway, which I don't think it used to do. So yeah, the order apparently can't be guarenteed on Windows or Linux so I'm happy to get rid of this expectation.
I'm happy to allow discontinuous indices in this case. I think it makes some intuitive sense rather than one monitor going to sleep and all the numbers shuffling around and getting reassigned to awake monitors.
What kind of scenario would have monitors connected to the same port? Would that be through a splitter/adapter? Might be worth testing that scenario and seeing what WMI/win32api spits out. I'll see if I have any splitters lying around I can mess with The UID idea seems promising, and could solve alot of the hassle of uniquely identifying monitors if Windows just handles it for us. On Linux, I'll play around with it as well, but for now the proposed changes on this branch make sense to me. I'll do some testing locally and let you know if there are any issues. Otherwise, LGTM |
So I've done some playing around with this and found a couple of things. First, I've taken a look at the current code and how displays are ordered, and they're ordered according to the return of Second is an issue with how Currently, screen_brightness_control/screen_brightness_control/windows.py Lines 190 to 196 in 8ca1197
In The problem is that this PR makes the index "global" scoped across all methods, and also reliant on the order that Consider a scenario where we have 1 laptop screen and 1 external monitor and the external monitor is enumerated first. The laptop screen will get an index of 1, but Unfortunately, because of this I don't think this PR is feasible. On Windows and Linux the In terms of including a unique identifer, I'm working on getting an HDMI splitter to see how Windows assigns the |
What’s this about:
I made a small change so that the
index
for each monitor is unique and matches the order fromwin32api.EnumDisplayMonitors()
. The order actually matters in some cases, and I think most people expectindex
to be unique already.What’s changed:
EnumDisplayMonitors()
will get theirindex
based on that order.Quick question:
Do you think this is a good idea for handling
index
? I’m not sure how this will work on Linux yet – I’ve looked at the code inlinux.py
, and it seems a bit overwhelming, so I’m not sure if this approach will be possible there. If you think it’s worth pursuing, I’ll dive into that too. Let me know what you think!