diff --git a/CMakeLists.txt b/CMakeLists.txt index 163ffc2..b622530 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,13 +12,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "" FORCE) option(CHECK_FOR_UPDATES "Enable checking for new versions of Jellyfin Media Player" ON) option(OPENELEC "Make an OpenELEC build" OFF) +option(LINUX_X11POWER "Enable non D-Bus screensaver management" OFF) option(USE_STATIC_MPVQT "Build MpvQt from bundled submodule instead of using system library" ON) if (NOT CHECK_FOR_UPDATES) add_definitions(-DDISABLE_UPDATE_CHECK) endif() -if(UNIX AND (NOT APPLE)) +if((NOT LINUX_X11POWER) AND (UNIX AND (NOT APPLE))) set(LINUX_DBUS ON) endif() diff --git a/CMakeModules/LinuxConfiguration.cmake b/CMakeModules/LinuxConfiguration.cmake index 5a0ea09..22b5545 100644 --- a/CMakeModules/LinuxConfiguration.cmake +++ b/CMakeModules/LinuxConfiguration.cmake @@ -10,8 +10,13 @@ else() set(X11_Xrandr_LIB "") endif() -add_definitions(-DLINUX_DBUS=1) -Message(STATUS "Enabling D-Bus power management") +if(LINUX_X11POWER) + add_definitions(-DUSE_X11POWER) + Message(STATUS "Enabling X11/XDG screensaver management") +else() + add_definitions(-DLINUX_DBUS=1) + Message(STATUS "Enabling D-Bus power management") +endif() set(INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR}) set(INSTALL_RESOURCE_DIR ${CMAKE_INSTALL_DATADIR}/jellyfinmediaplayer) diff --git a/src/power/CMakeLists.txt b/src/power/CMakeLists.txt index 31f26c4..d40d9da 100644 --- a/src/power/CMakeLists.txt +++ b/src/power/CMakeLists.txt @@ -4,6 +4,10 @@ if(APPLE) add_sources(PowerComponentMac.cpp PowerComponentMac.h) endif(APPLE) +if(LINUX_X11POWER) + add_sources(PowerComponentX11.cpp PowerComponentX11.h) +endif(LINUX_X11POWER) + if(LINUX_DBUS) add_sources(PowerComponentDBus.cpp PowerComponentDBus.h) endif(LINUX_DBUS) diff --git a/src/power/PowerComponent.cpp b/src/power/PowerComponent.cpp index dbcc9f8..31c4aed 100644 --- a/src/power/PowerComponent.cpp +++ b/src/power/PowerComponent.cpp @@ -10,6 +10,8 @@ #include "PowerComponentMac.h" #elif defined(LINUX_DBUS) #include "PowerComponentDBus.h" +#elif defined(USE_X11POWER) +#include "PowerComponentX11.h" #elif defined(Q_OS_WIN32) #include "PowerComponentWin.h" #endif @@ -23,6 +25,9 @@ PowerComponent& PowerComponent::Get() #elif defined(LINUX_DBUS) static PowerComponentDBus instance; return instance; +#elif defined(USE_X11POWER) + static PowerComponentX11 instance; + return instance; #elif defined(Q_OS_WIN32) static PowerComponentWin instance; return instance; diff --git a/src/power/PowerComponentX11.cpp b/src/power/PowerComponentX11.cpp new file mode 100644 index 0000000..e26cddb --- /dev/null +++ b/src/power/PowerComponentX11.cpp @@ -0,0 +1,54 @@ +#include + +#include "PowerComponentX11.h" + +/////////////////////////////////////////////////////////////////////////////////////////////////// +PowerComponentX11::PowerComponentX11() : PowerComponent(0) +{ + m_timer = new QTimer(this); + connect(m_timer, &QTimer::timeout, this, &PowerComponentX11::onTimer); + m_timer->setInterval(15 * 1000); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +void PowerComponentX11::onTimer() +{ + if (!m_process && !m_broken) + { + m_process = new QProcess(this); + m_process->setProcessChannelMode(QProcess::ForwardedChannels); + connect(m_process, (void (QProcess::*)(int,QProcess::ExitStatus))&QProcess::finished, + this, &PowerComponentX11::onProcessFinished); + connect(m_process, (void (QProcess::*)(QProcess::ProcessError))&QProcess::errorOccurred, + this, &PowerComponentX11::onProcessError); + m_process->start("xdg-screensaver", {"reset"}); + } +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +void PowerComponentX11::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) +{ + if (m_process) + m_process->deleteLater(); + m_process = 0; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +void PowerComponentX11::onProcessError(QProcess::ProcessError error) +{ + qCritical() << "Disabling screensaver is not working. Make sure xdg-screensaver is installed."; + m_broken = true; + onProcessFinished(-1, QProcess::CrashExit); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +void PowerComponentX11::doDisableScreensaver() +{ + m_timer->start(); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// +void PowerComponentX11::doEnableScreensaver() +{ + m_timer->stop(); +} diff --git a/src/power/PowerComponentX11.h b/src/power/PowerComponentX11.h new file mode 100644 index 0000000..ff1dac9 --- /dev/null +++ b/src/power/PowerComponentX11.h @@ -0,0 +1,31 @@ +#ifndef POWERCOMPONENTX11_H +#define POWERCOMPONENTX11_H + +#include +#include + +#include "PowerComponent.h" + +class PowerComponentX11 : public PowerComponent +{ + Q_OBJECT + +public: + PowerComponentX11(); + +protected: + virtual void doDisableScreensaver(); + virtual void doEnableScreensaver(); + +private slots: + void onTimer(); + void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); + void onProcessError(QProcess::ProcessError error); + +private: + bool m_broken = false; + QTimer* m_timer = 0; + QProcess* m_process = 0; +}; + +#endif // POWERCOMPONENTX11_H