bootmanager.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <glad/glad.h>
  5. #include <QApplication>
  6. #include <QCameraImageCapture>
  7. #include <QCameraInfo>
  8. #include <QHBoxLayout>
  9. #include <QMessageBox>
  10. #include <QPainter>
  11. #include <QScreen>
  12. #include <QString>
  13. #include <QStringList>
  14. #include <QWindow>
  15. #ifdef HAS_OPENGL
  16. #include <QOffscreenSurface>
  17. #include <QOpenGLContext>
  18. #endif
  19. #if !defined(WIN32)
  20. #include <qpa/qplatformnativeinterface.h>
  21. #endif
  22. #include <fmt/format.h>
  23. #include "common/assert.h"
  24. #include "common/microprofile.h"
  25. #include "common/scm_rev.h"
  26. #include "common/settings.h"
  27. #include "core/core.h"
  28. #include "core/cpu_manager.h"
  29. #include "core/frontend/framebuffer_layout.h"
  30. #include "input_common/drivers/camera.h"
  31. #include "input_common/drivers/keyboard.h"
  32. #include "input_common/drivers/mouse.h"
  33. #include "input_common/drivers/tas_input.h"
  34. #include "input_common/drivers/touch_screen.h"
  35. #include "input_common/main.h"
  36. #include "video_core/renderer_base.h"
  37. #include "yuzu/bootmanager.h"
  38. #include "yuzu/main.h"
  39. EmuThread::EmuThread(Core::System& system_) : system{system_} {}
  40. EmuThread::~EmuThread() = default;
  41. void EmuThread::run() {
  42. std::string name = "yuzu:EmuControlThread";
  43. MicroProfileOnThreadCreate(name.c_str());
  44. Common::SetCurrentThreadName(name.c_str());
  45. auto& gpu = system.GPU();
  46. auto stop_token = stop_source.get_token();
  47. bool debugger_should_start = system.DebuggerEnabled();
  48. system.RegisterHostThread();
  49. // Main process has been loaded. Make the context current to this thread and begin GPU and CPU
  50. // execution.
  51. gpu.Start();
  52. gpu.ObtainContext();
  53. emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0);
  54. if (Settings::values.use_disk_shader_cache.GetValue()) {
  55. system.Renderer().ReadRasterizer()->LoadDiskResources(
  56. system.GetCurrentProcessProgramID(), stop_token,
  57. [this](VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total) {
  58. emit LoadProgress(stage, value, total);
  59. });
  60. }
  61. emit LoadProgress(VideoCore::LoadCallbackStage::Complete, 0, 0);
  62. gpu.ReleaseContext();
  63. system.GetCpuManager().OnGpuReady();
  64. // Holds whether the cpu was running during the last iteration,
  65. // so that the DebugModeLeft signal can be emitted before the
  66. // next execution step
  67. bool was_active = false;
  68. while (!stop_token.stop_requested()) {
  69. if (running) {
  70. if (was_active) {
  71. emit DebugModeLeft();
  72. }
  73. running_guard = true;
  74. Core::SystemResultStatus result = system.Run();
  75. if (result != Core::SystemResultStatus::Success) {
  76. running_guard = false;
  77. this->SetRunning(false);
  78. emit ErrorThrown(result, system.GetStatusDetails());
  79. }
  80. if (debugger_should_start) {
  81. system.InitializeDebugger();
  82. debugger_should_start = false;
  83. }
  84. running_wait.Wait();
  85. result = system.Pause();
  86. if (result != Core::SystemResultStatus::Success) {
  87. running_guard = false;
  88. this->SetRunning(false);
  89. emit ErrorThrown(result, system.GetStatusDetails());
  90. }
  91. running_guard = false;
  92. if (!stop_token.stop_requested()) {
  93. was_active = true;
  94. emit DebugModeEntered();
  95. }
  96. } else {
  97. std::unique_lock lock{running_mutex};
  98. running_cv.wait(lock, stop_token, [this] { return IsRunning(); });
  99. }
  100. }
  101. // Shutdown the core emulation
  102. system.Shutdown();
  103. #if MICROPROFILE_ENABLED
  104. MicroProfileOnThreadExit();
  105. #endif
  106. }
  107. #ifdef HAS_OPENGL
  108. class OpenGLSharedContext : public Core::Frontend::GraphicsContext {
  109. public:
  110. /// Create the original context that should be shared from
  111. explicit OpenGLSharedContext(QSurface* surface_) : surface{surface_} {
  112. QSurfaceFormat format;
  113. format.setVersion(4, 6);
  114. format.setProfile(QSurfaceFormat::CompatibilityProfile);
  115. format.setOption(QSurfaceFormat::FormatOption::DeprecatedFunctions);
  116. if (Settings::values.renderer_debug) {
  117. format.setOption(QSurfaceFormat::FormatOption::DebugContext);
  118. }
  119. // TODO: expose a setting for buffer value (ie default/single/double/triple)
  120. format.setSwapBehavior(QSurfaceFormat::DefaultSwapBehavior);
  121. format.setSwapInterval(0);
  122. context = std::make_unique<QOpenGLContext>();
  123. context->setFormat(format);
  124. if (!context->create()) {
  125. LOG_ERROR(Frontend, "Unable to create main openGL context");
  126. }
  127. }
  128. /// Create the shared contexts for rendering and presentation
  129. explicit OpenGLSharedContext(QOpenGLContext* share_context, QSurface* main_surface = nullptr) {
  130. // disable vsync for any shared contexts
  131. auto format = share_context->format();
  132. format.setSwapInterval(main_surface ? Settings::values.use_vsync.GetValue() : 0);
  133. context = std::make_unique<QOpenGLContext>();
  134. context->setShareContext(share_context);
  135. context->setFormat(format);
  136. if (!context->create()) {
  137. LOG_ERROR(Frontend, "Unable to create shared openGL context");
  138. }
  139. if (!main_surface) {
  140. offscreen_surface = std::make_unique<QOffscreenSurface>(nullptr);
  141. offscreen_surface->setFormat(format);
  142. offscreen_surface->create();
  143. surface = offscreen_surface.get();
  144. } else {
  145. surface = main_surface;
  146. }
  147. }
  148. ~OpenGLSharedContext() {
  149. DoneCurrent();
  150. }
  151. void SwapBuffers() override {
  152. context->swapBuffers(surface);
  153. }
  154. void MakeCurrent() override {
  155. // We can't track the current state of the underlying context in this wrapper class because
  156. // Qt may make the underlying context not current for one reason or another. In particular,
  157. // the WebBrowser uses GL, so it seems to conflict if we aren't careful.
  158. // Instead of always just making the context current (which does not have any caching to
  159. // check if the underlying context is already current) we can check for the current context
  160. // in the thread local data by calling `currentContext()` and checking if its ours.
  161. if (QOpenGLContext::currentContext() != context.get()) {
  162. context->makeCurrent(surface);
  163. }
  164. }
  165. void DoneCurrent() override {
  166. context->doneCurrent();
  167. }
  168. QOpenGLContext* GetShareContext() {
  169. return context.get();
  170. }
  171. const QOpenGLContext* GetShareContext() const {
  172. return context.get();
  173. }
  174. private:
  175. // Avoid using Qt parent system here since we might move the QObjects to new threads
  176. // As a note, this means we should avoid using slots/signals with the objects too
  177. std::unique_ptr<QOpenGLContext> context;
  178. std::unique_ptr<QOffscreenSurface> offscreen_surface{};
  179. QSurface* surface;
  180. };
  181. #endif
  182. class DummyContext : public Core::Frontend::GraphicsContext {};
  183. class RenderWidget : public QWidget {
  184. public:
  185. explicit RenderWidget(GRenderWindow* parent) : QWidget(parent), render_window(parent) {
  186. setAttribute(Qt::WA_NativeWindow);
  187. setAttribute(Qt::WA_PaintOnScreen);
  188. }
  189. virtual ~RenderWidget() = default;
  190. QPaintEngine* paintEngine() const override {
  191. return nullptr;
  192. }
  193. private:
  194. GRenderWindow* render_window;
  195. };
  196. class OpenGLRenderWidget : public RenderWidget {
  197. public:
  198. explicit OpenGLRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {
  199. windowHandle()->setSurfaceType(QWindow::OpenGLSurface);
  200. }
  201. void SetContext(std::unique_ptr<Core::Frontend::GraphicsContext>&& context_) {
  202. context = std::move(context_);
  203. }
  204. private:
  205. std::unique_ptr<Core::Frontend::GraphicsContext> context;
  206. };
  207. class VulkanRenderWidget : public RenderWidget {
  208. public:
  209. explicit VulkanRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {
  210. windowHandle()->setSurfaceType(QWindow::VulkanSurface);
  211. }
  212. };
  213. static Core::Frontend::WindowSystemType GetWindowSystemType() {
  214. // Determine WSI type based on Qt platform.
  215. QString platform_name = QGuiApplication::platformName();
  216. if (platform_name == QStringLiteral("windows"))
  217. return Core::Frontend::WindowSystemType::Windows;
  218. else if (platform_name == QStringLiteral("xcb"))
  219. return Core::Frontend::WindowSystemType::X11;
  220. else if (platform_name == QStringLiteral("wayland"))
  221. return Core::Frontend::WindowSystemType::Wayland;
  222. LOG_CRITICAL(Frontend, "Unknown Qt platform!");
  223. return Core::Frontend::WindowSystemType::Windows;
  224. }
  225. static Core::Frontend::EmuWindow::WindowSystemInfo GetWindowSystemInfo(QWindow* window) {
  226. Core::Frontend::EmuWindow::WindowSystemInfo wsi;
  227. wsi.type = GetWindowSystemType();
  228. // Our Win32 Qt external doesn't have the private API.
  229. #if defined(WIN32) || defined(__APPLE__)
  230. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  231. #else
  232. QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
  233. wsi.display_connection = pni->nativeResourceForWindow("display", window);
  234. if (wsi.type == Core::Frontend::WindowSystemType::Wayland)
  235. wsi.render_surface = window ? pni->nativeResourceForWindow("surface", window) : nullptr;
  236. else
  237. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  238. #endif
  239. wsi.render_surface_scale = window ? static_cast<float>(window->devicePixelRatio()) : 1.0f;
  240. return wsi;
  241. }
  242. GRenderWindow::GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_,
  243. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem_,
  244. Core::System& system_)
  245. : QWidget(parent),
  246. emu_thread(emu_thread_), input_subsystem{std::move(input_subsystem_)}, system{system_} {
  247. setWindowTitle(QStringLiteral("yuzu %1 | %2-%3")
  248. .arg(QString::fromUtf8(Common::g_build_name),
  249. QString::fromUtf8(Common::g_scm_branch),
  250. QString::fromUtf8(Common::g_scm_desc)));
  251. setAttribute(Qt::WA_AcceptTouchEvents);
  252. auto* layout = new QHBoxLayout(this);
  253. layout->setContentsMargins(0, 0, 0, 0);
  254. setLayout(layout);
  255. input_subsystem->Initialize();
  256. this->setMouseTracking(true);
  257. connect(this, &GRenderWindow::FirstFrameDisplayed, parent, &GMainWindow::OnLoadComplete);
  258. connect(this, &GRenderWindow::ExecuteProgramSignal, parent, &GMainWindow::OnExecuteProgram,
  259. Qt::QueuedConnection);
  260. connect(this, &GRenderWindow::ExitSignal, parent, &GMainWindow::OnExit, Qt::QueuedConnection);
  261. connect(this, &GRenderWindow::TasPlaybackStateChanged, parent, &GMainWindow::OnTasStateChanged);
  262. }
  263. void GRenderWindow::ExecuteProgram(std::size_t program_index) {
  264. emit ExecuteProgramSignal(program_index);
  265. }
  266. void GRenderWindow::Exit() {
  267. emit ExitSignal();
  268. }
  269. GRenderWindow::~GRenderWindow() {
  270. input_subsystem->Shutdown();
  271. }
  272. void GRenderWindow::OnFrameDisplayed() {
  273. input_subsystem->GetTas()->UpdateThread();
  274. const InputCommon::TasInput::TasState new_tas_state =
  275. std::get<0>(input_subsystem->GetTas()->GetStatus());
  276. if (!first_frame) {
  277. last_tas_state = new_tas_state;
  278. first_frame = true;
  279. emit FirstFrameDisplayed();
  280. }
  281. if (new_tas_state != last_tas_state) {
  282. last_tas_state = new_tas_state;
  283. emit TasPlaybackStateChanged();
  284. }
  285. }
  286. bool GRenderWindow::IsShown() const {
  287. return !isMinimized();
  288. }
  289. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  290. //
  291. // Older versions get the window size (density independent pixels),
  292. // and hence, do not support DPI scaling ("retina" displays).
  293. // The result will be a viewport that is smaller than the extent of the window.
  294. void GRenderWindow::OnFramebufferSizeChanged() {
  295. // Screen changes potentially incur a change in screen DPI, hence we should update the
  296. // framebuffer size
  297. const qreal pixel_ratio = windowPixelRatio();
  298. const u32 width = this->width() * pixel_ratio;
  299. const u32 height = this->height() * pixel_ratio;
  300. UpdateCurrentFramebufferLayout(width, height);
  301. }
  302. void GRenderWindow::BackupGeometry() {
  303. geometry = QWidget::saveGeometry();
  304. }
  305. void GRenderWindow::RestoreGeometry() {
  306. // We don't want to back up the geometry here (obviously)
  307. QWidget::restoreGeometry(geometry);
  308. }
  309. void GRenderWindow::restoreGeometry(const QByteArray& geometry_) {
  310. // Make sure users of this class don't need to deal with backing up the geometry themselves
  311. QWidget::restoreGeometry(geometry_);
  312. BackupGeometry();
  313. }
  314. QByteArray GRenderWindow::saveGeometry() {
  315. // If we are a top-level widget, store the current geometry
  316. // otherwise, store the last backup
  317. if (parent() == nullptr) {
  318. return QWidget::saveGeometry();
  319. }
  320. return geometry;
  321. }
  322. qreal GRenderWindow::windowPixelRatio() const {
  323. return devicePixelRatioF();
  324. }
  325. std::pair<u32, u32> GRenderWindow::ScaleTouch(const QPointF& pos) const {
  326. const qreal pixel_ratio = windowPixelRatio();
  327. return {static_cast<u32>(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})),
  328. static_cast<u32>(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))};
  329. }
  330. void GRenderWindow::closeEvent(QCloseEvent* event) {
  331. emit Closed();
  332. QWidget::closeEvent(event);
  333. }
  334. int GRenderWindow::QtKeyToSwitchKey(Qt::Key qt_key) {
  335. switch (qt_key) {
  336. case Qt::Key_A:
  337. return Settings::NativeKeyboard::A;
  338. case Qt::Key_B:
  339. return Settings::NativeKeyboard::B;
  340. case Qt::Key_C:
  341. return Settings::NativeKeyboard::C;
  342. case Qt::Key_D:
  343. return Settings::NativeKeyboard::D;
  344. case Qt::Key_E:
  345. return Settings::NativeKeyboard::E;
  346. case Qt::Key_F:
  347. return Settings::NativeKeyboard::F;
  348. case Qt::Key_G:
  349. return Settings::NativeKeyboard::G;
  350. case Qt::Key_H:
  351. return Settings::NativeKeyboard::H;
  352. case Qt::Key_I:
  353. return Settings::NativeKeyboard::I;
  354. case Qt::Key_J:
  355. return Settings::NativeKeyboard::J;
  356. case Qt::Key_K:
  357. return Settings::NativeKeyboard::K;
  358. case Qt::Key_L:
  359. return Settings::NativeKeyboard::L;
  360. case Qt::Key_M:
  361. return Settings::NativeKeyboard::M;
  362. case Qt::Key_N:
  363. return Settings::NativeKeyboard::N;
  364. case Qt::Key_O:
  365. return Settings::NativeKeyboard::O;
  366. case Qt::Key_P:
  367. return Settings::NativeKeyboard::P;
  368. case Qt::Key_Q:
  369. return Settings::NativeKeyboard::Q;
  370. case Qt::Key_R:
  371. return Settings::NativeKeyboard::R;
  372. case Qt::Key_S:
  373. return Settings::NativeKeyboard::S;
  374. case Qt::Key_T:
  375. return Settings::NativeKeyboard::T;
  376. case Qt::Key_U:
  377. return Settings::NativeKeyboard::U;
  378. case Qt::Key_V:
  379. return Settings::NativeKeyboard::V;
  380. case Qt::Key_W:
  381. return Settings::NativeKeyboard::W;
  382. case Qt::Key_X:
  383. return Settings::NativeKeyboard::X;
  384. case Qt::Key_Y:
  385. return Settings::NativeKeyboard::Y;
  386. case Qt::Key_Z:
  387. return Settings::NativeKeyboard::Z;
  388. case Qt::Key_1:
  389. return Settings::NativeKeyboard::N1;
  390. case Qt::Key_2:
  391. return Settings::NativeKeyboard::N2;
  392. case Qt::Key_3:
  393. return Settings::NativeKeyboard::N3;
  394. case Qt::Key_4:
  395. return Settings::NativeKeyboard::N4;
  396. case Qt::Key_5:
  397. return Settings::NativeKeyboard::N5;
  398. case Qt::Key_6:
  399. return Settings::NativeKeyboard::N6;
  400. case Qt::Key_7:
  401. return Settings::NativeKeyboard::N7;
  402. case Qt::Key_8:
  403. return Settings::NativeKeyboard::N8;
  404. case Qt::Key_9:
  405. return Settings::NativeKeyboard::N9;
  406. case Qt::Key_0:
  407. return Settings::NativeKeyboard::N0;
  408. case Qt::Key_Return:
  409. return Settings::NativeKeyboard::Return;
  410. case Qt::Key_Escape:
  411. return Settings::NativeKeyboard::Escape;
  412. case Qt::Key_Backspace:
  413. return Settings::NativeKeyboard::Backspace;
  414. case Qt::Key_Tab:
  415. return Settings::NativeKeyboard::Tab;
  416. case Qt::Key_Space:
  417. return Settings::NativeKeyboard::Space;
  418. case Qt::Key_Minus:
  419. return Settings::NativeKeyboard::Minus;
  420. case Qt::Key_Plus:
  421. case Qt::Key_questiondown:
  422. return Settings::NativeKeyboard::Plus;
  423. case Qt::Key_BracketLeft:
  424. case Qt::Key_BraceLeft:
  425. return Settings::NativeKeyboard::OpenBracket;
  426. case Qt::Key_BracketRight:
  427. case Qt::Key_BraceRight:
  428. return Settings::NativeKeyboard::CloseBracket;
  429. case Qt::Key_Bar:
  430. return Settings::NativeKeyboard::Pipe;
  431. case Qt::Key_Dead_Tilde:
  432. return Settings::NativeKeyboard::Tilde;
  433. case Qt::Key_Ntilde:
  434. case Qt::Key_Semicolon:
  435. return Settings::NativeKeyboard::Semicolon;
  436. case Qt::Key_Apostrophe:
  437. return Settings::NativeKeyboard::Quote;
  438. case Qt::Key_Dead_Grave:
  439. return Settings::NativeKeyboard::Backquote;
  440. case Qt::Key_Comma:
  441. return Settings::NativeKeyboard::Comma;
  442. case Qt::Key_Period:
  443. return Settings::NativeKeyboard::Period;
  444. case Qt::Key_Slash:
  445. return Settings::NativeKeyboard::Slash;
  446. case Qt::Key_CapsLock:
  447. return Settings::NativeKeyboard::CapsLock;
  448. case Qt::Key_F1:
  449. return Settings::NativeKeyboard::F1;
  450. case Qt::Key_F2:
  451. return Settings::NativeKeyboard::F2;
  452. case Qt::Key_F3:
  453. return Settings::NativeKeyboard::F3;
  454. case Qt::Key_F4:
  455. return Settings::NativeKeyboard::F4;
  456. case Qt::Key_F5:
  457. return Settings::NativeKeyboard::F5;
  458. case Qt::Key_F6:
  459. return Settings::NativeKeyboard::F6;
  460. case Qt::Key_F7:
  461. return Settings::NativeKeyboard::F7;
  462. case Qt::Key_F8:
  463. return Settings::NativeKeyboard::F8;
  464. case Qt::Key_F9:
  465. return Settings::NativeKeyboard::F9;
  466. case Qt::Key_F10:
  467. return Settings::NativeKeyboard::F10;
  468. case Qt::Key_F11:
  469. return Settings::NativeKeyboard::F11;
  470. case Qt::Key_F12:
  471. return Settings::NativeKeyboard::F12;
  472. case Qt::Key_Print:
  473. return Settings::NativeKeyboard::PrintScreen;
  474. case Qt::Key_ScrollLock:
  475. return Settings::NativeKeyboard::ScrollLock;
  476. case Qt::Key_Pause:
  477. return Settings::NativeKeyboard::Pause;
  478. case Qt::Key_Insert:
  479. return Settings::NativeKeyboard::Insert;
  480. case Qt::Key_Home:
  481. return Settings::NativeKeyboard::Home;
  482. case Qt::Key_PageUp:
  483. return Settings::NativeKeyboard::PageUp;
  484. case Qt::Key_Delete:
  485. return Settings::NativeKeyboard::Delete;
  486. case Qt::Key_End:
  487. return Settings::NativeKeyboard::End;
  488. case Qt::Key_PageDown:
  489. return Settings::NativeKeyboard::PageDown;
  490. case Qt::Key_Right:
  491. return Settings::NativeKeyboard::Right;
  492. case Qt::Key_Left:
  493. return Settings::NativeKeyboard::Left;
  494. case Qt::Key_Down:
  495. return Settings::NativeKeyboard::Down;
  496. case Qt::Key_Up:
  497. return Settings::NativeKeyboard::Up;
  498. case Qt::Key_NumLock:
  499. return Settings::NativeKeyboard::NumLock;
  500. // Numpad keys are missing here
  501. case Qt::Key_F13:
  502. return Settings::NativeKeyboard::F13;
  503. case Qt::Key_F14:
  504. return Settings::NativeKeyboard::F14;
  505. case Qt::Key_F15:
  506. return Settings::NativeKeyboard::F15;
  507. case Qt::Key_F16:
  508. return Settings::NativeKeyboard::F16;
  509. case Qt::Key_F17:
  510. return Settings::NativeKeyboard::F17;
  511. case Qt::Key_F18:
  512. return Settings::NativeKeyboard::F18;
  513. case Qt::Key_F19:
  514. return Settings::NativeKeyboard::F19;
  515. case Qt::Key_F20:
  516. return Settings::NativeKeyboard::F20;
  517. case Qt::Key_F21:
  518. return Settings::NativeKeyboard::F21;
  519. case Qt::Key_F22:
  520. return Settings::NativeKeyboard::F22;
  521. case Qt::Key_F23:
  522. return Settings::NativeKeyboard::F23;
  523. case Qt::Key_F24:
  524. return Settings::NativeKeyboard::F24;
  525. // case Qt:::
  526. // return Settings::NativeKeyboard::KPComma;
  527. // case Qt:::
  528. // return Settings::NativeKeyboard::Ro;
  529. case Qt::Key_Hiragana_Katakana:
  530. return Settings::NativeKeyboard::KatakanaHiragana;
  531. case Qt::Key_yen:
  532. return Settings::NativeKeyboard::Yen;
  533. case Qt::Key_Henkan:
  534. return Settings::NativeKeyboard::Henkan;
  535. case Qt::Key_Muhenkan:
  536. return Settings::NativeKeyboard::Muhenkan;
  537. // case Qt:::
  538. // return Settings::NativeKeyboard::NumPadCommaPc98;
  539. case Qt::Key_Hangul:
  540. return Settings::NativeKeyboard::HangulEnglish;
  541. case Qt::Key_Hangul_Hanja:
  542. return Settings::NativeKeyboard::Hanja;
  543. case Qt::Key_Katakana:
  544. return Settings::NativeKeyboard::KatakanaKey;
  545. case Qt::Key_Hiragana:
  546. return Settings::NativeKeyboard::HiraganaKey;
  547. case Qt::Key_Zenkaku_Hankaku:
  548. return Settings::NativeKeyboard::ZenkakuHankaku;
  549. // Modifier keys are handled by the modifier property
  550. default:
  551. return Settings::NativeKeyboard::None;
  552. }
  553. }
  554. int GRenderWindow::QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers) {
  555. int modifier = 0;
  556. if ((qt_modifiers & Qt::KeyboardModifier::ShiftModifier) != 0) {
  557. modifier |= 1 << Settings::NativeKeyboard::LeftShift;
  558. }
  559. if ((qt_modifiers & Qt::KeyboardModifier::ControlModifier) != 0) {
  560. modifier |= 1 << Settings::NativeKeyboard::LeftControl;
  561. }
  562. if ((qt_modifiers & Qt::KeyboardModifier::AltModifier) != 0) {
  563. modifier |= 1 << Settings::NativeKeyboard::LeftAlt;
  564. }
  565. if ((qt_modifiers & Qt::KeyboardModifier::MetaModifier) != 0) {
  566. modifier |= 1 << Settings::NativeKeyboard::LeftMeta;
  567. }
  568. // TODO: These keys can't be obtained with Qt::KeyboardModifier
  569. // if ((qt_modifiers & 0x10) != 0) {
  570. // modifier |= 1 << Settings::NativeKeyboard::RightShift;
  571. // }
  572. // if ((qt_modifiers & 0x20) != 0) {
  573. // modifier |= 1 << Settings::NativeKeyboard::RightControl;
  574. // }
  575. // if ((qt_modifiers & 0x40) != 0) {
  576. // modifier |= 1 << Settings::NativeKeyboard::RightAlt;
  577. // }
  578. // if ((qt_modifiers & 0x80) != 0) {
  579. // modifier |= 1 << Settings::NativeKeyboard::RightMeta;
  580. // }
  581. // if ((qt_modifiers & 0x100) != 0) {
  582. // modifier |= 1 << Settings::NativeKeyboard::CapsLock;
  583. // }
  584. // if ((qt_modifiers & 0x200) != 0) {
  585. // modifier |= 1 << Settings::NativeKeyboard::NumLock;
  586. // }
  587. // if ((qt_modifiers & ???) != 0) {
  588. // modifier |= 1 << Settings::NativeKeyboard::ScrollLock;
  589. // }
  590. // if ((qt_modifiers & ???) != 0) {
  591. // modifier |= 1 << Settings::NativeKeyboard::Katakana;
  592. // }
  593. // if ((qt_modifiers & ???) != 0) {
  594. // modifier |= 1 << Settings::NativeKeyboard::Hiragana;
  595. // }
  596. return modifier;
  597. }
  598. void GRenderWindow::keyPressEvent(QKeyEvent* event) {
  599. /**
  600. * This feature can be enhanced with the following functions, but they do not provide
  601. * cross-platform behavior.
  602. *
  603. * event->nativeVirtualKey() can distinguish between keys on the numpad.
  604. * event->nativeModifiers() can distinguish between left and right keys and numlock,
  605. * capslock, scroll lock.
  606. */
  607. if (!event->isAutoRepeat()) {
  608. const auto modifier = QtModifierToSwitchModifier(event->modifiers());
  609. const auto key = QtKeyToSwitchKey(Qt::Key(event->key()));
  610. input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier);
  611. input_subsystem->GetKeyboard()->PressKeyboardKey(key);
  612. // This is used for gamepads that can have any key mapped
  613. input_subsystem->GetKeyboard()->PressKey(event->key());
  614. }
  615. }
  616. void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
  617. /**
  618. * This feature can be enhanced with the following functions, but they do not provide
  619. * cross-platform behavior.
  620. *
  621. * event->nativeVirtualKey() can distinguish between keys on the numpad.
  622. * event->nativeModifiers() can distinguish between left and right buttons and numlock,
  623. * capslock, scroll lock.
  624. */
  625. if (!event->isAutoRepeat()) {
  626. const auto modifier = QtModifierToSwitchModifier(event->modifiers());
  627. const auto key = QtKeyToSwitchKey(Qt::Key(event->key()));
  628. input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier);
  629. input_subsystem->GetKeyboard()->ReleaseKeyboardKey(key);
  630. // This is used for gamepads that can have any key mapped
  631. input_subsystem->GetKeyboard()->ReleaseKey(event->key());
  632. }
  633. }
  634. InputCommon::MouseButton GRenderWindow::QtButtonToMouseButton(Qt::MouseButton button) {
  635. switch (button) {
  636. case Qt::LeftButton:
  637. return InputCommon::MouseButton::Left;
  638. case Qt::RightButton:
  639. return InputCommon::MouseButton::Right;
  640. case Qt::MiddleButton:
  641. return InputCommon::MouseButton::Wheel;
  642. case Qt::BackButton:
  643. return InputCommon::MouseButton::Backward;
  644. case Qt::ForwardButton:
  645. return InputCommon::MouseButton::Forward;
  646. case Qt::TaskButton:
  647. return InputCommon::MouseButton::Task;
  648. default:
  649. return InputCommon::MouseButton::Extra;
  650. }
  651. }
  652. void GRenderWindow::mousePressEvent(QMouseEvent* event) {
  653. // Touch input is handled in TouchBeginEvent
  654. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  655. return;
  656. }
  657. // Qt sometimes returns the parent coordinates. To avoid this we read the global mouse
  658. // coordinates and map them to the current render area
  659. const auto pos = mapFromGlobal(QCursor::pos());
  660. const auto [x, y] = ScaleTouch(pos);
  661. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  662. const auto button = QtButtonToMouseButton(event->button());
  663. input_subsystem->GetMouse()->PressButton(x, y, touch_x, touch_y, button);
  664. emit MouseActivity();
  665. }
  666. void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
  667. // Touch input is handled in TouchUpdateEvent
  668. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  669. return;
  670. }
  671. // Qt sometimes returns the parent coordinates. To avoid this we read the global mouse
  672. // coordinates and map them to the current render area
  673. const auto pos = mapFromGlobal(QCursor::pos());
  674. const auto [x, y] = ScaleTouch(pos);
  675. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  676. const int center_x = width() / 2;
  677. const int center_y = height() / 2;
  678. input_subsystem->GetMouse()->MouseMove(x, y, touch_x, touch_y, center_x, center_y);
  679. if (Settings::values.mouse_panning && !Settings::values.mouse_enabled) {
  680. QCursor::setPos(mapToGlobal(QPoint{center_x, center_y}));
  681. }
  682. emit MouseActivity();
  683. }
  684. void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
  685. // Touch input is handled in TouchEndEvent
  686. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  687. return;
  688. }
  689. const auto button = QtButtonToMouseButton(event->button());
  690. input_subsystem->GetMouse()->ReleaseButton(button);
  691. }
  692. void GRenderWindow::wheelEvent(QWheelEvent* event) {
  693. const int x = event->angleDelta().x();
  694. const int y = event->angleDelta().y();
  695. input_subsystem->GetMouse()->MouseWheelChange(x, y);
  696. }
  697. void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) {
  698. QList<QTouchEvent::TouchPoint> touch_points = event->touchPoints();
  699. for (const auto& touch_point : touch_points) {
  700. const auto [x, y] = ScaleTouch(touch_point.pos());
  701. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  702. input_subsystem->GetTouchScreen()->TouchPressed(touch_x, touch_y, touch_point.id());
  703. }
  704. }
  705. void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) {
  706. QList<QTouchEvent::TouchPoint> touch_points = event->touchPoints();
  707. input_subsystem->GetTouchScreen()->ClearActiveFlag();
  708. for (const auto& touch_point : touch_points) {
  709. const auto [x, y] = ScaleTouch(touch_point.pos());
  710. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  711. input_subsystem->GetTouchScreen()->TouchMoved(touch_x, touch_y, touch_point.id());
  712. }
  713. input_subsystem->GetTouchScreen()->ReleaseInactiveTouch();
  714. }
  715. void GRenderWindow::TouchEndEvent() {
  716. input_subsystem->GetTouchScreen()->ReleaseAllTouch();
  717. }
  718. void GRenderWindow::InitializeCamera() {
  719. if (!Settings::values.enable_ir_sensor) {
  720. return;
  721. }
  722. bool camera_found = false;
  723. const QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
  724. for (const QCameraInfo& cameraInfo : cameras) {
  725. if (Settings::values.ir_sensor_device.GetValue() == cameraInfo.deviceName().toStdString() ||
  726. Settings::values.ir_sensor_device.GetValue() == "Auto") {
  727. camera = std::make_unique<QCamera>(cameraInfo);
  728. camera_found = true;
  729. break;
  730. }
  731. }
  732. if (!camera_found) {
  733. return;
  734. }
  735. camera_capture = std::make_unique<QCameraImageCapture>(camera.get());
  736. connect(camera_capture.get(), &QCameraImageCapture::imageCaptured, this,
  737. &GRenderWindow::OnCameraCapture);
  738. camera->unload();
  739. camera->setCaptureMode(QCamera::CaptureViewfinder);
  740. camera->load();
  741. camera->start();
  742. pending_camera_snapshots = 0;
  743. is_virtual_camera = false;
  744. camera_timer = std::make_unique<QTimer>();
  745. connect(camera_timer.get(), &QTimer::timeout, [this] { RequestCameraCapture(); });
  746. // This timer should be dependent of camera resolution 5ms for every 100 pixels
  747. camera_timer->start(100);
  748. }
  749. void GRenderWindow::FinalizeCamera() {
  750. if (camera_timer) {
  751. camera_timer->stop();
  752. }
  753. if (camera) {
  754. camera->unload();
  755. }
  756. }
  757. void GRenderWindow::RequestCameraCapture() {
  758. if (!Settings::values.enable_ir_sensor) {
  759. return;
  760. }
  761. // If the camera doesn't capture, test for virtual cameras
  762. if (pending_camera_snapshots > 5) {
  763. is_virtual_camera = true;
  764. }
  765. // Virtual cameras like obs need to reset the camera every capture
  766. if (is_virtual_camera) {
  767. camera->stop();
  768. camera->start();
  769. }
  770. pending_camera_snapshots++;
  771. camera_capture->capture();
  772. }
  773. void GRenderWindow::OnCameraCapture(int requestId, const QImage& img) {
  774. constexpr std::size_t camera_width = 320;
  775. constexpr std::size_t camera_height = 240;
  776. const auto converted =
  777. img.scaled(camera_width, camera_height, Qt::AspectRatioMode::IgnoreAspectRatio,
  778. Qt::TransformationMode::SmoothTransformation)
  779. .mirrored(false, true);
  780. std::vector<u32> camera_data{};
  781. camera_data.resize(camera_width * camera_height);
  782. std::memcpy(camera_data.data(), converted.bits(), camera_width * camera_height * sizeof(u32));
  783. input_subsystem->GetCamera()->SetCameraData(camera_width, camera_height, camera_data);
  784. pending_camera_snapshots = 0;
  785. }
  786. bool GRenderWindow::event(QEvent* event) {
  787. if (event->type() == QEvent::TouchBegin) {
  788. TouchBeginEvent(static_cast<QTouchEvent*>(event));
  789. return true;
  790. } else if (event->type() == QEvent::TouchUpdate) {
  791. TouchUpdateEvent(static_cast<QTouchEvent*>(event));
  792. return true;
  793. } else if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
  794. TouchEndEvent();
  795. return true;
  796. }
  797. return QWidget::event(event);
  798. }
  799. void GRenderWindow::focusOutEvent(QFocusEvent* event) {
  800. QWidget::focusOutEvent(event);
  801. input_subsystem->GetKeyboard()->ReleaseAllKeys();
  802. input_subsystem->GetMouse()->ReleaseAllButtons();
  803. input_subsystem->GetTouchScreen()->ReleaseAllTouch();
  804. }
  805. void GRenderWindow::resizeEvent(QResizeEvent* event) {
  806. QWidget::resizeEvent(event);
  807. OnFramebufferSizeChanged();
  808. }
  809. std::unique_ptr<Core::Frontend::GraphicsContext> GRenderWindow::CreateSharedContext() const {
  810. #ifdef HAS_OPENGL
  811. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  812. auto c = static_cast<OpenGLSharedContext*>(main_context.get());
  813. // Bind the shared contexts to the main surface in case the backend wants to take over
  814. // presentation
  815. return std::make_unique<OpenGLSharedContext>(c->GetShareContext(),
  816. child_widget->windowHandle());
  817. }
  818. #endif
  819. return std::make_unique<DummyContext>();
  820. }
  821. bool GRenderWindow::InitRenderTarget() {
  822. ReleaseRenderTarget();
  823. {
  824. // Create a dummy render widget so that Qt
  825. // places the render window at the correct position.
  826. const RenderWidget dummy_widget{this};
  827. }
  828. first_frame = false;
  829. switch (Settings::values.renderer_backend.GetValue()) {
  830. case Settings::RendererBackend::OpenGL:
  831. if (!InitializeOpenGL()) {
  832. return false;
  833. }
  834. break;
  835. case Settings::RendererBackend::Vulkan:
  836. if (!InitializeVulkan()) {
  837. return false;
  838. }
  839. break;
  840. }
  841. // Update the Window System information with the new render target
  842. window_info = GetWindowSystemInfo(child_widget->windowHandle());
  843. child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  844. layout()->addWidget(child_widget);
  845. // Reset minimum required size to avoid resizing issues on the main window after restarting.
  846. setMinimumSize(1, 1);
  847. resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  848. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  849. OnFramebufferSizeChanged();
  850. BackupGeometry();
  851. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  852. if (!LoadOpenGL()) {
  853. return false;
  854. }
  855. }
  856. return true;
  857. }
  858. void GRenderWindow::ReleaseRenderTarget() {
  859. if (child_widget) {
  860. layout()->removeWidget(child_widget);
  861. child_widget->deleteLater();
  862. child_widget = nullptr;
  863. }
  864. main_context.reset();
  865. }
  866. void GRenderWindow::CaptureScreenshot(const QString& screenshot_path) {
  867. auto& renderer = system.Renderer();
  868. const f32 res_scale = Settings::values.resolution_info.up_factor;
  869. if (renderer.IsScreenshotPending()) {
  870. LOG_WARNING(Render,
  871. "A screenshot is already requested or in progress, ignoring the request");
  872. return;
  873. }
  874. const Layout::FramebufferLayout layout{Layout::FrameLayoutFromResolutionScale(res_scale)};
  875. screenshot_image = QImage(QSize(layout.width, layout.height), QImage::Format_RGB32);
  876. renderer.RequestScreenshot(
  877. screenshot_image.bits(),
  878. [=, this](bool invert_y) {
  879. const std::string std_screenshot_path = screenshot_path.toStdString();
  880. if (screenshot_image.mirrored(false, invert_y).save(screenshot_path)) {
  881. LOG_INFO(Frontend, "Screenshot saved to \"{}\"", std_screenshot_path);
  882. } else {
  883. LOG_ERROR(Frontend, "Failed to save screenshot to \"{}\"", std_screenshot_path);
  884. }
  885. },
  886. layout);
  887. }
  888. bool GRenderWindow::IsLoadingComplete() const {
  889. return first_frame;
  890. }
  891. void GRenderWindow::OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) {
  892. setMinimumSize(minimal_size.first, minimal_size.second);
  893. }
  894. bool GRenderWindow::InitializeOpenGL() {
  895. #ifdef HAS_OPENGL
  896. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground,
  897. // WA_DontShowOnScreen, WA_DeleteOnClose
  898. auto child = new OpenGLRenderWidget(this);
  899. child_widget = child;
  900. child_widget->windowHandle()->create();
  901. auto context = std::make_shared<OpenGLSharedContext>(child->windowHandle());
  902. main_context = context;
  903. child->SetContext(
  904. std::make_unique<OpenGLSharedContext>(context->GetShareContext(), child->windowHandle()));
  905. return true;
  906. #else
  907. QMessageBox::warning(this, tr("OpenGL not available!"),
  908. tr("yuzu has not been compiled with OpenGL support."));
  909. return false;
  910. #endif
  911. }
  912. bool GRenderWindow::InitializeVulkan() {
  913. auto child = new VulkanRenderWidget(this);
  914. child_widget = child;
  915. child_widget->windowHandle()->create();
  916. main_context = std::make_unique<DummyContext>();
  917. return true;
  918. }
  919. bool GRenderWindow::LoadOpenGL() {
  920. auto context = CreateSharedContext();
  921. auto scope = context->Acquire();
  922. if (!gladLoadGL()) {
  923. QMessageBox::warning(
  924. this, tr("Error while initializing OpenGL!"),
  925. tr("Your GPU may not support OpenGL, or you do not have the latest graphics driver."));
  926. return false;
  927. }
  928. const QString renderer =
  929. QString::fromUtf8(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
  930. if (!GLAD_GL_VERSION_4_6) {
  931. LOG_ERROR(Frontend, "GPU does not support OpenGL 4.6: {}", renderer.toStdString());
  932. QMessageBox::warning(this, tr("Error while initializing OpenGL 4.6!"),
  933. tr("Your GPU may not support OpenGL 4.6, or you do not have the "
  934. "latest graphics driver.<br><br>GL Renderer:<br>%1")
  935. .arg(renderer));
  936. return false;
  937. }
  938. QStringList unsupported_gl_extensions = GetUnsupportedGLExtensions();
  939. if (!unsupported_gl_extensions.empty()) {
  940. QMessageBox::warning(
  941. this, tr("Error while initializing OpenGL!"),
  942. tr("Your GPU may not support one or more required OpenGL extensions. Please ensure you "
  943. "have the latest graphics driver.<br><br>GL Renderer:<br>%1<br><br>Unsupported "
  944. "extensions:<br>%2")
  945. .arg(renderer)
  946. .arg(unsupported_gl_extensions.join(QStringLiteral("<br>"))));
  947. return false;
  948. }
  949. return true;
  950. }
  951. QStringList GRenderWindow::GetUnsupportedGLExtensions() const {
  952. QStringList unsupported_ext;
  953. // Extensions required to support some texture formats.
  954. if (!GLAD_GL_EXT_texture_compression_s3tc) {
  955. unsupported_ext.append(QStringLiteral("EXT_texture_compression_s3tc"));
  956. }
  957. if (!GLAD_GL_ARB_texture_compression_rgtc) {
  958. unsupported_ext.append(QStringLiteral("ARB_texture_compression_rgtc"));
  959. }
  960. if (!unsupported_ext.empty()) {
  961. LOG_ERROR(Frontend, "GPU does not support all required extensions: {}",
  962. glGetString(GL_RENDERER));
  963. }
  964. for (const QString& ext : unsupported_ext) {
  965. LOG_ERROR(Frontend, "Unsupported GL extension: {}", ext.toStdString());
  966. }
  967. return unsupported_ext;
  968. }
  969. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread_) {
  970. emu_thread = emu_thread_;
  971. }
  972. void GRenderWindow::OnEmulationStopping() {
  973. emu_thread = nullptr;
  974. }
  975. void GRenderWindow::showEvent(QShowEvent* event) {
  976. QWidget::showEvent(event);
  977. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  978. connect(windowHandle(), &QWindow::screenChanged, this, &GRenderWindow::OnFramebufferSizeChanged,
  979. Qt::UniqueConnection);
  980. }
  981. bool GRenderWindow::eventFilter(QObject* object, QEvent* event) {
  982. if (event->type() == QEvent::HoverMove) {
  983. if (Settings::values.mouse_panning || Settings::values.mouse_enabled) {
  984. auto* hover_event = static_cast<QMouseEvent*>(event);
  985. mouseMoveEvent(hover_event);
  986. return false;
  987. }
  988. emit MouseActivity();
  989. }
  990. return false;
  991. }