From dcd2e6aded1c8f7ee39964ba2ecda9ee10b547ab Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Shmelev Date: Tue, 20 May 2025 09:56:56 +0200 Subject: [PATCH] Shutdown desktop environment on exit, initial implementation --- src/main.cpp | 6 ++++++ src/mainwindow.cpp | 28 ++++++++++++++++++++++++++++ src/mainwindow.h | 4 ++++ src/ogcommandline.h | 7 ++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index cff8758..fa87c1a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -70,6 +70,9 @@ int main(int argc, char *argv[]) QCommandLineOption stopRunning("stop-running", "Stop any running instances"); parser.addOption(stopRunning); + QCommandLineOption terminateEnvironmentOnShutdown("terminate-environment-on-shutdown", "Terminate the desktop environment if the browser is shut down (i3 and sway supported)"); + parser.addOption(terminateEnvironmentOnShutdown); + QCommandLineOption helpOption = parser.addHelpOption(); @@ -137,6 +140,9 @@ int main(int argc, char *argv[]) options.setIgnoreSslErrors(true); } + if (parser.isSet(terminateEnvironmentOnShutdown)) { + options.setTerminateEnvironmentOnShutdown(true); + } if ( positional.length() > 0) { options.setUrl( positional[0 ] ); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bbfe92a..983d17d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -202,6 +202,26 @@ void MainWindow::closeEvent(QCloseEvent *event) { qInfo() << "Modo quiosco activado, ignorando intento de cerrar ventana"; event->ignore(); } + + OGCommandLineOptions &options = OGCommandLineOptions::getInstance(); + + if (options.getTerminateEnvironmentOnShutdown()) { + qInfo() << "Shutdown requested, will try to terminate the desktop environment now."; + auto swaysock = qgetenv("SWAYSOCK"); + auto i3sock = qgetenv("I3SOCK"); + + if (swaysock.length() > 0) { + qInfo() << "We seem to be running Sway, sending termination command"; + event->ignore(); + m_shutdown_process.start("swaymsg", QStringList() << "exit"); + } else if (i3sock.length() > 0) { + qInfo() << "We seem to be running i3, sending termination command"; + event->ignore(); + m_shutdown_process.start("i3-msg", QStringList() << "exit"); + } else { + qInfo() << "Unknown environment running, can't stop it."; + } + } } @@ -288,6 +308,8 @@ void MainWindow::commandQueued(const QString &command, bool confirm, bool return connect(m_command.process, &QProcess::readyReadStandardOutput,this,&MainWindow::slotProcessOutput); connect(m_command.process, &QProcess::readyReadStandardError,this,&MainWindow::slotProcessErrorOutput); + connect(&m_shutdown_process, &QProcess::finished,this,&MainWindow::slotShutdownProcessFinished); + if(isAdmin()) { m_output->setTextColor(QColor(Qt::darkGreen)); @@ -471,6 +493,12 @@ void MainWindow::slotProcessError(QProcess::ProcessError error) finishProgressBar(); } +void slotShutdownProcessFinished(int code, QProcess::ExitStatus status) { + qInfo() << "Shutdown process finished with code" << code << "; status" << status; + qInfo() << "Now quitting."; + QCoreApplication::exit(0); +} + void MainWindow::slotCreateTerminal() { QTermWidget* console = new QTermWidget(1,this); diff --git a/src/mainwindow.h b/src/mainwindow.h index e257fa1..c5baefd 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -68,6 +68,9 @@ class MainWindow : public QMainWindow void slotProcessOutput(); void slotProcessErrorOutput(); + void slotShutdownProcessFinished(int code, QProcess::ExitStatus status); + + // Funcion para crear terminales void slotCreateTerminal(); void slotDeleteTerminal(); @@ -112,6 +115,7 @@ class MainWindow : public QMainWindow QLineEdit *m_webBar; //QProcess *m_process; + QProcess m_shutdown_process; QMap m_env; QFile *m_logfile; diff --git a/src/ogcommandline.h b/src/ogcommandline.h index c68e194..5b06582 100644 --- a/src/ogcommandline.h +++ b/src/ogcommandline.h @@ -38,6 +38,10 @@ class OGCommandLineOptions { bool getIgnoreSslErrors() const { return _ignoreSslErrors; } void setIgnoreSslErrors(bool value) { _ignoreSslErrors = value; } + bool getTerminateEnvironmentOnShutdown() const { return _terminateEnvironmentOnShutdown; } + void setTerminateEnvironmentOnShutdown(bool value) { _terminateEnvironmentOnShutdown = value; } + + private: OGCommandLineOptions() { @@ -48,4 +52,5 @@ private: NewTabBehavior _newTabBehavior{ReplacePage}; bool _ignoreSslErrors = false; -}; \ No newline at end of file + bool _terminateEnvironmentOnShutdown = false; +};