*
[groove] / qmaemo5rotator.cpp
1
2 #include "qmaemo5rotator.h"
3 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
4 #include <mce/dbus-names.h>
5 #include <mce/mode-names.h>
6 #include <QtDBus/QDBusConnection>
7 #include <QtDBus/QDBusMessage>
8
9
10 QMaemo5Rotator::QMaemo5Rotator(RotationBehavior behavior, QWidget *parent)
11     : QObject(parent),
12     isSetUp(false)
13 {
14     setCurrentBehavior(behavior);
15 }
16
17 QMaemo5Rotator::~QMaemo5Rotator()
18 {
19     QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
20 }
21
22 const QMaemo5Rotator::RotationBehavior QMaemo5Rotator::currentBehavior()
23 {
24     return _currentBehavior;
25 }
26
27 const QMaemo5Rotator::Orientation QMaemo5Rotator::currentOrientation()
28 {
29     return _currentOrientation;
30 }
31
32 void QMaemo5Rotator::setCurrentBehavior(QMaemo5Rotator::RotationBehavior value)
33 {
34     if (value == _currentBehavior && isSetUp)
35         return;
36
37     isSetUp = true;
38     _currentBehavior = value;
39
40     if (value == QMaemo5Rotator::AutomaticBehavior)
41     {
42         QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_ENABLE_REQ));
43         QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF, MCE_DEVICE_ORIENTATION_SIG, this, SLOT(on_orientation_changed(QString)));
44     }
45     else
46     {
47         QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
48
49         if (value == QMaemo5Rotator::PortraitBehavior)
50         {
51             setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
52         }
53         else
54         {
55             setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
56         }
57     }
58 }
59 void QMaemo5Rotator::test()
60 {
61     QMaemo5Rotator::setRotation(up);
62 }
63
64 void QMaemo5Rotator::setCurrentOrientation(QMaemo5Rotator::Orientation value)
65 {
66     _currentOrientation = value;
67     QWidget *par = (QWidget*)parent();
68
69     switch (value)
70     {
71     case QMaemo5Rotator::PortraitOrientation:
72         if (par != NULL)
73         {
74             par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
75             par->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
76         }
77         orientationChanged(QMaemo5Rotator::PortraitOrientation);
78         break;
79     case QMaemo5Rotator::LandscapeOrientation:
80         if (par != NULL)
81         {
82             par->setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
83             par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
84         }
85         orientationChanged(QMaemo5Rotator::LandscapeOrientation);
86
87         break;
88     }
89 }
90
91 void QMaemo5Rotator::on_orientation_changed(const QString& newOrientation)
92 {
93     if (newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT) || newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT_INVERTED))
94     {
95         setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
96     }
97     else
98     {
99         setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
100     }
101     QApplication::desktop()->updateGeometry();
102 }
103
104
105
106
107
108
109
110 static bool QMaemo5Rotator::setRotation(direction dir)
111 {
112     Rotation rotation = dir;
113     int reflection = 0;
114     Display * dpy = XOpenDisplay(NULL);
115     if ( dpy == NULL )
116     {
117         printf ( "Cannot open display %s\n", displayname ) ;
118         return false ;
119     }
120     int screen = DefaultScreen ( dpy ) ;
121     Window root = RootWindow ( dpy, screen ) ;
122
123     XSelectInput (dpy, root, StructureNotifyMask);
124     XRRSelectInput (dpy, root, RRScreenChangeNotifyMask);
125     int eventbase ;
126     int errorbase ;
127     XRRQueryExtension ( dpy, &eventbase, &errorbase ) ;
128
129     XRRScreenConfiguration * sc = XRRGetScreenInfo (dpy, root);
130     if ( sc == NULL )
131     {
132         printf ( "Cannot get screen info\n" ) ;
133         return false ;
134     }
135     int nsize ;
136     XRRScreenSize * sizes = XRRConfigSizes(sc, &nsize);
137     int sizeindex = 0 ;
138     if ( nsize = 0 )
139     {
140         printf ( "XRandr not available\n") ;
141         return false ;
142     }
143     Status status = XRRSetScreenConfig ( dpy, sc, DefaultRootWindow (dpy), (SizeID) sizeindex, (Rotation) (rotation | reflection), CurrentTime);
144     XEvent event;
145     bool rcvdrrnotify = false ;
146     bool rcvdconfignotify = false ;
147     if ( status == RRSetConfigSuccess)
148     {
149         printf("Screen rotation changed");
150     }
151     XCloseDisplay(dpy);
152     return true ;
153 }
154 #endif