Changeset View
Changeset View
Standalone View
Standalone View
common/orientation_sensor.cpp
- This file was added.
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 | #include <QOrientationReading> | ||||
21 | | ||||
22 | OrientationSensor::OrientationSensor(QObject *parent) | ||||
23 | : QObject(parent) | ||||
24 | , m_sensor(new QOrientationSensor(this)) | ||||
25 | { | ||||
26 | connect(m_sensor, &QOrientationSensor::activeChanged, this, &OrientationSensor::refresh); | ||||
27 | | ||||
28 | // Start the sensor once in the beginning to check if it is available. | ||||
29 | m_sensor->start(); | ||||
davidedmundson: Do we need to set mode to QSensor::FixedOrientation
it's not clear what the default is in the… | |||||
It says the default is fixed orientation: https://doc.qt.io/qt-5/qsensor.html#orientation romangg: It says the default is fixed orientation: https://doc.qt.io/qt-5/qsensor.html#orientation | |||||
30 | } | ||||
It's very wasteful to have this always on when we're not in autorotate mode. From a mobile POV, that's an entire sensor being active and causing wakeups. We also want it off when the screen is on dpms. davidedmundson: It's very wasteful to have this always on when we're not in autorotate mode.
From a mobile POV… | |||||
Yea, we need to start it in the beginning to see if a sensor is available. But we could cache the result and then shut it off again in case auto rotation or screen is off. Needs some more logic though. romangg: Yea, we need to start it in the beginning to see if a sensor is available. But we could cache… | |||||
From what I can tell, we can call if (!sensor->connectToBackend()) { in the constructor and then we have the option to call start/stop whenever. davidedmundson: From what I can tell, we can call
if (!sensor->connectToBackend()) {
m_available = false;
}… | |||||
romangg: Yea, I overlooked this getter. Will replace. | |||||
31 | | ||||
32 | OrientationSensor::~OrientationSensor() = default; | ||||
33 | | ||||
34 | void OrientationSensor::updateState() | ||||
35 | { | ||||
36 | auto toOrientation = [] (QOrientationReading *reading) { | ||||
37 | switch (reading->orientation()) { | ||||
38 | case QOrientationReading::Undefined: | ||||
39 | return DeviceOrientation::Undefined; | ||||
40 | case QOrientationReading::TopUp: | ||||
41 | return DeviceOrientation::TopUp; | ||||
42 | case QOrientationReading::TopDown: | ||||
43 | return DeviceOrientation::TopDown; | ||||
44 | case QOrientationReading::LeftUp: | ||||
45 | return DeviceOrientation::LeftUp; | ||||
46 | case QOrientationReading::RightUp: | ||||
47 | return DeviceOrientation::RightUp; | ||||
48 | case QOrientationReading::FaceUp: | ||||
49 | return DeviceOrientation::FaceUp; | ||||
50 | case QOrientationReading::FaceDown: | ||||
51 | return DeviceOrientation::FaceDown; | ||||
52 | default: | ||||
53 | Q_UNREACHABLE(); | ||||
54 | } | ||||
55 | }; | ||||
56 | const auto orientation = toOrientation(m_sensor->reading()); | ||||
57 | if (m_value != orientation) { | ||||
58 | m_value = orientation; | ||||
59 | Q_EMIT valueChanged(orientation); | ||||
60 | } | ||||
61 | } | ||||
62 | | ||||
63 | void OrientationSensor::refresh() | ||||
64 | { | ||||
65 | if (m_sensor->isActive()) { | ||||
66 | m_available = true; | ||||
67 | if (m_enabled) { | ||||
68 | updateState(); | ||||
69 | } | ||||
70 | Q_EMIT availableChanged(true); | ||||
71 | } else { | ||||
72 | Q_EMIT availableChanged(false); | ||||
73 | } | ||||
74 | } | ||||
75 | | ||||
76 | DeviceOrientation OrientationSensor::value() const | ||||
77 | { | ||||
78 | return m_value; | ||||
79 | } | ||||
80 | | ||||
81 | bool OrientationSensor::available() const | ||||
82 | { | ||||
83 | return m_available; | ||||
84 | } | ||||
85 | | ||||
86 | bool OrientationSensor::enabled() const | ||||
87 | { | ||||
88 | return m_sensor->isActive(); | ||||
89 | } | ||||
90 | | ||||
91 | void OrientationSensor::setEnabled(bool enable) | ||||
92 | { | ||||
93 | if (m_enabled == enable) { | ||||
94 | return; | ||||
95 | } | ||||
96 | m_enabled = enable; | ||||
97 | | ||||
98 | if (enable) { | ||||
99 | connect(m_sensor, &QOrientationSensor::readingChanged, | ||||
100 | this, &OrientationSensor::updateState); | ||||
101 | m_sensor->start(); | ||||
102 | } else { | ||||
103 | disconnect(m_sensor, &QOrientationSensor::readingChanged, | ||||
104 | this, &OrientationSensor::updateState); | ||||
105 | m_value = DeviceOrientation::Undefined; | ||||
106 | } | ||||
107 | Q_EMIT enabledChanged(enable); | ||||
108 | } |
Do we need to set mode to QSensor::FixedOrientation
it's not clear what the default is in the docs