- work to support Gnome & KDE native idle time protocols

This commit is contained in:
Dmytro Bogovych 2022-05-19 20:52:59 +03:00
parent a0de1f5944
commit fc2c6e2b99
2 changed files with 48 additions and 8 deletions

View File

@ -5,10 +5,16 @@
#if defined(TARGET_LINUX)
#include <QObject>
#include <QDBusConnection>
#include <QDBusReply>
#include <QDBusInterface>
// Thanks to https://stackoverflow.com/questions/222606/detecting-keyboard-mouse-activity-in-linux
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
@ -16,7 +22,11 @@
// This requires sudo apt install libxss-dev
#include <X11/extensions/scrnsaver.h> // This can require libxss-dev to be installed
#include <dlfcn.h>
// #include <qmetatype.h>
// #include <QDBusConnection>
/*
// 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<int> 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

View File

@ -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