File indexing completed on 2024-05-05 17:42:30

0001 /*
0002  * SPDX-FileCopyrightText: 2022 by Devin Lin <devin@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "screenrotationutil.h"
0008 
0009 #include <fcntl.h>
0010 #include <unistd.h>
0011 
0012 #include <QDebug>
0013 
0014 ScreenRotationUtil::ScreenRotationUtil(QObject *parent)
0015     : QObject{parent}
0016 {
0017     m_kscreenInterface = new org::kde::KScreen(QStringLiteral("org.kde.kded5"), QStringLiteral("/modules/kscreen"), QDBusConnection::sessionBus(), this);
0018 }
0019 
0020 bool ScreenRotationUtil::screenRotation()
0021 {
0022     QDBusPendingReply<bool> reply = m_kscreenInterface->getAutoRotate();
0023     reply.waitForFinished();
0024     if (reply.isError()) {
0025         qWarning() << "Getting auto rotate failed:" << reply.error().name() << reply.error().message();
0026         return false;
0027     } else {
0028         return reply.value();
0029     }
0030 }
0031 
0032 void ScreenRotationUtil::setScreenRotation(bool value)
0033 {
0034     QDBusPendingReply<> reply = m_kscreenInterface->setAutoRotate(value);
0035     reply.waitForFinished();
0036     if (reply.isError()) {
0037         qWarning() << "Setting auto rotate failed:" << reply.error().name() << reply.error().message();
0038     } else {
0039         Q_EMIT screenRotationChanged(value);
0040     }
0041 }
0042 
0043 bool ScreenRotationUtil::isAvailable()
0044 {
0045     QDBusPendingReply<bool> reply = m_kscreenInterface->isAutoRotateAvailable();
0046     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
0047 
0048     connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
0049         QDBusPendingReply<bool> reply = *watcher;
0050         if (reply.isError()) {
0051             qWarning() << "Getting available failed:" << reply.error().name() << reply.error().message();
0052         } else {
0053             // make sure we don't go into an infinite loop
0054             if (m_available != reply.value()) {
0055                 Q_EMIT availableChanged(m_available);
0056             }
0057 
0058             m_available = reply.value();
0059         }
0060         watcher->deleteLater();
0061     });
0062 
0063     return m_available;
0064 }