From d0d76e36a6d0fd5e4de35fe022e33fdcb736a63e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 3 Jul 2019 17:13:15 +0200 Subject: Address timing issues with WM spawning --- src/windowmanager.cpp | 47 +++++++++++++++++++++++++++++++---------------- 1 file 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 #include #include +#include +#include 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(); } } -- cgit v1.2.3-55-g7522