KIdleTime

xscreensaverbasedpoller.cpp
1 /* This file is part of the KDE libraries
2  * SPDX-FileCopyrightText: 2009 Dario Freddi <drf at kde.org>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 
7 #include "xscreensaverbasedpoller.h"
8 
9 #include <config-kidletime.h>
10 
11 #include <QGuiApplication>
12 
13 // sourcing the moc file before the Xlib include,
14 // whose defines like "Bool" break Qt names
15 #include "moc_xscreensaverbasedpoller.cpp"
16 
17 #include <X11/Xlib.h>
18 #include <X11/extensions/scrnsaver.h>
19 
20 static Display *display()
21 {
22  return qGuiApp->nativeInterface<QNativeInterface::QX11Application>()->display();
23 }
24 
25 XScreensaverBasedPoller::XScreensaverBasedPoller(QObject *parent)
26  : KWindowBasedIdleTimePoller(parent)
27  , m_screenSaverIface(nullptr)
28 {
29 }
30 
31 XScreensaverBasedPoller::~XScreensaverBasedPoller()
32 {
33 }
34 
35 bool XScreensaverBasedPoller::additionalSetUp()
36 {
37  m_screenSaverIface = new OrgFreedesktopScreenSaverInterface(QLatin1String("org.freedesktop.ScreenSaver"),
38  QLatin1String("/ScreenSaver"),
40  this);
41 
42  connect(m_screenSaverIface, &OrgFreedesktopScreenSaverInterface::ActiveChanged, this, &XScreensaverBasedPoller::screensaverActivated);
43 
44  return true;
45 }
46 
47 void XScreensaverBasedPoller::screensaverActivated(bool activated)
48 {
49  // We care only if it has been disactivated
50 
51  if (!activated) {
52  m_screenSaverIface->SimulateUserActivity();
53  Q_EMIT resumingFromIdle();
54  }
55 }
56 
57 int XScreensaverBasedPoller::getIdleTime()
58 {
59  XScreenSaverInfo *mitInfo = nullptr;
60  mitInfo = XScreenSaverAllocInfo();
61  XScreenSaverQueryInfo(display(), DefaultRootWindow(display()), mitInfo);
62  int ret = mitInfo->idle;
63  XFree(mitInfo);
64  return ret;
65 }
66 
67 void XScreensaverBasedPoller::simulateUserActivity()
68 {
69  stopCatchingIdleEvents();
70  XResetScreenSaver(display());
71  XFlush(display());
72 }
QDBusConnection sessionBus()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 04:07:39 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.