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/dialog.cpp | 9 +++++++++ src/dialog.h | 2 ++ src/windowmanager.cpp | 46 +++++++++++++++++++++++++++++++++------------- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/dialog.cpp b/src/dialog.cpp index dbc4b8e..bdeab16 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -26,11 +26,14 @@ static bool isProcessRunning(const QString &binary); +QDialog* Dialog::instance = nullptr; + static QDomDocument toDomDocument(const QString& what, const QByteArray& data, const QString& backupFile, const QString& mandatoryChild); Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { + instance = this; model_[TAB_NATIVE] = new SessionTreeModel(parent); model_[TAB_RECENT_COURSES] = new SessionTreeModel(parent, true); model_[TAB_ALL_VMS] = new SessionTreeModel(parent); @@ -110,6 +113,7 @@ Dialog::Dialog(QWidget *parent) } Dialog::~Dialog() { + instance = nullptr; delete ui; } @@ -821,6 +825,11 @@ void Dialog::showEvent(QShowEvent *e) }); } +QDialog* Dialog::getInstance() +{ + return instance; +} + static bool isProcessRunning(const QString &binary) { bool full = binary.contains('/'); diff --git a/src/dialog.h b/src/dialog.h index cc7f50e..d9c5d6b 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -41,6 +41,7 @@ class Dialog : public QDialog { void setTheme(); void startSession(const QString& name); void downloadData(const QString& locationIds); + static QDialog* getInstance(); protected: // Overrides void changeEvent(QEvent *e); @@ -50,6 +51,7 @@ class Dialog : public QDialog { void showEvent(QShowEvent * e) override; private: // Private vars n methods + static QDialog* instance; Ui::Dialog *ui; SessionTreeModel *model_[TAB_COUNT]; QPushButton *tabs_[TAB_COUNT]; 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