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 | | ||||
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 | | ||||
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 | void OrientationSensor::updateState() | ||||
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 | 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 | } |
Do we need to set mode to QSensor::FixedOrientation
it's not clear what the default is in the docs