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>
10 QMaemo5Rotator::QMaemo5Rotator(RotationBehavior behavior, QWidget *parent)
14 setCurrentBehavior(behavior);
17 QMaemo5Rotator::~QMaemo5Rotator()
19 QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
22 const QMaemo5Rotator::RotationBehavior QMaemo5Rotator::currentBehavior()
24 return _currentBehavior;
27 const QMaemo5Rotator::Orientation QMaemo5Rotator::currentOrientation()
29 return _currentOrientation;
32 void QMaemo5Rotator::setCurrentBehavior(QMaemo5Rotator::RotationBehavior value)
34 if (value == _currentBehavior && isSetUp)
38 _currentBehavior = value;
40 if (value == QMaemo5Rotator::AutomaticBehavior)
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)));
47 QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
49 if (value == QMaemo5Rotator::PortraitBehavior)
51 setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
55 setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
59 void QMaemo5Rotator::test()
61 QMaemo5Rotator::setRotation(up);
64 void QMaemo5Rotator::setCurrentOrientation(QMaemo5Rotator::Orientation value)
66 _currentOrientation = value;
67 QWidget *par = (QWidget*)parent();
71 case QMaemo5Rotator::PortraitOrientation:
74 par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
75 par->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
77 orientationChanged(QMaemo5Rotator::PortraitOrientation);
79 case QMaemo5Rotator::LandscapeOrientation:
82 par->setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
83 par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
85 orientationChanged(QMaemo5Rotator::LandscapeOrientation);
91 void QMaemo5Rotator::on_orientation_changed(const QString& newOrientation)
93 if (newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT) || newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT_INVERTED))
95 setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
99 setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
101 QApplication::desktop()->updateGeometry();
110 static bool QMaemo5Rotator::setRotation(direction dir)
112 Rotation rotation = dir;
114 Display * dpy = XOpenDisplay(NULL);
117 printf ( "Cannot open display %s\n", displayname ) ;
120 int screen = DefaultScreen ( dpy ) ;
121 Window root = RootWindow ( dpy, screen ) ;
123 XSelectInput (dpy, root, StructureNotifyMask);
124 XRRSelectInput (dpy, root, RRScreenChangeNotifyMask);
127 XRRQueryExtension ( dpy, &eventbase, &errorbase ) ;
129 XRRScreenConfiguration * sc = XRRGetScreenInfo (dpy, root);
132 printf ( "Cannot get screen info\n" ) ;
136 XRRScreenSize * sizes = XRRConfigSizes(sc, &nsize);
140 printf ( "XRandr not available\n") ;
143 Status status = XRRSetScreenConfig ( dpy, sc, DefaultRootWindow (dpy), (SizeID) sizeindex, (Rotation) (rotation | reflection), CurrentTime);
145 bool rcvdrrnotify = false ;
146 bool rcvdconfignotify = false ;
147 if ( status == RRSetConfigSuccess)
149 printf("Screen rotation changed");