qmlFrontend/hwengine.cpp
author unc0rr
Sun, 21 Sep 2014 00:37:50 +0400
branchqmlfrontend
changeset 10418 091d2c0216c3
parent 10416 1c301054694d
child 10420 02c573d19224
permissions -rw-r--r--
Move away from passing shortstrings into C code, now IPC works
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)
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    48
        m_args[i] = m_argsList[i].constData();
10416
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
{
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    71
    quint8 len = b.size() > 255 ? 255 : b.size();
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    72
    qDebug() << "sendIPC: len = " << len;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    73
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    74
    ipcToEngine(len, b.constData());
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    75
}
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    76
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    77
void HWEngine::engineMessageCallback(void *context, quint8 len, const char *msg)
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    78
{
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    79
    QByteArray b = QByteArray::fromRawData(msg, len);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    80
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    81
    qDebug() << "FLIPC in" << b;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    82
}