-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheqml.cpp
434 lines (360 loc) · 8.86 KB
/
eqml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <ei.h>
#include <QThread>
#include <QDateTime>
#include <QApplication>
#include <QQuickView>
#include <QQuickItem>
#include <QQmlEngine>
class eqmlTerm
{
const char * _buf;
int _index;
int _type;
int _arity;
int _size;
public:
eqmlTerm(const eqmlTerm &);
eqmlTerm(const char * buf, int index = 0) : _buf(buf), _index(index)
{
if (index == 0)
ei_decode_version(_buf, &_index, NULL);
ei_get_type(_buf, &_index, &_type, &_size);
if (isTuple())
ei_decode_tuple_header(_buf, &_index, &_arity);
}
eqmlTerm operator[] (int elem) const
{
int idx = _index;
for (int i = 1; i < elem; ++i)
ei_skip_term(_buf, &idx);
return eqmlTerm(_buf, idx);
}
QByteArray atom() const
{
int idx = _index; char p[MAXATOMLEN];
ei_decode_atom(_buf, &idx, p);
return QByteArray(p);
}
bool isTuple() const
{
return _type == ERL_SMALL_TUPLE_EXT || _type == ERL_LARGE_TUPLE_EXT;
}
bool isDouble() const
{
return _type == ERL_FLOAT_EXT || _type == NEW_FLOAT_EXT;
}
bool isAtom() const
{
return
_type == ERL_ATOM_EXT ||
_type == ERL_SMALL_ATOM_EXT ||
_type == ERL_ATOM_UTF8_EXT ||
_type == ERL_SMALL_ATOM_UTF8_EXT;
}
bool isInteger() const
{
return
_type == ERL_INTEGER_EXT ||
_type == ERL_SMALL_INTEGER_EXT ||
_type == ERL_SMALL_BIG_EXT ||
_type == ERL_LARGE_BIG_EXT;
}
bool isString() const
{
return
_type == ERL_LIST_EXT ||
_type == ERL_STRING_EXT ||
_type == ERL_NIL_EXT;
}
bool toBool() const
{
QByteArray a = atom();
if (a == "true" ) return true;
if (a == "false") return false;
qWarning("can't cast term to bool");
return false;
}
int toInteger() const
{
int idx = _index; long p;
ei_decode_long(_buf, &idx, &p);
return p;
}
double toDouble() const
{
int idx = _index; double p;
ei_decode_double(_buf, &idx, &p);
return p;
}
double toScalar() const
{
if (isInteger())
return toInteger();
if (isDouble())
return toDouble();
qWarning("can't cast term to scalar");
return 0.0;
}
QString toString() const
{
int idx = _index; QByteArray a(_size, 0);
ei_decode_string(_buf, &idx, a.data());
return QString(a);
}
QByteArray toArray() const
{
int idx = _index; QByteArray a(_size, 0);
if (isAtom())
ei_decode_atom(_buf, &idx, a.data());
else
ei_decode_string(_buf, &idx, a.data());
return a;
}
};
class eqmlLink : public QObject
{
Q_OBJECT
char _buf[1024];
int _index;
QDataStream & _os;
void end(int index)
{
_os << index;
_os.writeRawData(_buf, index);
}
int push(int index, const QVariant & v)
{
switch (v.type())
{
case QMetaType::Int:
ei_encode_long(_buf, &index, v.toInt());
break;
case QMetaType::Double:
ei_encode_double(_buf, &index, v.toDouble());
break;
case QMetaType::QString:
ei_encode_string(_buf, &index, qPrintable(v.toString()));
break;
default:
qWarning("can't cast QVariant to term");
}
return index;
}
public:
eqmlLink(QDataStream & os, QObject * obj, const eqmlTerm & term)
: _index(0)
, _os(os)
{
int order = term[4].toInteger();
QByteArray Args = "(" + QByteArray("QVariant").repeated(order) + ")";
Args.replace("tQ", "t,Q");
int signalIdx = obj->metaObject()->indexOfSignal(term[2].toArray() + Args);
QMetaMethod signalMethod = obj->metaObject()->method(signalIdx);
int slotIdx = metaObject()->indexOfSlot("link" + Args);
QMetaMethod slotMethod = metaObject()->method(slotIdx);
if (!QObject::connect(obj, signalMethod, this, slotMethod))
qWarning("connection fail");
ei_encode_version(_buf, &_index);
ei_encode_tuple_header(_buf, &_index, order + 3);
ei_encode_atom(_buf, &_index, "signal");
ei_encode_string(_buf, &_index, term[5].toArray().data());
ei_encode_atom(_buf, &_index, term[3].toArray().data());
}
public slots:
void link()
{
end(_index);
}
void link(const QVariant& a)
{
end(push(_index, a));
}
void link(const QVariant& a, const QVariant& b)
{
end(push(push(_index, a), b));
}
void link(const QVariant& a, const QVariant& b, const QVariant& c)
{
end(push(push(push(_index, a), b), c));
}
void link(const QVariant& a, const QVariant& b, const QVariant& c, const QVariant& d)
{
end(push(push(push(push(_index, a), b), c), d));
}
void link(const QVariant& a, const QVariant& b, const QVariant& c, const QVariant& d, const QVariant& e)
{
end(push(push(push(push(push(_index, a), b), c), d), e));
}
};
class eqmlPipe : public QThread
{
Q_OBJECT
public:
void run()
{
QFile inFile;
inFile.open(3, QIODevice::ReadOnly | QIODevice::Unbuffered);
QDataStream inStream(&inFile);
QByteArray buffer;
quint32 len;
next:
inStream >> len;
if (inStream.status() == QDataStream::Ok)
{
buffer.resize(len);
if (inStream.readRawData(buffer.data(), len) >= 0)
{
emit packet(buffer);
goto next;
}
}
}
signals:
void packet(QByteArray a);
};
class eqmlWindow : public QQuickView
{
Q_OBJECT
typedef void (eqmlWindow::*Handle)(const eqmlTerm &);
typedef QMap<QByteArray, Handle> Registry;
Registry registry;
QFile outFile;
QDataStream outStream;
typedef QVariant (eqmlWindow::*VarFun)(const eqmlTerm &);
typedef QMap<QByteArray, VarFun> VarMap;
VarMap _varMap;
QObject * find(const eqmlTerm & term)
{
QByteArray name = term.toArray();
QObject * root = rootObject();
if (root->objectName() == name)
return root;
if (QObject * obj = root->findChild<QObject *>(name))
return obj;
qWarning("can't find child %s", name.data());
return NULL;
}
public:
eqmlWindow(const QString & qmlFile)
{
connect(engine(), SIGNAL(quit()), SLOT(close()));
setResizeMode(QQuickView::SizeRootObjectToView);
setSource(QUrl::fromLocalFile(qmlFile));
show();
outFile.open(4, QIODevice::WriteOnly | QIODevice::Unbuffered);
outStream.setDevice(&outFile);
registry["connect"] = &eqmlWindow::onConnect;
registry["set"] = &eqmlWindow::onSet;
registry["invoke0"] = &eqmlWindow::onInvoke0;
registry["invoke1"] = &eqmlWindow::onInvoke1;
registry["invoke2"] = &eqmlWindow::onInvoke2;
registry["invoke3"] = &eqmlWindow::onInvoke3;
_varMap["url"] = &eqmlWindow::url;
_varMap["point"] = &eqmlWindow::point;
_varMap["datetime"] = &eqmlWindow::datetime;
}
QVariant var(const eqmlTerm & t)
{
if (t.isInteger())
return t.toInteger();
else if (t.isDouble())
return t.toDouble();
else if (t.isString())
return t.toString();
else if (t.isAtom())
return t.toBool();
else if (t.isTuple()) {
QByteArray tag = t[1].toArray();
VarMap::iterator it = _varMap.find(tag);
if (it != _varMap.end())
return (this->*it.value())(t[2]);
else
qWarning("unknown var \'%s\'", tag.data());
}
qWarning("can't cast term to QVariant");
return QVariant();
}
QVariant url(const eqmlTerm & t)
{
return QUrl(t.toString());
}
QVariant point(const eqmlTerm & t)
{
return QPointF(t[1].toScalar(), t[2].toScalar());
}
QVariant datetime(const eqmlTerm & t)
{
const eqmlTerm & date = t[1];
const eqmlTerm & time = t[2];
return QDateTime(
QDate(date[1].toInteger(), date[2].toInteger(), date[3].toInteger()),
QTime(time[1].toInteger(), time[2].toInteger(), time[3].toInteger())
);
}
void onConnect(const eqmlTerm & term)
{
QObject * obj = find(term[1]);
if (!obj)
return;
new eqmlLink(outStream, obj, term);
}
void onInvoke0(const eqmlTerm & t)
{
if (QObject * obj = find(t[1]))
QMetaObject::invokeMethod(obj, t[2].toArray());
}
void onInvoke1(const eqmlTerm & t)
{
if (QObject * obj = find(t[1]))
QMetaObject::invokeMethod(obj, t[2].toArray(),
Q_ARG(QVariant, var(t[3])));
}
void onInvoke2(const eqmlTerm & t)
{
if (QObject * obj = find(t[1]))
QMetaObject::invokeMethod(obj, t[2].toArray(),
Q_ARG(QVariant, var(t[3])), Q_ARG(QVariant, var(t[4])));
}
void onInvoke3(const eqmlTerm & t)
{
if (QObject * obj = find(t[1]))
QMetaObject::invokeMethod(obj, t[2].toArray(),
Q_ARG(QVariant, var(t[3])), Q_ARG(QVariant, var(t[4])), Q_ARG(QVariant, var(t[5])));
}
void onSet(const eqmlTerm & t)
{
if (QObject * obj = find(t[1]))
obj->setProperty(t[2].toArray(), var(t[3]));
}
public slots:
void dispatch(QByteArray buffer)
{
eqmlTerm term(buffer.data());
QByteArray tag = term[1].toArray();
Registry::iterator it = registry.find(tag);
if (it != registry.end())
(this->*it.value())(term[2]);
else
qWarning("unknown tag \'%s\', size=%d", tag.data(), tag.size());
}
};
void eqmlLog(QtMsgType, const QMessageLogContext &, const QString & msg)
{
fprintf(stderr, "%s\r\n", qPrintable(msg));
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(eqmlLog);
QApplication app(argc, argv);
if (app.arguments().size() < 2) {
qWarning("No QML file specified");
return -1;
}
eqmlPipe pipe;
eqmlWindow win(app.arguments().at(1));
app.connect(&pipe, SIGNAL(finished()), SLOT(quit()));
win.connect(&pipe, SIGNAL(packet(QByteArray)), SLOT(dispatch(QByteArray)));
pipe.start();
return app.exec();
}
#include "eqml.moc"