-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathLuaControl.cpp
More file actions
546 lines (442 loc) · 11.8 KB
/
Copy pathLuaControl.cpp
File metadata and controls
546 lines (442 loc) · 11.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2020 mjbudd77
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// LuaControl.cpp
//
#include <stdio.h>
#include <string.h>
#include <list>
#ifdef WIN32
#include <Windows.h>
#endif
#include <QTextEdit>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include "../../fceu.h"
#ifdef _S9XLUA_H
#include "../../fceulua.h"
#endif
#include "common/os_utils.h"
#include "Qt/LuaControl.h"
#include "Qt/main.h"
#include "Qt/input.h"
#include "Qt/config.h"
#include "Qt/keyscan.h"
#include "Qt/fceuWrapper.h"
#include "Qt/ConsoleUtilities.h"
static bool luaScriptRunning = false;
static bool updateLuaDisplay = false;
static bool openLuaKillMsgBox = false;
static int luaKillMsgBoxRetVal = 0;
struct luaConsoleOutputBuffer
{
int head;
int tail;
int size;
char *buf;
luaConsoleOutputBuffer(void)
{
tail = head = 0;
size = 4096;
buf = (char *)malloc(size);
}
~luaConsoleOutputBuffer(void)
{
if (buf)
{
free(buf);
buf = NULL;
}
}
void addLine(const char *l)
{
int i = 0;
//printf("Adding Line %i: '%s'\n", head, l );
while (l[i] != 0)
{
buf[head] = l[i];
i++;
head = (head + 1) % size;
if (head == tail)
{
tail = (tail + 1) % size;
}
}
}
void clear(void)
{
tail = head = 0;
}
};
static luaConsoleOutputBuffer outBuf;
static std::list<LuaControlDialog_t *> winList;
static void updateLuaWindows(void);
//----------------------------------------------------
LuaControlDialog_t::LuaControlDialog_t(QWidget *parent)
: QDialog(parent, Qt::Window)
{
QVBoxLayout *mainLayout;
QHBoxLayout *hbox;
QPushButton *closeButton;
QLabel *lbl;
std::string filename;
QSettings settings;
resize(512, 512);
setWindowTitle(tr("Lua Script Control"));
mainLayout = new QVBoxLayout();
lbl = new QLabel(tr("Script File:"));
scriptPath = new QLineEdit();
scriptArgs = new QLineEdit();
g_config->getOption("SDL.LastLoadLua", &filename);
scriptPath->setText( tr(filename.c_str()) );
scriptPath->setClearButtonEnabled(true);
scriptArgs->setClearButtonEnabled(true);
luaOutput = new QTextEdit();
luaOutput->setReadOnly(true);
hbox = new QHBoxLayout();
browseButton = new QPushButton(tr("Browse"));
stopButton = new QPushButton(tr("Stop"));
if (luaScriptRunning)
{
startButton = new QPushButton(tr("Restart"));
}
else
{
startButton = new QPushButton(tr("Start"));
}
stopButton->setEnabled(luaScriptRunning);
connect(browseButton, SIGNAL(clicked()), this, SLOT(openLuaScriptFile(void)));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stopLuaScript(void)));
connect(startButton, SIGNAL(clicked()), this, SLOT(startLuaScript(void)));
hbox->addWidget(browseButton);
hbox->addWidget(stopButton);
hbox->addWidget(startButton);
mainLayout->addWidget(lbl);
mainLayout->addWidget(scriptPath);
mainLayout->addLayout(hbox);
hbox = new QHBoxLayout();
lbl = new QLabel(tr("Arguments:"));
hbox->addWidget(lbl);
hbox->addWidget(scriptArgs);
mainLayout->addLayout(hbox);
lbl = new QLabel(tr("Output Console:"));
mainLayout->addWidget(lbl);
mainLayout->addWidget(luaOutput);
closeButton = new QPushButton( tr("Close") );
closeButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
connect(closeButton, SIGNAL(clicked(void)), this, SLOT(closeWindow(void)));
hbox = new QHBoxLayout();
hbox->addStretch(5);
hbox->addWidget( closeButton, 1 );
mainLayout->addLayout( hbox );
setLayout(mainLayout);
winList.push_back(this);
periodicTimer = new QTimer(this);
connect(periodicTimer, &QTimer::timeout, this, &LuaControlDialog_t::updatePeriodic);
periodicTimer->start(200); // 5hz
restoreGeometry(settings.value("LuaWindow/geometry").toByteArray());
}
//----------------------------------------------------
LuaControlDialog_t::~LuaControlDialog_t(void)
{
QSettings settings;
std::list<LuaControlDialog_t *>::iterator it;
//printf("Destroy Lua Control Window\n");
periodicTimer->stop();
for (it = winList.begin(); it != winList.end(); it++)
{
if ((*it) == this)
{
winList.erase(it);
//printf("Removing Lua Window\n");
break;
}
}
settings.setValue("LuaWindow/geometry", saveGeometry());
}
//----------------------------------------------------
void LuaControlDialog_t::closeEvent(QCloseEvent *event)
{
//printf("Lua Control Close Window Event\n");
done(0);
deleteLater();
event->accept();
}
//----------------------------------------------------
void LuaControlDialog_t::closeWindow(void)
{
//printf("Lua Control Close Window\n");
done(0);
deleteLater();
}
//----------------------------------------------------
void LuaControlDialog_t::updatePeriodic(void)
{
//printf("Update Lua\n");
if (updateLuaDisplay)
{
updateLuaWindows();
updateLuaDisplay = false;
}
if (openLuaKillMsgBox)
{
openLuaKillMessageBox();
openLuaKillMsgBox = false;
}
}
//----------------------------------------------------
void LuaControlDialog_t::openLuaKillMessageBox(void)
{
int ret;
QMessageBox msgBox(this);
luaKillMsgBoxRetVal = 0;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText(tr("The Lua script running has been running a long time.\nIt may have gone crazy. Kill it? (I won't ask again if you say No)\n"));
msgBox.setStandardButtons(QMessageBox::Yes);
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
ret = msgBox.exec();
if (ret == QMessageBox::Yes)
{
luaKillMsgBoxRetVal = 1;
}
}
//----------------------------------------------------
void LuaControlDialog_t::openLuaScriptFile(void)
{
#ifdef _S9XLUA_H
int ret, useNativeFileDialogVal;
QString filename;
std::string last;
std::string dir;
const char *exePath = nullptr;
const char *luaPath = nullptr;
QFileDialog dialog(this, tr("Open LUA Script"));
QList<QUrl> urls;
QDir d;
exePath = fceuExecutablePath();
//urls = dialog.sidebarUrls();
urls << QUrl::fromLocalFile(QDir::rootPath());
urls << QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first());
urls << QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first());
urls << QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::DownloadLocation).first());
urls << QUrl::fromLocalFile(QDir(FCEUI_GetBaseDirectory()).absolutePath());
if (exePath[0] != 0)
{
d.setPath(QString(exePath) + "/../luaScripts");
if (d.exists())
{
urls << QUrl::fromLocalFile(d.absolutePath());
}
}
#ifndef WIN32
d.setPath("/usr/share/fceux/luaScripts");
if (d.exists())
{
urls << QUrl::fromLocalFile(d.absolutePath());
}
#endif
luaPath = getenv("LUA_PATH");
// Parse LUA_PATH and add to urls
if (luaPath)
{
int i, j;
char stmp[2048];
i = j = 0;
while (luaPath[i] != 0)
{
if (luaPath[i] == ';')
{
stmp[j] = 0;
if (j > 0)
{
d.setPath(stmp);
if (d.exists())
{
urls << QUrl::fromLocalFile(d.absolutePath());
}
}
j = 0;
}
else
{
stmp[j] = luaPath[i];
j++;
}
i++;
}
stmp[j] = 0;
if (j > 0)
{
d.setPath(stmp);
if (d.exists())
{
urls << QUrl::fromLocalFile(d.absolutePath());
}
}
}
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter(tr("LUA Scripts (*.lua *.LUA) ;; All files (*)"));
dialog.setViewMode(QFileDialog::List);
dialog.setFilter(QDir::AllEntries | QDir::AllDirs | QDir::Hidden);
dialog.setLabelText(QFileDialog::Accept, tr("Load"));
g_config->getOption("SDL.LastLoadLua", &last);
if (last.size() == 0)
{
#ifdef WIN32
last.assign(FCEUI_GetBaseDirectory());
#else
last.assign("/usr/share/fceux/luaScripts");
#endif
}
getDirFromFile(last.c_str(), dir);
dialog.setDirectory(tr(dir.c_str()));
// Check config option to use native file dialog or not
g_config->getOption("SDL.UseNativeFileDialog", &useNativeFileDialogVal);
dialog.setOption(QFileDialog::DontUseNativeDialog, !useNativeFileDialogVal);
dialog.setSidebarUrls(urls);
ret = dialog.exec();
if (ret)
{
QStringList fileList;
fileList = dialog.selectedFiles();
if (fileList.size() > 0)
{
filename = fileList[0];
}
}
if (filename.isNull())
{
return;
}
qDebug() << "selected file path : " << filename.toLocal8Bit();
g_config->setOption("SDL.LastLoadLua", filename.toLocal8Bit().constData());
scriptPath->setText(filename);
#endif
}
//----------------------------------------------------
void LuaControlDialog_t::startLuaScript(void)
{
#ifdef _S9XLUA_H
outBuf.clear();
FCEU_WRAPPER_LOCK();
if (0 == FCEU_LoadLuaCode(scriptPath->text().toLocal8Bit().constData(), scriptArgs->text().toLocal8Bit().constData()))
{
char error_msg[2048];
snprintf( error_msg, sizeof(error_msg), "Error: Could not open the selected lua script: '%s'\n", scriptPath->text().toLocal8Bit().constData());
FCEUD_PrintError(error_msg);
}
FCEU_WRAPPER_UNLOCK();
#endif
}
//----------------------------------------------------
void LuaControlDialog_t::stopLuaScript(void)
{
#ifdef _S9XLUA_H
FCEU_WRAPPER_LOCK();
FCEU_LuaStop();
FCEU_WRAPPER_UNLOCK();
#endif
}
//----------------------------------------------------
void LuaControlDialog_t::refreshState(void)
{
int i;
std::string luaOutputText;
if (luaScriptRunning)
{
stopButton->setEnabled(true);
startButton->setText(tr("Restart"));
}
else
{
stopButton->setEnabled(false);
startButton->setText(tr("Start"));
}
i = outBuf.tail;
while (i != outBuf.head)
{
luaOutputText.append(1, outBuf.buf[i]);
i = (i + 1) % outBuf.size;
}
luaOutput->setText(luaOutputText.c_str());
luaOutput->moveCursor(QTextCursor::End);
}
//----------------------------------------------------
static void updateLuaWindows(void)
{
std::list<LuaControlDialog_t *>::iterator it;
for (it = winList.begin(); it != winList.end(); it++)
{
(*it)->refreshState();
}
}
//----------------------------------------------------
void WinLuaOnStart(intptr_t hDlgAsInt)
{
luaScriptRunning = true;
//printf("Lua Script Running: %i \n", luaScriptRunning );
updateLuaDisplay = true;
}
//----------------------------------------------------
void WinLuaOnStop(intptr_t hDlgAsInt)
{
luaScriptRunning = false;
//printf("Lua Script Running: %i \n", luaScriptRunning );
updateLuaDisplay = true;
}
//----------------------------------------------------
void PrintToWindowConsole(intptr_t hDlgAsInt, const char *str)
{
//printf("%s\n", str );
outBuf.addLine(str);
updateLuaDisplay = true;
}
//----------------------------------------------------
int LuaPrintfToWindowConsole( __FCEU_PRINTF_FORMAT const char * format, ...)
{
int retval;
va_list args;
char msg[2048];
va_start(args, format);
retval = ::vsnprintf(msg, sizeof(msg), format, args);
va_end(args);
msg[sizeof(msg) - 1] = 0;
outBuf.addLine(msg);
updateLuaDisplay = true;
return (retval);
};
//----------------------------------------------------
int LuaKillMessageBox(void)
{
//printf("Kill Lua Prompted\n");
luaKillMsgBoxRetVal = 0;
openLuaKillMsgBox = true;
while (openLuaKillMsgBox)
{
msleep(100);
}
return luaKillMsgBoxRetVal;
}
//----------------------------------------------------
bool LuaArgCompat = true;
// TODO: stub. scriptArgs->text().toLocal8Bit().constData() ?
bool GetLuaArgs(char *dst, int len) { dst[0] = 0; return true; }
//----------------------------------------------------