From fc2c6e2b99f757ee35082f3358ea1eb1965c80fa Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Thu, 19 May 2022 20:52:59 +0300 Subject: [PATCH] - work to support Gnome & KDE native idle time protocols --- app/idle_tracking.cpp | 52 ++++++++++++++++++++++++++++++++++++++----- app/qbreak.pro | 4 ++-- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/app/idle_tracking.cpp b/app/idle_tracking.cpp index ed8766c..3b766dc 100644 --- a/app/idle_tracking.cpp +++ b/app/idle_tracking.cpp @@ -5,10 +5,16 @@ #if defined(TARGET_LINUX) +#include +#include +#include +#include + // Thanks to https://stackoverflow.com/questions/222606/detecting-keyboard-mouse-activity-in-linux #include #include +#include #include #include #include @@ -16,7 +22,11 @@ // This requires sudo apt install libxss-dev #include // This can require libxss-dev to be installed #include +// #include +// #include +/* +// Prototype from stackoverflow int get_idle_time() { time_t idle_time; @@ -37,6 +47,7 @@ int get_idle_time() return idle_time; } +*/ int get_idle_time_x11() { @@ -88,6 +99,22 @@ int get_idle_time_x11() return idle_time; } +int get_idle_time_gnome() +{ + auto bus = QDBusConnection::sessionBus(); + if (!bus.isConnected()) + return 0; + + QDBusInterface interface( "org.gnome.Mutter.IdleMonitor", + "/org/gnome/Mutter/IdleMonitor/Core", + "org.gnome.Mutter.IdleMonitor"); + + QDBusReply reply = interface.call("GetIdletime"); + + return reply.isValid() ? reply.value() : 0; +} + +#if defined(USE_WAYLAND) int get_idle_time_wayland() { // Some ideas: @@ -101,19 +128,32 @@ int get_idle_time_wayland() return 0; } +#endif int get_idle_time_dynamically() { - // Check if we run under X11 - // const char* x11_display = std::getenv("DISPLAY"); - - // Check if we run under Wayland - // https://unix.stackexchange.com/questions/202891/how-to-know-whether-wayland-or-x11-is-being-used/371164#371164 +#if defined(USE_WAYLAND) const char* wl_display = std::getenv("WAYLAND_DISPLAY"); if (wl_display) - return get_idle_time_wayland(); + { + const char* desktop_name = std::getenv("XDG_DESKTOP_NAME"); + if (!desktop_name) + return 0; + + if (strcmp(desktop_name, "KDE") == 0) + return get_idle_time_wayland(); + else + if (strcmp(desktop_name, "GNOME") == 0) + return get_idle_time_gnome(); + else + return 0; + } else return get_idle_time_x11(); +#else + // Restrict to X11 + return get_idle_time_x11(); +#endif } #endif diff --git a/app/qbreak.pro b/app/qbreak.pro index c977645..3ba0add 100644 --- a/app/qbreak.pro +++ b/app/qbreak.pro @@ -1,6 +1,6 @@ QT += core gui svg multimedia -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets dbus CONFIG += c++17 lrelease embed_translations @@ -41,7 +41,7 @@ RESOURCES = qbreak.qrc TRANSLATIONS = strings.ts strings_en.ts strings_ru.ts -unix:!macx: DEFINES += TARGET_LINUX +unix:!macx: DEFINES += TARGET_LINUX USE_WAYLAND # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin