| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qeventdispatcher_glib_p.h" |
| 5 | |
| 6 | #include "qguiapplication.h" |
| 7 | |
| 8 | #include "qplatformdefs.h" |
| 9 | |
| 10 | #include <glib.h> |
| 11 | #include "private/qguiapplication_p.h" |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | struct GUserEventSource |
| 16 | { |
| 17 | GSource source; |
| 18 | QPAEventDispatcherGlib *q; |
| 19 | QPAEventDispatcherGlibPrivate *d; |
| 20 | }; |
| 21 | |
| 22 | static gboolean userEventSourcePrepare(GSource *source, gint *timeout) |
| 23 | { |
| 24 | Q_UNUSED(timeout); |
| 25 | GUserEventSource *userEventSource = reinterpret_cast<GUserEventSource *>(source); |
| 26 | return userEventSource->d->wakeUpCalled; |
| 27 | } |
| 28 | |
| 29 | static gboolean userEventSourceCheck(GSource *source) |
| 30 | { |
| 31 | return userEventSourcePrepare(source, timeout: nullptr); |
| 32 | } |
| 33 | |
| 34 | static gboolean userEventSourceDispatch(GSource *source, GSourceFunc, gpointer) |
| 35 | { |
| 36 | GUserEventSource *userEventSource = reinterpret_cast<GUserEventSource *>(source); |
| 37 | QPAEventDispatcherGlib *dispatcher = userEventSource->q; |
| 38 | QWindowSystemInterface::sendWindowSystemEvents(flags: dispatcher->m_flags); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | static GSourceFuncs userEventSourceFuncs = { |
| 43 | .prepare: userEventSourcePrepare, |
| 44 | .check: userEventSourceCheck, |
| 45 | .dispatch: userEventSourceDispatch, |
| 46 | NULL, |
| 47 | NULL, |
| 48 | NULL |
| 49 | }; |
| 50 | |
| 51 | QPAEventDispatcherGlibPrivate::QPAEventDispatcherGlibPrivate(GMainContext *context) |
| 52 | : QEventDispatcherGlibPrivate(context) |
| 53 | { |
| 54 | Q_Q(QPAEventDispatcherGlib); |
| 55 | |
| 56 | GSource *source = g_source_new(source_funcs: &userEventSourceFuncs, struct_size: sizeof(GUserEventSource)); |
| 57 | g_source_set_name(source, name: "[Qt] GUserEventSource" ); |
| 58 | userEventSource = reinterpret_cast<GUserEventSource *>(source); |
| 59 | |
| 60 | userEventSource->q = q; |
| 61 | userEventSource->d = this; |
| 62 | g_source_set_can_recurse(source: &userEventSource->source, can_recurse: true); |
| 63 | g_source_attach(source: &userEventSource->source, context: mainContext); |
| 64 | } |
| 65 | |
| 66 | QPAEventDispatcherGlibPrivate::~QPAEventDispatcherGlibPrivate() |
| 67 | = default; |
| 68 | |
| 69 | QPAEventDispatcherGlib::QPAEventDispatcherGlib(QObject *parent) |
| 70 | : QEventDispatcherGlib(*new QPAEventDispatcherGlibPrivate, parent) |
| 71 | , m_flags(QEventLoop::AllEvents) |
| 72 | { |
| 73 | Q_D(QPAEventDispatcherGlib); |
| 74 | d->userEventSource->q = this; |
| 75 | } |
| 76 | |
| 77 | QPAEventDispatcherGlib::~QPAEventDispatcherGlib() |
| 78 | { |
| 79 | Q_D(QPAEventDispatcherGlib); |
| 80 | |
| 81 | g_source_destroy(source: &d->userEventSource->source); |
| 82 | g_source_unref(source: &d->userEventSource->source); |
| 83 | d->userEventSource = nullptr; |
| 84 | } |
| 85 | |
| 86 | bool QPAEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags) |
| 87 | { |
| 88 | m_flags = flags; |
| 89 | return QEventDispatcherGlib::processEvents(flags: m_flags); |
| 90 | } |
| 91 | |
| 92 | QT_END_NAMESPACE |
| 93 | |
| 94 | #include "moc_qeventdispatcher_glib_p.cpp" |
| 95 | |