diff options
author | Simon Rettberg | 2019-07-03 17:13:15 +0200 |
---|---|---|
committer | Simon Rettberg | 2019-07-03 17:13:15 +0200 |
commit | d0d76e36a6d0fd5e4de35fe022e33fdcb736a63e (patch) | |
tree | 72dc9a766091bdbc3a568ad1f82be133167612eb | |
parent | Temporarily spawn openbox if no WM is running (diff) | |
download | vmchooser2-d0d76e36a6d0fd5e4de35fe022e33fdcb736a63e.tar.gz vmchooser2-d0d76e36a6d0fd5e4de35fe022e33fdcb736a63e.tar.xz vmchooser2-d0d76e36a6d0fd5e4de35fe022e33fdcb736a63e.zip |
Address timing issues with WM spawning
-rw-r--r-- | src/windowmanager.cpp | 47 |
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(); } } |