-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaFilter.cpp
More file actions
463 lines (378 loc) · 12.8 KB
/
Copy pathLuaFilter.cpp
File metadata and controls
463 lines (378 loc) · 12.8 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "LuaFilter.h"
#include "qkeysequence.h"
#include <QDebug>
#include <QFileInfo>
#include <QCoreApplication>
#include <exception>
int panic_handler(lua_State *L) {
const char* msg = lua_tostring(L, -1);
qDebug() << "LUA PANIC:" << (msg ? msg : "Unknown error");
return 0;
}
// Wrapper pro print_status(msg, [timeout])
static int l_print_status(lua_State *L) {
LuaFilter* self = (LuaFilter*)lua_touserdata(L, lua_upvalueindex(1));
const char* msg = lua_tostring(L, 1);
int timeout = luaL_optinteger(L, 2, 0);
// 3. Emit signal
if (self && msg) {
emit self->statusMessageRequested(QString::fromUtf8(msg), timeout);
}
return 0;
}
// Wrapper for print_log(msg) - Local echo to terminal
static int l_print_log(lua_State *L) {
LuaFilter* self = (LuaFilter*)lua_touserdata(L, lua_upvalueindex(1));
size_t len;
const char* msg = lua_tolstring(L, 1, &len);
if (self && msg) {
emit self->terminalLogRequested(QByteArray(msg, len));
}
return 0;
}
// Wrapper pro set_rts(bool)
static int l_set_rts(lua_State *L) {
LuaFilter* self = (LuaFilter*)lua_touserdata(L, lua_upvalueindex(1));
if (self) {
bool enable = lua_toboolean(L, 1);
emit self->setRtsRequested(enable);
}
return 0;
}
// Wrapper pro set_dtr(bool)
static int l_set_dtr(lua_State *L) {
LuaFilter* self = (LuaFilter*)lua_touserdata(L, lua_upvalueindex(1));
if (self) {
bool enable = lua_toboolean(L, 1);
emit self->setDtrRequested(enable);
}
return 0;
}
// Wrapper pro get_lines() -> vrací tabulku
static int l_get_lines(lua_State *L) {
LuaFilter* self = (LuaFilter*)lua_touserdata(L, lua_upvalueindex(1));
if (self) {
QVariantMap lines = self->getLastLines();
lua_newtable(L);
QMapIterator<QString, QVariant> i(lines);
while (i.hasNext()) {
i.next();
lua_pushstring(L, i.key().toUtf8().constData());
lua_pushboolean(L, i.value().toBool());
lua_settable(L, -3);
}
return 1; // Vracíme tabulku
}
return 0;
}
// Wrapper pro přímé odeslání dat (pro makra)
static int l_send_raw(lua_State *L) {
LuaFilter* self = (LuaFilter*)lua_touserdata(L, lua_upvalueindex(1));
size_t len;
const char* data = lua_tolstring(L, 1, &len);
if (self && data) {
// Emitujeme signál, který v MainWindow rovnou zapíše do portu
emit self->sendRawRequested(QByteArray(data, len));
}
return 0;
}
// ------------------------------------------------------------
LuaFilter::LuaFilter(QObject *parent) : QObject(parent)
{
initLua();
}
void LuaFilter::registerFunctions()
{
if (!L) return;
// 1. print_status
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, l_print_status, 1);
lua_setglobal(L, "print_status");
// 2. print_log
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, l_print_log, 1);
lua_setglobal(L, "print_log");
// 3. set_rts
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, l_set_rts, 1);
lua_setglobal(L, "set_rts");
// 4. set_dtr
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, l_set_dtr, 1);
lua_setglobal(L, "set_dtr");
// 5. get_lines
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, l_get_lines, 1);
lua_setglobal(L, "get_lines");
// 6. send_raw
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, l_send_raw, 1);
lua_setglobal(L, "send_raw");
}
void LuaFilter::initLua()
{
closeLua();
L = luaL_newstate();
lua_atpanic(L, panic_handler);
static const luaL_Reg safe_libs[] = {
{LUA_GNAME, luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_UTF8LIBNAME, luaopen_utf8},
// {LUA_IOLIBNAME, luaopen_io},
// {LUA_OSLIBNAME, luaopen_os},
{LUA_DBLIBNAME, luaopen_debug},
{NULL, NULL}
};
const luaL_Reg *lib;
for (lib = safe_libs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* module copy discard */
}
// // 1. print_status
// lua_pushlightuserdata(L, this); // Pushing 'this' on stack
// lua_pushcclosure(L, l_print_status, 1); // Creating a function with 1 "secret" variable (upvalue)
// lua_setglobal(L, "print_status"); // giving it name in lua
// // 2. print_log
// lua_pushlightuserdata(L, this);
// lua_pushcclosure(L, l_print_log, 1);
// lua_setglobal(L, "print_log");
registerFunctions();
qDebug() << "Lua Filter initialized (API Extended)";
}
LuaFilter::~LuaFilter()
{
if (L) {
lua_close(L);
}
}
void LuaFilter::closeLua()
{
if (L) {
lua_close(L);
L = nullptr;
}
m_scriptLoaded = false;
}
bool LuaFilter::loadScript(const QString &filePath)
{
m_scriptLoaded = false;
m_lastError.clear();
initLua();
qDebug() << "LUA LOAD: Loading file:" << filePath;
if (filePath.isEmpty()) return false;
if (!L) return false;
// TRY-CATCH BLOCK NECCESSARY!
try {
int ret = luaL_dofile(L, filePath.toLocal8Bit().constData());
if (ret != LUA_OK) {
m_lastError = QString::fromUtf8(lua_tostring(L, -1));
lua_pop(L, 1);
qDebug() << "LUA LOAD ERROR:" << m_lastError;
return false;
}
}
catch (const std::exception &e) {
qDebug() << "LUA C++ EXCEPTION:" << e.what();
return false;
}
catch (...) {
qDebug() << "LUA UNKNOWN C++ EXCEPTION";
return false;
}
qDebug() << "LUA LOAD: Success!";
m_scriptLoaded = true;
return true;
}
QByteArray LuaFilter::processRx(const QByteArray &inputData)
{
// If Lua isn't running, returning the input data (Pass-through)
if (!L || !m_scriptLoaded) return inputData;
// 1. Searching for "rx" function
lua_getglobal(L, "rx");
// 2. If not found (not provided by the author), returning the input
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1); // stack clean-up
return inputData;
}
// 3. rx(data) function found - calling it
lua_pushlstring(L, inputData.constData(), inputData.size());
// checking for error, optionaly returning error message
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
size_t len;
const char* errStr = lua_tolstring(L, -1, &len);
QByteArray err = QByteArray(errStr, len);
lua_pop(L, 1);
return "[LUA RX ERROR: %1]" + err;
}
// 4. Passing the result to display it
QByteArray result;
if (lua_isstring(L, -1)) {
size_t len;
const char* str = lua_tolstring(L, -1, &len);
result = QByteArray(str, len);
}
lua_pop(L, 1);
return result;
}
QByteArray LuaFilter::processTx(const QByteArray &inputData)
{
if (!L || !m_scriptLoaded) return inputData;
lua_getglobal(L, "tx");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return inputData;
}
lua_pushlstring(L, inputData.constData(), inputData.size());
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
QString err = QString::fromUtf8(lua_tostring(L, -1));
lua_pop(L, 1);
qDebug() << "Lua TX Error:" << err;
return inputData; // On error returning the original data
}
QByteArray result;
if (lua_isstring(L, -1)) {
size_t len;
const char* str = lua_tolstring(L, -1, &len);
result = QByteArray(str, (int)len);
} else {
// If the result is nil, returning empty array
result = QByteArray();
}
lua_pop(L, 1);
return result;
}
void LuaFilter::setGlobalInt(const QString &name, int value)
{
if (!L) return;
lua_pushinteger(L, value);
lua_setglobal(L, name.toUtf8().constData());
}
QByteArray LuaFilter::triggerResize(int rows, int cols)
{
setGlobalInt("TERM_ROWS", rows);
setGlobalInt("TERM_COLS", cols);
if (!L || !m_scriptLoaded) return QByteArray();
lua_getglobal(L, "on_resize");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return QByteArray(); // Function not found, nothing to do
}
lua_pushinteger(L, rows);
lua_pushinteger(L, cols);
if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
// Error in script -> write it out
QString err = QString::fromUtf8(lua_tostring(L, -1));
lua_pop(L, 1);
return "[LUA RESIZE ERROR: " + err.toUtf8() + "]";
}
QByteArray result;
if (lua_isstring(L, -1)) {
size_t len;
const char* str = lua_tolstring(L, -1, &len);
result = QByteArray(str, (int)len);
}
lua_pop(L, 1);
return result;
}
QByteArray LuaFilter::triggerTick(int deltaMs)
{
if (!L || !m_scriptLoaded) return QByteArray();
lua_getglobal(L, "on_tick");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return QByteArray(); // Function doesn't exist, nothing to do
}
lua_pushinteger(L, deltaMs);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
QString err = QString::fromUtf8(lua_tostring(L, -1));
lua_pop(L, 1);
qDebug() << "LUA TICK ERROR:" << err;
return QByteArray();
}
QByteArray result;
if (lua_isstring(L, -1)) {
size_t len;
const char* str = lua_tolstring(L, -1, &len);
result = QByteArray(str, (int)len);
}
lua_pop(L, 1);
return result;
}
void LuaFilter::updateSerialLines(const QVariantMap &lines)
{
// Pokud Lua neběží nebo není skript, jen aktualizujeme cache a končíme
if (!L || !m_scriptLoaded) {
m_lastLinesCache = lines;
return;
}
// Pokud se nic nezměnilo, končíme (optimalizace)
if (lines == m_lastLinesCache) return;
m_lastLinesCache = lines; // Aktualizace cache
// Hledáme funkci on_stat_line_change
lua_getglobal(L, "on_stat_line_change");
if (lua_isfunction(L, -1)) {
// Vytvoříme Lua tabulku z QVariantMap
lua_newtable(L);
QMapIterator<QString, QVariant> i(lines);
while (i.hasNext()) {
i.next();
lua_pushstring(L, i.key().toUtf8().constData());
lua_pushboolean(L, i.value().toBool());
lua_settable(L, -3);
}
// Volání: on_stat_line_change(table)
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
const char* error = lua_tostring(L, -1);
emit terminalLogRequested(QString("Lua Error (on_stat_line_change): %1\n").arg(error).toUtf8());
lua_pop(L, 1);
}
} else {
lua_pop(L, 1); // Funkce neexistuje, clean stack
}
}
bool LuaFilter::processKeyPress(int key, int modifiers)
{
// OPTIMALIZACE: Pokud Lua neběží, okamžitý návrat = NULOVÉ ZPOŽDĚNÍ
if (!L || !m_scriptLoaded) return false;
// Rychlý check, jestli funkce v Lua vůbec existuje
lua_getglobal(L, "on_key_down");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return false;
}
// Převedeme klávesu na string (např. "F1", "A", "Enter")
QString keyName;
if (key >= Qt::Key_F1 && key <= Qt::Key_F35) {
keyName = QString("F%1").arg(key - Qt::Key_F1 + 1);
} else if (key == Qt::Key_Return || key == Qt::Key_Enter) {
keyName = "Enter";
} else if (key == Qt::Key_Space) {
keyName = "Space";
} else {
// Zkusíme standardní znak, pokud je tisknutelný
QKeySequence seq(key);
keyName = seq.toString();
}
// Argument 1: Jméno klávesy
lua_pushstring(L, keyName.toUtf8().constData());
// Argument 2: Modifikátory (bool flagy v tabulce jsou hezčí než int)
lua_newtable(L);
lua_pushboolean(L, modifiers & Qt::ShiftModifier); lua_setfield(L, -2, "shift");
lua_pushboolean(L, modifiers & Qt::ControlModifier); lua_setfield(L, -2, "ctrl");
lua_pushboolean(L, modifiers & Qt::AltModifier); lua_setfield(L, -2, "alt");
// Voláme Lua: handled = on_key_down("F1", {shift=false, ...})
if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
QString err = QString::fromUtf8(lua_tostring(L, -1));
lua_pop(L, 1);
emit terminalLogRequested("[LUA KEY ERROR: " + err.toUtf8() + "]\n");
return false;
}
// Výsledek: true = klávesu jsme zpracovali (neposílat dál), false = nic
bool handled = lua_toboolean(L, -1);
lua_pop(L, 1);
return handled;
}