From fed3b28781f4df5805b9d58c92debf607e190835 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Fri, 11 Oct 2013 10:28:47 +0100 Subject: [PATCH] Add systemd suspend support --- CMakeLists.txt | 3 ++ src/tomahawk/CMakeLists.linux.cmake | 4 ++ src/tomahawk/CMakeLists.txt | 1 + src/tomahawk/Config.h.in | 1 + src/tomahawk/TomahawkApp.cpp | 8 ++++ src/tomahawk/linux/SystemdHandler.cpp | 66 +++++++++++++++++++++++++++ src/tomahawk/linux/SystemdHandler.h | 44 ++++++++++++++++++ 7 files changed, 127 insertions(+) create mode 100644 src/tomahawk/linux/SystemdHandler.cpp create mode 100644 src/tomahawk/linux/SystemdHandler.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b4d0f3a8..d3ab6e445 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,10 +47,13 @@ option(WITH_BINARY_ATTICA "Enable support for downloading binary resolvers autom option(LEGACY_KDE_INTEGRATION "Install tomahawk.protocol file, deprecated since 4.6.0" OFF) option(WITH_KDE4 "Build with support for KDE specific stuff" ON) +find_package(QtSystemd QUIET) CMAKE_DEPENDENT_OPTION(WITH_UPOWER "Build with support for UPower events" ON "UNIX;NOT APPLE" OFF) CMAKE_DEPENDENT_OPTION(WITH_GNOMESHORTCUTHANDLER "Build with shortcut handler for GNOME" ON "UNIX;NOT APPLE" OFF) +CMAKE_DEPENDENT_OPTION(WITH_SYSTEMD "Build with support for systemd events" ON + "QtSystemd_FOUND" OFF) IF( CMAKE_SYSTEM_PROCESSOR MATCHES "arm" ) message(STATUS "Build of breakpad library disabled on this platform.") diff --git a/src/tomahawk/CMakeLists.linux.cmake b/src/tomahawk/CMakeLists.linux.cmake index e9c357ae8..b382dc773 100644 --- a/src/tomahawk/CMakeLists.linux.cmake +++ b/src/tomahawk/CMakeLists.linux.cmake @@ -15,6 +15,10 @@ IF( WITH_GNOMESHORTCUTHANDLER ) SET( tomahawkSources ${tomahawkSources} linux/GnomeShortcutHandler.cpp ) ENDIF( WITH_GNOMESHORTCUTHANDLER ) +IF( WITH_SYSTEMD ) + SET( tomahawkSources ${tomahawkSources} linux/SystemdHandler.cpp ) +ENDIF( WITH_SYSTEMD ) + INSTALL( FILES ${CMAKE_SOURCE_DIR}/data/icons/tomahawk-icon.svg RENAME tomahawk.svg DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps ) INSTALL( FILES ${CMAKE_SOURCE_DIR}/admin/unix/tomahawk.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications ) diff --git a/src/tomahawk/CMakeLists.txt b/src/tomahawk/CMakeLists.txt index 1d18ea3e1..9f706b260 100644 --- a/src/tomahawk/CMakeLists.txt +++ b/src/tomahawk/CMakeLists.txt @@ -185,6 +185,7 @@ TARGET_LINK_LIBRARIES( tomahawk_bin ${ECHONEST_LIBRARIES} ${QJSON_LIBRARIES} ${TAGLIB_LIBRARIES} + ${QTSYSTEMD_LIBRARIES} ) IF( APPLE ) diff --git a/src/tomahawk/Config.h.in b/src/tomahawk/Config.h.in index e1d5aedbf..43f589146 100644 --- a/src/tomahawk/Config.h.in +++ b/src/tomahawk/Config.h.in @@ -18,6 +18,7 @@ #cmakedefine WITH_QtSparkle #cmakedefine WITH_UPOWER #cmakedefine WITH_GNOMESHORTCUTHANDLER +#cmakedefine WITH_SYSTEMD #cmakedefine LIBLASTFM_FOUND #cmakedefine QCA2_FOUND diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index ff151fc7d..767831052 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -92,6 +92,10 @@ #include "linux/GnomeShortcutHandler.h" #endif +#ifdef WITH_SYSTEMD + #include "linux/SystemdHandler.h" +#endif + #ifndef ENABLE_HEADLESS #include #endif @@ -568,6 +572,10 @@ TomahawkApp::initEnergyEventHandler() UPowerHandler* upower = new UPowerHandler( this ); upower->registerHandler(); #endif // WITH_UPOWER + +#ifdef WITH_SYSTEMD + new SystemdHandler( this ); +#endif // WITH_SYSTEMD } diff --git a/src/tomahawk/linux/SystemdHandler.cpp b/src/tomahawk/linux/SystemdHandler.cpp new file mode 100644 index 000000000..96b072f73 --- /dev/null +++ b/src/tomahawk/linux/SystemdHandler.cpp @@ -0,0 +1,66 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk 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. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#include "SystemdHandler.h" + +#include "accounts/AccountManager.h" +#include "utils/Logger.h" + +#include +#include + +#define SYSTEMD_RESUME_DELAY 2000 + +namespace Tomahawk { + +SystemdHandler::SystemdHandler(QObject *parent) : + QObject(parent) +{ + connect( Systemd::Logind::notifier(), SIGNAL( prepareForSleep( bool ) ), + SLOT( handleSleep( bool ) ) ); +} + + +void +SystemdHandler::handleSleep( bool active ) +{ + if ( active ) + { + QMutexLocker locker( &m_mutex ); + tLog( LOGVERBOSE ) << Q_FUNC_INFO << "About to sleep so disconnecting all accounts"; + Tomahawk::Accounts::AccountManager::instance()->disconnectAll(); + } + else + { + m_mutex.lock(); + // Delay resuming for other wakeup actions, e.g. reconnecting to the network, to take place. + QTimer::singleShot( SYSTEMD_RESUME_DELAY, this, SLOT( actualResume() ) ); + } + +} + + +void +SystemdHandler::actualResume() +{ + tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Awake from sleep so connecting all accounts"; + Tomahawk::Accounts::AccountManager::instance()->connectAll(); + m_mutex.unlock(); +} + +} // namespace Tomahawk diff --git a/src/tomahawk/linux/SystemdHandler.h b/src/tomahawk/linux/SystemdHandler.h new file mode 100644 index 000000000..87dbf0dba --- /dev/null +++ b/src/tomahawk/linux/SystemdHandler.h @@ -0,0 +1,44 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk 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. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#ifndef TOMAHAWK_SYSTEMDHANDLER_H +#define TOMAHAWK_SYSTEMDHANDLER_H + +#include +#include + +namespace Tomahawk { + +class SystemdHandler : public QObject +{ + Q_OBJECT +public: + explicit SystemdHandler(QObject *parent = 0); + +private slots: + void handleSleep( bool active ); + void actualResume(); + +private: + QMutex m_mutex; + +}; + +} // namespace Tomahawk + +#endif // TOMAHAWK_SYSTEMDHANDLER_H