diff --git a/CMakeLists.txt b/CMakeLists.txt index c0efd1b5..c9de75e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,8 @@ set(COMMON_SOURCES ${SRC_DIR}/utilsadapter.cpp ${SRC_DIR}/dbuserrorhandler.cpp ${SRC_DIR}/xrectsel.c - ${SRC_DIR}/moderatorlistmodel.cpp) + ${SRC_DIR}/moderatorlistmodel.cpp + ${SRC_DIR}/screensaver.cpp) set(COMMON_HEADERS ${SRC_DIR}/avatarimageprovider.h @@ -120,7 +121,8 @@ set(COMMON_HEADERS ${SRC_DIR}/utilsadapter.h ${SRC_DIR}/dbuserrorhandler.h ${SRC_DIR}/xrectsel.h - ${SRC_DIR}/moderatorlistmodel.h) + ${SRC_DIR}/moderatorlistmodel.h + ${SRC_DIR}/screensaver.h) find_package(PkgConfig REQUIRED) pkg_check_modules(LIBNM libnm) diff --git a/jami-qt.pro b/jami-qt.pro index 0656c657..37403b5b 100644 --- a/jami-qt.pro +++ b/jami-qt.pro @@ -159,6 +159,7 @@ HEADERS += \ src/avatarimageprovider.h \ src/moderatorlistmodel.h \ src/networkmanager.h \ + src/screensaver.h \ src/smartlistmodel.h \ src/updatemanager.h \ src/utils.h \ @@ -210,6 +211,7 @@ SOURCES += \ src/moderatorlistmodel.cpp \ src/networkmanager.cpp \ src/runguard.cpp \ + src/screensaver.cpp \ src/updatemanager.cpp \ src/webchathelpers.cpp \ src/main.cpp \ diff --git a/src/calladapter.cpp b/src/calladapter.cpp index eda844a2..4e3e23e4 100644 --- a/src/calladapter.cpp +++ b/src/calladapter.cpp @@ -432,6 +432,7 @@ CallAdapter::connectCallModel(const QString& accountId) } } } + preventScreenSaver(false); break; } case lrc::api::call::Status::CONNECTED: @@ -441,6 +442,7 @@ CallAdapter::connectCallModel(const QString& accountId) accInfo.conversationModel->selectConversation(convInfo.uid); } updateCall(convInfo.uid, accountId); + preventScreenSaver(true); break; } case lrc::api::call::Status::PAUSED: @@ -865,3 +867,14 @@ CallAdapter::setTime(const QString& accountId, const QString& convUid) emit updateTimeText(timeString); } } + +void +CallAdapter::preventScreenSaver(bool state) +{ + if (state) { + if (!screenSaver.isInhibited()) + screenSaver.inhibit(); + } else if (screenSaver.isInhibited()) { + screenSaver.uninhibit(); + } +}; diff --git a/src/calladapter.h b/src/calladapter.h index ed079e09..e1c01adc 100644 --- a/src/calladapter.h +++ b/src/calladapter.h @@ -22,6 +22,7 @@ #include "lrcinstance.h" #include "qmladapterbase.h" #include "globalsystemtray.h" +#include "screensaver.h" #include #include @@ -129,4 +130,7 @@ private: void updateCallOverlay(const lrc::api::conversation::Info& convInfo); void setTime(const QString& accountId, const QString& convUid); QTimer* oneSecondTimer_; + ScreenSaver screenSaver; + + void preventScreenSaver(bool state); }; diff --git a/src/screensaver.cpp b/src/screensaver.cpp new file mode 100644 index 00000000..14a08ece --- /dev/null +++ b/src/screensaver.cpp @@ -0,0 +1,129 @@ +/*! + * Copyright (C) 2021 by Savoir-faire Linux + * Author: Albert Babí + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "screensaver.h" + +#include + +ScreenSaver::ScreenSaver(QObject* parent) +#ifdef Q_OS_LINUX + : QObject(parent), + sessionBus_(QDBusConnection::sessionBus()), + screenSaverInterface_(nullptr) +{ + request_ = 0u; + createInterface(); +} +#else + : QObject(parent) {} +#endif + +#ifdef Q_OS_LINUX +bool +ScreenSaver::createInterface(void) +{ + if (!sessionBus_.isConnected()) { + qWarning() << "dbus not connected"; + return false; + } + + for(int i = 0; i <= N_SERVICES ; i++) { + screenSaverInterface_ = new QDBusInterface(services_[i], + paths_[i], + services_[i], + sessionBus_); + if (screenSaverInterface_->isValid()) { + qDebug() << "Screen saver dbus interface: " << services_[i]; + return true; + } + } + qWarning() << "Cannot find dbus interface for screen saver"; + screenSaverInterface_ = nullptr; + return false; +} +#endif + +bool +ScreenSaver::inhibit(void) +{ + if (isInhibited()) { + qDebug() << "Screen saver already inhibited"; + return false; + } +#ifdef Q_OS_LINUX + if (!screenSaverInterface_) { + if (!createInterface()) { + return false; + } + } + + QDBusReply reply = + screenSaverInterface_->call("Inhibit", "jami-qt", "In a call"); + if (reply.isValid()) { + qDebug() << "Screen saver inhibited"; + request_ = static_cast(reply.value()); + return true; + } else { + QDBusError error = reply.error(); + qDebug() << "Error inhibiting screen saver: " + << error.message() + << error.name(); + } +#endif + return false; +} + +bool +ScreenSaver::uninhibit(void) +{ + if (!isInhibited()) { + qDebug() << "Screen saver is not inhibited"; + return false; + } +#ifdef Q_OS_LINUX + if (!screenSaverInterface_) { + if (!createInterface()) { + return false; + } + } + + QDBusReply reply = + screenSaverInterface_->call("UnInhibit", static_cast(request_)); + if (reply.isValid()) { + qDebug() << "Screen saver uninhibited"; + request_ = 0u; + return true; + } else { + QDBusError error = reply.error(); + qDebug() << "Error uninhibiting screen saver: " + << error.message() + << error.name(); + } + request_ = 0u; +#endif + return false; +} + +bool +ScreenSaver::isInhibited(void) +{ +#ifdef Q_OS_LINUX + return request_ != 0u; +#endif + return false; +} diff --git a/src/screensaver.h b/src/screensaver.h new file mode 100644 index 00000000..f1de7a51 --- /dev/null +++ b/src/screensaver.h @@ -0,0 +1,55 @@ +/*! + * Copyright (C) 2021 by Savoir-faire Linux + * Author: Albert Babí + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +#ifdef Q_OS_LINUX +#include +#include +#define N_SERVICES 3 +#endif + +class ScreenSaver : public QObject +{ + Q_OBJECT +public: + explicit ScreenSaver(QObject* parent = nullptr); + virtual ~ScreenSaver() = default; + + bool inhibit(void); + bool uninhibit(void); + bool isInhibited(void); + +#ifdef Q_OS_LINUX +private: + bool createInterface(void); + QString services_[N_SERVICES] = { "org.freedesktop.ScreenSaver", + "org.gnome.ScreenSaver", + "org.mate.ScreenSaver" }; + + QString paths_[N_SERVICES] = { "/org/freedesktop/ScreenSaver", + "/org/gnome/ScreenSaver", + "/org/mate/ScreenSaver" }; + + uint request_; + QDBusConnection sessionBus_; + QDBusInterface* screenSaverInterface_; +#endif +};