Skip to content

Commit

Permalink
MacOS lock/unlock added
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulchen-Panther committed Nov 14, 2023
1 parent 114282f commit a238ebf
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dependencies/external/mbedtls
Submodule mbedtls updated 1148 files
2 changes: 1 addition & 1 deletion include/api/JsonAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// parent class
#include <api/API.h>
#include <events/Event.h>
#include <events/EventEnum.h>

// hyperion includes
#include <utils/Components.h>
Expand Down
2 changes: 1 addition & 1 deletion include/cec/CECHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <libcec/cec.h>

#include <utils/settings.h>
#include <events/Event.h>
#include <events/EventEnum.h>

using CECCallbacks = CEC::ICECCallbacks;
using CECAdapter = CEC::ICECAdapter;
Expand Down
6 changes: 3 additions & 3 deletions include/events/Event.h → include/events/EventEnum.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef EVENT_H
#define EVENT_H
#ifndef EVENTENUM_H
#define EVENTENUM_H

#include <QString>

Expand Down Expand Up @@ -47,4 +47,4 @@ inline Event stringToEvent(const QString& event)
}


#endif // EVENT_H
#endif // EVENTENUM_H
10 changes: 3 additions & 7 deletions include/events/EventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
#define EVENTHANDLER_H

#include <utils/settings.h>
#include <events/Event.h>
#include <events/EventEnum.h>

#include <QObject>

class Logger;

