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