diff options
Diffstat (limited to 'src/corelib/kernel/qeventdispatcher_win.cpp')
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_win.cpp | 172 |
1 files changed, 61 insertions, 111 deletions
diff --git a/src/corelib/kernel/qeventdispatcher_win.cpp b/src/corelib/kernel/qeventdispatcher_win.cpp index 8ff7637b7e..87623f304a 100644 --- a/src/corelib/kernel/qeventdispatcher_win.cpp +++ b/src/corelib/kernel/qeventdispatcher_win.cpp @@ -52,7 +52,6 @@ #include "qelapsedtimer.h" #include "qcoreapplication_p.h" #include <private/qthread_p.h> -#include <private/qmutexpool_p.h> #include <private/qwineventnotifier_p.h> QT_BEGIN_NAMESPACE @@ -82,11 +81,13 @@ extern uint qGlobalPostedEventsCount(); enum { WM_QT_SOCKETNOTIFIER = WM_USER, WM_QT_SENDPOSTEDEVENTS = WM_USER + 1, - WM_QT_ACTIVATENOTIFIERS = WM_USER + 2, + WM_QT_ACTIVATENOTIFIERS = WM_USER + 2 }; -enum : UINT_PTR { - SendPostedEventsWindowsTimerId = ~UINT_PTR(1) +// WM_QT_SENDPOSTEDEVENTS message parameter +enum { + WMWP_QT_TOFOREIGNLOOP = 0, + WMWP_QT_FROMWAKEUP }; class QEventDispatcherWin32Private; @@ -99,8 +100,8 @@ LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPA QEventDispatcherWin32Private::QEventDispatcherWin32Private() : threadId(GetCurrentThreadId()), interrupt(false), internalHwnd(0), - getMessageHook(0), serialNumber(0), lastSerialNumber(0), sendPostedEventsWindowsTimerId(0), - wakeUps(0), activateNotifiersPosted(false), winEventNotifierActivatedEvent(NULL) + getMessageHook(0), wakeUps(0), activateNotifiersPosted(false), + winEventNotifierActivatedEvent(NULL) { } @@ -139,7 +140,11 @@ LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPA msg.wParam = wp; msg.lParam = lp; QAbstractEventDispatcher* dispatcher = QAbstractEventDispatcher::instance(); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + qintptr result; +#else long result; +#endif if (!dispatcher) { if (message == WM_TIMER) KillTimer(hwnd, wp); @@ -233,30 +238,25 @@ LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPA return 0; } case WM_TIMER: - if (d->sendPostedEventsWindowsTimerId == 0 - || wp != uint(d->sendPostedEventsWindowsTimerId)) { - Q_ASSERT(d != 0); - d->sendTimerEvent(wp); - return 0; - } - // we also use a Windows timer to send posted events when the message queue is full - Q_FALLTHROUGH(); - case WM_QT_SENDPOSTEDEVENTS: { - const int localSerialNumber = d->serialNumber.load(); - if (localSerialNumber != d->lastSerialNumber) { - d->lastSerialNumber = localSerialNumber; - q->sendPostedEvents(); - } + Q_ASSERT(d != 0); + + d->sendTimerEvent(wp); + return 0; + case WM_QT_SENDPOSTEDEVENTS: + Q_ASSERT(d != 0); + + // We send posted events manually, if the window procedure was invoked + // by the foreign event loop (e.g. from the native modal dialog). + q->sendPostedEvents(); return 0; - } } // switch (message) return DefWindowProc(hwnd, message, wp, lp); } -static inline UINT inputTimerMask() +static inline UINT inputQueueMask() { - UINT result = QS_TIMER | QS_INPUT | QS_RAWINPUT; + UINT result = QS_ALLEVENTS; // QTBUG 28513, QTBUG-29097, QTBUG-29435: QS_TOUCH, QS_POINTER became part of // QS_INPUT in Windows Kit 8. They should not be used when running on pre-Windows 8. #if WINVER > 0x0601 @@ -270,41 +270,19 @@ LRESULT QT_WIN_CALLBACK qt_GetMessageHook(int code, WPARAM wp, LPARAM lp) { QEventDispatcherWin32 *q = qobject_cast<QEventDispatcherWin32 *>(QAbstractEventDispatcher::instance()); Q_ASSERT(q != 0); - - if (wp == PM_REMOVE) { - if (q) { - MSG *msg = (MSG *) lp; - QEventDispatcherWin32Private *d = q->d_func(); - const int localSerialNumber = d->serialNumber.load(); - static const UINT mask = inputTimerMask(); - if (HIWORD(GetQueueStatus(mask)) == 0) { - // no more input or timer events in the message queue, we can allow posted events to be sent normally now - if (d->sendPostedEventsWindowsTimerId != 0) { - // stop the timer to send posted events, since we now allow the WM_QT_SENDPOSTEDEVENTS message - KillTimer(d->internalHwnd, SendPostedEventsWindowsTimerId); - d->sendPostedEventsWindowsTimerId = 0; - } - (void) d->wakeUps.fetchAndStoreRelease(0); - if (localSerialNumber != d->lastSerialNumber - // if this message IS the one that triggers sendPostedEvents(), no need to post it again - && (msg->hwnd != d->internalHwnd - || msg->message != WM_QT_SENDPOSTEDEVENTS)) { - PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, 0, 0); - } - } else if (d->sendPostedEventsWindowsTimerId == 0 - && localSerialNumber != d->lastSerialNumber) { - // start a special timer to continue delivering posted events while - // there are still input and timer messages in the message queue - d->sendPostedEventsWindowsTimerId = SetTimer(d->internalHwnd, - SendPostedEventsWindowsTimerId, - 0, // we specify zero, but Windows uses USER_TIMER_MINIMUM - NULL); - // we don't check the return value of SetTimer()... if creating the timer failed, there's little - // we can do. we just have to accept that posted events will be starved - } + QEventDispatcherWin32Private *d = q->d_func(); + MSG *msg = reinterpret_cast<MSG *>(lp); + static const UINT mask = inputQueueMask(); + + if (HIWORD(GetQueueStatus(mask)) == 0 && wp == PM_REMOVE) { + // Allow posting WM_QT_SENDPOSTEDEVENTS message. + d->wakeUps.storeRelaxed(0); + if (!(msg->hwnd == d->internalHwnd && msg->message == WM_QT_SENDPOSTEDEVENTS)) { + PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, + WMWP_QT_TOFOREIGNLOOP, 0); } } - return q->d_func()->getMessageHook ? CallNextHookEx(0, code, wp, lp) : 0; + return d->getMessageHook ? CallNextHookEx(0, code, wp, lp) : 0; } // Provide class name and atom for the message window used by @@ -341,7 +319,7 @@ QWindowsMessageWindowClassContext::QWindowsMessageWindowClassContext() wc.lpszClassName = className; atom = RegisterClass(&wc); if (!atom) { - qErrnoWarning("%s RegisterClass() failed", qPrintable(qClassName)); + qErrnoWarning("%ls RegisterClass() failed", qUtf16Printable(qClassName)); delete [] className; className = 0; } @@ -485,36 +463,17 @@ void QEventDispatcherWin32::createInternalHwnd() return; d->internalHwnd = qt_create_internal_window(this); - installMessageHook(); - - // start all normal timers - for (int i = 0; i < d->timerVec.count(); ++i) - d->registerTimer(d->timerVec.at(i)); -} - -void QEventDispatcherWin32::installMessageHook() -{ - Q_D(QEventDispatcherWin32); - - if (d->getMessageHook) - return; - // setup GetMessage hook needed to drive our posted events d->getMessageHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC) qt_GetMessageHook, NULL, GetCurrentThreadId()); if (Q_UNLIKELY(!d->getMessageHook)) { int errorCode = GetLastError(); - qFatal("Qt: INTERNAL ERROR: failed to install GetMessage hook: %d, %s", - errorCode, qPrintable(qt_error_string(errorCode))); + qFatal("Qt: INTERNAL ERROR: failed to install GetMessage hook: %d, %ls", + errorCode, qUtf16Printable(qt_error_string(errorCode))); } -} - -void QEventDispatcherWin32::uninstallMessageHook() -{ - Q_D(QEventDispatcherWin32); - if (d->getMessageHook) - UnhookWindowsHookEx(d->getMessageHook); - d->getMessageHook = 0; + // start all normal timers + for (int i = 0; i < d->timerVec.count(); ++i) + d->registerTimer(d->timerVec.at(i)); } QEventDispatcherWin32::QEventDispatcherWin32(QObject *parent) @@ -555,13 +514,15 @@ bool QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags flags) wakeUp(); // trigger a call to sendPostedEvents() } - d->interrupt.store(false); + d->interrupt.storeRelaxed(false); emit awake(); + // To prevent livelocks, send posted events once per iteration. + // QCoreApplication::sendPostedEvents() takes care about recursions. + sendPostedEvents(); + bool canWait; bool retVal = false; - bool seenWM_QT_SENDPOSTEDEVENTS = false; - bool needWM_QT_SENDPOSTEDEVENTS = false; do { DWORD waitRet = 0; DWORD nCount = 0; @@ -571,7 +532,7 @@ bool QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags flags) pHandles = &d->winEventNotifierActivatedEvent; } QVarLengthArray<MSG> processedTimers; - while (!d->interrupt.load()) { + while (!d->interrupt.loadRelaxed()) { MSG msg; bool haveMessage; @@ -609,19 +570,13 @@ bool QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags flags) } } if (haveMessage) { - // WinCE doesn't support hooks at all, so we have to call this by hand :( - if (!d->getMessageHook) - (void) qt_GetMessageHook(0, PM_REMOVE, reinterpret_cast<LPARAM>(&msg)); - if (d->internalHwnd == msg.hwnd && msg.message == WM_QT_SENDPOSTEDEVENTS) { - if (seenWM_QT_SENDPOSTEDEVENTS) { - // when calling processEvents() "manually", we only want to send posted - // events once - needWM_QT_SENDPOSTEDEVENTS = true; - continue; - } - seenWM_QT_SENDPOSTEDEVENTS = true; - } else if (msg.message == WM_TIMER) { + // Set result to 'true', if the message was sent by wakeUp(). + if (msg.wParam == WMWP_QT_FROMWAKEUP) + retVal = true; + continue; + } + if (msg.message == WM_TIMER) { // avoid live-lock by keeping track of the timers we've already sent bool found = false; for (int i = 0; !found && i < processedTimers.count(); ++i) { @@ -652,7 +607,7 @@ bool QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags flags) // still nothing - wait for message or signalled objects canWait = (!retVal - && !d->interrupt.load() + && !d->interrupt.loadRelaxed() && (flags & QEventLoop::WaitForMoreEvents)); if (canWait) { emit aboutToBlock(); @@ -665,14 +620,6 @@ bool QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags flags) } } while (canWait); - if (!seenWM_QT_SENDPOSTEDEVENTS && (flags & QEventLoop::EventLoopExec) == 0) { - // when called "manually", always send posted events - sendPostedEvents(); - } - - if (needWM_QT_SENDPOSTEDEVENTS) - PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, 0, 0); - return retVal; } @@ -961,7 +908,7 @@ void QEventDispatcherWin32::activateEventNotifiers() for (int i = 0; i < d->winEventNotifierList.count(); ++i) { QWinEventNotifier *notifier = d->winEventNotifierList.at(i); QWinEventNotifierPrivate *nd = QWinEventNotifierPrivate::get(notifier); - if (nd->signaledCount.load() != 0) { + if (nd->signaledCount.loadRelaxed() != 0) { --nd->signaledCount; nd->unregisterWaitObject(); d->activateEventNotifier(notifier); @@ -1015,17 +962,18 @@ int QEventDispatcherWin32::remainingTime(int timerId) void QEventDispatcherWin32::wakeUp() { Q_D(QEventDispatcherWin32); - d->serialNumber.ref(); if (d->internalHwnd && d->wakeUps.testAndSetAcquire(0, 1)) { // post a WM_QT_SENDPOSTEDEVENTS to this thread if there isn't one already pending - PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, 0, 0); + if (!PostMessage(d->internalHwnd, WM_QT_SENDPOSTEDEVENTS, + WMWP_QT_FROMWAKEUP, 0)) + qErrnoWarning("QEventDispatcherWin32::wakeUp: Failed to post a message"); } } void QEventDispatcherWin32::interrupt() { Q_D(QEventDispatcherWin32); - d->interrupt.store(true); + d->interrupt.storeRelaxed(true); wakeUp(); } @@ -1056,7 +1004,9 @@ void QEventDispatcherWin32::closingDown() d->closingDown = true; - uninstallMessageHook(); + if (d->getMessageHook) + UnhookWindowsHookEx(d->getMessageHook); + d->getMessageHook = 0; } bool QEventDispatcherWin32::event(QEvent *e) |