main.h 19 KB

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