-
Notifications
You must be signed in to change notification settings - Fork 14
/
mainwindow.cpp
1199 lines (1049 loc) · 37.3 KB
/
mainwindow.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <QString>
#include <QFileDialog>
#include <QCloseEvent>
#include <QActionGroup>
#include <QDesktopServices>
#include "settingsdialog.h"
#include "plugindialog.h"
#include "hotkeydialog.h"
#include "cheats.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "interface/common.h"
#include "vidext.h"
#include "netplay/createroom.h"
#include "netplay/joinroom.h"
#include "osal/osal_preproc.h"
#include "interface/core_commands.h"
#include "interface/version.h"
#include "m64p_common.h"
#include "version.h"
#define RECENT_SIZE 10
#define SETTINGS_VER 2
m64p_video_extension_functions vidExtFunctions = {17,
qtVidExtFuncInit,
qtVidExtFuncQuit,
qtVidExtFuncListModes,
qtVidExtFuncListRates,
qtVidExtFuncSetMode,
qtVidExtFuncSetModeWithRate,
qtVidExtFuncGLGetProc,
qtVidExtFuncGLSetAttr,
qtVidExtFuncGLGetAttr,
qtVidExtFuncGLSwapBuf,
qtVidExtFuncSetCaption,
qtVidExtFuncToggleFS,
qtVidExtFuncResizeWindow,
qtVidExtFuncGLGetDefaultFramebuffer,
qtVidExtFuncInitWithRenderMode,
qtVidExtFuncGetVkSurface,
qtVidExtFuncGetVkInstExtensions};
void MainWindow::updatePlugins()
{
#ifdef PLUGIN_DIR_PATH
QString pluginPath = PLUGIN_DIR_PATH;
#else
QString pluginPath = QCoreApplication::applicationDirPath();
#endif
QDir PluginDir(pluginPath);
PluginDir.setFilter(QDir::Files);
QStringList Filter;
Filter.append("");
QStringList current;
QString default_value;
if (!settings->contains("inputPlugin"))
{
Filter.replace(0, "*-input-*");
current = PluginDir.entryList(Filter);
default_value = "simple64-input-qt";
default_value += OSAL_DLL_EXTENSION;
if (current.isEmpty())
settings->setValue("inputPlugin", "dummy");
else if (current.indexOf(default_value) != -1)
settings->setValue("inputPlugin", default_value);
else
settings->setValue("inputPlugin", current.at(0));
}
ui->actionController_Configuration->setEnabled(settings->value("inputPlugin").toString().contains("-qt"));
}
void MainWindow::updateGB(Ui::MainWindow *ui)
{
QMenu *GB = new QMenu(this);
GB->setTitle("Game Boy Cartridges");
ui->menuFile->insertMenu(ui->actionTake_Screenshot, GB);
QAction *fileSelect = new QAction(this);
QString current = settings->value("Player1GBROM").toString();
fileSelect->setText("Player 1 ROM: " + current);
GB->addAction(fileSelect);
connect(fileSelect, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB ROM File"), NULL, tr("GB ROM Files (*.gb *.gbc)"));
if (!filename.isNull()) {
settings->setValue("Player1GBROM", filename);
QString current = filename;
fileSelect->setText("Player 1 ROM: " + current);
} });
QAction *fileSelect2 = new QAction(this);
current = settings->value("Player1GBRAM").toString();
fileSelect2->setText("Player 1 SAV: " + current);
GB->addAction(fileSelect2);
connect(fileSelect2, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB RAM File"), NULL, tr("GB SAV Files (*.sav *.ram)"));
if (!filename.isNull()) {
settings->setValue("Player1GBRAM", filename);
QString current = filename;
fileSelect2->setText("Player 1 SAV: " + current);
} });
QAction *clearSelect = new QAction(this);
clearSelect->setText("Clear Player 1 Selections");
GB->addAction(clearSelect);
connect(clearSelect, &QAction::triggered, [=]()
{
settings->remove("Player1GBROM");
settings->remove("Player1GBRAM");
fileSelect->setText("Player 1 ROM: ");
fileSelect2->setText("Player 1 SAV: "); });
GB->addSeparator();
fileSelect = new QAction(this);
current = settings->value("Player2GBROM").toString();
fileSelect->setText("Player 2 ROM: " + current);
GB->addAction(fileSelect);
connect(fileSelect, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB ROM File"), NULL, tr("GB ROM Files (*.gb *.gbc)"));
if (!filename.isNull()) {
settings->setValue("Player2GBROM", filename);
QString current = filename;
fileSelect->setText("Player 2 ROM: " + current);
} });
fileSelect2 = new QAction(this);
current = settings->value("Player2GBRAM").toString();
fileSelect2->setText("Player 2 SAV: " + current);
GB->addAction(fileSelect2);
connect(fileSelect2, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB RAM File"), NULL, tr("GB SAV Files (*.sav *.ram)"));
if (!filename.isNull()) {
settings->setValue("Player2GBRAM", filename);
QString current = filename;
fileSelect2->setText("Player 2 SAV: " + current);
} });
clearSelect = new QAction(this);
clearSelect->setText("Clear Player 2 Selections");
GB->addAction(clearSelect);
connect(clearSelect, &QAction::triggered, [=]()
{
settings->remove("Player2GBROM");
settings->remove("Player2GBRAM");
fileSelect->setText("Player 2 ROM: ");
fileSelect2->setText("Player 2 SAV: "); });
GB->addSeparator();
fileSelect = new QAction(this);
current = settings->value("Player3GBROM").toString();
fileSelect->setText("Player 3 ROM: " + current);
GB->addAction(fileSelect);
connect(fileSelect, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB ROM File"), NULL, tr("GB ROM Files (*.gb *.gbc)"));
if (!filename.isNull()) {
settings->setValue("Player3GBROM", filename);
QString current = filename;
fileSelect->setText("Player 3 ROM: " + current);
} });
fileSelect2 = new QAction(this);
current = settings->value("Player3GBRAM").toString();
fileSelect2->setText("Player 3 SAV: " + current);
GB->addAction(fileSelect2);
connect(fileSelect2, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB RAM File"), NULL, tr("GB SAV Files (*.sav *.ram)"));
if (!filename.isNull()) {
settings->setValue("Player3GBRAM", filename);
QString current = filename;
fileSelect2->setText("Player 3 SAV: " + current);
} });
clearSelect = new QAction(this);
clearSelect->setText("Clear Player 3 Selections");
GB->addAction(clearSelect);
connect(clearSelect, &QAction::triggered, [=]()
{
settings->remove("Player3GBROM");
settings->remove("Player3GBRAM");
fileSelect->setText("Player 3 ROM: ");
fileSelect2->setText("Player 3 SAV: "); });
GB->addSeparator();
fileSelect = new QAction(this);
current = settings->value("Player4GBROM").toString();
fileSelect->setText("Player 4 ROM: " + current);
GB->addAction(fileSelect);
connect(fileSelect, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB ROM File"), NULL, tr("GB ROM Files (*.gb *.gbc)"));
if (!filename.isNull()) {
settings->setValue("Player4GBROM", filename);
QString current = filename;
fileSelect->setText("Player 4 ROM: " + current);
} });
fileSelect2 = new QAction(this);
current = settings->value("Player4GBRAM").toString();
fileSelect2->setText("Player 4 SAV: " + current);
GB->addAction(fileSelect2);
connect(fileSelect2, &QAction::triggered, [=]()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("GB RAM File"), NULL, tr("GB SAV Files (*.sav *.ram)"));
if (!filename.isNull()) {
settings->setValue("Player4GBRAM", filename);
QString current = filename;
fileSelect2->setText("Player 4 SAV: " + current);
} });
clearSelect = new QAction(this);
clearSelect->setText("Clear Player 4 Selections");
GB->addAction(clearSelect);
connect(clearSelect, &QAction::triggered, [=]()
{
settings->remove("Player4GBROM");
settings->remove("Player4GBRAM");
fileSelect->setText("Player 4 ROM: ");
fileSelect2->setText("Player 4 SAV: "); });
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
verbose = 0;
nogui = 0;
run_test = 0;
test_key_value = 225; // Shift/A
ui->setupUi(this);
this->setWindowTitle("simple64 build: " + QStringLiteral(GUI_VERSION).mid(0, 7));
QString ini_path = QDir(QCoreApplication::applicationDirPath()).filePath("simple64-gui.ini");
settings = new QSettings(ini_path, QSettings::IniFormat, this);
if (!settings->isWritable())
settings = new QSettings("simple64", "gui", this);
if (!settings->contains("version") || settings->value("version").toInt() != SETTINGS_VER)
{
settings->clear();
settings->setValue("version", SETTINGS_VER);
}
restoreGeometry(settings->value("geometry").toByteArray());
restoreState(settings->value("windowState").toByteArray());
QActionGroup *my_slots_group = new QActionGroup(this);
QAction *my_slots[10];
OpenRecent = new QMenu(this);
QMenu *SaveSlot = new QMenu(this);
OpenRecent->setTitle("Open Recent");
SaveSlot->setTitle("Change Save Slot");
ui->menuFile->insertMenu(ui->actionSave_State, OpenRecent);
ui->menuFile->insertSeparator(ui->actionSave_State);
ui->menuFile->insertMenu(ui->actionSave_State_To, SaveSlot);
ui->menuFile->insertSeparator(ui->actionSave_State_To);
for (int i = 0; i < 10; ++i)
{
my_slots[i] = new QAction(this);
my_slots[i]->setCheckable(true);
my_slots[i]->setText("Slot " + QString::number(i));
my_slots[i]->setActionGroup(my_slots_group);
SaveSlot->addAction(my_slots[i]);
QAction *temp_slot = my_slots[i];
connect(temp_slot, &QAction::triggered, [=](bool checked)
{
if (checked) {
(*CoreDoCommand)(M64CMD_STATE_SET_SLOT, i, NULL);
} });
}
updateOpenRecent();
updateGB(ui);
// migrate from old $CONFIG_PATH$ default
if (settings->value("configDirPath").toString().startsWith("$"))
settings->remove("configDirPath");
#ifdef CONFIG_DIR_PATH
settings->setValue("configDirPath", CONFIG_DIR_PATH);
#endif
updatePlugins();
if (!settings->contains("volume"))
settings->setValue("volume", 100);
VolumeAction *volumeAction = new VolumeAction(tr("Volume"), this);
connect(volumeAction->slider(), &QSlider::valueChanged, this, &MainWindow::volumeValueChanged);
volumeAction->slider()->setValue(settings->value("volume").toInt());
ui->menuEmulation->insertAction(ui->actionMute, volumeAction);
coreLib = nullptr;
gfxPlugin = nullptr;
rspPlugin = nullptr;
audioPlugin = nullptr;
inputPlugin = nullptr;
loadCoreLib();
loadPlugins();
if (coreLib)
{
m64p_handle coreConfigHandle;
m64p_error res = (*ConfigOpenSection)("Core", &coreConfigHandle);
if (res == M64ERR_SUCCESS)
{
int current_slot = (*ConfigGetParamInt)(coreConfigHandle, "CurrentStateSlot");
my_slots[current_slot]->setChecked(true);
}
}
setupDiscord();
FPSLabel = new QLabel(this);
statusBar()->addPermanentWidget(FPSLabel);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateApp()
{
#ifdef _AUTOUPDATE
QString disable_update = qEnvironmentVariable("SIMPLE64_DISABLE_UPDATER");
if (!disable_update.isEmpty())
return;
#ifndef __APPLE__
QNetworkAccessManager *updateManager = new QNetworkAccessManager(this);
connect(updateManager, &QNetworkAccessManager::finished,
this, &MainWindow::updateReplyFinished);
updateManager->get(QNetworkRequest(QUrl("https://api.github.com/repos/simple64/simple64/releases/latest")));
#endif
#endif
}
void MainWindow::setupDiscord()
{
QLibrary *discordLib = new QLibrary((QDir(QCoreApplication::applicationDirPath()).filePath("discord_game_sdk")), this);
memset(&discord_app, 0, sizeof(discord_app));
DiscordCreateParams params;
DiscordCreateParamsSetDefault(¶ms);
params.client_id = 770838334015930398LL;
params.flags = DiscordCreateFlags_NoRequireDiscord;
params.event_data = &discord_app;
typedef EDiscordResult (*CreatePrototype)(DiscordVersion version, struct DiscordCreateParams *params, struct IDiscordCore **result);
CreatePrototype createFunction = (CreatePrototype)discordLib->resolve("DiscordCreate");
if (createFunction)
createFunction(DISCORD_VERSION, ¶ms, &discord_app.core);
if (discord_app.core)
{
discord_app.activities = discord_app.core->get_activity_manager(discord_app.core);
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::discordCallback);
timer->start(1000);
}
}
struct Discord_Application *MainWindow::getDiscordApp()
{
return &discord_app;
}
void MainWindow::updateDiscordActivity(struct DiscordActivity activity)
{
if (discord_app.activities)
discord_app.activities->update_activity(discord_app.activities, &activity, &discord_app, nullptr);
}
void MainWindow::clearDiscordActivity()
{
if (discord_app.activities)
discord_app.activities->clear_activity(discord_app.activities, &discord_app, nullptr);
}
void MainWindow::discordCallback()
{
if (discord_app.core)
discord_app.core->run_callbacks(discord_app.core);
}
void MainWindow::updateDownloadFinished(QNetworkReply *reply)
{
if (!reply->error())
{
QTemporaryDir dir;
dir.setAutoRemove(false);
if (dir.isValid())
{
download_message->done(QDialog::Accepted);
QString fullpath = dir.filePath(reply->url().fileName());
QFile file(fullpath);
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
file.close();
QProcess process;
process.setProgram(fullpath);
QStringList arguments = {QCoreApplication::applicationDirPath()};
process.setArguments(arguments);
process.startDetached();
reply->deleteLater();
QCoreApplication::quit();
}
}
reply->deleteLater();
}
void MainWindow::updateReplyFinished(QNetworkReply *reply)
{
if (!reply->error())
{
QJsonDocument json_doc = QJsonDocument::fromJson(reply->readAll());
QJsonObject json = json_doc.object();
if (json.value("target_commitish").toString() != QString(GUI_VERSION))
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Update Available", "A newer version is available, update?", QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes)
{
QNetworkAccessManager *updateManager = new QNetworkAccessManager(this);
connect(updateManager, &QNetworkAccessManager::finished,
this, &MainWindow::updateDownloadFinished);
#ifdef _WIN32
QNetworkRequest req(QUrl("https://github.com/simple64/simple64-updater/releases/latest/download/simple64-updater.exe"));
#else
QNetworkRequest req(QUrl("https://github.com/simple64/simple64-updater/releases/latest/download/simple64-updater"));
#endif
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
updateManager->get(req);
download_message = new QMessageBox(this);
download_message->setStandardButtons(QMessageBox::NoButton);
download_message->setText("Downloading updater");
download_message->show();
}
}
}
reply->deleteLater();
}
void MainWindow::volumeValueChanged(int value)
{
if (value != settings->value("volume").toInt())
{
settings->setValue("volume", value);
settings->sync();
(*CoreDoCommand)(M64CMD_CORE_STATE_SET, M64CORE_AUDIO_VOLUME, &value);
}
}
void MainWindow::setVerbose()
{
verbose = 1;
}
void MainWindow::setNoGUI()
{
nogui = 1;
if (!menuBar()->isNativeMenuBar())
menuBar()->hide();
statusBar()->hide();
}
int MainWindow::getNoGUI()
{
return nogui;
}
void MainWindow::setTest(int value)
{
run_test = value;
}
int MainWindow::getTest()
{
return run_test;
}
int MainWindow::getVerbose()
{
return verbose;
}
void MainWindow::resizeMainWindow(int Width, int Height)
{
QSize size = this->size();
Height += (menuBar()->isNativeMenuBar() ? 0 : ui->menuBar->height()) + ui->statusBar->height();
if (size.width() != Width || size.height() != Height)
resize(Width, Height);
}
void MainWindow::toggleFS(int force)
{
if (coreLib == nullptr)
return;
int response = M64VIDEO_NONE;
if (force == M64VIDEO_NONE)
(*CoreDoCommand)(M64CMD_CORE_STATE_QUERY, M64CORE_VIDEO_MODE, &response);
if (response == M64VIDEO_WINDOWED || force == M64VIDEO_FULLSCREEN)
{
if (!menuBar()->isNativeMenuBar())
menuBar()->hide();
statusBar()->hide();
showFullScreen();
}
else if (response == M64VIDEO_FULLSCREEN || force == M64VIDEO_WINDOWED)
{
if (!nogui)
{
if (!menuBar()->isNativeMenuBar())
menuBar()->show();
statusBar()->show();
}
showNormal();
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
stopGame();
closePlugins();
closeCoreLib();
if (discord_app.activities)
discord_app.activities->clear_activity(discord_app.activities, &discord_app, nullptr);
if (discord_app.core)
{
discord_app.core->run_callbacks(discord_app.core);
discord_app.core->destroy(discord_app.core);
}
settings->setValue("geometry", saveGeometry());
settings->setValue("windowState", saveState());
event->accept();
}
void MainWindow::updateOpenRecent()
{
OpenRecent->clear();
QAction *recent[RECENT_SIZE];
QStringList list = settings->value("RecentROMs2").toStringList();
if (list.isEmpty())
{
OpenRecent->setEnabled(false);
return;
}
OpenRecent->setEnabled(true);
for (int i = 0; i < list.size() && i < RECENT_SIZE; ++i)
{
recent[i] = new QAction(this);
recent[i]->setText(list.at(i));
OpenRecent->addAction(recent[i]);
QAction *temp_recent = recent[i];
connect(temp_recent, &QAction::triggered, [=]()
{ openROM(list.at(i), "", 0, 0, QJsonObject()); });
}
OpenRecent->addSeparator();
QAction *clearRecent = new QAction(this);
clearRecent->setText("Clear List");
OpenRecent->addAction(clearRecent);
connect(clearRecent, &QAction::triggered, [=]()
{
settings->remove("RecentROMs2");
updateOpenRecent(); });
}
void MainWindow::showMessage(QString message)
{
QMessageBox *msgBox = new QMessageBox(this);
msgBox->setText(message);
msgBox->show();
}
void MainWindow::createVkWindow(QVulkanInstance *vulkan_inst)
{
my_window = new VkWindow;
my_window->setVulkanInstance(vulkan_inst);
QWidget *container = QWidget::createWindowContainer(my_window, this);
container->setFocusPolicy(Qt::StrongFocus);
my_window->setCursor(Qt::BlankCursor);
setCentralWidget(container);
my_window->installEventFilter(&keyPressFilter);
this->installEventFilter(&keyPressFilter);
frame_timer = new QTimer(this);
connect(frame_timer, &QTimer::timeout, this, &MainWindow::updateFrameCount);
frame_timer->start(1000);
frame_count = 0;
}
void MainWindow::deleteVkWindow()
{
QWidget *container = new QWidget(this);
setCentralWidget(container);
delete my_window;
frame_timer->stop();
frame_timer->deleteLater();
FPSLabel->clear();
}
void MainWindow::killThread()
{
printf("Application hung, exiting\n");
exit(1); // Force kill the application, it's stuck
}
void MainWindow::stopGame()
{
if (!coreLib)
return;
int response;
(*CoreDoCommand)(M64CMD_CORE_STATE_QUERY, M64CORE_EMU_STATE, &response);
if (response != M64EMU_STOPPED)
{
(*CoreDoCommand)(M64CMD_STOP, 0, NULL);
kill_timer = new QTimer(this);
kill_timer->setInterval(5000);
kill_timer->setSingleShot(true);
connect(kill_timer, &QTimer::timeout, this, &MainWindow::killThread);
kill_timer->start();
while (workerThread->isRunning())
QCoreApplication::processEvents();
kill_timer->stop();
kill_timer->deleteLater();
}
}
void MainWindow::resetCore()
{
closePlugins();
closeCoreLib();
loadCoreLib();
loadPlugins();
}
void MainWindow::openROM(QString filename, QString netplay_ip, int netplay_port, int netplay_player, QJsonObject cheats)
{
stopGame();
logViewer.clearLog();
resetCore();
workerThread = new WorkerThread(netplay_ip, netplay_port, netplay_player, cheats, this);
workerThread->setFileName(filename);
QStringList list;
if (settings->contains("RecentROMs2"))
list = settings->value("RecentROMs2").toStringList();
list.removeAll(filename);
list.prepend(filename);
if (list.size() > RECENT_SIZE)
list.removeLast();
settings->setValue("RecentROMs2", list);
updateOpenRecent();
workerThread->start();
}
void MainWindow::on_actionOpen_ROM_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open ROM"), settings->value("ROMdir").toString(), tr("ROM Files (*.n64 *.N64 *.z64 *.Z64 *.v64 *.V64 *.rom *.ROM *.zip *.ZIP *.7z)"));
if (!filename.isNull())
{
QFileInfo info(filename);
settings->setValue("ROMdir", info.absoluteDir().absolutePath());
openROM(filename, "", 0, 0, QJsonObject());
}
}
void MainWindow::on_actionPlugin_Paths_triggered()
{
SettingsDialog *settings = new SettingsDialog(this);
settings->show();
}
void MainWindow::on_actionStop_Game_triggered()
{
stopGame();
}
void MainWindow::on_actionExit_triggered()
{
this->close();
}
void MainWindow::on_actionPlugin_Settings_triggered()
{
PluginDialog *settings = new PluginDialog(this);
settings->show();
}
void MainWindow::on_actionPause_Game_triggered()
{
int response;
(*CoreDoCommand)(M64CMD_CORE_STATE_QUERY, M64CORE_EMU_STATE, &response);
if (response == M64EMU_RUNNING)
(*CoreDoCommand)(M64CMD_PAUSE, 0, NULL);
else if (response == M64EMU_PAUSED)
(*CoreDoCommand)(M64CMD_RESUME, 0, NULL);
}
void MainWindow::on_actionMute_triggered()
{
int response;
(*CoreDoCommand)(M64CMD_CORE_STATE_QUERY, M64CORE_AUDIO_MUTE, &response);
if (response == 0)
{
response = 1;
(*CoreDoCommand)(M64CMD_CORE_STATE_SET, M64CORE_AUDIO_MUTE, &response);
}
else if (response == 1)
{
response = 0;
(*CoreDoCommand)(M64CMD_CORE_STATE_SET, M64CORE_AUDIO_MUTE, &response);
}
}
void MainWindow::on_actionHard_Reset_triggered()
{
(*CoreDoCommand)(M64CMD_RESET, 1, NULL);
}
void MainWindow::on_actionSoft_Reset_triggered()
{
(*CoreDoCommand)(M64CMD_RESET, 0, NULL);
}
void MainWindow::on_actionTake_Screenshot_triggered()
{
(*CoreDoCommand)(M64CMD_TAKE_NEXT_SCREENSHOT, 0, NULL);
}
void MainWindow::on_actionSave_State_triggered()
{
(*CoreDoCommand)(M64CMD_STATE_SAVE, 1, NULL);
}
void MainWindow::on_actionLoad_State_triggered()
{
(*CoreDoCommand)(M64CMD_STATE_LOAD, 1, NULL);
}
void MainWindow::on_actionToggle_Fullscreen_triggered()
{
int response;
(*CoreDoCommand)(M64CMD_CORE_STATE_QUERY, M64CORE_VIDEO_MODE, &response);
if (response == M64VIDEO_WINDOWED)
{
response = M64VIDEO_FULLSCREEN;
(*CoreDoCommand)(M64CMD_CORE_STATE_SET, M64CORE_VIDEO_MODE, &response);
}
else if (response == M64VIDEO_FULLSCREEN)
{
response = M64VIDEO_WINDOWED;
(*CoreDoCommand)(M64CMD_CORE_STATE_SET, M64CORE_VIDEO_MODE, &response);
}
}
void MainWindow::on_actionCheats_triggered()
{
QString gameName = getCheatGameName();
if (!gameName.isEmpty())
{
CheatsDialog *cheats = new CheatsDialog(gameName, this);
cheats->show();
}
else
{
QMessageBox msgBox;
msgBox.setText("Game must be running.");
msgBox.exec();
}
}
void MainWindow::on_actionSave_State_To_triggered()
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Save State File"), NULL, NULL);
if (!filename.isNull())
{
if (!filename.contains(".st"))
filename.append(".state");
(*CoreDoCommand)(M64CMD_STATE_SAVE, 1, filename.toUtf8().data());
}
}
void MainWindow::on_actionLoad_State_From_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open Save State"), NULL, tr("State Files (*.st* *.pj*)"));
if (!filename.isNull())
{
(*CoreDoCommand)(M64CMD_STATE_LOAD, 1, filename.toUtf8().data());
}
}
void MainWindow::on_actionController_Configuration_triggered()
{
if (!coreLib)
return;
int response;
(*CoreDoCommand)(M64CMD_CORE_STATE_QUERY, M64CORE_EMU_STATE, &response);
if (response == M64EMU_STOPPED)
resetCore();
typedef void (*Config_Func)();
Config_Func PluginConfig = (Config_Func)osal_dynlib_getproc(inputPlugin, "PluginConfig");
if (PluginConfig)
PluginConfig();
}
void MainWindow::on_actionHotkey_Configuration_triggered()
{
HotkeyDialog *settings = new HotkeyDialog(this);
settings->show();
}
void MainWindow::on_actionToggle_Speed_Limiter_triggered()
{
int value;
(*CoreDoCommand)(M64CMD_CORE_STATE_QUERY, M64CORE_SPEED_LIMITER, &value);
value = !value;
(*CoreDoCommand)(M64CMD_CORE_STATE_SET, M64CORE_SPEED_LIMITER, &value);
}
void MainWindow::on_actionView_Log_triggered()
{
logViewer.show();
}
void MainWindow::on_actionCreate_Room_triggered()
{
CreateRoom *createRoom = new CreateRoom(this);
createRoom->show();
}
void MainWindow::on_actionJoin_Room_triggered()
{
JoinRoom *joinRoom = new JoinRoom(this);
joinRoom->show();
}
void MainWindow::on_actionSupport_on_Patreon_triggered()
{
QDesktopServices::openUrl(QUrl("https://www.patreon.com/loganmc10"));
}
void MainWindow::on_actionSupport_on_GithubSponser_triggered()
{
QDesktopServices::openUrl(QUrl("https://github.com/sponsors/loganmc10"));
}
void MainWindow::on_actionOpen_Discord_Channel_triggered()
{
QDesktopServices::openUrl(QUrl("https://discord.gg/tsR3RtYynZ"));
}
WorkerThread *MainWindow::getWorkerThread()
{
return workerThread;
}
VkWindow *MainWindow::getVkWindow()
{
return my_window;
}
QSettings *MainWindow::getSettings()
{
return settings;
}
LogViewer *MainWindow::getLogViewer()
{
return &logViewer;
}
m64p_dynlib_handle MainWindow::getAudioPlugin()
{
return audioPlugin;
}
m64p_dynlib_handle MainWindow::getRspPlugin()
{
return rspPlugin;
}
m64p_dynlib_handle MainWindow::getInputPlugin()
{
return inputPlugin;
}
m64p_dynlib_handle MainWindow::getGfxPlugin()
{
return gfxPlugin;
}
void MainWindow::closeCoreLib()
{
if (coreLib != nullptr)
{
(*ConfigSaveFile)();
(*CoreShutdown)();
osal_dynlib_close(coreLib);
coreLib = nullptr;
}
}
void MainWindow::loadCoreLib()
{
#ifdef CORE_LIBRARY_PATH
QString corePath = CORE_LIBRARY_PATH;
#else
QString corePath = QCoreApplication::applicationDirPath();
#endif
m64p_error res = osal_dynlib_open(&coreLib, QDir(corePath).filePath(OSAL_DEFAULT_DYNLIB_FILENAME).toUtf8().constData());
if (res != M64ERR_SUCCESS)
{
QMessageBox msgBox;
msgBox.setText("Failed to load core library");
msgBox.exec();
return;
}
CoreStartup = (ptr_CoreStartup)osal_dynlib_getproc(coreLib, "CoreStartup");
CoreShutdown = (ptr_CoreShutdown)osal_dynlib_getproc(coreLib, "CoreShutdown");
CoreDoCommand = (ptr_CoreDoCommand)osal_dynlib_getproc(coreLib, "CoreDoCommand");
CoreAttachPlugin = (ptr_CoreAttachPlugin)osal_dynlib_getproc(coreLib, "CoreAttachPlugin");
CoreDetachPlugin = (ptr_CoreDetachPlugin)osal_dynlib_getproc(coreLib, "CoreDetachPlugin");
CoreOverrideVidExt = (ptr_CoreOverrideVidExt)osal_dynlib_getproc(coreLib, "CoreOverrideVidExt");
ConfigGetUserConfigPath = (ptr_ConfigGetUserConfigPath)osal_dynlib_getproc(coreLib, "ConfigGetUserConfigPath");
ConfigSaveFile = (ptr_ConfigSaveFile)osal_dynlib_getproc(coreLib, "ConfigSaveFile");
ConfigGetParameterHelp = (ptr_ConfigGetParameterHelp)osal_dynlib_getproc(coreLib, "ConfigGetParameterHelp");
ConfigGetParamInt = (ptr_ConfigGetParamInt)osal_dynlib_getproc(coreLib, "ConfigGetParamInt");
ConfigGetParamFloat = (ptr_ConfigGetParamFloat)osal_dynlib_getproc(coreLib, "ConfigGetParamFloat");
ConfigGetParamBool = (ptr_ConfigGetParamBool)osal_dynlib_getproc(coreLib, "ConfigGetParamBool");
ConfigGetParamString = (ptr_ConfigGetParamString)osal_dynlib_getproc(coreLib, "ConfigGetParamString");
ConfigSetParameter = (ptr_ConfigSetParameter)osal_dynlib_getproc(coreLib, "ConfigSetParameter");
ConfigDeleteSection = (ptr_ConfigDeleteSection)osal_dynlib_getproc(coreLib, "ConfigDeleteSection");
ConfigOpenSection = (ptr_ConfigOpenSection)osal_dynlib_getproc(coreLib, "ConfigOpenSection");
ConfigSaveSection = (ptr_ConfigSaveSection)osal_dynlib_getproc(coreLib, "ConfigSaveSection");
ConfigListParameters = (ptr_ConfigListParameters)osal_dynlib_getproc(coreLib, "ConfigListParameters");
ConfigGetSharedDataFilepath = (ptr_ConfigGetSharedDataFilepath)osal_dynlib_getproc(coreLib, "ConfigGetSharedDataFilepath");
CoreAddCheat = (ptr_CoreAddCheat)osal_dynlib_getproc(coreLib, "CoreAddCheat");
QString qtConfigDir = settings->value("configDirPath").toString();
if (!qtConfigDir.isEmpty())
(*CoreStartup)(CORE_API_VERSION, qtConfigDir.toUtf8().constData() /*Config dir*/, QCoreApplication::applicationDirPath().toUtf8().constData(), (char *)"Core", DebugCallback, NULL, NULL);
else
(*CoreStartup)(CORE_API_VERSION, NULL /*Config dir*/, QCoreApplication::applicationDirPath().toUtf8().constData(), (char *)"Core", DebugCallback, NULL, NULL);
CoreOverrideVidExt(&vidExtFunctions);
}
void MainWindow::closePlugins()
{
ptr_PluginShutdown PluginShutdown;
if (gfxPlugin != nullptr)
{
PluginShutdown = (ptr_PluginShutdown)osal_dynlib_getproc(gfxPlugin, "PluginShutdown");
(*PluginShutdown)();