Don't send posted events from QWindowSystemInterface::sendWindowSystemEvents

The responsibility of sendWindowSystemEvents() is to process events from
the window system. Historially that logic was part of the QPA/QWS event
dispatcher, which naturally also sent posted events. Through refactoring,
the code at some point ended up in in the QWindowSystemInterface class,
still with the posting of events in place.

This resulted in QPA event dispatchers adopting a pattern of just calling
sendWindowSystemEvents(), as that would cover both posted and window system
events. Other event dispatchers would call sendWindowSystemEvents(), and
then use a base-class implementation from QtCore for processing events,
resulting in two calls to QCoreApplication::sendPostedEvents() per
iteration of processEvents(). This breaks the contract that processEvents
will only process posted events that has been queued up until then.

We fix this entanglement by removing the sendPostedEvents() call from
QWindowSystemInterface::sendWindowSystemEvents() and move it to the
respective event dispatchers. For some EDs it means an explicit call
to sendPostedEvents, while others were already doing sendPostedEvents
though a separate source (GLib), or using a base-class (UNIX/BB), and
did not need an extra call.

We still keep the ordering of the original sendWindowSystemEvents()
function of first sending posted events, and then processing any
window system events.

Task-number: QTBUG-33485
Change-Id: I8b069e76cea1f37875e72a034c11d09bf3fe166a
Reviewed-by: Paul Olav Tvete <paul.tvete@digia.com>
This commit is contained in:
Tor Arne Vestbø 2013-09-16 12:52:46 +02:00 committed by The Qt Project
parent 4c19055cb1
commit 4bae7158d3
9 changed files with 64 additions and 30 deletions

View File

@ -553,22 +553,11 @@ void QWindowSystemInterface::flushWindowSystemEvents()
QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
QWindowSystemInterfacePrivate::eventsFlushed.wait(&QWindowSystemInterfacePrivate::flushEventMutex);
} else {
sendWindowSystemEventsImplementation(QEventLoop::AllEvents);
sendWindowSystemEvents(QEventLoop::AllEvents);
}
}
bool QWindowSystemInterface::sendWindowSystemEvents(QEventLoop::ProcessEventsFlags flags)
{
QCoreApplication::sendPostedEvents(); // handle gui and posted events
return sendWindowSystemEventsImplementation(flags);
}
void QWindowSystemInterface::setSynchronousWindowsSystemEvents(bool enable)
{
QWindowSystemInterfacePrivate::synchronousWindowsSystemEvents = enable;
}
bool QWindowSystemInterface::sendWindowSystemEventsImplementation(QEventLoop::ProcessEventsFlags flags)
{
int nevents = 0;
@ -587,6 +576,11 @@ bool QWindowSystemInterface::sendWindowSystemEventsImplementation(QEventLoop::Pr
return (nevents > 0);
}
void QWindowSystemInterface::setSynchronousWindowsSystemEvents(bool enable)
{
QWindowSystemInterfacePrivate::synchronousWindowsSystemEvents = enable;
}
int QWindowSystemInterface::windowSystemEventsQueued()
{
return QWindowSystemInterfacePrivate::windowSystemEventsQueued();

View File

@ -193,9 +193,6 @@ public:
static void flushWindowSystemEvents();
static void deferredFlushWindowSystemEvents();
static int windowSystemEventsQueued();
private:
static bool sendWindowSystemEventsImplementation(QEventLoop::ProcessEventsFlags flags);
};
#ifndef QT_NO_DEBUG_STREAM

View File

@ -388,6 +388,10 @@ void QEventDispatcherCoreFoundation::processPostedEvents()
m_processEvents.processedPostedEvents = true;
qEventDispatcherDebug() << "Sending posted events for " << m_processEvents.flags; qIndent();
QCoreApplication::sendPostedEvents();
qUnIndent();
qEventDispatcherDebug() << "Sending window system events for " << m_processEvents.flags; qIndent();
QWindowSystemInterface::sendWindowSystemEvents(m_processEvents.flags);
qUnIndent();

View File

@ -67,13 +67,8 @@ QUnixEventDispatcherQPA::~QUnixEventDispatcherQPA()
bool QUnixEventDispatcherQPA::processEvents(QEventLoop::ProcessEventsFlags flags)
{
const bool didSendEvents = QWindowSystemInterface::sendWindowSystemEvents(flags);
if (QEventDispatcherUNIX::processEvents(flags)) {
return true;
}
return didSendEvents;
const bool didSendEvents = QEventDispatcherUNIX::processEvents(flags);
return QWindowSystemInterface::sendWindowSystemEvents(flags) || didSendEvents;
}
bool QUnixEventDispatcherQPA::hasPendingEvents()

View File

@ -876,6 +876,7 @@ void QCocoaEventDispatcherPrivate::processPostedEvents()
int serial = serialNumber.load();
if (!threadData->canWait || (serial != lastSerial)) {
lastSerial = serial;
QCoreApplication::sendPostedEvents();
QWindowSystemInterface::sendWindowSystemEvents(QEventLoop::AllEvents);
}
}

