main.h 16 KB

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