Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synergy 1088 clients ignore prevent sleeping option #7060

Merged
merged 7 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Bug fixes:
- #7057 | #7058 Fix copy paste logic, when OS reported about clipboard update too late
- #7055 Add secure input notification on Linux
- #7052 Add secure input notification on Windows
- #7047 Fix prevent sleep option on Mac and Windows
- #7047 | #7060 Fix prevent sleep option on Mac and Windows

Enhancements:
- #6998 Remove functionality related to the screen saver synchronisation
Expand Down
37 changes: 37 additions & 0 deletions src/lib/platform/MSWindowsPowerManager.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}
38 changes: 38 additions & 0 deletions src/lib/platform/MSWindowsPowerManager.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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
16 changes: 5 additions & 11 deletions src/lib/platform/MSWindowsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -2086,4 +2080,4 @@ MSWindowsScreen::updateScrollDirection()
});
scrollDirectionUpdateThread.detach();
}
}
}
2 changes: 2 additions & 0 deletions src/lib/platform/MSWindowsScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -374,4 +375,5 @@ class MSWindowsScreen : public PlatformScreen {
// -1 for natural scrolling direction, 1 otherwise
SInt32 m_scrollDirectionMouse = 1;
SInt32 m_scrollDirectionTouchpad = 1;
MSWindowsPowerManager m_powerManager;
};
50 changes: 50 additions & 0 deletions src/lib/platform/OSXPowerManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

#include "OSXPowerManager.h"
#include "base/Log.h"

OSXPowerManager::OSXPowerManager()
{
}

OSXPowerManager::~OSXPowerManager()
{
enableSleep();
}

void OSXPowerManager::disableSleep()
{
if (!m_sleepPreventionAssertionID) {
CFStringRef reasonForActivity = CFSTR("Synergy application");
IOReturn result = IOPMAssertionCreateWithName(kIOPMAssertPreventUserIdleDisplaySleep,
kIOPMAssertionLevelOn, reasonForActivity,
&m_sleepPreventionAssertionID);
if (result != kIOReturnSuccess) {
m_sleepPreventionAssertionID = 0;
LOG((CLOG_ERR "failed to disable system idle sleep"));
}
}
}

void OSXPowerManager::enableSleep()
{
if (m_sleepPreventionAssertionID) {
IOPMAssertionRelease(m_sleepPreventionAssertionID);
}
}
48 changes: 48 additions & 0 deletions src/lib/platform/OSXPowerManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

#ifndef OSXPOWERMANAGER_H
#define OSXPOWERMANAGER_H

#include <IOKit/pwr_mgt/IOPMLib.h>

class OSXPowerManager
{
public:
OSXPowerManager();
~OSXPowerManager();

/**
* @brief Prevents the system from sleep automatically
*/
void disableSleep();

/**
* @brief Enable automatically sleeping
*/
void enableSleep();

OSXPowerManager(const OSXPowerManager&) = delete;
OSXPowerManager& operator=(const OSXPowerManager&) = delete;

private:
// handler for assertion preventing the system from going to sleep
IOPMAssertionID m_sleepPreventionAssertionID = 0;
};

#endif // OSXPOWERMANAGER_H
5 changes: 2 additions & 3 deletions src/lib/platform/OSXScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include "platform/OSXClipboard.h"
#include "platform/OSXPowerManager.h"
#include "synergy/PlatformScreen.h"
#include "synergy/DragInformation.h"
#include "base/EventTypes.h"
Expand All @@ -30,7 +31,6 @@
#include <mach/mach_port.h>
#include <mach/mach_interface.h>
#include <mach/mach_init.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>

extern "C" {
Expand Down Expand Up @@ -347,8 +347,7 @@ class OSXScreen : public PlatformScreen {
CondVar<bool>* m_carbonLoopReady;
#endif

// handler for assertion preventing the system from going to sleep
IOPMAssertionID m_sleepPreventionAssertionID = 0;
OSXPowerManager m_powerManager;

class OSXScreenImpl* m_impl;
};
25 changes: 7 additions & 18 deletions src/lib/platform/OSXScreen.mm
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@
try {
m_screensaver = new OSXScreenSaver(m_events, getEventTarget());
m_keyState = new OSXKeyState(m_events);


if (App::instance().argsBase().m_preventSleep) {
m_powerManager.disableSleep();
}

// only needed when running as a server.
if (m_isPrimary) {

Expand Down Expand Up @@ -175,7 +179,7 @@
if (m_switchEventHandlerRef != 0) {
RemoveEventHandler(m_switchEventHandlerRef);
}

CGDisplayRemoveReconfigurationCallback(displayReconfigurationCallback, this);

delete m_keyState;
Expand All @@ -195,6 +199,7 @@
OSXScreen::~OSXScreen()
{
disable();

m_events->adoptBuffer(NULL);
m_events->removeHandler(Event::kSystem, m_events->getSystemTarget());

Expand Down Expand Up @@ -754,18 +759,6 @@
void
OSXScreen::enable()
{
if(App::instance().argsBase().m_preventSleep) {
CFStringRef reasonForActivity = CFSTR("Synergy application");

IOReturn result = IOPMAssertionCreateWithName(kIOPMAssertPreventUserIdleDisplaySleep,
kIOPMAssertionLevelOn, reasonForActivity,
&m_sleepPreventionAssertionID);
if(result != kIOReturnSuccess) {
m_sleepPreventionAssertionID = 0;
LOG((CLOG_ERR "failed to disable system idle sleep"));
}
}

// watch the clipboard
m_clipboardTimer = m_events->newTimer(1.0, NULL);
m_events->adoptHandler(Event::kTimer, m_clipboardTimer,
Expand Down Expand Up @@ -815,10 +808,6 @@
void
OSXScreen::disable()
{
if(App::instance().argsBase().m_preventSleep && m_sleepPreventionAssertionID) {
IOPMAssertionRelease(m_sleepPreventionAssertionID);
}

if (m_autoShowHideCursor) {
showCursor();
}
Expand Down
60 changes: 60 additions & 0 deletions src/lib/platform/XWindowsPowerManager.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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() const
{
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() const
{
if (!sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kScreenSaver) &&
!sleepInhibitCall(false, ArchSystemUnix::InhibitScreenServices::kSessionManager))
{
LOG((CLOG_INFO "Failed to enable system idle sleep"));
}
}