From e0b1a5715bd7d0bea7867fda14c226ca0000dd1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Fl=C3=B6ser?= Date: Thu, 9 Nov 2017 18:03:15 +0100 Subject: [PATCH] Add DBus interface to OrientationSensor and a persistent configuration Summary: The initial state for user enabled is now read from config. In addition a DBus interface is provided exporting this property. This allows KScreen to determine whether automatic screen rotation is available and whether the user has it enabled or not. Furthermore KScreen can change the property and this gets stored into the configuration. Thus KScreen can offer a user interface to enable/disable automatic screen rotation as well as enabling/disabling the manual rotation based on the current user settings. Test Plan: Not yet tested, coded on the system which doesn't have an orientation sensor Reviewers: #kwin, #plasma, sebas, davidedmundson Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D8738 --- CMakeLists.txt | 1 + autotests/CMakeLists.txt | 7 +++++++ org.kde.kwin.OrientationSensor.xml | 6 ++++++ orientation_sensor.cpp | 27 +++++++++++++++++++++++++++ orientation_sensor.h | 18 ++++++++++++++++++ screens.cpp | 8 ++++++++ screens.h | 6 ------ tests/CMakeLists.txt | 10 ++++++++-- 8 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 org.kde.kwin.OrientationSensor.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 27928fc1a5..75313d3d08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -549,6 +549,7 @@ qt5_add_dbus_adaptor( kwin_KDEINIT_SRCS org.kde.KWin.xml dbusinterface.h KWin::D qt5_add_dbus_adaptor( kwin_KDEINIT_SRCS org.kde.kwin.Compositing.xml dbusinterface.h KWin::CompositorDBusInterface ) qt5_add_dbus_adaptor( kwin_KDEINIT_SRCS org.kde.kwin.ColorCorrect.xml colorcorrection/colorcorrectdbusinterface.h KWin::ColorCorrect::ColorCorrectDBusInterface ) qt5_add_dbus_adaptor( kwin_KDEINIT_SRCS ${kwin_effects_dbus_xml} effects.h KWin::EffectsHandlerImpl ) +qt5_add_dbus_adaptor( kwin_KDEINIT_SRCS org.kde.kwin.OrientationSensor.xml orientation_sensor.h KWin::OrientationSensor) qt5_add_dbus_interface( kwin_KDEINIT_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.ScreenSaver.xml screenlocker_interface) diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index d2992fb808..b5cfbc8265 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -168,6 +168,7 @@ set( testScriptedEffectLoader_SRCS ../orientation_sensor.cpp ) kconfig_add_kcfg_files(testScriptedEffectLoader_SRCS ../settings.kcfgc) +qt5_add_dbus_adaptor( testScriptedEffectLoader_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../org.kde.kwin.OrientationSensor.xml ${CMAKE_CURRENT_SOURCE_DIR}/../orientation_sensor.h KWin::OrientationSensor) add_executable( testScriptedEffectLoader ${testScriptedEffectLoader_SRCS}) target_link_libraries(testScriptedEffectLoader @@ -239,10 +240,12 @@ set( testScreens_SRCS ../orientation_sensor.cpp ) kconfig_add_kcfg_files(testScreens_SRCS ../settings.kcfgc) +qt5_add_dbus_adaptor( testScreens_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../org.kde.kwin.OrientationSensor.xml ${CMAKE_CURRENT_SOURCE_DIR}/../orientation_sensor.h KWin::OrientationSensor) add_executable( testScreens ${testScreens_SRCS}) target_include_directories(testScreens BEFORE PRIVATE ./) target_link_libraries(testScreens + Qt5::DBus Qt5::Sensors Qt5::Test Qt5::X11Extras @@ -273,9 +276,12 @@ set( testXRandRScreens_SRCS ../orientation_sensor.cpp ) kconfig_add_kcfg_files(testXRandRScreens_SRCS ../settings.kcfgc) +qt5_add_dbus_adaptor( testXRandRScreens_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../org.kde.kwin.OrientationSensor.xml ${CMAKE_CURRENT_SOURCE_DIR}/../orientation_sensor.h KWin::OrientationSensor) add_executable( testXRandRScreens ${testXRandRScreens_SRCS} ) target_link_libraries( testXRandRScreens Qt5::Test + Qt5::DBus + Qt5::Gui Qt5::Sensors Qt5::Widgets KF5::ConfigCore @@ -316,6 +322,7 @@ set( testScreenEdges_SRCS ) kconfig_add_kcfg_files(testScreenEdges_SRCS ../settings.kcfgc) qt5_add_dbus_interface( testScreenEdges_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../org.freedesktop.ScreenSaver.xml screenlocker_interface) +qt5_add_dbus_adaptor( testScreenEdges_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../org.kde.kwin.OrientationSensor.xml ${CMAKE_CURRENT_SOURCE_DIR}/../orientation_sensor.h KWin::OrientationSensor) add_executable( testScreenEdges ${testScreenEdges_SRCS}) set_target_properties(testScreenEdges PROPERTIES COMPILE_DEFINITIONS "NO_NONE_WINDOW") diff --git a/org.kde.kwin.OrientationSensor.xml b/org.kde.kwin.OrientationSensor.xml new file mode 100644 index 0000000000..12f245c624 --- /dev/null +++ b/org.kde.kwin.OrientationSensor.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/orientation_sensor.cpp b/orientation_sensor.cpp index 7d9dcd5c85..c78a3794fc 100644 --- a/orientation_sensor.cpp +++ b/orientation_sensor.cpp @@ -18,10 +18,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ #include "orientation_sensor.h" +#include #include #include +#include #include #include @@ -84,14 +86,26 @@ void OrientationSensor::setEnabled(bool enabled) } m_enabled = enabled; if (m_enabled) { + loadConfig(); setupStatusNotifier(); + m_adaptor = new OrientationSensorAdaptor(this); } else { delete m_sni; m_sni = nullptr; + delete m_adaptor; + m_adaptor = nullptr; } startStopSensor(); } +void OrientationSensor::loadConfig() +{ + if (!m_config) { + return; + } + m_userEnabled = m_config->group("OrientationSensor").readEntry("Enabled", true); +} + void OrientationSensor::setupStatusNotifier() { if (m_sni) { @@ -110,6 +124,7 @@ void OrientationSensor::setupStatusNotifier() [this] { m_userEnabled = !m_userEnabled; startStopSensor(); + emit userEnabledChanged(m_userEnabled); } ); } @@ -123,4 +138,16 @@ void OrientationSensor::startStopSensor() } } +void OrientationSensor::setUserEnabled(bool enabled) +{ + if (m_userEnabled == enabled) { + return; + } + m_userEnabled = enabled; + if (m_config) { + m_config->group("OrientationSensor").writeEntry("Enabled", m_userEnabled); + } + emit userEnabledChanged(m_userEnabled); +} + } diff --git a/orientation_sensor.h b/orientation_sensor.h index 5234360c2e..29b2ef703d 100644 --- a/orientation_sensor.h +++ b/orientation_sensor.h @@ -21,9 +21,12 @@ along with this program. If not, see . #include +#include + #include class QOrientationSensor; +class OrientationSensorAdaptor; class KStatusNotifierItem; namespace KWin @@ -32,6 +35,8 @@ namespace KWin class KWIN_EXPORT OrientationSensor : public QObject { Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.kde.kwin.OrientationSensor") + Q_PROPERTY(bool userEnabled READ isUserEnabled WRITE setUserEnabled NOTIFY userEnabledChanged) public: explicit OrientationSensor(QObject *parent = nullptr); ~OrientationSensor(); @@ -56,17 +61,30 @@ public: return m_orientation; } + void setConfig(KSharedConfig::Ptr config) { + m_config = config; + } + + bool isUserEnabled() const { + return m_userEnabled; + } + void setUserEnabled(bool enabled); + Q_SIGNALS: void orientationChanged(); + void userEnabledChanged(bool); private: void setupStatusNotifier(); void startStopSensor(); + void loadConfig(); QOrientationSensor *m_sensor; bool m_enabled = false; bool m_userEnabled = true; Orientation m_orientation = Orientation::Undefined; KStatusNotifierItem *m_sni = nullptr; + KSharedConfig::Ptr m_config; + OrientationSensorAdaptor *m_adaptor = nullptr; }; diff --git a/screens.cpp b/screens.cpp index d6df56f9b5..d28f75ce1a 100644 --- a/screens.cpp +++ b/screens.cpp @@ -226,6 +226,14 @@ Qt::ScreenOrientation Screens::orientation(int screen) const return Qt::PrimaryOrientation; } +void Screens::setConfig(KSharedConfig::Ptr config) +{ + m_config = config; + if (m_orientationSensor) { + m_orientationSensor->setConfig(config); + } +} + BasicScreens::BasicScreens(Platform *backend, QObject *parent) : Screens(parent) , m_backend(backend) diff --git a/screens.h b/screens.h index 76add1381b..4fda86fb9f 100644 --- a/screens.h +++ b/screens.h @@ -223,12 +223,6 @@ private: QVector m_scales; }; -inline -void Screens::setConfig(KSharedConfig::Ptr config) -{ - m_config = config; -} - inline int Screens::count() const { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 35eae423a3..48f307c77f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -44,5 +44,11 @@ target_link_libraries(pointergestures Qt5::Gui Qt5::Quick KF5::WaylandClient) add_executable(cursorhotspottest cursorhotspottest.cpp) target_link_libraries(cursorhotspottest Qt5::Widgets) -add_executable(orientationtest orientationtest.cpp ../orientation_sensor.cpp) -target_link_libraries(orientationtest Qt5::Widgets Qt5::Sensors KF5::Notifications KF5::I18n) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) +set( orientationtest_SRCS + orientationtest.cpp + ../orientation_sensor.cpp +) +qt5_add_dbus_adaptor( orientationtest_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../org.kde.kwin.OrientationSensor.xml ${CMAKE_CURRENT_SOURCE_DIR}/../orientation_sensor.h KWin::OrientationSensor) +add_executable(orientationtest ${orientationtest_SRCS}) +target_link_libraries(orientationtest Qt5::DBus Qt5::Widgets Qt5::Sensors KF5::ConfigCore KF5::Notifications KF5::I18n)