aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/events/morientationtracker.cpp
blob: 9fecdf5072bd9f82994948a869205ad4efc7b839 (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
/***************************************************************************
**
** 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 "mkeyboardstatetracker.h"
#include "mapplication.h"
#include "mwindow.h"
#include "mwindow_p.h"
#include "mcomponentdata.h"
#include "mdeviceprofile.h"
#include <QCoreApplication>
#include <QVariant>
#include <QTimer>
#ifdef HAVE_CONTEXTSUBSCRIBER
#include "contextproperty.h"
#endif

#include <MDebug>

#include "morientationtracker.h"
#include "morientationtracker_p.h"

MOrientationTracker *MOrientationTrackerPrivate::tracker = 0;

MOrientationTrackerPrivate::MOrientationTrackerPrivate(MOrientationTracker *controller) :
    currentAngle(M::Angle0),
    currentIsCovered(false),
    currentIsTvConnected(false),
    currentIsKeyboardOpen(MKeyboardStateTracker::instance()->isOpen())
#ifdef HAVE_CONTEXTSUBSCRIBER
    , videoRouteProperty(new ContextProperty("com.nokia.policy.video_route"))
    , topEdgeProperty(new ContextProperty("Screen.TopEdge"))
    , isCoveredProperty(new ContextProperty("Screen.IsCovered"))
    , isFlatProperty(new ContextProperty("Position.IsFlat"))
    , isSubscribed(false)
    , hasJustSubscribed(false)
#endif
#ifdef Q_WS_X11
    , widCurrentAppWindow(0)
    , currentAppWindowHadXPropertyChangeMask(false)
#endif //Q_WS_X11
    , q_ptr(controller)
{
    if (MComponentData::isOrientationForced()) {
        M::Orientation orientation = M::Landscape;

        currentAngle = MComponentData::forcedOrientationAngle();
        if (currentAngle == M::Angle90 || currentAngle == M::Angle270)
            orientation = M::Portrait;

        foreach(MWindow * window, MApplication::windows()) {
            if (window->orientation() == orientation)
                window->setOrientationAngle(currentAngle);
        }
        //orientation is fixed so we do not need to register for any signals
        return;
    }

#ifdef HAVE_CONTEXTSUBSCRIBER
    connect(topEdgeProperty, SIGNAL(valueChanged()),
            this, SLOT(updateOrientationAngle()));
    connect(isCoveredProperty, SIGNAL(valueChanged()),
            this, SLOT(isCoveredChanged()));
    connect(videoRouteProperty, SIGNAL(valueChanged()),
            this, SLOT(videoRouteChanged()));
    connect(isFlatProperty, SIGNAL(valueChanged()),
            this, SLOT(updateOrientationAngle()));
    connect(MKeyboardStateTracker::instance(), SIGNAL(stateChanged()),
            this, SLOT(updateOrientationAngle()));
#endif

    waitForSensorPropertiesToSubscribe();
    isCoveredChanged();
    videoRouteChanged();

#ifdef Q_WS_X11
    orientationAngleAtom = XInternAtom(QX11Info::display(),
                                       "_MEEGOTOUCH_ORIENTATION_ANGLE", False);
    currentAppWindowAtom = XInternAtom(QX11Info::display(),
                                       "_MEEGOTOUCH_CURRENT_APP_WINDOW", False);
#endif //Q_WS_X11
}

MOrientationTrackerPrivate::~MOrientationTrackerPrivate()
{
#ifdef HAVE_CONTEXTSUBSCRIBER
    delete videoRouteProperty;
    delete topEdgeProperty;
    delete isCoveredProperty;
    delete isFlatProperty;
#endif
}

void MOrientationTrackerPrivate::videoRouteChanged()
{
#ifdef HAVE_CONTEXTSUBSCRIBER
    QString value = videoRouteProperty->value().toString();
    mDebug("MOrientationTracker") << "VideoRoute:" << value;

    currentIsTvConnected = (value == "tvout" ||
                            value == "builtinandtvout");
    updateOrientationAngle();
#endif
}

void MOrientationTrackerPrivate::isCoveredChanged()
{
#ifdef HAVE_CONTEXTSUBSCRIBER
    Q_Q(MOrientationTracker);

    bool isCovered = isCoveredProperty->value().toBool();

    if (isCovered != currentIsCovered) {
        mDebug("MOrientationTracker") << "Covered:" << isCovered;

        currentIsCovered = isCovered;
        if (isCovered)
            emit q->faceFlippedDown();
        else
            emit q->faceUp();
    }
#endif
}

void MOrientationTrackerPrivate::resolveIfOrientationUpdatesRequired()
{
#ifdef HAVE_CONTEXTSUBSCRIBER
    bool updatesRequired = false;
    foreach(MWindow* win, MApplication::windows()) {
#ifdef Q_WS_X11
        if (win && (win->isOnDisplay()) &&
            !windowsFollowingCurrentAppWindow.contains(win) &&
            !windowsFollowingWithConstraintsCurrentAppWindow.contains(win))
#else
        if (win && win->isOnDisplay())
#endif
        {
            updatesRequired = true;
            break;
        }
    }
    if (updatesRequired && !isSubscribed)
        subscribeToSensorProperies();
    else if (!updatesRequired && isSubscribed)
        unsubscribeToSensorProperties();
#endif
}

void MOrientationTrackerPrivate::updateOrientationAngle()
{
#ifdef HAVE_CONTEXTSUBSCRIBER
    if (!isSubscribed) {
        mWarning("MOrientationTrackerPrivate::updateOrientationAngle") << "orientation update requested"
                << " when unsubscribed";
        return;
    }

    M::OrientationAngle angle = currentAngle;
    QString edge = topEdgeProperty->value().toString();

    angle = angleForTopEdge(edge);

    bool isDeviceFlat = isFlatProperty->value().toBool();

    bool isKeyboardOpen = MKeyboardStateTracker::instance()->isOpen();

    doUpdateOrientationAngle(angle, isKeyboardOpen, isDeviceFlat, currentIsTvConnected);
#endif
}

void MOrientationTrackerPrivate::doUpdateOrientationAngle(M::OrientationAngle angle, bool isKeyboardOpen,
                                                          bool isDeviceFlat, bool tvIsConnected)
{
#ifdef HAVE_CONTEXTSUBSCRIBER
    //Check if the keyboard was just closed, old angle is supported and
    //if device is lying flat (like on the table)
    if (currentIsKeyboardOpen == true && isKeyboardOpen == false
        && MDeviceProfile::instance()->orientationAngleIsSupported(currentAngle, isKeyboardOpen) &&
        isDeviceFlat) {
        currentIsKeyboardOpen = isKeyboardOpen;
        //do nothing
        return;
    }
    currentIsKeyboardOpen = isKeyboardOpen;

    if (tvIsConnected) // TV forces landscape for now, no transformations
        angle = M::Angle0;
    else if (!MDeviceProfile::instance()->orientationAngleIsSupported(angle, isKeyboardOpen)) {
        //it seems that orientation does not match allowed for current kybrd state.
        //check if the previous one was ok:
        if (MDeviceProfile::instance()->orientationAngleIsSupported(currentAngle, isKeyboardOpen)) {
            //it was: let's just use an old angle
            angle = currentAngle;
        } else {
            //it was not: let's use first allowed:
            if (MDeviceProfile::instance()->orientationAngleIsSupported(M::Angle0, isKeyboardOpen))
                angle = M::Angle0;
            else if (MDeviceProfile::instance()->orientationAngleIsSupported(M::Angle270, isKeyboardOpen))
                angle = M::Angle270;
            else if (MDeviceProfile::instance()->orientationAngleIsSupported(M::Angle90, isKeyboardOpen))
                angle = M::Angle90;
            else if (MDeviceProfile::instance()->orientationAngleIsSupported(M::Angle180, isKeyboardOpen))
                angle = M::Angle180;
            else
                qFatal("MOrientationTrackerPrivate::updateOrientationAngle() - current keyboard state does not seem to be covered in target configuration file");
        }
    }

    if (angle != currentAngle || hasJustSubscribed) {
        hasJustSubscribed = false;
        currentAngle = angle;
        // instead of emitting a signal we have to explicitely call setOrientationAngle
        // on windows, because this is very often excuted before the application's
        // event loop is started and leads to crash in QMetaObject
        foreach(MWindow * window, MApplication::windows()) {
            if(!windowsFollowingCurrentAppWindow.contains(window) &&
               !windowsFollowingWithConstraintsCurrentAppWindow.contains(window))
                rotateToAngleIfAllowed(angle, window);
        }
    }
#else
    Q_UNUSED(angle);
    Q_UNUSED(isKeyboardOpen);
    Q_UNUSED(isDeviceFlat);
    Q_UNUSED(tvIsConnected);
#endif //HAVE_CONTEXTSUBSCRIBER
}

void MOrientationTrackerPrivate::rotateToAngleIfAllowed(M::OrientationAngle angle, MWindow* window)
{
    M::Orientation orientation = M::Landscape;
    if (angle == M::Angle90 || angle == M::Angle270)
        orientation = M::Portrait;
    if (!window->isOrientationAngleLocked() &&
        (!window->isOrientationLocked() || window->orientation() == orientation)) {
        window->setOrientationAngle(angle);
    }
}

#ifdef HAVE_CONTEXTSUBSCRIBER
M::OrientationAngle MOrientationTrackerPrivate::angleForTopEdge(QString topEdge) const
{
    if (topEdge == "left")
        return M::Angle270;
    else if (topEdge == "right")
        return M::Angle90;
    else if (topEdge == "bottom")
        return M::Angle180;
    else
        return M::Angle0;
}
#endif //HAVE_CONTEXTSUBSCRIBER

MOrientationTracker::MOrientationTracker() :
    d_ptr(new MOrientationTrackerPrivate(this))
{
    setParent(QCoreApplication::instance()); //get collected when needed.
}

MOrientationTracker *MOrientationTracker::instance()
{
    if (!MOrientationTrackerPrivate::tracker)
        MOrientationTrackerPrivate::tracker = new MOrientationTracker();
    return MOrientationTrackerPrivate::tracker;
}

MOrientationTracker::~MOrientationTracker()
{
    if (this == MOrientationTrackerPrivate::tracker)
        MOrientationTrackerPrivate::tracker = 0;
}

M::OrientationAngle MOrientationTracker::orientationAngle() const
{
    Q_D(const MOrientationTracker);

    return d->currentAngle;
}

void MOrientationTracker::childEvent(QChildEvent *event)
{
    Q_UNUSED(event);
}

void MOrientationTracker::doUpdateOrientationAngle(
        M::OrientationAngle angle, bool isKeyboardOpen,
        bool isDeviceFlat, bool tvIsConnected)
{
    Q_D(MOrientationTracker);
    d->doUpdateOrientationAngle(angle, isKeyboardOpen, isDeviceFlat, tvIsConnected);
}
#ifdef HAVE_CONTEXTSUBSCRIBER

void MOrientationTrackerPrivate::subscribeToSensorProperies()
{
    if(!isSubscribed) {
        //waiting for properties to synchronize
        topEdgeProperty->subscribe();
        isCoveredProperty->subscribe();
        videoRouteProperty->subscribe();
        isFlatProperty->subscribe();
        waitForSensorPropertiesToSubscribe();
    }
    updateOrientationAngle();
}

void MOrientationTrackerPrivate::unsubscribeToSensorProperties()
{
    if(isSubscribed) {
        isSubscribed = false;
        topEdgeProperty->unsubscribe();
        isCoveredProperty->unsubscribe();
        videoRouteProperty->unsubscribe();
        isFlatProperty->unsubscribe();
    }
}

void MOrientationTrackerPrivate::waitForSensorPropertiesToSubscribe()
{
    topEdgeProperty->waitForSubscription();
    isCoveredProperty->waitForSubscription();
    videoRouteProperty->waitForSubscription();
    isFlatProperty->waitForSubscription();
    isSubscribed = true;
    hasJustSubscribed = true;
}
#endif //HAVE_CONTEXTSUBSCRIBER

#ifdef Q_WS_X11

void MOrientationTracker::handleCurrentAppWindowChange()
{
    Q_D(MOrientationTracker);
    d->handleCurrentAppWindowChange();
}

void MOrientationTracker::handleCurrentAppWindowOrientationAngleChange()
{
    Q_D(MOrientationTracker);
    d->handleCurrentAppWindowOrientationAngleChange();
}

bool MOrientationTrackerPrivate::handleX11PropertyEvent(XPropertyEvent *event)
{
    //handle only if there are any windows registered  to follow Current Window
    if (windowsFollowingCurrentAppWindow.count() > 0) {
        if (event->atom == orientationAngleAtom) {
            handleCurrentAppWindowOrientationAngleChange();
            return true;
        } else if (event->atom == currentAppWindowAtom) {
            handleCurrentAppWindowChange();
            return true;
        }
    }
    return false;
}

void MOrientationTrackerPrivate::handleCurrentAppWindowOrientationAngleChange()
{
    M::OrientationAngle angle = fetchCurrentAppWindowOrientationAngle();
    //windows like system dialog will follow current window to any orientation
    foreach(MWindow * win, windowsFollowingCurrentAppWindow) {
        win->setOrientationAngle(angle);
    }
    //some special windows have their constraints and we have to follow them
    foreach(MWindow * window, windowsFollowingWithConstraintsCurrentAppWindow) {
        rotateToAngleIfAllowed(angle, window);
    }
}

void MOrientationTrackerPrivate::handleCurrentAppWindowChange()
{
    Display* display = QX11Info::display();
    if(!display)
        return;

    XWindowAttributes attributes;
    if (widCurrentAppWindow) {
        XGetWindowAttributes(display, widCurrentAppWindow, &attributes);
        //We unset PropertyChangeMask if it was not set before
        if (!currentAppWindowHadXPropertyChangeMask)
            attributes.your_event_mask &= ~PropertyChangeMask;
        XSelectInput(display, widCurrentAppWindow, attributes.your_event_mask);
    }

    //fetch ID of new current window
    widCurrentAppWindow = fetchWIdCurrentAppWindow();

    //current window is invalid or not set then return
    if (widCurrentAppWindow == 0)
        return;

    XGetWindowAttributes(display, widCurrentAppWindow, &attributes);
    //remember if PropertyChangeMask is already set
    currentAppWindowHadXPropertyChangeMask = (bool)(attributes.your_event_mask & PropertyChangeMask);
    //set PropertyChangeMask for current app window if it is not set
    XSelectInput(display, widCurrentAppWindow, attributes.your_event_mask | PropertyChangeMask);
}

WId MOrientationTrackerPrivate::fetchWIdCurrentAppWindow()
{
    WId currentWindowId = 0;
    Atom actualType = 0;
    int actualFormat = 0;
    unsigned long nitems = 0;
    unsigned long bytes = 0;

    union {
        unsigned char* asUChar;
        unsigned long* asULong;
    } data = {0};

    int status = XGetWindowProperty(QX11Info::display(), RootWindow(QX11Info::display(), 0),
                                    currentAppWindowAtom, 0, 1, False, AnyPropertyType,
                                    &actualType, &actualFormat, &nitems,
                                    &bytes, &data.asUChar);
    if (status == Success && actualType == XA_WINDOW && actualFormat == 32)
        currentWindowId = data.asULong[0];

    if (status == Success)
        XFree(data.asUChar);

    return currentWindowId;
}

M::OrientationAngle MOrientationTrackerPrivate::fetchCurrentAppWindowOrientationAngle()
{
    M::OrientationAngle angle = M::Angle0;
    Atom actualType = 0;
    int actualFormat = 0;
    unsigned long nitems = 0;
    unsigned long bytes = 0;

    union {
        unsigned char* asUChar;
        unsigned int* asInt;
    } data = {0};
    //we are using fetchWIdCurrentAppWindow() instead of stored value (widCurrentAppWindow)
    //to eliminate a risk of race condition
    int status = XGetWindowProperty(QX11Info::display(), fetchWIdCurrentAppWindow(),
                                    orientationAngleAtom, 0, 1, False, AnyPropertyType,
                                    &actualType, &actualFormat, &nitems,
                                    &bytes, &data.asUChar);

    if (status == Success && actualType == XA_CARDINAL && actualFormat == 32 && nitems == 1) {
        if (data.asInt[0] == M::Angle0 || data.asInt[0] == M::Angle90 || data.asInt[0] == M::Angle180
            || data.asInt[0] == M::Angle270 ){
            angle = (M::OrientationAngle)( data.asInt[0] );
        }
    }

    if (status == Success)
        XFree(data.asUChar);
    return angle;
}

void MOrientationTrackerPrivate::startFollowingCurrentAppWindow(MWindow *win, bool limitedByConstraints)
{
    M::OrientationAngle angle = fetchCurrentAppWindowOrientationAngle();
    if (limitedByConstraints && !windowsFollowingWithConstraintsCurrentAppWindow.contains(win)) {
        windowsFollowingWithConstraintsCurrentAppWindow.append(win);
        rotateToAngleIfAllowed(angle, win);
    } else {
        windowsFollowingCurrentAppWindow.append(win);
        win->setOrientationAngle(angle);
    }
    handleCurrentAppWindowChange();
}

void MOrientationTrackerPrivate::stopFollowingCurrentAppWindow(MWindow *win, bool limitedByConstraints)
{
    if (limitedByConstraints)
        windowsFollowingWithConstraintsCurrentAppWindow.removeAll(win);
    else
        windowsFollowingCurrentAppWindow.removeAll(win);

    if (windowsFollowingCurrentAppWindow.count() == 0 &&
        windowsFollowingWithConstraintsCurrentAppWindow.count() == 0) {
        if (widCurrentAppWindow) {
            XWindowAttributes attributes;
            XGetWindowAttributes(QX11Info::display(), widCurrentAppWindow, &attributes);
            //We unset PropertyChangeMask if it was not set before
            if (!currentAppWindowHadXPropertyChangeMask)
                attributes.your_event_mask &= ~PropertyChangeMask;
            XSelectInput(QX11Info::display(), widCurrentAppWindow, attributes.your_event_mask);
            widCurrentAppWindow = 0;
        }
        currentAppWindowHadXPropertyChangeMask = false;
    }
}

#endif //Q_WS_X11