From 57fddf4cdc9722f172435acaaf66de12326ce164 Mon Sep 17 00:00:00 2001 From: crs Date: Sat, 6 Nov 2004 16:13:52 +0000 Subject: [PATCH] Fixed screensaver detection on XP. --- lib/arch/CArchMiscWindows.cpp | 25 +++++++++++++++++++++++++ lib/arch/CArchMiscWindows.h | 7 +++++++ lib/platform/CMSWindowsDesks.cpp | 28 +++++++++++++++++++++++++++- lib/platform/CMSWindowsDesks.h | 3 +++ lib/platform/CMSWindowsScreen.cpp | 14 ++++++++++---- lib/platform/CMSWindowsScreen.h | 1 + 6 files changed, 73 insertions(+), 5 deletions(-) diff --git a/lib/arch/CArchMiscWindows.cpp b/lib/arch/CArchMiscWindows.cpp index b4010040..f900f741 100644 --- a/lib/arch/CArchMiscWindows.cpp +++ b/lib/arch/CArchMiscWindows.cpp @@ -62,6 +62,31 @@ CArchMiscWindows::isWindows95Family() return result; } +bool +CArchMiscWindows::isWindowsModern() +{ + static bool init = false; + static bool result = false; + + if (!init) { + OSVERSIONINFO version; + version.dwOSVersionInfoSize = sizeof(version); + if (GetVersionEx(&version) == 0) { + // cannot determine OS; assume not modern + result = false; + } + else { + result = ((version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && + version.dwMajorVersion == 4 && + version.dwMinorVersion > 0) || + (version.dwPlatformId == VER_PLATFORM_WIN32_NT && + version.dwMajorVersion > 4)); + } + init = true; + } + return result; +} + int CArchMiscWindows::runDaemon(RunFunc runFunc) { diff --git a/lib/arch/CArchMiscWindows.h b/lib/arch/CArchMiscWindows.h index e55b6dd2..973f9b4e 100644 --- a/lib/arch/CArchMiscWindows.h +++ b/lib/arch/CArchMiscWindows.h @@ -49,6 +49,13 @@ public: */ static bool isWindows95Family(); + //! Test if windows 95, et al. + /*! + Returns true iff the platform is win98 or win2k or higher (i.e. + not windows 95 or windows NT). + */ + static bool isWindowsModern(); + //! Run the daemon /*! Delegates to CArchDaemonWindows. diff --git a/lib/platform/CMSWindowsDesks.cpp b/lib/platform/CMSWindowsDesks.cpp index 0a95df8d..ec56d2c1 100644 --- a/lib/platform/CMSWindowsDesks.cpp +++ b/lib/platform/CMSWindowsDesks.cpp @@ -33,6 +33,9 @@ #if !defined(SPI_SETMOUSESPEED) #define SPI_SETMOUSESPEED 113 #endif +#if !defined(SPI_GETSCREENSAVERRUNNING) +#define SPI_GETSCREENSAVERRUNNING 114 +#endif // X button stuff #if !defined(WM_XBUTTONDOWN) @@ -84,6 +87,7 @@ CMSWindowsDesks::CMSWindowsDesks( const IScreenSaver* screensaver, IJob* updateKeys) : m_isPrimary(isPrimary), m_is95Family(CArchMiscWindows::isWindows95Family()), + m_isModernFamily(CArchMiscWindows::isWindowsModern()), m_isOnScreen(m_isPrimary), m_x(0), m_y(0), m_w(0), m_h(0), @@ -622,8 +626,22 @@ CMSWindowsDesks::deskLeave(CDesk* desk, HKL keyLayout) strcmp(className, "ConsoleWindowClass") == 0) { EnableWindow(desk->m_window, TRUE); SetActiveWindow(desk->m_window); + + // force our window to the foreground. we can't + // simply call SetForegroundWindow() because that + // will only alert the user that the window wants + // to be the foreground as of windows 98/2000. we + // have to attach to the thread of the current + // foreground window then call it on our window + // and finally detach the threads. + DWORD thisThread = + GetWindowThreadProcessId(desk->m_window, NULL); + DWORD thatThread = + GetWindowThreadProcessId(foreground, NULL); + AttachThreadInput(thatThread, thisThread, TRUE); + SetForegroundWindow(desk->m_window); + AttachThreadInput(thatThread, thisThread, FALSE); } - LOG((CLOG_DEBUG1 "active window class: %s", className)); } } @@ -901,6 +919,14 @@ void CMSWindowsDesks::handleCheckDesk(const CEvent&, void*) { checkDesk(); + + // also check if screen saver is running if on a modern OS and + // this is the primary screen. + if (m_isPrimary && m_isModernFamily) { + BOOL running; + SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &running, FALSE); + PostThreadMessage(m_threadID, SYNERGY_MSG_SCREEN_SAVER, running, 0); + } } HDESK diff --git a/lib/platform/CMSWindowsDesks.h b/lib/platform/CMSWindowsDesks.h index df209eca..caa1bb7d 100644 --- a/lib/platform/CMSWindowsDesks.h +++ b/lib/platform/CMSWindowsDesks.h @@ -214,6 +214,9 @@ private: // true if windows 95/98/me bool m_is95Family; + // true if windows 98/2k or higher (i.e. not 95/nt) + bool m_isModernFamily; + // true if mouse has entered the screen bool m_isOnScreen; diff --git a/lib/platform/CMSWindowsScreen.cpp b/lib/platform/CMSWindowsScreen.cpp index 720b9669..4951dad3 100644 --- a/lib/platform/CMSWindowsScreen.cpp +++ b/lib/platform/CMSWindowsScreen.cpp @@ -94,6 +94,7 @@ CMSWindowsScreen::CMSWindowsScreen(bool isPrimary, m_fixTimer(NULL), m_screensaver(NULL), m_screensaverNotify(false), + m_screensaverActive(false), m_window(NULL), m_nextClipboardWindow(NULL), m_ownClipboard(false), @@ -1133,7 +1134,9 @@ CMSWindowsScreen::onScreensaver(bool activated) } if (activated) { - if (m_screensaver->checkStarted(SYNERGY_MSG_SCREEN_SAVER, FALSE, 0)) { + if (!m_screensaverActive && + m_screensaver->checkStarted(SYNERGY_MSG_SCREEN_SAVER, FALSE, 0)) { + m_screensaverActive = true; sendEvent(getScreensaverActivatedEvent()); // enable display power down @@ -1141,10 +1144,13 @@ CMSWindowsScreen::onScreensaver(bool activated) } } else { - sendEvent(getScreensaverDeactivatedEvent()); + if (m_screensaverActive) { + m_screensaverActive = false; + sendEvent(getScreensaverDeactivatedEvent()); - // disable display power down - CArchMiscWindows::addBusyState(CArchMiscWindows::kDISPLAY); + // disable display power down + CArchMiscWindows::addBusyState(CArchMiscWindows::kDISPLAY); + } } return true; diff --git a/lib/platform/CMSWindowsScreen.h b/lib/platform/CMSWindowsScreen.h index 6fcb691a..2b8b7366 100644 --- a/lib/platform/CMSWindowsScreen.h +++ b/lib/platform/CMSWindowsScreen.h @@ -238,6 +238,7 @@ private: // screen saver stuff CMSWindowsScreenSaver* m_screensaver; bool m_screensaverNotify; + bool m_screensaverActive; // clipboard stuff. our window is used mainly as a clipboard // owner and as a link in the clipboard viewer chain.