kernel/qabstracteventdispatcher.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qabstracteventdispatcher.h" -
43#include "qabstracteventdispatcher_p.h" -
44#include "qabstractnativeeventfilter.h" -
45 -
46#include "qthread.h" -
47#include <private/qthread_p.h> -
48#include <private/qcoreapplication_p.h> -
49#include <private/qfreelist_p.h> -
50 -
51QT_BEGIN_NAMESPACE -
52 -
53// we allow for 2^24 = 8^8 = 16777216 simultaneously running timers -
54struct QtTimerIdFreeListConstants : public QFreeListDefaultConstants -
55{ -
56 enum -
57 { -
58 InitialNextValue = 1, -
59 BlockCount = 6 -
60 }; -
61 -
62 static const int Sizes[BlockCount]; -
63}; -
64 -
65enum { -
66 Offset0 = 0x00000000, -
67 Offset1 = 0x00000040, -
68 Offset2 = 0x00000100, -
69 Offset3 = 0x00001000, -
70 Offset4 = 0x00010000, -
71 Offset5 = 0x00100000, -
72 -
73 Size0 = Offset1 - Offset0, -
74 Size1 = Offset2 - Offset1, -
75 Size2 = Offset3 - Offset2, -
76 Size3 = Offset4 - Offset3, -
77 Size4 = Offset5 - Offset4, -
78 Size5 = QtTimerIdFreeListConstants::MaxIndex - Offset5 -
79}; -
80 -
81const int QtTimerIdFreeListConstants::Sizes[QtTimerIdFreeListConstants::BlockCount] = { -
82 Size0, -
83 Size1, -
84 Size2, -
85 Size3, -
86 Size4, -
87 Size5 -
88}; -
89 -
90typedef QFreeList<void, QtTimerIdFreeListConstants> QtTimerIdFreeList; -
91Q_GLOBAL_STATIC(QtTimerIdFreeList, timerIdFreeList)
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:263347
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:42
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:42
yes
Evaluation Count:263305
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:42
no
Evaluation Count:0
0-263347
92 -
93int QAbstractEventDispatcherPrivate::allocateTimerId() -
94{ -
95 return timerIdFreeList()->next();
executed: return timerIdFreeList()->next();
Execution Count:132414
132414
96} -
97 -
98void QAbstractEventDispatcherPrivate::releaseTimerId(int timerId) -
99{ -
100 // this function may be called by a global destructor after -
101 // timerIdFreeList() has been destructed -
102 if (QtTimerIdFreeList *fl = timerIdFreeList())
partially evaluated: QtTimerIdFreeList *fl = timerIdFreeList()
TRUEFALSE
yes
Evaluation Count:130933
no
Evaluation Count:0
0-130933
103 fl->release(timerId);
executed: fl->release(timerId);
Execution Count:130933
130933
104}
executed: }
Execution Count:130933
130933
105 -
106/*! -
107 \class QAbstractEventDispatcher -
108 \inmodule QtCore -
109 \brief The QAbstractEventDispatcher class provides an interface to manage Qt's event queue. -
110 -
111 \ingroup events -
112 -
113 An event dispatcher receives events from the window system and other -
114 sources. It then sends them to the QCoreApplication or QApplication -
115 instance for processing and delivery. QAbstractEventDispatcher provides -
116 fine-grained control over event delivery. -
117 -
118 For simple control of event processing use -
119 QCoreApplication::processEvents(). -
120 -
121 For finer control of the application's event loop, call -
122 instance() and call functions on the QAbstractEventDispatcher -
123 object that is returned. If you want to use your own instance of -
124 QAbstractEventDispatcher or of a QAbstractEventDispatcher -
125 subclass, you must install it with QCoreApplication::setEventDispatcher() -
126 or QThread::setEventDispatcher() \e before a default event dispatcher has -
127 been installed. -
128 -
129 The main event loop is started by calling -
130 QCoreApplication::exec(), and stopped by calling -
131 QCoreApplication::exit(). Local event loops can be created using -
132 QEventLoop. -
133 -
134 Programs that perform long operations can call processEvents() -
135 with a bitwise OR combination of various QEventLoop::ProcessEventsFlag -
136 values to control which events should be delivered. -
137 -
138 QAbstractEventDispatcher also allows the integration of an -
139 external event loop with the Qt event loop. -
140 -
141 \sa QEventLoop, QCoreApplication, QThread -
142*/ -
143 -
144/*! -
145 Constructs a new event dispatcher with the given \a parent. -
146*/ -
147QAbstractEventDispatcher::QAbstractEventDispatcher(QObject *parent) -
148 : QObject(*new QAbstractEventDispatcherPrivate, parent) {}
executed: }
Execution Count:2
2
149 -
150/*! -
151 \internal -
152*/ -
153QAbstractEventDispatcher::QAbstractEventDispatcher(QAbstractEventDispatcherPrivate &dd, -
154 QObject *parent) -
155 : QObject(dd, parent) {}
executed: }
Execution Count:1717500
1717500
156 -
157/*! -
158 Destroys the event dispatcher. -
159*/ -
160QAbstractEventDispatcher::~QAbstractEventDispatcher() -
161{ } -
162 -
163/*! -
164 Returns a pointer to the event dispatcher object for the specified -
165 \a thread. If \a thread is zero, the current thread is used. If no -
166 event dispatcher exists for the specified thread, this function -
167 returns 0. -
168 -
169 \b{Note:} If Qt is built without thread support, the \a thread -
170 argument is ignored. -
171 */ -
172QAbstractEventDispatcher *QAbstractEventDispatcher::instance(QThread *thread) -
173{ -
174 QThreadData *data = thread ? QThreadData::get2(thread) : QThreadData::current();
evaluated: thread
TRUEFALSE
yes
Evaluation Count:136
yes
Evaluation Count:286304
136-286304
175 return data->eventDispatcher;
executed: return data->eventDispatcher;
Execution Count:286440
286440
176} -
177 -
178/*! -
179 \fn bool QAbstractEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags) -
180 -
181 Processes pending events that match \a flags until there are no -
182 more events to process. Returns true if an event was processed; -
183 otherwise returns false. -
184 -
185 This function is especially useful if you have a long running -
186 operation and want to show its progress without allowing user -
187 input; i.e. by using the QEventLoop::ExcludeUserInputEvents flag. -
188 -
189 If the QEventLoop::WaitForMoreEvents flag is set in \a flags, the -
190 behavior of this function is as follows: -
191 -
192 \list -
193 -
194 \li If events are available, this function returns after processing -
195 them. -
196 -
197 \li If no events are available, this function will wait until more -
198 are available and return after processing newly available events. -
199 -
200 \endlist -
201 -
202 If the QEventLoop::WaitForMoreEvents flag is not set in \a flags, -
203 and no events are available, this function will return -
204 immediately. -
205 -
206 \b{Note:} This function does not process events continuously; it -
207 returns after all available events are processed. -
208 -
209 \sa hasPendingEvents() -
210*/ -
211 -
212/*! \fn bool QAbstractEventDispatcher::hasPendingEvents() -
213 -
214 Returns true if there is an event waiting; otherwise returns -
215 false. -
216*/ -
217 -
218/*! -
219 \fn void QAbstractEventDispatcher::registerSocketNotifier(QSocketNotifier *notifier) -
220 -
221 Registers \a notifier with the event loop. Subclasses must -
222 implement this method to tie a socket notifier into another -
223 event loop. -
224*/ -
225 -
226/*! \fn void QAbstractEventDispatcher::unregisterSocketNotifier(QSocketNotifier *notifier) -
227 -
228 Unregisters \a notifier from the event dispatcher. Subclasses must -
229 reimplement this method to tie a socket notifier into another -
230 event loop. Reimplementations must call the base -
231 implementation. -
232*/ -
233 -
234/*! -
235 \obsolete -
236 -
237 \fn int QAbstractEventDispatcher::registerTimer(int interval, QObject *object) -
238 -
239 Registers a timer with the specified \a interval for the given \a object -
240 and returns the timer id. -
241*/ -
242 -
243/*! -
244 \obsolete -
245 -
246 \fn void QAbstractEventDispatcher::registerTimer(int timerId, int interval, QObject *object) -
247 -
248 Register a timer with the specified \a timerId and \a interval for the -
249 given \a object. -
250*/ -
251 -
252/*! -
253 Registers a timer with the specified \a interval and \a timerType for the -
254 given \a object and returns the timer id. -
255*/ -
256int QAbstractEventDispatcher::registerTimer(int interval, Qt::TimerType timerType, QObject *object) -
257{ -
258 int id = QAbstractEventDispatcherPrivate::allocateTimerId();
executed (the execution status of this line is deduced): int id = QAbstractEventDispatcherPrivate::allocateTimerId();
-
259 registerTimer(id, interval, timerType, object);
executed (the execution status of this line is deduced): registerTimer(id, interval, timerType, object);
-
260 return id;
executed: return id;
Execution Count:132414
132414
261} -
262 -
263/*! -
264 \fn void QAbstractEventDispatcher::registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *object) -
265 -
266 Register a timer with the specified \a timerId, \a interval, and \a -
267 timerType for the given \a object. -
268*/ -
269 -
270/*! -
271 \fn bool QAbstractEventDispatcher::unregisterTimer(int timerId) -
272 -
273 Unregisters the timer with the given \a timerId. -
274 Returns true if successful; otherwise returns false. -
275 -
276 \sa registerTimer(), unregisterTimers() -
277*/ -
278 -
279/*! -
280 \fn bool QAbstractEventDispatcher::unregisterTimers(QObject *object) -
281 -
282 Unregisters all the timers associated with the given \a object. -
283 Returns true if all timers were successful removed; otherwise returns false. -
284 -
285 \sa unregisterTimer(), registeredTimers() -
286*/ -
287 -
288/*! -
289 \fn QList<TimerInfo> QAbstractEventDispatcher::registeredTimers(QObject *object) const -
290 -
291 Returns a list of registered timers for \a object. The TimerInfo struct has -
292 \c timerId, \c interval, and \c timerType members. -
293 -
294 \sa Qt::TimerType -
295*/ -
296 -
297/*! -
298 \fn int QAbstractEventDispatcher::remainingTime(int timerId) -
299 -
300 Returns the remaining time in milliseconds with the given \a timerId. -
301 If the timer is inactive, the returned value will be -1. If the timer is -
302 overdue, the returned value will be 0. -
303 -
304 \sa Qt::TimerType -
305*/ -
306 -
307/*! \fn void QAbstractEventDispatcher::wakeUp() -
308 \threadsafe -
309 -
310 Wakes up the event loop. -
311 -
312 \sa awake() -
313*/ -
314 -
315/*! -
316 \fn void QAbstractEventDispatcher::interrupt() -
317 -
318 Interrupts event dispatching; i.e. the event dispatcher will -
319 return from processEvents() as soon as possible. -
320*/ -
321 -
322/*! \fn void QAbstractEventDispatcher::flush() -
323 -
324 Flushes the event queue. This normally returns almost -
325 immediately. Does nothing on platforms other than X11. -
326*/ -
327 -
328// ### DOC: Are these called when the _application_ starts/stops or just -
329// when the current _event loop_ starts/stops? -
330/*! -
331 \internal -
332*/ -
333void QAbstractEventDispatcher::startingUp() -
334{ } -
335 -
336/*! -
337 \internal -
338*/ -
339void QAbstractEventDispatcher::closingDown() -
340{ } -
341 -
342/*! -
343 \class QAbstractEventDispatcher::TimerInfo -
344 \inmodule QtCore -
345 -
346 This struct represents information about a timer: -
347 \l{QAbstractEventDispatcher::TimerInfo::timerId}{timerId}, -
348 \l{QAbstractEventDispatcher::TimerInfo::interval}{interval}, and -
349 \l{QAbstractEventDispatcher::TimerInfo::timerType}{timerType}. -
350 -
351 \sa registeredTimers() -
352*/ -
353/*! \fn QAbstractEventDispatcher::TimerInfo::TimerInfo(int timerId, int interval, Qt::TimerType timerType) -
354 -
355 Constructs a TimerInfo struct with the given \a timerId, \a interval, and -
356 \a timerType. -
357*/ -
358/*! -
359 \variable QAbstractEventDispatcher::TimerInfo::timerId -
360 -
361 The timer's unique id. -
362*/ -
363/*! -
364 \variable QAbstractEventDispatcher::TimerInfo::interval -
365 -
366 The timer's interval. -
367*/ -
368/*! -
369 \variable QAbstractEventDispatcher::TimerInfo::timerType -
370 -
371 The timer's type -
372 -
373 \sa Qt::TimerType -
374*/ -
375 -
376/*! -
377 Installs an event filter \a filterObj for all native event filters -
378 received by the application. -
379 -
380 The event filter \a filterObj receives events via its nativeEventFilter() -
381 function, which is called for all events received by all threads. -
382 -
383 The nativeEventFilter() function should return true if the event should -
384 be filtered, (i.e. stopped). It should return false to allow -
385 normal Qt processing to continue: the native event can then be translated -
386 into a QEvent and handled by the standard Qt \l{QEvent} {event} filtering, -
387 e.g. QObject::installEventFilter(). -
388 -
389 If multiple event filters are installed, the filter that was installed last -
390 is activated first. -
391 -
392 \note The filter function set here receives native messages, -
393 i.e. MSG or XEvent structs. -
394 -
395 For maximum portability, you should always try to use QEvents -
396 and QObject::installEventFilter() whenever possible. -
397 -
398 \sa QObject::installEventFilter() -
399 -
400 \since 5.0 -
401*/ -
402void QAbstractEventDispatcher::installNativeEventFilter(QAbstractNativeEventFilter *filterObj) -
403{ -
404 Q_D(QAbstractEventDispatcher);
executed (the execution status of this line is deduced): QAbstractEventDispatcherPrivate * const d = d_func();
-
405 -
406 // clean up unused items in the list -
407 d->eventFilters.removeAll(0);
executed (the execution status of this line is deduced): d->eventFilters.removeAll(0);
-
408 d->eventFilters.removeAll(filterObj);
executed (the execution status of this line is deduced): d->eventFilters.removeAll(filterObj);
-
409 d->eventFilters.prepend(filterObj);
executed (the execution status of this line is deduced): d->eventFilters.prepend(filterObj);
-
410}
executed: }
Execution Count:1
1
411 -
412/*! -
413 Removes the event filter \a filter from this object. The -
414 request is ignored if such an event filter has not been installed. -
415 -
416 All event filters for this object are automatically removed when -
417 this object is destroyed. -
418 -
419 It is always safe to remove an event filter, even during event -
420 filter activation (i.e. from the nativeEventFilter() function). -
421 -
422 \sa installNativeEventFilter(), QAbstractNativeEventFilter -
423 \since 5.0 -
424*/ -
425void QAbstractEventDispatcher::removeNativeEventFilter(QAbstractNativeEventFilter *filter) -
426{ -
427 Q_D(QAbstractEventDispatcher);
executed (the execution status of this line is deduced): QAbstractEventDispatcherPrivate * const d = d_func();
-
428 for (int i = 0; i < d->eventFilters.count(); ++i) {
partially evaluated: i < d->eventFilters.count()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
429 if (d->eventFilters.at(i) == filter) {
partially evaluated: d->eventFilters.at(i) == filter
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
430 d->eventFilters[i] = 0;
executed (the execution status of this line is deduced): d->eventFilters[i] = 0;
-
431 break;
executed: break;
Execution Count:1
1
432 } -
433 }
never executed: }
0
434}
executed: }
Execution Count:1
1
435 -
436/*! -
437 Sends \a message through the event filters that were set by -
438 installNativeEventFilter(). This function returns true as soon as an -
439 event filter returns true, and false otherwise to indicate that -
440 the processing of the event should continue. -
441 -
442 Subclasses of QAbstractEventDispatcher \e must call this function -
443 for \e all messages received from the system to ensure -
444 compatibility with any extensions that may be used in the -
445 application. The type of event \a eventType is specific to the platform -
446 plugin chosen at run-time, and can be used to cast message to the right type. -
447 The \a result pointer is only used on Windows, and corresponds to the LRESULT pointer. -
448 -
449 Note that the type of \a message is platform dependent. See -
450 QAbstractNativeEventFilter for details. -
451 -
452 \sa installNativeEventFilter(), QAbstractNativeEventFilter::nativeEventFilter() -
453 \since 5.0 -
454*/ -
455bool QAbstractEventDispatcher::filterNativeEvent(const QByteArray &eventType, void *message, long *result) -
456{ -
457 Q_D(QAbstractEventDispatcher);
executed (the execution status of this line is deduced): QAbstractEventDispatcherPrivate * const d = d_func();
-
458 if (!d->eventFilters.isEmpty()) {
evaluated: !d->eventFilters.isEmpty()
TRUEFALSE
yes
Evaluation Count:5072
yes
Evaluation Count:143523
5072-143523
459 // Raise the loopLevel so that deleteLater() calls in or triggered -
460 // by event_filter() will be processed from the main event loop. -
461 QScopedLoopLevelCounter loopLevelCounter(d->threadData);
executed (the execution status of this line is deduced): QScopedLoopLevelCounter loopLevelCounter(d->threadData);
-
462 for (int i = 0; i < d->eventFilters.size(); ++i) {
evaluated: i < d->eventFilters.size()
TRUEFALSE
yes
Evaluation Count:5072
yes
Evaluation Count:5072
5072
463 QAbstractNativeEventFilter *filter = d->eventFilters.at(i);
executed (the execution status of this line is deduced): QAbstractNativeEventFilter *filter = d->eventFilters.at(i);
-
464 if (!filter)
evaluated: !filter
TRUEFALSE
yes
Evaluation Count:4974
yes
Evaluation Count:98
98-4974
465 continue;
executed: continue;
Execution Count:4974
4974
466 if (filter->nativeEventFilter(eventType, message, result))
partially evaluated: filter->nativeEventFilter(eventType, message, result)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:98
0-98
467 return true;
never executed: return true;
0
468 }
executed: }
Execution Count:98
98
469 }
executed: }
Execution Count:5072
5072
470 return false;
executed: return false;
Execution Count:148595
148595
471} -
472 -
473/*! \fn bool QAbstractEventDispatcher::filterEvent(void *message) -
474 \deprecated -
475 -
476 Calls filterNativeEvent() with an empty eventType and \a message. -
477 This function returns true as soon as an -
478 event filter returns true, and false otherwise to indicate that -
479 the processing of the event should continue. -
480*/ -
481 -
482/*! \fn bool QAbstractEventDispatcher::registerEventNotifier(QWinEventNotifier *notifier); -
483 -
484 This pure virtual method exists on windows only and has to be reimplemented by a Windows specific -
485 event dispatcher implementation. \a notifier is the QWinEventNotifier instance to be registered. -
486 -
487 The method should return true if the registration of \a notifier was sucessful, otherwise false. -
488 -
489 QWinEventNotifier calls this method in it's constructor and there should never be a need to call this -
490 method directly. -
491 -
492 \sa QWinEventNotifier, unregisterEventNotifier() -
493*/ -
494 -
495/*! \fn bool QAbstractEventDispatcher::unregisterEventNotifier(QWinEventNotifier *notifier); -
496 -
497 This pure virtual method exists on windows only and has to be reimplemented by a Windows specific -
498 event dispatcher implementation. \a notifier is the QWinEventNotifier instance to be unregistered. -
499 -
500 QWinEventNotifier calls this method in it's destructor and there should never be a need to call this -
501 method directly. -
502 -
503 \sa QWinEventNotifier, registerEventNotifier() -
504*/ -
505 -
506/*! \fn void QAbstractEventDispatcher::awake() -
507 -
508 This signal is emitted after the event loop returns from a -
509 function that could block. -
510 -
511 \sa wakeUp(), aboutToBlock() -
512*/ -
513 -
514/*! \fn void QAbstractEventDispatcher::aboutToBlock() -
515 -
516 This signal is emitted before the event loop calls a function that -
517 could block. -
518 -
519 \sa awake() -
520*/ -
521 -
522QT_END_NAMESPACE -
523 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial