Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/CMakeLists.txt b/CMakeLists.txt
- index 4fa42b61..00c2ecc1 100644
- --- a/CMakeLists.txt
- +++ b/CMakeLists.txt
- @@ -199,6 +199,10 @@ if(UNIX AND NOT (APPLE OR HAIKU))
- if(USE_X11)
- add_definitions( -DHAVE_X11 )
- message(STATUS "X11 features support - ENABLED")
- + if(USE_XSS)
- + add_definitions( -DHAVE_XSS )
- + message(STATUS "Xscreensaver support - ENABLED")
- + endif()
- elseif(NOT LIMIT_X11_USAGE)
- set(USE_XSS OFF)
- endif()
- @@ -211,10 +215,6 @@ if(UNIX AND NOT (APPLE OR HAIKU))
- -DAPP_PREFIX=${CMAKE_INSTALL_PREFIX}
- -DAPP_BIN_NAME=${PROJECT_NAME}
- )
- - if(USE_XSS)
- - add_definitions( -DHAVE_XSS )
- - message(STATUS "Xscreensaver support - ENABLED")
- - endif()
- if(USE_DBUS)
- message(STATUS "DBus support - ENABLED")
- endif()
- diff --git a/src/libpsi/tools/idle/idle_x11.cpp b/src/libpsi/tools/idle/idle_x11.cpp
- index b2f7b461..5f066f6c 100644
- --- a/src/libpsi/tools/idle/idle_x11.cpp
- +++ b/src/libpsi/tools/idle/idle_x11.cpp
- @@ -19,14 +19,25 @@
- #include "idle.h"
- -#if !defined(HAVE_XSS) && !defined(USE_DBUS)
- +#ifdef HAVE_XSS
- +#include <QApplication>
- +#include <QDesktopWidget>
- +#include <QX11Info>
- +#include <X11/Xlib.h>
- +#include <X11/Xutil.h>
- +#include <X11/extensions/scrnsaver.h>
- -IdlePlatform::IdlePlatform() { d = nullptr; }
- -IdlePlatform::~IdlePlatform() { }
- -bool IdlePlatform::init() { return false; }
- -int IdlePlatform::secondsIdle() { return 0; }
- +static XErrorHandler old_handler = 0;
- +extern "C" int xerrhandler(Display *dpy, XErrorEvent *err)
- +{
- + if (err->error_code == BadDrawable)
- + return 0;
- -#elif defined(USE_DBUS) && !defined(HAVE_X11) && !defined(LIMIT_X11_USAGE)
- + return (*old_handler)(dpy, err);
- +}
- +#endif // HAVE_XSS
- +
- +#ifdef USE_DBUS
- #include <QDBusConnection>
- #include <QDBusConnectionInterface>
- @@ -44,92 +55,58 @@ static const QLatin1String GNOME_SS_PATH("/org/gnome/Mutter/IdleMonitor/Core");
- static const QLatin1String GNOME_SS_F("GetIdletime");
- static const QLatin1String COMMON_SS_F("GetSessionIdleTime");
- -class IdlePlatform::Private {
- -public:
- - Private() { }
- - QString getServicesAvailable() const
- - {
- - const auto services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
- - const QStringList idleServices = { COMMON_SS_SERV, KDE_SS_SERV, GNOME_SS_SERV };
- - // find first available dbus-service
- - for (const auto &service : idleServices) {
- - if (services.contains(service)) {
- - return service;
- - }
- - }
- - return QString();
- - }
- - int sendDBusCall() const
- - {
- - const auto serviceName = getServicesAvailable();
- - if (!serviceName.isEmpty()) {
- - // KDE and freedesktop uses the same path interface and method but gnome uses other
- - bool isNotGnome = serviceName == COMMON_SS_SERV || serviceName == KDE_SS_SERV;
- - const QLatin1String iface = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
- - const QLatin1String path = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
- - const QLatin1String method = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
- - auto interface = QDBusInterface(serviceName, path, iface);
- - if (interface.isValid()) {
- - QDBusReply<uint> reply = interface.call(method);
- - // probably reply value for freedesktop and kde need to be converted to seconds
- - if (reply.isValid())
- - return isNotGnome ? reply.value() / 1000 : reply.value();
- - }
- - }
- - return -1;
- - }
- -};
- -
- -IdlePlatform::IdlePlatform() { d = new Private; }
- -IdlePlatform::~IdlePlatform() { delete d; }
- -bool IdlePlatform::init() { return d->sendDBusCall() >= 0; }
- -
- -int IdlePlatform::secondsIdle()
- -{
- - const int result = d->sendDBusCall();
- - return (result > 0) ? result : 0;
- -}
- -
- -#else
- -
- -#include <QApplication>
- -#include <QDesktopWidget>
- -#include <QX11Info>
- -#include <X11/Xlib.h>
- -#include <X11/Xutil.h>
- -#include <X11/extensions/scrnsaver.h>
- -
- -static XErrorHandler old_handler = 0;
- -extern "C" int xerrhandler(Display *dpy, XErrorEvent *err)
- -{
- - if (err->error_code == BadDrawable)
- - return 0;
- -
- - return (*old_handler)(dpy, err);
- -}
- +#endif // USE_DBUS
- class IdlePlatform::Private {
- public:
- Private() { }
- +#ifdef USE_DBUS
- + QString dbusService = QString();
- +#endif // USE_DBUS
- +#ifdef HAVE_XSS
- XScreenSaverInfo *ss_info = nullptr;
- +#endif // HAVE_XSS
- };
- -IdlePlatform::IdlePlatform() { d = new Private; }
- +IdlePlatform::IdlePlatform()
- +{
- +#if defined(HAVE_XSS) || defined(USE_DBUS)
- + d = new Private;
- +#else
- + d = nullptr;
- +#endif
- +}
- IdlePlatform::~IdlePlatform()
- {
- +#ifdef HAVE_XSS
- if (d->ss_info)
- XFree(d->ss_info);
- if (old_handler) {
- XSetErrorHandler(old_handler);
- old_handler = 0;
- }
- - delete d;
- +#endif // HAVE_XSS
- + if (d)
- + delete d;
- }
- bool IdlePlatform::init()
- {
- +#ifdef USE_DBUS
- + // if DBUS idle is available using it else try to use XSS functions
- + const auto services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
- + const QStringList idleServices = { COMMON_SS_SERV, KDE_SS_SERV, GNOME_SS_SERV };
- + // find first available dbus-service
- + for (const auto &service : idleServices) {
- + if (services.contains(service)) {
- + d->dbusService = service;
- + return true;
- + }
- + }
- +#endif // USE_DBUS
- +#ifdef HAVE_XSS
- if (!QX11Info::isPlatformX11())
- return false;
- @@ -139,24 +116,41 @@ bool IdlePlatform::init()
- old_handler = XSetErrorHandler(xerrhandler);
- int event_base, error_base;
- -#if defined(HAVE_XSS) && !defined(LIMIT_X11_USAGE)
- +#ifndef LIMIT_X11_USAGE
- if (XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) {
- d->ss_info = XScreenSaverAllocInfo();
- return true;
- }
- -#endif
- +#endif // LIMIT_X11_USAGE
- +#endif // HAVE_XSS
- return false;
- }
- int IdlePlatform::secondsIdle()
- {
- +#ifdef USE_DBUS
- + if (!d->dbusService.isEmpty()) {
- + // KDE and freedesktop uses the same path interface and method but gnome uses other
- + bool isNotGnome = d->dbusService == COMMON_SS_SERV || d->dbusService == KDE_SS_SERV;
- + const QLatin1String iface = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
- + const QLatin1String path = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
- + const QLatin1String method = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
- + auto interface = QDBusInterface(d->dbusService, path, iface);
- + if (interface.isValid()) {
- + QDBusReply<uint> reply = interface.call(method);
- + // probably reply value for freedesktop and kde need to be converted to seconds
- + if (reply.isValid())
- + return isNotGnome ? reply.value() / 1000 : reply.value();
- + }
- + }
- +#endif // USE_DBUS
- +#ifdef HAVE_XSS
- if (!d->ss_info)
- return 0;
- -#if defined(HAVE_XSS)
- +
- if (!XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), d->ss_info))
- return 0;
- -#endif
- return d->ss_info->idle / 1000;
- +#endif // HAVE_XSS
- + return 0;
- }
- -
- -#endif // ! ( defined(USE_DBUS) && !defined(HAVE_X11) && !defined(LIMIT_X11_USAGE) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement