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>
9 QMaemo5Rotator::QMaemo5Rotator(RotationBehavior behavior, QWidget *parent)
13 setCurrentBehavior(behavior);
16 QMaemo5Rotator::~QMaemo5Rotator()
18 QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
21 const QMaemo5Rotator::RotationBehavior QMaemo5Rotator::currentBehavior()
23 return _currentBehavior;
26 const QMaemo5Rotator::Orientation QMaemo5Rotator::currentOrientation()
28 return _currentOrientation;
31 void QMaemo5Rotator::setCurrentBehavior(QMaemo5Rotator::RotationBehavior value)
33 if (value == _currentBehavior && isSetUp)
37 _currentBehavior = value;
39 if (value == QMaemo5Rotator::AutomaticBehavior)
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)));
46 QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
48 if (value == QMaemo5Rotator::PortraitBehavior)
50 setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
54 setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
58 void QMaemo5Rotator::test()
60 QMaemo5Rotator::setRotation(right);
63 void QMaemo5Rotator::setCurrentOrientation(QMaemo5Rotator::Orientation value)
65 _currentOrientation = value;
66 QWidget *par = (QWidget*)parent();
70 case QMaemo5Rotator::PortraitOrientation:
73 par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
74 par->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
76 orientationChanged(QMaemo5Rotator::PortraitOrientation);
78 case QMaemo5Rotator::LandscapeOrientation:
81 par->setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
82 par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
84 orientationChanged(QMaemo5Rotator::LandscapeOrientation);
90 void QMaemo5Rotator::on_orientation_changed(const QString& newOrientation)
92 if (newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT) || newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT_INVERTED))
94 setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
98 setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
100 QApplication::desktop()->updateGeometry();
102 bool QMaemo5Rotator::setRotation(direction dir)
104 Rotation rotation = dir;
106 Display * dpy = XOpenDisplay(NULL);
109 printf ( "Cannot open display" ) ;
112 int screen = DefaultScreen ( dpy ) ;
113 Window root = RootWindow ( dpy, screen ) ;
115 XSelectInput (dpy, root, StructureNotifyMask);
116 XRRSelectInput (dpy, root, RRScreenChangeNotifyMask);
119 XRRQueryExtension ( dpy, &eventbase, &errorbase ) ;
121 XRRScreenConfiguration * sc = XRRGetScreenInfo (dpy, root);
124 printf ( "Cannot get screen info\n" ) ;
128 XRRScreenSize * sizes = XRRConfigSizes(sc, &nsize);
132 printf ( "XRandr not available\n") ;
135 Status status = XRRSetScreenConfig ( dpy, sc, DefaultRootWindow (dpy), (SizeID) sizeindex, (Rotation) (rotation | reflection), CurrentTime);
137 bool rcvdrrnotify = false ;
138 bool rcvdconfignotify = false ;
139 if ( status == RRSetConfigSuccess)
141 printf("Screen rotation changed");