/// <summary> /// Delay before monitoring paunse/unpause. /// </summary> IEnumerator Startup() { //why I did this instead of just sleeping the thread before entering loop, I will never know... x_x yield return(new WaitForSeconds(5f)); hWnd = StaticPinvoke.GetForegroundWindow(); t_watcher.Start(); }
public static volatile bool isDesktop = true; //volatile - cache flush/refresh, thread synchronisation not essential in this application..? //string currProcessName = null; //foreground process-name /// <summary> /// Background thread that monitor foreground windows type, size etc to decide pause/unpause unity main-thread. /// </summary> private void Update_T() { while (true) { // delay, can be higher but longer time to unpause/pause...50milliseconds seems fine on my i5 4core system. Thread.Sleep(50); // current foreground window. Only used to get foreground process-name, nothing is being stored. If you don't like rePaper monitoring then replace hWnd = StaticPinvoke.GetForegroundWindow() with hWnd = workerWOrig; hWnd = StaticPinvoke.GetForegroundWindow(); try { // retrieve foreground window process-name. Since repaper is started as non-admin process, name retrieval might fail for admin/certain processes. StaticPinvoke.GetWindowThreadProcessId(hWnd, out processID); currProcess = Process.GetProcessById(processID); } catch (Exception) { // todo: unity functions are thread unsafe, cant use unity debug. } if (true) //old-code/future use. { #region Exceptions_&_Fixes // failed to find in some cases. if (IntPtr.Equals(workerWOrig, IntPtr.Zero) == true) { folderView = StaticPinvoke.FindWindowEx(progman, IntPtr.Zero, "SHELLDLL_DefView", null); if (folderView == IntPtr.Zero) { do { workerWOrig = StaticPinvoke.FindWindowEx(StaticPinvoke.GetDesktopWindow(), workerWOrig, "WorkerW", null); folderView = StaticPinvoke.FindWindowEx(workerWOrig, IntPtr.Zero, "SHELLDLL_DefView", null); } while (folderView == IntPtr.Zero && workerWOrig != IntPtr.Zero); } } try { //currProcessName = currProcess.ProcessName.toLowerInvarient(); if (pause_disable == true || MenuController.menuController.userSettings.bvar2 == true || String.IsNullOrEmpty(currProcess.ProcessName)) //debugging, skip rePaper pause/sleep. { continue; } skipAhead = false; if (MenuController.menuController.userSettings.appFocusPause == false) { //user defined application-pause/sleep-rules foreach (var item in MenuController.menuController.appList) { //if (item.processName.ToLowerInvariant() == currProcessName) if (currProcess.ProcessName.Equals(item.processName, StringComparison.InvariantCultureIgnoreCase)) { if (item.sleep == false) //always run when this process is foreground window, even when maximized { hWnd = workerWOrig; //assume desktop, to force unpause/wakeup of rePaper. } else //sleep { isDesktop = false; runningFullScreen = true; main.instance.ChangeTrayIcon(runningFullScreen); manualResetEvent.Reset(); skipAhead = true; } break; } } if (skipAhead == true) { continue; } } if (currProcess.ProcessName.Equals("repaper-unity", StringComparison.InvariantCultureIgnoreCase) || // bug fix: early launch, might detect application as hWnd ID currProcess.ProcessName.Equals("config-repaper", StringComparison.InvariantCultureIgnoreCase) || //config utility for rePaper currProcess.ProcessName.Equals("shellexperiencehost", StringComparison.InvariantCultureIgnoreCase) || //notification tray etc currProcess.ProcessName.Equals("searchui", StringComparison.InvariantCultureIgnoreCase) //startmenu search etc //|| currProcessName.Equals("applicationframehost", StringComparison.InvariantCultureIgnoreCase) //windows10 dialogues like adjust time, all uwp processes(store apps) comes under this. //|| currProcessName.Equals("discord", StringComparison.InvariantCultureIgnoreCase) //fix, when trying to minimize maximised discord window, it does not minimize properly to systemtray, foreground windowhandle still at discord. ) { hWnd = workerWOrig; //assume desktop, to force unpause/wakeup of rePaper. For win7 workerWOrig is set as progman in Start() } } catch (Exception) { //ignore } if (IntPtr.Equals(old_unity_handle, hWnd))// bug fix: early launch, might detect application as hWnd ID. what a pain.. { hWnd = workerWOrig; } if (IntPtr.Equals(shell_tray, IntPtr.Zero) == true) //startmenu, sometimes become zero? { shell_tray = StaticPinvoke.FindWindow("Shell_TrayWnd", null); } #endregion Exceptions_&_Fixes if (hWnd != null && !hWnd.Equals(IntPtr.Zero)) { if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle))) { StaticPinvoke.GetWindowRect(hWnd, out appBounds); //open applications window size, could use IsZoomed() if such fine control is not required. screenBounds = System.Windows.Forms.Screen.FromHandle(hWnd).Bounds; if ((appBounds.Bottom - appBounds.Top) >= screenBounds.Height * .9f && (appBounds.Right - appBounds.Left) >= screenBounds.Width * .9f) //foreground window > 90% work_area, ie: work_area = display_resolution - taskbar { //todo: could avoid this check everytime by making two versions of this fn, win7 & other. if (System.Environment.OSVersion.Version.Major == 6 && System.Environment.OSVersion.Version.Minor == 1) //windows 7 { if (IntPtr.Equals(hWnd, progman) != true) //not equal to desktop & fullscreen apprunning { isDesktop = false; runningFullScreen = true; main.instance.ChangeTrayIcon(runningFullScreen); manualResetEvent.Reset(); } else // currently at desktop(if user clicks or minimize all apps) { isDesktop = true; manualResetEvent.Set(); runningFullScreen = false; main.instance.ChangeTrayIcon(runningFullScreen); } } else // windows 8, 10 etc.. { if (IntPtr.Equals(hWnd, workerWOrig) != true) //not equal to desktop & fullscreen apprunning { isDesktop = false; runningFullScreen = true; main.instance.ChangeTrayIcon(runningFullScreen); manualResetEvent.Reset(); } else // currently at desktop(if user clicks) { isDesktop = true; manualResetEvent.Set(); runningFullScreen = false; main.instance.ChangeTrayIcon(runningFullScreen); } } } else if (IntPtr.Equals(shell_tray, IntPtr.Zero) != true && IntPtr.Equals(hWnd, shell_tray) == true) //systrayhandle { isDesktop = true; main.instance.ChangeTrayIcon(runningFullScreen); runningFullScreen = false; manualResetEvent.Set(); } else //application window is not greater >90% { isDesktop = false; if (MenuController.menuController.userSettings.appFocusPause == false) { runningFullScreen = false; main.instance.ChangeTrayIcon(runningFullScreen); manualResetEvent.Set(); } else { runningFullScreen = true; main.instance.ChangeTrayIcon(runningFullScreen); manualResetEvent.Reset(); } } } } } } //while loop }