Nonary / MonitorSwapAutomation

Automates swapping to a dummy plug when streaming, then automates swapping back to primary monitor once finished.
289 stars 8 forks source link

Issues with restoring both monitors (dual screen) #9

Open Nonary opened 1 year ago

Nonary commented 1 year ago

A bug has been identified in the Nirsoft MultiMonitor tool that prevents certain user setups from being able to properly restore both monitors, usually what will happen is only one of the two will be restored. I am aware of this issue and currently I do not have an easy solution to it as I do not maintain the MultiMonitor tool.

All other monitor profile loading tools are very user unfriendly compared to editing a simple config file, as such I do not have any plans to swap the backend of the script because it would become even more confusing to the general user public.

Workaround

For now, I suggest using this workaround to the issue until the issue has been resolved on Nirsoft's MultiMonitorTool.

  1. Download Monitor Switcher from Sourceforge https://sourceforge.net/projects/monitorswitcher/

  2. Copy the MonitorSwitcher.exe and paste it into the same folder MonitorSwapper is saved in.

  3. Make sure you current Monitor Settings are exactly how you want them to be once you've finished streaming, as we will be saving that configuration as the restore point once you've finished streaming.

  4. Open command prompt or PowerShell at the folder MonitorSwapper (not Switcher) is saved in, for Windows 11 users that can easily be done by right clicking a blank area in the folder and selecting Open in Terminal.

  5. Type in MonitorSwitcher, then press tab so the terminal automatically completes the rest of the command, then type in -save:Primary.xml

  6. It should look like this:

    .\MonitorSwitcher.exe -save:Primary.xml
    
    OR
    
    MonitorSwitcher.exe -save:Primary.xml
    

    PowerShell will prepend the command with .\ this is normal.

  7. Open the Events.ps1 file, or if you're using an older version of this script... open the MonitorSwapper-Functions.ps1 file instead.

  8. In the "SetPrimaryScreen" function, look for this line:

      & .\MultiMonitorTool.exe /LoadConfig "primary.cfg"
  9. Replace the line mentioned above with this line instead.

    & .\MonitorSwitcher.exe -load:Primary.xml
  10. Save the file, and you should be good to go.

nem8 commented 11 months ago

Seems to work like a charm. Thanks for the writeup!

kdliu86 commented 8 months ago

Confirming that I had to do this,

My log was spitting out the following

Screen will not be reverted because we are already streaming
Failed to restore display(s), some displays require multiple attempts and may not restore until returning back to the computer. Trying again after 5 seconds...
Failed to restore display(s), some displays require multiple attempts and may not restore until returning back to the computer. Trying again after 5 seconds...

Etc.

My monitors are "slow' to turn on though,

Mon0: Dummy Plug Mon1: 1920x1080 LG24GM77 Mon2: 2560x1440 LG27GP850 Mon3: 1920x1080 Asus 247H

Mon1 and Mon2 would hang and not reconnect. Adding in MonitorSwitcher worked.

tzhouML commented 5 months ago
  1. Open the MonitorSwapper-Functions.ps1 file.
  2. In the "SetPrimaryScreen" function, look for this line:
      & .\MultiMonitorTool.exe /LoadConfig "primary.cfg"
  3. Replace the line mentioned above with this line instead.
    & .\MonitorSwitcher.exe -load:Primary.xml
  4. Save the file, and you should be good to go.

Heads up for anyone doing this fix at step 7 and can't find MonitorSwapper-Functions.ps1, the file to look in is now Events.ps1

Metrinui commented 5 months ago

Did the steps but when I close my stream via Moonlight, the first time it won't switch back, second time I close stream it switches back but then my Main display turns on and off every 5-10 seconds. Going into Display settings and enabling my virtual monitor cause it to becomes disabled when my main display turns off, like the script is still running and repeating itself

Nonary commented 5 months ago

Did the steps but when I close my stream via Moonlight, the first time it won't switch back, second time I close stream it switches back but then my Main display turns on and off every 5-10 seconds. Going into Display settings and enabling my virtual monitor cause it to becomes disabled when my main display turns off, like the script is still running and repeating itself

Can you zip the entire script folder, and also upload the current_monitor_cfg file located in %temp% so I can look at that?

Metrinui commented 5 months ago

Did the steps but when I close my stream via Moonlight, the first time it won't switch back, second time I close stream it switches back but then my Main display turns on and off every 5-10 seconds. Going into Display settings and enabling my virtual monitor cause it to becomes disabled when my main display turns off, like the script is still running and repeating itself

Can you zip the entire script folder, and also upload the current_monitor_cfg file located in %temp% so I can look at that?

