dolphin/Source/Core/DolphinQt2/MainWindow.cpp

822 lines
25 KiB
C++
Raw Normal View History

2015-11-27 08:33:07 +00:00
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <QApplication>
2017-06-24 15:00:37 +00:00
#include <QCloseEvent>
2015-11-27 08:33:07 +00:00
#include <QDir>
2017-06-26 21:22:40 +00:00
#include <QDragEnterEvent>
#include <QDropEvent>
2015-11-27 08:33:07 +00:00
#include <QFileDialog>
2017-06-26 21:22:40 +00:00
#include <QFileInfo>
2015-11-27 08:33:07 +00:00
#include <QIcon>
#include <QMessageBox>
2017-06-26 21:22:40 +00:00
#include <QMimeData>
2015-11-27 08:33:07 +00:00
#include "Common/Common.h"
Boot: Clean up the boot code * Move out boot parameters to a separate struct, which is not part of SConfig/ConfigManager because there is no reason for it to be there. * Move out file name parsing and constructing the appropriate params from paths to a separate function that does that, and only that. * For every different boot type we support, add a proper struct with only the required parameters, with descriptive names and use std::variant to only store what we need. * Clean up the bHLE_BS2 stuff which made no sense sometimes. Now instead of using bHLE_BS2 for two different things, both for storing the user config setting and as a runtime boot parameter, we simply replace the Disc boot params with BootParameters::IPL. * Const correctness so it's clear what can or cannot update the config. * Drop unused parameters and unneeded checks. * Make a few checks a lot more concise. (Looking at you, extension checks for disc images.) * Remove a mildly terrible workaround where we needed to pass an empty string in order to boot the GC IPL without any game inserted. (Not required anymore thanks to std::variant and std::optional.) The motivation for this are multiple: cleaning up and being able to add support for booting an installed NAND title. Without this change, it'd be pretty much impossible to implement that. Also, using std::visit with std::variant makes the compiler do additional type checks: now we're guaranteed that the boot code will handle all boot types and no invalid boot type will be possible.
2017-05-27 13:43:40 +00:00
#include "Core/Boot/Boot.h"
2015-11-27 08:33:07 +00:00
#include "Core/BootManager.h"
2017-07-06 09:01:32 +00:00
#include "Core/CommonTitles.h"
2017-07-21 20:48:21 +00:00
#include "Core/Config/NetplaySettings.h"
#include "Core/ConfigManager.h"
2015-11-27 08:33:07 +00:00
#include "Core/Core.h"
2017-05-23 20:12:01 +00:00
#include "Core/HW/GCKeyboard.h"
#include "Core/HW/GCPad.h"
#include "Core/HW/ProcessorInterface.h"
2017-05-23 20:12:01 +00:00
#include "Core/HW/Wiimote.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
#include "Core/HotkeyManager.h"
#include "Core/Movie.h"
2017-07-21 20:48:21 +00:00
#include "Core/NetPlayClient.h"
2017-05-09 16:49:10 +00:00
#include "Core/NetPlayProto.h"
2017-07-21 20:48:21 +00:00
#include "Core/NetPlayServer.h"
#include "Core/State.h"
#include "DolphinQt2/AboutDialog.h"
2017-05-09 16:49:10 +00:00
#include "DolphinQt2/Config/ControllersWindow.h"
#include "DolphinQt2/Config/Graphics/GraphicsWindow.h"
#include "DolphinQt2/Config/LoggerWidget.h"
#include "DolphinQt2/Config/Mapping/MappingWindow.h"
#include "DolphinQt2/Config/SettingsWindow.h"
2015-11-27 08:33:07 +00:00
#include "DolphinQt2/Host.h"
#include "DolphinQt2/HotkeyScheduler.h"
2015-11-27 08:33:07 +00:00
#include "DolphinQt2/MainWindow.h"
2017-07-21 20:48:21 +00:00
#include "DolphinQt2/NetPlay/NetPlayDialog.h"
#include "DolphinQt2/NetPlay/NetPlaySetupDialog.h"
#include "DolphinQt2/QtUtils/WindowActivationEventFilter.h"
2015-11-27 08:33:07 +00:00
#include "DolphinQt2/Resources.h"
#include "DolphinQt2/Settings.h"
#include "DolphinQt2/WiiUpdate.h"
2015-11-27 08:33:07 +00:00
2017-05-23 20:12:01 +00:00
#include "InputCommon/ControllerInterface/ControllerInterface.h"
2017-06-24 15:00:37 +00:00
#include "UICommon/UICommon.h"
#if defined(HAVE_XRANDR) && HAVE_XRANDR
#include <qpa/qplatformnativeinterface.h>
#include "UICommon/X11Utils.h"
#endif
2015-11-27 08:33:07 +00:00
MainWindow::MainWindow() : QMainWindow(nullptr)
{
setWindowTitle(QString::fromStdString(scm_rev_str));
setWindowIcon(QIcon(Resources::GetMisc(Resources::LOGO_SMALL)));
setUnifiedTitleAndToolBarOnMac(true);
2017-06-26 21:22:40 +00:00
setAcceptDrops(true);
2015-11-27 08:33:07 +00:00
CreateComponents();
2016-02-10 04:42:06 +00:00
ConnectGameList();
ConnectToolBar();
ConnectRenderWidget();
ConnectStack();
ConnectMenuBar();
2017-05-23 20:12:01 +00:00
InitControllers();
InitCoreCallbacks();
2017-07-21 20:48:21 +00:00
NetPlayInit();
2015-11-27 08:33:07 +00:00
}
2016-02-15 01:04:16 +00:00
MainWindow::~MainWindow()
{
m_render_widget->deleteLater();
ShutdownControllers();
2016-02-15 01:04:16 +00:00
}
2017-05-23 20:12:01 +00:00
void MainWindow::InitControllers()
{
if (g_controller_interface.IsInit())
return;
g_controller_interface.Initialize(reinterpret_cast<void*>(winId()));
Pad::Initialize();
Keyboard::Initialize();
Wiimote::Initialize(Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES);
m_hotkey_scheduler = new HotkeyScheduler();
m_hotkey_scheduler->Start();
ConnectHotkeys();
2017-05-23 20:12:01 +00:00
}
void MainWindow::ShutdownControllers()
{
m_hotkey_scheduler->Stop();
g_controller_interface.Shutdown();
Pad::Shutdown();
Keyboard::Shutdown();
Wiimote::Shutdown();
HotkeyManagerEmu::Shutdown();
m_hotkey_scheduler->deleteLater();
}
void MainWindow::InitCoreCallbacks()
{
Core::SetOnStoppedCallback([this] { emit EmulationStopped(); });
2017-06-24 15:00:37 +00:00
installEventFilter(this);
m_render_widget->installEventFilter(this);
}
static void InstallHotkeyFilter(QWidget* dialog)
{
auto* filter = new WindowActivationEventFilter();
dialog->installEventFilter(filter);
filter->connect(filter, &WindowActivationEventFilter::windowDeactivated,
[] { HotkeyManagerEmu::Enable(true); });
filter->connect(filter, &WindowActivationEventFilter::windowActivated,
[] { HotkeyManagerEmu::Enable(false); });
}
2016-02-10 04:42:06 +00:00
void MainWindow::CreateComponents()
2015-11-27 08:33:07 +00:00
{
m_menu_bar = new MenuBar(this);
m_tool_bar = new ToolBar(this);
m_game_list = new GameList(this);
m_render_widget = new RenderWidget;
m_stack = new QStackedWidget(this);
2017-05-09 16:49:10 +00:00
m_controllers_window = new ControllersWindow(this);
m_settings_window = new SettingsWindow(this);
m_hotkey_window = new MappingWindow(this, 0);
m_logger_widget = new LoggerWidget(this);
2017-06-21 08:27:21 +00:00
connect(this, &MainWindow::EmulationStarted, m_settings_window,
&SettingsWindow::EmulationStarted);
connect(this, &MainWindow::EmulationStopped, m_settings_window,
&SettingsWindow::EmulationStopped);
#if defined(HAVE_XRANDR) && HAVE_XRANDR
m_graphics_window = new GraphicsWindow(
new X11Utils::XRRConfiguration(
static_cast<Display*>(QGuiApplication::platformNativeInterface()->nativeResourceForWindow(
"display", windowHandle())),
winId()),
this);
#else
m_graphics_window = new GraphicsWindow(nullptr, this);
#endif
InstallHotkeyFilter(m_hotkey_window);
InstallHotkeyFilter(m_controllers_window);
InstallHotkeyFilter(m_settings_window);
InstallHotkeyFilter(m_graphics_window);
2015-11-27 08:33:07 +00:00
}
2016-02-10 04:42:06 +00:00
void MainWindow::ConnectMenuBar()
2015-11-27 08:33:07 +00:00
{
setMenuBar(m_menu_bar);
// File
connect(m_menu_bar, &MenuBar::Open, this, &MainWindow::Open);
connect(m_menu_bar, &MenuBar::Exit, this, &MainWindow::close);
// Emulation
connect(m_menu_bar, &MenuBar::Pause, this, &MainWindow::Pause);
connect(m_menu_bar, &MenuBar::Play, this, &MainWindow::Play);
connect(m_menu_bar, &MenuBar::Stop, this, &MainWindow::RequestStop);
connect(m_menu_bar, &MenuBar::Reset, this, &MainWindow::Reset);
connect(m_menu_bar, &MenuBar::Fullscreen, this, &MainWindow::FullScreen);
connect(m_menu_bar, &MenuBar::FrameAdvance, this, &MainWindow::FrameAdvance);
connect(m_menu_bar, &MenuBar::Screenshot, this, &MainWindow::ScreenShot);
connect(m_menu_bar, &MenuBar::StateLoad, this, &MainWindow::StateLoad);
connect(m_menu_bar, &MenuBar::StateSave, this, &MainWindow::StateSave);
connect(m_menu_bar, &MenuBar::StateLoadSlot, this, &MainWindow::StateLoadSlot);
connect(m_menu_bar, &MenuBar::StateSaveSlot, this, &MainWindow::StateSaveSlot);
connect(m_menu_bar, &MenuBar::StateLoadSlotAt, this, &MainWindow::StateLoadSlotAt);
connect(m_menu_bar, &MenuBar::StateSaveSlotAt, this, &MainWindow::StateSaveSlotAt);
connect(m_menu_bar, &MenuBar::StateLoadUndo, this, &MainWindow::StateLoadUndo);
connect(m_menu_bar, &MenuBar::StateSaveUndo, this, &MainWindow::StateSaveUndo);
connect(m_menu_bar, &MenuBar::StateSaveOldest, this, &MainWindow::StateSaveOldest);
connect(m_menu_bar, &MenuBar::SetStateSlot, this, &MainWindow::SetStateSlot);
// Options
connect(m_menu_bar, &MenuBar::Configure, this, &MainWindow::ShowSettingsWindow);
connect(m_menu_bar, &MenuBar::ConfigureGraphics, this, &MainWindow::ShowGraphicsWindow);
connect(m_menu_bar, &MenuBar::ConfigureAudio, this, &MainWindow::ShowAudioWindow);
connect(m_menu_bar, &MenuBar::ConfigureControllers, this, &MainWindow::ShowControllersWindow);
connect(m_menu_bar, &MenuBar::ConfigureHotkeys, this, &MainWindow::ShowHotkeyDialog);
// Tools
connect(m_menu_bar, &MenuBar::PerformOnlineUpdate, this, &MainWindow::PerformOnlineUpdate);
2017-07-06 09:01:32 +00:00
connect(m_menu_bar, &MenuBar::BootWiiSystemMenu, this, &MainWindow::BootWiiSystemMenu);
2017-07-21 20:48:21 +00:00
connect(m_menu_bar, &MenuBar::StartNetPlay, this, &MainWindow::ShowNetPlaySetupDialog);
// View
connect(m_menu_bar, &MenuBar::ShowList, m_game_list, &GameList::SetListView);
connect(m_menu_bar, &MenuBar::ShowGrid, m_game_list, &GameList::SetGridView);
2017-05-08 17:03:59 +00:00
connect(m_menu_bar, &MenuBar::ColumnVisibilityToggled, m_game_list,
&GameList::OnColumnVisibilityToggled);
connect(m_menu_bar, &MenuBar::GameListPlatformVisibilityToggled, m_game_list,
&GameList::OnGameListVisibilityChanged);
connect(m_menu_bar, &MenuBar::GameListRegionVisibilityToggled, m_game_list,
&GameList::OnGameListVisibilityChanged);
connect(m_menu_bar, &MenuBar::ShowAboutDialog, this, &MainWindow::ShowAboutDialog);
connect(this, &MainWindow::EmulationStarted, m_menu_bar, &MenuBar::EmulationStarted);
connect(this, &MainWindow::EmulationPaused, m_menu_bar, &MenuBar::EmulationPaused);
connect(this, &MainWindow::EmulationStopped, m_menu_bar, &MenuBar::EmulationStopped);
2017-05-09 16:49:10 +00:00
connect(this, &MainWindow::EmulationStarted, this,
[=]() { m_controllers_window->OnEmulationStateChanged(true); });
connect(this, &MainWindow::EmulationStopped, this,
[=]() { m_controllers_window->OnEmulationStateChanged(false); });
2015-11-27 08:33:07 +00:00
}
void MainWindow::ConnectHotkeys()
{
connect(m_hotkey_scheduler, &HotkeyScheduler::ExitHotkey, this, &MainWindow::close);
connect(m_hotkey_scheduler, &HotkeyScheduler::PauseHotkey, this, &MainWindow::Pause);
connect(m_hotkey_scheduler, &HotkeyScheduler::StopHotkey, this, &MainWindow::RequestStop);
connect(m_hotkey_scheduler, &HotkeyScheduler::ScreenShotHotkey, this, &MainWindow::ScreenShot);
connect(m_hotkey_scheduler, &HotkeyScheduler::FullScreenHotkey, this, &MainWindow::FullScreen);
connect(m_hotkey_scheduler, &HotkeyScheduler::StateLoadSlotHotkey, this,
&MainWindow::StateLoadSlot);
connect(m_hotkey_scheduler, &HotkeyScheduler::StateSaveSlotHotkey, this,
&MainWindow::StateSaveSlot);
connect(m_hotkey_scheduler, &HotkeyScheduler::SetStateSlotHotkey, this,
&MainWindow::SetStateSlot);
}
2016-02-10 04:42:06 +00:00
void MainWindow::ConnectToolBar()
2015-11-27 08:33:07 +00:00
{
addToolBar(m_tool_bar);
connect(m_tool_bar, &ToolBar::OpenPressed, this, &MainWindow::Open);
connect(m_tool_bar, &ToolBar::PlayPressed, this, &MainWindow::Play);
connect(m_tool_bar, &ToolBar::PausePressed, this, &MainWindow::Pause);
connect(m_tool_bar, &ToolBar::StopPressed, this, &MainWindow::RequestStop);
connect(m_tool_bar, &ToolBar::FullScreenPressed, this, &MainWindow::FullScreen);
connect(m_tool_bar, &ToolBar::ScreenShotPressed, this, &MainWindow::ScreenShot);
connect(m_tool_bar, &ToolBar::SettingsPressed, this, &MainWindow::ShowSettingsWindow);
2017-05-09 16:49:10 +00:00
connect(m_tool_bar, &ToolBar::ControllersPressed, this, &MainWindow::ShowControllersWindow);
connect(m_tool_bar, &ToolBar::GraphicsPressed, this, &MainWindow::ShowGraphicsWindow);
connect(this, &MainWindow::EmulationStarted, m_tool_bar, &ToolBar::EmulationStarted);
connect(this, &MainWindow::EmulationPaused, m_tool_bar, &ToolBar::EmulationPaused);
connect(this, &MainWindow::EmulationStopped, m_tool_bar, &ToolBar::EmulationStopped);
2017-06-24 15:00:37 +00:00
connect(this, &MainWindow::EmulationStopped, this, &MainWindow::OnStopComplete);
2015-11-27 08:33:07 +00:00
}
2016-02-10 04:42:06 +00:00
void MainWindow::ConnectGameList()
2015-11-27 08:33:07 +00:00
{
connect(m_game_list, &GameList::GameSelected, this, &MainWindow::Play);
2017-07-21 20:48:21 +00:00
connect(m_game_list, &GameList::NetPlayHost, this, &MainWindow::NetPlayHost);
connect(this, &MainWindow::EmulationStarted, m_game_list, &GameList::EmulationStarted);
connect(this, &MainWindow::EmulationStopped, m_game_list, &GameList::EmulationStopped);
2015-11-27 08:33:07 +00:00
}
2016-02-10 04:42:06 +00:00
void MainWindow::ConnectRenderWidget()
2015-11-27 08:33:07 +00:00
{
m_rendering_to_main = false;
m_render_widget->hide();
connect(m_render_widget, &RenderWidget::EscapePressed, this, &MainWindow::RequestStop);
connect(m_render_widget, &RenderWidget::Closed, this, &MainWindow::ForceStop);
2015-11-27 08:33:07 +00:00
}
2016-02-10 04:42:06 +00:00
void MainWindow::ConnectStack()
2015-11-27 08:33:07 +00:00
{
m_stack->addWidget(m_game_list);
setCentralWidget(m_stack);
addDockWidget(Qt::RightDockWidgetArea, m_logger_widget);
2015-11-27 08:33:07 +00:00
}
void MainWindow::Open()
{
QString file = QFileDialog::getOpenFileName(
this, tr("Select a File"), QDir::currentPath(),
2016-12-17 13:48:49 +00:00
tr("All GC/Wii files (*.elf *.dol *.gcm *.iso *.tgc *.wbfs *.ciso *.gcz *.wad);;"
"All Files (*)"));
if (!file.isEmpty())
StartGame(file);
2015-11-27 08:33:07 +00:00
}
void MainWindow::Play()
{
// If we're in a paused game, start it up again.
// Otherwise, play the selected game, if there is one.
// Otherwise, play the default game.
// Otherwise, play the last played game, if there is one.
// Otherwise, prompt for a new game.
if (Core::GetState() == Core::State::Paused)
{
Core::SetState(Core::State::Running);
emit EmulationStarted();
}
else
{
QString selection = m_game_list->GetSelectedGame()->GetFilePath();
if (selection.length() > 0)
{
StartGame(selection);
}
else
{
auto default_path = QString::fromStdString(SConfig::GetInstance().m_strDefaultISO);
if (!default_path.isEmpty() && QFile::exists(default_path))
{
StartGame(default_path);
}
else
{
2017-06-04 20:43:41 +00:00
Open();
}
}
}
2015-11-27 08:33:07 +00:00
}
void MainWindow::Pause()
{
Core::SetState(Core::State::Paused);
emit EmulationPaused();
2015-11-27 08:33:07 +00:00
}
void MainWindow::OnStopComplete()
{
m_stop_requested = false;
m_render_widget->hide();
if (m_exit_requested)
QGuiApplication::instance()->quit();
// If the current emulation prevented the booting of another, do that now
if (!m_pending_boot.isEmpty())
{
StartGame(m_pending_boot);
m_pending_boot.clear();
}
}
bool MainWindow::RequestStop()
2015-11-27 08:33:07 +00:00
{
2017-06-24 15:00:37 +00:00
if (!Core::IsRunning())
{
Core::QueueHostJob([this] { OnStopComplete(); }, true);
2017-06-24 15:00:37 +00:00
return true;
}
2017-06-24 15:00:37 +00:00
if (SConfig::GetInstance().bConfirmStop)
{
2017-06-24 15:00:37 +00:00
const Core::State state = Core::GetState();
2017-07-21 20:48:21 +00:00
// Only pause the game, if NetPlay is not running
bool pause = Settings::Instance().GetNetPlayClient() != nullptr;
2017-06-24 15:00:37 +00:00
if (pause)
Core::SetState(Core::State::Paused);
QMessageBox::StandardButton confirm;
2017-06-24 15:00:37 +00:00
confirm = QMessageBox::question(m_render_widget, tr("Confirm"),
m_stop_requested ?
tr("A shutdown is already in progress. Unsaved data "
"may be lost if you stop the current emulation "
"before it completes. Force stop?") :
tr("Do you want to stop the current emulation?"));
2017-06-24 15:00:37 +00:00
if (pause)
Core::SetState(state);
if (confirm != QMessageBox::Yes)
return false;
}
2015-11-27 08:33:07 +00:00
2017-06-24 15:00:37 +00:00
// TODO: Add Movie shutdown
// TODO: Add Debugger shutdown
if (!m_stop_requested && UICommon::TriggerSTMPowerEvent())
{
2017-06-24 15:00:37 +00:00
m_stop_requested = true;
// Unpause because gracefully shutting down needs the game to actually request a shutdown.
// TODO: Do not unpause in debug mode to allow debugging until the complete shutdown.
2017-06-24 15:00:37 +00:00
if (Core::GetState() == Core::State::Paused)
Core::SetState(Core::State::Running);
return true;
2017-06-24 15:00:37 +00:00
}
ForceStop();
#ifdef Q_OS_WIN
2017-06-24 15:00:37 +00:00
// Allow windows to idle or turn off display again
SetThreadExecutionState(ES_CONTINUOUS);
#endif
2017-06-24 15:00:37 +00:00
return true;
2015-11-27 08:33:07 +00:00
}
void MainWindow::ForceStop()
{
BootManager::Stop();
HideRenderWidget();
2015-11-27 08:33:07 +00:00
}
void MainWindow::Reset()
{
if (Movie::IsRecordingInput())
2016-08-04 16:54:45 +00:00
Movie::SetReset(true);
ProcessorInterface::ResetButton_Tap();
}
void MainWindow::FrameAdvance()
{
Movie::DoFrameStep();
EmulationPaused();
}
2015-11-27 08:33:07 +00:00
void MainWindow::FullScreen()
{
// If the render widget is fullscreen we want to reset it to whatever is in
// settings. If it's set to be fullscreen then it just remakes the window,
// which probably isn't ideal.
bool was_fullscreen = m_render_widget->isFullScreen();
HideRenderWidget();
if (was_fullscreen)
ShowRenderWidget();
else
m_render_widget->showFullScreen();
2015-11-27 08:33:07 +00:00
}
void MainWindow::ScreenShot()
{
Core::SaveScreenShot();
2015-11-27 08:33:07 +00:00
}
void MainWindow::StartGame(const QString& path)
2015-11-27 08:33:07 +00:00
{
// If we're running, only start a new game once we've stopped the last.
if (Core::GetState() != Core::State::Uninitialized)
{
if (!RequestStop())
return;
// As long as the shutdown isn't complete, we can't boot, so let's boot later
m_pending_boot = path;
return;
}
// Boot up, show an error if it fails to load the game.
Boot: Clean up the boot code * Move out boot parameters to a separate struct, which is not part of SConfig/ConfigManager because there is no reason for it to be there. * Move out file name parsing and constructing the appropriate params from paths to a separate function that does that, and only that. * For every different boot type we support, add a proper struct with only the required parameters, with descriptive names and use std::variant to only store what we need. * Clean up the bHLE_BS2 stuff which made no sense sometimes. Now instead of using bHLE_BS2 for two different things, both for storing the user config setting and as a runtime boot parameter, we simply replace the Disc boot params with BootParameters::IPL. * Const correctness so it's clear what can or cannot update the config. * Drop unused parameters and unneeded checks. * Make a few checks a lot more concise. (Looking at you, extension checks for disc images.) * Remove a mildly terrible workaround where we needed to pass an empty string in order to boot the GC IPL without any game inserted. (Not required anymore thanks to std::variant and std::optional.) The motivation for this are multiple: cleaning up and being able to add support for booting an installed NAND title. Without this change, it'd be pretty much impossible to implement that. Also, using std::visit with std::variant makes the compiler do additional type checks: now we're guaranteed that the boot code will handle all boot types and no invalid boot type will be possible.
2017-05-27 13:43:40 +00:00
if (!BootManager::BootCore(BootParameters::GenerateFromFile(path.toStdString())))
{
QMessageBox::critical(this, tr("Error"), tr("Failed to init core"), QMessageBox::Ok);
return;
}
ShowRenderWidget();
emit EmulationStarted();
#ifdef Q_OS_WIN
// Prevents Windows from sleeping, turning off the display, or idling
EXECUTION_STATE shouldScreenSave =
SConfig::GetInstance().bDisableScreenSaver ? ES_DISPLAY_REQUIRED : 0;
SetThreadExecutionState(ES_CONTINUOUS | shouldScreenSave | ES_SYSTEM_REQUIRED);
#endif
2015-11-27 08:33:07 +00:00
}
void MainWindow::ShowRenderWidget()
{
if (SConfig::GetInstance().bRenderToMain)
{
// If we're rendering to main, add it to the stack and update our title when necessary.
m_rendering_to_main = true;
m_stack->setCurrentIndex(m_stack->addWidget(m_render_widget));
connect(Host::GetInstance(), &Host::RequestTitle, this, &MainWindow::setWindowTitle);
}
else
{
// Otherwise, just show it.
m_rendering_to_main = false;
if (SConfig::GetInstance().bFullscreen)
{
m_render_widget->showFullScreen();
}
else
{
m_render_widget->showNormal();
m_render_widget->resize(640, 480);
}
}
2015-11-27 08:33:07 +00:00
}
void MainWindow::HideRenderWidget()
{
if (m_rendering_to_main)
{
// Remove the widget from the stack and reparent it to nullptr, so that it can draw
// itself in a new window if it wants. Disconnect the title updates.
m_stack->removeWidget(m_render_widget);
m_render_widget->setParent(nullptr);
m_rendering_to_main = false;
disconnect(Host::GetInstance(), &Host::RequestTitle, this, &MainWindow::setWindowTitle);
setWindowTitle(QString::fromStdString(scm_rev_str));
}
m_render_widget->hide();
2015-11-27 08:33:07 +00:00
}
2016-02-10 04:42:06 +00:00
2017-05-09 16:49:10 +00:00
void MainWindow::ShowControllersWindow()
{
m_controllers_window->show();
m_controllers_window->raise();
m_controllers_window->activateWindow();
}
void MainWindow::ShowSettingsWindow()
{
m_settings_window->show();
m_settings_window->raise();
m_settings_window->activateWindow();
}
void MainWindow::ShowAudioWindow()
{
m_settings_window->SelectAudioPane();
ShowSettingsWindow();
}
void MainWindow::ShowAboutDialog()
{
AboutDialog* about = new AboutDialog(this);
about->show();
}
void MainWindow::ShowHotkeyDialog()
{
m_hotkey_window->ChangeMappingType(MappingWindow::Type::MAPPING_HOTKEYS);
m_hotkey_window->show();
m_hotkey_window->raise();
m_hotkey_window->activateWindow();
}
void MainWindow::ShowGraphicsWindow()
{
m_graphics_window->show();
m_graphics_window->raise();
m_graphics_window->activateWindow();
}
2017-07-21 20:48:21 +00:00
void MainWindow::ShowNetPlaySetupDialog()
{
m_netplay_setup_dialog->show();
m_netplay_setup_dialog->raise();
m_netplay_setup_dialog->activateWindow();
}
void MainWindow::StateLoad()
{
QString path = QFileDialog::getOpenFileName(this, tr("Select a File"), QDir::currentPath(),
tr("All Save States (*.sav *.s##);; All Files (*)"));
State::LoadAs(path.toStdString());
}
void MainWindow::StateSave()
{
QString path = QFileDialog::getSaveFileName(this, tr("Select a File"), QDir::currentPath(),
tr("All Save States (*.sav *.s##);; All Files (*)"));
State::SaveAs(path.toStdString());
}
void MainWindow::StateLoadSlot()
{
State::Load(m_state_slot);
}
void MainWindow::StateSaveSlot()
{
State::Save(m_state_slot, true);
m_menu_bar->UpdateStateSlotMenu();
}
void MainWindow::StateLoadSlotAt(int slot)
{
State::Load(slot);
}
void MainWindow::StateSaveSlotAt(int slot)
{
State::Save(slot, true);
m_menu_bar->UpdateStateSlotMenu();
}
void MainWindow::StateLoadUndo()
{
State::UndoLoadState();
}
void MainWindow::StateSaveUndo()
{
State::UndoSaveState();
}
void MainWindow::StateSaveOldest()
{
State::SaveFirstSaved();
}
void MainWindow::SetStateSlot(int slot)
{
Settings::Instance().SetStateSlot(slot);
m_state_slot = slot;
}
2017-06-24 15:00:37 +00:00
void MainWindow::PerformOnlineUpdate(const std::string& region)
{
WiiUpdate::PerformOnlineUpdate(region, this);
// Since the update may have installed a newer system menu, refresh the tools menu.
m_menu_bar->UpdateToolsMenu(false);
}
2017-07-06 09:01:32 +00:00
void MainWindow::BootWiiSystemMenu()
{
StartGame(QString::fromStdString(
Common::GetTitleContentPath(Titles::SYSTEM_MENU, Common::FROM_CONFIGURED_ROOT)));
}
2017-07-21 20:48:21 +00:00
void MainWindow::NetPlayInit()
{
m_netplay_setup_dialog = new NetPlaySetupDialog(this);
m_netplay_dialog = new NetPlayDialog(this);
connect(m_netplay_dialog, &NetPlayDialog::Boot, this, &MainWindow::StartGame);
connect(m_netplay_dialog, &NetPlayDialog::Stop, this, &MainWindow::RequestStop);
connect(m_netplay_dialog, &NetPlayDialog::rejected, this, &MainWindow::NetPlayQuit);
connect(this, &MainWindow::EmulationStopped, m_netplay_dialog, &NetPlayDialog::EmulationStopped);
connect(m_netplay_setup_dialog, &NetPlaySetupDialog::Join, this, &MainWindow::NetPlayJoin);
connect(m_netplay_setup_dialog, &NetPlaySetupDialog::Host, this, &MainWindow::NetPlayHost);
}
bool MainWindow::NetPlayJoin()
{
if (Core::IsRunning())
{
QMessageBox::critical(
nullptr, QObject::tr("Error"),
QObject::tr("Can't start a NetPlay Session while a game is still running!"));
return false;
}
if (m_netplay_dialog->isVisible())
{
QMessageBox::critical(nullptr, QObject::tr("Error"),
QObject::tr("A NetPlay Session is already in progress!"));
return false;
}
// Settings
std::string host_ip;
u16 host_port;
2017-07-21 20:48:21 +00:00
if (Settings::Instance().GetNetPlayServer() != nullptr)
{
host_ip = "127.0.0.1";
host_port = Settings::Instance().GetNetPlayServer()->GetPort();
}
else
{
host_ip = Config::Get(Config::NETPLAY_HOST_CODE);
host_port = Config::Get(Config::NETPLAY_HOST_PORT);
}
const std::string traversal_choice = Config::Get(Config::NETPLAY_TRAVERSAL_CHOICE);
const bool is_traversal = traversal_choice == "traversal";
2017-07-21 20:48:21 +00:00
const std::string traversal_host = Config::Get(Config::NETPLAY_TRAVERSAL_SERVER);
const u16 traversal_port = Config::Get(Config::NETPLAY_TRAVERSAL_PORT);
const std::string nickname = Config::Get(Config::NETPLAY_NICKNAME);
2017-07-21 20:48:21 +00:00
// Create Client
Settings::Instance().ResetNetPlayClient(new NetPlayClient(
host_ip, host_port, m_netplay_dialog, nickname,
NetTraversalConfig{Settings::Instance().GetNetPlayServer() != nullptr ? false : is_traversal,
traversal_host, traversal_port}));
2017-07-21 20:48:21 +00:00
if (!Settings::Instance().GetNetPlayClient()->IsConnected())
{
QMessageBox::critical(nullptr, QObject::tr("Error"),
QObject::tr("Failed to connect to server"));
return false;
}
m_netplay_setup_dialog->close();
m_netplay_dialog->show(nickname, is_traversal);
return true;
}
bool MainWindow::NetPlayHost(const QString& game_id)
{
if (Core::IsRunning())
{
QMessageBox::critical(
nullptr, QObject::tr("Error"),
QObject::tr("Can't start a NetPlay Session while a game is still running!"));
return false;
}
if (m_netplay_dialog->isVisible())
{
QMessageBox::critical(nullptr, QObject::tr("Error"),
QObject::tr("A NetPlay Session is already in progress!"));
return false;
}
// Settings
u16 host_port = Config::Get(Config::NETPLAY_HOST_PORT);
const std::string traversal_choice = Config::Get(Config::NETPLAY_TRAVERSAL_CHOICE);
const bool is_traversal = traversal_choice == "traversal";
const bool use_upnp = Config::Get(Config::NETPLAY_USE_UPNP);
const std::string traversal_host = Config::Get(Config::NETPLAY_TRAVERSAL_SERVER);
const u16 traversal_port = Config::Get(Config::NETPLAY_TRAVERSAL_PORT);
const std::string nickname = Config::Get(Config::NETPLAY_NICKNAME);
2017-07-21 20:48:21 +00:00
if (is_traversal)
host_port = Config::Get(Config::NETPLAY_LISTEN_PORT);
// Create Server
Settings::Instance().ResetNetPlayServer(new NetPlayServer(
host_port, use_upnp, NetTraversalConfig{is_traversal, traversal_host, traversal_port}));
2017-07-21 20:48:21 +00:00
if (!Settings::Instance().GetNetPlayServer()->is_connected)
{
QMessageBox::critical(
nullptr, QObject::tr("Failed to open server"),
QObject::tr(
"Failed to listen on port %1. Is another instance of the NetPlay server running?")
.arg(host_port));
return false;
}
Settings::Instance().GetNetPlayServer()->ChangeGame(game_id.toStdString());
// Join our local server
return NetPlayJoin();
}
void MainWindow::NetPlayQuit()
{
Settings::Instance().ResetNetPlayClient();
Settings::Instance().ResetNetPlayServer();
}
2017-06-24 15:00:37 +00:00
bool MainWindow::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::Close)
2017-06-24 15:00:37 +00:00
{
if (RequestStop() && object == this)
m_exit_requested = true;
2017-06-24 15:00:37 +00:00
static_cast<QCloseEvent*>(event)->ignore();
return true;
}
return false;
}
2017-06-26 21:22:40 +00:00
void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1)
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent* event)
{
const auto& urls = event->mimeData()->urls();
if (urls.empty())
return;
const auto& url = urls[0];
QFileInfo file_info(url.toLocalFile());
auto path = file_info.filePath();
if (!file_info.exists() || !file_info.isReadable())
{
QMessageBox::critical(this, tr("Error"), tr("Failed to open '%1'").arg(path));
return;
}
if (file_info.isFile())
{
StartGame(path);
}
else
{
auto& settings = Settings::Instance();
if (settings.GetPaths().size() != 0)
{
if (QMessageBox::question(
this, tr("Confirm"),
tr("Do you want to add \"%1\" to the list of Game Paths?").arg(path)) !=
QMessageBox::Yes)
return;
}
settings.AddPath(path);
}
}
QSize MainWindow::sizeHint() const
{
return QSize(800, 600);
}