-
Notifications
You must be signed in to change notification settings - Fork 69
/
qmlbridge.cpp
700 lines (564 loc) · 17 KB
/
qmlbridge.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#include <cassert>
#include <unistd.h>
#include <QUrl>
#include "emuthread.h"
#include "qmlbridge.h"
#ifndef MOBILE_UI
#include "mainwindow.h"
#endif
#include "core/emu.h"
#include "core/os/os.h"
#include "core/keypad.h"
#include "core/usblink_queue.h"
QMLBridge *the_qml_bridge = nullptr;
QMLBridge::QMLBridge(QObject *parent) : QObject(parent)
#ifdef IS_IOS_BUILD
/* This is needed for iOS, as the app location changes at reinstall */
, settings(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + QStringLiteral("/firebird.ini"), QSettings::IniFormat)
#endif
{
assert(the_qml_bridge == nullptr);
the_qml_bridge = this;
//Migrate old settings
if(settings.contains(QStringLiteral("usbdir")) && !settings.contains(QStringLiteral("usbdirNew")))
setUSBDir(QStringLiteral("/") + settings.value(QStringLiteral("usbdir")).toString());
bool add_default_kit = false;
// Kits need to be loaded manually
if(!settings.contains(QStringLiteral("kits")))
{
// Migrate
add_default_kit = true;
}
else
{
kit_model = settings.value(QStringLiteral("kits")).value<KitModel>();
// No kits is a bad situation to be in, as kits can only be duplicated...
if(kit_model.rowCount() == 0)
add_default_kit = true;
}
if(add_default_kit)
{
kit_model.addKit(tr("Default"),
settings.value(QStringLiteral("boot1")).toString(),
settings.value(QStringLiteral("flash")).toString(),
settings.value(QStringLiteral("snapshotPath")).toString());
}
// Same for debug_on_*
debug_on_start = getDebugOnStart();
debug_on_warn = getDebugOnWarn();
print_on_warn = getPrintOnWarn();
connect(&kit_model, SIGNAL(anythingChanged()), this, SLOT(saveKits()), Qt::QueuedConnection);
setActive(true);
}
QMLBridge::~QMLBridge()
{}
unsigned int QMLBridge::getGDBPort()
{
return settings.value(QStringLiteral("gdbPort"), 3333).toInt();
}
void QMLBridge::setGDBPort(unsigned int port)
{
if(getGDBPort() == port)
return;
settings.setValue(QStringLiteral("gdbPort"), port);
emit gdbPortChanged();
}
bool QMLBridge::getGDBEnabled()
{
return settings.value(QStringLiteral("gdbEnabled"), !isMobile()).toBool();
}
void QMLBridge::setGDBEnabled(bool e)
{
if(getGDBEnabled() == e)
return;
settings.setValue(QStringLiteral("gdbEnabled"), e);
emit gdbEnabledChanged();
}
unsigned int QMLBridge::getRDBPort()
{
return settings.value(QStringLiteral("rdbgPort"), 3334).toInt();
}
void QMLBridge::setRDBPort(unsigned int port)
{
if(getRDBPort() == port)
return;
settings.setValue(QStringLiteral("rdbgPort"), port);
emit rdbPortChanged();
}
bool QMLBridge::getRDBEnabled()
{
return settings.value(QStringLiteral("rdbgEnabled"), !isMobile()).toBool();
}
void QMLBridge::setRDBEnabled(bool e)
{
if(getRDBEnabled() == e)
return;
settings.setValue(QStringLiteral("rdbgEnabled"), e);
emit rdbEnabledChanged();
}
bool QMLBridge::getDebugOnWarn()
{
return settings.value(QStringLiteral("debugOnWarn"), !isMobile()).toBool();
}
void QMLBridge::setDebugOnWarn(bool e)
{
if(getDebugOnWarn() == e)
return;
debug_on_warn = e;
settings.setValue(QStringLiteral("debugOnWarn"), e);
emit debugOnWarnChanged();
}
bool QMLBridge::getDebugOnStart()
{
return settings.value(QStringLiteral("debugOnStart"), false).toBool();
}
void QMLBridge::setDebugOnStart(bool e)
{
if(getDebugOnStart() == e)
return;
debug_on_start = e;
settings.setValue(QStringLiteral("debugOnStart"), e);
emit debugOnStartChanged();
}
void QMLBridge::setPrintOnWarn(bool p)
{
if (getPrintOnWarn() == p)
return;
print_on_warn = p;
settings.setValue(QStringLiteral("printOnWarn"), p);
emit printOnWarnChanged();
}
bool QMLBridge::getPrintOnWarn()
{
return settings.value(QStringLiteral("printOnWarn"), true).toBool();
}
bool QMLBridge::getAutostart()
{
return settings.value(QStringLiteral("emuAutostart"), true).toBool();
}
void QMLBridge::setAutostart(bool e)
{
if(getAutostart() == e)
return;
settings.setValue(QStringLiteral("emuAutostart"), e);
emit autostartChanged();
}
unsigned int QMLBridge::getDefaultKit()
{
return settings.value(QStringLiteral("defaultKit"), 0).toUInt();
}
void QMLBridge::setDefaultKit(unsigned int id)
{
if(getDefaultKit() == id)
return;
settings.setValue(QStringLiteral("defaultKit"), id);
emit defaultKitChanged();
}
bool QMLBridge::getLeftHanded()
{
return settings.value(QStringLiteral("leftHanded"), false).toBool();
}
void QMLBridge::setLeftHanded(bool e)
{
if(getLeftHanded() == e)
return;
settings.setValue(QStringLiteral("leftHanded"), e);
emit leftHandedChanged();
}
bool QMLBridge::getSuspendOnClose()
{
return settings.value(QStringLiteral("suspendOnClose"), true).toBool();
}
void QMLBridge::setSuspendOnClose(bool e)
{
if(getSuspendOnClose() == e)
return;
settings.setValue(QStringLiteral("suspendOnClose"), e);
emit suspendOnCloseChanged();
}
QString QMLBridge::getUSBDir()
{
return settings.value(QStringLiteral("usbdirNew"), QStringLiteral("/ndless")).toString();
}
void QMLBridge::setUSBDir(QString dir)
{
if(getUSBDir() == dir)
return;
settings.setValue(QStringLiteral("usbdirNew"), dir);
emit usbDirChanged();
}
bool QMLBridge::getIsRunning()
{
return emu_thread.isRunning();
}
QString QMLBridge::getVersion()
{
#define STRINGIFYMAGIC(x) #x
#define STRINGIFY(x) STRINGIFYMAGIC(x)
return QStringLiteral(STRINGIFY(FB_VERSION));
}
void QMLBridge::setButtonState(int id, bool state)
{
int col = id % KEYPAD_COLS, row = id / KEYPAD_COLS;
::keypad_set_key(row, col, state);
}
void QMLBridge::setTouchpadState(qreal x, qreal y, bool contact, bool down)
{
::touchpad_set_state(x, y, contact, down);
touchpadStateChanged();
}
bool QMLBridge::isMobile()
{
// TODO: Mobile UI on desktop? Q_OS_ANDROID doesn't work somehow.
#ifdef MOBILE_UI
return true;
#else
return false;
#endif
}
void QMLBridge::sendFile(QUrl url, QString dir)
{
auto local = toLocalFile(url);
auto remote = dir + QLatin1Char('/') + basename(local);
usblink_queue_put_file(local.toStdString(), remote.toStdString(), QMLBridge::usblink_progress_changed, this);
}
void QMLBridge::sendExitPTT()
{
usblink_queue_new_dir("/Press-to-Test", nullptr, nullptr);
usblink_queue_put_file(std::string(), "/Press-to-Test/Exit Test Mode.tns", QMLBridge::usblink_progress_changed, this);
}
QString QMLBridge::basename(QString path)
{
if(path.isEmpty())
return tr("None");
#ifdef Q_OS_ANDROID
QScopedPointer<char, QScopedPointerPodDeleter> android_bn{android_basename(path.toUtf8().data())};
if(android_bn)
return QString::fromUtf8(android_bn.data());
#endif
return QFileInfo(path).fileName();
}
QUrl QMLBridge::dir(QString path)
{
return QUrl{QUrl::fromLocalFile(path).toString(QUrl::RemoveFilename)};
}
QString QMLBridge::toLocalFile(QUrl url)
{
// Pass through Android content url, see fopen_utf8
if(url.scheme() == QStringLiteral("content"))
return url.toString(QUrl::FullyEncoded);
return url.toLocalFile();
}
bool QMLBridge::fileExists(QString path)
{
return QFile::exists(path);
}
int QMLBridge::kitIndexForID(unsigned int id)
{
return kit_model.indexForID(id);
}
#ifndef MOBILE_UI
void QMLBridge::switchUIMode(bool mobile_ui)
{
main_window->switchUIMode(mobile_ui);
}
#endif
bool QMLBridge::createFlash(QString path, int productID, int featureValues, QString manuf, QString boot2, QString os, QString diags)
{
bool is_cx = productID >= 0x0F0;
std::string preload_str[4] = { manuf.toStdString(), boot2.toStdString(), diags.toStdString(), os.toStdString() };
const char *preload[4] = { nullptr, nullptr, nullptr, nullptr };
for(unsigned int i = 0; i < 4; ++i)
if(preload_str[i] != "")
preload[i] = preload_str[i].c_str();
uint8_t *nand_data = nullptr;
size_t nand_size;
if(!flash_create_new(is_cx, preload, productID, featureValues, is_cx, &nand_data, &nand_size))
{
free(nand_data);
return false;
}
QFile flash_file(path);
if(!flash_file.open(QFile::WriteOnly) || !flash_file.write(reinterpret_cast<char*>(nand_data), nand_size))
{
free(nand_data);
return false;
}
free(nand_data);
flash_file.close();
return true;
}
QString QMLBridge::componentDescription(QString path, QString expected_type)
{
FILE *file = fopen_utf8(path.toUtf8().data(), "rb");
if(!file)
return tr("Open failed");
std::string type, version;
bool b = flash_component_info(file, type, version);
fclose(file);
if(!b)
return QStringLiteral("???");
if(type != expected_type.toStdString())
return tr("Found %1 instead").arg(QString::fromStdString(type).trimmed());
return QString::fromStdString(version);
}
QString QMLBridge::manufDescription(QString path)
{
FILE *file = fopen_utf8(path.toUtf8().data(), "rb");
if(!file)
return tr("Open failed");
auto raw_type = flash_read_type(file, true);
fclose(file);
// Reading or parsing failed
if(raw_type == "" || raw_type == "???")
return QStringLiteral("???");
return QString::fromStdString(raw_type);
}
QString QMLBridge::osDescription(QString path)
{
FILE *file = fopen_utf8(path.toUtf8().data(), "rb");
if(!file)
return tr("Open failed");
std::string version;
bool b = flash_os_info(file, version);
fclose(file);
if(!b)
return QStringLiteral("???");
return QString::fromStdString(version);
}
void QMLBridge::setActive(bool b)
{
if(is_active == b)
return;
if(b)
{
connect(&emu_thread, SIGNAL(speedChanged(double)), this, SLOT(speedChanged(double)), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(turboModeChanged(bool)), this, SIGNAL(turboModeChanged()), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(stopped()), this, SIGNAL(isRunningChanged()), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(started(bool)), this, SIGNAL(isRunningChanged()), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(suspended(bool)), this, SIGNAL(isRunningChanged()), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(resumed(bool)), this, SIGNAL(isRunningChanged()), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(started(bool)), this, SLOT(started(bool)), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(resumed(bool)), this, SLOT(resumed(bool)), Qt::QueuedConnection);
connect(&emu_thread, SIGNAL(suspended(bool)), this, SLOT(suspended(bool)), Qt::QueuedConnection);
// We might have missed some events.
turboModeChanged();
speedChanged();
isRunningChanged();
}
else
{
disconnect(&emu_thread, SIGNAL(speedChanged(double)), this, SLOT(speedChanged(double)));
disconnect(&emu_thread, SIGNAL(turboModeChanged(bool)), this, SIGNAL(turboModeChanged()));
disconnect(&emu_thread, SIGNAL(stopped()), this, SIGNAL(isRunningChanged()));
disconnect(&emu_thread, SIGNAL(started(bool)), this, SIGNAL(isRunningChanged()));
disconnect(&emu_thread, SIGNAL(suspended(bool)), this, SIGNAL(isRunningChanged()));
disconnect(&emu_thread, SIGNAL(resumed(bool)), this, SIGNAL(isRunningChanged()));
disconnect(&emu_thread, SIGNAL(started(bool)), this, SLOT(started(bool)));
disconnect(&emu_thread, SIGNAL(resumed(bool)), this, SLOT(resumed(bool)));
disconnect(&emu_thread, SIGNAL(suspended(bool)), this, SLOT(suspended(bool)));
}
is_active = b;
}
void QMLBridge::saveKits()
{
settings.setValue(QStringLiteral("kits"), QVariant::fromValue(kit_model));
}
void QMLBridge::usblink_progress_changed(int percent, void *qml_bridge_p)
{
auto &&qml_bridge = reinterpret_cast<QMLBridge*>(qml_bridge_p);
emit qml_bridge->usblinkProgressChanged(percent);
}
void QMLBridge::setTurboMode(bool b)
{
emu_thread.setTurboMode(b);
}
int QMLBridge::getMobileX()
{
return settings.value(QStringLiteral("mobileX"), -1).toInt();
}
void QMLBridge::setMobileX(int x)
{
settings.setValue(QStringLiteral("mobileX"), x);
}
int QMLBridge::getMobileY()
{
return settings.value(QStringLiteral("mobileY"), -1).toInt();
}
void QMLBridge::setMobileY(int y)
{
settings.setValue(QStringLiteral("mobileY"), y);
}
int QMLBridge::getMobileWidth()
{
return settings.value(QStringLiteral("mobileWidth"), -1).toInt();
}
void QMLBridge::setMobileWidth(int w)
{
settings.setValue(QStringLiteral("mobileWidth"), w);
}
int QMLBridge::getMobileHeight()
{
return settings.value(QStringLiteral("mobileHeight"), -1).toInt();
}
void QMLBridge::setMobileHeight(int h)
{
settings.setValue(QStringLiteral("mobileHeight"), h);
}
bool QMLBridge::restart()
{
if(emu_thread.isRunning() && !emu_thread.stop())
{
toastMessage(tr("Could not stop emulation"));
return false;
}
emu_thread.port_gdb = getGDBEnabled() ? getGDBPort() : 0;
emu_thread.port_rdbg = getRDBEnabled() ? getRDBPort() : 0;
if(!emu_thread.boot1.isEmpty() && !emu_thread.flash.isEmpty()) {
toastMessage(tr("Starting emulation"));
emu_thread.start();
return true;
} else {
toastMessage(tr("No boot1 or flash selected.\nSwipe keypad left for configuration."));
return false;
}
}
void QMLBridge::setPaused(bool b)
{
emu_thread.setPaused(b);
}
void QMLBridge::reset()
{
emu_thread.reset();
}
void QMLBridge::suspend()
{
toastMessage(tr("Suspending emulation"));
auto snapshot_path = getSnapshotPath();
if(!snapshot_path.isEmpty())
emu_thread.suspend(snapshot_path);
else {
toastMessage(tr("The current kit does not have a snapshot file configured"));
emit emuSuspended(false);
}
}
void QMLBridge::resume()
{
toastMessage(tr("Resuming emulation"));
emu_thread.port_gdb = getGDBEnabled() ? getGDBPort() : 0;
emu_thread.port_rdbg = getRDBEnabled() ? getRDBPort() : 0;
auto snapshot_path = getSnapshotPath();
if(!snapshot_path.isEmpty())
emu_thread.resume(snapshot_path);
else
toastMessage(tr("The current kit does not have a snapshot file configured"));
}
bool QMLBridge::useDefaultKit()
{
if(setCurrentKit(getDefaultKit()))
return true;
setCurrentKit(kit_model.getKits()[0].id); // Use first kit as fallback
return false;
}
bool QMLBridge::setCurrentKit(unsigned int id)
{
const Kit *kit = useKit(id);
if(!kit)
return false;
current_kit_id = id;
emit currentKitChanged(*kit);
return true;
}
int QMLBridge::getCurrentKitId()
{
return current_kit_id;
}
const Kit *QMLBridge::useKit(unsigned int id)
{
int kitIndex = kitIndexForID(id);
if(kitIndex < 0)
return nullptr;
auto &&kit = kit_model.getKits()[kitIndex];
emu_thread.boot1 = kit.boot1;
emu_thread.flash = kit.flash;
fallback_snapshot_path = kit.snapshot;
return &kit;
}
bool QMLBridge::stop()
{
return emu_thread.stop();
}
bool QMLBridge::saveFlash()
{
return flash_save_changes();
}
QString QMLBridge::getBoot1Path()
{
return emu_thread.boot1;
}
QString QMLBridge::getFlashPath()
{
return emu_thread.flash;
}
QString QMLBridge::getSnapshotPath()
{
int kitIndex = kitIndexForID(current_kit_id);
if(kitIndex >= 0)
return kit_model.getKits()[kitIndex].snapshot;
else
return fallback_snapshot_path;
}
bool QMLBridge::saveDialogSupported()
{
#ifdef Q_OS_ANDROID
// Starting with Qt 5.13, the Android "File Picker" is used, but that doesn't allow creation of files.
return QVersionNumber::fromString(QString::fromUtf8(qVersion())) < QVersionNumber(5, 13);
#else
return true;
#endif
}
void QMLBridge::speedChanged(double speed)
{
this->speed = speed;
emit speedChanged();
}
void QMLBridge::started(bool success)
{
if(success)
toastMessage(tr("Emulation started"));
else
toastMessage(tr("Couldn't start emulation"));
}
void QMLBridge::resumed(bool success)
{
if(success)
toastMessage(tr("Emulation resumed"));
else
toastMessage(tr("Could not resume"));
}
void QMLBridge::suspended(bool success)
{
if(success)
toastMessage(tr("Flash and snapshot saved")); // When clicking on save, flash is saved as well
else
toastMessage(tr("Couldn't save snapshot"));
emit emuSuspended(success);
}
double QMLBridge::getSpeed()
{
return speed;
}
bool QMLBridge::getTurboMode()
{
return turbo_mode;
}
void QMLBridge::notifyButtonStateChanged(int row, int col, bool state)
{
assert(row < KEYPAD_ROWS);
assert(col < KEYPAD_COLS);
emit buttonStateChanged(col + row * KEYPAD_COLS, state);
}
void QMLBridge::touchpadStateChanged()
{
touchpadStateChanged(float(keypad.touchpad_x)/TOUCHPAD_X_MAX, 1.0f-(float(keypad.touchpad_y)/TOUCHPAD_Y_MAX), keypad.touchpad_contact, keypad.touchpad_down);
}