-
Notifications
You must be signed in to change notification settings - Fork 20
/
screenrotator.cpp
92 lines (74 loc) · 1.91 KB
/
screenrotator.cpp
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
#include "screenrotator.h"
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QProcess>
#include <KPluginFactory>
ScreenRotator::ScreenRotator(QObject *parent, const QVariantList &) :
KDEDModule(parent)
{
sensor = new QOrientationSensor(this);
sensor->start();
connect(sensor, &QOrientationSensor::readingChanged, this, &ScreenRotator::startProgress);
connect(&timer, &QTimer::timeout, this, &ScreenRotator::updateProgress);
updateOrientation();
progress = -1;
}
void ScreenRotator::startProgress() {
if (progress == -1) {
timer.start(25);
progress = 0;
}
}
void ScreenRotator::updateProgress() {
if (!sensor->reading()) return;
if (sensor->reading()->orientation() != currentOrientation) {
progress++;
QDBusMessage msg = QDBusMessage::createMethodCall(
QStringLiteral("org.kde.plasmashell"),
QStringLiteral("/org/kde/osdService"),
QStringLiteral("org.kde.osdService"),
QStringLiteral("mediaPlayerVolumeChanged")
);
msg.setArguments({progress, "screen", "view-refresh"});
QDBusConnection::sessionBus().asyncCall(msg);
if (progress == 100) {
updateOrientation();
timer.stop();
progress = -1;
}
} else {
timer.stop();
progress = -1;
}
}
void ScreenRotator::updateOrientation() {
if (!sensor->reading()) return;
currentOrientation = sensor->reading()->orientation();
QString o;
switch (currentOrientation) {
case QOrientationReading::TopUp:
o = "normal";
break;
case QOrientationReading::TopDown:
o = "bottom-up";
break;
case QOrientationReading::LeftUp:
o = "left-up";
break;
case QOrientationReading::RightUp:
o = "right-up";
break;
default:
o = "none";
return;
}
helper.start("orientation-helper", {"-r", o});
}
ScreenRotator::~ScreenRotator()
{
delete sensor;
}
K_PLUGIN_FACTORY(ScreenRotatorFactory,
registerPlugin<ScreenRotator>();)
#include "screenrotator.moc"