qmlFrontend/hwengine.cpp
author unc0rr
Mon, 22 Sep 2014 02:10:39 +0400
branchqmlfrontend
changeset 10420 02c573d19224
parent 10418 091d2c0216c3
child 10424 4be6cd55f1cf
permissions -rw-r--r--
flib: - Ability to pass raw data via IPC qmlfrontend: - Pass message received by callback through event loop to handle it in main thread - Get preview image from engine on button click and render it in Image object
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>
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     5
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     6
#include "hwengine.h"
10420
unc0rr
parents: 10418
diff changeset
     7
#include "previewimageprovider.h"
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     8
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
     9
extern "C" {
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    10
    RunEngine_t *RunEngine;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    11
    registerIPCCallback_t *registerIPCCallback;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    12
    ipcToEngine_t *ipcToEngine;
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    13
    flibInit_t *flibInit;
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    14
}
10420
unc0rr
parents: 10418
diff changeset
    15
unc0rr
parents: 10418
diff changeset
    16
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) :
unc0rr
parents: 10418
diff changeset
    17
    QObject(parent),
unc0rr
parents: 10418
diff changeset
    18
    m_engine(engine)
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    19
{
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    20
    QLibrary hwlib("./libhwengine.so");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    21
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    22
    if(!hwlib.load())
10404
1baaab44a0b2 - Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents: 10402
diff changeset
    23
        qWarning() << "Engine library not found" << hwlib.errorString();
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    24
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    25
    RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    26
    registerIPCCallback = (registerIPCCallback_t*) hwlib.resolve("registerIPCCallback");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    27
    ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    28
    flibInit = (flibInit_t*) hwlib.resolve("flibInit");
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    29
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    30
    flibInit();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    31
    registerIPCCallback(this, &engineMessageCallback);
10402
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
HWEngine::~HWEngine()
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
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    38
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    39
void HWEngine::run()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    40
{
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    41
    m_argsList.clear();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    42
    m_argsList << "";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    43
    m_argsList << "--internal";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    44
    //m_argsList << "--user-prefix";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    45
    //m_argsList << cfgdir->absolutePath();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    46
    //m_argsList << "--prefix";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    47
    //m_argsList << datadir->absolutePath();
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    48
    m_argsList << "--landpreview";
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    49
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    50
    m_args.resize(m_argsList.size());
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    51
    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
    52
        m_args[i] = m_argsList[i].constData();
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    53
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    54
    RunEngine(m_args.size(), m_args.data());
10420
unc0rr
parents: 10418
diff changeset
    55
    sendIPC("eseed helloworld");
unc0rr
parents: 10418
diff changeset
    56
    sendIPC("e$mapgen 0");
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    57
    sendIPC("!");
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    58
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    59
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    60
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    61
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    62
    Q_UNUSED(scriptEngine)
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    63
10420
unc0rr
parents: 10418
diff changeset
    64
    HWEngine *hwengine = new HWEngine(engine);
10402
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    65
    return hwengine;
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    66
}
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    67
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    68
void HWEngine::exposeToQML()
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    69
{
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    70
    qDebug("HWEngine::exposeToQML");
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    71
    qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider);
3313336c1ee0 Let's get it started
unc0rr
parents:
diff changeset
    72
}
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    73
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    74
void HWEngine::sendIPC(const QByteArray & b)
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    75
{
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    76
    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
    77
    qDebug() << "sendIPC: len = " << len;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    78
10420
unc0rr
parents: 10418
diff changeset
    79
    ipcToEngine(b.constData(), len);
unc0rr
parents: 10418
diff changeset
    80
}
unc0rr
parents: 10418
diff changeset
    81
unc0rr
parents: 10418
diff changeset
    82
void HWEngine::engineMessageCallback(void *context, const char * msg, quint32 len)
unc0rr
parents: 10418
diff changeset
    83
{
unc0rr
parents: 10418
diff changeset
    84
    HWEngine * obj = (HWEngine *)context;
unc0rr
parents: 10418
diff changeset
    85
    QByteArray b = QByteArray::fromRawData(msg, len);
unc0rr
parents: 10418
diff changeset
    86
unc0rr
parents: 10418
diff changeset
    87
    qDebug() << "FLIPC in" << b.size() << b;
unc0rr
parents: 10418
diff changeset
    88
unc0rr
parents: 10418
diff changeset
    89
    QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(QByteArray, b));
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    90
}
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    91
10420
unc0rr
parents: 10418
diff changeset
    92
void HWEngine::engineMessageHandler(const QByteArray &msg)
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
    93
{
10420
unc0rr
parents: 10418
diff changeset
    94
    if(msg.size() == 128 * 256)
unc0rr
parents: 10418
diff changeset
    95
    {
unc0rr
parents: 10418
diff changeset
    96
        QVector<QRgb> colorTable;
unc0rr
parents: 10418
diff changeset
    97
        colorTable.resize(256);
unc0rr
parents: 10418
diff changeset
    98
        for(int i = 0; i < 256; ++i)
unc0rr
parents: 10418
diff changeset
    99
            colorTable[i] = qRgba(255, 255, 0, i);
unc0rr
parents: 10418
diff changeset
   100
unc0rr
parents: 10418
diff changeset
   101
        const quint8 *buf = (const quint8*) msg.constData();
unc0rr
parents: 10418
diff changeset
   102
        QImage im(buf, 256, 128, QImage::Format_Indexed8);
unc0rr
parents: 10418
diff changeset
   103
        im.setColorTable(colorTable);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   104
10420
unc0rr
parents: 10418
diff changeset
   105
        QPixmap px = QPixmap::fromImage(im, Qt::ColorOnly);
unc0rr
parents: 10418
diff changeset
   106
        //QPixmap pxres(px.size());
unc0rr
parents: 10418
diff changeset
   107
        //QPainter p(&pxres);
unc0rr
parents: 10418
diff changeset
   108
unc0rr
parents: 10418
diff changeset
   109
        //p.fillRect(pxres.rect(), linearGrad);
unc0rr
parents: 10418
diff changeset
   110
        //p.drawPixmap(0, 0, px);
unc0rr
parents: 10418
diff changeset
   111
unc0rr
parents: 10418
diff changeset
   112
        PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
unc0rr
parents: 10418
diff changeset
   113
        preview->setPixmap(px);
unc0rr
parents: 10418
diff changeset
   114
        emit previewImageChanged();
unc0rr
parents: 10418
diff changeset
   115
    }
10416
1c301054694d - Remove --port command
unc0rr
parents: 10404
diff changeset
   116
}