diff --git a/src/lib/platform/MSWindowsPowerManager.cpp b/src/lib/platform/MSWindowsPowerManager.cpp new file mode 100644 index 0000000000..4d0b24eab8 --- /dev/null +++ b/src/lib/platform/MSWindowsPowerManager.cpp @@ -0,0 +1,37 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2012-2021 Symless Ltd. + * Copyright (C) 2008 Volker Lanz (vl@fidra.de) + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "MSWindowsPowerManager.h" +#include "arch/win32/ArchMiscWindows.h" + +MSWindowsPowerManager::~MSWindowsPowerManager() +{ + enableSleep(); +} + +void MSWindowsPowerManager::disableSleep() +{ + ArchMiscWindows::addBusyState(ArchMiscWindows::kSYSTEM); + ArchMiscWindows::addBusyState(ArchMiscWindows::kDISPLAY); +} + +void MSWindowsPowerManager::enableSleep() +{ + // allow the system to enter power saving mode + ArchMiscWindows::removeBusyState(ArchMiscWindows::kSYSTEM); + ArchMiscWindows::removeBusyState(ArchMiscWindows::kDISPLAY); +} diff --git a/src/lib/platform/MSWindowsPowerManager.h b/src/lib/platform/MSWindowsPowerManager.h new file mode 100644 index 0000000000..42eaa9ede5 --- /dev/null +++ b/src/lib/platform/MSWindowsPowerManager.h @@ -0,0 +1,38 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2012-2021 Symless Ltd. + * Copyright (C) 2008 Volker Lanz (vl@fidra.de) + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef MSWINDOWSPOWERMANAGER_H +#define MSWINDOWSPOWERMANAGER_H + + +class MSWindowsPowerManager +{ +public: + ~MSWindowsPowerManager(); + + /** + * @brief Prevents the system from sleep automatically + */ + void disableSleep(); + + /** + * @brief Enable automatically sleeping + */ + void enableSleep(); +}; + +#endif // MSWINDOWSPOWERMANAGER_H diff --git a/src/lib/platform/MSWindowsScreen.cpp b/src/lib/platform/MSWindowsScreen.cpp index ac1ca8be30..f630c65d55 100644 --- a/src/lib/platform/MSWindowsScreen.cpp +++ b/src/lib/platform/MSWindowsScreen.cpp @@ -164,6 +164,10 @@ MSWindowsScreen::MSWindowsScreen( LOG((CLOG_ERR "failed to get desktop path, no drop target available, error=%d", GetLastError())); } + if (App::instance().argsBase().m_preventSleep) { + m_powerManager.disableSleep(); + } + OleInitialize(0); m_dropWindow = createDropWindow(m_class, "DropWindow"); m_dropTarget = new MSWindowsDropTarget(); @@ -248,11 +252,6 @@ MSWindowsScreen::enable() // watch jump zones m_hook.setMode(kHOOK_WATCH_JUMP_ZONE); } - - if (App::instance().argsBase().m_preventSleep) { - ArchMiscWindows::addBusyState(ArchMiscWindows::kSYSTEM); - ArchMiscWindows::addBusyState(ArchMiscWindows::kDISPLAY); - } } void @@ -268,11 +267,6 @@ MSWindowsScreen::disable() // enable special key sequences on win95 family enableSpecialKeys(true); } - else { - // allow the system to enter power saving mode - ArchMiscWindows::removeBusyState(ArchMiscWindows::kSYSTEM); - ArchMiscWindows::removeBusyState(ArchMiscWindows::kDISPLAY); - } // tell key state m_keyState->disable(); @@ -2086,4 +2080,4 @@ MSWindowsScreen::updateScrollDirection() }); scrollDirectionUpdateThread.detach(); } -} \ No newline at end of file +} diff --git a/src/lib/platform/MSWindowsScreen.h b/src/lib/platform/MSWindowsScreen.h index d72889cfa1..dbab412854 100644 --- a/src/lib/platform/MSWindowsScreen.h +++ b/src/lib/platform/MSWindowsScreen.h @@ -19,6 +19,7 @@ #pragma once #include "platform/MSWindowsHook.h" +#include "platform/MSWindowsPowerManager.h" #include "synergy/PlatformScreen.h" #include "synergy/DragInformation.h" #include "platform/synwinhk.h" @@ -374,4 +375,5 @@ private: // -1 for natural scrolling direction, 1 otherwise SInt32 m_scrollDirectionMouse = 1; SInt32 m_scrollDirectionTouchpad = 1; + MSWindowsPowerManager m_powerManager; }; diff --git a/src/lib/platform/XWindowsPowerManager.cpp b/src/lib/platform/XWindowsPowerManager.cpp new file mode 100644 index 0000000000..1e8c9eaab2 --- /dev/null +++ b/src/lib/platform/XWindowsPowerManager.cpp @@ -0,0 +1,60 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2012-2016 Symless Ltd. + * Copyright (C) 2002 Chris Schoeneman + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "XWindowsPowerManager.h" +#include "arch/Arch.h" +#include "base/Log.h" + +namespace{ + +bool sleepInhibitCall(bool state, ArchSystemUnix::InhibitScreenServices serviceID) +{ + std::string error; + + if (!ArchSystemUnix::DBusInhibitScreenCall(serviceID, state, error)) + { + LOG((CLOG_DEBUG "DBus inhibit error %s", error.c_str())); + return false; + } + + return true; +} + +} + +XWindowsPowerManager::~XWindowsPowerManager() +{ + enableSleep(); +} + +void XWindowsPowerManager::disableSleep() +{ + if (!sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kScreenSaver) && + !sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kSessionManager)) + { + LOG((CLOG_INFO "Failed to prevent system from going to sleep")); + } +} + +void XWindowsPowerManager::enableSleep() +{ + if (!sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kScreenSaver) && + !sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kSessionManager)) + { + LOG((CLOG_INFO "Failed to enable system idle sleep")); + } +} diff --git a/src/lib/platform/XWindowsPowerManager.h b/src/lib/platform/XWindowsPowerManager.h new file mode 100644 index 0000000000..43fe5a6f21 --- /dev/null +++ b/src/lib/platform/XWindowsPowerManager.h @@ -0,0 +1,30 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2012-2016 Symless Ltd. + * Copyright (C) 2002 Chris Schoeneman + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef XWINDOWSPOWERMANAGER_H +#define XWINDOWSPOWERMANAGER_H + + +class XWindowsPowerManager +{ +public: + ~XWindowsPowerManager(); + void disableSleep(); + void enableSleep(); +}; + +#endif // XWINDOWSPOWERMANAGER_H diff --git a/src/lib/platform/XWindowsScreen.cpp b/src/lib/platform/XWindowsScreen.cpp index bf2472d66a..a6d1f04e75 100644 --- a/src/lib/platform/XWindowsScreen.cpp +++ b/src/lib/platform/XWindowsScreen.cpp @@ -182,6 +182,11 @@ XWindowsScreen::XWindowsScreen( XTestGrabControl(m_display, True); } + // disable sleep if the flag is set + if (App::instance().argsBase().m_preventSleep) { + m_powerManager.disableSleep(); + } + // initialize the clipboards for (ClipboardID id = 0; id < kClipboardEnd; ++id) { m_clipboard[id] = new XWindowsClipboard(m_display, m_window, id); @@ -247,12 +252,6 @@ XWindowsScreen::enable() // warp the mouse to the cursor center fakeMouseMove(m_xCenter, m_yCenter); } - - // disable sleep if the flag is set - if (App::instance().argsBase().m_preventSleep && - !disableIdleSleep()) { - LOG((CLOG_INFO "Failed to prevent system from going to sleep")); - } } void @@ -271,12 +270,6 @@ XWindowsScreen::disable() if (!m_isPrimary && m_autoRepeat) { //XAutoRepeatOn(m_display); } - - // enable sleep when the display is disabled - if (App::instance().argsBase().m_preventSleep && - !enableIdleSleep()) { - LOG((CLOG_INFO "Failed to enable system idle sleep")); - } } void @@ -2201,25 +2194,3 @@ XWindowsScreen::updateScrollDirection() } } -bool XWindowsScreen::sleepInhibitCall(bool state, ArchSystemUnix::InhibitScreenServices serviceID) -{ - std::string error; - if(!ArchSystemUnix::DBusInhibitScreenCall(serviceID, state, error)) - { - LOG((CLOG_DEBUG "DBus inhibit error %s", error.c_str())); - return false; - } - return true; -} - -bool XWindowsScreen::disableIdleSleep() -{ - return sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kScreenSaver) || - sleepInhibitCall(true, ArchSystemUnix::InhibitScreenServices::kSessionManager); -} - -bool XWindowsScreen::enableIdleSleep() -{ - return sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kScreenSaver) || - sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kSessionManager); -} diff --git a/src/lib/platform/XWindowsScreen.h b/src/lib/platform/XWindowsScreen.h index 9e1c4e8c68..b674b708d7 100644 --- a/src/lib/platform/XWindowsScreen.h +++ b/src/lib/platform/XWindowsScreen.h @@ -23,6 +23,7 @@ #include "synergy/KeyMap.h" #include "common/stdset.h" #include "common/stdvector.h" +#include "platform/XWindowsPowerManager.h" #if X_DISPLAY_MISSING # error X11 is required to build synergy @@ -118,11 +119,6 @@ private: void onError(); static int ioErrorHandler(Display*); - // sleep management - static bool sleepInhibitCall(bool state, ArchSystemUnix::InhibitScreenServices serviceID); - static bool disableIdleSleep(); - static bool enableIdleSleep(); - private: class KeyEventFilter { public: @@ -263,4 +259,5 @@ private: // -1 for natural scrolling direction, 1 otherwise SInt32 m_scrollDirectionMouse = 1; SInt32 m_scrollDirectionTouchpad = 1; + XWindowsPowerManager m_powerManager; };