forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgui_application.cpp
More file actions
1537 lines (1313 loc) · 45 KB
/
Copy pathgui_application.cpp
File metadata and controls
1537 lines (1313 loc) · 45 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
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 "stdafx.h"
#include "gui_application.h"
#include "qt_utils.h"
#include "permissions.h"
#include "welcome_dialog.h"
#include "main_window.h"
#include "emu_settings.h"
#include "gui_settings.h"
#include "persistent_settings.h"
#include "gs_frame.h"
#include "gl_gs_frame.h"
#include "localized_emu.h"
#include "qt_camera_handler.h"
#include "qt_music_handler.h"
#include "rpcs3_version.h"
#include "display_sleep_control.h"
#ifdef WITH_DISCORD_RPC
#include "_discord_utils.h"
#endif
#ifdef HAVE_SDL3
#include "Input/sdl_camera_handler.h"
#endif
#include "Emu/Audio/audio_utils.h"
#include "Emu/Cell/Modules/cellSysutil.h"
#include "Emu/Io/Null/null_camera_handler.h"
#include "Emu/Io/Null/null_music_handler.h"
#include "Emu/vfs_config.h"
#include "util/init_mutex.hpp"
#include "util/console.h"
#include "qt_video_source.h"
#include "trophy_notification_helper.h"
#include "save_data_dialog.h"
#include "msg_dialog_frame.h"
#include "osk_dialog_frame.h"
#include "recvmessage_dialog_frame.h"
#include "sendmessage_dialog_frame.h"
#include "stylesheets.h"
#include "progress_dialog.h"
#include <QScreen>
#include <QFontDatabase>
#include <QLayout>
#include <QLibraryInfo>
#include <QDirIterator>
#include <QFileInfo>
#include <QMessageBox>
#include <QTextDocument>
#include <QStyleFactory>
#include <QStyleHints>
#include <clocale>
#include "Emu/RSX/Null/NullGSRender.h"
#include "Emu/RSX/GL/GLGSRender.h"
#if defined(HAVE_VULKAN)
#include "Emu/RSX/VK/VKGSRender.h"
#endif
#ifdef _WIN32
#include <Usbiodef.h>
#include <Dbt.h>
#include "Emu/Cell/lv2/sys_usbd.h"
#endif
LOG_CHANNEL(gui_log, "GUI");
std::unique_ptr<raw_mouse_handler> g_raw_mouse_handler;
s32 gui_application::m_language_id = static_cast<s32>(CELL_SYSUTIL_LANG_ENGLISH_US);
[[noreturn]] void report_fatal_error(std::string_view text, bool is_html = false, bool include_help_text = true);
gui_application::gui_application(int& argc, char** argv) : QApplication(argc, argv)
{
std::setlocale(LC_NUMERIC, "C"); // On linux Qt changes to system locale while initializing QCoreApplication
}
gui_application::~gui_application()
{
#ifdef WITH_DISCORD_RPC
discord::shutdown();
#endif
#ifdef _WIN32
unregister_device_notification();
#endif
}
int gui_application::exec()
{
// Show a series of dialogs using the main event loop before finally showing the main window.
// We used to do this synchronous with e.g. QDialog::exec() before we called gui_application::exec(),
// but when you call quit() (as seen in these dialogs) without a running event loop,
// then the destructors of QObjects won't be called, leading to all sorts of issues.
std::shared_ptr<u32> step_index = std::make_shared<u32>(0);
std::shared_ptr<std::vector<std::function<void()>>> steps = std::make_shared<std::vector<std::function<void()>>>();
m_show_next_dialog = [this, step_index, steps]()
{
ensure(steps && step_index);
const auto& func = ::at32(*steps, (*step_index)++);
ensure(func);
func();
};
if (!rpcs3::is_release_build() && !rpcs3::is_local_build())
{
steps->push_back([this]()
{
const std::string_view branch_name = rpcs3::get_full_branch();
gui_log.warning("Experimental Build Warning! Build origin: %s", branch_name);
QMessageBox* msg = new QMessageBox();
msg->setAttribute(Qt::WA_DeleteOnClose);
msg->setWindowModality(Qt::WindowModal);
msg->setWindowTitle(tr("Experimental Build Warning"));
msg->setIcon(QMessageBox::Critical);
msg->setTextFormat(Qt::RichText);
msg->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msg->setDefaultButton(QMessageBox::No);
msg->setText(gui::utils::make_paragraph(tr(
"Please understand that this build is not an official RPCS3 release.\n"
"This build contains changes that may break games, or even <b>damage</b> your data.\n"
"We recommend to download and use the official build from the %0.\n"
"\n"
"Build origin: %1\n"
"Do you wish to use this build anyway?")
.arg(gui::utils::make_link(tr("RPCS3 website"), "https://rpcs3.net/download"))
.arg(Qt::convertFromPlainText(branch_name.data()))));
msg->layout()->setSizeConstraint(QLayout::SetFixedSize);
connect(msg, &QMessageBox::finished, this, [this](int)
{
const QMessageBox* box = qobject_cast<QMessageBox*>(sender());
if (!box || box->standardButton(box->clickedButton()) == QMessageBox::No)
{
Emu.Quit(true);
return;
}
m_show_next_dialog();
});
msg->open();
});
}
#ifdef __linux__
const bool is_flatpak = qEnvironmentVariableIsSet("FLATPAK_ID");
const bool is_snap = qEnvironmentVariableIsSet("SNAP");
const QString unofficial_build = is_flatpak ? "Flatpak" : (is_snap ? "Snap" : "");
if (!unofficial_build.isEmpty())
{
steps->push_back([this, unofficial_build]()
{
gui_log.warning("%s Build Warning!", unofficial_build);
QMessageBox* msg = new QMessageBox();
msg->setAttribute(Qt::WA_DeleteOnClose);
msg->setWindowModality(Qt::WindowModal);
msg->setWindowTitle(tr("Unofficial Build Warning"));
msg->setIcon(QMessageBox::Critical);
msg->setTextFormat(Qt::RichText);
msg->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msg->setDefaultButton(QMessageBox::No);
msg->setText(gui::utils::make_paragraph(tr(
"Warning! You're running an unofficial %0 build of RPCS3.\n"
"You will get no official support for this build.\n"
"Issues opened on the RPCS3 GitHub related to %0 builds are not allowed and will be closed.\n"
"We recommend to download and use the official build from the %1.\n"
"\n"
"Do you wish to use this build anyway?")
.arg(unofficial_build)
.arg(gui::utils::make_link(tr("RPCS3 website"), "https://rpcs3.net/download"))));
msg->layout()->setSizeConstraint(QLayout::SetFixedSize);
connect(msg, &QMessageBox::finished, this, [this](int)
{
const QMessageBox* box = qobject_cast<QMessageBox*>(sender());
if (!box || box->standardButton(box->clickedButton()) == QMessageBox::No)
{
Emu.Quit(true);
return;
}
m_show_next_dialog();
});
msg->open();
});
}
#endif
if (m_render_creator->vulkan_timed_out)
{
steps->push_back([this]()
{
gui_log.error("Vulkan device enumeration timed out");
QMessageBox* msg = new QMessageBox();
msg->setAttribute(Qt::WA_DeleteOnClose);
msg->setWindowModality(Qt::WindowModal);
msg->setIcon(QMessageBox::Critical);
msg->setStandardButtons(QMessageBox::Ignore | QMessageBox::Abort);
msg->setDefaultButton(QMessageBox::Abort);
msg->setWindowTitle(tr("Vulkan Check Timeout"));
msg->setText(tr("Querying for Vulkan-compatible devices is taking too long. This is usually caused by malfunctioning "
"graphics drivers, reinstalling them could fix the issue.\n\n"
"Selecting ignore starts the emulator without Vulkan support."));
msg->layout()->setSizeConstraint(QLayout::SetFixedSize);
connect(msg, &QMessageBox::finished, this, [this](int)
{
const QMessageBox* box = qobject_cast<QMessageBox*>(sender());
if (!box || box->standardButton(box->clickedButton()) == QMessageBox::Abort)
{
Emu.Quit(true);
return;
}
m_show_next_dialog();
});
msg->open();
});
}
if (m_gui_settings->GetValue(gui::ib_show_welcome).toBool())
{
steps->push_back([this]()
{
welcome_dialog* welcome = new welcome_dialog(m_gui_settings, false);
connect(welcome, &QDialog::finished, this, [this](int result)
{
if (result == QDialog::Rejected)
{
Emu.Quit(true);
return;
}
m_show_next_dialog();
});
welcome->open();
});
}
steps->push_back([this]()
{
if (m_main_window)
{
m_main_window->show();
}
#ifdef __APPLE__
if (!m_render_creator->Vulkan.supported)
{
QMessageBox::warning(nullptr,
tr("Warning"),
tr("Vulkan is not supported on this Mac.\n"
"No graphics will be rendered."));
}
#endif
// Check maxfiles
if (utils::get_maxfiles() < 4096)
{
QMessageBox::warning(nullptr,
tr("Warning"),
tr("The current limit of maximum file descriptors is too low.\n"
"Some games will crash.\n"
"\n"
"Please increase the limit before running RPCS3."));
}
});
m_show_next_dialog();
return QGuiApplication::exec();
}
void gui_application::Init()
{
#ifndef __APPLE__
setWindowIcon(QIcon(":/rpcs3.ico"));
#endif
m_emu_settings = std::make_shared<emu_settings>(m_render_creator);
m_gui_settings = std::make_shared<gui_settings>();
m_persistent_settings = std::make_shared<persistent_settings>();
if (m_gui_settings->GetValue(gui::m_attachCommandLine).toBool())
{
utils::attach_console(utils::console_stream::std_err, true);
}
else
{
m_gui_settings->SetValue(gui::m_attachCommandLine, false);
}
// The user might be set by cli arg. If not, set another user.
if (m_active_user.empty())
{
// Get active user with standard user as fallback
m_active_user = m_persistent_settings->GetCurrentUser("00000001").toStdString();
}
// Create callbacks from the emulator, which reference the handlers.
InitializeCallbacks();
// Force init the emulator
InitializeEmulator(m_active_user, m_show_gui, false);
// Create connects to propagate events throughout Gui.
InitializeConnects();
// Create the main window
if (m_show_gui)
{
m_main_window = new main_window(m_gui_settings, m_emu_settings, m_persistent_settings, m_with_cli_boot, nullptr);
const auto codes = GetAvailableLanguageCodes();
const auto language = m_gui_settings->GetValue(gui::loc_language).toString();
const auto index = codes.indexOf(language);
LoadLanguage(index < 0 ? QLocale(QLocale::English).bcp47Name() : ::at32(codes, index));
connect(m_main_window, &main_window::RequestLanguageChange, this, &gui_application::LoadLanguage);
connect(m_main_window, &main_window::RequestGlobalStylesheetChange, this, &gui_application::OnChangeStyleSheetRequest);
connect(m_main_window, &main_window::NotifyEmuSettingsChange, this, [this](){ OnEmuSettingsChange(); });
connect(m_main_window, &main_window::NotifyShortcutHandlers, this, &gui_application::OnShortcutChange);
connect(this, &gui_application::OnEmulatorRun, m_main_window, &main_window::OnEmuRun);
connect(this, &gui_application::OnEmulatorStop, m_main_window, &main_window::OnEmuStop);
connect(this, &gui_application::OnEmulatorPause, m_main_window, &main_window::OnEmuPause);
connect(this, &gui_application::OnEmulatorResume, m_main_window, &main_window::OnEmuResume);
connect(this, &gui_application::OnEmulatorReady, m_main_window, &main_window::OnEmuReady);
connect(this, &gui_application::OnEnableDiscEject, m_main_window, &main_window::OnEnableDiscEject);
connect(this, &gui_application::OnEnableDiscInsert, m_main_window, &main_window::OnEnableDiscInsert);
connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, [this](){ OnChangeStyleSheetRequest(); });
m_main_window->Init();
}
#ifdef WITH_DISCORD_RPC
// Discord Rich Presence Integration
if (m_gui_settings->GetValue(gui::m_richPresence).toBool())
{
discord::initialize();
}
#endif
// Install native event filter
#ifdef _WIN32 // Currently only needed for raw mouse input on windows
installNativeEventFilter(&m_native_event_filter);
if (m_main_window)
{
register_device_notification(m_main_window->winId());
}
#endif
}
void gui_application::SwitchTranslator(const QString& language_code)
{
// remove the old translator
removeTranslator(&m_translator);
for (QTranslator* qt_translator : m_qt_translators)
{
removeTranslator(qt_translator);
qt_translator->deleteLater();
}
m_qt_translators.clear();
const QString default_code = QLocale(QLocale::English).bcp47Name();
const QString lang_path = QLibraryInfo::path(QLibraryInfo::TranslationsPath) + QStringLiteral("/");
// Load qt translation files
const QDir dir(lang_path);
if (dir.exists())
{
QStringList qm_files = dir.entryList(QStringList() << QStringLiteral("qt*_%1.qm").arg(language_code), QDir::Files | QDir::Readable);
if (qm_files.empty())
{
qm_files = dir.entryList(QStringList() << QStringLiteral("qt*_%1.qm").arg(QLocale::languageToCode(QLocale(language_code).language())), QDir::Files | QDir::Readable);
}
for (const QString& qm_file : qm_files)
{
const QString file_path = lang_path + qm_file;
QTranslator* qt_translator = new QTranslator(this);
if (qt_translator->load(file_path))
{
gui_log.notice("Installing translation: '%s'", file_path);
installTranslator(qt_translator);
m_qt_translators.push_back(std::move(qt_translator));
}
else
{
gui_log.error("Failed to load translation: '%s'", file_path);
qt_translator->deleteLater();
}
}
}
else
{
gui_log.error("Qt translation dir '%s' does not exist", lang_path);
}
const QString file_path = lang_path + QStringLiteral("rpcs3_%1.qm").arg(language_code);
if (QFileInfo(file_path).isFile())
{
// load the new translator
if (m_translator.load(file_path))
{
gui_log.notice("Installing translation: '%s'", file_path);
installTranslator(&m_translator);
}
else
{
gui_log.error("Failed to load translation: '%s'", file_path);
}
}
else if (language_code != default_code)
{
// show error, but ignore default case "en", since it is handled in source code
gui_log.error("No translation file found in: '%s'", file_path);
// reset current language to default "en"
set_language_code(default_code);
}
}
void gui_application::LoadLanguage(const QString& language_code)
{
if (m_language_code == language_code)
{
return;
}
set_language_code(language_code);
const QLocale locale = QLocale(language_code);
const QString locale_name = QLocale::languageToString(locale.language());
QLocale::setDefault(locale);
// Idk if this is overruled by the QLocale default, so I'll change it here just to be sure.
// As per QT recommendations to avoid conflicts for POSIX functions
std::setlocale(LC_NUMERIC, "C");
SwitchTranslator(language_code);
if (m_main_window)
{
const QString default_code = QLocale(QLocale::English).bcp47Name();
QStringList language_codes = GetAvailableLanguageCodes();
if (!language_codes.contains(default_code))
{
language_codes.prepend(default_code);
}
m_main_window->RetranslateUI(language_codes, m_language_code);
}
m_gui_settings->SetValue(gui::loc_language, m_language_code);
gui_log.notice("Current language changed to %s (%s)", locale_name, language_code);
}
QStringList gui_application::GetAvailableLanguageCodes()
{
QStringList language_codes;
const QString language_path = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
gui_log.notice("Checking languages in '%s'", language_path);
if (QFileInfo(language_path).isDir())
{
const QDir dir(language_path);
const QStringList filenames = dir.entryList(QStringList("rpcs3_*.qm"));
for (const QString& filename : filenames)
{
QString language_code = filename; // "rpcs3_en.qm"
language_code.truncate(language_code.lastIndexOf('.')); // "rpcs3_en"
language_code.remove(0, language_code.indexOf('_') + 1); // "en"
if (language_codes.contains(language_code))
{
gui_log.error("Found duplicate language '%s' (%s)", language_code, filename);
}
else
{
gui_log.notice("Found language '%s' (%s)", language_code, filename);
language_codes << language_code;
}
}
}
else
{
gui_log.error("Language dir not found: '%s'", language_path);
}
return language_codes;
}
void gui_application::set_language_code(QString language_code)
{
m_language_code = language_code;
// Transform language code to lowercase and use '-'
language_code = language_code.toLower().replace("_", "-");
// Try to find the CELL language ID for this language code
static const std::map<QString, CellSysutilLang> language_ids = {
{"ja", CELL_SYSUTIL_LANG_JAPANESE },
{"en", CELL_SYSUTIL_LANG_ENGLISH_US },
{"en-us", CELL_SYSUTIL_LANG_ENGLISH_US },
{"en-gb", CELL_SYSUTIL_LANG_ENGLISH_GB },
{"fr", CELL_SYSUTIL_LANG_FRENCH },
{"es", CELL_SYSUTIL_LANG_SPANISH },
{"de", CELL_SYSUTIL_LANG_GERMAN },
{"it", CELL_SYSUTIL_LANG_ITALIAN },
{"nl", CELL_SYSUTIL_LANG_DUTCH },
{"pt", CELL_SYSUTIL_LANG_PORTUGUESE_PT },
{"pt-pt", CELL_SYSUTIL_LANG_PORTUGUESE_PT },
{"pt-br", CELL_SYSUTIL_LANG_PORTUGUESE_BR },
{"ru", CELL_SYSUTIL_LANG_RUSSIAN },
{"ko", CELL_SYSUTIL_LANG_KOREAN },
{"zh", CELL_SYSUTIL_LANG_CHINESE_T },
{"zh-hant", CELL_SYSUTIL_LANG_CHINESE_T },
{"zh-hans", CELL_SYSUTIL_LANG_CHINESE_S },
{"fi", CELL_SYSUTIL_LANG_FINNISH },
{"sv", CELL_SYSUTIL_LANG_SWEDISH },
{"da", CELL_SYSUTIL_LANG_DANISH },
{"no", CELL_SYSUTIL_LANG_NORWEGIAN },
{"nn", CELL_SYSUTIL_LANG_NORWEGIAN },
{"nb", CELL_SYSUTIL_LANG_NORWEGIAN },
{"pl", CELL_SYSUTIL_LANG_POLISH },
{"tr", CELL_SYSUTIL_LANG_TURKISH },
};
// Check direct match first
const auto it = language_ids.find(language_code);
if (it != language_ids.cend())
{
m_language_id = static_cast<s32>(it->second);
return;
}
// Try to find closest match
for (const auto& [code, id] : language_ids)
{
if (language_code.startsWith(code))
{
m_language_id = static_cast<s32>(id);
return;
}
}
// Fallback to English (US)
m_language_id = static_cast<s32>(CELL_SYSUTIL_LANG_ENGLISH_US);
}
s32 gui_application::get_language_id()
{
return m_language_id;
}
void gui_application::InitializeConnects()
{
connect(&m_timer, &QTimer::timeout, this, &gui_application::UpdatePlaytime);
connect(this, &gui_application::OnEmulatorRun, this, &gui_application::StartPlaytime);
connect(this, &gui_application::OnEmulatorStop, this, &gui_application::StopPlaytime);
connect(this, &gui_application::OnEmulatorPause, this, &gui_application::StopPlaytime);
connect(this, &gui_application::OnEmulatorResume, this, &gui_application::StartPlaytime);
connect(this, &QGuiApplication::applicationStateChanged, this, &gui_application::OnAppStateChanged);
#ifdef WITH_DISCORD_RPC
connect(this, &gui_application::OnEmulatorRun, [this](bool /*start_playtime*/)
{
// Discord Rich Presence Integration
if (m_gui_settings->GetValue(gui::m_richPresence).toBool())
{
discord::update_presence(Emu.GetTitleID(), Emu.GetTitle());
}
});
connect(this, &gui_application::OnEmulatorStop, [this]()
{
// Discord Rich Presence Integration
if (m_gui_settings->GetValue(gui::m_richPresence).toBool())
{
discord::update_presence(m_gui_settings->GetValue(gui::m_discordState).toString().toStdString());
}
});
#endif
qRegisterMetaType<std::function<void()>>("std::function<void()>");
connect(this, &gui_application::RequestCallFromMainThread, this, &gui_application::CallFromMainThread);
}
std::unique_ptr<gs_frame> gui_application::get_gs_frame()
{
// Load AppIcon
const QIcon app_icon = m_main_window ? m_main_window->GetAppIcon() : gui::utils::get_app_icon_from_path(Emu.GetBoot(), Emu.GetTitleID());
if (m_game_window)
{
// Check if the continuous mode is enabled. We reset the mode after each use in order to ensure that it is only used when explicitly needed.
const bool continuous_mode_enabled = Emu.ContinuousModeEnabled(true);
// Make sure we run the same config
const bool is_same_renderer = m_game_window->renderer() == g_cfg.video.renderer;
if (is_same_renderer && (Emu.IsChildProcess() || continuous_mode_enabled))
{
gui_log.notice("gui_application: Re-using old game window (IsChildProcess=%d, ContinuousModeEnabled=%d)", Emu.IsChildProcess(), continuous_mode_enabled);
if (!app_icon.isNull())
{
m_game_window->setIcon(app_icon);
}
return std::unique_ptr<gs_frame>(m_game_window);
}
// Clean-up old game window. This should only happen if the renderer changed or there was an unexpected error during boot.
Emu.GetCallbacks().close_gs_frame();
}
gui_log.notice("gui_application: Creating new game window");
extern const std::unordered_map<video_resolution, std::pair<int, int>, value_hash<video_resolution>> g_video_out_resolution_map;
auto [w, h] = ::at32(g_video_out_resolution_map, g_cfg.video.resolution);
const bool resize_game_window = m_gui_settings->GetValue(gui::gs_resize).toBool();
if (resize_game_window)
{
if (m_gui_settings->GetValue(gui::gs_resize_manual).toBool())
{
w = m_gui_settings->GetValue(gui::gs_width).toInt();
h = m_gui_settings->GetValue(gui::gs_height).toInt();
}
else
{
const qreal device_pixel_ratio = devicePixelRatio();
w /= device_pixel_ratio;
h /= device_pixel_ratio;
}
}
QScreen* screen = nullptr;
QRect base_geometry{};
// Use screen index set by CLI argument
int screen_index = m_game_screen_index;
const int last_screen_index = m_gui_settings->GetValue(gui::gs_screen).toInt();
// Use last used screen if no CLI index was set
if (screen_index < 0)
{
screen_index = last_screen_index;
}
// Try to find the specified screen
if (screen_index >= 0)
{
const QList<QScreen*> available_screens = screens();
if (screen_index < available_screens.count())
{
screen = ::at32(available_screens, screen_index);
if (screen)
{
base_geometry = screen->geometry();
}
}
if (!screen)
{
gui_log.error("The selected game screen with index %d is not available (available screens: %d)", screen_index, available_screens.count());
}
}
// Fallback to the screen of the main window. Use the primary screen as last resort.
if (!screen)
{
screen = m_main_window ? m_main_window->screen() : primaryScreen();
base_geometry = m_main_window ? m_main_window->frameGeometry() : primaryScreen()->geometry();
}
// Use saved geometry if possible. Ignore this if the last used screen is different than the requested screen.
QRect frame_geometry = screen_index != last_screen_index ? QRect{} : m_gui_settings->GetValue(gui::gs_geometry).value<QRect>();
if (frame_geometry.isNull() || frame_geometry.isEmpty())
{
// Center above main window or inside screen if the saved geometry is invalid
frame_geometry = gui::utils::create_centered_window_geometry(screen, base_geometry, w, h);
}
else if (resize_game_window)
{
// Apply size override to our saved geometry if needed
frame_geometry.setSize(QSize(w, h));
}
gs_frame* frame = nullptr;
switch (g_cfg.video.renderer.get())
{
case video_renderer::opengl:
{
frame = new gl_gs_frame(screen, frame_geometry, app_icon, m_gui_settings, m_start_games_fullscreen);
break;
}
case video_renderer::null:
case video_renderer::vulkan:
{
frame = new gs_frame(screen, frame_geometry, app_icon, m_gui_settings, m_start_games_fullscreen);
break;
}
}
m_game_window = frame;
ensure(m_game_window);
#ifdef _WIN32
if (!m_show_gui)
{
register_device_notification(m_game_window->winId());
}
#endif
connect(m_game_window, &gs_frame::destroyed, this, [this]()
{
gui_log.notice("gui_application: Deleting old game window");
m_game_window = nullptr;
#ifdef _WIN32
if (!m_show_gui)
{
unregister_device_notification();
}
#endif
});
return std::unique_ptr<gs_frame>(frame);
}
/** RPCS3 emulator has functions it desires to call from the GUI at times. Initialize them in here. */
void gui_application::InitializeCallbacks()
{
EmuCallbacks callbacks = CreateCallbacks();
callbacks.try_to_quit = [this](bool force_quit, std::function<void()> on_exit) -> bool
{
// Close rpcs3 if closed in no-gui mode
if (force_quit || !m_main_window)
{
if (on_exit)
{
on_exit();
}
const bool no_gui = !m_main_window;
if (m_main_window)
{
// Close main window in order to save its window state
m_main_window->close();
}
gui_log.notice("Quitting gui application (force_quit=%d, no-gui=%d)", force_quit, no_gui);
quit();
return true;
}
return false;
};
callbacks.call_from_main_thread = [this](std::function<void()> func, atomic_t<u32>* wake_up)
{
RequestCallFromMainThread(std::move(func), wake_up);
};
callbacks.init_gs_render = [](utils::serial* ar)
{
switch (g_cfg.video.renderer.get())
{
case video_renderer::null:
{
g_fxo->init<rsx::thread, named_thread<NullGSRender>>(ar);
break;
}
case video_renderer::opengl:
{
#if not defined(__APPLE__)
g_fxo->init<rsx::thread, named_thread<GLGSRender>>(ar);
#endif
break;
}
case video_renderer::vulkan:
{
#if defined(HAVE_VULKAN)
g_fxo->init<rsx::thread, named_thread<VKGSRender>>(ar);
#endif
break;
}
}
};
callbacks.get_camera_handler = []() -> std::shared_ptr<camera_handler_base>
{
switch (g_cfg.io.camera.get())
{
case camera_handler::null:
case camera_handler::fake:
{
return std::make_shared<null_camera_handler>();
}
case camera_handler::qt:
{
return std::make_shared<qt_camera_handler>();
}
#ifdef HAVE_SDL3
case camera_handler::sdl:
{
return std::make_shared<sdl_camera_handler>();
}
#endif
}
return nullptr;
};
callbacks.get_music_handler = []() -> std::shared_ptr<music_handler_base>
{
switch (g_cfg.audio.music.get())
{
case music_handler::null:
{
return std::make_shared<null_music_handler>();
}
case music_handler::qt:
{
return std::make_shared<qt_music_handler>();
}
}
return nullptr;
};
callbacks.close_gs_frame = [this]()
{
if (m_game_window)
{
gui_log.warning("gui_application: Closing old game window");
m_game_window->ignore_stop_events();
delete m_game_window;
m_game_window = nullptr;
}
};
callbacks.get_gs_frame = [this]() -> std::unique_ptr<GSFrameBase> { return get_gs_frame(); };
callbacks.get_msg_dialog = [this]() -> std::shared_ptr<MsgDialogBase> { return m_show_gui ? std::make_shared<msg_dialog_frame>() : nullptr; };
callbacks.get_osk_dialog = [this]() -> std::shared_ptr<OskDialogBase> { return m_show_gui ? std::make_shared<osk_dialog_frame>() : nullptr; };
callbacks.get_save_dialog = []() -> std::unique_ptr<SaveDialogBase> { return std::make_unique<save_data_dialog>(); };
callbacks.get_sendmessage_dialog = []() -> std::shared_ptr<SendMessageDialogBase> { return std::make_shared<sendmessage_dialog_frame>(); };
callbacks.get_recvmessage_dialog = []() -> std::shared_ptr<RecvMessageDialogBase> { return std::make_shared<recvmessage_dialog_frame>(); };
callbacks.get_trophy_notification_dialog = [this]() -> std::unique_ptr<TrophyNotificationBase> { return std::make_unique<trophy_notification_helper>(m_game_window); };
callbacks.on_run = [this](bool start_playtime) { OnEmulatorRun(start_playtime); };
callbacks.on_pause = [this]() { OnEmulatorPause(); };
callbacks.on_resume = [this]() { OnEmulatorResume(true); };
callbacks.on_stop = [this]() { OnEmulatorStop(); };
callbacks.on_ready = [this]() { OnEmulatorReady(); };
callbacks.enable_disc_eject = [this](bool enabled)
{
Emu.CallFromMainThread([this, enabled]()
{
OnEnableDiscEject(enabled);
});
};
callbacks.enable_disc_insert = [this](bool enabled)
{
Emu.CallFromMainThread([this, enabled]()
{
OnEnableDiscInsert(enabled);
});
};
callbacks.on_missing_fw = [this]()
{
if (m_main_window)
{
m_main_window->OnMissingFw();
}
};
callbacks.handle_taskbar_progress = [this](s32 type, s32 value)
{
if (m_game_window)
{
switch (type)
{
case 0: m_game_window->progress_reset(value); break;
case 1: m_game_window->progress_increment(value); break;
case 2: m_game_window->progress_set_limit(value); break;
case 3: m_game_window->progress_set_value(value); break;
default: gui_log.fatal("Unknown type in handle_taskbar_progress(type=%d, value=%d)", type, value); break;
}
}
};
callbacks.get_localized_string = [](localized_string_id id, const char* args) -> std::string
{
return localized_emu::get_string(id, args);
};
callbacks.get_localized_u32string = [](localized_string_id id, const char* args) -> std::u32string
{
return localized_emu::get_u32string(id, args);
};
callbacks.get_localized_setting = [this](const cfg::_base* node, u32 enum_index) -> std::string
{
ensure(!!m_emu_settings);
return m_emu_settings->GetLocalizedSetting(node, enum_index);
};
callbacks.play_sound = [this](const std::string& path, std::optional<f32> volume)
{
Emu.CallFromMainThread([this, path, volume]()
{
if (fs::is_file(path))
{
// Allow to play 3 sound effects at the same time
while (m_sound_effects.size() >= 3)
{
m_sound_effects.pop_front();
}
// Create a new sound effect. Re-using the same object seems to be broken for some users starting with Qt 6.6.3.
std::unique_ptr<QSoundEffect> sound_effect = std::make_unique<QSoundEffect>();
sound_effect->setSource(QUrl::fromLocalFile(QString::fromStdString(path)));
sound_effect->setVolume(volume ? *volume : audio::get_volume());
sound_effect->play();
m_sound_effects.push_back(std::move(sound_effect));
}
}, nullptr, false);
};
if (m_show_gui) // If this is false, we already have a fallback in the main_application.
{
callbacks.on_install_pkgs = [this](const std::vector<std::string>& pkgs, bool from_optical_drive)
{
ensure(!pkgs.empty());
QStringList pkg_list;
for (const std::string& pkg : pkgs)
{
pkg_list << QString::fromStdString(pkg);
}
return main_window::InstallPackages(m_main_window, pkg_list, true, from_optical_drive);
};
}
callbacks.on_emulation_stop_no_response = [](std::shared_ptr<atomic_t<bool>> closed_successfully, int seconds_waiting_already)
{
const std::string terminate_message = tr("Stopping emulator took too long."
"\nSome thread has probably deadlocked. Aborting.").toStdString();
if (!closed_successfully)
{
report_fatal_error(terminate_message);
}
Emu.CallFromMainThread([closed_successfully, seconds_waiting_already, terminate_message]
{
const auto seconds = std::make_shared<int>(seconds_waiting_already);
QMessageBox* mb = new QMessageBox();
mb->setWindowTitle(tr("PS3 Game/Application Is Unresponsive"));
mb->setIcon(QMessageBox::Critical);
mb->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
mb->setDefaultButton(QMessageBox::No);
mb->button(QMessageBox::Yes)->setText(tr("Terminate RPCS3"));
mb->button(QMessageBox::No)->setText(tr("Keep Waiting"));
QString text_base = tr("Waiting for %0 second(s) already to stop emulation without success."
"\nKeep waiting or terminate RPCS3 unsafely at your own risk?");
mb->setText(text_base.arg(10));
mb->layout()->setSizeConstraint(QLayout::SetFixedSize);
mb->setAttribute(Qt::WA_DeleteOnClose);
QTimer* update_timer = new QTimer(mb);
connect(update_timer, &QTimer::timeout, [mb, seconds, text_base, closed_successfully]()
{
*seconds += 1;
mb->setText(text_base.arg(*seconds));
if (*closed_successfully)
{
mb->reject();
}