qmlFrontend/hwengine.cpp
author unc0rr
Sat, 20 Sep 2014 00:56:54 +0400
branchqmlfrontend
changeset 10416 1c301054694d
parent 10404 1baaab44a0b2
child 10418 091d2c0216c3
permissions -rw-r--r--
- Remove --port command - Some refactoring here and there - Try to get preview from engine, which doesn't work due to some ABI mismatch, probably shouldn't try to pass string255 to pascal
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     1
#include <QLibrary>
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     2
#include <QtQml>
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
     3
#include <QDebug>
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     4
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     5
#include "hwengine.h"
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     6
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     7
extern "C" {
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
     8
    RunEngine_t *RunEngine;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
     9
    registerIPCCallback_t *registerIPCCallback;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    10
    ipcToEngine_t *ipcToEngine;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    11
    flibInit_t *flibInit;
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    12
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    13
HWEngine::HWEngine(QObject *parent) :
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    14
    QObject(parent)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    15
{
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    16
    QLibrary hwlib("./libhwengine.so");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    17
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    18
    if(!hwlib.load())
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    19
        qWarning() << "Engine library not found" << hwlib.errorString();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    20
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    21
    RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    22
    registerIPCCallback = (registerIPCCallback_t*) hwlib.resolve("registerIPCCallback");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    23
    ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    24
    flibInit = (flibInit_t*) hwlib.resolve("flibInit");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    25
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    26
    flibInit();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    27
    registerIPCCallback(this, &engineMessageCallback);
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    28
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    29
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    30
HWEngine::~HWEngine()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    31
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    32
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    33
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    34
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    35
void HWEngine::run()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    36
{
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    37
    m_argsList.clear();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    38
    m_argsList << "";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    39
    m_argsList << "--internal";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    40
    //m_argsList << "--user-prefix";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    41
    //m_argsList << cfgdir->absolutePath();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    42
    //m_argsList << "--prefix";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    43
    //m_argsList << datadir->absolutePath();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    44
    m_argsList << "--landpreview";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    45
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    46
    m_args.resize(m_argsList.size());
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    47
    for(int i = m_argsList.size() - 1; i >=0; --i)
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    48
        m_args[i] = m_argsList[i].data();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    49
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    50
    RunEngine(m_args.size(), m_args.data());
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    51
    sendIPC("!");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    52
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    53
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    54
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    55
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    56
    Q_UNUSED(engine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    57
    Q_UNUSED(scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    58
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    59
    HWEngine *hwengine = new HWEngine();
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    60
    return hwengine;
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    61
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    62
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    63
void HWEngine::exposeToQML()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    64
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    65
    qDebug("HWEngine::exposeToQML");
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    66
    qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider);
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    67
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    68
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    69
void HWEngine::sendIPC(const QByteArray & b)
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    70
{
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    71
    string255 str;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    72
    str.len = b.size() > 255 ? 255 : b.size();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    73
    qDebug() << "semdIPC: len = " << str.len;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    74
    qCopy(b.data(), &(b.data()[str.len - 1]), &(str.str[0]));
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    75
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    76
    ipcToEngine(str);
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    77
}
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    78
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    79
void HWEngine::engineMessageCallback(void *context, string255 str)
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    80
{
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    81
    QByteArray b = QByteArray::fromRawData((const char *)&str.s, str.len + 1);
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    82
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    83
    qDebug() << "FLIPC in" << b;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    84
}