main.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <memory>
  5. #include <optional>
  6. #include <filesystem>
  7. #include <QMainWindow>
  8. #include <QMessageBox>
  9. #include <QPushButton>
  10. #include <QTimer>
  11. #include <QTranslator>
  12. #include "common/announce_multiplayer_room.h"
  13. #include "common/common_types.h"
  14. #include "configuration/qt_config.h"
  15. #include "input_common/drivers/tas_input.h"
  16. #include "yuzu/compatibility_list.h"
  17. #include "yuzu/hotkeys.h"
  18. #include "yuzu/util/controller_navigation.h"
  19. #ifdef __unix__
  20. #include <QVariant>
  21. #include <QtDBus/QDBusInterface>
  22. #include <QtDBus/QtDBus>
  23. #endif
  24. class QtConfig;
  25. class ClickableLabel;
  26. class EmuThread;
  27. class GameList;
  28. class GImageInfo;
  29. class GRenderWindow;
  30. class LoadingScreen;
  31. class MicroProfileDialog;
  32. class OverlayDialog;
  33. class ProfilerWidget;
  34. class ControllerDialog;
  35. class QLabel;
  36. class MultiplayerState;
  37. class QPushButton;
  38. class QProgressDialog;
  39. class QSlider;
  40. class QHBoxLayout;
  41. class WaitTreeWidget;
  42. enum class GameListOpenTarget;
  43. enum class GameListRemoveTarget;
  44. enum class GameListShortcutTarget;
  45. enum class DumpRomFSTarget;
  46. enum class InstalledEntryType;
  47. class GameListPlaceholder;
  48. class QtAmiiboSettingsDialog;
  49. class QtControllerSelectorDialog;
  50. class QtProfileSelectionDialog;
  51. class QtSoftwareKeyboardDialog;
  52. class QtNXWebEngineView;
  53. enum class StartGameType {
  54. Normal, // Can use custom configuration
  55. Global, // Only uses global configuration
  56. };
  57. enum class AmLaunchType {
  58. UserInitiated,
  59. ApplicationInitiated,
  60. };
  61. namespace Core {
  62. enum class SystemResultStatus : u32;
  63. class System;
  64. } // namespace Core
  65. namespace Core::Frontend {
  66. struct CabinetParameters;
  67. struct ControllerParameters;
  68. struct InlineAppearParameters;
  69. struct InlineTextParameters;
  70. struct KeyboardInitializeParameters;
  71. struct ProfileSelectParameters;
  72. } // namespace Core::Frontend
  73. namespace DiscordRPC {
  74. class DiscordInterface;
  75. }
  76. namespace PlayTime {
  77. class PlayTimeManager;
  78. }
  79. namespace FileSys {
  80. class ContentProvider;
  81. class ManualContentProvider;
  82. class VfsFilesystem;
  83. } // namespace FileSys
  84. namespace InputCommon {
  85. class InputSubsystem;
  86. }
  87. namespace Service::AM::Applets {
  88. enum class SwkbdResult : u32;
  89. enum class SwkbdTextCheckResult : u32;
  90. enum class SwkbdReplyType : u32;
  91. enum class WebExitReason : u32;
  92. } // namespace Service::AM::Applets
  93. namespace Service::NFC {
  94. class NfcDevice;
  95. } // namespace Service::NFC
  96. namespace Service::NFP {
  97. enum class CabinetMode : u8;
  98. } // namespace Service::NFP
  99. namespace Ui {
  100. class MainWindow;
  101. }
  102. enum class EmulatedDirectoryTarget {
  103. NAND,
  104. SDMC,
  105. };
  106. enum class InstallResult {
  107. Success,
  108. Overwrite,
  109. Failure,
  110. BaseInstallAttempted,
  111. };
  112. enum class ReinitializeKeyBehavior {
  113. NoWarning,
  114. Warning,
  115. };
  116. namespace VkDeviceInfo {
  117. class Record;
  118. }
  119. class VolumeButton : public QPushButton {
  120. Q_OBJECT
  121. public:
  122. explicit VolumeButton(QWidget* parent = nullptr) : QPushButton(parent), scroll_multiplier(1) {
  123. connect(&scroll_timer, &QTimer::timeout, this, &VolumeButton::ResetMultiplier);
  124. }
  125. signals:
  126. void VolumeChanged();
  127. protected:
  128. void wheelEvent(QWheelEvent* event) override;
  129. private slots:
  130. void ResetMultiplier();
  131. private:
  132. int scroll_multiplier;
  133. QTimer scroll_timer;
  134. constexpr static int MaxMultiplier = 8;
  135. };
  136. class GMainWindow : public QMainWindow {
  137. Q_OBJECT
  138. /// Max number of recently loaded items to keep track of
  139. static const int max_recent_files_item = 10;
  140. // TODO: Make use of this!
  141. enum {
  142. UI_IDLE,
  143. UI_EMU_BOOTING,
  144. UI_EMU_RUNNING,
  145. UI_EMU_STOPPING,
  146. };
  147. enum {
  148. CREATE_SHORTCUT_MSGBOX_FULLSCREEN_YES,
  149. CREATE_SHORTCUT_MSGBOX_SUCCESS,
  150. CREATE_SHORTCUT_MSGBOX_ERROR,
  151. CREATE_SHORTCUT_MSGBOX_APPVOLATILE_WARNING,
  152. };
  153. public:
  154. void filterBarSetChecked(bool state);
  155. void UpdateUITheme();
  156. explicit GMainWindow(std::unique_ptr<QtConfig> config_, bool has_broken_vulkan);
  157. ~GMainWindow() override;
  158. bool DropAction(QDropEvent* event);
  159. void AcceptDropEvent(QDropEvent* event);
  160. signals:
  161. /**
  162. * Signal that is emitted when a new EmuThread has been created and an emulation session is
  163. * about to start. At this time, the core system emulation has been initialized, and all
  164. * emulation handles and memory should be valid.
  165. *
  166. * @param emu_thread Pointer to the newly created EmuThread (to be used by widgets that need to
  167. * access/change emulation state).
  168. */
  169. void EmulationStarting(EmuThread* emu_thread);
  170. /**
  171. * Signal that is emitted when emulation is about to stop. At this time, the EmuThread and core
  172. * system emulation handles and memory are still valid, but are about become invalid.
  173. */
  174. void EmulationStopping();
  175. // Signal that tells widgets to update icons to use the current theme
  176. void UpdateThemedIcons();
  177. void UpdateInstallProgress();
  178. void AmiiboSettingsFinished(bool is_success, const std::string& name);
  179. void ControllerSelectorReconfigureFinished(bool is_success);
  180. void ErrorDisplayFinished();
  181. void ProfileSelectorFinishedSelection(std::optional<Common::UUID> uuid);
  182. void SoftwareKeyboardSubmitNormalText(Service::AM::Applets::SwkbdResult result,
  183. std::u16string submitted_text, bool confirmed);
  184. void SoftwareKeyboardSubmitInlineText(Service::AM::Applets::SwkbdReplyType reply_type,
  185. std::u16string submitted_text, s32 cursor_position);
  186. void WebBrowserExtractOfflineRomFS();
  187. void WebBrowserClosed(Service::AM::Applets::WebExitReason exit_reason, std::string last_url);
  188. void SigInterrupt();
  189. public slots:
  190. void OnLoadComplete();
  191. void OnExecuteProgram(std::size_t program_index);
  192. void OnExit();
  193. void OnSaveConfig();
  194. void AmiiboSettingsShowDialog(const Core::Frontend::CabinetParameters& parameters,
  195. std::shared_ptr<Service::NFC::NfcDevice> nfp_device);
  196. void AmiiboSettingsRequestExit();
  197. void ControllerSelectorReconfigureControllers(
  198. const Core::Frontend::ControllerParameters& parameters);
  199. void ControllerSelectorRequestExit();
  200. void SoftwareKeyboardInitialize(
  201. bool is_inline, Core::Frontend::KeyboardInitializeParameters initialize_parameters);
  202. void SoftwareKeyboardShowNormal();
  203. void SoftwareKeyboardShowTextCheck(Service::AM::Applets::SwkbdTextCheckResult text_check_result,
  204. std::u16string text_check_message);
  205. void SoftwareKeyboardShowInline(Core::Frontend::InlineAppearParameters appear_parameters);
  206. void SoftwareKeyboardHideInline();
  207. void SoftwareKeyboardInlineTextChanged(Core::Frontend::InlineTextParameters text_parameters);
  208. void SoftwareKeyboardExit();
  209. void ErrorDisplayDisplayError(QString error_code, QString error_text);
  210. void ErrorDisplayRequestExit();
  211. void ProfileSelectorSelectProfile(const Core::Frontend::ProfileSelectParameters& parameters);
  212. void ProfileSelectorRequestExit();
  213. void WebBrowserOpenWebPage(const std::string& main_url, const std::string& additional_args,
  214. bool is_local);
  215. void WebBrowserRequestExit();
  216. void OnAppFocusStateChanged(Qt::ApplicationState state);
  217. void OnTasStateChanged();
  218. private:
  219. /// Updates an action's shortcut and text to reflect an updated hotkey from the hotkey registry.
  220. void LinkActionShortcut(QAction* action, const QString& action_name,
  221. const bool tas_allowed = false);
  222. void RegisterMetaTypes();
  223. void InitializeWidgets();
  224. void InitializeDebugWidgets();
  225. void InitializeRecentFileMenuActions();
  226. void SetDefaultUIGeometry();
  227. void RestoreUIState();
  228. void ConnectWidgetEvents();
  229. void ConnectMenuEvents();
  230. void UpdateMenuState();
  231. void SetupPrepareForSleep();
  232. void PreventOSSleep();
  233. void AllowOSSleep();
  234. bool LoadROM(const QString& filename, u64 program_id, std::size_t program_index,
  235. AmLaunchType launch_type);
  236. void BootGame(const QString& filename, u64 program_id = 0, std::size_t program_index = 0,
  237. StartGameType with_config = StartGameType::Normal,
  238. AmLaunchType launch_type = AmLaunchType::UserInitiated);
  239. void ShutdownGame();
  240. void ShowTelemetryCallout();
  241. void SetDiscordEnabled(bool state);
  242. void LoadAmiibo(const QString& filename);
  243. bool SelectAndSetCurrentUser(const Core::Frontend::ProfileSelectParameters& parameters);
  244. /**
  245. * Stores the filename in the recently loaded files list.
  246. * The new filename is stored at the beginning of the recently loaded files list.
  247. * After inserting the new entry, duplicates are removed meaning that if
  248. * this was inserted from \a OnMenuRecentFile(), the entry will be put on top
  249. * and remove from its previous position.
  250. *
  251. * Finally, this function calls \a UpdateRecentFiles() to update the UI.
  252. *
  253. * @param filename the filename to store
  254. */
  255. void StoreRecentFile(const QString& filename);
  256. /**
  257. * Updates the recent files menu.
  258. * Menu entries are rebuilt from the configuration file.
  259. * If there is no entry in the menu, the menu is greyed out.
  260. */
  261. void UpdateRecentFiles();
  262. /**
  263. * If the emulation is running,
  264. * asks the user if he really want to close the emulator
  265. *
  266. * @return true if the user confirmed
  267. */
  268. bool ConfirmClose();
  269. bool ConfirmChangeGame();
  270. bool ConfirmForceLockedExit();
  271. void RequestGameExit();
  272. void RequestGameResume();
  273. void changeEvent(QEvent* event) override;
  274. void closeEvent(QCloseEvent* event) override;
  275. std::string CreateTASFramesString(
  276. std::array<size_t, InputCommon::TasInput::PLAYER_NUMBER> frames) const;
  277. #ifdef __unix__
  278. void SetupSigInterrupts();
  279. static void HandleSigInterrupt(int);
  280. void OnSigInterruptNotifierActivated();
  281. void SetGamemodeEnabled(bool state);
  282. #endif
  283. private slots:
  284. void OnStartGame();
  285. void OnRestartGame();
  286. void OnPauseGame();
  287. void OnPauseContinueGame();
  288. void OnStopGame();
  289. void OnPrepareForSleep(bool prepare_sleep);
  290. void OnMenuReportCompatibility();
  291. void OnOpenModsPage();
  292. void OnOpenQuickstartGuide();
  293. void OnOpenFAQ();
  294. /// Called whenever a user selects a game in the game list widget.
  295. void OnGameListLoadFile(QString game_path, u64 program_id);
  296. void OnGameListOpenFolder(u64 program_id, GameListOpenTarget target,
  297. const std::string& game_path);
  298. void OnTransferableShaderCacheOpenFile(u64 program_id);
  299. void OnGameListRemoveInstalledEntry(u64 program_id, InstalledEntryType type);
  300. void OnGameListRemoveFile(u64 program_id, GameListRemoveTarget target,
  301. const std::string& game_path);
  302. void OnGameListRemovePlayTimeData(u64 program_id);
  303. void OnGameListDumpRomFS(u64 program_id, const std::string& game_path, DumpRomFSTarget target);
  304. void OnGameListVerifyIntegrity(const std::string& game_path);
  305. void OnGameListCopyTID(u64 program_id);
  306. void OnGameListNavigateToGamedbEntry(u64 program_id,
  307. const CompatibilityList& compatibility_list);
  308. void OnGameListCreateShortcut(u64 program_id, const std::string& game_path,
  309. GameListShortcutTarget target);
  310. void OnGameListOpenDirectory(const QString& directory);
  311. void OnGameListAddDirectory();
  312. void OnGameListShowList(bool show);
  313. void OnGameListOpenPerGameProperties(const std::string& file);
  314. void OnMenuLoadFile();
  315. void OnMenuLoadFolder();
  316. void IncrementInstallProgress();
  317. void OnMenuInstallToNAND();
  318. void OnMenuRecentFile();
  319. void OnConfigure();
  320. void OnConfigureTas();
  321. void OnDecreaseVolume();
  322. void OnIncreaseVolume();
  323. void OnMute();
  324. void OnTasStartStop();
  325. void OnTasRecord();
  326. void OnTasReset();
  327. void OnToggleGraphicsAPI();
  328. void OnToggleDockedMode();
  329. void OnToggleGpuAccuracy();
  330. void OnToggleAdaptingFilter();
  331. void OnConfigurePerGame();
  332. void OnLoadAmiibo();
  333. void OnOpenYuzuFolder();
  334. void OnVerifyInstalledContents();
  335. void OnAbout();
  336. void OnToggleFilterBar();
  337. void OnToggleStatusBar();
  338. void OnDisplayTitleBars(bool);
  339. void InitializeHotkeys();
  340. void ToggleFullscreen();
  341. bool UsingExclusiveFullscreen();
  342. void ShowFullscreen();
  343. void HideFullscreen();
  344. void ToggleWindowMode();
  345. void ResetWindowSize(u32 width, u32 height);
  346. void ResetWindowSize720();
  347. void ResetWindowSize900();
  348. void ResetWindowSize1080();
  349. void OnAlbum();
  350. void OnCabinet(Service::NFP::CabinetMode mode);
  351. void OnMiiEdit();
  352. void OnOpenControllerMenu();
  353. void OnCaptureScreenshot();
  354. void OnReinitializeKeys(ReinitializeKeyBehavior behavior);
  355. void OnLanguageChanged(const QString& locale);
  356. void OnMouseActivity();
  357. bool OnShutdownBegin();
  358. void OnShutdownBeginDialog();
  359. void OnEmulationStopped();
  360. void OnEmulationStopTimeExpired();
  361. private:
  362. QString GetGameListErrorRemoving(InstalledEntryType type) const;
  363. void RemoveBaseContent(u64 program_id, InstalledEntryType type);
  364. void RemoveUpdateContent(u64 program_id, InstalledEntryType type);
  365. void RemoveAddOnContent(u64 program_id, InstalledEntryType type);
  366. void RemoveTransferableShaderCache(u64 program_id, GameListRemoveTarget target);
  367. void RemoveVulkanDriverPipelineCache(u64 program_id);
  368. void RemoveAllTransferableShaderCaches(u64 program_id);
  369. void RemoveCustomConfiguration(u64 program_id, const std::string& game_path);
  370. void RemovePlayTimeData(u64 program_id);
  371. void RemoveCacheStorage(u64 program_id);
  372. bool SelectRomFSDumpTarget(const FileSys::ContentProvider&, u64 program_id,
  373. u64* selected_title_id, u8* selected_content_record_type);
  374. InstallResult InstallNSP(const QString& filename);
  375. InstallResult InstallNCA(const QString& filename);
  376. void MigrateConfigFiles();
  377. void UpdateWindowTitle(std::string_view title_name = {}, std::string_view title_version = {},
  378. std::string_view gpu_vendor = {});
  379. void UpdateDockedButton();
  380. void UpdateAPIText();
  381. void UpdateFilterText();
  382. void UpdateAAText();
  383. void UpdateVolumeUI();
  384. void UpdateStatusBar();
  385. void UpdateGPUAccuracyButton();
  386. void UpdateStatusButtons();
  387. void UpdateUISettings();
  388. void UpdateInputDrivers();
  389. void HideMouseCursor();
  390. void ShowMouseCursor();
  391. void OpenURL(const QUrl& url);
  392. void LoadTranslation();
  393. void OpenPerGameConfiguration(u64 title_id, const std::string& file_name);
  394. bool CheckDarkMode();
  395. bool CheckSystemArchiveDecryption();
  396. bool CheckFirmwarePresence();
  397. void SetFirmwareVersion();
  398. void ConfigureFilesystemProvider(const std::string& filepath);
  399. /**
  400. * Open (or not) the right confirm dialog based on current setting and game exit lock
  401. * @returns true if the player confirmed or the settings do no require it
  402. */
  403. bool ConfirmShutdownGame();
  404. QString GetTasStateDescription() const;
  405. bool CreateShortcutMessagesGUI(QWidget* parent, int imsg, const QString& game_title);
  406. bool MakeShortcutIcoPath(const u64 program_id, const std::string_view game_file_name,
  407. std::filesystem::path& out_icon_path);
  408. bool CreateShortcutLink(const std::filesystem::path& shortcut_path, const std::string& comment,
  409. const std::filesystem::path& icon_path,
  410. const std::filesystem::path& command, const std::string& arguments,
  411. const std::string& categories, const std::string& keywords,
  412. const std::string& name);
  413. /**
  414. * Mimic the behavior of QMessageBox::question but link controller navigation to the dialog
  415. * The only difference is that it returns a boolean.
  416. *
  417. * @returns true if buttons contains QMessageBox::Yes and the user clicks on the "Yes" button.
  418. */
  419. bool question(QWidget* parent, const QString& title, const QString& text,
  420. QMessageBox::StandardButtons buttons =
  421. QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No),
  422. QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
  423. std::unique_ptr<Ui::MainWindow> ui;
  424. std::unique_ptr<Core::System> system;
  425. std::unique_ptr<DiscordRPC::DiscordInterface> discord_rpc;
  426. std::unique_ptr<PlayTime::PlayTimeManager> play_time_manager;
  427. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem;
  428. MultiplayerState* multiplayer_state = nullptr;
  429. GRenderWindow* render_window;
  430. GameList* game_list;
  431. LoadingScreen* loading_screen;
  432. QTimer shutdown_timer;
  433. OverlayDialog* shutdown_dialog{};
  434. GameListPlaceholder* game_list_placeholder;
  435. std::vector<VkDeviceInfo::Record> vk_device_records;
  436. // Status bar elements
  437. QLabel* message_label = nullptr;
  438. QLabel* shader_building_label = nullptr;
  439. QLabel* res_scale_label = nullptr;
  440. QLabel* emu_speed_label = nullptr;
  441. QLabel* game_fps_label = nullptr;
  442. QLabel* emu_frametime_label = nullptr;
  443. QLabel* tas_label = nullptr;
  444. QLabel* firmware_label = nullptr;
  445. QPushButton* gpu_accuracy_button = nullptr;
  446. QPushButton* renderer_status_button = nullptr;
  447. QPushButton* dock_status_button = nullptr;
  448. QPushButton* filter_status_button = nullptr;
  449. QPushButton* aa_status_button = nullptr;
  450. VolumeButton* volume_button = nullptr;
  451. QWidget* volume_popup = nullptr;
  452. QSlider* volume_slider = nullptr;
  453. QTimer status_bar_update_timer;
  454. std::unique_ptr<QtConfig> config;
  455. // Whether emulation is currently running in yuzu.
  456. bool emulation_running = false;
  457. std::unique_ptr<EmuThread> emu_thread;
  458. // The path to the game currently running
  459. QString current_game_path;
  460. bool auto_paused = false;
  461. bool auto_muted = false;
  462. QTimer mouse_hide_timer;
  463. QTimer update_input_timer;
  464. QString startup_icon_theme;
  465. bool os_dark_mode = false;
  466. // FS
  467. std::shared_ptr<FileSys::VfsFilesystem> vfs;
  468. std::unique_ptr<FileSys::ManualContentProvider> provider;
  469. // Debugger panes
  470. ProfilerWidget* profilerWidget;
  471. MicroProfileDialog* microProfileDialog;
  472. WaitTreeWidget* waitTreeWidget;
  473. ControllerDialog* controller_dialog;
  474. QAction* actions_recent_files[max_recent_files_item];
  475. // stores default icon theme search paths for the platform
  476. QStringList default_theme_paths;
  477. HotkeyRegistry hotkey_registry;
  478. QTranslator translator;
  479. // Install progress dialog
  480. QProgressDialog* install_progress;
  481. // Last game booted, used for multi-process apps
  482. QString last_filename_booted;
  483. // Applets
  484. QtAmiiboSettingsDialog* cabinet_applet = nullptr;
  485. QtControllerSelectorDialog* controller_applet = nullptr;
  486. QtProfileSelectionDialog* profile_select_applet = nullptr;
  487. QDialog* error_applet = nullptr;
  488. QtSoftwareKeyboardDialog* software_keyboard = nullptr;
  489. QtNXWebEngineView* web_applet = nullptr;
  490. // True if amiibo file select is visible
  491. bool is_amiibo_file_select_active{};
  492. // True if load file select is visible
  493. bool is_load_file_select_active{};
  494. // True if TAS recording dialog is visible
  495. bool is_tas_recording_dialog_active{};
  496. #ifdef __unix__
  497. QSocketNotifier* sig_interrupt_notifier;
  498. static std::array<int, 3> sig_interrupt_fds;
  499. #endif
  500. protected:
  501. void dropEvent(QDropEvent* event) override;
  502. void dragEnterEvent(QDragEnterEvent* event) override;
  503. void dragMoveEvent(QDragMoveEvent* event) override;
  504. };