File indexing completed on 2024-05-19 05:32:29

0001 /*
0002     SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
0003     SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 #include "orientationsensor.h"
0008 
0009 #include <QOrientationSensor>
0010 
0011 namespace KWin
0012 {
0013 
0014 OrientationSensor::OrientationSensor()
0015     : m_sensor(std::make_unique<QOrientationSensor>())
0016     , m_reading(std::make_unique<QOrientationReading>())
0017 {
0018     m_reading->setOrientation(QOrientationReading::Orientation::Undefined);
0019 }
0020 
0021 OrientationSensor::~OrientationSensor() = default;
0022 
0023 void OrientationSensor::setEnabled(bool enable)
0024 {
0025     if (enable) {
0026         connect(m_sensor.get(), &QOrientationSensor::readingChanged, this, &OrientationSensor::update, Qt::UniqueConnection);
0027         m_sensor->start();
0028     } else {
0029         disconnect(m_sensor.get(), &QOrientationSensor::readingChanged, this, &OrientationSensor::update);
0030         m_reading->setOrientation(QOrientationReading::Undefined);
0031     }
0032 }
0033 
0034 QOrientationReading *OrientationSensor::reading() const
0035 {
0036     return m_reading.get();
0037 }
0038 
0039 void OrientationSensor::update()
0040 {
0041     if (auto reading = m_sensor->reading()) {
0042         if (m_reading->orientation() != reading->orientation()) {
0043             m_reading->setOrientation(reading->orientation());
0044             Q_EMIT orientationChanged();
0045         }
0046     } else if (m_reading->orientation() != QOrientationReading::Orientation::Undefined) {
0047         m_reading->setOrientation(QOrientationReading::Orientation::Undefined);
0048         Q_EMIT orientationChanged();
0049     }
0050 }
0051 
0052 }
0053 
0054 #include "moc_orientationsensor.cpp"