From 8c04eb45d23f2e8eac896050756df2656005a61d Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Fri, 20 May 2022 10:20:41 +0300 Subject: [PATCH] - more work to detect idle time in KDE - not tested yet --- app/idle_tracking.cpp | 90 ++++++++++++++++++++++++++++++++++++++++--- app/qbreak.pro | 8 +++- 2 files changed, 91 insertions(+), 7 deletions(-) diff --git a/app/idle_tracking.cpp b/app/idle_tracking.cpp index 5503e25..2746f62 100644 --- a/app/idle_tracking.cpp +++ b/app/idle_tracking.cpp @@ -115,15 +115,95 @@ int get_idle_time_gnome() } #if defined(USE_WAYLAND) +#include + +class kde_idle_detector +{ +private: + wayland::seat_t seat; + wayland::display_t d; + wayland::org_kde_kwin_idle_t idle; + wayland::org_kde_kwin_idle_timeout_t idle_timer; + + uint64_t idle_start = 0; + uint64_t idle_finish = 0; + bool active = false; + +public: + kde_idle_detector() + {} + ~kde_idle_detector() + {} + + // Idle timeout is in msec + void start(int idle_timeout) + { + if (active) + return; + + auto registry = d.get_registry(); + registry.on_global() = [&] (uint32_t name, const std::string& interface, uint32_t version) + { + if (interface == wayland::seat_t::interface_name) + registry.bind(name, seat, version); + if (interface == wayland::org_kde_kwin_idle_t::interface_name) + registry.bind(name, idle, version); + }; + d.roundtrip(); + + bool has_keyboard = false, has_pointer = false; + seat.on_capabilities() = [&] (const wayland::seat_capability& capability) + { + has_keyboard = capability & wayland::seat_capability::keyboard; + has_pointer = capability & wayland::seat_capability::pointer; + }; + d.roundtrip(); + + idle_timer = idle.get_idle_timeout(seat, idle_timeout); + idle_timer.on_idle() = [&]() + { + idle_start = ::time(nullptr); + }; + + idle_timer.on_resumed() = [&]() + { + idle_finish = ::time(nullptr); + }; + + active = true; + } + + void stop() + { + if (!active) + return; + + active = false; + idle_timer.release(); + seat.release(); + } + + // Return idle time in microseconds + int get_idle_time() const + { + if (idle_start > idle_finish) + { + return (::time(nullptr) - idle_start) * 1000; + } + + return 0; + } +}; + +kde_idle_detector kde_idle; int get_idle_time_kde_wayland() { - // Some ideas: - // https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/29 - // https://github.com/NilsBrause/waylandpp - // For KDE: use kde idle protocol https://wayland.app/protocols/kde-idle + // Ensure idle detector runs + kde_idle.start(1000); - return 0; + return kde_idle.get_idle_time(); } + #endif int get_idle_time_dynamically() diff --git a/app/qbreak.pro b/app/qbreak.pro index 3ba0add..65432a1 100644 --- a/app/qbreak.pro +++ b/app/qbreak.pro @@ -41,13 +41,17 @@ RESOURCES = qbreak.qrc TRANSLATIONS = strings.ts strings_en.ts strings_ru.ts -unix:!macx: DEFINES += TARGET_LINUX USE_WAYLAND +unix:!macx: DEFINES += TARGET_LINUX # USE_WAYLAND # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target -unix:LIBS += -L/usr/X11R6/lib/ -lX11 -lXext -lXss -ldl +# X11 and wayland libraries +unix:LIBS += -L/usr/X11R6/lib/ \ + -L/usr/local/lib \ + -lX11 -lXext -lXss -ldl \ + -lwayland-client-unstable++ -lwayland-client-extra++ -lwayland-client++