Skip to content

Commit

Permalink
smart recognition of gaming session (compared with interactive normal…
Browse files Browse the repository at this point in the history
… session) and avoid auto-restore for it
  • Loading branch information
kangyu-california committed Feb 16, 2022
1 parent 78d2f7e commit 0112f55
Showing 1 changed file with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private Dictionary<string, Dictionary<IntPtr, List<ApplicationDisplayMetrics>>>
private Queue<IntPtr> pendingMoveEvents = new Queue<IntPtr>(); // queue of window with possible position change for capture
private HashSet<IntPtr> pendingActivateWindows = new HashSet<IntPtr>();
private HashSet<string> normalSessions = new HashSet<string>(); //normal user sessions, for differentiating full screen game session or other transient session
private Dictionary<string, int> windowActiveCnt = new Dictionary<string, int>();
private bool userMove = false; //received window event due to user move
private bool userMovePrev = false; //prev value of userMove
private HashSet<IntPtr> tidyTabWindows = new HashSet<IntPtr>(); //tabbed windows bundled by tidytab
Expand Down Expand Up @@ -477,25 +478,9 @@ public bool Start(bool auto_restore_from_db = false)
if (showDesktop)
ShowDesktop();

//remove capture in gaming mode
if (displayKey != curDisplayKey && monitorApplications.ContainsKey(curDisplayKey))
{
bool gaming_mode = true;
foreach (var hwnd in monitorApplications[curDisplayKey].Keys)
{
if (monitorApplications[curDisplayKey][hwnd].Count > 1)
{
gaming_mode = false;
break;
}
}

if (gaming_mode)
{
monitorApplications.Remove(curDisplayKey);
Log.Error("finish gaming mode {0}", curDisplayKey);
}
}
// reset foreground window counter for gaming mode
if (windowActiveCnt.ContainsKey(curDisplayKey))
windowActiveCnt[curDisplayKey] = 0;

// change display on the fly
curDisplayKey = displayKey;
Expand Down Expand Up @@ -606,6 +591,8 @@ public bool Start(bool auto_restore_from_db = false)
{
bool db_exist = persistDB.CollectionExists(curDisplayKey);
enableRestoreMenu(db_exist);
if (db_exist)
normalSessions.Add(curDisplayKey);
if (db_exist && auto_restore_from_db)
{
restoringFromDB = true;
Expand Down Expand Up @@ -854,6 +841,18 @@ private void PostActivateWindows()
ActivateWindow(hwnd);
}

if (pendingWindows.Count > 0)
{
if (!windowActiveCnt.ContainsKey(curDisplayKey))
windowActiveCnt.Add(curDisplayKey, 0);
windowActiveCnt[curDisplayKey]++;
if (windowActiveCnt[curDisplayKey] >= 2)
{
normalSessions.Add(curDisplayKey);
Log.Error("normal session {0} due to active count {1}", curDisplayKey, windowActiveCnt[curDisplayKey]);
}
}

pendingActivateWindows.Clear();
}
catch (Exception ex)
Expand Down Expand Up @@ -1063,6 +1062,7 @@ private void WinEventProc(IntPtr hWinEventHook, User32Events eventType, IntPtr h
case User32Events.EVENT_SYSTEM_MINIMIZESTART:
case User32Events.EVENT_SYSTEM_MOVESIZEEND:
// only care about child windows that are moved by user
//normalSessions.Add(curDisplayKey);
allUserMoveWindows.Add(hwnd);
break;

Expand Down Expand Up @@ -1215,7 +1215,6 @@ private void WinEventProc(IntPtr hWinEventHook, User32Events eventType, IntPtr h
foreGroundWindow[curDisplayKey] = hwnd;
foregroundTimer.Change(100, Timeout.Infinite);


// Occasionaly OS might bring a window to foreground upon sleep
// If the window move is initiated by OS (before sleep),
// keep restart capture timer would eventually discard these moves
Expand All @@ -1225,6 +1224,7 @@ private void WinEventProc(IntPtr hWinEventHook, User32Events eventType, IntPtr h
// If the window move is caused by user snapping window to screen edge,
// delay capture by a few seconds should be fine.

Log.Error("activate {0}", GetWindowTitle(hwnd));
if (!pendingActivateWindows.Contains(hwnd))
pendingActivateWindows.Add(hwnd);

Expand All @@ -1233,7 +1233,7 @@ private void WinEventProc(IntPtr hWinEventHook, User32Events eventType, IntPtr h
else
StartCaptureTimer();

userMove = true;
//userMove = true;
}
}

Expand Down Expand Up @@ -1282,6 +1282,7 @@ private void WinEventProc(IntPtr hWinEventHook, User32Events eventType, IntPtr h
{
//capture with slight delay inperceivable by user, required for full screen mode recovery
StartCaptureTimer(UserMoveLatency / 4);
Log.Event("{0} {1}", eventType, GetWindowTitle(hwnd));
userMove = true;
}
break;
Expand All @@ -1306,7 +1307,9 @@ private void WinEventProc(IntPtr hWinEventHook, User32Events eventType, IntPtr h
if (monitorApplications.ContainsKey(curDisplayKey) && monitorApplications[curDisplayKey].ContainsKey(hwnd) || allUserMoveWindows.Contains(hwnd))
{
StartCaptureTimer(UserMoveLatency / 4);
userMove = true;
Log.Event("{0} {1}", eventType, GetWindowTitle(hwnd));
if (eventType == User32Events.EVENT_SYSTEM_MOVESIZEEND)
userMove = true;
}
break;
}
Expand Down Expand Up @@ -1730,6 +1733,7 @@ public void BatchCaptureApplicationsOnCurrentDisplays(bool saveToDB = false)
if (userMovePrev)
{
normalSessions.Add(curDisplayKey);
Log.Error("normal session {0} due to user move", curDisplayKey, userMovePrev);
}

CaptureApplicationsOnCurrentDisplays(displayKey, saveToDB : saveToDB); //implies auto delayed capture
Expand Down Expand Up @@ -1894,7 +1898,9 @@ private void CaptureApplicationsOnCurrentDisplays(string displayKey, bool saveTo
// confirmed user moves
RecordLastUserActionTime(time: DateTime.Now, displayKey : displayKey);
if (movedWindows > 0)
{
Log.Trace("{0} windows captured", movedWindows);
}
}
else
{
Expand Down Expand Up @@ -2219,6 +2225,9 @@ private void TimerRestore()
if (!restoringFromMem && !restoringFromDB)
return;

if (restoringFromDB || restoringSnapshot)
normalSessions.Add(curDisplayKey);

Log.Trace("Restore timer expired");

lock(restoreLock)
Expand Down

0 comments on commit 0112f55

Please sign in to comment.