summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/windowmanager.cpp47
1 files changed, 31 insertions, 16 deletions
diff --git a/src/windowmanager.cpp b/src/windowmanager.cpp
index e5e14a3..e85a08e 100644
--- a/src/windowmanager.cpp
+++ b/src/windowmanager.cpp
@@ -3,27 +3,35 @@
#include <QProcess>
#include <QTimer>
#include <QDebug>
+#include <QThread>
+#include <QCoreApplication>
namespace WindowManager {
static QProcess wm;
+static void killInstance();
+
void ensureRunning()
{
wm.start(QLatin1String("openbox"));
wm.closeReadChannel(QProcess::StandardError);
wm.closeReadChannel(QProcess::StandardOutput);
wm.closeWriteChannel();
- wm.waitForStarted(1000);
- QTimer::singleShot(200, []() {
- if (wm.state() == QProcess::Running) {
- qDebug() << "- Spawned openbox";
- } else if (wm.exitCode() == 0) {
- qDebug() << "- A WM is already running";
- } else {
- qDebug() << "- openbox binary not in $PATH";
- }
- });
+ wm.waitForStarted(500);
+ // Ugly, but if openbox initializes just as we map
+ // the main window, it might become invisible
+ for (int i = 0; i < 10 && wm.state() == QProcess::Running; ++i) {
+ QThread::msleep(25);
+ }
+ if (wm.state() == QProcess::Running) {
+ qDebug() << "- Spawned openbox";
+ QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, killInstance);
+ } else if (wm.exitCode() == 0) {
+ qDebug() << "- A WM is already running";
+ } else {
+ qDebug() << "- openbox binary not in $PATH";
+ }
}
void stopOwnInstance()
@@ -31,12 +39,19 @@ void stopOwnInstance()
if (wm.state() == QProcess::Running) {
qDebug() << "- Politely terminating spawned WM";
wm.terminate();
- QTimer::singleShot(1000, []() {
- if (wm.state() == QProcess::Running) {
- qDebug() << "- Forcefully killing spawned WM";
- wm.kill();
- }
- });
+ QTimer::singleShot(500, killInstance);
+ }
+}
+
+void killInstance()
+{
+ if (wm.state() == QProcess::Running) {
+ wm.terminate();
+ QThread::msleep(50);
+ }
+ if (wm.state() == QProcess::Running) {
+ qDebug() << "- Forcefully killing spawned WM";
+ wm.kill();
}
}