patrick-theprogrammer / WindowsDisplayManager

A powershell module for managing Windows display settings.
https://www.powershellgallery.com/packages/WindowsDisplayManager
MIT License
18 stars 1 forks source link

Can't enable disabled monitor #2

Open Cynary opened 4 months ago

Cynary commented 4 months ago

If a monitor is disabled when I run GetAllPotentialDisplays, I have no way to enable it. But if I use an earlier returned value, it works. Here's an easy repro:

# display 0 is enabled
$displays = WindowsDisplayManager\GetAllPotentialDisplays
$displays[0].disable() # Returns True, display 0 is disabled now.
$displays[0].enable() # Returns True, display 0 is enabled now.
$displays[0].disable() # Returns True, display 0 is disabled now.
$displays = WindowsDisplayManager\GetAllPotentialDisplays # display 0 now has no target.
$displays[0].enable() # Returns True, but nothing happens.

Output with display 0 enabled:

Display ID   Description            Active  Enabled  Primary  Resolution           HDR Info               Position   Recommended Resolution   Source                       Target
----------   -----------            ------  -------  -------  ----------           --------               --------   ----------------------   ------                       ------
0-4353       AW3423DWF via NVIDIA   True    True     False    Width       : 3440   HdrSupported : True    X : 3840   Width  : 3440            Id          : 0              Id             : 4353
             GeForce RTX 4090                                 Height      : 1440   HdrEnabled   : True    Y : 0      Height : 1440            Name        : \\.\DISPLAY1   FriendlyName   : AW3423DWF
                                                              RefreshRate : 60     BitDepth     : 10                                          Description : NVIDIA         ConnectionType :
                                                                                                                                              GeForce RTX 4090             DisplayportExternal
1            Unconnected NVIDIA     True    False    True     Width       : 3840   N/A                    X : 0      N/A                      Id          : 1              None
             GeForce RTX 4090                                 Height      : 2160                          Y : 0                               Name        : \\.\DISPLAY2
             source                                           RefreshRate : 120                                                               Description : NVIDIA
                                                                                                                                              GeForce RTX 4090
2            Unconnected NVIDIA     False   False    False    N/A                  N/A                    N/A        N/A                      Id          : 2              None
             GeForce RTX 4090                                                                                                                 Name        : \\.\DISPLAY3
             source                                                                                                                           Description : NVIDIA
                                                                                                                                              GeForce RTX 4090
3            Unconnected NVIDIA     False   False    False    N/A                  N/A                    N/A        N/A                      Id          : 3              None
             GeForce RTX 4090                                                                                                                 Name        : \\.\DISPLAY4
             source                                                                                                                           Description : NVIDIA
                                                                                                                                              GeForce RTX 4090

Output with display 0 disabled:

Display ID   Description            Active  Enabled  Primary  Resolution           HDR Info               Position   Recommended Resolution   Source                       Target
----------   -----------            ------  -------  -------  ----------           --------               --------   ----------------------   ------                       ------
0            Unconnected NVIDIA     False   False    False    N/A                  N/A                    N/A        N/A                      Id          : 0              None
             GeForce RTX 4090                                                                                                                 Name        : \\.\DISPLAY1
             source                                                                                                                           Description : NVIDIA
                                                                                                                                              GeForce RTX 4090
1-4352       DENON-AVR via NVIDIA   True    True     True     Width       : 3840   HdrSupported : True    X : 0      Width  : 3840            Id          : 1              Id             : 4352
             GeForce RTX 4090                                 Height      : 2160   HdrEnabled   : True    Y : 0      Height : 2160            Name        : \\.\DISPLAY2   FriendlyName   : DENON-AVR
                                                              RefreshRate : 120    BitDepth     : 10                                          Description : NVIDIA         ConnectionType : Hdmi
                                                                                                                                              GeForce RTX 4090
2            Unconnected NVIDIA     False   False    False    N/A                  N/A                    N/A        N/A                      Id          : 2              None
             GeForce RTX 4090                                                                                                                 Name        : \\.\DISPLAY3
             source                                                                                                                           Description : NVIDIA
                                                                                                                                              GeForce RTX 4090
3            Unconnected NVIDIA     False   False    False    N/A                  N/A                    N/A        N/A                      Id          : 3              None
             GeForce RTX 4090                                                                                                                 Name        : \\.\DISPLAY4
             source                                                                                                                           Description : NVIDIA
                                                                                                                                              GeForce RTX 4090
patrick-theprogrammer commented 2 weeks ago

You can manually supply a target id of the device you want to enable to the enable function like $displays[0].enable(4353) in this case.

The windows display apis only really give us a list of sources eg GPU ports and, if a one of those is connected, information about the target it's connected to eg your monitor. Also if you unplug and plug things back in, the target IDs may change.

Basically you have to remember the target id of the device back when it was enabled to be able to reenable it.

Display objects will hold onto the IDs they have when created which is why you can reenable so long as you don't refetch the display. The only way I can think of making this better would involve implementing some persistent storage for the tool to remember previous target IDs even for newly fetched disabled displays. That would add some complexity though to do properly, and I'm not sure when or if I'll have time to get to it.

Curious do you have a particular reason in your use case why you can't hold on to the display object and have to re-fetch it after disable?