summaryrefslogblamecommitdiffstats
path: root/src/windowmanager.cpp
blob: 74b1dadfa329c84ed2d92c0f2bcb43d52edbc39f (plain) (tree)
1
2
3
4
5
6
7
8
                          
                   



                   

                           




                         

                      

                           

                                

                    



                             



                                                  
                           




                                                                                                     

 
                                   


                                                        





                                                  






                                          


                                                                     



                                                      


     






















                                                                                         
 
#include "windowmanager.h"
#include "dialog.h"

#include <QProcess>
#include <QTimer>
#include <QDebug>
#include <QThread>
#include <QCoreApplication>

namespace WindowManager {

static QProcess wm;

static int topCounter;

static void killInstance();

static void forceChooserToTop();

void ensureRunning()
{
    static bool once = false;
    if (once)
        return;
    once = true;
    wm.start(QLatin1String("openbox"));
    wm.closeReadChannel(QProcess::StandardError);
    wm.closeReadChannel(QProcess::StandardOutput);
    wm.closeWriteChannel();
    wm.waitForStarted(500);
    if (wm.state() == QProcess::Running) {
        QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, killInstance);
    }
    topCounter = 0;
    QTimer::singleShot(100, forceChooserToTop);
}

void stopOwnInstance(bool waitSync)
{
    if (wm.state() == QProcess::Running) {
        qDebug() << "- Politely terminating spawned WM";
        if (waitSync) {
            killInstance();
        } else {
            wm.terminate();
            QTimer::singleShot(500, killInstance);
        }
    }
}

void killInstance()
{
    if (wm.state() == QProcess::Running) {
        wm.terminate();
    }
    for (int i = 0; i < 50 && wm.state() == QProcess::Running; ++i) {
        QThread::msleep(10);
    }
    if (wm.state() == QProcess::Running) {
        qDebug() << "- Forcefully killing spawned WM";
        wm.kill();
    }
}

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";
    }
}

}