summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/ios/qioseventdispatcher.mm
blob: e9bf039047d8b60d61e58a617ec0191ace0a6a2e (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 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 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qioseventdispatcher.h"
#import "qiosapplicationdelegate.h"
#include <qdebug.h>
#include <qpa/qwindowsysteminterface.h>
#include <QtCore/QThread>
#include <QtCore/private/qcoreapplication_p.h>
#include <UIKit/UIApplication.h>

QT_BEGIN_NAMESPACE
QT_USE_NAMESPACE

static Boolean runLoopSourceEqualCallback(const void *info1, const void *info2)
{
    return info1 == info2;
}

void QIOSEventDispatcher::postedEventsRunLoopCallback(void *info)
{
    QIOSEventDispatcher *self = static_cast<QIOSEventDispatcher *>(info);
    self->processPostedEvents();
}

void QIOSEventDispatcher::nonBlockingTimerRunLoopCallback(CFRunLoopTimerRef, void *info)
{
    // The (one and only) CFRunLoopTimer has fired, which means that at least
    // one QTimer should now fire as well. Note that CFRunLoopTimer's callback will
    // never recurse. So if the app starts a new QEventLoop within this callback, other
    // timers will stop working. The work-around is to forward the callback to a
    // dedicated CFRunLoopSource that can recurse:
    QIOSEventDispatcher *self = static_cast<QIOSEventDispatcher *>(info);
    CFRunLoopSourceSignal(self->m_blockingTimerRunLoopSource);
}

void QIOSEventDispatcher::blockingTimerRunLoopCallback(void *info)
{
    // TODO:
    // We also need to block this new timer source
    // along with the posted event source when calling processEvents()
    // "manually" to prevent livelock deep in CFRunLoop.

    QIOSEventDispatcher *self = static_cast<QIOSEventDispatcher *>(info);
    self->m_timerInfoList.activateTimers();
    self->maybeStartCFRunLoopTimer();
}

void QIOSEventDispatcher::maybeStartCFRunLoopTimer()
{
    // Find out when the next registered timer should fire, and schedule
    // runLoopTimer accordingly. If the runLoopTimer does not yet exist, and
    // at least one timer is registered, start by creating the timer:
    if (m_timerInfoList.isEmpty()) {
        Q_ASSERT(m_runLoopTimerRef == 0);
        return;
    }

    CFAbsoluteTime ttf = CFAbsoluteTimeGetCurrent();
    CFTimeInterval interval;

    if (m_runLoopTimerRef == 0) {
        // start the CFRunLoopTimer
        CFTimeInterval oneyear = CFTimeInterval(3600. * 24. * 365.);

        // calculate when the next timer should fire:
        struct timespec tv;
        if (m_timerInfoList.timerWait(tv)) {
            interval = qMax(tv.tv_sec + tv.tv_nsec / 1000000000., 0.0000001);
        } else {
            // this shouldn't really happen, but in case it does, set the timer
            // to fire a some point in the distant future:
            interval = oneyear;
        }

        ttf += interval;
        CFRunLoopTimerContext info = { 0, this, 0, 0, 0 };
        // create the timer with a large interval, as recommended by the CFRunLoopTimerSetNextFireDate()
        // documentation, since we will adjust the timer's time-to-fire as needed to keep Qt timers working
        m_runLoopTimerRef = CFRunLoopTimerCreate(0, ttf, oneyear, 0, 0, QIOSEventDispatcher::nonBlockingTimerRunLoopCallback, &info);
        Q_ASSERT(m_runLoopTimerRef != 0);

        CFRunLoopAddTimer(CFRunLoopGetMain(), m_runLoopTimerRef, kCFRunLoopCommonModes);
    } else {
        struct timespec tv;
        // Calculate when the next timer should fire:
        if (m_timerInfoList.timerWait(tv)) {
            interval = qMax(tv.tv_sec + tv.tv_nsec / 1000000000., 0.0000001);
        } else {
            // no timers can fire, but we cannot stop the CFRunLoopTimer, set the timer to fire at some
            // point in the distant future (the timer interval is one year)
            interval = CFRunLoopTimerGetInterval(m_runLoopTimerRef);
        }

        ttf += interval;
        CFRunLoopTimerSetNextFireDate(m_runLoopTimerRef, ttf);
    }
}

void QIOSEventDispatcher::maybeStopCFRunLoopTimer()
{
    if (m_runLoopTimerRef == 0)
        return;

    CFRunLoopTimerInvalidate(m_runLoopTimerRef);
    CFRelease(m_runLoopTimerRef);
    m_runLoopTimerRef = 0;
}

void QIOSEventDispatcher::processPostedEvents()
{
    QWindowSystemInterface::sendWindowSystemEvents(QEventLoop::AllEvents);
}

QIOSEventDispatcher::QIOSEventDispatcher(QObject *parent)
    : QAbstractEventDispatcher(parent)
    , m_interrupted(false)
    , m_runLoopTimerRef(0)
{
    m_cfSocketNotifier.setHostEventDispatcher(this);

    CFRunLoopRef mainRunLoop = CFRunLoopGetMain();
    CFRunLoopSourceContext context;
    bzero(&context, sizeof(CFRunLoopSourceContext));
    context.equal = runLoopSourceEqualCallback;
    context.info = this;

    // source used to handle timers:
    context.perform = QIOSEventDispatcher::blockingTimerRunLoopCallback;
    m_blockingTimerRunLoopSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
    Q_ASSERT(m_blockingTimerRunLoopSource);
    CFRunLoopAddSource(mainRunLoop, m_blockingTimerRunLoopSource, kCFRunLoopCommonModes);

    // source used to handle posted events:
    context.perform = QIOSEventDispatcher::postedEventsRunLoopCallback;
    m_postedEventsRunLoopSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
    Q_ASSERT(m_postedEventsRunLoopSource);
    CFRunLoopAddSource(mainRunLoop, m_postedEventsRunLoopSource, kCFRunLoopCommonModes);
}

QIOSEventDispatcher::~QIOSEventDispatcher()
{
    CFRunLoopRef mainRunLoop = CFRunLoopGetMain();
    CFRunLoopRemoveSource(mainRunLoop, m_postedEventsRunLoopSource, kCFRunLoopCommonModes);
    CFRelease(m_postedEventsRunLoopSource);

    qDeleteAll(m_timerInfoList);
    maybeStopCFRunLoopTimer();
    CFRunLoopRemoveSource(CFRunLoopGetMain(), m_blockingTimerRunLoopSource, kCFRunLoopCommonModes);
    CFRelease(m_blockingTimerRunLoopSource);

    m_cfSocketNotifier.removeSocketNotifiers();
}

bool QIOSEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
{
    m_interrupted = false;
    bool eventsProcessed = false;

    bool excludeUserEvents = flags & QEventLoop::ExcludeUserInputEvents;
    bool execFlagSet = (flags & QEventLoop::DialogExec) || (flags & QEventLoop::EventLoopExec);
    bool useExecMode = execFlagSet && !excludeUserEvents;

    if (useExecMode) {
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        while ([runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]] && !m_interrupted);
        eventsProcessed = true;
    } else {
        if (!(flags & QEventLoop::WaitForMoreEvents))
            wakeUp();
        eventsProcessed = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    return eventsProcessed;
}

bool QIOSEventDispatcher::hasPendingEvents()
{
    qDebug() << __FUNCTION__ << "not implemented";
    return false;
}

void QIOSEventDispatcher::registerSocketNotifier(QSocketNotifier *notifier)
{
    m_cfSocketNotifier.registerSocketNotifier(notifier);
}

void QIOSEventDispatcher::unregisterSocketNotifier(QSocketNotifier *notifier)
{
    m_cfSocketNotifier.unregisterSocketNotifier(notifier);
}

void QIOSEventDispatcher::registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *obj)
{
#ifndef QT_NO_DEBUG
    if (timerId < 1 || interval < 0 || !obj) {
        qWarning("QIOSEventDispatcher::registerTimer: invalid arguments");
        return;
    } else if (obj->thread() != thread() || thread() != QThread::currentThread()) {
        qWarning("QIOSEventDispatcher: timers cannot be started from another thread");
        return;
    }
#endif

    m_timerInfoList.registerTimer(timerId, interval, timerType, obj);
    maybeStartCFRunLoopTimer();
}