View File

@ -73,9 +73,9 @@ public:
bool processEvents(QEventLoop::ProcessEventsFlags flags)
{
bool didSendEvents = QWindowSystemInterface::sendWindowSystemEvents(flags);
bool didSendEvents = BaseEventDispatcher::processEvents(flags);
return BaseEventDispatcher::processEvents(flags) || didSendEvents;
return QWindowSystemInterface::sendWindowSystemEvents(flags) || didSendEvents;
}
bool hasPendingEvents()

View File

@ -58,12 +58,8 @@ QQnxEventDispatcherBlackberry::~QQnxEventDispatcherBlackberry()
bool QQnxEventDispatcherBlackberry::processEvents(QEventLoop::ProcessEventsFlags flags)
{
const bool didSendEvents = QWindowSystemInterface::sendWindowSystemEvents(flags);
if (QEventDispatcherBlackberry::processEvents(flags))
return true;
return didSendEvents;
const bool didSendEvents = QEventDispatcherBlackberry::processEvents(flags);
return QWindowSystemInterface::sendWindowSystemEvents(flags) || didSendEvents;
}
bool QQnxEventDispatcherBlackberry::hasPendingEvents()

View File

@ -44,6 +44,7 @@
#include <qpa/qwindowsysteminterface.h>
#include <QtCore/QCoreApplication>
#include <QtCore/QStack>
#include <QtCore/QDebug>
@ -83,6 +84,7 @@ bool QWindowsGuiEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags fl
void QWindowsGuiEventDispatcher::sendPostedEvents()
{
QCoreApplication::sendPostedEvents();
QWindowSystemInterface::sendWindowSystemEvents(m_flags);
}

View File

@ -78,6 +78,7 @@ private slots:
/* void registerEventNotifiier(); */ // Not implemented here, see tst_QWinEventNotifier instead
void sendPostedEvents_data();
void sendPostedEvents();
void processEventsOnlySendsQueuedEvents();
};
bool tst_QEventDispatcher::event(QEvent *e)
@ -207,5 +208,49 @@ void tst_QEventDispatcher::sendPostedEvents()
}
}
class ProcessEventsOnlySendsQueuedEvents : public QObject
{
Q_OBJECT
public:
int eventsReceived;
inline ProcessEventsOnlySendsQueuedEvents() : eventsReceived(0) {}
bool event(QEvent *event)
{
++eventsReceived;
if (event->type() == QEvent::User)
QCoreApplication::postEvent(this, new QEvent(QEvent::Type(QEvent::User + 1)));
return QObject::event(event);
}
public slots:
void timerFired()
{
QCoreApplication::postEvent(this, new QEvent(QEvent::Type(QEvent::User + 1)));
}
};
void tst_QEventDispatcher::processEventsOnlySendsQueuedEvents()
{
ProcessEventsOnlySendsQueuedEvents object;
// Posted events during event processing should be handled on
// the next processEvents iteration.
QCoreApplication::postEvent(&object, new QEvent(QEvent::User));
QCoreApplication::processEvents();
QCOMPARE(object.eventsReceived, 1);
QCoreApplication::processEvents();
QCOMPARE(object.eventsReceived, 2);
// The same goes for posted events during timer processing
QTimer::singleShot(0, &object, SLOT(timerFired()));
QCoreApplication::processEvents();
QCOMPARE(object.eventsReceived, 3);
QCoreApplication::processEvents();
QCOMPARE(object.eventsReceived, 4);
}
QTEST_MAIN(tst_QEventDispatcher)
#include "tst_qeventdispatcher.moc"