?? qeventdispatcher_glib.cpp
字號:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qeventdispatcher_glib_p.h"#include "qeventdispatcher_unix_p.h"#include <private/qthread_p.h>#include "qcoreapplication.h"#include "qsocketnotifier.h"#include <QtCore/qhash.h>#include <QtCore/qlist.h>#include <QtCore/qpair.h>#include <glib.h>struct GPollFDWithQSocketNotifier{ GPollFD pollfd; QSocketNotifier *socketNotifier;};struct GSocketNotifierSource{ GSource source; QList<GPollFDWithQSocketNotifier *> pollfds;};static gboolean socketNotifierSourcePrepare(GSource *, gint *timeout){ if (timeout) *timeout = -1; return false;}static gboolean socketNotifierSourceCheck(GSource *source){ GSocketNotifierSource *src = reinterpret_cast<GSocketNotifierSource *>(source); bool pending = false; for (int i = 0; !pending && i < src->pollfds.count(); ++i) { GPollFDWithQSocketNotifier *p = src->pollfds.at(i); if (p->pollfd.revents & G_IO_NVAL) { // disable the invalid socket notifier static const char *t[] = { "Read", "Write", "Exception" }; qWarning("QSocketNotifier: Invalid socket %d and type '%s', disabling...", p->pollfd.fd, t[int(p->socketNotifier->type())]); // ### note, modifies src->pollfds! p->socketNotifier->setEnabled(false); } pending = ((p->pollfd.revents & p->pollfd.events) != 0); } return pending;}static gboolean socketNotifierSourceDispatch(GSource *source, GSourceFunc, gpointer){ QEvent event(QEvent::SockAct); GSocketNotifierSource *src = reinterpret_cast<GSocketNotifierSource *>(source); for (int i = 0; i < src->pollfds.count(); ++i) { GPollFDWithQSocketNotifier *p = src->pollfds.at(i); if ((p->pollfd.revents & p->pollfd.events) != 0) QCoreApplication::sendEvent(p->socketNotifier, &event); } return true; // ??? don't remove, right?}static GSourceFuncs socketNotifierSourceFuncs = { socketNotifierSourcePrepare, socketNotifierSourceCheck, socketNotifierSourceDispatch, NULL, NULL, NULL};struct GTimerSource{ GSource source; QTimerInfoList timerList;};static gboolean timerSourcePrepare(GSource *source, gint *timeout){ gint dummy; if (!timeout) timeout = &dummy; GTimerSource *src = reinterpret_cast<GTimerSource *>(source); timeval tv = { 0l, 0l }; if (src->timerList.timerWait(tv)) *timeout = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); else *timeout = -1; return false;}static gboolean timerSourceCheck(GSource *source){ GTimerSource *src = reinterpret_cast<GTimerSource *>(source); if (src->timerList.isEmpty()) return false; timeval currentTime; getTime(currentTime); if (currentTime < src->timerList.first()->timeout) return false; return true;}static gboolean timerSourceDispatch(GSource *source, GSourceFunc, gpointer){ GTimerSource *src = reinterpret_cast<GTimerSource *>(source); bool first = true; timeval currentTime; int n_act = 0, maxCount = src->timerList.size(); QTimerInfo *begin = 0; while (maxCount--) { getTime(currentTime); if (first) { src->timerList.updateWatchTime(currentTime); first = false; } if (src->timerList.isEmpty()) break; QTimerInfo *t = src->timerList.first(); if (currentTime < t->timeout) break; // no timer has expired if (!begin) { begin = t; } else if (begin == t) { // avoid sending the same timer multiple times break; } else if (t->interval < begin->interval || t->interval == begin->interval) { begin = t; } // remove from list src->timerList.removeFirst(); t->timeout += t->interval; if (t->timeout < currentTime) t->timeout = currentTime + t->interval; // reinsert timer src->timerList.timerInsert(t); if (t->interval.tv_usec > 0 || t->interval.tv_sec > 0) n_act++; if (!t->inTimerEvent) { // send event, but don't allow it to recurse t->inTimerEvent = true; QTimerEvent e(t->id); QCoreApplication::sendEvent(t->obj, &e); if (src->timerList.contains(t)) t->inTimerEvent = false; } if (!src->timerList.contains(begin)) begin = 0; } return true; // ??? don't remove, right again?}static GSourceFuncs timerSourceFuncs = { timerSourcePrepare, timerSourceCheck, timerSourceDispatch, NULL, NULL, NULL};struct GPostEventSource{ GSource source; GPollFD pollfd; int wakeUpPipe[2]; QEventLoop::ProcessEventsFlags flags, previousFlags;};static gboolean postEventSourcePrepare(GSource *s, gint *timeout){ QThreadData *data = QThreadData::current(); if (!data) return false; gint dummy; if (!timeout) timeout = &dummy; *timeout = data->canWait ? -1 : 0; GPostEventSource *source = reinterpret_cast<GPostEventSource *>(s); return (!data->canWait || (source->flags != source->previousFlags));}static gboolean postEventSourceCheck(GSource *source){ return (postEventSourcePrepare(source, 0) || reinterpret_cast<GPostEventSource *>(source)->pollfd.revents != 0);}static gboolean postEventSourceDispatch(GSource *s, GSourceFunc, gpointer){ GPostEventSource *source = reinterpret_cast<GPostEventSource *>(s); char c[16]; while (::read(source->wakeUpPipe[0], c, sizeof(c)) > 0) ; source->previousFlags = source->flags; QCoreApplication::sendPostedEvents(0, (source->flags & QEventLoop::DeferredDeletion) ? -1 : 0); return true; // i dunno, george...}static GSourceFuncs postEventSourceFuncs = { postEventSourcePrepare, postEventSourceCheck, postEventSourceDispatch, NULL, NULL, NULL};QEventDispatcherGlibPrivate::QEventDispatcherGlibPrivate(){ QCoreApplication *app = QCoreApplication::instance(); if (app && QThread::currentThread() == app->thread()) { mainContext = g_main_context_default(); g_main_context_ref(mainContext); } else { mainContext = g_main_context_new(); } postEventSource = reinterpret_cast<GPostEventSource *>(g_source_new(&postEventSourceFuncs, sizeof(GPostEventSource))); g_source_set_can_recurse(&postEventSource->source, true); pipe(postEventSource->wakeUpPipe); fcntl(postEventSource->wakeUpPipe[0], F_SETFD, FD_CLOEXEC); fcntl(postEventSource->wakeUpPipe[1], F_SETFD, FD_CLOEXEC); fcntl(postEventSource->wakeUpPipe[0], F_SETFL, fcntl(postEventSource->wakeUpPipe[0], F_GETFL) | O_NONBLOCK);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -