12,964
回編集
215行目: | 215行目: | ||
<br><br> | <br><br> | ||
== | == スリープ == | ||
* プロセスを直接起動する場合 | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QProcess> | |||
#include <QDebug> | |||
void Converter::suspend() | void Converter::suspend() | ||
{ | { | ||
auto trySuspendCommand = [](const QString &command, const QStringList &arguments) { | |||
QProcess process; | |||
process.start(command, arguments); | |||
process.waitForFinished(5000); // 5秒待機 | |||
return process.exitCode() == 0; | |||
}; | |||
// Systemctlを使用してサスペンドを試行 | |||
if (trySuspendCommand("systemctl", QStringList() << "suspend")) { | |||
qInfo() << "Systemctl経由でサスペンド"; | |||
return; | |||
} | |||
qWarning() << "Systemctl経由でのサスペンドに失敗"; | |||
// pm-suspendを使用してサスペンドを試行 (古いLinuxシステム向け) | |||
{ | if (trySuspendCommand("pm-suspend", QStringList())) { | ||
qInfo() << "pm-suspend経由でサスペンド"; | |||
return; | |||
} | } | ||
qWarning() << "pm-suspend経由でのサスペンドに失敗"; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
* D-Busを使用する場合 | |||
<syntaxhighlight lang="c++"> | |||
#include <QDBus> | |||
#include <QDebug> | |||
void Converter::suspend() | |||
{ | |||
auto tryDBusCall = [](const QString &service, const QString &path, const QString &interface, const QString &method) { | |||
QDBusInterface dbusInterface(service, path, interface, QDBusConnection::systemBus()); | |||
QDBusReply<void> reply = dbusInterface.call(method); | |||
return reply.isValid(); | |||
}; | |||
// Systemdを試行 | |||
if (tryDBusCall("org.freedesktop.login1", | |||
"/org/freedesktop/login1", | |||
"org.freedesktop.login1.Manager", | |||
"Suspend")) { | |||
qInfo() << "システムはsystemd経由でサスペンドされました"; | |||
return; | |||
} | } | ||
qWarning() << "Systemd経由でのサスペンドに失敗"; | |||
if( | // Systemdが失敗した場合、GNOMEのセッションマネージャを試行 | ||
if (tryDBusCall("org.gnome.SessionManager", | |||
"/org/gnome/SessionManager", | |||
"org.gnome.SessionManager", | |||
"Suspend")) { | |||
qInfo() << "GNOMEセッションマネージャ経由でサスペンド"; | |||
return; | |||
} | |||
qWarning() << "GNOMEセッションマネージャ経由でのサスペンドに失敗"; | |||
// UPowerインターフェースを試行 | |||
if (tryDBusCall("org.freedesktop.UPower", | |||
"/org/freedesktop/UPower", | |||
"org.freedesktop.UPower", | |||
"Suspend")) { | |||
qInfo() << "UPower経由でサスペンド"; | |||
return; | |||
} | } | ||
qWarning() << "UPower経由でのサスペンドに失敗"; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |