KIdleTime

widgetbasedpoller.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.0-only
5  */
6 
7 #include "widgetbasedpoller.h"
8 
9 #include <QTimer>
10 #include <QEvent>
11 #include <QWindow>
12 
13 WidgetBasedPoller::WidgetBasedPoller(QObject *parent)
14  : AbstractSystemPoller(parent)
15 {
16 }
17 
18 WidgetBasedPoller::~WidgetBasedPoller()
19 {
20 }
21 
22 bool WidgetBasedPoller::isAvailable()
23 {
24  return true;
25 }
26 
27 bool WidgetBasedPoller::setUpPoller()
28 {
29  m_pollTimer = new QTimer(this);
30 
31  //setup idle timer, with some smart polling
32  connect(m_pollTimer, &QTimer::timeout, this, &WidgetBasedPoller::poll);
33 
34  m_grabber = new QWindow();
35  m_grabber->setFlag(Qt::X11BypassWindowManagerHint);
36  m_grabber->setPosition(-1000, -1000);
37  m_grabber->installEventFilter(this);
38  m_grabber->setObjectName(QStringLiteral("KIdleGrabberWidget"));
39 
40  return additionalSetUp();
41 }
42 
43 void WidgetBasedPoller::unloadPoller()
44 {
45  m_pollTimer->deleteLater();
46  m_grabber->deleteLater();
47 }
48 
49 QList<int> WidgetBasedPoller::timeouts() const
50 {
51  return m_timeouts;
52 }
53 
54 void WidgetBasedPoller::addTimeout(int nextTimeout)
55 {
56  m_timeouts.append(nextTimeout);
57  poll();
58 }
59 
60 bool WidgetBasedPoller::eventFilter(QObject *object, QEvent *event)
61 {
62  if (object == m_grabber
63  && (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress)) {
64  detectedActivity();
65  return true;
66  } else if (object != m_grabber) {
67  // If it's not the grabber, fallback to default event filter
68  return false;
69  }
70 
71  // Otherwise, simply ignore it
72  return false;
73 
74 }
75 
76 void WidgetBasedPoller::waitForActivity()
77 {
78  m_grabber->show();
79  m_grabber->setMouseGrabEnabled(true);
80  m_grabber->setKeyboardGrabEnabled(true);
81 
82 }
83 
84 void WidgetBasedPoller::detectedActivity()
85 {
86  stopCatchingIdleEvents();
87  emit resumingFromIdle();
88 }
89 
90 void WidgetBasedPoller::releaseInputLock()
91 {
92  m_grabber->setMouseGrabEnabled(false);
93  m_grabber->setKeyboardGrabEnabled(false);
94  m_grabber->hide();
95 }
96 
97 int WidgetBasedPoller::poll()
98 {
99  int idle = getIdleTime();
100 
101  // Check if we reached a timeout..
102  for(int timeOut : qAsConst(m_timeouts)) {
103  if ( ( timeOut - idle < 300 && timeOut >= idle ) || ( idle - timeOut < 300 && idle > timeOut ) ) {
104  // Bingo!
105  emit timeoutReached( timeOut );
106  }
107  }
108 
109  // Let's check the timer now!
110  int mintime = 0;
111 
112  for (int i : qAsConst(m_timeouts)) {
113  if (i > idle && (i < mintime || mintime == 0)) {
114  mintime = i;
115  }
116  }
117 
118  //qDebug() << "mintime " << mintime << "idle " << idle;
119 
120  if (mintime != 0) {
121  m_pollTimer->start(mintime - idle);
122  } else {
123  m_pollTimer->stop();
124  }
125 
126  return idle;
127 }
128 
129 int WidgetBasedPoller::forcePollRequest()
130 {
131  return poll();
132 }
133 
134 void WidgetBasedPoller::removeTimeout(int timeout)
135 {
136  m_timeouts.removeOne(timeout);
137  poll();
138 }
139 
140 void WidgetBasedPoller::catchIdleEvent()
141 {
142  waitForActivity();
143 }
144 
145 void WidgetBasedPoller::stopCatchingIdleEvents()
146 {
147  releaseInputLock();
148 }
149 
QEvent::Type type() const const
virtual bool event(QEvent *e)
void timeout()
void append(const T &value)
void timeoutReached(int identifier)
Triggered when the system has been idle for x milliseconds, identified by the previously set timeout...
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void resumingFromIdle()
Triggered, if KIdleTime is catching resume events, when the system resumes from an idle state...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 31 2020 23:08:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.