summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/pepper/qpeppereventdispatcher.cpp
blob: 7af6b2d1618ccdf895e455ca939f382bf7182454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/****************************************************************************
**
** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt for Native Client platform plugin.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qpeppereventdispatcher.h"

#include <QtCore/QDebug>
#include <QtCore/QCoreApplication>

Q_LOGGING_CATEGORY(QT_PLATFORM_PEPPER_EVENTDISPATHCER, "qt.platform.pepper.eventdispatcher");

QPepperEventDispatcher::QPepperEventDispatcher(QObject *parent)
    : QUnixEventDispatcherQPA(parent)
    , m_currentTimerSerial(0)
    , m_messageLoop(pp::MessageLoop::GetCurrent())
    , m_completionCallbackFactory(this)
    , m_hasPendingProcessEvents(false)
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "QPepperEventDispatcher()";
}

QPepperEventDispatcher::~QPepperEventDispatcher() {}

bool QPepperEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
{
    bool processed = false;
#ifdef Q_OS_NACL_EMSCRIPTEN
    // We need to give the control back to the browser due to lack of PTHREADS
    // Limit the number of events that may be processed at the time
    // TODO: Set maxProcessedEvents to the actual number of pending events, the real issue is that events may spawn new events
    int maxProcessedEvents = 2;
    int processedCount = 0;
    do {
        processed = QUnixEventDispatcherQPA::processEvents(flags);
        processedCount += 1;
    } while (processed && hasPendingEvents() && processedCount < maxProcessedEvents);
    // Schedule a new processing loop if we still have events pending
    if (hasPendingEvents()) {
        scheduleProcessEvents();
    }
#else
    do {
        processed = QUnixEventDispatcherQPA::processEvents(flags);
    } while (processed && hasPendingEvents());
#endif
    return true;
}

bool QPepperEventDispatcher::hasPendingEvents()
{
    return QUnixEventDispatcherQPA::hasPendingEvents();
}

void QPepperEventDispatcher::registerTimer(int timerId, int interval, Qt::TimerType timerType,
                                           QObject *object)
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "QPepperEventDispatcher::registerTimer"
                                                << timerId << interval << object;

    // Maintain QObject *-> Qt timer id mapping.
    m_activeObjectTimers.insertMulti(object, timerId);

    // Capture timer detail for later use
    PepperTimerInfo timerInfo(timerId, interval, timerType, object);
    m_timerDetails.insert(timerId, timerInfo);

    startTimer(timerInfo);
}

bool QPepperEventDispatcher::unregisterTimer(int timerId)
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "QPepperEventDispatcher::unregisterTimer"
                                                << timerId;

    // Remove QObject -> Qt timer id mapping.
    QObject *timerObject = m_timerDetails.value(timerId).object;
    m_activeObjectTimers.remove(timerObject, timerId);

    m_timerDetails.remove(timerId);

    // Remove the timerId and the currently active serial.
    int timerSerial = m_activeTimerSerials.value(timerId);
    m_activeTimerSerials.remove(timerId);
    m_activeTimerIds.remove(timerSerial);

    return true;
}

bool QPepperEventDispatcher::unregisterTimers(QObject *object)
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "QPepperEventDispatcher::unregisterTimers"
                                                << object;

    // Find all active Qt timer ids for the given object and copy them to a list.
    // (we will update the QObject * -> timerId hash while iterating the list later on)
    QList<int> timerIds;
    QMultiHash<QObject *, int>::iterator it = m_activeObjectTimers.find(object);
    while (it != m_activeObjectTimers.end() && it.key() == object)
        timerIds.append(it.value());

    // Unregister each timer.
    foreach (int timerId, timerIds)
        unregisterTimer(timerId);

    return true;
}

void QPepperEventDispatcher::flush()
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "QPepperEventDispatcher::flush";

    QUnixEventDispatcherQPA::flush();
}

void QPepperEventDispatcher::wakeUp()
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "QPepperEventDispatcher::wakeup";
    scheduleProcessEvents();
}

void QPepperEventDispatcher::startTimer(PepperTimerInfo info)
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "startTimer" << info.timerId;

    // Allocate a timer serial value for this startTimer call.
    //
    // Qt reuses timer id's. This means there is time for Qt to unregister a
    // timer and register a new timer with the same id before our timer
    // callback is called.
    //
    // This is compounded by single-shot timer behavior: We are not informed
    // that a given timer is of the single-shot variant. Instead Qt will
    // unregister it during the timer fire event.
    //
    // This is further compounded by the fact that the Pepper timer API is of
    // the single-shot variant. After sending the timer event to Qt a decision
    // has to be made if the timer should be rescheduled or not.
    //
    // In summary: To avoid confusing separate uses of the same timer id we
    // give each call to CallOnMainThread a serial number which we can
    // independently cancel.
    //
    ++m_currentTimerSerial;
    m_activeTimerIds[m_currentTimerSerial] = info.timerId;
    m_activeTimerSerials[info.timerId] = m_currentTimerSerial;

    m_messageLoop.PostWork(m_completionCallbackFactory.NewCallback(
                               &QPepperEventDispatcher::timerCallback, m_currentTimerSerial),
                           info.interval);
}

void QPepperEventDispatcher::timerCallback(int32_t result, int32_t timerSerial)
{
    Q_UNUSED(result);
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "timerCallback" << timerSerial;

    // The timer might have been unregistered. In that case don't fire.
    if (!m_activeTimerIds.contains(timerSerial))
        return;

    // Get the timer info for the timerSerial/timerID.
    int timerId = m_activeTimerIds.value(timerSerial);
    const PepperTimerInfo &info = m_timerDetails.value(timerId);

    // Send the timer event
    QTimerEvent e(info.timerId);
    QCoreApplication::sendEvent(info.object, &e);
    processEvents();

    // After running Qt and application code the timer may have been unregistered,
    // and the timer id may have been reused. The timerSerial will hower not
    // be reused; use that to determine if the timer is active.
    if (m_activeTimerIds.contains(timerSerial))
        startTimer(info);

    // one serial number per callback, we are done with this one.
    m_activeTimerIds.remove(timerSerial);
}

void QPepperEventDispatcher::scheduleProcessEvents()
{
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "scheduleProcessEvents" << m_hasPendingProcessEvents;
    if (!m_hasPendingProcessEvents) {
        m_hasPendingProcessEvents = true;
        pp::CompletionCallback processEvents
            = m_completionCallbackFactory.NewCallback(&QPepperEventDispatcher::processEventsCallback);
        int32_t result = m_messageLoop.PostWork(processEvents);
        if (result != PP_OK)
            qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "scheduleProcessEvents PostWork error"
                                                        << result;
    }
}

void QPepperEventDispatcher::processEventsCallback(int32_t status)
{
    Q_UNUSED(status);
    qCDebug(QT_PLATFORM_PEPPER_EVENTDISPATHCER) << "processEvents";
    m_hasPendingProcessEvents = false;

    processEvents();
}