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




                          

                           




                         

                           

                    



                             



                                                  
                           










                                                                                                         






                                                        












                                                      



     
#include "windowmanager.h"

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

namespace WindowManager {

static QProcess wm;

static void killInstance();

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);
    QTimer::singleShot(100, []() {
        if (wm.state() == QProcess::Running) {
            qDebug() << "- Spawned openbox";
            QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, killInstance);
            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";
        }
    });
}

void stopOwnInstance()
{
    if (wm.state() == QProcess::Running) {
        qDebug() << "- Politely terminating spawned WM";
        wm.terminate();
        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();
    }
}

}