aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/core/mapplication.cpp
blob: eb35c5f95a84dc92ed8d2f9ac8044ee0ab0091eb (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it 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.
**
****************************************************************************/

#include <qglobal.h>
#include <cstdlib>
#include <QCoreApplication>

#include "mapplication.h"
#include "mcomponentdata.h"
#include "mondisplaychangeevent.h"
#include "mapplicationwindow.h"
#include "mwindow_p.h"
#include <MDebug>
#include "mapplication_p.h"
#include "morientationtracker.h"
#include "morientationtracker_p.h"

/* Must be last, as it conflicts with some of the Qt defined types */
#ifdef Q_WS_X11
#include <QX11Info>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif //Q_WS_X11

#ifdef HAVE_XDAMAGE
#include <X11/extensions/Xdamage.h>
#endif //HAVE_XDAMAGE

MApplicationPrivate::MApplicationPrivate():
        componentData(NULL),
        xDamageEventBase(0),
        xDamageErrorBase(0),
        q_ptr(NULL)
{
#ifdef HAVE_XDAMAGE
    XDamageQueryExtension(QX11Info::display(), &xDamageEventBase, &xDamageErrorBase);
#endif //HAVE_XDAMAGE

#ifdef Q_WS_X11
    visibleAtom = XInternAtom(QX11Info::display(),
                              "_MEEGOTOUCH_VISIBLE_IN_SWITCHER", False);
    minimizeAnimationAtom = XInternAtom(QX11Info::display(),
                                        "_MEEGOTOUCH_MINIMIZE_ANIMATION", False);
#endif
}

MApplicationPrivate::~MApplicationPrivate()
{
    delete componentData;
}
#ifdef Q_WS_X11
void MApplicationPrivate::setWindowVisibility(MWindow * window, bool visible)
{
    MOnDisplayChangeEvent ev(visible, QRectF(QPointF(0, 0), window->visibleSceneSize()));
    MApplication::instance()->sendEvent(window, &ev);
}

int MApplicationPrivate::handleXError(Display *, XErrorEvent *)
{
    return 0;
}


void MApplicationPrivate::setX11PrestartPropertyForWindows(bool set)
{
    Q_FOREACH(MWindow * win, MApplication::windows()) {
        win->d_ptr->setX11PrestartProperty(set);
    }
}


MWindow * MApplicationPrivate::windowForId(Window window)
{
    Q_FOREACH(MWindow * p, MComponentData::windows()) {
        if (p && p->effectiveWinId() == static_cast<unsigned int>(window)) {
            return p;
        }
    }
    return NULL;
}
#endif

MApplication *MApplication::instance()
{
    return qobject_cast<MApplication *>(QCoreApplication::instance());
}

MApplication::MApplication(int &argc, char **argv, const QString &appIdentifier, MApplicationService *service)
    : QApplication(argc, argv),
      d_ptr(new MApplicationPrivate)
{
    Q_D(MApplication);
    d->q_ptr = this;

    d->init(argc, argv, appIdentifier, service);
}

MApplication::MApplication(int &argc, char **argv, MApplicationService *service) :
    QApplication(argc, argv),
    d_ptr(new MApplicationPrivate)
{
    Q_D(MApplication);
    d->q_ptr = this;

    d->init(argc, argv, QString(), service);
}

void MApplicationPrivate::init(int &argc, char **argv, const QString &appIdentifier, MApplicationService *newService)
{
    componentData = new MComponentData(argc, argv, appIdentifier, newService);
}

void MApplicationPrivate::releasePrestart()
{
    if (MApplication::isPrestarted()) {

        MComponentData::setPrestarted(false);

        // Call the virtual handler and emit the signal
        MApplication *mApp = MApplication::instance();
        if (mApp) {
            mApp->releasePrestart();
            emit mApp->prestartReleased();
        }

#ifdef Q_WS_X11
        setX11PrestartPropertyForWindows(false);
#endif
    }
}

void MApplicationPrivate::restorePrestart()
{
    if (!MApplication::isPrestarted() && prestartModeIsLazyShutdown()) {

        MComponentData::setPrestarted(true);

        // Call the virtual handler and emit the signal
        MApplication *mApp = MApplication::instance();
        if (mApp) {
            mApp->restorePrestart();
            emit mApp->prestartRestored();
        }

#ifdef Q_WS_X11
        setX11PrestartPropertyForWindows(true);
#endif
    }
}

void MApplicationPrivate::hideWindows()
{
    Q_FOREACH(MWindow * win, MApplication::windows()) {
        win->hide();
        win->lower();
    }
}

void MApplication::setPrestarted(bool enable)
{
    if (enable) {
        MApplicationPrivate::restorePrestart();
    } else {
        MApplicationPrivate::releasePrestart();
    }
}

bool MApplicationPrivate::prestartModeIsLazyShutdown()
{
    return MApplication::prestartMode() == M::LazyShutdown ||
            MApplication::prestartMode() == M::LazyShutdownMultiWindow;
}

bool MApplicationPrivate::prestartModeIsMultiWindowed()
{
    return MApplication::prestartMode() == M::TerminateOnCloseMultiWindow ||
            MApplication::prestartMode() == M::LazyShutdownMultiWindow;
}

MApplication::~MApplication()
{
    delete d_ptr;
}

QString MApplication::deviceName()
{
    return MComponentData::deviceName();
}

bool MApplication::softwareRendering()
{
    return MComponentData::softwareRendering();
}

bool MApplication::fullScreen()
{
    return MComponentData::fullScreen();
}

bool MApplication::showBoundingRect()
{
    return MComponentData::showBoundingRect();
}

bool MApplication::showFps()
{
    return MComponentData::showFps();
}

bool MApplication::logFps()
{
    return MComponentData::logFps();
}

bool MApplication::showSize()
{
    return MComponentData::showSize();
}

bool MApplication::showPosition()
{
    return MComponentData::showPosition();
}

bool MApplication::showMargins()
{
    return MComponentData::showMargins();
}

bool MApplication::showObjectNames()
{
    return MComponentData::showObjectNames();
}

bool MApplication::showStyleNames()
{
    return MComponentData::showStyleNames();
}

bool MApplication::showCursor()
{
    return MComponentData::showCursor();
}

bool MApplication::emulateTwoFingerGestures()
{
    return MComponentData::emulateTwoFingerGestures();
}

void MApplication::setShowPosition(bool show)
{
    MComponentData::setShowPosition(show);
}

void MApplication::setShowSize(bool show)
{
    MComponentData::setShowSize(show);
}

void MApplication::setShowMargins(bool show)
{
    MComponentData::setShowMargins(show);
}

void MApplication::setShowObjectNames(bool show)
{
    MComponentData::setShowObjectNames(show);
}

void MApplication::setShowStyleNames(bool show)
{
    MComponentData::setShowStyleNames(show);
}

void MApplication::setShowBoundingRect(bool show)
{
    MComponentData::setShowBoundingRect(show);
}

void MApplication::setShowFps(bool show)
{
    MComponentData::setShowFps(show);
}

void MApplication::setLogFps(bool show)
{
    MComponentData::setLogFps(show);
}

void MApplication::setShowCursor(bool show)
{
    MComponentData::setShowCursor(show);
}

MApplicationWindow *MApplication::activeApplicationWindow()
{
    return MComponentData::activeApplicationWindow();
}

MWindow *MApplication::activeWindow()
{
    return MComponentData::activeWindow();
}

QList<MWindow *> MApplication::windows()
{
    return MComponentData::windows();
}

MFeedbackPlayer *MApplication::feedbackPlayer()
{
    return MComponentData::feedbackPlayer();
}

bool MApplication::isLoadMInputContextEnabled()
{
    return MComponentData::isLoadMInputContextEnabled();
}

void MApplication::setLoadMInputContext(bool enable)
{
    MComponentData::setLoadMInputContext(enable);
}

#ifdef Q_WS_X11
bool MApplication::x11EventFilter(XEvent *event)
{
    Q_D(MApplication);

    if (event->type == VisibilityNotify) {
        XVisibilityEvent *xevent = (XVisibilityEvent *) event;
        d->handleXVisibilityEvent(xevent);

    } else if (event->type == PropertyNotify) {
        XPropertyEvent *xevent = (XPropertyEvent *) event;
        d->handleXPropertyEvent(xevent);
    } else if (event->type == ClientMessage) {
        XClientMessageEvent *e = (XClientMessageEvent*) event;
        if (e->message_type == d->minimizeAnimationAtom) {
            switch (e->data.l[0]) {
            case 0:   // minimizing was cancelled
                emit minimizingCanceled();
                break;
            case 1:   // minimizing started
                emit minimizing();
                break;
            case 100: // minimizing finished
                emit minimized();
                break;
            default:
                break;
            }
        }
    }
#ifdef HAVE_XDAMAGE
    else if (event->type == d->xDamageEventBase + XDamageNotify) {

        XDamageNotifyEvent *xevent = (XDamageNotifyEvent *) event;
        XserverRegion parts;
        XRectangle *rects;
        int n_rect;

        // It is possible that the Damage has already been destroyed so register an error handler to suppress X errors
        XErrorHandler errh = XSetErrorHandler(MApplicationPrivate::handleXError);

        // Get the damaged rectangles from the damage event
        parts = XFixesCreateRegion(QX11Info::display(), 0, 0);
        // TODO does subtracting 0 regions really make any sense?
        XDamageSubtract(QX11Info::display(), xevent->damage, None, parts);
        rects = XFixesFetchRegion(QX11Info::display(), parts, &n_rect);
        XFixesDestroyRegion(QX11Info::display(), parts);

        // Create separate damage event signals from the damaged rectangles
        for (int i = 0; i < n_rect; ++i)
            emit damageEvent(xevent->damage, rects[i].x, rects[i].y, rects[i].width, rects[i].height);

        // Free the rectangles and reset the original error handler
        XFree(rects);
        XSetErrorHandler(errh);

        return true;
    }
#else //HAVE_XDAMAGE
    Q_UNUSED(d);
#endif //HAVE_XDAMAGE

    return false;
}

void MApplicationPrivate::handleXVisibilityEvent(XVisibilityEvent *xevent)
{
    switch (xevent->state) {
    case VisibilityFullyObscured:
        // Listen only to synthetic events by compositor
        if (xevent->send_event)
        {
            MWindow * window = MApplicationPrivate::windowForId(xevent->window);
            if (window) {
                window->d_ptr->fullyObscured = true;
                if (!window->d_ptr->visibleInSwitcher) {
                    setWindowVisibility(window, false);
                }
            }
        }
        break;

        // Always listen to these events, because if compositor is not running
        // we are not getting any synthetic events at all and the window would never get
        // the display entered signal.
    case VisibilityUnobscured:
    case VisibilityPartiallyObscured:
        {
            MWindow * window = MApplicationPrivate::windowForId(xevent->window);
            if (window) {
                window->d_ptr->fullyObscured = false;
                setWindowVisibility(window, true);
            }
        }
        break;
    default:
        break;
    }
}

void MApplicationPrivate::handleXPropertyEvent(XPropertyEvent *xevent)
{
    if (MOrientationTracker::instance()->d_func()->handleX11PropertyEvent(xevent))
        return;

    // _MEEGOTOUCH_VISIBLE_IN_SWITCHER is set by Home Screen for
    // windows that are in the switcher and visible. Set/unset the
    // flag for corresponding window because we need to combine this
    // information with VisibilityNotify's.

    MWindow *window = windowForId(xevent->window);
    if (window && xevent->state == PropertyNewValue) {
        Atom           type;
        int            format;
        unsigned long  nItems;
        unsigned long  bytesAfter;
        unsigned char *data = NULL;

        if (xevent->atom == visibleAtom) {
            // Read value of the property. Should be 1 or 0.
            if (XGetWindowProperty(QX11Info::display(), xevent->window, visibleAtom,
                                   0, 1, False, XA_CARDINAL, &type, &format, &nItems,
                                   &bytesAfter, &data) == Success && data) {
                const bool obscured = window->d_ptr->fullyObscured;
                bool visible = *data;
                window->d_ptr->visibleInSwitcher = visible;

                if (visible) {
                    setWindowVisibility(window, true);
                } else if (obscured){
                    setWindowVisibility(window, false);
                }

                XFree(data);
            }
        }
    }
}

#endif

QString MApplication::appName()
{
    return MComponentData::appName();
}

QString MApplication::binaryName()
{
    return MComponentData::binaryName();
}

void MApplicationPrivate::stdExit(int status)
{
    /*
      This exit function can be reached either from
      the MApplication constructor or from the event loop,
      since the MComponentCache was introduced.

      If this function is called from the event loop, we
      want to make sure that we use qApp->exit() so that
      qt can tear down itself correctly, see NB#195013
      for problems that can arise here.

      But when this function is called without a running
      event loop ( i.e. from the constructor ), then
      qApp->exit() does nothing, see its documentation for
      reference. To really exit in this case, we call
      the c exit() function.
     */

    // exit when an event loop is running
    qApp->exit( status );

    // exit when no event loop is running
    _exit( status );
}

void MApplication::setPrestartMode(M::PrestartMode mode)
{
    // Set prestart mode only if the app was started with -prestart.
    // This way we can later check if the app was originally prestarted or not.
    // Prevent application from exiting after the last window has been closed if
    // lazy shutdown was set.

    if (MComponentData::prestarted()) {
        MComponentData::setPrestartMode(mode);
        if (MApplicationPrivate::prestartModeIsLazyShutdown()) {
            MApplication::setQuitOnLastWindowClosed(false);
        }
    }
}

M::PrestartMode MApplication::prestartMode()
{
    return MComponentData::prestartMode();
}

void MApplication::releasePrestart()
{
    // Show the window only if we are not in a multi-windowed mode.
    // In that case the user must re-implement this, because we don't know which
    // window needs to be shown.

    if (!MApplicationPrivate::prestartModeIsMultiWindowed()) {
        if (MApplication::activeWindow()) {
            MApplication::activeWindow()->show();
            MApplication::activeWindow()->activateWindow();
            MApplication::activeWindow()->raise();
        }
    }
}

void MApplication::restorePrestart()
{
    if (MApplicationPrivate::prestartModeIsLazyShutdown()) {
        MApplicationPrivate::hideWindows();
    }
}

bool MApplication::isPrestarted()
{
    return (MComponentData::prestartMode() != M::NoPrestart) && MComponentData::prestarted();
}

#ifdef __arm__
/*
 * When in "runfast" mode, single-precision VFP instructions execute in the
 * NEON pipeline. If using gcc, adding the flags -ffast-math -fno-math-errno,
 * and avoiding double precision is advisable, assuming results are still
 * accurate enough.
 */
static int set_runfast()
{
#if defined(__VFP_FP__) && !defined(__SOFTFP__)
    int tmp;
    __asm__ volatile(
        "fmrx       %[tmp], fpscr\n\t"
        "orr        %[tmp], %[tmp], #(1 << 24)\n\t" /* flush-to-zero */
        "orr        %[tmp], %[tmp], #(1 << 25)\n\t" /* default NaN */
        "bic        %[tmp], %[tmp], #((1 << 15) | (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8))\n\t" /* clear exception bits */
        "fmxr       fpscr, %[tmp]\n\t"
        : [tmp] "=r"(tmp));
    return tmp;
#else
    return 0;
#endif
}

/*
 * IEEE standard floating point calculation
 */
static int set_ieee()
{
#if defined(__VFP_FP__) && !defined(__SOFTFP__)
    int tmp;
    __asm__ volatile(
        "fmrx       %[tmp], fpscr\n\t"
        "bic        %[tmp], %[tmp], #(1 << 24)\n\t" /* flush-to-zero */
        "bic        %[tmp], %[tmp], #(1 << 25)\n\t" /* default NaN */
        "fmxr       fpscr, %[tmp]\n\t"
        : [tmp] "=r"(tmp));
    return tmp;
#else
    return 0;
#endif
}

void MApplication::fastFloatMath(bool val)
{
    if (val) {
        set_runfast();
    } else {
        set_ieee();
    }
}

#endif