diff --git a/NEWS.md b/NEWS.md index 741edd14c..84c5674e4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,9 +8,9 @@ queue completion. * MKVToolNix GUI: implemented several built-in actions that can be executed either on special events or once via the "watch jobs" tool. These are: - playing an audio file (implemented for all operating systems); suspending - and shutting down the computer (only implemented for Windows and for Linux - systems using systemd). + playing an audio file (implemented for all operating systems); hibernating, + sleeping and shutting down the computer (only implemented for Windows and + for Linux systems using systemd). ## Bug fixes diff --git a/src/mkvtoolnix-gui/jobs/program_runner.cpp b/src/mkvtoolnix-gui/jobs/program_runner.cpp index 1e331956a..adac0f31b 100644 --- a/src/mkvtoolnix-gui/jobs/program_runner.cpp +++ b/src/mkvtoolnix-gui/jobs/program_runner.cpp @@ -104,8 +104,11 @@ ProgramRunner::run(Util::Settings::RunProgramForEvent forEvent, else if (runConfig->m_type == Util::Settings::RunProgramType::ShutDownComputer) shutDownComputer(*runConfig); - else if (runConfig->m_type == Util::Settings::RunProgramType::SuspendComputer) - suspendComputer(*runConfig); + else if (runConfig->m_type == Util::Settings::RunProgramType::HibernateComputer) + hibernateComputer(*runConfig); + + else if (runConfig->m_type == Util::Settings::RunProgramType::SleepComputer) + sleepComputer(*runConfig); } } @@ -215,7 +218,12 @@ ProgramRunner::shutDownComputer(Util::Settings::RunProgramConfig &/* config */) } void -ProgramRunner::suspendComputer(Util::Settings::RunProgramConfig &/* config */) { +ProgramRunner::hibernateComputer(Util::Settings::RunProgramConfig &/* config */) { + // Not supported in an OS-agnostic way. +} + +void +ProgramRunner::sleepComputer(Util::Settings::RunProgramConfig &/* config */) { // Not supported in an OS-agnostic way. } diff --git a/src/mkvtoolnix-gui/jobs/program_runner.h b/src/mkvtoolnix-gui/jobs/program_runner.h index 5a9f08884..59f8465b5 100644 --- a/src/mkvtoolnix-gui/jobs/program_runner.h +++ b/src/mkvtoolnix-gui/jobs/program_runner.h @@ -49,7 +49,8 @@ protected: virtual void executeProgram(Util::Settings::RunProgramConfig &config, std::function const &setupVariables, VariableMap const &generalVariables); virtual void playAudioFile(Util::Settings::RunProgramConfig &config); virtual void shutDownComputer(Util::Settings::RunProgramConfig &config); - virtual void suspendComputer(Util::Settings::RunProgramConfig &config); + virtual void hibernateComputer(Util::Settings::RunProgramConfig &config); + virtual void sleepComputer(Util::Settings::RunProgramConfig &config); public: static std::unique_ptr create(); diff --git a/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.cpp b/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.cpp index 6fefa6290..fe0a35505 100644 --- a/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.cpp +++ b/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.cpp @@ -24,7 +24,7 @@ LinuxProgramRunner::isRunProgramTypeSupported(Util::Settings::RunProgramType typ if (ProgramRunner::isRunProgramTypeSupported(type)) return true; - return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::SuspendComputer); + return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::HibernateComputer, Util::Settings::RunProgramType::SleepComputer); } void @@ -40,15 +40,24 @@ LinuxProgramRunner::shutDownComputer(Util::Settings::RunProgramConfig &) { } void -LinuxProgramRunner::suspendComputer(Util::Settings::RunProgramConfig &) { - qDebug() << "LinuxProgramRunner::suspendComputer: about to shut down the computer via systemctl"; +LinuxProgramRunner::hibernateOrSleepComputer(bool hibernate) { + auto action = Q(hibernate ? "hibernate" : "suspend"); + qDebug() << "LinuxProgramRunner::hibernateOrSleepComputer: about to" << action << "the computer via systemctl"; - auto result = QProcess::execute("systemctl suspend"); + auto result = QProcess::execute(Q("systemctl %1").arg(action)); - if (result == 0) - return; + if (result != 0) + qDebug() << "LinuxProgramRunner::hibernateOrSleepComputer: 'systemctl" << action << "' failed:" << result; +} - qDebug() << "LinuxProgramRunner::suspendComputer: 'systemctl poweroff' failed:" << result; +void +LinuxProgramRunner::hibernateComputer(Util::Settings::RunProgramConfig &) { + hibernateOrSleepComputer(true); +} + +void +LinuxProgramRunner::sleepComputer(Util::Settings::RunProgramConfig &) { + hibernateOrSleepComputer(false); } }}} diff --git a/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.h b/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.h index d828a72e8..ed533c94d 100644 --- a/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.h +++ b/src/mkvtoolnix-gui/jobs/program_runner/linux_program_runner.h @@ -22,7 +22,9 @@ public: protected: virtual void shutDownComputer(Util::Settings::RunProgramConfig &config) override; - virtual void suspendComputer(Util::Settings::RunProgramConfig &config) override; + virtual void hibernateComputer(Util::Settings::RunProgramConfig &config) override; + virtual void sleepComputer(Util::Settings::RunProgramConfig &config) override; + virtual void hibernateOrSleepComputer(bool hibernate); }; }}} diff --git a/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.cpp b/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.cpp index 97c3fc084..56f297469 100644 --- a/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.cpp +++ b/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.cpp @@ -28,7 +28,7 @@ WindowsProgramRunner::isRunProgramTypeSupported(Util::Settings::RunProgramType t if (ProgramRunner::isRunProgramTypeSupported(type)) return true; - return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::SuspendComputer); + return mtx::included_in(type, Util::Settings::RunProgramType::ShutDownComputer, Util::Settings::RunProgramType::HibernateComputer, Util::Settings::RunProgramType::SleepComputer); } void @@ -68,22 +68,27 @@ WindowsProgramRunner::shutDownComputer(Util::Settings::RunProgramConfig &) { } void -WindowsProgramRunner::suspendComputer(Util::Settings::RunProgramConfig &) { - qDebug() << "WindowsProgramRunner::suspendComputer: about to hibernate"; +WindowsProgramRunner::hibernateOrSleepComputer(bool hibernate) { + auto action = Q(hibernate ? "hibernate" : "sleep"); + qDebug() << "WindowsProgramRunner::hibernateOrSleepComputer: about to" << action; addShutdownNamePrivilege(); - if (SetSuspendState(true, false, false)) + if (SetSuspendState(hibernate, false, false)) return; auto error = GetLastError(); - qDebug() << "WindowsProgramRunner::suspendComputer: hibernate failed, about to sleep; error:" << error << Q(mtx::sys::format_windows_message(error)); + qDebug() << "WindowsProgramRunner::hibernateOrSleepComputer:" << action << "failed; error:" << error << Q(mtx::sys::format_windows_message(error)); +} - if (SetSuspendState(false, false, false)) - return; +void +WindowsProgramRunner::hibernateComputer(Util::Settings::RunProgramConfig &) { + hibernateOrSleepComputer(true); +} - error = GetLastError(); - qDebug() << "WindowsProgramRunner::suspendComputer: sleep failed, too. Not trying anything else. Error:" << error << Q(mtx::sys::format_windows_message(error)); +void +WindowsProgramRunner::sleepComputer(Util::Settings::RunProgramConfig &) { + hibernateOrSleepComputer(false); } QString diff --git a/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.h b/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.h index 4bf6ddf59..13f9eb279 100644 --- a/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.h +++ b/src/mkvtoolnix-gui/jobs/program_runner/windows_program_runner.h @@ -22,7 +22,9 @@ public: protected: virtual void shutDownComputer(Util::Settings::RunProgramConfig &config) override; - virtual void suspendComputer(Util::Settings::RunProgramConfig &config) override; + virtual void hibernateComputer(Util::Settings::RunProgramConfig &config) override; + virtual void sleepComputer(Util::Settings::RunProgramConfig &config) override; + virtual void hibernateOrSleepComputer(bool hibernate); virtual void addShutdownNamePrivilege(); }; diff --git a/src/mkvtoolnix-gui/main_window/prefs_run_program_widget.cpp b/src/mkvtoolnix-gui/main_window/prefs_run_program_widget.cpp index c9dbf58dd..0995ad3bf 100644 --- a/src/mkvtoolnix-gui/main_window/prefs_run_program_widget.cpp +++ b/src/mkvtoolnix-gui/main_window/prefs_run_program_widget.cpp @@ -195,12 +195,14 @@ PrefsRunProgramWidget::setupTypeControl(Util::Settings::RunProgramConfig const & addItemIfSupported(QY("Execute a program"), Util::Settings::RunProgramType::ExecuteProgram); addItemIfSupported(QY("Play an audio file"), Util::Settings::RunProgramType::PlayAudioFile); addItemIfSupported(QY("Shut down the computer"), Util::Settings::RunProgramType::ShutDownComputer); - addItemIfSupported(QY("Suspend the computer"), Util::Settings::RunProgramType::SuspendComputer); + addItemIfSupported(QY("Hibernate the computer"), Util::Settings::RunProgramType::HibernateComputer); + addItemIfSupported(QY("Sleep the computer"), Util::Settings::RunProgramType::SleepComputer); - d->pagesByType[Util::Settings::RunProgramType::ExecuteProgram] = d->ui->executeProgramTypePage; - d->pagesByType[Util::Settings::RunProgramType::PlayAudioFile] = d->ui->playAudioFileTypePage; - d->pagesByType[Util::Settings::RunProgramType::ShutDownComputer] = d->ui->emptyTypePage; - d->pagesByType[Util::Settings::RunProgramType::SuspendComputer] = d->ui->emptyTypePage; + d->pagesByType[Util::Settings::RunProgramType::ExecuteProgram] = d->ui->executeProgramTypePage; + d->pagesByType[Util::Settings::RunProgramType::PlayAudioFile] = d->ui->playAudioFileTypePage; + d->pagesByType[Util::Settings::RunProgramType::ShutDownComputer] = d->ui->emptyTypePage; + d->pagesByType[Util::Settings::RunProgramType::HibernateComputer] = d->ui->emptyTypePage; + d->pagesByType[Util::Settings::RunProgramType::SleepComputer] = d->ui->emptyTypePage; showPageForType(cfg.m_type); diff --git a/src/mkvtoolnix-gui/util/settings.cpp b/src/mkvtoolnix-gui/util/settings.cpp index 4b8a7b523..6b01c9840 100644 --- a/src/mkvtoolnix-gui/util/settings.cpp +++ b/src/mkvtoolnix-gui/util/settings.cpp @@ -51,11 +51,12 @@ Settings::RunProgramConfig::name() if (!m_name.isEmpty()) return m_name; - return m_type == RunProgramType::ExecuteProgram ? nameForExternalProgram() - : m_type == RunProgramType::PlayAudioFile ? nameForPlayAudioFile() - : m_type == RunProgramType::ShutDownComputer ? QY("Shut down the computer") - : m_type == RunProgramType::SuspendComputer ? QY("Suspend the computer") - : Q("unknown"); + return m_type == RunProgramType::ExecuteProgram ? nameForExternalProgram() + : m_type == RunProgramType::PlayAudioFile ? nameForPlayAudioFile() + : m_type == RunProgramType::ShutDownComputer ? QY("Shut down the computer") + : m_type == RunProgramType::HibernateComputer ? QY("Hibernate the computer") + : m_type == RunProgramType::SleepComputer ? QY("Sleep the computer") + : Q("unknown"); } QString @@ -420,7 +421,8 @@ Settings::addDefaultRunProgramConfigurations(QSettings ®) { auto numConfigurationsBefore = m_runProgramConfigurations.count(); addDefaultRunProgramConfigurationForType(reg, RunProgramType::PlayAudioFile, [](RunProgramConfig &cfg) { cfg.m_audioFile = App::programRunner().defaultAudioFileName(); }); - addDefaultRunProgramConfigurationForType(reg, RunProgramType::SuspendComputer); + addDefaultRunProgramConfigurationForType(reg, RunProgramType::SleepComputer); + addDefaultRunProgramConfigurationForType(reg, RunProgramType::HibernateComputer); addDefaultRunProgramConfigurationForType(reg, RunProgramType::ShutDownComputer); if (numConfigurationsBefore != m_runProgramConfigurations.count()) diff --git a/src/mkvtoolnix-gui/util/settings.h b/src/mkvtoolnix-gui/util/settings.h index 8f924ddee..e99def9ff 100644 --- a/src/mkvtoolnix-gui/util/settings.h +++ b/src/mkvtoolnix-gui/util/settings.h @@ -26,7 +26,8 @@ public: ExecuteProgram, PlayAudioFile, ShutDownComputer, - SuspendComputer, + HibernateComputer, + SleepComputer, Max, Default = ExecuteProgram, };