2024-03-31 14:44:57 +00:00
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
2024-07-30 11:42:36 +00:00
// SPDX-License-Identifier: GPL-3.0+
2021-12-13 12:12:54 +00:00
2023-05-13 04:30:41 +00:00
# include "AutoUpdaterDialog.h"
# include "DisplayWidget.h"
# include "GameList/GameListWidget.h"
2024-01-11 09:38:28 +00:00
# include "LogWindow.h"
2023-05-13 04:30:41 +00:00
# include "MainWindow.h"
# include "QtHost.h"
2024-01-07 05:26:56 +00:00
# include "QtProgressCallback.h"
2023-05-13 04:30:41 +00:00
# include "QtUtils.h"
2023-06-23 13:18:10 +00:00
# include "SetupWizardDialog.h"
2023-05-13 04:30:41 +00:00
# include "svnrev.h"
2022-09-07 09:20:50 +00:00
# include "pcsx2/CDVD/CDVDcommon.h"
2023-05-13 04:30:41 +00:00
# include "pcsx2/Achievements.h"
# include "pcsx2/CDVD/CDVD.h"
2022-09-07 09:20:50 +00:00
# include "pcsx2/Counters.h"
# include "pcsx2/DebugTools/Debug.h"
# include "pcsx2/GS.h"
# include "pcsx2/GS/GS.h"
# include "pcsx2/GSDumpReplayer.h"
2023-05-13 04:30:41 +00:00
# include "pcsx2/GameList.h"
# include "pcsx2/Host.h"
2022-09-14 12:41:50 +00:00
# include "pcsx2/INISettingsInterface.h"
2023-05-13 04:30:41 +00:00
# include "pcsx2/ImGui/FullscreenUI.h"
2024-05-14 21:25:06 +00:00
# include "pcsx2/ImGui/ImGuiFullscreen.h"
2023-05-13 04:30:41 +00:00
# include "pcsx2/ImGui/ImGuiManager.h"
2023-12-14 11:44:32 +00:00
# include "pcsx2/ImGui/ImGuiOverlays.h"
2023-05-13 04:30:41 +00:00
# include "pcsx2/Input/InputManager.h"
2023-06-24 07:46:36 +00:00
# include "pcsx2/MTGS.h"
2022-09-07 09:20:50 +00:00
# include "pcsx2/PerformanceMetrics.h"
2024-04-23 15:17:52 +00:00
# include "pcsx2/SPU2/spu2.h"
2022-09-07 09:20:50 +00:00
# include "pcsx2/VMManager.h"
2023-05-13 04:30:41 +00:00
# include "common/Assertions.h"
# include "common/Console.h"
# include "common/CrashHandler.h"
2024-04-01 11:15:52 +00:00
# include "common/Error.h"
2023-05-13 04:30:41 +00:00
# include "common/FileSystem.h"
2024-01-07 05:26:56 +00:00
# include "common/HTTPDownloader.h"
2023-05-13 04:30:41 +00:00
# include "common/Path.h"
# include "common/SettingsWrapper.h"
# include "common/StringUtil.h"
# include "common/Timer.h"
2022-03-04 10:40:03 +00:00
# include <QtCore/QTimer>
2022-07-09 09:07:28 +00:00
# include <QtWidgets/QApplication>
2022-03-04 10:40:03 +00:00
# include <QtWidgets/QMessageBox>
2024-05-14 21:25:06 +00:00
# include <QtWidgets/QFileDialog>
2022-08-20 10:20:36 +00:00
# include <QtGui/QClipboard>
2022-09-03 12:29:36 +00:00
# include <QtGui/QInputMethod>
2022-03-04 10:40:03 +00:00
2022-09-17 12:47:35 +00:00
# include "fmt/core.h"
2023-05-13 04:30:41 +00:00
# include <cmath>
# include <csignal>
2022-04-18 13:35:14 +00:00
2022-09-07 09:20:50 +00:00
static constexpr u32 SETTINGS_SAVE_DELAY = 1000 ;
2024-01-07 05:26:56 +00:00
static constexpr const char * RUNTIME_RESOURCES_URL =
" https://github.com/PCSX2/pcsx2-windows-dependencies/releases/download/runtime-resources/ " ;
2022-09-07 09:20:50 +00:00
EmuThread * g_emu_thread = nullptr ;
//////////////////////////////////////////////////////////////////////////
// Local function declarations
//////////////////////////////////////////////////////////////////////////
2022-12-04 08:03:30 +00:00
namespace QtHost
{
2024-01-11 08:08:16 +00:00
static void InitializeEarlyConsole ( ) ;
2022-12-04 08:03:30 +00:00
static void PrintCommandLineVersion ( ) ;
2024-05-14 23:42:40 +00:00
static void PrintCommandLineHelp ( const std : : string_view progname ) ;
2022-12-04 08:03:30 +00:00
static std : : shared_ptr < VMBootParameters > & AutoBoot ( std : : shared_ptr < VMBootParameters > & autoboot ) ;
static bool ParseCommandLineOptions ( const QStringList & args , std : : shared_ptr < VMBootParameters > & autoboot ) ;
static bool InitializeConfig ( ) ;
static void SaveSettings ( ) ;
static void HookSignals ( ) ;
2023-06-23 13:18:10 +00:00
static void RegisterTypes ( ) ;
static bool RunSetupWizard ( ) ;
2024-01-07 05:26:56 +00:00
static std : : optional < bool > DownloadFile ( QWidget * parent , const QString & title , std : : string url , std : : vector < u8 > * data ) ;
2022-12-04 08:03:30 +00:00
} // namespace QtHost
2022-09-07 09:20:50 +00:00
//////////////////////////////////////////////////////////////////////////
// Local variable declarations
//////////////////////////////////////////////////////////////////////////
static std : : unique_ptr < QTimer > s_settings_save_timer ;
static std : : unique_ptr < INISettingsInterface > s_base_settings_interface ;
static bool s_batch_mode = false ;
static bool s_nogui_mode = false ;
static bool s_start_fullscreen_ui = false ;
static bool s_start_fullscreen_ui_fullscreen = false ;
2022-11-23 11:21:20 +00:00
static bool s_test_config_and_exit = false ;
2023-06-23 13:18:10 +00:00
static bool s_run_setup_wizard = false ;
2024-01-14 07:09:49 +00:00
static bool s_cleanup_after_update = false ;
2023-02-01 11:53:31 +00:00
static bool s_boot_and_debug = false ;
2022-09-07 09:20:50 +00:00
//////////////////////////////////////////////////////////////////////////
// CPU Thread
//////////////////////////////////////////////////////////////////////////
EmuThread : : EmuThread ( QThread * ui_thread )
: QThread ( )
, m_ui_thread ( ui_thread )
{
}
EmuThread : : ~ EmuThread ( ) = default ;
void EmuThread : : start ( )
{
pxAssertRel ( ! g_emu_thread , " Emu thread does not exist " ) ;
g_emu_thread = new EmuThread ( QThread : : currentThread ( ) ) ;
2023-05-25 02:14:16 +00:00
g_emu_thread - > setStackSize ( VMManager : : EMU_THREAD_STACK_SIZE ) ;
2022-09-07 09:20:50 +00:00
g_emu_thread - > QThread : : start ( ) ;
g_emu_thread - > m_started_semaphore . acquire ( ) ;
g_emu_thread - > moveToThread ( g_emu_thread ) ;
}
void EmuThread : : stop ( )
{
pxAssertRel ( g_emu_thread , " Emu thread exists " ) ;
pxAssertRel ( ! g_emu_thread - > isOnEmuThread ( ) , " Not called on the emu thread " ) ;
QMetaObject : : invokeMethod ( g_emu_thread , & EmuThread : : stopInThread , Qt : : QueuedConnection ) ;
while ( g_emu_thread - > isRunning ( ) )
QApplication : : processEvents ( QEventLoop : : ExcludeUserInputEvents , 1 ) ;
}
void EmuThread : : stopInThread ( )
{
if ( VMManager : : HasValidVM ( ) )
destroyVM ( ) ;
if ( m_run_fullscreen_ui )
stopFullscreenUI ( ) ;
m_event_loop - > quit ( ) ;
m_shutdown_flag . store ( true ) ;
}
bool EmuThread : : confirmMessage ( const QString & title , const QString & message )
{
if ( ! isOnEmuThread ( ) )
{
// This is definitely deadlock risky, but unlikely to happen (why would GS be confirming?).
bool result = false ;
2022-12-04 08:03:30 +00:00
QMetaObject : : invokeMethod ( g_emu_thread , " confirmMessage " , Qt : : BlockingQueuedConnection , Q_RETURN_ARG ( bool , result ) ,
Q_ARG ( const QString & , title ) , Q_ARG ( const QString & , message ) ) ;
2022-09-07 09:20:50 +00:00
return result ;
}
// Easy if there's no VM.
if ( ! VMManager : : HasValidVM ( ) )
return emit messageConfirmed ( title , message ) ;
// Preemptively pause/set surfaceless on the emu thread, because it can't run while the popup is open.
const bool was_paused = ( VMManager : : GetState ( ) = = VMState : : Paused ) ;
const bool was_fullscreen = isFullscreen ( ) ;
if ( ! was_paused )
VMManager : : SetPaused ( true ) ;
if ( was_fullscreen )
setSurfaceless ( true ) ;
// This won't return until the user confirms one way or another.
const bool result = emit messageConfirmed ( title , message ) ;
// Resume VM after confirming.
if ( was_fullscreen )
setSurfaceless ( false ) ;
if ( ! was_paused )
VMManager : : SetPaused ( false ) ;
return result ;
}
void EmuThread : : startFullscreenUI ( bool fullscreen )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " startFullscreenUI " , Qt : : QueuedConnection , Q_ARG ( bool , fullscreen ) ) ;
return ;
}
2023-06-24 07:46:36 +00:00
if ( VMManager : : HasValidVM ( ) | | MTGS : : IsOpen ( ) )
2022-09-07 09:20:50 +00:00
return ;
2023-04-01 05:54:04 +00:00
// this should just set the flag so it gets automatically started
ImGuiManager : : InitializeFullscreenUI ( ) ;
2024-03-31 10:39:16 +00:00
m_run_fullscreen_ui . store ( true , std : : memory_order_release ) ;
2023-04-25 12:52:41 +00:00
m_is_rendering_to_main = shouldRenderToMain ( ) ;
m_is_fullscreen = fullscreen ;
2022-09-07 09:20:50 +00:00
2023-06-24 07:46:36 +00:00
if ( ! MTGS : : WaitForOpen ( ) )
2022-09-07 09:20:50 +00:00
{
2024-03-31 10:39:16 +00:00
m_run_fullscreen_ui . store ( false , std : : memory_order_release ) ;
2022-09-07 09:20:50 +00:00
return ;
}
2023-09-04 11:34:57 +00:00
emit onFullscreenUIStateChange ( true ) ;
2022-09-07 09:20:50 +00:00
// poll more frequently so we don't lose events
stopBackgroundControllerPollTimer ( ) ;
startBackgroundControllerPollTimer ( ) ;
}
void EmuThread : : stopFullscreenUI ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : stopFullscreenUI , Qt : : QueuedConnection ) ;
// wait until the host display is gone
2024-03-31 10:39:16 +00:00
// have to test the bool, because MTGS::IsOpen() goes false as soon as the close request happens.
while ( m_run_fullscreen_ui . load ( std : : memory_order_acquire ) | | ( ! QtHost : : IsVMValid ( ) & & MTGS : : IsOpen ( ) ) )
2022-09-07 09:20:50 +00:00
QApplication : : processEvents ( QEventLoop : : ExcludeUserInputEvents , 1 ) ;
return ;
}
2024-04-12 14:53:58 +00:00
setFullscreen ( false , true ) ;
2024-03-31 10:39:16 +00:00
if ( MTGS : : IsOpen ( ) & & ! VMManager : : HasValidVM ( ) )
MTGS : : WaitForClose ( ) ;
if ( m_run_fullscreen_ui . load ( std : : memory_order_acquire ) )
2023-09-04 11:34:57 +00:00
{
2024-03-31 10:39:16 +00:00
m_run_fullscreen_ui . store ( false , std : : memory_order_release ) ;
2023-09-04 11:34:57 +00:00
emit onFullscreenUIStateChange ( false ) ;
}
2022-09-07 09:20:50 +00:00
}
void EmuThread : : startVM ( std : : shared_ptr < VMBootParameters > boot_params )
{
if ( ! isOnEmuThread ( ) )
{
2022-12-04 08:03:30 +00:00
QMetaObject : : invokeMethod ( this , " startVM " , Qt : : QueuedConnection , Q_ARG ( std : : shared_ptr < VMBootParameters > , boot_params ) ) ;
2022-09-07 09:20:50 +00:00
return ;
}
pxAssertRel ( ! VMManager : : HasValidVM ( ) , " VM is shut down " ) ;
2022-09-14 12:41:50 +00:00
// Determine whether to start fullscreen or not.
2023-04-25 12:52:41 +00:00
m_is_rendering_to_main = shouldRenderToMain ( ) ;
2022-09-07 09:20:50 +00:00
if ( boot_params - > fullscreen . has_value ( ) )
m_is_fullscreen = boot_params - > fullscreen . value ( ) ;
2022-09-14 12:41:50 +00:00
else
m_is_fullscreen = Host : : GetBaseBoolSettingValue ( " UI " , " StartFullscreen " , false ) ;
2022-09-07 09:20:50 +00:00
if ( ! VMManager : : Initialize ( * boot_params ) )
return ;
if ( ! Host : : GetBoolSettingValue ( " UI " , " StartPaused " , false ) )
{
// This will come back and call OnVMResumed().
VMManager : : SetState ( VMState : : Running ) ;
}
else
{
// When starting paused, redraw the window, so there's at least something there.
redrawDisplayWindow ( ) ;
Host : : OnVMPaused ( ) ;
}
m_event_loop - > quit ( ) ;
}
void EmuThread : : resetVM ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : resetVM , Qt : : QueuedConnection ) ;
return ;
}
VMManager : : Reset ( ) ;
}
void EmuThread : : setVMPaused ( bool paused )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " setVMPaused " , Qt : : QueuedConnection , Q_ARG ( bool , paused ) ) ;
return ;
}
VMManager : : SetPaused ( paused ) ;
}
void EmuThread : : shutdownVM ( bool save_state /* = true */ )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " shutdownVM " , Qt : : QueuedConnection , Q_ARG ( bool , save_state ) ) ;
return ;
}
const VMState state = VMManager : : GetState ( ) ;
if ( state = = VMState : : Paused )
m_event_loop - > quit ( ) ;
else if ( state ! = VMState : : Running )
return ;
m_save_state_on_shutdown = save_state ;
VMManager : : SetState ( VMState : : Stopping ) ;
}
void EmuThread : : loadState ( const QString & filename )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " loadState " , Qt : : QueuedConnection , Q_ARG ( const QString & , filename ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
VMManager : : LoadState ( filename . toUtf8 ( ) . constData ( ) ) ;
}
void EmuThread : : loadStateFromSlot ( qint32 slot )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " loadStateFromSlot " , Qt : : QueuedConnection , Q_ARG ( qint32 , slot ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
VMManager : : LoadStateFromSlot ( slot ) ;
}
void EmuThread : : saveState ( const QString & filename )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " saveState " , Qt : : QueuedConnection , Q_ARG ( const QString & , filename ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
if ( ! VMManager : : SaveState ( filename . toUtf8 ( ) . constData ( ) ) )
{
// this one is usually the result of a user-chosen path, so we can display a message box safely here
Console . Error ( " Failed to save state " ) ;
}
}
void EmuThread : : saveStateToSlot ( qint32 slot )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " saveStateToSlot " , Qt : : QueuedConnection , Q_ARG ( qint32 , slot ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
VMManager : : SaveStateToSlot ( slot ) ;
}
void EmuThread : : run ( )
{
2022-09-14 12:41:50 +00:00
// Qt-specific initialization.
2022-09-07 09:20:50 +00:00
m_event_loop = new QEventLoop ( ) ;
m_started_semaphore . release ( ) ;
2022-09-14 12:41:50 +00:00
connectSignals ( ) ;
2022-09-07 09:20:50 +00:00
2022-09-14 12:41:50 +00:00
// Common host initialization (VM setup, etc).
2023-05-13 10:22:46 +00:00
if ( ! VMManager : : Internal : : CPUThreadInitialize ( ) )
{
VMManager : : Internal : : CPUThreadShutdown ( ) ;
QMetaObject : : invokeMethod ( qApp , & QCoreApplication : : quit , Qt : : QueuedConnection ) ;
return ;
}
2022-09-07 09:20:50 +00:00
// Start background polling because the VM won't do it for us.
createBackgroundControllerPollTimer ( ) ;
startBackgroundControllerPollTimer ( ) ;
2022-09-14 12:41:50 +00:00
// Main CPU thread loop.
2022-09-07 09:20:50 +00:00
while ( ! m_shutdown_flag . load ( ) )
{
switch ( VMManager : : GetState ( ) )
{
case VMState : : Initializing :
2023-11-26 15:23:02 +00:00
pxFailRel ( " Shouldn't be in the starting state " ) ;
2022-09-07 09:20:50 +00:00
continue ;
2023-11-26 15:23:02 +00:00
case VMState : : Shutdown :
2022-09-07 09:20:50 +00:00
case VMState : : Paused :
m_event_loop - > exec ( ) ;
continue ;
case VMState : : Running :
m_event_loop - > processEvents ( QEventLoop : : AllEvents ) ;
VMManager : : Execute ( ) ;
continue ;
2023-01-25 12:02:19 +00:00
case VMState : : Resetting :
VMManager : : Reset ( ) ;
continue ;
2022-09-07 09:20:50 +00:00
case VMState : : Stopping :
destroyVM ( ) ;
2023-11-26 15:23:02 +00:00
continue ;
2022-09-07 09:20:50 +00:00
default :
continue ;
}
}
2023-11-26 15:23:02 +00:00
// Teardown in reverse order.
stopBackgroundControllerPollTimer ( ) ;
destroyBackgroundControllerPollTimer ( ) ;
VMManager : : Internal : : CPUThreadShutdown ( ) ;
// Move back to the UI thread, since we're no longer running.
moveToThread ( m_ui_thread ) ;
deleteLater ( ) ;
}
void EmuThread : : destroyVM ( )
{
m_last_speed = 0.0f ;
m_last_game_fps = 0.0f ;
m_last_video_fps = 0.0f ;
m_last_internal_width = 0 ;
m_last_internal_height = 0 ;
m_was_paused_by_focus_loss = false ;
VMManager : : Shutdown ( m_save_state_on_shutdown ) ;
m_save_state_on_shutdown = false ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : createBackgroundControllerPollTimer ( )
{
pxAssert ( ! m_background_controller_polling_timer ) ;
m_background_controller_polling_timer = new QTimer ( this ) ;
m_background_controller_polling_timer - > setSingleShot ( false ) ;
m_background_controller_polling_timer - > setTimerType ( Qt : : CoarseTimer ) ;
connect ( m_background_controller_polling_timer , & QTimer : : timeout , this , & EmuThread : : doBackgroundControllerPoll ) ;
}
void EmuThread : : destroyBackgroundControllerPollTimer ( )
{
delete m_background_controller_polling_timer ;
m_background_controller_polling_timer = nullptr ;
}
void EmuThread : : startBackgroundControllerPollTimer ( )
{
if ( m_background_controller_polling_timer - > isActive ( ) )
return ;
2022-12-04 08:03:30 +00:00
m_background_controller_polling_timer - > start (
FullscreenUI : : IsInitialized ( ) ? FULLSCREEN_UI_CONTROLLER_POLLING_INTERVAL : BACKGROUND_CONTROLLER_POLLING_INTERVAL ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : stopBackgroundControllerPollTimer ( )
{
if ( ! m_background_controller_polling_timer - > isActive ( ) )
return ;
m_background_controller_polling_timer - > stop ( ) ;
}
void EmuThread : : doBackgroundControllerPoll ( )
{
2023-09-17 10:19:51 +00:00
VMManager : : IdlePollUpdate ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : toggleFullscreen ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : toggleFullscreen , Qt : : QueuedConnection ) ;
return ;
}
2023-04-25 12:52:41 +00:00
setFullscreen ( ! m_is_fullscreen , true ) ;
2022-09-07 09:20:50 +00:00
}
2023-04-25 12:52:41 +00:00
void EmuThread : : setFullscreen ( bool fullscreen , bool allow_render_to_main )
2022-09-07 09:20:50 +00:00
{
if ( ! isOnEmuThread ( ) )
{
2023-04-25 12:52:41 +00:00
QMetaObject : : invokeMethod ( this , " setFullscreen " , Qt : : QueuedConnection , Q_ARG ( bool , fullscreen ) , Q_ARG ( bool , allow_render_to_main ) ) ;
2022-09-07 09:20:50 +00:00
return ;
}
2023-06-24 07:46:36 +00:00
if ( ! MTGS : : IsOpen ( ) | | m_is_fullscreen = = fullscreen )
2022-09-07 09:20:50 +00:00
return ;
// This will call back to us on the MTGS thread.
m_is_fullscreen = fullscreen ;
2023-04-25 12:52:41 +00:00
m_is_rendering_to_main = allow_render_to_main & & shouldRenderToMain ( ) ;
2023-06-24 07:46:36 +00:00
MTGS : : UpdateDisplayWindow ( ) ;
MTGS : : WaitGS ( ) ;
2022-09-07 09:20:50 +00:00
// If we're using exclusive fullscreen, the refresh rate may have changed.
2024-01-12 11:41:54 +00:00
VMManager : : UpdateTargetSpeed ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : setSurfaceless ( bool surfaceless )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " setSurfaceless " , Qt : : QueuedConnection , Q_ARG ( bool , surfaceless ) ) ;
return ;
}
2023-06-24 07:46:36 +00:00
if ( ! MTGS : : IsOpen ( ) | | m_is_surfaceless = = surfaceless )
2022-09-07 09:20:50 +00:00
return ;
// This will call back to us on the MTGS thread.
m_is_surfaceless = surfaceless ;
2023-06-24 07:46:36 +00:00
MTGS : : UpdateDisplayWindow ( ) ;
MTGS : : WaitGS ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : applySettings ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : applySettings , Qt : : QueuedConnection ) ;
return ;
}
VMManager : : ApplySettings ( ) ;
}
void EmuThread : : reloadGameSettings ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : reloadGameSettings , Qt : : QueuedConnection ) ;
return ;
}
// this will skip applying settings when they're not active
2022-09-14 12:41:50 +00:00
VMManager : : ReloadGameSettings ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : updateEmuFolders ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : updateEmuFolders , Qt : : QueuedConnection ) ;
return ;
}
2023-05-13 10:22:46 +00:00
VMManager : : Internal : : UpdateEmuFolders ( ) ;
2022-09-07 09:20:50 +00:00
}
2022-09-14 12:41:50 +00:00
void EmuThread : : connectSignals ( )
2022-09-07 09:20:50 +00:00
{
2022-09-14 12:41:50 +00:00
connect ( qApp , & QGuiApplication : : applicationStateChanged , this , & EmuThread : : onApplicationStateChanged ) ;
2022-09-07 09:20:50 +00:00
}
2022-09-14 12:41:50 +00:00
void EmuThread : : loadSettings ( SettingsInterface & si , std : : unique_lock < std : : mutex > & lock )
2022-09-07 09:20:50 +00:00
{
2022-09-14 12:41:50 +00:00
m_verbose_status = si . GetBoolValue ( " UI " , " VerboseStatusBar " , false ) ;
m_pause_on_focus_loss = si . GetBoolValue ( " UI " , " PauseOnFocusLoss " , false ) ;
2022-09-07 09:20:50 +00:00
}
2022-09-14 12:41:50 +00:00
void Host : : LoadSettings ( SettingsInterface & si , std : : unique_lock < std : : mutex > & lock )
2022-09-07 09:20:50 +00:00
{
2022-09-14 12:41:50 +00:00
g_emu_thread - > loadSettings ( si , lock ) ;
2022-09-07 09:20:50 +00:00
}
2022-09-14 12:41:50 +00:00
void EmuThread : : checkForSettingChanges ( const Pcsx2Config & old_config )
2022-09-07 09:20:50 +00:00
{
2023-06-23 13:18:10 +00:00
if ( g_main_window )
{
QMetaObject : : invokeMethod ( g_main_window , & MainWindow : : checkForSettingChanges , Qt : : QueuedConnection ) ;
updatePerformanceMetrics ( true ) ;
}
2022-09-07 09:20:50 +00:00
2023-06-24 07:46:36 +00:00
if ( MTGS : : IsOpen ( ) )
2022-09-07 09:20:50 +00:00
{
const bool render_to_main = shouldRenderToMain ( ) ;
if ( ! m_is_fullscreen & & m_is_rendering_to_main ! = render_to_main )
{
m_is_rendering_to_main = render_to_main ;
2023-06-24 07:46:36 +00:00
MTGS : : UpdateDisplayWindow ( ) ;
MTGS : : WaitGS ( ) ;
2022-09-07 09:20:50 +00:00
}
}
2022-09-14 12:41:50 +00:00
}
2022-09-07 09:20:50 +00:00
2022-09-14 12:41:50 +00:00
void Host : : CheckForSettingsChanges ( const Pcsx2Config & old_config )
{
g_emu_thread - > checkForSettingChanges ( old_config ) ;
2022-09-07 09:20:50 +00:00
}
bool EmuThread : : shouldRenderToMain ( ) const
{
2022-10-22 03:43:12 +00:00
return ! Host : : GetBoolSettingValue ( " UI " , " RenderToSeparateWindow " , false ) & & ! QtHost : : InNoGUIMode ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : toggleSoftwareRendering ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : toggleSoftwareRendering , Qt : : QueuedConnection ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
2023-06-24 07:46:36 +00:00
MTGS : : ToggleSoftwareRendering ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : changeDisc ( CDVD_SourceType source , const QString & path )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " changeDisc " , Qt : : QueuedConnection , Q_ARG ( CDVD_SourceType , source ) , Q_ARG ( const QString & , path ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
VMManager : : ChangeDisc ( source , path . toStdString ( ) ) ;
}
2023-09-09 05:37:31 +00:00
void EmuThread : : setELFOverride ( const QString & path )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " setELFOverride " , Qt : : QueuedConnection , Q_ARG ( const QString & , path ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
VMManager : : SetELFOverride ( path . toStdString ( ) ) ;
}
void EmuThread : : changeGSDump ( const QString & path )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " changeGSDump " , Qt : : QueuedConnection , Q_ARG ( const QString & , path ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
VMManager : : ChangeGSDump ( path . toStdString ( ) ) ;
}
2022-09-07 09:20:50 +00:00
void EmuThread : : reloadPatches ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : reloadPatches , Qt : : QueuedConnection ) ;
return ;
}
2023-06-13 12:43:11 +00:00
VMManager : : ReloadPatches ( true , false , true , true ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : reloadInputSources ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : reloadInputSources , Qt : : QueuedConnection ) ;
return ;
}
2024-05-15 13:29:01 +00:00
VMManager : : ReloadInputSources ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : reloadInputBindings ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : reloadInputBindings , Qt : : QueuedConnection ) ;
return ;
}
2024-05-15 13:29:01 +00:00
VMManager : : ReloadInputBindings ( ) ;
2022-09-07 09:20:50 +00:00
}
2022-10-15 11:20:05 +00:00
void EmuThread : : reloadInputDevices ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : reloadInputDevices , Qt : : QueuedConnection ) ;
return ;
}
InputManager : : ReloadDevices ( ) ;
}
void EmuThread : : closeInputSources ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : reloadInputDevices , Qt : : BlockingQueuedConnection ) ;
return ;
}
InputManager : : CloseSources ( ) ;
}
2022-09-07 09:20:50 +00:00
void EmuThread : : requestDisplaySize ( float scale )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " requestDisplaySize " , Qt : : QueuedConnection , Q_ARG ( float , scale ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
VMManager : : RequestDisplaySize ( scale ) ;
}
void EmuThread : : enumerateInputDevices ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : enumerateInputDevices , Qt : : QueuedConnection ) ;
return ;
}
const std : : vector < std : : pair < std : : string , std : : string > > devs ( InputManager : : EnumerateDevices ( ) ) ;
QList < QPair < QString , QString > > qdevs ;
qdevs . reserve ( devs . size ( ) ) ;
for ( const std : : pair < std : : string , std : : string > & dev : devs )
qdevs . emplace_back ( QString : : fromStdString ( dev . first ) , QString : : fromStdString ( dev . second ) ) ;
onInputDevicesEnumerated ( qdevs ) ;
}
2022-03-04 10:40:03 +00:00
2022-09-07 09:20:50 +00:00
void EmuThread : : enumerateVibrationMotors ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : enumerateVibrationMotors , Qt : : QueuedConnection ) ;
return ;
}
2021-12-13 12:12:54 +00:00
2022-09-07 09:20:50 +00:00
const std : : vector < InputBindingKey > motors ( InputManager : : EnumerateMotors ( ) ) ;
QList < InputBindingKey > qmotors ;
qmotors . reserve ( motors . size ( ) ) ;
for ( InputBindingKey key : motors )
qmotors . push_back ( key ) ;
2021-12-13 12:12:54 +00:00
2022-09-07 09:20:50 +00:00
onVibrationMotorsEnumerated ( qmotors ) ;
}
2021-12-13 12:12:54 +00:00
2022-09-07 09:20:50 +00:00
void EmuThread : : connectDisplaySignals ( DisplayWidget * widget )
{
widget - > disconnect ( this ) ;
2021-12-13 12:12:54 +00:00
2022-09-07 09:20:50 +00:00
connect ( widget , & DisplayWidget : : windowResizedEvent , this , & EmuThread : : onDisplayWindowResized ) ;
connect ( widget , & DisplayWidget : : windowRestoredEvent , this , & EmuThread : : redrawDisplayWindow ) ;
2022-03-04 10:40:03 +00:00
}
2021-12-13 12:12:54 +00:00
2022-09-07 09:20:50 +00:00
void EmuThread : : onDisplayWindowResized ( int width , int height , float scale )
{
2023-06-24 07:46:36 +00:00
if ( ! MTGS : : IsOpen ( ) )
2022-09-07 09:20:50 +00:00
return ;
2021-12-13 12:12:54 +00:00
2023-06-24 07:46:36 +00:00
MTGS : : ResizeDisplayWindow ( width , height , scale ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : onApplicationStateChanged ( Qt : : ApplicationState state )
{
// NOTE: This is executed on the emu thread, not UI thread.
2023-03-02 12:01:14 +00:00
if ( ! VMManager : : HasValidVM ( ) )
2022-09-07 09:20:50 +00:00
return ;
const bool focus_loss = ( state ! = Qt : : ApplicationActive ) ;
if ( focus_loss )
{
2023-03-02 12:01:14 +00:00
if ( m_pause_on_focus_loss & & ! m_was_paused_by_focus_loss & & VMManager : : GetState ( ) = = VMState : : Running )
2022-09-07 09:20:50 +00:00
{
m_was_paused_by_focus_loss = true ;
VMManager : : SetPaused ( true ) ;
}
2023-03-02 12:01:14 +00:00
// Clear the state of all keyboard binds.
// That way, if we had a key held down, and lost focus, the bind won't be stuck enabled because we never
// got the key release message, because it happened in another window which "stole" the event.
InputManager : : ClearBindStateFromSource ( InputManager : : MakeHostKeyboardKey ( 0 ) ) ;
2022-09-07 09:20:50 +00:00
}
else
{
if ( m_was_paused_by_focus_loss )
{
m_was_paused_by_focus_loss = false ;
if ( VMManager : : GetState ( ) = = VMState : : Paused )
VMManager : : SetPaused ( false ) ;
}
}
}
void EmuThread : : redrawDisplayWindow ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , & EmuThread : : redrawDisplayWindow , Qt : : QueuedConnection ) ;
return ;
}
// If we're running, we're going to re-present anyway.
if ( ! VMManager : : HasValidVM ( ) | | VMManager : : GetState ( ) = = VMState : : Running )
return ;
2023-06-24 07:46:36 +00:00
MTGS : : PresentCurrentFrame ( ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : runOnCPUThread ( const std : : function < void ( ) > & func )
{
func ( ) ;
}
void EmuThread : : queueSnapshot ( quint32 gsdump_frames )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " queueSnapshot " , Qt : : QueuedConnection , Q_ARG ( quint32 , gsdump_frames ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
2023-06-24 07:46:36 +00:00
MTGS : : RunOnGSThread ( [ gsdump_frames ] ( ) { GSQueueSnapshot ( std : : string ( ) , gsdump_frames ) ; } ) ;
2022-09-07 09:20:50 +00:00
}
2022-12-18 13:05:00 +00:00
void EmuThread : : beginCapture ( const QString & path )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " beginCapture " , Qt : : QueuedConnection , Q_ARG ( const QString & , path ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
2023-06-24 07:46:36 +00:00
MTGS : : RunOnGSThread ( [ path = path . toStdString ( ) ] ( ) {
2022-12-18 13:05:00 +00:00
GSBeginCapture ( std : : move ( path ) ) ;
} ) ;
2023-01-19 14:01:54 +00:00
// Sync GS thread. We want to start adding audio at the same time as video.
// TODO: This could be up to 64 frames behind... use the pts to adjust it.
2023-06-24 07:46:36 +00:00
MTGS : : WaitGS ( false , false , false ) ;
2022-12-18 13:05:00 +00:00
}
void EmuThread : : endCapture ( )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " endCapture " , Qt : : QueuedConnection ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
2023-06-24 07:46:36 +00:00
MTGS : : RunOnGSThread ( & GSEndCapture ) ;
2022-12-18 13:05:00 +00:00
}
2024-04-23 15:17:52 +00:00
void EmuThread : : setAudioOutputVolume ( int volume , int fast_forward_volume )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " setAudioOutputVolume " , Qt : : QueuedConnection , Q_ARG ( int , volume ) ,
Q_ARG ( int , fast_forward_volume ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
EmuConfig . SPU2 . OutputVolume = static_cast < u32 > ( volume ) ;
EmuConfig . SPU2 . FastForwardVolume = static_cast < u32 > ( fast_forward_volume ) ;
SPU2 : : SetOutputVolume ( SPU2 : : GetResetVolume ( ) ) ;
}
void EmuThread : : setAudioOutputMuted ( bool muted )
{
if ( ! isOnEmuThread ( ) )
{
QMetaObject : : invokeMethod ( this , " setAudioOutputMuted " , Qt : : QueuedConnection , Q_ARG ( bool , muted ) ) ;
return ;
}
if ( ! VMManager : : HasValidVM ( ) )
return ;
EmuConfig . SPU2 . OutputMuted = muted ;
SPU2 : : SetOutputVolume ( SPU2 : : GetResetVolume ( ) ) ;
}
2023-04-25 12:52:41 +00:00
std : : optional < WindowInfo > EmuThread : : acquireRenderWindow ( bool recreate_window )
2022-09-07 09:20:50 +00:00
{
2023-04-25 12:52:41 +00:00
// Check if we're wanting to get exclusive fullscreen. This should be safe to read, since we're going to be calling from the GS thread.
m_is_exclusive_fullscreen = m_is_fullscreen & & GSWantsExclusiveFullscreen ( ) ;
const bool window_fullscreen = m_is_fullscreen & & ! m_is_exclusive_fullscreen ;
const bool render_to_main = ! m_is_exclusive_fullscreen & & ! window_fullscreen & & m_is_rendering_to_main ;
2022-09-07 09:20:50 +00:00
2023-04-25 12:52:41 +00:00
return emit onAcquireRenderWindowRequested ( recreate_window , window_fullscreen , render_to_main , m_is_surfaceless ) ;
2022-09-07 09:20:50 +00:00
}
2023-04-01 05:54:04 +00:00
void EmuThread : : releaseRenderWindow ( )
2022-09-07 09:20:50 +00:00
{
2023-04-25 12:52:41 +00:00
emit onReleaseRenderWindowRequested ( ) ;
2022-09-07 09:20:50 +00:00
}
2023-04-25 12:52:41 +00:00
std : : optional < WindowInfo > Host : : AcquireRenderWindow ( bool recreate_window )
2022-09-07 09:20:50 +00:00
{
2023-04-25 12:52:41 +00:00
return g_emu_thread - > acquireRenderWindow ( recreate_window ) ;
2022-09-07 09:20:50 +00:00
}
2023-04-01 05:54:04 +00:00
void Host : : ReleaseRenderWindow ( )
2022-09-07 09:20:50 +00:00
{
2023-04-01 05:54:04 +00:00
return g_emu_thread - > releaseRenderWindow ( ) ;
2022-09-07 09:20:50 +00:00
}
2023-04-01 05:54:04 +00:00
void Host : : BeginPresentFrame ( )
2022-09-07 09:20:50 +00:00
{
}
void Host : : RequestResizeHostDisplay ( s32 width , s32 height )
{
2023-04-25 12:52:41 +00:00
g_emu_thread - > onResizeRenderWindowRequested ( width , height ) ;
2022-09-07 09:20:50 +00:00
}
void Host : : OnVMStarting ( )
{
g_emu_thread - > stopBackgroundControllerPollTimer ( ) ;
emit g_emu_thread - > onVMStarting ( ) ;
}
void Host : : OnVMStarted ( )
{
2024-03-31 14:44:57 +00:00
g_emu_thread - > updatePerformanceMetrics ( true ) ;
2022-09-07 09:20:50 +00:00
emit g_emu_thread - > onVMStarted ( ) ;
}
void Host : : OnVMDestroyed ( )
{
emit g_emu_thread - > onVMStopped ( ) ;
g_emu_thread - > startBackgroundControllerPollTimer ( ) ;
}
void Host : : OnVMPaused ( )
{
g_emu_thread - > startBackgroundControllerPollTimer ( ) ;
emit g_emu_thread - > onVMPaused ( ) ;
}
void Host : : OnVMResumed ( )
{
// exit the event loop when we eventually return
g_emu_thread - > getEventLoop ( ) - > quit ( ) ;
g_emu_thread - > stopBackgroundControllerPollTimer ( ) ;
// if we were surfaceless (view->game list, system->unpause), get our display widget back
if ( g_emu_thread - > isSurfaceless ( ) )
g_emu_thread - > setSurfaceless ( false ) ;
emit g_emu_thread - > onVMResumed ( ) ;
}
2023-06-13 12:43:11 +00:00
void Host : : OnGameChanged ( const std : : string & title , const std : : string & elf_override , const std : : string & disc_path ,
const std : : string & disc_serial , u32 disc_crc , u32 current_crc )
2022-09-07 09:20:50 +00:00
{
2023-06-13 12:43:11 +00:00
emit g_emu_thread - > onGameChanged ( QString : : fromStdString ( title ) , QString : : fromStdString ( elf_override ) ,
QString : : fromStdString ( disc_path ) , QString : : fromStdString ( disc_serial ) , disc_crc , current_crc ) ;
2022-09-07 09:20:50 +00:00
}
void EmuThread : : updatePerformanceMetrics ( bool force )
{
if ( m_verbose_status & & VMManager : : HasValidVM ( ) )
{
std : : string gs_stat_str ;
GSgetTitleStats ( gs_stat_str ) ;
QString gs_stat ;
if ( THREAD_VU1 )
{
2023-10-30 15:31:46 +00:00
gs_stat = tr ( " Slot: %1 | %2 | EE: %3% | VU: %4% | GS: %5% " )
2023-12-14 11:44:32 +00:00
. arg ( SaveStateSelectorUI : : GetCurrentSlot ( ) )
2022-12-04 08:03:30 +00:00
. arg ( gs_stat_str . c_str ( ) )
. arg ( PerformanceMetrics : : GetCPUThreadUsage ( ) , 0 , ' f ' , 0 )
. arg ( PerformanceMetrics : : GetVUThreadUsage ( ) , 0 , ' f ' , 0 )
. arg ( PerformanceMetrics : : GetGSThreadUsage ( ) , 0 , ' f ' , 0 ) ;
2022-09-07 09:20:50 +00:00
}
else
{
2023-10-30 15:31:46 +00:00
gs_stat = tr ( " Slot: %1 | %2 | EE: %3% | GS: %4% " )
2023-12-14 11:44:32 +00:00
. arg ( SaveStateSelectorUI : : GetCurrentSlot ( ) )
2022-09-07 09:20:50 +00:00
. arg ( gs_stat_str . c_str ( ) )
. arg ( PerformanceMetrics : : GetCPUThreadUsage ( ) , 0 , ' f ' , 0 )
. arg ( PerformanceMetrics : : GetGSThreadUsage ( ) , 0 , ' f ' , 0 ) ;
}
QMetaObject : : invokeMethod ( g_main_window - > getStatusVerboseWidget ( ) , " setText " , Qt : : QueuedConnection , Q_ARG ( const QString & , gs_stat ) ) ;
}
2024-01-10 08:41:42 +00:00
const GSRendererType renderer = GSGetCurrentRenderer ( ) ; // Reading from GS thread, therefore racey, but it's just visual.
2022-09-07 09:20:50 +00:00
const float speed = std : : round ( PerformanceMetrics : : GetSpeed ( ) ) ;
const float gfps = std : : round ( PerformanceMetrics : : GetInternalFPS ( ) ) ;
const float vfps = std : : round ( PerformanceMetrics : : GetFPS ( ) ) ;
int iwidth , iheight ;
GSgetInternalResolution ( & iwidth , & iheight ) ;
2022-12-04 08:03:30 +00:00
if ( iwidth ! = m_last_internal_width | | iheight ! = m_last_internal_height | | speed ! = m_last_speed | | gfps ! = m_last_game_fps | |
vfps ! = m_last_video_fps | | renderer ! = m_last_renderer | | force )
2022-09-07 09:20:50 +00:00
{
2024-03-31 14:44:57 +00:00
if ( renderer ! = m_last_renderer | | force )
2022-09-07 09:20:50 +00:00
{
2024-03-31 14:44:57 +00:00
QMetaObject : : invokeMethod ( g_main_window - > getStatusRendererWidget ( ) , " setText " , Qt : : QueuedConnection ,
Q_ARG ( const QString & , QString : : fromUtf8 ( Pcsx2Config : : GSOptions : : GetRendererName ( renderer ) ) ) ) ;
m_last_renderer = renderer ;
}
if ( iwidth ! = m_last_internal_width | | iheight ! = m_last_internal_height | | force )
{
QString text ;
if ( iwidth = = 0 | | iheight = = 0 )
2024-04-06 11:29:33 +00:00
text = tr ( " No Image " ) ;
2024-03-31 14:44:57 +00:00
else
text = tr ( " %1x%2 " ) . arg ( iwidth ) . arg ( iheight ) ;
2022-12-04 08:03:30 +00:00
QMetaObject : : invokeMethod (
2024-03-31 14:44:57 +00:00
g_main_window - > getStatusResolutionWidget ( ) , " setText " , Qt : : QueuedConnection , Q_ARG ( const QString & , text ) ) ;
m_last_internal_width = iwidth ;
m_last_internal_height = iheight ;
2022-09-07 09:20:50 +00:00
}
2024-03-31 14:44:57 +00:00
if ( gfps ! = m_last_game_fps | | force )
{
QMetaObject : : invokeMethod ( g_main_window - > getStatusFPSWidget ( ) , " setText " , Qt : : QueuedConnection ,
2024-09-14 13:40:48 +00:00
Q_ARG ( const QString & , tr ( " FPS: %1 " ) . arg ( gfps , 0 , ' f ' , 0 ) ) ) ;
2024-03-31 14:44:57 +00:00
m_last_game_fps = gfps ;
}
2024-09-25 16:11:18 +00:00
if ( vfps ! = m_last_video_fps | | force )
2022-09-07 09:20:50 +00:00
{
2024-03-31 14:44:57 +00:00
QMetaObject : : invokeMethod ( g_main_window - > getStatusVPSWidget ( ) , " setText " , Qt : : QueuedConnection ,
2024-09-15 15:03:29 +00:00
Q_ARG ( const QString & , tr ( " VPS: %1 " ) . arg ( vfps , 0 , ' f ' , 0 ) ) ) ;
2024-03-31 14:44:57 +00:00
m_last_video_fps = vfps ;
2024-09-15 15:03:29 +00:00
if ( speed ! = m_last_speed | | force )
{
QMetaObject : : invokeMethod ( g_main_window - > getStatusSpeedWidget ( ) , " setText " , Qt : : QueuedConnection ,
Q_ARG ( const QString & , tr ( " Speed: %1% " ) . arg ( speed , 0 , ' f ' , 0 ) ) ) ;
m_last_speed = speed ;
}
2022-09-07 09:20:50 +00:00
}
}
}
void Host : : OnPerformanceMetricsUpdated ( )
{
g_emu_thread - > updatePerformanceMetrics ( false ) ;
}
2024-05-14 23:42:40 +00:00
void Host : : OnSaveStateLoading ( const std : : string_view filename )
2022-09-07 09:20:50 +00:00
{
emit g_emu_thread - > onSaveStateLoading ( QtUtils : : StringViewToQString ( filename ) ) ;
}
2024-05-14 23:42:40 +00:00
void Host : : OnSaveStateLoaded ( const std : : string_view filename , bool was_successful )
2022-09-07 09:20:50 +00:00
{
emit g_emu_thread - > onSaveStateLoaded ( QtUtils : : StringViewToQString ( filename ) , was_successful ) ;
}
2024-05-14 23:42:40 +00:00
void Host : : OnSaveStateSaved ( const std : : string_view filename )
2022-09-07 09:20:50 +00:00
{
emit g_emu_thread - > onSaveStateSaved ( QtUtils : : StringViewToQString ( filename ) ) ;
}
2023-07-27 09:24:55 +00:00
void Host : : OnAchievementsLoginRequested ( Achievements : : LoginRequestReason reason )
{
emit g_emu_thread - > onAchievementsLoginRequested ( reason ) ;
}
2023-09-17 10:19:51 +00:00
void Host : : OnAchievementsLoginSuccess ( const char * username , u32 points , u32 sc_points , u32 unread_messages )
{
2024-05-14 21:42:19 +00:00
const QString message =
qApp - > translate ( " QtHost " , " RA: Logged in as %1 (%2 pts, softcore: %3 pts). %4 unread messages. " )
. arg ( QString : : fromUtf8 ( username ) )
. arg ( points )
. arg ( sc_points )
. arg ( unread_messages ) ;
emit g_emu_thread - > statusMessage ( message ) ;
2023-09-17 10:19:51 +00:00
}
2022-04-18 13:35:14 +00:00
void Host : : OnAchievementsRefreshed ( )
{
u32 game_id = 0 ;
QString game_info ;
if ( Achievements : : HasActiveGame ( ) )
{
game_id = Achievements : : GetGameID ( ) ;
2023-09-17 10:19:51 +00:00
game_info = qApp
- > translate ( " EmuThread " , " Game: %1 (%2) \n " )
2022-04-18 13:35:14 +00:00
. arg ( QString : : fromStdString ( Achievements : : GetGameTitle ( ) ) )
2023-09-17 10:19:51 +00:00
. arg ( game_id ) ;
2022-04-18 13:35:14 +00:00
2023-09-17 10:19:51 +00:00
const std : : string & rich_presence_string = Achievements : : GetRichPresenceString ( ) ;
2022-04-18 13:35:14 +00:00
if ( ! rich_presence_string . empty ( ) )
2023-09-17 10:19:51 +00:00
game_info . append ( QString : : fromStdString ( StringUtil : : Ellipsise ( rich_presence_string , 128 ) ) ) ;
2022-04-18 13:35:14 +00:00
else
game_info . append ( qApp - > translate ( " EmuThread " , " Rich presence inactive or unsupported. " ) ) ;
}
else
{
game_info = qApp - > translate ( " EmuThread " , " Game not loaded or no RetroAchievements available. " ) ;
}
2023-09-17 10:19:51 +00:00
emit g_emu_thread - > onAchievementsRefreshed ( game_id , game_info ) ;
}
void Host : : OnAchievementsHardcoreModeChanged ( bool enabled )
{
emit g_emu_thread - > onAchievementsHardcoreModeChanged ( enabled ) ;
2022-04-18 13:35:14 +00:00
}
2023-10-02 04:57:20 +00:00
void Host : : OnCoverDownloaderOpenRequested ( )
{
emit g_emu_thread - > onCoverDownloaderOpenRequested ( ) ;
}
2024-01-18 11:10:32 +00:00
void Host : : OnCreateMemoryCardOpenRequested ( )
{
emit g_emu_thread - > onCreateMemoryCardOpenRequested ( ) ;
}
2024-05-14 21:25:06 +00:00
bool Host : : ShouldPreferHostFileSelector ( )
{
# ifdef __linux__
// If running inside a flatpak, we want to use native selectors/portals.
return ( std : : getenv ( " container " ) ! = nullptr ) ;
# else
return false ;
# endif
}
void Host : : OpenHostFileSelectorAsync ( std : : string_view title , bool select_directory , FileSelectorCallback callback ,
FileSelectorFilters filters , std : : string_view initial_directory )
{
const bool from_cpu_thread = g_emu_thread - > isOnEmuThread ( ) ;
QString filters_str ;
if ( ! filters . empty ( ) )
{
filters_str . append ( QStringLiteral ( " All File Types (%1) " )
. arg ( QString : : fromStdString ( StringUtil : : JoinString ( filters . begin ( ) , filters . end ( ) , " " ) ) ) ) ;
for ( const std : : string & filter : filters )
{
filters_str . append (
QStringLiteral ( " ;;%1 Files (%2) " )
. arg (
QtUtils : : StringViewToQString ( std : : string_view ( filter ) . substr ( filter . starts_with ( " *. " ) ? 2 : 0 ) ) . toUpper ( ) )
. arg ( QString : : fromStdString ( filter ) ) ) ;
}
}
QtHost : : RunOnUIThread ( [ title = QtUtils : : StringViewToQString ( title ) , select_directory , callback = std : : move ( callback ) ,
filters_str = std : : move ( filters_str ) ,
initial_directory = QtUtils : : StringViewToQString ( initial_directory ) ,
from_cpu_thread ] ( ) mutable {
auto lock = g_main_window - > pauseAndLockVM ( ) ;
QString path ;
if ( select_directory )
path = QFileDialog : : getExistingDirectory ( lock . getDialogParent ( ) , title , initial_directory ) ;
else
path = QFileDialog : : getOpenFileName ( lock . getDialogParent ( ) , title , initial_directory , filters_str ) ;
if ( ! path . isEmpty ( ) )
path = QDir : : toNativeSeparators ( path ) ;
if ( from_cpu_thread )
Host : : RunOnCPUThread ( [ callback = std : : move ( callback ) , path = path . toStdString ( ) ] ( ) { callback ( path ) ; } ) ;
else
callback ( path . toStdString ( ) ) ;
} ) ;
}
2024-01-26 11:27:36 +00:00
void Host : : PumpMessagesOnCPUThread ( )
2022-09-07 09:20:50 +00:00
{
g_emu_thread - > getEventLoop ( ) - > processEvents ( QEventLoop : : AllEvents ) ;
}
void Host : : RunOnCPUThread ( std : : function < void ( ) > function , bool block /* = false */ )
{
2023-06-13 12:43:11 +00:00
if ( block & & g_emu_thread - > isOnEmuThread ( ) )
2022-09-07 09:20:50 +00:00
{
// probably shouldn't ever happen, but just in case..
function ( ) ;
return ;
}
2022-12-04 08:03:30 +00:00
QMetaObject : : invokeMethod ( g_emu_thread , " runOnCPUThread " , block ? Qt : : BlockingQueuedConnection : Qt : : QueuedConnection ,
2022-09-07 09:20:50 +00:00
Q_ARG ( const std : : function < void ( ) > & , std : : move ( function ) ) ) ;
}
void Host : : RefreshGameListAsync ( bool invalidate_cache )
{
2022-12-04 08:03:30 +00:00
QMetaObject : : invokeMethod ( g_main_window , " refreshGameList " , Qt : : QueuedConnection , Q_ARG ( bool , invalidate_cache ) ) ;
2022-09-07 09:20:50 +00:00
}
void Host : : CancelGameListRefresh ( )
{
QMetaObject : : invokeMethod ( g_main_window , " cancelGameListRefresh " , Qt : : BlockingQueuedConnection ) ;
}
2024-04-12 14:53:58 +00:00
void Host : : RequestExitApplication ( bool allow_confirm )
2022-09-07 09:20:50 +00:00
{
2023-01-28 03:33:51 +00:00
QMetaObject : : invokeMethod ( g_main_window , " requestExit " , Qt : : QueuedConnection , Q_ARG ( bool , allow_confirm ) ) ;
2022-09-07 09:20:50 +00:00
}
2024-04-12 14:53:58 +00:00
void Host : : RequestExitBigPicture ( )
{
g_emu_thread - > stopFullscreenUI ( ) ;
}
2022-09-07 09:20:50 +00:00
void Host : : RequestVMShutdown ( bool allow_confirm , bool allow_save_state , bool default_save_state )
{
if ( ! VMManager : : HasValidVM ( ) )
return ;
2023-01-28 03:33:51 +00:00
// This is a bit messy here - we want to shut down immediately (in case it was requested by the game),
// but we also need to exit-on-shutdown for batch mode. So, if we're running on the CPU thread, destroy
// the VM, then request the main window to exit.
if ( allow_confirm | | ! g_emu_thread - > isOnEmuThread ( ) )
2023-01-22 17:23:24 +00:00
{
// Run it on the host thread, that way we get the confirm prompt (if enabled).
QMetaObject : : invokeMethod ( g_main_window , " requestShutdown " , Qt : : QueuedConnection , Q_ARG ( bool , allow_confirm ) ,
2023-01-28 03:33:51 +00:00
Q_ARG ( bool , allow_save_state ) , Q_ARG ( bool , default_save_state ) ) ;
2023-01-22 17:23:24 +00:00
}
else
{
2023-01-28 03:33:51 +00:00
// Change state to stopping -> return -> shut down VM.
2023-01-22 17:23:24 +00:00
g_emu_thread - > shutdownVM ( allow_save_state & & default_save_state ) ;
2023-01-28 03:33:51 +00:00
// This will probably call shutdownVM() again, but by the time it runs, we'll have already shut down
// and it'll be a noop.
if ( QtHost : : InBatchMode ( ) )
QMetaObject : : invokeMethod ( g_main_window , " requestExit " , Qt : : QueuedConnection , Q_ARG ( bool , false ) ) ;
2023-01-22 17:23:24 +00:00
}
2022-09-07 09:20:50 +00:00
}
bool Host : : IsFullscreen ( )
{
return g_emu_thread - > isFullscreen ( ) ;
}
void Host : : SetFullscreen ( bool enabled )
{
2023-04-25 12:52:41 +00:00
g_emu_thread - > setFullscreen ( enabled , true ) ;
2022-09-07 09:20:50 +00:00
}
2023-07-06 13:18:30 +00:00
void Host : : OnCaptureStarted ( const std : : string & filename )
{
emit g_emu_thread - > onCaptureStarted ( QString : : fromStdString ( filename ) ) ;
}
void Host : : OnCaptureStopped ( )
{
emit g_emu_thread - > onCaptureStopped ( ) ;
}
2022-09-07 07:44:10 +00:00
bool QtHost : : InitializeConfig ( )
2021-12-13 12:12:54 +00:00
{
2024-04-01 11:15:52 +00:00
Error error ;
EmuFolders : : SetAppRoot ( ) ;
if ( ! EmuFolders : : SetResourcesDirectory ( ) )
2021-12-13 12:12:54 +00:00
{
2022-09-07 07:44:10 +00:00
QMessageBox : : critical ( nullptr , QStringLiteral ( " PCSX2 " ) ,
2024-04-01 11:15:52 +00:00
QStringLiteral ( " Resources directory is missing, your installation is incomplete. " ) ) ;
return false ;
}
if ( ! EmuFolders : : SetDataDirectory ( & error ) )
{
// no point translating, config isn't loaded
QMessageBox : : critical (
nullptr , QStringLiteral ( " PCSX2 " ) ,
QStringLiteral ( " Failed to create data directory at path \n \n %1 \n \n "
2024-05-14 21:25:06 +00:00
" The error was: %2 \n "
" Please ensure this directory is writable. You can also try portable mode "
" by creating portable.txt in the same directory you installed PCSX2 into. " )
2024-04-01 11:15:52 +00:00
. arg ( QString : : fromStdString ( EmuFolders : : DataRoot ) )
. arg ( QString : : fromStdString ( error . GetDescription ( ) ) ) ) ;
2021-12-13 12:12:54 +00:00
return false ;
}
2023-05-13 10:22:46 +00:00
// Write crash dumps to the data directory, since that'll be accessible for certain.
CrashHandler : : SetWriteDirectory ( EmuFolders : : DataRoot ) ;
2024-04-01 11:15:52 +00:00
const std : : string path = Path : : Combine ( EmuFolders : : Settings , " PCSX2.ini " ) ;
const bool settings_exists = FileSystem : : FileExists ( path . c_str ( ) ) ;
Console . WriteLnFmt ( " Loading config from {}. " , path ) ;
2022-09-07 07:44:10 +00:00
2021-12-13 12:12:54 +00:00
s_base_settings_interface = std : : make_unique < INISettingsInterface > ( std : : move ( path ) ) ;
Host : : Internal : : SetBaseSettingsLayer ( s_base_settings_interface . get ( ) ) ;
2024-04-01 11:15:52 +00:00
if ( ! settings_exists | | ! s_base_settings_interface - > Load ( ) | | ! VMManager : : Internal : : CheckSettingsVersion ( ) )
2021-12-13 12:12:54 +00:00
{
2022-09-07 07:44:10 +00:00
// If the config file doesn't exist, assume this is a new install and don't prompt to overwrite.
if ( FileSystem : : FileExists ( s_base_settings_interface - > GetFileName ( ) . c_str ( ) ) & &
QMessageBox : : question ( nullptr , QStringLiteral ( " PCSX2 " ) ,
2022-12-04 08:03:30 +00:00
QStringLiteral ( " Settings failed to load, or are the incorrect version. Clicking Yes will reset all settings to defaults. "
" Do you want to continue? " ) ) ! = QMessageBox : : Yes )
2022-09-07 07:44:10 +00:00
{
return false ;
}
2023-05-13 10:22:46 +00:00
VMManager : : SetDefaultSettings ( * s_base_settings_interface , true , true , true , true , true ) ;
2023-12-23 20:32:26 +00:00
2024-04-01 11:15:52 +00:00
// Flag for running the setup wizard if this is our first run. We want to run it next time if they don't finish it.
s_base_settings_interface - > SetBoolValue ( " UI " , " SetupWizardIncomplete " , true ) ;
// Make sure we can actually save the config, and the user doesn't have some permission issue.
if ( ! s_base_settings_interface - > Save ( & error ) )
{
QMessageBox : : critical (
nullptr , QStringLiteral ( " PCSX2 " ) ,
QStringLiteral (
" Failed to save configuration to \n \n %1 \n \n The error was: %2 \n \n Please ensure this directory is writable. You "
" can also try portable mode by creating portable.txt in the same directory you installed PCSX2 into. " )
. arg ( QString : : fromStdString ( s_base_settings_interface - > GetFileName ( ) ) )
. arg ( QString : : fromStdString ( error . GetDescription ( ) ) ) ) ;
return false ;
}
2023-06-23 13:18:10 +00:00
// Don't save if we're running the setup wizard. We want to run it next time if they don't finish it.
if ( ! s_run_setup_wizard )
SaveSettings ( ) ;
2021-12-13 12:12:54 +00:00
}
2023-06-23 13:18:10 +00:00
// Setup wizard was incomplete last time?
s_run_setup_wizard =
s_run_setup_wizard | | s_base_settings_interface - > GetBoolValue ( " UI " , " SetupWizardIncomplete " , false ) ;
2024-01-11 08:08:16 +00:00
// TODO: -nogui console block?
2023-05-13 10:22:46 +00:00
VMManager : : Internal : : LoadStartupSettings ( ) ;
2024-01-07 05:27:39 +00:00
InstallTranslator ( nullptr ) ;
2021-12-13 12:12:54 +00:00
return true ;
}
2022-09-07 07:44:10 +00:00
void Host : : SetDefaultUISettings ( SettingsInterface & si )
2021-12-13 12:12:54 +00:00
{
2022-09-07 07:44:10 +00:00
si . SetBoolValue ( " UI " , " InhibitScreensaver " , true ) ;
si . SetBoolValue ( " UI " , " ConfirmShutdown " , true ) ;
si . SetBoolValue ( " UI " , " StartPaused " , false ) ;
si . SetBoolValue ( " UI " , " PauseOnFocusLoss " , false ) ;
si . SetBoolValue ( " UI " , " StartFullscreen " , false ) ;
si . SetBoolValue ( " UI " , " DoubleClickTogglesFullscreen " , true ) ;
si . SetBoolValue ( " UI " , " HideMouseCursor " , false ) ;
si . SetBoolValue ( " UI " , " RenderToSeparateWindow " , false ) ;
si . SetBoolValue ( " UI " , " HideMainWindowWhenRunning " , false ) ;
si . SetBoolValue ( " UI " , " DisableWindowResize " , false ) ;
2023-09-09 22:36:10 +00:00
si . SetBoolValue ( " UI " , " PreferEnglishGameList " , false ) ;
2023-04-25 10:24:22 +00:00
si . SetStringValue ( " UI " , " Theme " , QtHost : : GetDefaultThemeName ( ) ) ;
2021-12-13 12:12:54 +00:00
}
2022-03-04 10:40:03 +00:00
void QtHost : : SaveSettings ( )
2021-12-13 12:12:54 +00:00
{
pxAssertRel ( ! g_emu_thread - > isOnEmuThread ( ) , " Saving should happen on the UI thread. " ) ;
{
2024-04-05 10:08:36 +00:00
Error error ;
2021-12-13 12:12:54 +00:00
auto lock = Host : : GetSettingsLock ( ) ;
2024-04-05 10:08:36 +00:00
if ( ! s_base_settings_interface - > Save ( & error ) )
Console . ErrorFmt ( " Failed to save settings: {} " , error . GetDescription ( ) ) ;
2021-12-13 12:12:54 +00:00
}
2024-04-05 10:08:36 +00:00
if ( s_settings_save_timer )
{
s_settings_save_timer - > deleteLater ( ) ;
s_settings_save_timer . release ( ) ;
}
2021-12-13 12:12:54 +00:00
}
2022-09-07 07:44:10 +00:00
void Host : : CommitBaseSettingChanges ( )
2021-12-13 12:12:54 +00:00
{
2022-09-07 07:44:10 +00:00
if ( ! QtHost : : IsOnUIThread ( ) )
{
QtHost : : RunOnUIThread ( & Host : : CommitBaseSettingChanges ) ;
return ;
}
auto lock = Host : : GetSettingsLock ( ) ;
2021-12-13 12:12:54 +00:00
if ( s_settings_save_timer )
return ;
s_settings_save_timer = std : : make_unique < QTimer > ( ) ;
2022-09-07 07:44:10 +00:00
s_settings_save_timer - > connect ( s_settings_save_timer . get ( ) , & QTimer : : timeout , & QtHost : : SaveSettings ) ;
2021-12-13 12:12:54 +00:00
s_settings_save_timer - > setSingleShot ( true ) ;
s_settings_save_timer - > start ( SETTINGS_SAVE_DELAY ) ;
}
2022-05-03 04:26:49 +00:00
bool QtHost : : InBatchMode ( )
{
return s_batch_mode ;
}
2022-07-09 09:14:48 +00:00
bool QtHost : : InNoGUIMode ( )
{
return s_nogui_mode ;
}
2022-09-07 07:44:10 +00:00
bool QtHost : : IsOnUIThread ( )
{
QThread * ui_thread = qApp - > thread ( ) ;
return ( QThread : : currentThread ( ) = = ui_thread ) ;
}
2022-11-23 13:20:49 +00:00
bool QtHost : : ShouldShowAdvancedSettings ( )
{
return Host : : GetBaseBoolSettingValue ( " UI " , " ShowAdvancedSettings " , false ) ;
}
2022-04-06 10:49:00 +00:00
void QtHost : : RunOnUIThread ( const std : : function < void ( ) > & func , bool block /*= false*/ )
{
// main window always exists, so it's fine to attach it to that.
2022-12-04 08:03:30 +00:00
QMetaObject : : invokeMethod ( g_main_window , " runOnUIThread " , block ? Qt : : BlockingQueuedConnection : Qt : : QueuedConnection ,
2022-04-06 10:49:00 +00:00
Q_ARG ( const std : : function < void ( ) > & , func ) ) ;
}
2022-09-07 07:44:10 +00:00
bool Host : : RequestResetSettings ( bool folders , bool core , bool controllers , bool hotkeys , bool ui )
{
{
auto lock = Host : : GetSettingsLock ( ) ;
2023-05-13 10:22:46 +00:00
VMManager : : SetDefaultSettings ( * s_base_settings_interface . get ( ) , folders , core , controllers , hotkeys , ui ) ;
2022-09-07 07:44:10 +00:00
}
Host : : CommitBaseSettingChanges ( ) ;
g_emu_thread - > applySettings ( ) ;
if ( folders )
g_emu_thread - > updateEmuFolders ( ) ;
return true ;
}
2022-05-11 08:24:56 +00:00
QString QtHost : : GetAppNameAndVersion ( )
{
2024-01-07 09:36:40 +00:00
return QStringLiteral ( " PCSX2 " GIT_REV ) ;
2022-05-11 08:24:56 +00:00
}
QString QtHost : : GetAppConfigSuffix ( )
{
# if defined(PCSX2_DEBUG)
return QStringLiteral ( " [Debug] " ) ;
# elif defined(PCSX2_DEVBUILD)
return QStringLiteral ( " [Devel] " ) ;
# else
return QString ( ) ;
# endif
}
2024-01-07 05:26:56 +00:00
QIcon QtHost : : GetAppIcon ( )
{
return QIcon ( QStringLiteral ( " :/icons/AppIcon64.png " ) ) ;
}
2022-06-04 05:53:31 +00:00
QString QtHost : : GetResourcesBasePath ( )
{
return QString : : fromStdString ( EmuFolders : : Resources ) ;
}
2024-01-07 05:26:56 +00:00
std : : string QtHost : : GetRuntimeDownloadedResourceURL ( std : : string_view name )
{
2024-03-13 09:51:44 +00:00
return fmt : : format ( " {}/{} " , RUNTIME_RESOURCES_URL , Path : : URLEncode ( name ) ) ;
2024-01-07 05:26:56 +00:00
}
2024-05-04 13:33:35 +00:00
bool QtHost : : SaveGameSettings ( SettingsInterface * sif , bool delete_if_empty )
{
INISettingsInterface * ini = static_cast < INISettingsInterface * > ( sif ) ;
Error error ;
// if there's no keys, just toss the whole thing out
if ( delete_if_empty & & ini - > IsEmpty ( ) )
{
INFO_LOG ( " Removing empty gamesettings ini {} " , Path : : GetFileName ( ini - > GetFileName ( ) ) ) ;
if ( FileSystem : : FileExists ( ini - > GetFileName ( ) . c_str ( ) ) & &
! FileSystem : : DeleteFilePath ( ini - > GetFileName ( ) . c_str ( ) , & error ) )
{
Host : : ReportErrorAsync (
TRANSLATE_SV ( " QtHost " , " Error " ) ,
fmt : : format ( TRANSLATE_FS ( " QtHost " , " An error occurred while deleting empty game settings: \n {} " ) ,
error . GetDescription ( ) ) ) ;
return false ;
}
return true ;
}
// clean unused sections, stops the file being bloated
sif - > RemoveEmptySections ( ) ;
if ( ! sif - > Save ( & error ) )
{
Host : : ReportErrorAsync (
TRANSLATE_SV ( " QtHost " , " Error " ) ,
fmt : : format ( TRANSLATE_FS ( " QtHost " , " An error occurred while saving game settings: \n {} " ) , error . GetDescription ( ) ) ) ;
return false ;
}
return true ;
}
2024-01-07 05:26:56 +00:00
std : : optional < bool > QtHost : : DownloadFile ( QWidget * parent , const QString & title , std : : string url , std : : vector < u8 > * data )
{
static constexpr u32 HTTP_POLL_INTERVAL = 10 ;
std : : unique_ptr < HTTPDownloader > http = HTTPDownloader : : Create ( Host : : GetHTTPUserAgent ( ) ) ;
if ( ! http )
{
QMessageBox : : critical ( parent , qApp - > translate ( " EmuThread " , " Error " ) , qApp - > translate ( " EmuThread " , " Failed to create HTTPDownloader. " ) ) ;
return false ;
}
std : : optional < bool > download_result ;
const std : : string : : size_type url_file_part_pos = url . rfind ( ' / ' ) ;
QtModalProgressCallback progress ( parent ) ;
progress . GetDialog ( ) . setLabelText (
2024-04-01 11:15:52 +00:00
qApp - > translate ( " EmuThread " , " Downloading %1... " ) . arg ( QtUtils : : StringViewToQString ( std : : string_view ( url ) . substr ( ( url_file_part_pos ! = std : : string : : npos ) ? ( url_file_part_pos + 1 ) : 0 ) ) ) ) ;
2024-01-07 05:26:56 +00:00
progress . GetDialog ( ) . setWindowTitle ( title ) ;
progress . GetDialog ( ) . setWindowIcon ( GetAppIcon ( ) ) ;
progress . SetCancellable ( true ) ;
http - > CreateRequest (
2024-01-08 10:45:05 +00:00
std : : move ( url ) , [ parent , data , & download_result ] ( s32 status_code , const std : : string & , std : : vector < u8 > hdata ) {
2024-01-07 05:26:56 +00:00
if ( status_code = = HTTPDownloader : : HTTP_STATUS_CANCELLED )
return ;
if ( status_code ! = HTTPDownloader : : HTTP_STATUS_OK )
{
QMessageBox : : critical ( parent , qApp - > translate ( " EmuThread " , " Error " ) ,
2024-01-07 10:14:01 +00:00
qApp - > translate ( " EmuThread " , " Download failed with HTTP status code %1. " ) . arg ( status_code ) ) ;
2024-01-07 05:26:56 +00:00
download_result = false ;
return ;
}
if ( hdata . empty ( ) )
{
QMessageBox : : critical ( parent , qApp - > translate ( " EmuThread " , " Error " ) ,
qApp - > translate ( " EmuThread " , " Download failed: Data is empty. " ) . arg ( status_code ) ) ;
2024-04-01 11:15:52 +00:00
download_result = false ;
return ;
2024-01-07 05:26:56 +00:00
}
* data = std : : move ( hdata ) ;
download_result = true ;
} ,
& progress ) ;
// Block until completion.
while ( http - > HasAnyRequests ( ) )
{
QApplication : : processEvents ( QEventLoop : : AllEvents , HTTP_POLL_INTERVAL ) ;
http - > PollRequests ( ) ;
}
return download_result ;
}
bool QtHost : : DownloadFile ( QWidget * parent , const QString & title , std : : string url , const std : : string & path )
{
std : : vector < u8 > data ;
if ( ! DownloadFile ( parent , title , std : : move ( url ) , & data ) . value_or ( false ) | | data . empty ( ) )
return false ;
// Directory may not exist. Create it.
const std : : string directory ( Path : : GetDirectory ( path ) ) ;
if ( ( ! directory . empty ( ) & & ! FileSystem : : DirectoryExists ( directory . c_str ( ) ) & &
! FileSystem : : CreateDirectoryPath ( directory . c_str ( ) , true ) ) | |
! FileSystem : : WriteBinaryFile ( path . c_str ( ) , data . data ( ) , data . size ( ) ) )
{
QMessageBox : : critical ( parent , qApp - > translate ( " EmuThread " , " Error " ) ,
qApp - > translate ( " EmuThread " , " Failed to write '%1'. " ) . arg ( QString : : fromStdString ( path ) ) ) ;
return false ;
}
return true ;
}
2024-05-14 23:42:40 +00:00
void Host : : ReportErrorAsync ( const std : : string_view title , const std : : string_view message )
2021-12-13 12:12:54 +00:00
{
if ( ! title . empty ( ) & & ! message . empty ( ) )
2024-05-14 23:42:40 +00:00
ERROR_LOG ( " ReportErrorAsync: {}: {} " , title , message ) ;
2021-12-13 12:12:54 +00:00
else if ( ! message . empty ( ) )
2024-05-14 23:42:40 +00:00
ERROR_LOG ( " ReportErrorAsync: {} " , message ) ;
2021-12-13 12:12:54 +00:00
QMetaObject : : invokeMethod ( g_main_window , " reportError " , Qt : : QueuedConnection ,
Q_ARG ( const QString & , title . empty ( ) ? QString ( ) : QString : : fromUtf8 ( title . data ( ) , title . size ( ) ) ) ,
Q_ARG ( const QString & , message . empty ( ) ? QString ( ) : QString : : fromUtf8 ( message . data ( ) , message . size ( ) ) ) ) ;
}
2024-05-14 23:42:40 +00:00
bool Host : : ConfirmMessage ( const std : : string_view title , const std : : string_view message )
2022-08-20 10:20:36 +00:00
{
const QString qtitle ( QString : : fromUtf8 ( title . data ( ) , title . size ( ) ) ) ;
const QString qmessage ( QString : : fromUtf8 ( message . data ( ) , message . size ( ) ) ) ;
return g_emu_thread - > confirmMessage ( qtitle , qmessage ) ;
}
2024-05-14 23:42:40 +00:00
void Host : : OpenURL ( const std : : string_view url )
2022-08-20 10:20:36 +00:00
{
2022-12-04 08:03:30 +00:00
QtHost : : RunOnUIThread ( [ url = QtUtils : : StringViewToQString ( url ) ] ( ) { QtUtils : : OpenURL ( g_main_window , QUrl ( url ) ) ; } ) ;
2022-08-20 10:20:36 +00:00
}
2024-05-14 23:42:40 +00:00
bool Host : : CopyTextToClipboard ( const std : : string_view text )
2022-08-20 10:20:36 +00:00
{
QtHost : : RunOnUIThread ( [ text = QtUtils : : StringViewToQString ( text ) ] ( ) {
QClipboard * clipboard = QGuiApplication : : clipboard ( ) ;
if ( clipboard )
clipboard - > setText ( text ) ;
} ) ;
return true ;
}
2022-09-03 12:29:36 +00:00
void Host : : BeginTextInput ( )
{
QInputMethod * method = qApp - > inputMethod ( ) ;
if ( method )
QMetaObject : : invokeMethod ( method , " show " , Qt : : QueuedConnection ) ;
}
void Host : : EndTextInput ( )
{
QInputMethod * method = qApp - > inputMethod ( ) ;
if ( method )
QMetaObject : : invokeMethod ( method , " hide " , Qt : : QueuedConnection ) ;
}
2022-10-15 09:03:52 +00:00
std : : optional < WindowInfo > Host : : GetTopLevelWindowInfo ( )
{
std : : optional < WindowInfo > ret ;
QMetaObject : : invokeMethod ( g_main_window , & MainWindow : : getWindowInfo , Qt : : BlockingQueuedConnection , & ret ) ;
return ret ;
}
2024-05-14 21:42:19 +00:00
void Host : : OnInputDeviceConnected ( const std : : string_view identifier , const std : : string_view device_name )
2021-12-13 12:12:54 +00:00
{
2022-12-04 08:03:30 +00:00
emit g_emu_thread - > onInputDeviceConnected ( identifier . empty ( ) ? QString ( ) : QString : : fromUtf8 ( identifier . data ( ) , identifier . size ( ) ) ,
2021-12-13 12:12:54 +00:00
device_name . empty ( ) ? QString ( ) : QString : : fromUtf8 ( device_name . data ( ) , device_name . size ( ) ) ) ;
2024-05-14 21:42:19 +00:00
if ( VMManager : : HasValidVM ( ) | | g_emu_thread - > isRunningFullscreenUI ( ) )
{
Host : : AddIconOSDMessage ( fmt : : format ( " controller_connected_{} " , identifier ) , ICON_FA_GAMEPAD ,
fmt : : format ( TRANSLATE_FS ( " QtHost " , " Controller {} connected. " ) , identifier ) ,
Host : : OSD_INFO_DURATION ) ;
}
2021-12-13 12:12:54 +00:00
}
2024-05-14 21:42:19 +00:00
void Host : : OnInputDeviceDisconnected ( const InputBindingKey key , const std : : string_view identifier )
2021-12-13 12:12:54 +00:00
{
2022-12-04 08:03:30 +00:00
emit g_emu_thread - > onInputDeviceDisconnected ( identifier . empty ( ) ? QString ( ) : QString : : fromUtf8 ( identifier . data ( ) , identifier . size ( ) ) ) ;
2024-05-14 21:42:19 +00:00
if ( VMManager : : GetState ( ) = = VMState : : Running & & Host : : GetBoolSettingValue ( " UI " , " PauseOnControllerDisconnection " , false ) & &
InputManager : : HasAnyBindingsForSource ( key ) )
{
std : : string message =
fmt : : format ( TRANSLATE_FS ( " QtHost " , " System paused because controller {} was disconnected. " ) , identifier ) ;
Host : : RunOnCPUThread ( [ message = QString : : fromStdString ( message ) ] ( ) {
VMManager : : SetPaused ( true ) ;
// has to be done after pause, otherwise pause message takes precedence
emit g_emu_thread - > statusMessage ( message ) ;
} ) ;
Host : : AddIconOSDMessage ( fmt : : format ( " controller_connected_{} " , identifier ) , ICON_FA_GAMEPAD , std : : move ( message ) ,
Host : : OSD_WARNING_DURATION ) ;
}
else if ( VMManager : : HasValidVM ( ) | | g_emu_thread - > isRunningFullscreenUI ( ) )
{
Host : : AddIconOSDMessage ( fmt : : format ( " controller_connected_{} " , identifier ) , ICON_FA_GAMEPAD ,
fmt : : format ( TRANSLATE_FS ( " QtHost " , " Controller {} disconnected. " ) , identifier ) ,
Host : : OSD_INFO_DURATION ) ;
}
2021-12-13 12:12:54 +00:00
}
2023-07-23 05:07:46 +00:00
void Host : : SetMouseMode ( bool relative_mode , bool hide_cursor )
2022-12-04 15:00:06 +00:00
{
2023-07-23 05:07:46 +00:00
emit g_emu_thread - > onMouseModeRequested ( relative_mode , hide_cursor ) ;
2022-12-04 15:00:06 +00:00
}
2024-06-09 16:27:38 +00:00
namespace {
class QtHostProgressCallback final : public BaseProgressCallback
{
public :
QtHostProgressCallback ( ) ;
~ QtHostProgressCallback ( ) override ;
__fi const std : : string & GetName ( ) const { return m_name ; }
void PushState ( ) override ;
void PopState ( ) override ;
bool IsCancelled ( ) const override ;
void SetCancellable ( bool cancellable ) override ;
void SetTitle ( const char * title ) override ;
void SetStatusText ( const char * text ) override ;
void SetProgressRange ( u32 range ) override ;
void SetProgressValue ( u32 value ) override ;
void DisplayError ( const char * message ) override ;
void DisplayWarning ( const char * message ) override ;
void DisplayInformation ( const char * message ) override ;
void DisplayDebugMessage ( const char * message ) override ;
void ModalError ( const char * message ) override ;
bool ModalConfirmation ( const char * message ) override ;
void ModalInformation ( const char * message ) override ;
void SetCancelled ( ) ;
private :
struct SharedData
{
QProgressDialog * dialog = nullptr ;
QString init_title ;
QString init_status_text ;
std : : atomic_bool cancelled { false } ;
bool cancellable = true ;
bool was_fullscreen = false ;
} ;
void EnsureHasData ( ) ;
static void EnsureDialogVisible ( const std : : shared_ptr < SharedData > & data ) ;
void Redraw ( bool force ) ;
std : : string m_name ;
std : : shared_ptr < SharedData > m_data ;
int m_last_progress_percent = - 1 ;
} ;
}
QtHostProgressCallback : : QtHostProgressCallback ( )
: BaseProgressCallback ( )
{
}
QtHostProgressCallback : : ~ QtHostProgressCallback ( )
{
if ( m_data )
{
QtHost : : RunOnUIThread ( [ data = m_data ] ( ) {
if ( ! data - > dialog )
return ;
data - > dialog - > close ( ) ;
delete data - > dialog ;
if ( data - > was_fullscreen )
g_emu_thread - > setFullscreen ( true , false ) ;
} ) ;
}
}
void QtHostProgressCallback : : PushState ( )
{
BaseProgressCallback : : PushState ( ) ;
}
void QtHostProgressCallback : : PopState ( )
{
BaseProgressCallback : : PopState ( ) ;
Redraw ( true ) ;
}
void QtHostProgressCallback : : SetCancellable ( bool cancellable )
{
BaseProgressCallback : : SetCancellable ( cancellable ) ;
EnsureHasData ( ) ;
m_data - > cancellable = cancellable ;
}
void QtHostProgressCallback : : SetTitle ( const char * title )
{
EnsureHasData ( ) ;
QtHost : : RunOnUIThread ( [ data = m_data , title = QString : : fromUtf8 ( title ) ] ( ) {
if ( data - > dialog )
data - > dialog - > setWindowTitle ( title ) ;
else
data - > init_title = title ;
} ) ;
}
void QtHostProgressCallback : : SetStatusText ( const char * text )
{
BaseProgressCallback : : SetStatusText ( text ) ;
EnsureHasData ( ) ;
QtHost : : RunOnUIThread ( [ data = m_data , text = QString : : fromUtf8 ( text ) ] ( ) {
if ( data - > dialog )
data - > dialog - > setLabelText ( text ) ;
else
data - > init_status_text = text ;
} ) ;
}
void QtHostProgressCallback : : SetProgressRange ( u32 range )
{
u32 last_range = m_progress_range ;
BaseProgressCallback : : SetProgressRange ( range ) ;
if ( m_progress_range ! = last_range )
Redraw ( false ) ;
}
void QtHostProgressCallback : : SetProgressValue ( u32 value )
{
u32 lastValue = m_progress_value ;
BaseProgressCallback : : SetProgressValue ( value ) ;
if ( m_progress_value ! = lastValue )
Redraw ( false ) ;
}
void QtHostProgressCallback : : Redraw ( bool force )
{
const int percent = static_cast < int > ( ( static_cast < float > ( m_progress_value ) / static_cast < float > ( m_progress_range ) ) * 100.0f ) ;
if ( percent = = m_last_progress_percent & & ! force )
return ;
// If this is the emu uthread, we need to process the un-fullscreen message.
if ( g_emu_thread - > isOnEmuThread ( ) )
Host : : PumpMessagesOnCPUThread ( ) ;
m_last_progress_percent = percent ;
EnsureHasData ( ) ;
QtHost : : RunOnUIThread ( [ data = m_data , percent ] ( ) {
EnsureDialogVisible ( data ) ;
data - > dialog - > setValue ( percent ) ;
} ) ;
}
void QtHostProgressCallback : : DisplayError ( const char * message )
{
Console . Error ( message ) ;
Host : : ReportErrorAsync ( " Error " , message ) ;
}
void QtHostProgressCallback : : DisplayWarning ( const char * message )
{
Console . Warning ( message ) ;
}
void QtHostProgressCallback : : DisplayInformation ( const char * message )
{
Console . WriteLn ( message ) ;
}
void QtHostProgressCallback : : DisplayDebugMessage ( const char * message )
{
DevCon . WriteLn ( message ) ;
}
void QtHostProgressCallback : : ModalError ( const char * message )
{
Console . Error ( message ) ;
Host : : ReportErrorAsync ( " Error " , message ) ;
}
bool QtHostProgressCallback : : ModalConfirmation ( const char * message )
{
return false ;
}
void QtHostProgressCallback : : ModalInformation ( const char * message )
{
Console . WriteLn ( message ) ;
}
void QtHostProgressCallback : : SetCancelled ( )
{
// not done here
}
bool QtHostProgressCallback : : IsCancelled ( ) const
{
return m_data & & m_data - > cancelled . load ( std : : memory_order_acquire ) ;
}
void QtHostProgressCallback : : EnsureHasData ( )
{
if ( ! m_data )
m_data = std : : make_shared < SharedData > ( ) ;
}
void QtHostProgressCallback : : EnsureDialogVisible ( const std : : shared_ptr < SharedData > & data )
{
pxAssert ( data ) ;
if ( data - > dialog )
return ;
data - > was_fullscreen = g_emu_thread - > isFullscreen ( ) ;
if ( data - > was_fullscreen )
g_emu_thread - > setFullscreen ( false , true ) ;
data - > dialog = new QProgressDialog ( data - > init_status_text ,
data - > cancellable ? qApp - > translate ( " QtHost " , " Cancel " ) : QString ( ) ,
0 , 100 , g_main_window ) ;
if ( data - > cancellable )
{
data - > dialog - > connect ( data - > dialog , & QProgressDialog : : canceled ,
[ data ] ( ) { data - > cancelled . store ( true , std : : memory_order_release ) ; } ) ;
}
data - > dialog - > setWindowIcon ( QtHost : : GetAppIcon ( ) ) ;
data - > dialog - > setMinimumWidth ( 400 ) ;
data - > dialog - > show ( ) ;
data - > dialog - > raise ( ) ;
data - > dialog - > activateWindow ( ) ;
if ( ! data - > init_title . isEmpty ( ) )
{
data - > dialog - > setWindowTitle ( data - > init_title ) ;
data - > init_title = QString ( ) ;
}
}
std : : unique_ptr < ProgressCallback > Host : : CreateHostProgressCallback ( )
{
return std : : make_unique < QtHostProgressCallback > ( ) ;
}
2022-09-07 09:20:50 +00:00
//////////////////////////////////////////////////////////////////////////
// Hotkeys
//////////////////////////////////////////////////////////////////////////
BEGIN_HOTKEY_LIST ( g_host_hotkeys )
END_HOTKEY_LIST ( )
2021-12-13 12:12:54 +00:00
//////////////////////////////////////////////////////////////////////////
// Interface Stuff
//////////////////////////////////////////////////////////////////////////
2022-03-04 10:40:03 +00:00
static void SignalHandler ( int signal )
{
// First try the normal (graceful) shutdown/exit.
static bool graceful_shutdown_attempted = false ;
if ( ! graceful_shutdown_attempted & & g_main_window )
{
std : : fprintf ( stderr , " Received CTRL+C, attempting graceful shutdown. Press CTRL+C again to force. \n " ) ;
graceful_shutdown_attempted = true ;
// This could be a bit risky invoking from a signal handler... hopefully it's okay.
2023-01-28 03:33:51 +00:00
QMetaObject : : invokeMethod ( g_main_window , " requestExit " , Qt : : QueuedConnection , Q_ARG ( bool , false ) ) ;
2022-03-04 10:40:03 +00:00
return ;
}
std : : signal ( signal , SIG_DFL ) ;
// MacOS is missing std::quick_exit() despite it being C++11...
# ifndef __APPLE__
std : : quick_exit ( 1 ) ;
# else
_Exit ( 1 ) ;
# endif
}
2024-01-11 08:08:16 +00:00
# ifdef _WIN32
static BOOL WINAPI ConsoleCtrlHandler ( DWORD dwCtrlType )
{
if ( dwCtrlType ! = CTRL_C_EVENT )
return FALSE ;
SignalHandler ( SIGTERM ) ;
return TRUE ;
}
# endif
2022-03-04 10:40:03 +00:00
void QtHost : : HookSignals ( )
{
std : : signal ( SIGINT , SignalHandler ) ;
std : : signal ( SIGTERM , SignalHandler ) ;
2022-04-18 13:35:14 +00:00
2024-01-11 08:08:16 +00:00
# if defined(_WIN32)
SetConsoleCtrlHandler ( ConsoleCtrlHandler , FALSE ) ;
# elif defined(__linux__)
2022-04-18 13:35:14 +00:00
// Ignore SIGCHLD by default on Linux, since we kick off aplay asynchronously.
struct sigaction sa_chld = { } ;
sigemptyset ( & sa_chld . sa_mask ) ;
sa_chld . sa_flags = SA_SIGINFO | SA_RESTART | SA_NOCLDSTOP | SA_NOCLDWAIT ;
sigaction ( SIGCHLD , & sa_chld , nullptr ) ;
# endif
2022-03-04 10:40:03 +00:00
}
2022-07-09 09:07:28 +00:00
2024-01-11 08:08:16 +00:00
void QtHost : : InitializeEarlyConsole ( )
{
Log : : SetConsoleOutputLevel ( LOGLEVEL_DEBUG ) ;
}
2022-07-09 09:07:28 +00:00
void QtHost : : PrintCommandLineVersion ( )
{
2024-01-11 08:08:16 +00:00
InitializeEarlyConsole ( ) ;
2022-07-09 09:07:28 +00:00
std : : fprintf ( stderr , " %s \n " , ( GetAppNameAndVersion ( ) + GetAppConfigSuffix ( ) ) . toUtf8 ( ) . constData ( ) ) ;
std : : fprintf ( stderr , " https://pcsx2.net/ \n " ) ;
std : : fprintf ( stderr , " \n " ) ;
}
2024-05-14 23:42:40 +00:00
void QtHost : : PrintCommandLineHelp ( const std : : string_view progname )
2022-07-09 09:07:28 +00:00
{
PrintCommandLineVersion ( ) ;
2022-09-17 12:47:35 +00:00
fmt : : print ( stderr , " Usage: {} [parameters] [--] [boot filename] \n " , progname ) ;
2022-07-09 09:07:28 +00:00
std : : fprintf ( stderr , " \n " ) ;
std : : fprintf ( stderr , " -help: Displays this information and exits. \n " ) ;
std : : fprintf ( stderr , " -version: Displays version information and exits. \n " ) ;
std : : fprintf ( stderr , " -batch: Enables batch mode (exits after shutting down). \n " ) ;
2022-07-09 09:14:48 +00:00
std : : fprintf ( stderr , " -nogui: Hides main window while running (implies batch mode). \n " ) ;
2022-07-09 09:07:28 +00:00
std : : fprintf ( stderr , " -elf <file>: Overrides the boot ELF with the specified filename. \n " ) ;
2023-12-23 20:32:26 +00:00
std : : fprintf ( stderr , " -gameargs <string>: passes the specified quoted space-delimited string of launch arguments. \n " ) ;
2022-07-09 09:07:28 +00:00
std : : fprintf ( stderr , " -disc <path>: Uses the specified host DVD drive as a source. \n " ) ;
2022-10-22 11:33:41 +00:00
std : : fprintf ( stderr , " -logfile <path>: Writes the application log to path instead of emulog.txt. \n " ) ;
2022-07-09 09:07:28 +00:00
std : : fprintf ( stderr , " -bios: Starts the BIOS (System Menu/OSDSYS). \n " ) ;
std : : fprintf ( stderr , " -fastboot: Force fast boot for provided filename. \n " ) ;
std : : fprintf ( stderr , " -slowboot: Force slow boot for provided filename. \n " ) ;
std : : fprintf ( stderr , " -state <index>: Loads specified save state by index. \n " ) ;
std : : fprintf ( stderr , " -statefile <filename>: Loads state from the specified filename. \n " ) ;
std : : fprintf ( stderr , " -fullscreen: Enters fullscreen mode immediately after starting. \n " ) ;
std : : fprintf ( stderr , " -nofullscreen: Prevents fullscreen mode from triggering if enabled. \n " ) ;
2023-06-07 17:00:36 +00:00
std : : fprintf ( stderr , " -bigpicture: Forces PCSX2 to use the Big Picture mode (useful for controller-only and couch play). \n " ) ;
2022-07-09 09:07:28 +00:00
std : : fprintf ( stderr , " -earlyconsolelog: Forces logging of early console messages to console. \n " ) ;
2022-11-23 11:21:20 +00:00
std : : fprintf ( stderr , " -testconfig: Initializes configuration and checks version, then exits. \n " ) ;
2023-06-23 13:18:10 +00:00
std : : fprintf ( stderr , " -setupwizard: Forces initial setup wizard to run. \n " ) ;
2023-02-01 11:53:31 +00:00
std : : fprintf ( stderr , " -debugger: Open debugger and break on entry point. \n " ) ;
2022-04-18 13:35:14 +00:00
# ifdef ENABLE_RAINTEGRATION
std : : fprintf ( stderr , " -raintegration: Use RAIntegration instead of built-in achievement support. \n " ) ;
# endif
2022-07-09 09:07:28 +00:00
std : : fprintf ( stderr , " --: Signals that no more arguments will follow and the remaining \n "
" parameters make up the filename. Use when the filename contains \n "
" spaces or starts with a dash. \n " ) ;
std : : fprintf ( stderr , " \n " ) ;
}
std : : shared_ptr < VMBootParameters > & QtHost : : AutoBoot ( std : : shared_ptr < VMBootParameters > & autoboot )
{
if ( ! autoboot )
autoboot = std : : make_shared < VMBootParameters > ( ) ;
return autoboot ;
}
2022-09-17 12:47:35 +00:00
bool QtHost : : ParseCommandLineOptions ( const QStringList & args , std : : shared_ptr < VMBootParameters > & autoboot )
2022-07-09 09:07:28 +00:00
{
bool no_more_args = false ;
2022-09-17 12:47:35 +00:00
if ( args . empty ( ) )
{
// Nothing to do here.
return true ;
}
for ( auto it = std : : next ( args . begin ( ) ) ; it ! = args . end ( ) ; + + it )
2022-07-09 09:07:28 +00:00
{
if ( ! no_more_args )
{
2022-09-17 12:47:35 +00:00
# define CHECK_ARG(str) (*it == str)
# define CHECK_ARG_PARAM(str) (*it == str && std::next(it) != args.end())
2022-07-09 09:07:28 +00:00
2022-09-17 12:47:35 +00:00
if ( CHECK_ARG ( QStringLiteral ( " -help " ) ) )
2022-07-09 09:07:28 +00:00
{
2022-09-17 12:47:35 +00:00
PrintCommandLineHelp ( args . front ( ) . toStdString ( ) ) ;
2022-07-09 09:07:28 +00:00
return false ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -version " ) ) )
2022-07-09 09:07:28 +00:00
{
PrintCommandLineVersion ( ) ;
return false ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -batch " ) ) )
2022-07-09 09:07:28 +00:00
{
s_batch_mode = true ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -nogui " ) ) )
2022-07-09 09:14:48 +00:00
{
s_batch_mode = true ;
s_nogui_mode = true ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -fastboot " ) ) )
2022-07-09 09:07:28 +00:00
{
AutoBoot ( autoboot ) - > fast_boot = true ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -slowboot " ) ) )
2022-07-09 09:07:28 +00:00
{
AutoBoot ( autoboot ) - > fast_boot = false ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG_PARAM ( QStringLiteral ( " -state " ) ) )
2022-07-09 09:07:28 +00:00
{
2022-09-17 12:47:35 +00:00
AutoBoot ( autoboot ) - > state_index = ( + + it ) - > toInt ( ) ;
2022-07-09 09:07:28 +00:00
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG_PARAM ( QStringLiteral ( " -statefile " ) ) )
2022-07-09 09:07:28 +00:00
{
2022-09-17 12:47:35 +00:00
AutoBoot ( autoboot ) - > save_state = ( + + it ) - > toStdString ( ) ;
2022-07-09 09:07:28 +00:00
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG_PARAM ( QStringLiteral ( " -elf " ) ) )
2022-07-09 09:07:28 +00:00
{
2022-09-17 12:47:35 +00:00
AutoBoot ( autoboot ) - > elf_override = ( + + it ) - > toStdString ( ) ;
2022-07-09 09:07:28 +00:00
continue ;
}
2023-12-23 20:32:26 +00:00
else if ( CHECK_ARG_PARAM ( QStringLiteral ( " -gameargs " ) ) )
{
EmuConfig . CurrentGameArgs = ( + + it ) - > toStdString ( ) ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG_PARAM ( QStringLiteral ( " -disc " ) ) )
2022-07-09 09:07:28 +00:00
{
AutoBoot ( autoboot ) - > source_type = CDVD_SourceType : : Disc ;
2022-09-17 12:47:35 +00:00
AutoBoot ( autoboot ) - > filename = ( + + it ) - > toStdString ( ) ;
2022-07-09 09:07:28 +00:00
continue ;
}
2022-10-22 11:33:41 +00:00
else if ( CHECK_ARG_PARAM ( QStringLiteral ( " -logfile " ) ) )
{
2024-01-11 08:08:16 +00:00
VMManager : : Internal : : SetFileLogPath ( ( + + it ) - > toStdString ( ) ) ;
2022-10-22 11:33:41 +00:00
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -bios " ) ) )
2022-07-09 09:07:28 +00:00
{
AutoBoot ( autoboot ) - > source_type = CDVD_SourceType : : NoDisc ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -fullscreen " ) ) )
2022-07-09 09:07:28 +00:00
{
AutoBoot ( autoboot ) - > fullscreen = true ;
2022-05-15 08:20:21 +00:00
s_start_fullscreen_ui_fullscreen = true ;
2022-07-09 09:07:28 +00:00
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -nofullscreen " ) ) )
2022-07-09 09:07:28 +00:00
{
AutoBoot ( autoboot ) - > fullscreen = false ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -earlyconsolelog " ) ) )
2022-07-09 09:07:28 +00:00
{
2024-01-11 08:08:16 +00:00
InitializeEarlyConsole ( ) ;
2022-07-09 09:07:28 +00:00
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -bigpicture " ) ) )
2022-05-15 08:20:21 +00:00
{
s_start_fullscreen_ui = true ;
continue ;
}
2022-11-23 11:21:20 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -testconfig " ) ) )
{
s_test_config_and_exit = true ;
continue ;
}
2023-06-23 13:18:10 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -setupwizard " ) ) )
{
s_run_setup_wizard = true ;
continue ;
}
2023-02-01 11:53:31 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -debugger " ) ) )
{
s_boot_and_debug = true ;
continue ;
}
2023-03-21 13:13:31 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -updatecleanup " ) ) )
{
2024-01-14 07:09:49 +00:00
s_cleanup_after_update = AutoUpdaterDialog : : isSupported ( ) ;
2023-03-21 13:13:31 +00:00
continue ;
}
2022-04-18 13:35:14 +00:00
# ifdef ENABLE_RAINTEGRATION
else if ( CHECK_ARG ( QStringLiteral ( " -raintegration " ) ) )
{
Achievements : : SwitchToRAIntegration ( ) ;
continue ;
}
# endif
2022-09-17 12:47:35 +00:00
else if ( CHECK_ARG ( QStringLiteral ( " -- " ) ) )
2022-07-09 09:07:28 +00:00
{
no_more_args = true ;
continue ;
}
2022-09-17 12:47:35 +00:00
else if ( ( * it ) [ 0 ] = = ' - ' )
2022-07-09 09:07:28 +00:00
{
2022-09-17 12:47:35 +00:00
QMessageBox : : critical ( nullptr , QStringLiteral ( " Error " ) , QStringLiteral ( " Unknown parameter: '%1' " ) . arg ( * it ) ) ;
2022-07-09 09:07:28 +00:00
return false ;
}
# undef CHECK_ARG
# undef CHECK_ARG_PARAM
}
if ( ! AutoBoot ( autoboot ) - > filename . empty ( ) )
AutoBoot ( autoboot ) - > filename + = ' ' ;
2022-09-17 12:47:35 +00:00
AutoBoot ( autoboot ) - > filename + = it - > toStdString ( ) ;
2022-07-09 09:07:28 +00:00
}
// check autoboot parameters, if we set something like fullscreen without a bios
// or disc, we don't want to actually start.
if ( autoboot & & ! autoboot - > source_type . has_value ( ) & & autoboot - > filename . empty ( ) & & autoboot - > elf_override . empty ( ) )
{
Console . Warning ( " Skipping autoboot due to no boot parameters. " ) ;
autoboot . reset ( ) ;
}
// if we don't have autoboot, we definitely don't want batch mode (because that'll skip
// scanning the game list).
2022-05-15 08:20:21 +00:00
if ( s_batch_mode & & ! s_start_fullscreen_ui & & ! autoboot )
2022-07-09 09:07:28 +00:00
{
2022-12-04 08:03:30 +00:00
QMessageBox : : critical ( nullptr , QStringLiteral ( " Error " ) ,
s_nogui_mode ? QStringLiteral ( " Cannot use no-gui mode, because no boot filename was specified. " ) :
2023-12-23 20:32:26 +00:00
QStringLiteral ( " Cannot use batch mode, because no boot filename was specified. " ) ) ;
2022-07-09 09:07:28 +00:00
return false ;
}
return true ;
}
# ifndef _WIN32
// See note in EarlyHardwareChecks.cpp as to why we don't do this on Windows.
static bool PerformEarlyHardwareChecks ( )
{
// NOTE: No point translating this message, because the configuration isn't loaded yet, so we
// won't know which language to use, and loading the configuration uses float instructions.
const char * error ;
if ( VMManager : : PerformEarlyHardwareChecks ( & error ) )
return true ;
QMessageBox : : critical ( nullptr , QStringLiteral ( " Hardware Check Failed " ) , QString : : fromUtf8 ( error ) ) ;
return false ;
}
# endif
2023-06-23 13:18:10 +00:00
void QtHost : : RegisterTypes ( )
2022-07-09 09:07:28 +00:00
{
qRegisterMetaType < std : : optional < bool > > ( ) ;
2022-10-15 09:03:52 +00:00
qRegisterMetaType < std : : optional < WindowInfo > > ( " std::optional<WindowInfo>() " ) ;
2022-08-26 03:37:56 +00:00
// Bit of fun with metatype names
// On Windows, the real type name here is "std::function<void __cdecl(void)>"
// Normally, the fact that we `Q_DECLARE_METATYPE(std::function<void()>);` in QtHost.h would make it also register under "std::function<void()>"
// The metatype is a pointer to `QMetaTypeInterfaceWrapper<std::function<void()>>::metaType`, which contains a pointer to the function that would register the alternate name
// But to anyone who can't see QtHost.h, that pointer should be null, opening us up to ODR violations
// Turns out some of our automoc files also instantiate that metaType (with the null pointer), so if we try to rely on it, everything will break if we get unlucky with link order
// Instead, manually register under the desired name:
2022-09-07 09:20:50 +00:00
qRegisterMetaType < std : : function < void ( ) > > ( " std::function<void()> " ) ;
2022-07-09 09:07:28 +00:00
qRegisterMetaType < std : : shared_ptr < VMBootParameters > > ( ) ;
qRegisterMetaType < GSRendererType > ( ) ;
qRegisterMetaType < InputBindingKey > ( ) ;
qRegisterMetaType < CDVD_SourceType > ( ) ;
qRegisterMetaType < const GameList : : Entry * > ( ) ;
2023-07-27 09:24:55 +00:00
qRegisterMetaType < Achievements : : LoginRequestReason > ( ) ;
2022-07-09 09:07:28 +00:00
}
2023-06-23 13:18:10 +00:00
bool QtHost : : RunSetupWizard ( )
{
SetupWizardDialog dialog ;
if ( dialog . exec ( ) = = QDialog : : Rejected )
return false ;
// Remove the flag.
Host : : SetBaseBoolSettingValue ( " UI " , " SetupWizardIncomplete " , false ) ;
Host : : CommitBaseSettingChanges ( ) ;
return true ;
}
2022-07-09 09:07:28 +00:00
int main ( int argc , char * argv [ ] )
{
CrashHandler : : Install ( ) ;
QGuiApplication : : setHighDpiScaleFactorRoundingPolicy ( Qt : : HighDpiScaleFactorRoundingPolicy : : PassThrough ) ;
2023-06-23 13:18:10 +00:00
QtHost : : RegisterTypes ( ) ;
2022-07-09 09:07:28 +00:00
QApplication app ( argc , argv ) ;
# ifndef _WIN32
if ( ! PerformEarlyHardwareChecks ( ) )
return EXIT_FAILURE ;
# endif
std : : shared_ptr < VMBootParameters > autoboot ;
2022-09-17 12:47:35 +00:00
if ( ! QtHost : : ParseCommandLineOptions ( app . arguments ( ) , autoboot ) )
2022-07-09 09:07:28 +00:00
return EXIT_FAILURE ;
// Bail out if we can't find any config.
if ( ! QtHost : : InitializeConfig ( ) )
return EXIT_FAILURE ;
2022-11-23 11:21:20 +00:00
// Are we just setting up the configuration?
if ( s_test_config_and_exit )
return EXIT_SUCCESS ;
2024-01-14 07:09:49 +00:00
// Remove any previous-version remanants.
if ( s_cleanup_after_update )
AutoUpdaterDialog : : cleanupAfterUpdate ( ) ;
2022-07-24 13:52:22 +00:00
// Set theme before creating any windows.
2023-04-25 10:24:22 +00:00
QtHost : : UpdateApplicationTheme ( ) ;
2022-07-24 13:52:22 +00:00
2024-01-11 09:38:28 +00:00
// Start logging early.
LogWindow : : updateSettings ( ) ;
2022-07-24 13:52:22 +00:00
// Start up the CPU thread.
2022-07-09 09:07:28 +00:00
QtHost : : HookSignals ( ) ;
EmuThread : : start ( ) ;
2023-06-23 13:18:10 +00:00
// Optionally run setup wizard.
int result ;
if ( s_run_setup_wizard & & ! QtHost : : RunSetupWizard ( ) )
{
result = EXIT_FAILURE ;
goto shutdown_and_exit ;
}
2022-06-23 11:58:14 +00:00
// Create all window objects, the emuthread might still be starting up at this point.
2023-06-23 13:18:10 +00:00
g_main_window = new MainWindow ( ) ;
g_main_window - > initialize ( ) ;
2022-07-09 09:07:28 +00:00
2022-06-23 11:58:14 +00:00
// When running in batch mode, ensure game list is loaded, but don't scan for any new files.
2022-07-09 09:07:28 +00:00
if ( ! s_batch_mode )
2023-06-23 13:18:10 +00:00
g_main_window - > refreshGameList ( false ) ;
2022-06-23 11:58:14 +00:00
else
GameList : : Refresh ( false , true ) ;
2022-07-09 09:07:28 +00:00
2022-06-23 11:58:14 +00:00
// Don't bother showing the window in no-gui mode.
2022-07-09 09:14:48 +00:00
if ( ! s_nogui_mode )
2024-01-11 09:38:28 +00:00
{
2023-06-23 13:18:10 +00:00
g_main_window - > show ( ) ;
2024-01-11 09:38:28 +00:00
g_main_window - > raise ( ) ;
g_main_window - > activateWindow ( ) ;
}
2022-07-09 09:07:28 +00:00
2022-05-15 08:20:21 +00:00
// Initialize big picture mode if requested.
if ( s_start_fullscreen_ui )
g_emu_thread - > startFullscreenUI ( s_start_fullscreen_ui_fullscreen ) ;
2023-02-01 11:53:31 +00:00
if ( s_boot_and_debug )
{
DebugInterface : : setPauseOnEntry ( true ) ;
2023-06-23 13:18:10 +00:00
g_main_window - > openDebugger ( ) ;
2023-02-01 11:53:31 +00:00
}
2022-06-23 11:58:14 +00:00
// Skip the update check if we're booting a game directly.
2022-07-09 09:07:28 +00:00
if ( autoboot )
g_emu_thread - > startVM ( std : : move ( autoboot ) ) ;
2022-05-15 08:20:21 +00:00
else if ( ! s_nogui_mode )
2023-06-23 13:18:10 +00:00
g_main_window - > startupUpdateCheck ( ) ;
2022-07-09 09:07:28 +00:00
// This doesn't return until we exit.
2023-06-23 13:18:10 +00:00
result = app . exec ( ) ;
2022-07-09 09:07:28 +00:00
2023-06-23 13:18:10 +00:00
shutdown_and_exit :
2022-07-09 09:07:28 +00:00
// Shutting down.
EmuThread : : stop ( ) ;
if ( g_main_window )
{
g_main_window - > close ( ) ;
delete g_main_window ;
}
2022-11-23 11:21:20 +00:00
// Ensure config is written. Prevents destruction order issues.
if ( s_base_settings_interface - > IsDirty ( ) )
s_base_settings_interface - > Save ( ) ;
2022-07-09 09:07:28 +00:00
return result ;
}