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
13QT_BEGIN_NAMESPACE
14
15struct GUserEventSource
16{
17 GSource source;
18 QPAEventDispatcherGlib *q;
19 QPAEventDispatcherGlibPrivate *d;
20};
21
22static 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
29static gboolean userEventSourceCheck(GSource *source)
30{
31 return userEventSourcePrepare(source, timeout: nullptr);
32}
33
34static 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
42static GSourceFuncs userEventSourceFuncs = {
43 .prepare: userEventSourcePrepare,
44 .check: userEventSourceCheck,
45 .dispatch: userEventSourceDispatch,
46 NULL,
47 NULL,
48 NULL
49};
50
51QPAEventDispatcherGlibPrivate::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
67QPAEventDispatcherGlib::QPAEventDispatcherGlib(QObject *parent)
68 : QEventDispatcherGlib(*new QPAEventDispatcherGlibPrivate, parent)
69 , m_flags(QEventLoop::AllEvents)
70{
71 Q_D(QPAEventDispatcherGlib);
72 d->userEventSource->q = this;
73}
74
75QPAEventDispatcherGlib::~QPAEventDispatcherGlib()
76{
77 Q_D(QPAEventDispatcherGlib);
78
79 g_source_destroy(source: &d->userEventSource->source);
80 g_source_unref(source: &d->userEventSource->source);
81 d->userEventSource = nullptr;
82}
83
84bool QPAEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags)
85{
86 m_flags = flags;
87 return QEventDispatcherGlib::processEvents(flags: m_flags);
88}
89
90QT_END_NAMESPACE
91
92#include "moc_qeventdispatcher_glib_p.cpp"
93

source code of qtbase/src/gui/platform/unix/qeventdispatcher_glib.cpp