~neon/project-neon/kscreen

« back to all changes in this revision

Viewing changes to common/orientation_sensor.cpp

  • Committer: Roman Gilg
  • Date: 2020-01-15 13:57:12 UTC
  • Revision ID: git-v1:8032881459d18c31b00d1023f814d9ed5049532a
feat(kded): add orientation sensor

Summary:
With this patch KScreen controls rotation of internal outputs if an orientation
sensor is present and emitting rotation values.

The default behavior is auto-rotation but can be configured through control
files.

This functionality is developed for the Wayland session but might potentially
also work on X11, what needs to be tested more.

Test Plan: Screen of convertible with orientation sensor rotates.

Reviewers: #plasma, davidedmundson

Reviewed By: #plasma, davidedmundson

Subscribers: plasma-devel, davidedmundson

Tags: #plasma

Maniphest Tasks: T11475

Differential Revision: https://phabricator.kde.org/D26037

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
Copyright © 2019 Roman Gilg <subdiff@gmail.com>
 
3
 
 
4
This program is free software; you can redistribute it and/or modify
 
5
it under the terms of the GNU General Public License as published by
 
6
the Free Software Foundation; either version 2 of the License, or
 
7
(at your option) any later version.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
GNU General Public License for more details.
 
13
 
 
14
You should have received a copy of the GNU General Public License
 
15
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
*********************************************************************/
 
17
#include "orientation_sensor.h"
 
18
 
 
19
#include <QOrientationSensor>
 
20
 
 
21
OrientationSensor::OrientationSensor(QObject *parent)
 
22
    : QObject(parent)
 
23
    , m_sensor(new QOrientationSensor(this))
 
24
{
 
25
    connect(m_sensor, &QOrientationSensor::activeChanged, this, &OrientationSensor::refresh);
 
26
}
 
27
 
 
28
OrientationSensor::~OrientationSensor() = default;
 
29
 
 
30
void OrientationSensor::updateState()
 
31
{
 
32
    const auto orientation = m_sensor->reading()->orientation();
 
33
    if (m_value != orientation) {
 
34
        m_value = orientation;
 
35
        Q_EMIT valueChanged(orientation);
 
36
    }
 
37
}
 
38
 
 
39
void OrientationSensor::refresh()
 
40
{
 
41
    if (m_sensor->isActive()) {
 
42
        if (m_enabled) {
 
43
            updateState();
 
44
        }
 
45
        Q_EMIT availableChanged(true);
 
46
    } else {
 
47
        Q_EMIT availableChanged(false);
 
48
    }
 
49
}
 
50
 
 
51
QOrientationReading::Orientation OrientationSensor::value() const
 
52
{
 
53
    return m_value;
 
54
}
 
55
 
 
56
bool OrientationSensor::available() const
 
57
{
 
58
    return  m_sensor->connectToBackend();
 
59
}
 
60
 
 
61
bool OrientationSensor::enabled() const
 
62
{
 
63
    return m_sensor->isActive();
 
64
}
 
65
 
 
66
void OrientationSensor::setEnabled(bool enable)
 
67
{
 
68
    if (m_enabled == enable) {
 
69
        return;
 
70
    }
 
71
    m_enabled = enable;
 
72
 
 
73
    if (enable) {
 
74
        connect(m_sensor, &QOrientationSensor::readingChanged,
 
75
                this, &OrientationSensor::updateState);
 
76
        m_sensor->start();
 
77
    } else {
 
78
        disconnect(m_sensor, &QOrientationSensor::readingChanged,
 
79
                   this, &OrientationSensor::updateState);
 
80
        m_value = QOrientationReading::Undefined;
 
81
    }
 
82
    Q_EMIT enabledChanged(enable);
 
83
}