diff --git a/src/lib/platform/OSXPowerManager.cpp b/src/lib/platform/OSXPowerManager.cpp
new file mode 100644
index 000000000..734b2a81d
--- /dev/null
+++ b/src/lib/platform/OSXPowerManager.cpp
@@ -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 .
+ */
+
+#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);
+ }
+}
diff --git a/src/lib/platform/OSXPowerManager.h b/src/lib/platform/OSXPowerManager.h
new file mode 100644
index 000000000..0babc1dc3
--- /dev/null
+++ b/src/lib/platform/OSXPowerManager.h
@@ -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 .
+ */
+
+#ifndef OSXPOWERMANAGER_H
+#define OSXPOWERMANAGER_H
+
+#include
+
+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
diff --git a/src/lib/platform/OSXScreen.h b/src/lib/platform/OSXScreen.h
index 62af60833..6756cfb85 100644
--- a/src/lib/platform/OSXScreen.h
+++ b/src/lib/platform/OSXScreen.h
@@ -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"
@@ -30,7 +31,6 @@
#include
#include
#include
-#include
#include
extern "C" {
@@ -347,8 +347,7 @@ private:
CondVar* m_carbonLoopReady;
#endif
- // handler for assertion preventing the system from going to sleep
- IOPMAssertionID m_sleepPreventionAssertionID = 0;
+ OSXPowerManager m_powerManager;
class OSXScreenImpl* m_impl;
};
diff --git a/src/lib/platform/OSXScreen.mm b/src/lib/platform/OSXScreen.mm
index 0bf2731bc..b08c6ee66 100644
--- a/src/lib/platform/OSXScreen.mm
+++ b/src/lib/platform/OSXScreen.mm
@@ -119,7 +119,11 @@ OSXScreen::OSXScreen(IEventQueue* events, bool isPrimary, bool autoShowHideCurso
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) {
@@ -175,7 +179,7 @@ OSXScreen::OSXScreen(IEventQueue* events, bool isPrimary, bool autoShowHideCurso
if (m_switchEventHandlerRef != 0) {
RemoveEventHandler(m_switchEventHandlerRef);
}
-
+
CGDisplayRemoveReconfigurationCallback(displayReconfigurationCallback, this);
delete m_keyState;
@@ -195,6 +199,7 @@ OSXScreen::OSXScreen(IEventQueue* events, bool isPrimary, bool autoShowHideCurso
OSXScreen::~OSXScreen()
{
disable();
+
m_events->adoptBuffer(NULL);
m_events->removeHandler(Event::kSystem, m_events->getSystemTarget());
@@ -754,18 +759,6 @@ OSXScreen::hideCursor()
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,
@@ -815,10 +808,6 @@ OSXScreen::enable()
void
OSXScreen::disable()
{
- if(App::instance().argsBase().m_preventSleep && m_sleepPreventionAssertionID) {
- IOPMAssertionRelease(m_sleepPreventionAssertionID);
- }
-
if (m_autoShowHideCursor) {
showCursor();
}