bool QIOSEventDispatcher::unregisterTimer(int timerId)
{
#ifndef QT_NO_DEBUG
    if (timerId < 1) {
        qWarning("QIOSEventDispatcher::unregisterTimer: invalid argument");
        return false;
    } else if (thread() != QThread::currentThread()) {
        qWarning("QObject::killTimer: timers cannot be stopped from another thread");
        return false;
    }
#endif

    bool returnValue = m_timerInfoList.unregisterTimer(timerId);
    m_timerInfoList.isEmpty() ? maybeStopCFRunLoopTimer() : maybeStartCFRunLoopTimer();
    return returnValue;
}

bool QIOSEventDispatcher::unregisterTimers(QObject *object)
{
#ifndef QT_NO_DEBUG
    if (!object) {
        qWarning("QIOSEventDispatcher::unregisterTimers: invalid argument");
        return false;
    } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
        qWarning("QObject::killTimers: timers cannot be stopped from another thread");
        return false;
    }
#endif

    bool returnValue = m_timerInfoList.unregisterTimers(object);
    m_timerInfoList.isEmpty() ? maybeStopCFRunLoopTimer() : maybeStartCFRunLoopTimer();
    return returnValue;
}

QList<QAbstractEventDispatcher::TimerInfo> QIOSEventDispatcher::registeredTimers(QObject *object) const
{
#ifndef QT_NO_DEBUG
    if (!object) {
        qWarning("QIOSEventDispatcher:registeredTimers: invalid argument");
        return QList<TimerInfo>();
    }
#endif

    return m_timerInfoList.registeredTimers(object);
}

int QIOSEventDispatcher::remainingTime(int timerId)
{
#ifndef QT_NO_DEBUG
    if (timerId < 1) {
        qWarning("QIOSEventDispatcher::remainingTime: invalid argument");
        return -1;
    }
#endif

    return m_timerInfoList.timerRemainingTime(timerId);
}

void QIOSEventDispatcher::wakeUp()
{
    CFRunLoopSourceSignal(m_postedEventsRunLoopSource);
    CFRunLoopWakeUp(CFRunLoopGetMain());
}

void QIOSEventDispatcher::interrupt()
{
    wakeUp();
    m_interrupted = true;
}

void QIOSEventDispatcher::flush()
{
    // X11 only.
}

QT_END_NAMESPACE