class EventHandler : public QObject {
class EventHandler : public QObject
{
Q_OBJECT

public:

EventHandler();
~EventHandler() override;

static EventHandler* getInstance();

public slots:

virtual void handleSettingsUpdate(settings::type type, const QJsonDocument& config);

void suspend(bool sleep);
Expand All @@ -36,15 +35,12 @@ public slots:
void handleEvent(Event event);

signals:

void signalEvent(Event event);

protected:

Logger * _log {};

private:

bool _isSuspended;
bool _isIdle;
};
Expand Down
45 changes: 34 additions & 11 deletions include/events/OsEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <QObject>
#include <QJsonDocument>

#include <events/Event.h>
#include <events/EventEnum.h>

#if defined(_WIN32)
#include <QAbstractNativeEventFilter>
Expand All @@ -16,26 +16,24 @@

class Logger;

class OsEventHandlerBase : public QObject {
class OsEventHandlerBase : public QObject
{
Q_OBJECT

public:
OsEventHandlerBase();
~OsEventHandlerBase() override;

public slots:

void suspend(bool sleep);
void lock(bool isLocked);

virtual void handleSettingsUpdate(settings::type type, const QJsonDocument& config);

signals:

void signalEvent(Event event);

protected:

virtual bool registerOsEventHandler() { return true; }
virtual void unregisterOsEventHandler() {}
virtual bool registerLockHandler() { return true; }
Expand All @@ -49,14 +47,12 @@ public slots:
bool _isLockRegistered;

Logger * _log {};

private:

};

#if defined(_WIN32)

class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeEventFilter {
class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeEventFilter
{

public:
OsEventHandlerWindows();
Expand All @@ -70,7 +66,6 @@ class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeE
#endif

private:

bool registerOsEventHandler() override;
void unregisterOsEventHandler() override;
bool registerLockHandler() override;
Expand All @@ -83,7 +78,8 @@ class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeE
using OsEventHandler = OsEventHandlerWindows;

#elif defined(__linux__)
class OsEventHandlerLinux : public OsEventHandlerBase {
class OsEventHandlerLinux : public OsEventHandlerBase
{
Q_OBJECT

static void static_signaleHandler(int signum)
Expand All @@ -109,6 +105,33 @@ class OsEventHandlerLinux : public OsEventHandlerBase {

using OsEventHandler = OsEventHandlerLinux;

#elif defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>

class OsEventHandlerMacOS : public OsEventHandlerBase
{
Q_OBJECT

static void notificationCenterCallBack(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo)
{
OsEventHandlerMacOS::getInstance()->handleSignal(name);
}

public:
void handleSignal (CFStringRef lock_unlock);

private:
static OsEventHandlerMacOS* getInstance();

CFStringRef lockSignal = CFSTR("com.apple.screenIsLocked");
CFStringRef unlockSignal = CFSTR("com.apple.screenIsUnlocked");
bool registerLockHandler() override;
void unregisterLockHandler() override;

};

using OsEventHandler = OsEventHandlerMacOS;

#else
using OsEventHandler = OsEventHandlerBase;
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/grabber/V4L2Grabber.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// Determine the cmake options
#include <HyperionConfig.h>

#include <events/Event.h>
#include <events/EventEnum.h>

///
/// Capture class for V4L2 devices
Expand Down
2 changes: 1 addition & 1 deletion include/hyperion/Grabber.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <utils/Logger.h>
#include <utils/Components.h>

#include <events/Event.h>
#include <events/EventEnum.h>

///
/// @brief The Grabber class is responsible to apply image resizes (with or without ImageResampler)
Expand Down
2 changes: 1 addition & 1 deletion include/hyperion/GrabberWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <grabber/GrabberType.h>

#include <events/Event.h>
#include <events/EventEnum.h>

class Grabber;
class GlobalSignals;
Expand Down
2 changes: 1 addition & 1 deletion include/hyperion/HyperionIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <utils/VideoMode.h>
#include <utils/settings.h>
#include <utils/Components.h>
#include <events/Event.h>
#include <events/EventEnum.h>

// qt
#include <QMap>
Expand Down
2 changes: 1 addition & 1 deletion libsrc/api/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <grabber/QtGrabber.h>

#include <utils/WeakConnect.h>
#include <events/Event.h>
#include <events/EventEnum.h>

#if defined(ENABLE_MF)
#include <grabber/MFGrabber.h>
Expand Down
2 changes: 1 addition & 1 deletion libsrc/events/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/events)
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/events)

add_library(events
${CURRENT_HEADER_DIR}/Event.h
${CURRENT_HEADER_DIR}/EventEnum.h
${CURRENT_HEADER_DIR}/EventHandler.h
${CURRENT_SOURCE_DIR}/EventHandler.cpp
${CURRENT_HEADER_DIR}/OsEventHandler.h
Expand Down
2 changes: 0 additions & 2 deletions libsrc/events/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <QJsonDocument>
#include <QJsonObject>

#include <events/Event.h>

#include <utils/Logger.h>
#include <utils/Process.h>
#include <hyperion/HyperionIManager.h>
Expand Down
59 changes: 58 additions & 1 deletion libsrc/events/OsEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,61 @@ void OsEventHandlerLinux::unregisterLockHandler()
}
#endif // HYPERION_HAS_DBUS

#endif // __linux__
#elif defined(__APPLE__)

OsEventHandlerMacOS* OsEventHandlerMacOS::getInstance()
{
static OsEventHandlerMacOS instance;
return &instance;
}

void OsEventHandlerMacOS::handleSignal (CFStringRef lock_unlock)
{
if (CFEqual(lock_unlock, CFSTR("com.apple.screenIsLocked")))
{
lock(true);
}
else if (CFEqual(lock_unlock, CFSTR("com.apple.screenIsUnlocked")))
{
lock(false);
}
}

bool OsEventHandlerMacOS::registerLockHandler()
{
bool isRegistered{ _isLockRegistered };
if (!_isLockRegistered)
{
CFNotificationCenterRef distCenter;

distCenter = CFNotificationCenterGetDistributedCenter();
if (distCenter != nullptr)
{
CFNotificationCenterAddObserver(distCenter, this, &OsEventHandlerMacOS::notificationCenterCallBack, lockSignal, nullptr, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(distCenter, this, &OsEventHandlerMacOS::notificationCenterCallBack, unlockSignal, nullptr, CFNotificationSuspensionBehaviorDeliverImmediately);
isRegistered = true;
}
else
{
Error(_log, "Could not register for lock/unlock events!");
}

}

if (isRegistered)
{
_isLockRegistered = true;
}
return isRegistered;
}

void OsEventHandlerMacOS::unregisterLockHandler()
{
if (_isLockRegistered)
{
CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDistributedCenter(), this);
_isLockRegistered = false;
}
}

#endif

0 comments on commit a238ebf

Please sign in to comment.