MonitorSwapper.zip

Nonary commented 5 months ago

Did the steps but when I close my stream via Moonlight, the first time it won't switch back, second time I close stream it switches back but then my Main display turns on and off every 5-10 seconds. Going into Display settings and enabling my virtual monitor cause it to becomes disabled when my main display turns off, like the script is still running and repeating itself

Can you zip the entire script folder, and also upload the current_monitor_cfg file located in %temp% so I can look at that?

MonitorSwapper.zip

Not seeing any errors in the log that would cause that, can you replace your Event.ps1 with this one

Events.zip

And in settings, enable the debug option by setting it to true

Nonary commented 5 months ago

@Metrinui I think I fixed it, can you try this one and let me know?

MonitorSwapper_v1.1.9.zip

Metrinui commented 5 months ago

@Metrinui I think I fixed it, can you try this one and let me know?

MonitorSwapper_v1.1.9.zip

You're amazing, thank you so much! Problem solved!!! What an absolute legend!

RikuKunMS2 commented 4 months ago

Thanks for this tip, I ended up having issues with it switching between dual monitors and a dummy because of the monitor startup time, (also OP's monitorswitcher didn't work) I found that using the commands from https://www.nirsoft.net/utils/multi_monitor_tool.html and editing events.ps1 I was able to get it working by using /enable and /disable to one by one enable and disable monitors then use /LoadConfig to set my left and right monitors to the appropriate locations

function SetPrimaryScreen() {
    Write-Debug "Starting SetPrimaryScreen function"
    Write-Debug "Checking if currently streaming"
    if (IsCurrentlyStreaming) {
        Write-Debug "Currently streaming, exiting function"
        return
    }
    Write-Debug "Loading Monitor 1"
    & .\MultiMonitorTool.exe /enable "\\.\DISPLAY1"
    Write-Debug "Sleeping for 3 seconds to allow configuration to take effect"
    Start-Sleep -Seconds 3
        Write-Debug "Loading Monitor 2"
    & .\MultiMonitorTool.exe /enable "\\.\DISPLAY2"
    Write-Debug "Sleeping for 3 seconds to allow configuration to take effect"
    Start-Sleep -Seconds 3
            Write-Debug "Disable Monitor 3"
    & .\MultiMonitorTool.exe /disable "\\.\DISPLAY5"
    Write-Debug "Sleeping for 3 seconds to allow configuration to take effect"
    Start-Sleep -Seconds 3
        Write-Debug "Set Left Right"
    & .\MultiMonitorTool.exe /LoadConfig primary.cfg
    Write-Debug "Sleeping for 3 seconds to allow configuration to take effect"
    Start-Sleep -Seconds 3
    Write-Debug "SetPrimaryScreen function completed"}
jdkang commented 1 month ago

Based on @RikuKunMS2 comment, this is what I had to do for my "long startup" monitor.

The delta is mostly auto-detecting which monitors to /disable and /enable.

One might have to play with the delay variables.

My Setup

Events.ps1

function SetPrimaryScreen() {
    Write-Debug "Starting SetPrimaryScreen function"

    Write-Debug "Checking if currently streaming"
    if (IsCurrentlyStreaming) {
        Write-Debug "Currently streaming, exiting function"
        return
    }

    # ---------------------------------------------
    # extra cruft for 'long turn on' monitors
    # https://github.com/Nonary/MonitorSwapAutomation/issues/9#issuecomment-2181039417
    # ---------------------------------------------
    $sleepDelayPerMon = 4
    $trailingSleep = 3
    $monIds = @( Get-Content .\primary.cfg  | Where-Object { $_ -match '^MonitorID=' } | ForEach-Object { (($_ -split '=')[1]) } )

    # there's a bug in the windows spotify client with multi monitors
    #get-process -Name '*spotify*' | Stop-Process -Force -ea 0

    Write-Debug "Disabling $($dummyMonitorId) + $($sleepDelayPerMon)s sleep"
    & .\MultiMonitorTool.exe /disable $dummyMonitorId
    Start-Sleep -Seconds $sleepDelayPerMon

    foreach($monId in $monIds) {
        if($monId -eq $dummyMonitorId) {
            continue
        }
        Write-Debug "Enabling $($monId) + $($sleepDelayPerMon)s sleep"
        & .\MultiMonitorTool.exe /enable $monId
        Start-Sleep -Seconds $sleepDelayPerMon
    }
    Write-Debug "Trailing disable/enable sleep for $($trailingSleep)s"
    Start-Sleep -Seconds $trailingSleep
    # ------------------------------------------

# ...