1. Force restore in case OS does not generate display changed event

2. Different restore event handling for password protected session and no-password session
This commit is contained in:
Kang Yu 2020-03-19 20:03:49 -07:00
parent 497c515c23
commit de9654707a

View file

@ -16,6 +16,7 @@ namespace Ninjacrab.PersistentWindows.Common
{ {
// constant // constant
private const int RestoreLatency = 500; // milliseconds to wait for next pass of window position recovery private const int RestoreLatency = 500; // milliseconds to wait for next pass of window position recovery
private const int DefaultRestoreLatency = 2000; // restore latency in case display changed event is not generated
private const int MaxRestoreLatency = 5000; // max milliseconds to wait after previous restore pass to tell if restore is finished private const int MaxRestoreLatency = 5000; // max milliseconds to wait after previous restore pass to tell if restore is finished
private const int MinRestoreTimes = 2; // restores with fixed RestoreLatency private const int MinRestoreTimes = 2; // restores with fixed RestoreLatency
private const int MaxRestoreTimesLocal = 4; // Max restores activated by further window event for local console session private const int MaxRestoreTimesLocal = 4; // Max restores activated by further window event for local console session
@ -47,6 +48,7 @@ namespace Ninjacrab.PersistentWindows.Common
// session control // session control
private bool remoteSession = false; private bool remoteSession = false;
private bool sessionLocked = false; //requires password to unlock
// last session // last session
private Dictionary<string, DateTime> eolTime = new Dictionary<string, DateTime>(); //time when end of life private Dictionary<string, DateTime> eolTime = new Dictionary<string, DateTime>(); //time when end of life
@ -168,7 +170,7 @@ namespace Ninjacrab.PersistentWindows.Common
{ {
DateTime time = DateTime.Now; DateTime time = DateTime.Now;
Log.Info("Display settings changing {0}", time); Log.Info("Display settings changing {0}", time);
ResetState(); EndDisplaySession();
}; };
SystemEvents.DisplaySettingsChanging += this.displaySettingsChangingHandler; SystemEvents.DisplaySettingsChanging += this.displaySettingsChangingHandler;
@ -179,10 +181,17 @@ namespace Ninjacrab.PersistentWindows.Common
DateTime time = DateTime.Now; DateTime time = DateTime.Now;
Log.Info("Display settings changed {0}", time); Log.Info("Display settings changed {0}", time);
ResetState(); if (sessionLocked)
{
restoringWindowPos = true; //wait for session unlock to start restore
StartRestoreTimer(); }
else
{
// change display on the fly
ResetState();
restoringWindowPos = true;
StartRestoreTimer();
}
}; };
SystemEvents.DisplaySettingsChanged += this.displaySettingsChangedHandler; SystemEvents.DisplaySettingsChanged += this.displaySettingsChangedHandler;
@ -194,10 +203,20 @@ namespace Ninjacrab.PersistentWindows.Common
{ {
case PowerModes.Suspend: case PowerModes.Suspend:
Log.Info("System suspending"); Log.Info("System suspending");
if (!sessionLocked)
{
EndDisplaySession();
}
break; break;
case PowerModes.Resume: case PowerModes.Resume:
Log.Info("System Resuming"); Log.Info("System Resuming");
if (!sessionLocked)
{
// force restore in case OS does not generate display changed event
restoringWindowPos = true;
StartRestoreTimer(milliSecond: DefaultRestoreLatency);
}
break; break;
} }
}; };
@ -208,23 +227,29 @@ namespace Ninjacrab.PersistentWindows.Common
{ {
switch (args.Reason) switch (args.Reason)
{ {
case SessionSwitchReason.RemoteDisconnect:
case SessionSwitchReason.SessionLock: case SessionSwitchReason.SessionLock:
Log.Trace("Session closing: reason {0}", args.Reason);
sessionLocked = true;
EndDisplaySession();
break;
case SessionSwitchReason.SessionUnlock:
Log.Trace("Session opening: reason {0}", args.Reason);
sessionLocked = false;
// force restore in case OS does not generate display changed event
restoringWindowPos = true;
StartRestoreTimer(milliSecond: DefaultRestoreLatency);
break;
case SessionSwitchReason.RemoteDisconnect:
case SessionSwitchReason.ConsoleDisconnect: case SessionSwitchReason.ConsoleDisconnect:
Log.Trace("Session closing: reason {0}", args.Reason); Log.Trace("Session closing: reason {0}", args.Reason);
ResetState();
RecordBatchCaptureTime(DateTime.Now);
break; break;
case SessionSwitchReason.RemoteConnect: case SessionSwitchReason.RemoteConnect:
remoteSession = true;
goto case SessionSwitchReason.SessionUnlock;
case SessionSwitchReason.SessionUnlock:
Log.Trace("Session opening: reason {0}", args.Reason); Log.Trace("Session opening: reason {0}", args.Reason);
remoteSession = true;
break; break;
case SessionSwitchReason.ConsoleConnect: case SessionSwitchReason.ConsoleConnect:
// session control
remoteSession = false; remoteSession = false;
Log.Trace("Session opening: reason {0}", args.Reason); Log.Trace("Session opening: reason {0}", args.Reason);
break; break;
@ -504,6 +529,13 @@ namespace Ninjacrab.PersistentWindows.Common
thread.Start(); thread.Start();
} }
private void EndDisplaySession()
{
CancelCaptureTimer();
ResetState();
RecordBatchCaptureTime(DateTime.Now);
}
private void ResetState() private void ResetState()
{ {
lock(controlLock) lock(controlLock)