2023-11-27 09:06:43 +01:00
|
|
|
#ifndef OSEVENTHANDLER_H
|
|
|
|
#define OSEVENTHANDLER_H
|
|
|
|
#include <QObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
|
|
|
#include <events/EventEnum.h>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <QAbstractNativeEventFilter>
|
|
|
|
#include <QAbstractEventDispatcher>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <windows.h>
|
2024-01-03 19:43:46 +01:00
|
|
|
#include <powrprof.h>
|
2023-11-27 09:06:43 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <utils/settings.h>
|
|
|
|
|
|
|
|
class Logger;
|
|
|
|
|
|
|
|
class OsEventHandlerBase : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2024-01-03 19:43:46 +01:00
|
|
|
|
2023-11-27 09:06:43 +01:00
|
|
|
OsEventHandlerBase();
|
2024-01-03 19:43:46 +01:00
|
|
|
virtual ~OsEventHandlerBase();
|
2023-11-27 09:06:43 +01:00
|
|
|
|
|
|
|
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; }
|
|
|
|
virtual void unregisterLockHandler() {}
|
|
|
|
|
|
|
|
bool _isSuspendEnabled;
|
|
|
|
bool _isLockEnabled;
|
|
|
|
bool _isSuspendOnLock;
|
|
|
|
|
|
|
|
bool _isSuspendRegistered;
|
|
|
|
bool _isLockRegistered;
|
|
|
|
|
2024-01-03 19:43:46 +01:00
|
|
|
bool _isService;
|
|
|
|
|
|
|
|
Logger* _log{};
|
2023-11-27 09:06:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
class OsEventHandlerWindows : public OsEventHandlerBase, public QAbstractNativeEventFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OsEventHandlerWindows();
|
2024-01-03 19:43:46 +01:00
|
|
|
~OsEventHandlerWindows();
|
|
|
|
|
|
|
|
void handleSuspendResumeEvent(bool sleep);
|
2023-11-27 09:06:43 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
bool nativeEventFilter(const QByteArray& eventType, void* message, qintptr* result) override;
|
|
|
|
#else
|
|
|
|
bool nativeEventFilter(const QByteArray& eventType, void* message, long int* result) override;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool registerOsEventHandler() override;
|
|
|
|
void unregisterOsEventHandler() override;
|
|
|
|
bool registerLockHandler() override;
|
|
|
|
void unregisterLockHandler() override;
|
|
|
|
|
2024-01-03 19:43:46 +01:00
|
|
|
private:
|
|
|
|
static OsEventHandlerWindows* getInstance();
|
|
|
|
|
|
|
|
static DEVICE_NOTIFY_CALLBACK_ROUTINE handlePowerNotifications;
|
|
|
|
|
|
|
|
QWidget* _widget;
|
2023-11-27 09:06:43 +01:00
|
|
|
HPOWERNOTIFY _notifyHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
using OsEventHandler = OsEventHandlerWindows;
|
|
|
|
|
|
|
|
#elif defined(__linux__)
|
|
|
|
class OsEventHandlerLinux : public OsEventHandlerBase
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2024-01-03 19:43:46 +01:00
|
|
|
static void static_signaleHandler(int signum)
|
2023-11-27 09:06:43 +01:00
|
|
|
{
|
|
|
|
OsEventHandlerLinux::getInstance()->handleSignal(signum);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
OsEventHandlerLinux();
|
|
|
|
|
2024-01-03 19:43:46 +01:00
|
|
|
void handleSignal(int signum);
|
2023-11-27 09:06:43 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
static OsEventHandlerLinux* getInstance();
|
|
|
|
|
|
|
|
#if defined(HYPERION_HAS_DBUS)
|
|
|
|
bool registerOsEventHandler() override;
|
|
|
|
void unregisterOsEventHandler() override;
|
|
|
|
bool registerLockHandler() override;
|
|
|
|
void unregisterLockHandler() override;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
using OsEventHandler = OsEventHandlerLinux;
|
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
class OsEventHandlerMacOS : public OsEventHandlerBase
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
OsEventHandlerMacOS();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool registerOsEventHandler() override;
|
|
|
|
void unregisterOsEventHandler() override;
|
|
|
|
bool registerLockHandler() override;
|
|
|
|
void unregisterLockHandler() override;
|
|
|
|
|
2024-01-03 19:43:46 +01:00
|
|
|
void* _sleepEventHandler;
|
|
|
|
void* _lockEventHandler;
|
2023-11-27 09:06:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using OsEventHandler = OsEventHandlerMacOS;
|
|
|
|
|
|
|
|
#else
|
|
|
|
using OsEventHandler = OsEventHandlerBase;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // OSEVENTHANDLER_H
|