1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 BogDan Vatra <bogdan@kde.org>
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 "qandroideventdispatcher.h"
41 #include "androidjnimain.h"
42 #include "androiddeadlockprotector.h"
43
QAndroidEventDispatcher(QObject * parent)44 QAndroidEventDispatcher::QAndroidEventDispatcher(QObject *parent) :
45 QUnixEventDispatcherQPA(parent)
46 {
47 if (QtAndroid::blockEventLoopsWhenSuspended())
48 QAndroidEventDispatcherStopper::instance()->addEventDispatcher(this);
49 }
50
~QAndroidEventDispatcher()51 QAndroidEventDispatcher::~QAndroidEventDispatcher()
52 {
53 if (QtAndroid::blockEventLoopsWhenSuspended())
54 QAndroidEventDispatcherStopper::instance()->removeEventDispatcher(this);
55 }
56
57 enum States {Running = 0, StopRequest = 1, Stopping = 2};
58
start()59 void QAndroidEventDispatcher::start()
60 {
61 int prevState = m_stopRequest.fetchAndStoreAcquire(Running);
62 if (prevState == Stopping) {
63 m_semaphore.release();
64 wakeUp();
65 } else if (prevState == Running) {
66 qWarning("Error: start without corresponding stop");
67 }
68 //else if prevState == StopRequest, no action needed
69 }
70
stop()71 void QAndroidEventDispatcher::stop()
72 {
73 if (m_stopRequest.testAndSetAcquire(Running, StopRequest))
74 wakeUp();
75 else
76 qWarning("Error: start/stop out of sync");
77 }
78
goingToStop(bool stop)79 void QAndroidEventDispatcher::goingToStop(bool stop)
80 {
81 m_goingToStop.storeRelaxed(stop ? 1 : 0);
82 if (!stop)
83 wakeUp();
84 }
85
processEvents(QEventLoop::ProcessEventsFlags flags)86 bool QAndroidEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
87 {
88 if (m_goingToStop.loadRelaxed())
89 flags |= QEventLoop::ExcludeSocketNotifiers | QEventLoop::X11ExcludeTimers;
90
91 {
92 AndroidDeadlockProtector protector;
93 if (protector.acquire() && m_stopRequest.testAndSetAcquire(StopRequest, Stopping)) {
94 m_semaphore.acquire();
95 wakeUp();
96 }
97 }
98
99 return QUnixEventDispatcherQPA::processEvents(flags);
100 }
101
instance()102 QAndroidEventDispatcherStopper *QAndroidEventDispatcherStopper::instance()
103 {
104 static QAndroidEventDispatcherStopper androidEventDispatcherStopper;
105 return &androidEventDispatcherStopper;
106 }
107
startAll()108 void QAndroidEventDispatcherStopper::startAll()
109 {
110 QMutexLocker lock(&m_mutex);
111 if (!m_started.testAndSetOrdered(0, 1))
112 return;
113
114 for (QAndroidEventDispatcher *d : qAsConst(m_dispatchers))
115 d->start();
116 }
117
stopAll()118 void QAndroidEventDispatcherStopper::stopAll()
119 {
120 QMutexLocker lock(&m_mutex);
121 if (!m_started.testAndSetOrdered(1, 0))
122 return;
123
124 for (QAndroidEventDispatcher *d : qAsConst(m_dispatchers))
125 d->stop();
126 }
127
addEventDispatcher(QAndroidEventDispatcher * dispatcher)128 void QAndroidEventDispatcherStopper::addEventDispatcher(QAndroidEventDispatcher *dispatcher)
129 {
130 QMutexLocker lock(&m_mutex);
131 m_dispatchers.push_back(dispatcher);
132 }
133
removeEventDispatcher(QAndroidEventDispatcher * dispatcher)134 void QAndroidEventDispatcherStopper::removeEventDispatcher(QAndroidEventDispatcher *dispatcher)
135 {
136 QMutexLocker lock(&m_mutex);
137 m_dispatchers.erase(std::find(m_dispatchers.begin(), m_dispatchers.end(), dispatcher));
138 }
139
goingToStop(bool stop)140 void QAndroidEventDispatcherStopper::goingToStop(bool stop)
141 {
142 QMutexLocker lock(&m_mutex);
143 for (QAndroidEventDispatcher *d : qAsConst(m_dispatchers))
144 d->goingToStop(stop);
145 }
146