From b13f0690c8192d416c90333d084ca19d9bb6eea7 Mon Sep 17 00:00:00 2001 From: narunlifescience Date: Thu, 10 Dec 2015 00:43:52 -0600 Subject: [PATCH] Add an option to inhibit suspend while playing minor modifications --- src/CMakeLists.txt | 8 +- src/ui/dbusidlehandler.cpp | 78 +++++++++++ .../{dbusscreensaver.h => dbusidlehandler.h} | 23 ++-- src/ui/dbusscreensaver.cpp | 43 ------ src/ui/idlehandler.cpp | 126 ++++++++++++++++++ src/ui/idlehandler.h | 61 +++++++++ ...{macscreensaver.cpp => macidlehandler.cpp} | 38 +++++- src/ui/{macscreensaver.h => macidlehandler.h} | 19 +-- src/ui/mainwindow.cpp | 30 ++++- src/ui/mainwindow.h | 7 + src/ui/playbacksettingspage.cpp | 8 ++ src/ui/playbacksettingspage.ui | 42 +++++- src/ui/screensaver.cpp | 66 --------- src/ui/screensaver.h | 42 ------ src/ui/settingsdialog.h | 1 + src/ui/settingspage.h | 1 + src/ui/windowsidlehandler.cpp | 64 +++++++++ ...dowsscreensaver.h => windowsidlehandler.h} | 17 ++- src/ui/windowsscreensaver.cpp | 30 ----- src/visualisations/visualisationcontainer.cpp | 6 +- 20 files changed, 487 insertions(+), 223 deletions(-) create mode 100644 src/ui/dbusidlehandler.cpp rename src/ui/{dbusscreensaver.h => dbusidlehandler.h} (65%) delete mode 100644 src/ui/dbusscreensaver.cpp create mode 100644 src/ui/idlehandler.cpp create mode 100644 src/ui/idlehandler.h rename src/ui/{macscreensaver.cpp => macidlehandler.cpp} (52%) rename src/ui/{macscreensaver.h => macidlehandler.h} (69%) delete mode 100644 src/ui/screensaver.cpp delete mode 100644 src/ui/screensaver.h create mode 100644 src/ui/windowsidlehandler.cpp rename src/ui/{windowsscreensaver.h => windowsidlehandler.h} (70%) delete mode 100644 src/ui/windowsscreensaver.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0cb379451..8a3d6d6da 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -343,6 +343,7 @@ set(SOURCES ui/globalshortcutgrabber.cpp ui/globalshortcutssettingspage.cpp ui/iconloader.cpp + ui/idlehandler.cpp ui/mainwindow.cpp ui/networkproxysettingspage.cpp ui/networkremotesettingspage.cpp @@ -351,7 +352,6 @@ set(SOURCES ui/organiseerrordialog.cpp ui/playbacksettingspage.cpp ui/qtsystemtrayicon.cpp - ui/screensaver.cpp ui/settingsdialog.cpp ui/settingspage.cpp ui/standarditemiconloader.cpp @@ -860,7 +860,7 @@ optional_source(APPLE engines/osxdevicefinder.cpp networkremote/bonjour.mm ui/globalshortcutgrabber.mm - ui/macscreensaver.cpp + ui/macidlehandler.cpp ui/macsystemtrayicon.mm widgets/osd_mac.mm HEADERS @@ -875,7 +875,7 @@ optional_source(WIN32 SOURCES engines/directsounddevicefinder.cpp networkremote/tinysvcmdns.cpp - ui/windowsscreensaver.cpp + ui/windowsidlehandler.cpp widgets/osd_win.cpp INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/3rdparty/tinysvcmdns @@ -981,7 +981,7 @@ optional_source(HAVE_DBUS core/mpris1.cpp core/mpris2.cpp networkremote/avahi.cpp - ui/dbusscreensaver.cpp + ui/dbusidlehandler.cpp HEADERS core/mpris.h core/mpris1.h diff --git a/src/ui/dbusidlehandler.cpp b/src/ui/dbusidlehandler.cpp new file mode 100644 index 000000000..45412f1ac --- /dev/null +++ b/src/ui/dbusidlehandler.cpp @@ -0,0 +1,78 @@ +/* This file is part of Clementine. + Copyright 2010, David Sansome + Copyright 2015, Arun Narayanankutty + + Clementine 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. + + Clementine 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 Clementine. If not, see . +*/ + +#include "dbusidlehandler.h" + +#include +#include +#include + +#include + +#include "core/logging.h" + +DBusIdleHandler::DBusIdleHandler(const QString& service, const QString& path, + const QString& interface) + : service_(service), path_(path), interface_(interface) {} + +void DBusIdleHandler::Inhibit(const char* reason) { + QDBusInterface iface(service_, path_, + interface_, QDBusConnection::sessionBus()); + QDBusReply reply; + if (service_ == IdleHandler::kGnomePowermanagerService) { + reply = + iface.call("Inhibit", QCoreApplication::applicationName(), + quint32(0), QObject::tr(reason), + quint32(Inhibit_Suspend)); + } else { + reply = + iface.call("Inhibit", QCoreApplication::applicationName(), + QObject::tr("reason")); + } + + if (reply.isValid()) { + cookie_ = reply.value(); + } else { + qLog(Warning) << "Failed to inhibit screensaver/suspend dbus service: " + << service_ << " path: " << path_ << " interface: " + << interface_; + } +} + +void DBusIdleHandler::Uninhibit() { + QDBusInterface iface(service_, path_, + interface_, QDBusConnection::sessionBus()); + if (service_ == IdleHandler::kGnomePowermanagerService) { + iface.call("Uninhibit", cookie_); + } else { + iface.call("UnInhibit", cookie_); + } +} + +bool DBusIdleHandler::Isinhibited() { + QDBusInterface iface(service_, path_, + interface_, QDBusConnection::sessionBus()); + QDBusReply reply; + if (service_ == IdleHandler::kGnomePowermanagerService) { + reply = iface.call("IsInhibited", quint32(Inhibit_Suspend)); + } else { + reply = iface.call("HasInhibit"); + } + + return reply; +} diff --git a/src/ui/dbusscreensaver.h b/src/ui/dbusidlehandler.h similarity index 65% rename from src/ui/dbusscreensaver.h rename to src/ui/dbusidlehandler.h index 4cae2a16c..f213ba0b0 100644 --- a/src/ui/dbusscreensaver.h +++ b/src/ui/dbusidlehandler.h @@ -1,5 +1,6 @@ /* This file is part of Clementine. Copyright 2010, David Sansome + Copyright 2015, Arun Narayanankutty Clementine is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,22 +16,28 @@ along with Clementine. If not, see . */ -#ifndef DBUSSCREENSAVER_H -#define DBUSSCREENSAVER_H +#ifndef DBUSIDLEHANDLER_H +#define DBUSIDLEHANDLER_H -#include "screensaver.h" +#include "idlehandler.h" #include -class DBusScreensaver : public Screensaver { +class DBusIdleHandler : public IdleHandler { public: - DBusScreensaver(const QString& service, const QString& path, + DBusIdleHandler(const QString& service, const QString& path, const QString& interface); - void Inhibit(); - void Uninhibit(); + void Inhibit(const char* reason) override; + void Uninhibit() override; + bool Isinhibited() override; private: + enum GnomeIdleHandlerFlags { + Inhibit_Suspend = 4, + Inhibit_Mark_Idle = 8 + }; + QString service_; QString path_; QString interface_; @@ -38,4 +45,4 @@ class DBusScreensaver : public Screensaver { quint32 cookie_; }; -#endif +#endif // DBUSIDLEHANDLER_H diff --git a/src/ui/dbusscreensaver.cpp b/src/ui/dbusscreensaver.cpp deleted file mode 100644 index 21fa3a9de..000000000 --- a/src/ui/dbusscreensaver.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* This file is part of Clementine. - Copyright 2010, David Sansome - - Clementine 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. - - Clementine 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 Clementine. If not, see . -*/ - -#include "dbusscreensaver.h" - -#include -#include -#include - -DBusScreensaver::DBusScreensaver(const QString& service, const QString& path, - const QString& interface) - : service_(service), path_(path), interface_(interface) {} - -void DBusScreensaver::Inhibit() { - QDBusInterface gnome_screensaver("org.gnome.ScreenSaver", "/", - "org.gnome.ScreenSaver"); - QDBusReply reply = - gnome_screensaver.call("Inhibit", QCoreApplication::applicationName(), - QObject::tr("Visualizations")); - if (reply.isValid()) { - cookie_ = reply.value(); - } -} - -void DBusScreensaver::Uninhibit() { - QDBusInterface gnome_screensaver("org.gnome.ScreenSaver", "/", - "org.gnome.ScreenSaver"); - gnome_screensaver.call("UnInhibit", cookie_); -} diff --git a/src/ui/idlehandler.cpp b/src/ui/idlehandler.cpp new file mode 100644 index 000000000..fc803d177 --- /dev/null +++ b/src/ui/idlehandler.cpp @@ -0,0 +1,126 @@ +/* This file is part of Clementine. + Copyright 2010, David Sansome + Copyright 2015, Arun Narayanankutty + + Clementine 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. + + Clementine 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 Clementine. If not, see . +*/ + +#include "config.h" +#include "idlehandler.h" +#include "core/logging.h" + +#include + +#ifdef HAVE_DBUS +#include "dbusidlehandler.h" +#include +#include +#endif + +#ifdef Q_OS_DARWIN +#include "macidlehandler.h" +#endif + +#ifdef Q_OS_WIN32 +#include "windowsidlehandler.h" +#endif + +const char* IdleHandler::kGnomeScreensaverService + = "org.gnome.ScreenSaver"; +const char* IdleHandler::kGnomeScreensaverPath + = "/"; +const char* IdleHandler::kGnomeScreensaverInterface + = "org.gnome.ScreenSaver"; + +const char* IdleHandler::kFreedesktopScreensaverService + = "org.freedesktop.ScreenSaver"; +const char* IdleHandler::kFreedesktopScreensaverPath + = "/ScreenSaver"; +const char* IdleHandler::kFreedesktopScreensaverInterface + = "org.freedesktop.ScreenSaver"; + +const char* IdleHandler::kGnomePowermanagerService + = "org.gnome.SessionManager"; +const char* IdleHandler::kGnomePowermanagerPath + = "/org/gnome/SessionManager"; +const char* IdleHandler::kGnomePowermanagerInterface + = "org.gnome.SessionManager"; + +const char* IdleHandler::kFreedesktopPowermanagerService + = "org.freedesktop.PowerManagement"; +const char* IdleHandler::kFreedesktopPowermanagerPath + = "/org/freedesktop/PowerManagement/Inhibit"; +const char* IdleHandler::kFreedesktopPowermanagerInterface + = "org.freedesktop.PowerManagement.Inhibit"; + +IdleHandler* IdleHandler::screensaver_ = 0; +IdleHandler* IdleHandler::suspend_ = 0; + +IdleHandler::Inhibitor IdleHandler::inbtr_; + +IdleHandler* IdleHandler::GetScreensaver() { + inbtr_ = Screensaver; + if (!screensaver_) { +#if defined(HAVE_DBUS) + // For gnome + if (QDBusConnection::sessionBus().interface()->isServiceRegistered( + kGnomeScreensaverService)) { + screensaver_ = new DBusIdleHandler(kGnomeScreensaverService, + kGnomeScreensaverPath, + kGnomeScreensaverInterface); + } else if (QDBusConnection::sessionBus().interface()->isServiceRegistered( + kFreedesktopScreensaverService)) /* For KDE, XFCE & others */ { + screensaver_ = new DBusIdleHandler(kFreedesktopScreensaverService, + kFreedesktopScreensaverPath, + kFreedesktopScreensaverInterface); + } else { + qLog(Warning) << "no supported dbus screensaver service available"; + } + +#elif defined(Q_OS_DARWIN) + screensaver_ = new MacIdleHandler(); +#elif defined(Q_OS_WIN32) + screensaver_ = new WindowsIdleHandler(); +#endif + } + return screensaver_; +} + +IdleHandler* IdleHandler::GetSuspend() { + inbtr_ = Suspend; + if (!suspend_) { +#if defined(HAVE_DBUS) + // For gnome + if (QDBusConnection::sessionBus().interface()->isServiceRegistered( + kGnomePowermanagerService)) { + suspend_ = new DBusIdleHandler(kGnomePowermanagerService, + kGnomePowermanagerPath, + kGnomePowermanagerInterface); + } else if (QDBusConnection::sessionBus().interface()->isServiceRegistered( + kFreedesktopPowermanagerService)) /* For KDE, XFCE & others */ { + suspend_ = new DBusIdleHandler(kFreedesktopPowermanagerService, + kFreedesktopPowermanagerPath, + kFreedesktopPowermanagerInterface); + } else { + qLog(Warning) << "no supported dbus powermanager service available"; + } + +#elif defined(Q_OS_DARWIN) + suspend_ = new MacIdleHandler(); +#elif defined(Q_OS_WIN32) + suspend_ = new WindowsIdleHandler(); +#endif + } + return suspend_; +} diff --git a/src/ui/idlehandler.h b/src/ui/idlehandler.h new file mode 100644 index 000000000..c08df2a8c --- /dev/null +++ b/src/ui/idlehandler.h @@ -0,0 +1,61 @@ +/* This file is part of Clementine. + Copyright 2010, David Sansome + Copyright 2015, Arun Narayanankutty + + Clementine 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. + + Clementine 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 Clementine. If not, see . +*/ + +#ifndef IDLEHANDLER_H +#define IDLEHANDLER_H + +class IdleHandler { + public: + virtual ~IdleHandler() {} + + static const char* kGnomeScreensaverService; + static const char* kGnomeScreensaverPath; + static const char* kGnomeScreensaverInterface; + + static const char* kGnomePowermanagerService; + static const char* kGnomePowermanagerPath; + static const char* kGnomePowermanagerInterface; + + static const char* kFreedesktopScreensaverService; + static const char* kFreedesktopScreensaverPath; + static const char* kFreedesktopScreensaverInterface; + + static const char* kFreedesktopPowermanagerService; + static const char* kFreedesktopPowermanagerPath; + static const char* kFreedesktopPowermanagerInterface; + + virtual void Inhibit(const char* reason) = 0; + virtual void Uninhibit() = 0; + virtual bool Isinhibited() = 0; + + static IdleHandler* GetScreensaver(); + static IdleHandler* GetSuspend(); + + enum Inhibitor + { + Screensaver = 0, + Suspend = 1 + }; + static Inhibitor inbtr_; + + private: + static IdleHandler* screensaver_; + static IdleHandler* suspend_; +}; + +#endif // IDLEHANDLER_H diff --git a/src/ui/macscreensaver.cpp b/src/ui/macidlehandler.cpp similarity index 52% rename from src/ui/macscreensaver.cpp rename to src/ui/macidlehandler.cpp index 9bf3d076d..8b4ffeb33 100644 --- a/src/ui/macscreensaver.cpp +++ b/src/ui/macidlehandler.cpp @@ -1,5 +1,6 @@ /* This file is part of Clementine. Copyright 2010, David Sansome + Copyright 2015, Arun Narayanankutty Clementine is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,20 +16,43 @@ along with Clementine. If not, see . */ -#include "macscreensaver.h" +#include "macidlehandler.h" #include #include +#include "core/logging.h" #include "core/utilities.h" -MacScreensaver::MacScreensaver() : assertion_id_(0) {} +bool MacIdleHandler::is_inhibit_; -void MacScreensaver::Inhibit() { - IOPMAssertionCreateWithName( - kIOPMAssertPreventUserIdleDisplaySleep, kIOPMAssertionLevelOn, - CFSTR("Showing full-screen Clementine visualisations"), &assertion_id_); +MacIdleHandler::MacIdleHandler() : assertion_id_(0) { + is_inhibit_ = false; } -void MacScreensaver::Uninhibit() { IOPMAssertionRelease(assertion_id_); } +void MacIdleHandler::Inhibit(const char* reason) { + IOReturn reply = IOPMAssertionCreateWithName( + kIOPMAssertPreventUserIdleDisplaySleep, kIOPMAssertionLevelOn, + CFSTR(reason), &assertion_id_); + + if (reply == kIOReturnSuccess) { + is_inhibit_ = true; + } else { + qLog(Warning) << "Failed to inhibit screensaver/suspend"; + } +} + +void MacIdleHandler::Uninhibit() { + IOReturn reply = IOPMAssertionRelease(assertion_id_); + + if (reply == kIOReturnSuccess) { + is_inhibit_ = false; + } else { + qLog(Warning) << "Failed to uninhibit screensaver/suspend"; + } +} + +bool WindowsIdleHandler::Isinhibited() { + return is_inhibit_; +} diff --git a/src/ui/macscreensaver.h b/src/ui/macidlehandler.h similarity index 69% rename from src/ui/macscreensaver.h rename to src/ui/macidlehandler.h index ac0251c3e..06d0b0474 100644 --- a/src/ui/macscreensaver.h +++ b/src/ui/macidlehandler.h @@ -1,5 +1,6 @@ /* This file is part of Clementine. Copyright 2010, David Sansome + Copyright 2015, Arun Narayanankutty Clementine is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,22 +16,24 @@ along with Clementine. If not, see . */ -#ifndef MACSCREENSAVER_H -#define MACSCREENSAVER_H +#ifndef MACIDLEHANDLER_H +#define MACIDLEHANDLER_H -#include "screensaver.h" +#include "idlehandler.h" #include -class MacScreensaver : public Screensaver { +class MacIdleHandler : public IdleHandler { public: - MacScreensaver(); + MacIdleHandler(); - void Inhibit(); - void Uninhibit(); + void Inhibit(const char* reason) override; + void Uninhibit() override; + bool Isinhibited() override; private: IOPMAssertionID assertion_id_; + static bool is_inhibit_; }; -#endif +#endif // MACIDLEHANDLER_H diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index abdf99a80..8e2b9a14a 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -195,7 +195,9 @@ MainWindow::MainWindow(Application* app, SystemTrayIcon* tray_icon, OSD* osd, saved_playback_state_(Engine::Empty), doubleclick_addmode_(AddBehaviour_Append), doubleclick_playmode_(PlayBehaviour_IfStopped), - menu_playmode_(PlayBehaviour_IfStopped) { + menu_playmode_(PlayBehaviour_IfStopped), + idlehandler_(IdleHandler::GetSuspend()), + is_suspend_inhibited(false) { qLog(Debug) << "Starting"; connect(app, SIGNAL(ErrorAdded(QString)), SLOT(ShowErrorDialog(QString))); @@ -1007,6 +1009,11 @@ void MainWindow::ReloadSettings() { PlaylistAddBehaviour_Play).toInt()); menu_playmode_ = PlayBehaviour(s.value("menu_playmode", PlayBehaviour_IfStopped).toInt()); + s.endGroup(); + + s.beginGroup(Engine::Base::kSettingsGroup); + inhibit_suspend_while_playing_status_ = + s.value("InhibitSuspendWhilePlaying", false).toBool(); } void MainWindow::ReloadAllSettings() { @@ -1047,6 +1054,7 @@ void MainWindow::MediaStopped() { ui_->track_slider->SetStopped(); tray_icon_->SetProgress(0); tray_icon_->SetStopped(); + HandleInhibitSuspendWhilePlaying(false); } void MainWindow::MediaPaused() { @@ -1061,6 +1069,7 @@ void MainWindow::MediaPaused() { track_slider_timer_->stop(); tray_icon_->SetPaused(); + HandleInhibitSuspendWhilePlaying(false); } void MainWindow::MediaPlaying() { @@ -1089,6 +1098,7 @@ void MainWindow::MediaPlaying() { track_position_timer_->start(); track_slider_timer_->start(); UpdateTrackPosition(); + HandleInhibitSuspendWhilePlaying(true); } void MainWindow::VolumeChanged(int volume) { @@ -2417,6 +2427,11 @@ void MainWindow::EnsureSettingsDialogCreated() { SLOT(SetWiimotedevInterfaceActived(bool))); #endif + // Handle Suspend ststus + connect(settings_dialog_.get(), + SIGNAL(InhibitSuspendWhilePlaying(bool)), + SLOT(HandleInhibitSuspendWhilePlaying(bool))); + // Allows custom notification preview connect(settings_dialog_.get(), SIGNAL(NotificationPreview(OSD::Behaviour, QString, QString)), @@ -2792,3 +2807,16 @@ void MainWindow::keyPressEvent(QKeyEvent* event) { QMainWindow::keyPressEvent(event); } } + +void MainWindow::HandleInhibitSuspendWhilePlaying(bool status) { + if (idlehandler_) { + if (inhibit_suspend_while_playing_status_ && + !is_suspend_inhibited && status) { + idlehandler_->Inhibit("Clementine is playing"); + is_suspend_inhibited = idlehandler_->Isinhibited(); + } else if (is_suspend_inhibited && !status){ + idlehandler_->Uninhibit(); + is_suspend_inhibited = idlehandler_->Isinhibited(); + } + } +} diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index 3176dd9f8..42cf7e201 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -30,6 +30,7 @@ #include "engines/engine_fwd.h" #include "library/librarymodel.h" #include "playlist/playlistitem.h" +#include "ui/idlehandler.h" #include "ui/settingsdialog.h" class About; @@ -64,6 +65,7 @@ class PlaylistBackend; class PlaylistListContainer; class PlaylistManager; class QueueManager; +class IdleHandler; class InternetItem; class InternetModel; class InternetViewContainer; @@ -278,6 +280,8 @@ signals: void ShowConsole(); + void HandleInhibitSuspendWhilePlaying(bool status); + private: void ConnectInfoView(SongInfoBase* view); @@ -373,6 +377,9 @@ signals: PlayBehaviour menu_playmode_; BackgroundStreams* background_streams_; + IdleHandler* idlehandler_; + bool inhibit_suspend_while_playing_status_; + bool is_suspend_inhibited; }; #endif // MAINWINDOW_H diff --git a/src/ui/playbacksettingspage.cpp b/src/ui/playbacksettingspage.cpp index 957317377..e5a883e7c 100644 --- a/src/ui/playbacksettingspage.cpp +++ b/src/ui/playbacksettingspage.cpp @@ -87,6 +87,8 @@ void PlaybackSettingsPage::Load() { s.value("FadeoutPauseEnabled", false).toBool()); ui_->fading_pause_duration->setValue( s.value("FadeoutPauseDuration", 250).toInt()); + ui_->inhibit_suspend_while_playing->setChecked( + s.value("InhibitSuspendWhilePlaying", false).toBool()); s.endGroup(); s.beginGroup(GstEngine::kSettingsGroup); @@ -134,6 +136,8 @@ void PlaybackSettingsPage::Save() { s.setValue("NoCrossfadeSameAlbum", ui_->fading_samealbum->isChecked()); s.setValue("FadeoutPauseEnabled", ui_->fadeout_pause->isChecked()); s.setValue("FadeoutPauseDuration", ui_->fading_pause_duration->value()); + s.setValue("InhibitSuspendWhilePlaying", + ui_->inhibit_suspend_while_playing->isChecked()); s.endGroup(); GstEngine::OutputDetails details = @@ -154,6 +158,10 @@ void PlaybackSettingsPage::Save() { ui_->sample_rate->itemData(ui_->sample_rate->currentIndex()).toInt()); s.setValue("bufferminfill", ui_->buffer_min_fill->value()); s.endGroup(); + + //Emit inhibit suspend while playing signal + emit InhibitSuspendWhilePlaying( + ui_->inhibit_suspend_while_playing->isChecked()); } void PlaybackSettingsPage::RgPreampChanged(int value) { diff --git a/src/ui/playbacksettingspage.ui b/src/ui/playbacksettingspage.ui index d2b405b66..e2ebe07b5 100644 --- a/src/ui/playbacksettingspage.ui +++ b/src/ui/playbacksettingspage.ui @@ -7,7 +7,7 @@ 0 0 596 - 667 + 684 @@ -25,7 +25,7 @@ - + Fading @@ -70,7 +70,16 @@ - + + 0 + + + 0 + + + 0 + + 0 @@ -168,6 +177,22 @@ + + + + System Power Management Options + + + + + + Inhibit suspend while playing + + + + + + @@ -187,7 +212,16 @@ false - + + 0 + + + 0 + + + 0 + + 0 diff --git a/src/ui/screensaver.cpp b/src/ui/screensaver.cpp deleted file mode 100644 index af55ab8e1..000000000 --- a/src/ui/screensaver.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* This file is part of Clementine. - Copyright 2010, David Sansome - - Clementine 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. - - Clementine 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 Clementine. If not, see . -*/ - -#include "config.h" -#include "screensaver.h" - -#include - -#ifdef HAVE_DBUS -#include "dbusscreensaver.h" -#include -#include -#endif - -#ifdef Q_OS_DARWIN -#include "macscreensaver.h" -#endif - -#ifdef Q_OS_WIN32 -#include "windowsscreensaver.h" -#endif - -#include - -const char* Screensaver::kGnomeService = "org.gnome.ScreenSaver"; -const char* Screensaver::kGnomePath = "/"; -const char* Screensaver::kGnomeInterface = "org.gnome.ScreenSaver"; -const char* Screensaver::kKdeService = "org.kde.ScreenSaver"; -const char* Screensaver::kKdePath = "/ScreenSaver/"; -const char* Screensaver::kKdeInterface = "org.freedesktop.ScreenSaver"; - -Screensaver* Screensaver::screensaver_ = 0; - -Screensaver* Screensaver::GetScreensaver() { - if (!screensaver_) { -#if defined(HAVE_DBUS) - if (QDBusConnection::sessionBus().interface()->isServiceRegistered( - kGnomeService)) { - screensaver_ = - new DBusScreensaver(kGnomeService, kGnomePath, kGnomeInterface); - } else if (QDBusConnection::sessionBus().interface()->isServiceRegistered( - kKdeService)) { - screensaver_ = new DBusScreensaver(kKdeService, kKdePath, kKdeInterface); - } -#elif defined(Q_OS_DARWIN) - screensaver_ = new MacScreensaver(); -#elif defined(Q_OS_WIN32) - screensaver_ = new WindowsScreensaver(); -#endif - } - return screensaver_; -} diff --git a/src/ui/screensaver.h b/src/ui/screensaver.h deleted file mode 100644 index a33eeb5f3..000000000 --- a/src/ui/screensaver.h +++ /dev/null @@ -1,42 +0,0 @@ -/* This file is part of Clementine. - Copyright 2010, David Sansome - - Clementine 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. - - Clementine 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 Clementine. If not, see . -*/ - -#ifndef SCREENSAVER_H -#define SCREENSAVER_H - -class Screensaver { - public: - virtual ~Screensaver() {} - - static const char* kGnomeService; - static const char* kGnomePath; - static const char* kGnomeInterface; - - static const char* kKdeService; - static const char* kKdePath; - static const char* kKdeInterface; - - virtual void Inhibit() = 0; - virtual void Uninhibit() = 0; - - static Screensaver* GetScreensaver(); - - private: - static Screensaver* screensaver_; -}; - -#endif diff --git a/src/ui/settingsdialog.h b/src/ui/settingsdialog.h index 14bb9765b..9f97e5b41 100644 --- a/src/ui/settingsdialog.h +++ b/src/ui/settingsdialog.h @@ -119,6 +119,7 @@ class SettingsDialog : public QDialog { signals: void NotificationPreview(OSD::Behaviour, QString, QString); void SetWiimotedevInterfaceActived(bool); + void InhibitSuspendWhilePlaying(bool); private slots: void CurrentItemChanged(QTreeWidgetItem* item); diff --git a/src/ui/settingspage.h b/src/ui/settingspage.h index 99e33014d..32783d204 100644 --- a/src/ui/settingspage.h +++ b/src/ui/settingspage.h @@ -45,6 +45,7 @@ class SettingsPage : public QWidget { signals: void NotificationPreview(OSD::Behaviour, QString, QString); void SetWiimotedevInterfaceActived(bool); + void InhibitSuspendWhilePlaying(bool); private: SettingsDialog* dialog_; diff --git a/src/ui/windowsidlehandler.cpp b/src/ui/windowsidlehandler.cpp new file mode 100644 index 000000000..26f303828 --- /dev/null +++ b/src/ui/windowsidlehandler.cpp @@ -0,0 +1,64 @@ +/* This file is part of Clementine. + Copyright 2015, John Maguire + Copyright 2015, Arun Narayanankutty + + Clementine 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. + + Clementine 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 Clementine. If not, see . +*/ + +#include "windowsidlehandler.h" + +#include "core/logging.h" + +// TODO: use PowerCreateRequest on Win7+ + +bool WindowsIdleHandler::is_inhibit_; + +WindowsIdleHandler::WindowsIdleHandler() : previous_state_(0) { + is_inhibit_ = false; +} + +void WindowsIdleHandler::Inhibit(const char*) { + switch (IdleHandler::inbtr_) { + case IdleHandler::Screensaver : + // resetting the display idle timer. + previous_state_ = + SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED); + break; + case IdleHandler::Suspend : + // resetting the system idle timer. + previous_state_ = + SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED); + break; + } + + if (previous_state_! = 0) { + is_inhibit_ = true; + } else { + qLog(Warning) << "Failed to inhibit screensaver/suspend"; + } +} + +void WindowsIdleHandler::Uninhibit() { + previous_state_ = + SetThreadExecutionState(ES_CONTINUOUS | previous_state_); + if (previous_state_! = 0) { + is_inhibit_ = false; + } else { + qLog(Warning) << "Failed to uninhibit screensaver/suspend"; + } +} + +bool WindowsIdleHandler::Isinhibited() { + return is_inhibit_; +} diff --git a/src/ui/windowsscreensaver.h b/src/ui/windowsidlehandler.h similarity index 70% rename from src/ui/windowsscreensaver.h rename to src/ui/windowsidlehandler.h index fc3bd8f07..78a0fe46d 100644 --- a/src/ui/windowsscreensaver.h +++ b/src/ui/windowsidlehandler.h @@ -1,5 +1,6 @@ /* This file is part of Clementine. Copyright 2015, John Maguire + Copyright 2015, Arun Narayanankutty Clementine is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,22 +15,24 @@ You should have received a copy of the GNU General Public License along with Clementine. If not, see . */ -#ifndef WINDOWSSCREENSAVER_H -#define WINDOWSSCREENSAVER_H +#ifndef WINDOWSIDLEHANDLER_H +#define WINDOWSIDLEHANDLER_H -#include "screensaver.h" +#include "idlehandler.h" #include -class WindowsScreensaver : public Screensaver { +class WindowsIdleHandler : public IdleHandler { public: - WindowsScreensaver(); + WindowsIdleHandler(); - void Inhibit() override; + void Inhibit(const char*) override; void Uninhibit() override; + bool Isinhibited() override; private: EXECUTION_STATE previous_state_; + static bool is_inhibit_; }; -#endif // WINDOWSSCREENSAVER_H +#endif // WINDOWSIDLEHANDLER_H diff --git a/src/ui/windowsscreensaver.cpp b/src/ui/windowsscreensaver.cpp deleted file mode 100644 index 850ba3866..000000000 --- a/src/ui/windowsscreensaver.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* This file is part of Clementine. - Copyright 2015, John Maguire - - Clementine 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. - - Clementine 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 Clementine. If not, see . -*/ - -#include "windowsscreensaver.h" - -WindowsScreensaver::WindowsScreensaver() : previous_state_(0) {} - -void WindowsScreensaver::Inhibit() { - // TODO: use PowerCreateRequest on Win7+ - previous_state_ = - SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED); -} - -void WindowsScreensaver::Uninhibit() { - SetThreadExecutionState(ES_CONTINUOUS | previous_state_); -} diff --git a/src/visualisations/visualisationcontainer.cpp b/src/visualisations/visualisationcontainer.cpp index 361ee2007..3df111ffd 100644 --- a/src/visualisations/visualisationcontainer.cpp +++ b/src/visualisations/visualisationcontainer.cpp @@ -22,7 +22,7 @@ #include "visualisationselector.h" #include "engines/gstengine.h" #include "ui/iconloader.h" -#include "ui/screensaver.h" +#include "ui/idlehandler.h" #include #include @@ -265,9 +265,9 @@ void VisualisationContainer::keyReleaseEvent(QKeyEvent* event) { void VisualisationContainer::ToggleFullscreen() { setWindowState(windowState() ^ Qt::WindowFullScreen); - Screensaver* screensaver = Screensaver::GetScreensaver(); + IdleHandler* screensaver = IdleHandler::GetScreensaver(); if (screensaver) - isFullScreen() ? screensaver->Inhibit() : screensaver->Uninhibit(); + isFullScreen() ? screensaver->Inhibit("Visualisation") : screensaver->Uninhibit(); } void VisualisationContainer::SetFps(int fps) {