#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QDebug>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QDir>

static void msleep(unsigned long msecs)
{
    QMutex mutex;
    mutex.lock();

    QWaitCondition waitCondition;
    waitCondition.wait(&mutex, msecs);

    mutex.unlock();
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QProcess proc;
    QString cwd = QDir::currentPath();
    // This version will hang forever in drainOutputPipes ..
//    QString emul = cwd + "/../Emulator/debug/Emulator.exe spawntype=createprocess";
    // .. this one won't.
    QString emul = cwd + "/../Emulator/debug/Emulator.exe spawntype=createprocess -noinherit";
    proc.start(emul);
    while(1) {
        msleep(500);
        if(proc.waitForFinished(0)) {
            qDebug() << "waitForFinished done" << endl;
            return;
        }
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
