• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KUtils

  • sources
  • kde-4.14
  • kdelibs
  • kutils
  • kidletime
xsyncbasedpoller.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2009 Dario Freddi <drf at kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "xsyncbasedpoller.h"
20 
21 #include <QX11Info>
22 
23 #include <klocalizedstring.h>
24 #include <kglobal.h>
25 
26 #include <fixx11h.h>
27 
28 class XSyncBasedPollerHelper
29 {
30 public:
31  XSyncBasedPollerHelper() : q(0) {}
32  ~XSyncBasedPollerHelper() {
33  delete q;
34  }
35  XSyncBasedPoller *q;
36 };
37 
38 K_GLOBAL_STATIC(XSyncBasedPollerHelper, s_globalXSyncBasedPoller)
39 
40 XSyncBasedPoller *XSyncBasedPoller::instance()
41 {
42  if (!s_globalXSyncBasedPoller->q) {
43  new XSyncBasedPoller;
44  }
45 
46  return s_globalXSyncBasedPoller->q;
47 }
48 
49 XSyncBasedPoller::XSyncBasedPoller(QWidget *parent)
50  : AbstractSystemPoller(parent)
51  , m_display(QX11Info::display())
52  , m_idleCounter(X::None)
53  , m_resetAlarm(X::None)
54  , m_available(true)
55 {
56  Q_ASSERT(!s_globalXSyncBasedPoller->q);
57  s_globalXSyncBasedPoller->q = this;
58 
59  int sync_major, sync_minor;
60  int ncounters;
61  XSyncSystemCounter *counters;
62 
63  if (!XSyncQueryExtension(m_display, &m_sync_event, &m_sync_error)) {
64  m_available = false;
65  return;
66  }
67 
68  if (!XSyncInitialize(m_display, &sync_major, &sync_minor)) {
69  m_available = false;
70  return;
71  }
72 
73  kDebug() << sync_major << sync_minor;
74 
75  counters = XSyncListSystemCounters(m_display, &ncounters);
76 
77  bool idleFound = false;
78 
79  for (int i = 0; i < ncounters; ++i) {
80  if (!strcmp(counters[i].name, "IDLETIME")) {
81  m_idleCounter = counters[i].counter;
82  idleFound = true;
83  break;
84  }
85  }
86 
87  XSyncFreeSystemCounterList(counters);
88 
89  if (!idleFound) {
90  m_available = false;
91  }
92 
93  if (m_available) {
94  kDebug() << "XSync seems available and ready";
95  } else {
96  kDebug() << "XSync seems not available";
97  }
98 }
99 
100 XSyncBasedPoller::~XSyncBasedPoller()
101 {
102 }
103 
104 bool XSyncBasedPoller::isAvailable()
105 {
106  return m_available;
107 }
108 
109 bool XSyncBasedPoller::setUpPoller()
110 {
111  if (!isAvailable()) {
112  return false;
113  }
114 
115  kDebug() << "XSync Inited";
116 
117  KApplication::kApplication()->installX11EventFilter(this);
118 
119  kDebug() << "Supported, init completed";
120 
121  return true;
122 }
123 
124 void XSyncBasedPoller::unloadPoller()
125 {
126 }
127 
128 void XSyncBasedPoller::addTimeout(int nextTimeout)
129 {
130  /* We need to set the counter to the idle time + the value
131  * requested for next timeout
132  */
133 
134  // If there's already an alarm for the requested timeout, skip
135  if (m_timeoutAlarm.contains(nextTimeout)) {
136  return;
137  }
138 
139  XSyncValue timeout;
140  XSyncAlarm newalarm = X::None;
141 
142  XSyncIntToValue(&timeout, nextTimeout);
143 
144  setAlarm(m_display, &newalarm, m_idleCounter,
145  XSyncPositiveComparison, timeout);
146 
147  m_timeoutAlarm.insert(nextTimeout, newalarm);
148 }
149 
150 int XSyncBasedPoller::forcePollRequest()
151 {
152  return poll();
153 }
154 
155 int XSyncBasedPoller::poll()
156 {
157  XSyncValue idleTime;
158  XSyncQueryCounter(m_display, m_idleCounter, &idleTime);
159 
160  return XSyncValueLow32(idleTime);
161 }
162 
163 void XSyncBasedPoller::removeTimeout(int timeout)
164 {
165  if (m_timeoutAlarm.contains(timeout)) {
166  XSyncAlarm a = m_timeoutAlarm[timeout];
167  XSyncDestroyAlarm(m_display, a);
168  m_timeoutAlarm.remove(timeout);
169  }
170 }
171 
172 QList<int> XSyncBasedPoller::timeouts() const
173 {
174  return m_timeoutAlarm.keys();
175 }
176 
177 void XSyncBasedPoller::stopCatchingIdleEvents()
178 {
179  if (m_resetAlarm != X::None) {
180  XSyncDestroyAlarm(m_display, m_resetAlarm);
181  m_resetAlarm = X::None;
182  }
183 }
184 
185 void XSyncBasedPoller::catchIdleEvent()
186 {
187  XSyncValue idleTime;
188 
189  XSyncQueryCounter(m_display, m_idleCounter, &idleTime);
190 
191  /* Set the reset alarm to fire the next time idleCounter < the
192  * current counter value. XSyncNegativeComparison means <= so
193  * we have to subtract 1 from the counter value
194  */
195 
196  //NOTE: this must be a int, else compilation might fail
197  int overflow;
198  XSyncValue add;
199  XSyncValue plusone;
200  XSyncIntToValue(&add, -1);
201  XSyncValueAdd(&plusone, idleTime, add, &overflow);
202  setAlarm(m_display, &m_resetAlarm, m_idleCounter,
203  XSyncNegativeComparison, plusone);
204 }
205 
206 void XSyncBasedPoller::reloadAlarms()
207 {
208  XSyncValue timeout;
209 
210  for (QHash<int, XSyncAlarm>::iterator i = m_timeoutAlarm.begin(); i != m_timeoutAlarm.end(); ++i) {
211  XSyncIntToValue(&timeout, i.key());
212 
213  setAlarm(m_display, &(i.value()), m_idleCounter,
214  XSyncPositiveComparison, timeout);
215  }
216 }
217 
218 bool XSyncBasedPoller::x11Event(XEvent *event)
219 {
220  XSyncAlarmNotifyEvent *alarmEvent;
221 
222  if (event->type != m_sync_event + XSyncAlarmNotify) {
223  return false;
224  }
225 
226  alarmEvent = (XSyncAlarmNotifyEvent *)event;
227 
228  if (alarmEvent->state == XSyncAlarmDestroyed) {
229  return false;
230  }
231 
232  for (QHash<int, XSyncAlarm>::const_iterator i = m_timeoutAlarm.constBegin(); i != m_timeoutAlarm.constEnd(); ++i) {
233  if (alarmEvent->alarm == i.value()) {
234  /* Bling! Caught! */
235  emit timeoutReached(i.key());
236  // Update the alarm to fire back if the system gets inactive for the same time
237  catchIdleEvent();
238  return false;
239  }
240  }
241 
242  if (alarmEvent->alarm == m_resetAlarm) {
243  /* Resuming from idle here! */
244  stopCatchingIdleEvents();
245  reloadAlarms();
246  emit resumingFromIdle();
247  }
248 
249  return false;
250 }
251 
252 void XSyncBasedPoller::setAlarm(Display *dpy, XSyncAlarm *alarm, XSyncCounter counter,
253  XSyncTestType test, XSyncValue value)
254 {
255  XSyncAlarmAttributes attr;
256  XSyncValue delta;
257  unsigned int flags;
258 
259  XSyncIntToValue(&delta, 0);
260 
261  attr.trigger.counter = counter;
262  attr.trigger.value_type = XSyncAbsolute;
263  attr.trigger.test_type = test;
264  attr.trigger.wait_value = value;
265  attr.delta = delta;
266 
267  flags = XSyncCACounter | XSyncCAValueType | XSyncCATestType |
268  XSyncCAValue | XSyncCADelta;
269 
270  if (*alarm) {
271  XSyncChangeAlarm(dpy, *alarm, flags, &attr);
272  } else {
273  *alarm = XSyncCreateAlarm(dpy, flags, &attr);
274  }
275 }
276 
277 void XSyncBasedPoller::simulateUserActivity()
278 {
279  XResetScreenSaver(QX11Info::display());
280 }
281 
282 #include "xsyncbasedpoller.moc"
add
KGuiItem add()
QWidget
AbstractSystemPoller::timeoutReached
void timeoutReached(int msec)
QHash::insert
iterator insert(const Key &key, const T &value)
XSyncBasedPoller::addTimeout
void addTimeout(int nextTimeout)
Definition: xsyncbasedpoller.cpp:128
xsyncbasedpoller.h
XSyncBasedPoller
Definition: xsyncbasedpoller.h:32
timeout
int timeout
QX11Info::display
Display * display()
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
XSyncBasedPoller::catchIdleEvent
void catchIdleEvent()
Definition: xsyncbasedpoller.cpp:185
XSyncBasedPoller::~XSyncBasedPoller
virtual ~XSyncBasedPoller()
Definition: xsyncbasedpoller.cpp:100
XSyncBasedPoller::stopCatchingIdleEvents
void stopCatchingIdleEvents()
Definition: xsyncbasedpoller.cpp:177
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KApplication::kApplication
static KApplication * kApplication()
XSyncBasedPoller::unloadPoller
void unloadPoller()
Definition: xsyncbasedpoller.cpp:124
AbstractSystemPoller::resumingFromIdle
void resumingFromIdle()
QObject::name
const char * name() const
kglobal.h
XSyncBasedPoller::removeTimeout
void removeTimeout(int nextTimeout)
Definition: xsyncbasedpoller.cpp:163
QHash::constEnd
const_iterator constEnd() const
QHash
QHash::begin
iterator begin()
XSyncBasedPoller::XSyncBasedPoller
XSyncBasedPoller(QWidget *parent=0)
Definition: xsyncbasedpoller.cpp:49
QList< int >
QHash::remove
int remove(const Key &key)
XSyncBasedPoller::setUpPoller
bool setUpPoller()
Definition: xsyncbasedpoller.cpp:109
QHash::keys
QList< Key > keys() const
fixx11h.h
AbstractSystemPoller
Definition: abstractsystempoller.h:24
XSyncBasedPoller::timeouts
QList< int > timeouts() const
Definition: xsyncbasedpoller.cpp:172
QHash::constBegin
const_iterator constBegin() const
XSyncBasedPoller::isAvailable
bool isAvailable()
Definition: xsyncbasedpoller.cpp:104
klocalizedstring.h
XSyncBasedPoller::forcePollRequest
int forcePollRequest()
Definition: xsyncbasedpoller.cpp:150
X
#define X
QHash::contains
bool contains(const Key &key) const
QHash::end
iterator end()
XSyncBasedPoller::simulateUserActivity
void simulateUserActivity()
Definition: xsyncbasedpoller.cpp:277
KApplication::installX11EventFilter
void installX11EventFilter(QWidget *filter)
QX11Info
KCModuleLoader::None
no error reporting is done
Definition: kcmoduleloader.h:52
XSyncBasedPoller::x11Event
bool x11Event(XEvent *event)
Definition: xsyncbasedpoller.cpp:218
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:25:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal