Skip to content

Commit

Permalink
Use D-Bus for suspend (systemd and ConsoleKit)
Browse files Browse the repository at this point in the history
Closes #321
  • Loading branch information
zaps166 committed Jul 4, 2020
1 parent 1d8849f commit cb20f4d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/gui/MenuBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ MenuBar::Player::Player(MenuBar *parent) :
newAction(Player::tr("Detach from receiving &commands"), this, detach, false, QIcon(), false);
}

if (!QMPlay2CoreClass::canSuspend())
suspend = nullptr;
else
if (QMPlay2Core.canSuspend())
{
addSeparator();
newAction(Player::tr("Suspend after playbac&k is finished"), this, suspend, false, QIcon(), true);
Expand Down
3 changes: 2 additions & 1 deletion src/gui/MenuBar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class MenuBar : public QMenuBar

Repeat *repeat;
AspectRatio *aRatio;
QAction *togglePlay, *stop, *next, *prev, *prevFrame, *nextFrame, *abRepeat, *seekF, *seekB, *lSeekB, *lSeekF, *speedUp, *slowDown, *setSpeed, *switchARatio, *zoomIn, *zoomOut, *reset, *volUp, *volDown, *toggleMute, *detach, *suspend;
QAction *togglePlay, *stop, *next, *prev, *prevFrame, *nextFrame, *abRepeat, *seekF, *seekB, *lSeekB, *lSeekF, *speedUp, *slowDown, *setSpeed, *switchARatio, *zoomIn, *zoomOut, *reset, *volUp, *volDown, *toggleMute, *detach;
QAction *suspend = nullptr;
};

class Playback : public QMenu
Expand Down
2 changes: 1 addition & 1 deletion src/gui/PlayClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ void PlayClass::demuxThrFinished()
doSuspend = false;
emit uncheckSuspend();
if (canDoSuspend)
QMPlay2CoreClass::suspend();
QMPlay2Core.suspend();
}
}

Expand Down
93 changes: 72 additions & 21 deletions src/qmplay2/QMPlay2Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include <powrprof.h>
#elif defined Q_OS_MACOS
#include <QStandardPaths>
#elif !defined Q_OS_ANDROID
#include <QProcess>
#endif

#include <cstdarg>
Expand All @@ -57,6 +59,19 @@ extern "C"
#include <libavutil/log.h>
}

#if !defined(Q_OS_ANDROID) && !defined(Q_OS_WIN) && !defined(Q_OS_MACOS)
static QStringList getDBusSuspendArgs(const QString &method, const QString &name, const QString &pathSuffix = QString())
{
return {
"--system",
"--print-reply",
"--dest=org.freedesktop." + name,
"/org/freedesktop/" + name + pathSuffix,
"org.freedesktop." + name + ".Manager." + method
};
};
#endif

/**/

Q_LOGGING_CATEGORY(ffmpeglog, "FFmpegLog")
Expand Down Expand Up @@ -163,27 +178,6 @@ QString QMPlay2CoreClass::getLongFromShortLanguage(const QString &lng)
return lang == "C" ? lng : lang;
}

bool QMPlay2CoreClass::canSuspend()
{
#if defined Q_OS_LINUX
return !system("systemctl --help 2> /dev/null | grep -q suspend");
#elif defined Q_OS_WIN || defined Q_OS_MACOS
return true;
#else
return false;
#endif
}
void QMPlay2CoreClass::suspend()
{
#if defined Q_OS_LINUX
Q_UNUSED(system("systemctl suspend > /dev/null 2>&1 &"));
#elif defined Q_OS_WIN
SetSuspendState(false, false, false);
#elif defined Q_OS_MACOS
Q_UNUSED(system("pmset sleepnow > /dev/null 2>&1 &"));
#endif
}

int QMPlay2CoreClass::getCPUFlags()
{
return av_get_cpu_flags();
Expand Down Expand Up @@ -398,6 +392,63 @@ void QMPlay2CoreClass::quit()
m_gpuInstance.reset();
}

bool QMPlay2CoreClass::canSuspend()
{
#if defined Q_OS_WIN || defined Q_OS_MACOS
m_suspend = 1;
return true;
#elif !defined O_OS_ANDROID
auto checkSuspendDBus = [](const QStringList &args) {
QProcess p;
p.start("dbus-send", args);
if (!p.waitForStarted() || !p.waitForFinished())
return false;
return (p.readAllStandardOutput().split('\n').value(1).simplified().replace("\"", "").split(' ').value(1).compare("yes", Qt::CaseInsensitive) == 0);
};
if (checkSuspendDBus(getDBusSuspendArgs("CanSuspend", "login1")))
{
m_suspend = 1;
return true;
}
if (checkSuspendDBus(getDBusSuspendArgs("CanSuspend", "ConsoleKit", "/Manager")))
{
m_suspend = 2;
return true;
}
#endif
return false;
}
void QMPlay2CoreClass::suspend()
{
if (m_suspend == 0)
return;

#if defined Q_OS_WIN
SetSuspendState(false, false, false);
#elif defined Q_OS_MACOS
Q_UNUSED(system("pmset sleepnow > /dev/null 2>&1 &"));
#elif !defined Q_OS_ANDROID
QStringList args;
switch (m_suspend)
{
case 1:
args = getDBusSuspendArgs("Suspend", "login1");
break;
case 2:
args = getDBusSuspendArgs("Suspend", "ConsoleKit", "/Manager");
break;
default:
return;
}
args += "boolean:true";

QProcess p;
p.start("dbus-send", args);
if (p.waitForStarted())
p.waitForFinished();
#endif
}

QStringList QMPlay2CoreClass::getModules(const QString &type, int typeLen) const
{
QStringList defaultModules;
Expand Down
8 changes: 5 additions & 3 deletions src/qmplay2/QMPlay2Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ class QMPLAY2SHAREDLIB_EXPORT QMPlay2CoreClass : public QObject

static QString getLongFromShortLanguage(const QString &lng);

static bool canSuspend();
static void suspend();

static int getCPUFlags();

#ifdef USE_OPENGL
Expand All @@ -80,6 +77,9 @@ class QMPLAY2SHAREDLIB_EXPORT QMPlay2CoreClass : public QObject
void init(bool loadModules, bool modulesInSubdirs, const QString &libPath, const QString &sharePath, const QString &profileName);
void quit();

bool canSuspend();
void suspend();

QStringList getModules(const QString &type, int typeLen) const;

inline QVector<Module *> getPluginsInstance() const
Expand Down Expand Up @@ -253,6 +253,8 @@ private slots:
std::shared_ptr<GPUInstance> m_gpuInstance;

CommonJS *m_commonJS = nullptr;

int m_suspend = 0;
};

#define QMPlay2Core QMPlay2CoreClass::instance()

0 comments on commit cb20f4d

Please sign in to comment.