From 98ecc8a132becaa4e37939ca5f7fd2031c603565 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 23 Oct 2019 14:14:54 +0200 Subject: Try even harder to restore focus after spawning openbox I just need a bigger hammer. --- src/windowmanager.cpp | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'src/windowmanager.cpp') diff --git a/src/windowmanager.cpp b/src/windowmanager.cpp index c05e234..74b1dad 100644 --- a/src/windowmanager.cpp +++ b/src/windowmanager.cpp @@ -1,4 +1,5 @@ #include "windowmanager.h" +#include "dialog.h" #include #include @@ -10,8 +11,12 @@ namespace WindowManager { static QProcess wm; +static int topCounter; + static void killInstance(); +static void forceChooserToTop(); + void ensureRunning() { static bool once = false; @@ -23,19 +28,11 @@ void ensureRunning() wm.closeReadChannel(QProcess::StandardOutput); wm.closeWriteChannel(); wm.waitForStarted(500); - QTimer::singleShot(500, []() { - if (wm.state() == QProcess::Running) { - qDebug() << "- Spawned openbox"; - QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, killInstance); - // Try to make vmchooser the foreground window again, since starting the WM after - // vmchooser makes it lose focus - QProcess::startDetached("wmctrl", QStringList() << "-a" << "vmchooser" << "-F"); - } else if (wm.exitCode() == 0) { - qDebug() << "- A WM is already running"; - } else { - qDebug() << "- openbox binary not in $PATH"; - } - }); + if (wm.state() == QProcess::Running) { + QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, killInstance); + } + topCounter = 0; + QTimer::singleShot(100, forceChooserToTop); } void stopOwnInstance(bool waitSync) @@ -65,4 +62,27 @@ void killInstance() } } +void forceChooserToTop() +{ + if (wm.state() == QProcess::Running) { + // Try to make vmchooser the foreground window again, since starting the WM after + // vmchooser makes it lose focus + QProcess::startDetached("wmctrl", QStringList() << "-a" << "vmchooser" << "-F"); + QDialog* d = Dialog::getInstance(); + if (d != nullptr) { + d->raise(); + d->activateWindow(); + } + if (++topCounter >= 13) { + qDebug() << "- Spawned openbox"; + } else { + QTimer::singleShot(100, forceChooserToTop); + } + } else if (wm.exitCode() == 0) { + qDebug() << "- A WM is already running"; + } else { + qDebug() << "- openbox binary not in $PATH"; + } +} + } -- cgit v1.2.3-55-g7522