📄 qeventdispatcher_glib.cpp
字号:
g_source_attach(&postEventSource->source, mainContext); // setup socketNotifierSource socketNotifierSource = reinterpret_cast<GSocketNotifierSource *>(g_source_new(&socketNotifierSourceFuncs, sizeof(GSocketNotifierSource))); (void) new (&socketNotifierSource->pollfds) QList<GPollFDWithQSocketNotifier *>(); g_source_set_can_recurse(&socketNotifierSource->source, true); g_source_attach(&socketNotifierSource->source, mainContext); // setup timerSource timerSource = reinterpret_cast<GTimerSource *>(g_source_new(&timerSourceFuncs, sizeof(GTimerSource))); (void) new (&timerSource->timerList) QTimerInfoList(); g_source_set_can_recurse(&timerSource->source, true); g_source_attach(&timerSource->source, mainContext);}QEventDispatcherGlib::QEventDispatcherGlib(QObject *parent) : QAbstractEventDispatcher(*(new QEventDispatcherGlibPrivate), parent){}QEventDispatcherGlib::QEventDispatcherGlib(GMainContext *mainContext, QObject *parent) : QAbstractEventDispatcher(*(new QEventDispatcherGlibPrivate(mainContext)), parent){}QEventDispatcherGlib::~QEventDispatcherGlib(){ Q_D(QEventDispatcherGlib); // destroy all timer sources qDeleteAll(d->timerSource->timerList); d->timerSource->timerList.~QTimerInfoList(); g_source_destroy(&d->timerSource->source); g_source_unref(&d->timerSource->source); d->timerSource = 0; // destroy socket notifier source for (int i = 0; i < d->socketNotifierSource->pollfds.count(); ++i) { GPollFDWithQSocketNotifier *p = d->socketNotifierSource->pollfds[i]; g_source_remove_poll(&d->socketNotifierSource->source, &p->pollfd); delete p; } d->socketNotifierSource->pollfds.~QList<GPollFDWithQSocketNotifier *>(); g_source_destroy(&d->socketNotifierSource->source); g_source_unref(&d->socketNotifierSource->source); d->socketNotifierSource = 0; // destroy post event source g_source_remove_poll(&d->postEventSource->source, &d->postEventSource->pollfd); close(d->postEventSource->wakeUpPipe[0]); close(d->postEventSource->wakeUpPipe[1]); d->postEventSource->wakeUpPipe[0] = 0; d->postEventSource->wakeUpPipe[1] = 0; g_source_destroy(&d->postEventSource->source); g_source_unref(&d->postEventSource->source); d->postEventSource = 0; Q_ASSERT(d->mainContext != 0); g_main_context_unref(d->mainContext); d->mainContext = 0;}bool QEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags){ Q_D(QEventDispatcherGlib); const bool canWait = (flags & QEventLoop::WaitForMoreEvents); if (canWait) emit aboutToBlock(); else emit awake(); // tell postEventSourcePrepare() about any new flags d->postEventSource->flags = flags; bool result = g_main_context_iteration(d->mainContext, canWait); while (!result && canWait) result = g_main_context_iteration(d->mainContext, canWait); if (canWait) emit awake(); return result;}bool QEventDispatcherGlib::hasPendingEvents(){ Q_D(QEventDispatcherGlib); return g_main_context_pending(d->mainContext);}void QEventDispatcherGlib::registerSocketNotifier(QSocketNotifier *notifier){ Q_ASSERT(notifier); int sockfd = notifier->socket(); int type = notifier->type();#ifndef QT_NO_DEBUG if (sockfd < 0 || unsigned(sockfd) >= FD_SETSIZE) { qWarning("QSocketNotifier: Internal error"); return; } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) { qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread"); return; }#endif Q_D(QEventDispatcherGlib); GPollFDWithQSocketNotifier *p = new GPollFDWithQSocketNotifier; p->pollfd.fd = sockfd; switch (type) { case QSocketNotifier::Read: p->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR; break; case QSocketNotifier::Write: p->pollfd.events = G_IO_OUT | G_IO_ERR; break; case QSocketNotifier::Exception: p->pollfd.events = G_IO_PRI | G_IO_ERR; break; } p->socketNotifier = notifier; d->socketNotifierSource->pollfds.append(p); g_source_add_poll(&d->socketNotifierSource->source, &p->pollfd);}void QEventDispatcherGlib::unregisterSocketNotifier(QSocketNotifier *notifier){ Q_ASSERT(notifier);#ifndef QT_NO_DEBUG int sockfd = notifier->socket(); if (sockfd < 0 || unsigned(sockfd) >= FD_SETSIZE) { qWarning("QSocketNotifier: Internal error"); return; } else if (notifier->thread() != thread() || thread() != QThread::currentThread()) { qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread"); return; }#endif Q_D(QEventDispatcherGlib); for (int i = 0; i < d->socketNotifierSource->pollfds.count(); ++i) { GPollFDWithQSocketNotifier *p = d->socketNotifierSource->pollfds.at(i); if (p->socketNotifier == notifier) { // found it g_source_remove_poll(&d->socketNotifierSource->source, &p->pollfd); d->socketNotifierSource->pollfds.removeAt(i); delete p; return; } }}void QEventDispatcherGlib::registerTimer(int timerId, int interval, QObject *object){#ifndef QT_NO_DEBUG if (timerId < 1 || interval < 0 || !object) { qWarning("QEventDispatcherUNIX::registerTimer: invalid arguments"); return; } else if (object->thread() != thread() || thread() != QThread::currentThread()) { qWarning("QObject::startTimer: timers cannot be started from another thread"); return; }#endif Q_D(QEventDispatcherGlib); d->timerSource->timerList.registerTimer(timerId, interval, object);}bool QEventDispatcherGlib::unregisterTimer(int timerId){#ifndef QT_NO_DEBUG if (timerId < 1) { qWarning("QEventDispatcherUNIX::unregisterTimer: invalid argument"); return false; } else if (thread() != QThread::currentThread()) { qWarning("QObject::killTimer: timers cannot be stopped from another thread"); return false; }#endif Q_D(QEventDispatcherGlib); return d->timerSource->timerList.unregisterTimer(timerId);}bool QEventDispatcherGlib::unregisterTimers(QObject *object){#ifndef QT_NO_DEBUG if (!object) { qWarning("QEventDispatcherUNIX::unregisterTimers: invalid argument"); return false; } else if (object->thread() != thread() || thread() != QThread::currentThread()) { qWarning("QObject::killTimers: timers cannot be stopped from another thread"); return false; }#endif Q_D(QEventDispatcherGlib); return d->timerSource->timerList.unregisterTimers(object);}QList<QEventDispatcherGlib::TimerInfo> QEventDispatcherGlib::registeredTimers(QObject *object) const{ if (!object) { qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument"); return QList<TimerInfo>(); } Q_D(const QEventDispatcherGlib); return d->timerSource->timerList.registeredTimers(object);}void QEventDispatcherGlib::interrupt(){ wakeUp();}void QEventDispatcherGlib::wakeUp(){ Q_D(QEventDispatcherGlib); char c = 0; ::write(d->postEventSource->wakeUpPipe[1], &c, 1);}void QEventDispatcherGlib::flush(){}bool QEventDispatcherGlib::versionSupported(){#if !defined(GLIB_MAJOR_VERSION) || !defined(GLIB_MINOR_VERSION) || !defined(GLIB_MICRO_VERSION) return false;#else return ((GLIB_MAJOR_VERSION << 16) + (GLIB_MINOR_VERSION << 8) + GLIB_MICRO_VERSION) >= 0x020301;#endif}QEventDispatcherGlib::QEventDispatcherGlib(QEventDispatcherGlibPrivate &dd, QObject *parent) : QAbstractEventDispatcher(dd, parent){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -