Skip to content

Commit

Permalink
fixes a bug that caused script to prematurely exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonary committed Apr 21, 2024
1 parent 56ed662 commit e183833
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion MonitorSwapper-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function Create-Pipe($pipeName) {

for ($i = 0; $i -lt 10; $i++) {
# We could be pending a previous termination, so lets wait up to 10 seconds.
if(-not (Test-Path "\\.\pipe\$pipeName")){
if (-not (Test-Path "\\.\pipe\$pipeName")) {
break
}

Expand Down
33 changes: 17 additions & 16 deletions MonitorSwapper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ if ($null -eq $async) {

. .\MonitorSwapper-Functions.ps1

if(Test-Path "\\.\pipe\MonitorSwapper"){
if (Test-Path "\\.\pipe\MonitorSwapper") {
Send-PipeMessage MonitorSwapper Terminate
Start-Sleep -Seconds 20
}

if(Test-Path "\\.\pipe\MonitorSwapper-OnStreamEnd"){
if (Test-Path "\\.\pipe\MonitorSwapper-OnStreamEnd") {
Send-PipeMessage MonitorSwapper-OnStreamEnd Terminate
Start-Sleep -Seconds 5
}

Start-Transcript -Path .\log.txt
# Attempt to start the transcript multiple times in case previous process is still running.
for ($i = 0; $i -lt 10; $i++) {

try {
Start-Transcript .\log.txt -ErrorAction Stop
break;
}
catch {
Start-Sleep -Seconds 1
}
}

try {

Expand Down Expand Up @@ -62,7 +72,7 @@ try {
$pipeName = "MonitorSwapper"
for ($i = 0; $i -lt 10; $i++) {
# We could be pending a previous termination, so lets wait up to 10 seconds.
if(-not (Test-Path "\\.\pipe\$pipeName")){
if (-not (Test-Path "\\.\pipe\$pipeName")) {
break
}

Expand Down Expand Up @@ -95,27 +105,18 @@ try {
if ($null -ne $eventFired) {
$eventName = $eventFired.MessageData
Write-Host "Processing event: $eventName"
if($eventName -eq "Start"){
if ($eventName -eq "Start") {
OnStreamStart
}
else{
else {
OnStreamEndAsJob | Wait-Job
break;
}
Remove-Event -EventIdentifier $eventFired.EventIdentifier
}
elseif ($pipeJob.State -eq "Completed") {
Write-Host "Request to terminate has been processed, script will now revert monitor configuration."
$endJob = OnStreamEndAsJob

# Continually poll the job to write to log file once every 1 seconds
while($endJob.State -ne "Completed"){
$endJob | Receive-Job
Start-Sleep -Seconds 1
}

# Pipe output one more time in case there is any remaining log statements.
$endJob | Receive-Job
OnStreamEndAsJob | Wait-Job | Receive-Job
break;
}

Expand Down
6 changes: 2 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,5 @@ If you encounter issues with the script, you can try the following:
- Double check and make sure you have put the correct "dummyMonitorId" in the settings.json file, that way the script doesn't attempt to restore monitor profiles that are already active.
### Recent Changes
- Fixes a bug that prevented the script from restoring the display in some scenarios, if user left their Moonlight client at the host screen.
- Fixed a bug that prevented the script from self-terminating itself after the user suspended the session longer than their defined grace period in the settings file.
- Better multi-monitor support by validating that all screens have been restored instead of just the main primary one.
- Primary monitor id is no longer required in settings and has been removed, script will now automatically figure out the primary monitors identity.
- v1.1.3 - Resolved an issue where monitor configurations could incorrectly alter under certain conditions, such as when a stream was suspended and subsequently resumed, if the monitor restoration process from a previous session was not completed successfully. This was due to an unexpected behavior in Windows' display settings API, which applied changes even when a monitor swap failed. The fix ensures the script no longer attempts to restore the primary monitor if users are currently streaming, which would cause the host resolution to change.
- v1.1.4 - Fixed an issue that caused the script to prematurely exit before restoring the monitor (Fixes #31)

0 comments on commit e183833

Please sign in to comment.