SYNERGY-1088 MacOS client ignores prevent sleeping option

This commit is contained in:
Serhii Hadzhilov 2021-07-23 14:48:29 +03:00
parent 2def465256
commit 8dd68abf56
4 changed files with 107 additions and 21 deletions

View file

@ -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);
}
}

View file

@ -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

View file

@ -19,6 +19,7 @@
#pragma once #pragma once
#include "platform/OSXClipboard.h" #include "platform/OSXClipboard.h"
#include "platform/OSXPowerManager.h"
#include "synergy/PlatformScreen.h" #include "synergy/PlatformScreen.h"
#include "synergy/DragInformation.h" #include "synergy/DragInformation.h"
#include "base/EventTypes.h" #include "base/EventTypes.h"
@ -30,7 +31,6 @@
#include <mach/mach_port.h> #include <mach/mach_port.h>
#include <mach/mach_interface.h> #include <mach/mach_interface.h>
#include <mach/mach_init.h> #include <mach/mach_init.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h> #include <IOKit/IOMessage.h>
extern "C" { extern "C" {
@ -347,8 +347,7 @@ private:
CondVar<bool>* m_carbonLoopReady; CondVar<bool>* m_carbonLoopReady;
#endif #endif
// handler for assertion preventing the system from going to sleep OSXPowerManager m_powerManager;
IOPMAssertionID m_sleepPreventionAssertionID = 0;
class OSXScreenImpl* m_impl; class OSXScreenImpl* m_impl;
}; };

View file

@ -120,6 +120,10 @@ OSXScreen::OSXScreen(IEventQueue* events, bool isPrimary, bool autoShowHideCurso
m_screensaver = new OSXScreenSaver(m_events, getEventTarget()); m_screensaver = new OSXScreenSaver(m_events, getEventTarget());
m_keyState = new OSXKeyState(m_events); m_keyState = new OSXKeyState(m_events);
if (App::instance().argsBase().m_preventSleep) {
m_powerManager.disableSleep();
}
// only needed when running as a server. // only needed when running as a server.
if (m_isPrimary) { if (m_isPrimary) {
@ -195,6 +199,7 @@ OSXScreen::OSXScreen(IEventQueue* events, bool isPrimary, bool autoShowHideCurso
OSXScreen::~OSXScreen() OSXScreen::~OSXScreen()
{ {
disable(); disable();
m_events->adoptBuffer(NULL); m_events->adoptBuffer(NULL);
m_events->removeHandler(Event::kSystem, m_events->getSystemTarget()); m_events->removeHandler(Event::kSystem, m_events->getSystemTarget());
@ -754,18 +759,6 @@ OSXScreen::hideCursor()
void void
OSXScreen::enable() 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 // watch the clipboard
m_clipboardTimer = m_events->newTimer(1.0, NULL); m_clipboardTimer = m_events->newTimer(1.0, NULL);
m_events->adoptHandler(Event::kTimer, m_clipboardTimer, m_events->adoptHandler(Event::kTimer, m_clipboardTimer,
@ -815,10 +808,6 @@ OSXScreen::enable()
void void
OSXScreen::disable() OSXScreen::disable()
{ {
if(App::instance().argsBase().m_preventSleep && m_sleepPreventionAssertionID) {
IOPMAssertionRelease(m_sleepPreventionAssertionID);
}
if (m_autoShowHideCursor) { if (m_autoShowHideCursor) {
showCursor(); showCursor();
} }