author | unc0rr |
Sun, 28 Sep 2014 00:18:01 +0400 | |
branch | qmlfrontend |
changeset 10426 | 727a154cf784 |
parent 10424 | 4be6cd55f1cf |
child 10428 | 7c25297720f1 |
permissions | -rw-r--r-- |
10402 | 1 |
#include <QLibrary> |
2 |
#include <QtQml> |
|
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
3 |
#include <QDebug> |
10420 | 4 |
#include <QPainter> |
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
5 |
#include <QUuid> |
10402 | 6 |
|
7 |
#include "hwengine.h" |
|
10420 | 8 |
#include "previewimageprovider.h" |
10402 | 9 |
|
10 |
extern "C" { |
|
10416 | 11 |
RunEngine_t *RunEngine; |
10426 | 12 |
registerPreviewCallback_t *registerPreviewCallback; |
10416 | 13 |
ipcToEngine_t *ipcToEngine; |
14 |
flibInit_t *flibInit; |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
15 |
flibFree_t *flibFree; |
10402 | 16 |
} |
10420 | 17 |
|
18 |
HWEngine::HWEngine(QQmlEngine *engine, QObject *parent) : |
|
19 |
QObject(parent), |
|
20 |
m_engine(engine) |
|
10402 | 21 |
{ |
10404
1baaab44a0b2
- Fix arguments parsing in engine assuming paramcount > 0
unc0rr
parents:
10402
diff
changeset
|
22 |
QLibrary hwlib("./libhwengine.so"); |
10402 | 23 |
|
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 | 26 |
|
10416 | 27 |
RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine"); |
10426 | 28 |
registerPreviewCallback = (registerPreviewCallback_t*) hwlib.resolve("registerIPCCallback"); |
10416 | 29 |
ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine"); |
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 | 32 |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
33 |
flibInit(".", "~/.hedgewars"); |
10426 | 34 |
registerPreviewCallback(this, &enginePreviewCallback); |
10402 | 35 |
} |
36 |
||
37 |
HWEngine::~HWEngine() |
|
38 |
{ |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
39 |
flibFree(); |
10402 | 40 |
} |
41 |
||
42 |
void HWEngine::run() |
|
43 |
{ |
|
10416 | 44 |
m_argsList.clear(); |
45 |
m_argsList << ""; |
|
46 |
m_argsList << "--internal"; |
|
47 |
m_argsList << "--landpreview"; |
|
48 |
||
49 |
m_args.resize(m_argsList.size()); |
|
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 | 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 | 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 | 57 |
sendIPC("e$mapgen 0"); |
10416 | 58 |
sendIPC("!"); |
10402 | 59 |
} |
60 |
||
61 |
static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
|
62 |
{ |
|
63 |
Q_UNUSED(scriptEngine) |
|
64 |
||
10420 | 65 |
HWEngine *hwengine = new HWEngine(engine); |
10402 | 66 |
return hwengine; |
67 |
} |
|
68 |
||
69 |
void HWEngine::exposeToQML() |
|
70 |
{ |
|
71 |
qDebug("HWEngine::exposeToQML"); |
|
72 |
qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider); |
|
73 |
} |
|
10416 | 74 |
|
75 |
void HWEngine::sendIPC(const QByteArray & b) |
|
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 | 79 |
|
10420 | 80 |
ipcToEngine(b.constData(), len); |
81 |
} |
|
82 |
||
10426 | 83 |
void HWEngine::enginePreviewCallback(void *context, const char * msg, quint32 len) |
10420 | 84 |
{ |
85 |
HWEngine * obj = (HWEngine *)context; |
|
86 |
QByteArray b = QByteArray::fromRawData(msg, len); |
|
87 |
||
88 |
qDebug() << "FLIPC in" << b.size() << b; |
|
89 |
||
90 |
QMetaObject::invokeMethod(obj, "engineMessageHandler", Qt::QueuedConnection, Q_ARG(QByteArray, b)); |
|
10416 | 91 |
} |
92 |
||
10420 | 93 |
void HWEngine::engineMessageHandler(const QByteArray &msg) |
10416 | 94 |
{ |
10426 | 95 |
if(msg.size() == 128 * 256) |
96 |
{ |
|
97 |
PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview")); |
|
98 |
preview->setPixmap(msg); |
|
99 |
emit previewImageChanged(); |
|
100 |
} |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
101 |
} |
10416 | 102 |
|
10424
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
103 |
QString HWEngine::currentSeed() |
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
104 |
{ |
4be6cd55f1cf
- Get rid of engine's PathPrefix and UserPathPrefix
unc0rr
parents:
10420
diff
changeset
|
105 |
return m_seed; |
10416 | 106 |
} |