1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qeventdispatcher_glib_p.h"
41
42#include "qguiapplication.h"
43
44#include "qplatformdefs.h"
45
46#include <glib.h>
47#include "private/qguiapplication_p.h"
48
49QT_BEGIN_NAMESPACE
50
51struct GUserEventSource
52{
53 GSource source;
54 QPAEventDispatcherGlib *q;
55 QPAEventDispatcherGlibPrivate *d;
56};
57
58static gboolean userEventSourcePrepare(GSource *source, gint *timeout)
59{
60 Q_UNUSED(timeout);
61 GUserEventSource *userEventSource = reinterpret_cast<GUserEventSource *>(source);
62 return userEventSource->d->wakeUpCalled;
63}
64
65static gboolean userEventSourceCheck(GSource *source)
66{
67 return userEventSourcePrepare(source, nullptr);
68}
69
70static gboolean userEventSourceDispatch(GSource *source, GSourceFunc, gpointer)
71{
72 GUserEventSource *userEventSource = reinterpret_cast<GUserEventSource *>(source);
73 QPAEventDispatcherGlib *dispatcher = userEventSource->q;
74 QWindowSystemInterface::sendWindowSystemEvents(dispatcher->m_flags);
75 return true;
76}
77
78static GSourceFuncs userEventSourceFuncs = {
79 userEventSourcePrepare,
80 userEventSourceCheck,
81 userEventSourceDispatch,
82 NULL,
83 NULL,
84 NULL
85};
86
87QPAEventDispatcherGlibPrivate::QPAEventDispatcherGlibPrivate(GMainContext *context)
88 : QEventDispatcherGlibPrivate(context)
89{
90 Q_Q(QPAEventDispatcherGlib);
91
92 GSource *source = g_source_new(&userEventSourceFuncs, sizeof(GUserEventSource));
93 g_source_set_name(source, "[Qt] GUserEventSource");
94 userEventSource = reinterpret_cast<GUserEventSource *>(source);
95
96 userEventSource->q = q;
97 userEventSource->d = this;
98 g_source_set_can_recurse(&userEventSource->source, true);
99 g_source_attach(&userEventSource->source, mainContext);
100}
101
102
103QPAEventDispatcherGlib::QPAEventDispatcherGlib(QObject *parent)
104 : QEventDispatcherGlib(*new QPAEventDispatcherGlibPrivate, parent)
105 , m_flags(QEventLoop::AllEvents)
106{
107 Q_D(QPAEventDispatcherGlib);
108 d->userEventSource->q = this;
109}
110
111QPAEventDispatcherGlib::~QPAEventDispatcherGlib()
112{
113 Q_D(QPAEventDispatcherGlib);
114
115 g_source_destroy(&d->userEventSource->source);
116 g_source_unref(&d->userEventSource->source);
117 d->userEventSource = nullptr;
118}
119
120bool QPAEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags)
121{
122 m_flags = flags;
123 return QEventDispatcherGlib::processEvents(m_flags);
124}
125
126QT_END_NAMESPACE
127