PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/qt/qtbase/src/corelib/kernel/qabstracteventdispatcher.cpp

https://gitlab.com/x33n/phantomjs
C++ | 524 lines | 105 code | 44 blank | 375 comment | 8 complexity | 70da6d86d8e0f3e90f205c1cd7576c16 MD5 | raw file
  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. #include "qabstracteventdispatcher.h"
  42. #include "qabstracteventdispatcher_p.h"
  43. #include "qabstractnativeeventfilter.h"
  44. #include "qthread.h"
  45. #include <private/qthread_p.h>
  46. #include <private/qcoreapplication_p.h>
  47. #include <private/qfreelist_p.h>
  48. QT_BEGIN_NAMESPACE
  49. // we allow for 2^24 = 8^8 = 16777216 simultaneously running timers
  50. struct QtTimerIdFreeListConstants : public QFreeListDefaultConstants
  51. {
  52. enum
  53. {
  54. InitialNextValue = 1,
  55. BlockCount = 6
  56. };
  57. static const int Sizes[BlockCount];
  58. };
  59. enum {
  60. Offset0 = 0x00000000,
  61. Offset1 = 0x00000040,
  62. Offset2 = 0x00000100,
  63. Offset3 = 0x00001000,
  64. Offset4 = 0x00010000,
  65. Offset5 = 0x00100000,
  66. Size0 = Offset1 - Offset0,
  67. Size1 = Offset2 - Offset1,
  68. Size2 = Offset3 - Offset2,
  69. Size3 = Offset4 - Offset3,
  70. Size4 = Offset5 - Offset4,
  71. Size5 = QtTimerIdFreeListConstants::MaxIndex - Offset5
  72. };
  73. const int QtTimerIdFreeListConstants::Sizes[QtTimerIdFreeListConstants::BlockCount] = {
  74. Size0,
  75. Size1,
  76. Size2,
  77. Size3,
  78. Size4,
  79. Size5
  80. };
  81. typedef QFreeList<void, QtTimerIdFreeListConstants> QtTimerIdFreeList;
  82. Q_GLOBAL_STATIC(QtTimerIdFreeList, timerIdFreeList)
  83. int QAbstractEventDispatcherPrivate::allocateTimerId()
  84. {
  85. return timerIdFreeList()->next();
  86. }
  87. void QAbstractEventDispatcherPrivate::releaseTimerId(int timerId)
  88. {
  89. // this function may be called by a global destructor after
  90. // timerIdFreeList() has been destructed
  91. if (QtTimerIdFreeList *fl = timerIdFreeList())
  92. fl->release(timerId);
  93. }
  94. /*!
  95. \class QAbstractEventDispatcher
  96. \inmodule QtCore
  97. \brief The QAbstractEventDispatcher class provides an interface to manage Qt's event queue.
  98. \ingroup events
  99. An event dispatcher receives events from the window system and other
  100. sources. It then sends them to the QCoreApplication or QApplication
  101. instance for processing and delivery. QAbstractEventDispatcher provides
  102. fine-grained control over event delivery.
  103. For simple control of event processing use
  104. QCoreApplication::processEvents().
  105. For finer control of the application's event loop, call
  106. instance() and call functions on the QAbstractEventDispatcher
  107. object that is returned. If you want to use your own instance of
  108. QAbstractEventDispatcher or of a QAbstractEventDispatcher
  109. subclass, you must install it with QCoreApplication::setEventDispatcher()
  110. or QThread::setEventDispatcher() \e before a default event dispatcher has
  111. been installed.
  112. The main event loop is started by calling
  113. QCoreApplication::exec(), and stopped by calling
  114. QCoreApplication::exit(). Local event loops can be created using
  115. QEventLoop.
  116. Programs that perform long operations can call processEvents()
  117. with a bitwise OR combination of various QEventLoop::ProcessEventsFlag
  118. values to control which events should be delivered.
  119. QAbstractEventDispatcher also allows the integration of an
  120. external event loop with the Qt event loop.
  121. \sa QEventLoop, QCoreApplication, QThread
  122. */
  123. /*!
  124. Constructs a new event dispatcher with the given \a parent.
  125. */
  126. QAbstractEventDispatcher::QAbstractEventDispatcher(QObject *parent)
  127. : QObject(*new QAbstractEventDispatcherPrivate, parent) {}
  128. /*!
  129. \internal
  130. */
  131. QAbstractEventDispatcher::QAbstractEventDispatcher(QAbstractEventDispatcherPrivate &dd,
  132. QObject *parent)
  133. : QObject(dd, parent) {}
  134. /*!
  135. Destroys the event dispatcher.
  136. */
  137. QAbstractEventDispatcher::~QAbstractEventDispatcher()
  138. { }
  139. /*!
  140. Returns a pointer to the event dispatcher object for the specified
  141. \a thread. If \a thread is zero, the current thread is used. If no
  142. event dispatcher exists for the specified thread, this function
  143. returns 0.
  144. \b{Note:} If Qt is built without thread support, the \a thread
  145. argument is ignored.
  146. */
  147. QAbstractEventDispatcher *QAbstractEventDispatcher::instance(QThread *thread)
  148. {
  149. QThreadData *data = thread ? QThreadData::get2(thread) : QThreadData::current();
  150. return data->eventDispatcher.load();
  151. }
  152. /*!
  153. \fn bool QAbstractEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
  154. Processes pending events that match \a flags until there are no
  155. more events to process. Returns \c true if an event was processed;
  156. otherwise returns \c false.
  157. This function is especially useful if you have a long running
  158. operation and want to show its progress without allowing user
  159. input; i.e. by using the QEventLoop::ExcludeUserInputEvents flag.
  160. If the QEventLoop::WaitForMoreEvents flag is set in \a flags, the
  161. behavior of this function is as follows:
  162. \list
  163. \li If events are available, this function returns after processing
  164. them.
  165. \li If no events are available, this function will wait until more
  166. are available and return after processing newly available events.
  167. \endlist
  168. If the QEventLoop::WaitForMoreEvents flag is not set in \a flags,
  169. and no events are available, this function will return
  170. immediately.
  171. \b{Note:} This function does not process events continuously; it
  172. returns after all available events are processed.
  173. \sa hasPendingEvents()
  174. */
  175. /*! \fn bool QAbstractEventDispatcher::hasPendingEvents()
  176. \deprecated
  177. Returns \c true if there is an event waiting; otherwise returns false. This
  178. function is an implementation detail for
  179. QCoreApplication::hasPendingEvents() and must not be called directly.
  180. */
  181. /*!
  182. \fn void QAbstractEventDispatcher::registerSocketNotifier(QSocketNotifier *notifier)
  183. Registers \a notifier with the event loop. Subclasses must
  184. implement this method to tie a socket notifier into another
  185. event loop.
  186. */
  187. /*! \fn void QAbstractEventDispatcher::unregisterSocketNotifier(QSocketNotifier *notifier)
  188. Unregisters \a notifier from the event dispatcher. Subclasses must
  189. reimplement this method to tie a socket notifier into another
  190. event loop. Reimplementations must call the base
  191. implementation.
  192. */
  193. /*!
  194. \obsolete
  195. \fn int QAbstractEventDispatcher::registerTimer(int interval, QObject *object)
  196. Registers a timer with the specified \a interval for the given \a object
  197. and returns the timer id.
  198. */
  199. /*!
  200. \obsolete
  201. \fn void QAbstractEventDispatcher::registerTimer(int timerId, int interval, QObject *object)
  202. Register a timer with the specified \a timerId and \a interval for the
  203. given \a object.
  204. */
  205. /*!
  206. Registers a timer with the specified \a interval and \a timerType for the
  207. given \a object and returns the timer id.
  208. */
  209. int QAbstractEventDispatcher::registerTimer(int interval, Qt::TimerType timerType, QObject *object)
  210. {
  211. int id = QAbstractEventDispatcherPrivate::allocateTimerId();
  212. registerTimer(id, interval, timerType, object);
  213. return id;
  214. }
  215. /*!
  216. \fn void QAbstractEventDispatcher::registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *object)
  217. Register a timer with the specified \a timerId, \a interval, and \a
  218. timerType for the given \a object.
  219. */
  220. /*!
  221. \fn bool QAbstractEventDispatcher::unregisterTimer(int timerId)
  222. Unregisters the timer with the given \a timerId.
  223. Returns \c true if successful; otherwise returns \c false.
  224. \sa registerTimer(), unregisterTimers()
  225. */
  226. /*!
  227. \fn bool QAbstractEventDispatcher::unregisterTimers(QObject *object)
  228. Unregisters all the timers associated with the given \a object.
  229. Returns \c true if all timers were successful removed; otherwise returns \c false.
  230. \sa unregisterTimer(), registeredTimers()
  231. */
  232. /*!
  233. \fn QList<TimerInfo> QAbstractEventDispatcher::registeredTimers(QObject *object) const
  234. Returns a list of registered timers for \a object. The TimerInfo struct has
  235. \c timerId, \c interval, and \c timerType members.
  236. \sa Qt::TimerType
  237. */
  238. /*!
  239. \fn int QAbstractEventDispatcher::remainingTime(int timerId)
  240. Returns the remaining time in milliseconds with the given \a timerId.
  241. If the timer is inactive, the returned value will be -1. If the timer is
  242. overdue, the returned value will be 0.
  243. \sa Qt::TimerType
  244. */
  245. /*! \fn void QAbstractEventDispatcher::wakeUp()
  246. \threadsafe
  247. Wakes up the event loop.
  248. \sa awake()
  249. */
  250. /*!
  251. \fn void QAbstractEventDispatcher::interrupt()
  252. Interrupts event dispatching; i.e. the event dispatcher will
  253. return from processEvents() as soon as possible.
  254. */
  255. /*! \fn void QAbstractEventDispatcher::flush()
  256. Flushes the event queue. This normally returns almost
  257. immediately. Does nothing on platforms other than X11.
  258. */
  259. // ### DOC: Are these called when the _application_ starts/stops or just
  260. // when the current _event loop_ starts/stops?
  261. /*!
  262. \internal
  263. */
  264. void QAbstractEventDispatcher::startingUp()
  265. { }
  266. /*!
  267. \internal
  268. */
  269. void QAbstractEventDispatcher::closingDown()
  270. { }
  271. /*!
  272. \class QAbstractEventDispatcher::TimerInfo
  273. \inmodule QtCore
  274. This struct represents information about a timer:
  275. \l{QAbstractEventDispatcher::TimerInfo::timerId}{timerId},
  276. \l{QAbstractEventDispatcher::TimerInfo::interval}{interval}, and
  277. \l{QAbstractEventDispatcher::TimerInfo::timerType}{timerType}.
  278. \sa registeredTimers()
  279. */
  280. /*! \fn QAbstractEventDispatcher::TimerInfo::TimerInfo(int timerId, int interval, Qt::TimerType timerType)
  281. Constructs a TimerInfo struct with the given \a timerId, \a interval, and
  282. \a timerType.
  283. */
  284. /*!
  285. \variable QAbstractEventDispatcher::TimerInfo::timerId
  286. The timer's unique id.
  287. */
  288. /*!
  289. \variable QAbstractEventDispatcher::TimerInfo::interval
  290. The timer's interval.
  291. */
  292. /*!
  293. \variable QAbstractEventDispatcher::TimerInfo::timerType
  294. The timer's type
  295. \sa Qt::TimerType
  296. */
  297. /*!
  298. Installs an event filter \a filterObj for all native event filters
  299. received by the application.
  300. The event filter \a filterObj receives events via its nativeEventFilter()
  301. function, which is called for all events received by all threads.
  302. The nativeEventFilter() function should return true if the event should
  303. be filtered, (i.e. stopped). It should return false to allow
  304. normal Qt processing to continue: the native event can then be translated
  305. into a QEvent and handled by the standard Qt \l{QEvent} {event} filtering,
  306. e.g. QObject::installEventFilter().
  307. If multiple event filters are installed, the filter that was installed last
  308. is activated first.
  309. \note The filter function set here receives native messages,
  310. i.e. MSG or XEvent structs.
  311. For maximum portability, you should always try to use QEvents
  312. and QObject::installEventFilter() whenever possible.
  313. \sa QObject::installEventFilter()
  314. \since 5.0
  315. */
  316. void QAbstractEventDispatcher::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
  317. {
  318. Q_D(QAbstractEventDispatcher);
  319. // clean up unused items in the list
  320. d->eventFilters.removeAll(0);
  321. d->eventFilters.removeAll(filterObj);
  322. d->eventFilters.prepend(filterObj);
  323. }
  324. /*!
  325. Removes the event filter \a filter from this object. The
  326. request is ignored if such an event filter has not been installed.
  327. All event filters for this object are automatically removed when
  328. this object is destroyed.
  329. It is always safe to remove an event filter, even during event
  330. filter activation (i.e. from the nativeEventFilter() function).
  331. \sa installNativeEventFilter(), QAbstractNativeEventFilter
  332. \since 5.0
  333. */
  334. void QAbstractEventDispatcher::removeNativeEventFilter(QAbstractNativeEventFilter *filter)
  335. {
  336. Q_D(QAbstractEventDispatcher);
  337. for (int i = 0; i < d->eventFilters.count(); ++i) {
  338. if (d->eventFilters.at(i) == filter) {
  339. d->eventFilters[i] = 0;
  340. break;
  341. }
  342. }
  343. }
  344. /*!
  345. Sends \a message through the event filters that were set by
  346. installNativeEventFilter(). This function returns \c true as soon as an
  347. event filter returns \c true, and false otherwise to indicate that
  348. the processing of the event should continue.
  349. Subclasses of QAbstractEventDispatcher \e must call this function
  350. for \e all messages received from the system to ensure
  351. compatibility with any extensions that may be used in the
  352. application. The type of event \a eventType is specific to the platform
  353. plugin chosen at run-time, and can be used to cast message to the right type.
  354. The \a result pointer is only used on Windows, and corresponds to the LRESULT pointer.
  355. Note that the type of \a message is platform dependent. See
  356. QAbstractNativeEventFilter for details.
  357. \sa installNativeEventFilter(), QAbstractNativeEventFilter::nativeEventFilter()
  358. \since 5.0
  359. */
  360. bool QAbstractEventDispatcher::filterNativeEvent(const QByteArray &eventType, void *message, long *result)
  361. {
  362. Q_D(QAbstractEventDispatcher);
  363. if (!d->eventFilters.isEmpty()) {
  364. // Raise the loopLevel so that deleteLater() calls in or triggered
  365. // by event_filter() will be processed from the main event loop.
  366. QScopedLoopLevelCounter loopLevelCounter(d->threadData);
  367. for (int i = 0; i < d->eventFilters.size(); ++i) {
  368. QAbstractNativeEventFilter *filter = d->eventFilters.at(i);
  369. if (!filter)
  370. continue;
  371. if (filter->nativeEventFilter(eventType, message, result))
  372. return true;
  373. }
  374. }
  375. return false;
  376. }
  377. /*! \fn bool QAbstractEventDispatcher::filterEvent(void *message)
  378. \deprecated
  379. Calls filterNativeEvent() with an empty eventType and \a message.
  380. This function returns \c true as soon as an
  381. event filter returns \c true, and false otherwise to indicate that
  382. the processing of the event should continue.
  383. */
  384. /*! \fn bool QAbstractEventDispatcher::registerEventNotifier(QWinEventNotifier *notifier);
  385. This pure virtual method exists on windows only and has to be reimplemented by a Windows specific
  386. event dispatcher implementation. \a notifier is the QWinEventNotifier instance to be registered.
  387. The method should return true if the registration of \a notifier was sucessful, otherwise false.
  388. QWinEventNotifier calls this method in it's constructor and there should never be a need to call this
  389. method directly.
  390. \sa QWinEventNotifier, unregisterEventNotifier()
  391. */
  392. /*! \fn bool QAbstractEventDispatcher::unregisterEventNotifier(QWinEventNotifier *notifier);
  393. This pure virtual method exists on windows only and has to be reimplemented by a Windows specific
  394. event dispatcher implementation. \a notifier is the QWinEventNotifier instance to be unregistered.
  395. QWinEventNotifier calls this method in it's destructor and there should never be a need to call this
  396. method directly.
  397. \sa QWinEventNotifier, registerEventNotifier()
  398. */
  399. /*! \fn void QAbstractEventDispatcher::awake()
  400. This signal is emitted after the event loop returns from a
  401. function that could block.
  402. \sa wakeUp(), aboutToBlock()
  403. */
  404. /*! \fn void QAbstractEventDispatcher::aboutToBlock()
  405. This signal is emitted before the event loop calls a function that
  406. could block.
  407. \sa awake()
  408. */
  409. QT_END_NAMESPACE