summaryrefslogtreecommitdiffstats
path: root/src/windowmanager.cpp
blob: e85a08e966666cd965c078e9732c52cf75886b07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "windowmanager.h"

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

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(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()
{
    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();
    }
}

}