main.h 18 KB

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