qmlFrontend/hwengine.cpp
author unc0rr
Sat, 27 Sep 2014 12:49:08 +0400
branchqmlfrontend
changeset 10424 4be6cd55f1cf
parent 10420 02c573d19224
child 10426 727a154cf784
permissions -rw-r--r--
- Get rid of engine's PathPrefix and UserPathPrefix - Some refactoring
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>
10420
unc0rr
parents: 10418
diff changeset
     4
#include <QPainter>
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
     5
#include <QUuid>
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     6
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     7
#include "hwengine.h"
10420
unc0rr
parents: 10418
diff changeset
     8
#include "previewimageprovider.h"
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     9
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    10
extern "C" {
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    11
    RunEngine_t *RunEngine;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    12
    registerIPCCallback_t *registerIPCCallback;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    13
    ipcToEngine_t *ipcToEngine;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    14
    flibInit_t *flibInit;
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    15
    flibFree_t *flibFree;
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    16
}
10420
unc0rr
parents: 10418
diff changeset
    17
unc0rr
parents: 10418
diff changeset
    18
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) :
unc0rr
parents: 10418
diff changeset
    19
    QObject(parent),
unc0rr
parents: 10418
diff changeset
    20
    m_engine(engine)
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    21
{
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    22
    QLibrary hwlib("./libhwengine.so");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    23
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    24
    if(!hwlib.load())
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    25
        qWarning() << "Engine library not found" << hwlib.errorString();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    26
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    27
    RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    28
    registerIPCCallback = (registerIPCCallback_t*) hwlib.resolve("registerIPCCallback");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    29
    ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    30
    flibInit = (flibInit_t*) hwlib.resolve("flibInit");
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    31
    flibFree = (flibFree_t*) hwlib.resolve("flibFree");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    32
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    33
    flibInit(".", "~/.hedgewars");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    34
    registerIPCCallback(this, &engineMessageCallback);
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    35
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    36
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    37
HWEngine::~HWEngine()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    38
{
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    39
    flibFree();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    40
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    41
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    42
void HWEngine::run()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    43
{
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    44
    m_argsList.clear();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    45
    m_argsList << "";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    46
    m_argsList << "--internal";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    47
    m_argsList << "--landpreview";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    48
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    49
    m_args.resize(m_argsList.size());
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    50
    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
    51
        m_args[i] = m_argsList[i].constData();
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    52
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    53
    m_seed = QUuid::createUuid().toString();
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    54
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    55
    RunEngine(m_args.size(), m_args.data());
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    56
    sendIPC("eseed " + m_seed.toLatin1());
10420
unc0rr
parents: 10418
diff changeset
    57
    sendIPC("e$mapgen 0");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    58
    sendIPC("!");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    59
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    60
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    61
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    62
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    63
    Q_UNUSED(scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    64
10420
unc0rr
parents: 10418
diff changeset
    65
    HWEngine *hwengine = new HWEngine(engine);
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    66
    return hwengine;
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    67
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    68
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    69
void HWEngine::exposeToQML()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    70
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    71
    qDebug("HWEngine::exposeToQML");
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    72
    qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider);
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    73
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    74
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    75
void HWEngine::sendIPC(const QByteArray & b)
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
    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
    78
    qDebug() << "sendIPC: len = " << len;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    79
10420
unc0rr
parents: 10418
diff changeset
    80
    ipcToEngine(b.constData(), len);
unc0rr
parents: 10418
diff changeset
    81
}
unc0rr
parents: 10418
diff changeset
    82
unc0rr
parents: 10418
diff changeset
    83
void HWEngine::engineMessageCallback(void *context, const char * msg, quint32 len)
unc0rr
parents: 10418
diff changeset
    84
{
unc0rr
parents: 10418
diff changeset
    85
    HWEngine * obj = (HWEngine *)context;
unc0rr
parents: 10418
diff changeset
    86
    QByteArray b = QByteArray::fromRawData(msg, len);
unc0rr
parents: 10418
diff changeset
    87
unc0rr
parents: 10418
diff changeset
    88
    qDebug() << "FLIPC in" << b.size() << b;
unc0rr
parents: 10418
diff changeset
    89
unc0rr
parents: 10418
diff changeset
    90
    QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(QByteArray, b));
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    91
}
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    92
10420
unc0rr
parents: 10418
diff changeset
    93
void HWEngine::engineMessageHandler(const QByteArray &msg)
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    94
{
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    95
    PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    96
    preview->setPixmap(msg);
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    97
    emit previewImageChanged();
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
    98
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    99
10424
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   100
QString HWEngine::currentSeed()
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   101
{
4be6cd55f1cf - Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents: 10420
diff changeset
   102
    return m_seed;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   103
}