bootmanager.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <condition_variable>
  7. #include <memory>
  8. #include <mutex>
  9. #include <QImage>
  10. #include <QThread>
  11. #include <QTouchEvent>
  12. #include <QWidget>
  13. #include "common/thread.h"
  14. #include "core/frontend/emu_window.h"
  15. class GRenderWindow;
  16. class GMainWindow;
  17. class QKeyEvent;
  18. class QStringList;
  19. namespace Core {
  20. enum class SystemResultStatus : u32;
  21. class System;
  22. } // namespace Core
  23. namespace InputCommon {
  24. class InputSubsystem;
  25. enum class MouseButton;
  26. } // namespace InputCommon
  27. namespace InputCommon::TasInput {
  28. enum class TasState;
  29. } // namespace InputCommon::TasInput
  30. namespace VideoCore {
  31. enum class LoadCallbackStage;
  32. class RendererBase;
  33. } // namespace VideoCore
  34. class EmuThread final : public QThread {
  35. Q_OBJECT
  36. public:
  37. explicit EmuThread(Core::System& system_);
  38. ~EmuThread() override;
  39. /**
  40. * Start emulation (on new thread)
  41. * @warning Only call when not running!
  42. */
  43. void run() override;
  44. /**
  45. * Steps the emulation thread by a single CPU instruction (if the CPU is not already running)
  46. * @note This function is thread-safe
  47. */
  48. void ExecStep() {
  49. exec_step = true;
  50. running_cv.notify_all();
  51. }
  52. /**
  53. * Sets whether the emulation thread is running or not
  54. * @param running Boolean value, set the emulation thread to running if true
  55. * @note This function is thread-safe
  56. */
  57. void SetRunning(bool running) {
  58. std::unique_lock lock{running_mutex};
  59. this->running = running;
  60. lock.unlock();
  61. running_cv.notify_all();
  62. if (!running) {
  63. running_wait.Set();
  64. /// Wait until effectively paused
  65. while (running_guard)
  66. ;
  67. }
  68. }
  69. /**
  70. * Check if the emulation thread is running or not
  71. * @return True if the emulation thread is running, otherwise false
  72. * @note This function is thread-safe
  73. */
  74. bool IsRunning() const {
  75. return running;
  76. }
  77. /**
  78. * Requests for the emulation thread to stop running
  79. */
  80. void RequestStop() {
  81. stop_source.request_stop();
  82. SetRunning(false);
  83. }
  84. private:
  85. bool exec_step = false;
  86. bool running = false;
  87. std::stop_source stop_source;
  88. std::mutex running_mutex;
  89. std::condition_variable_any running_cv;
  90. Common::Event running_wait{};
  91. std::atomic_bool running_guard{false};
  92. Core::System& system;
  93. signals:
  94. /**
  95. * Emitted when the CPU has halted execution
  96. *
  97. * @warning When connecting to this signal from other threads, make sure to specify either
  98. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  99. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  100. */
  101. void DebugModeEntered();
  102. /**
  103. * Emitted right before the CPU continues execution
  104. *
  105. * @warning When connecting to this signal from other threads, make sure to specify either
  106. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  107. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  108. */
  109. void DebugModeLeft();
  110. void ErrorThrown(Core::SystemResultStatus, std::string);
  111. void LoadProgress(VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total);
  112. };
  113. class GRenderWindow : public QWidget, public Core::Frontend::EmuWindow {
  114. Q_OBJECT
  115. public:
  116. explicit GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_,
  117. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem_,
  118. Core::System& system_);
  119. ~GRenderWindow() override;
  120. // EmuWindow implementation.
  121. void OnFrameDisplayed() override;
  122. bool IsShown() const override;
  123. std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;
  124. void BackupGeometry();
  125. void RestoreGeometry();
  126. void restoreGeometry(const QByteArray& geometry); // overridden
  127. QByteArray saveGeometry(); // overridden
  128. qreal windowPixelRatio() const;
  129. void closeEvent(QCloseEvent* event) override;
  130. void resizeEvent(QResizeEvent* event) override;
  131. /// Converts a Qt keybard key into NativeKeyboard key
  132. static int QtKeyToSwitchKey(Qt::Key qt_keys);
  133. /// Converts a Qt modifier keys into NativeKeyboard modifier keys
  134. static int QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers);
  135. void keyPressEvent(QKeyEvent* event) override;
  136. void keyReleaseEvent(QKeyEvent* event) override;
  137. /// Converts a Qt mouse button into MouseInput mouse button
  138. static InputCommon::MouseButton QtButtonToMouseButton(Qt::MouseButton button);
  139. void mousePressEvent(QMouseEvent* event) override;
  140. void mouseMoveEvent(QMouseEvent* event) override;
  141. void mouseReleaseEvent(QMouseEvent* event) override;
  142. void wheelEvent(QWheelEvent* event) override;
  143. bool event(QEvent* event) override;
  144. void focusOutEvent(QFocusEvent* event) override;
  145. bool InitRenderTarget();
  146. /// Destroy the previous run's child_widget which should also destroy the child_window
  147. void ReleaseRenderTarget();
  148. bool IsLoadingComplete() const;
  149. void CaptureScreenshot(const QString& screenshot_path);
  150. std::pair<u32, u32> ScaleTouch(const QPointF& pos) const;
  151. /**
  152. * Instructs the window to re-launch the application using the specified program_index.
  153. * @param program_index Specifies the index within the application of the program to launch.
  154. */
  155. void ExecuteProgram(std::size_t program_index);
  156. /// Instructs the window to exit the application.
  157. void Exit();
  158. public slots:
  159. void OnEmulationStarting(EmuThread* emu_thread);
  160. void OnEmulationStopping();
  161. void OnFramebufferSizeChanged();
  162. signals:
  163. /// Emitted when the window is closed
  164. void Closed();
  165. void FirstFrameDisplayed();
  166. void ExecuteProgramSignal(std::size_t program_index);
  167. void ExitSignal();
  168. void MouseActivity();
  169. void TasPlaybackStateChanged();
  170. private:
  171. void TouchBeginEvent(const QTouchEvent* event);
  172. void TouchUpdateEvent(const QTouchEvent* event);
  173. void TouchEndEvent();
  174. void TouchStart(const QTouchEvent::TouchPoint& touch_point);
  175. bool TouchUpdate(const QTouchEvent::TouchPoint& touch_point);
  176. bool TouchExist(std::size_t id, const QList<QTouchEvent::TouchPoint>& touch_points) const;
  177. void OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) override;
  178. bool InitializeOpenGL();
  179. bool InitializeVulkan();
  180. bool LoadOpenGL();
  181. QStringList GetUnsupportedGLExtensions() const;
  182. EmuThread* emu_thread;
  183. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem;
  184. // Main context that will be shared with all other contexts that are requested.
  185. // If this is used in a shared context setting, then this should not be used directly, but
  186. // should instead be shared from
  187. std::shared_ptr<Core::Frontend::GraphicsContext> main_context;
  188. /// Temporary storage of the screenshot taken
  189. QImage screenshot_image;
  190. QByteArray geometry;
  191. QWidget* child_widget = nullptr;
  192. bool first_frame = false;
  193. InputCommon::TasInput::TasState last_tas_state;
  194. std::array<std::size_t, 16> touch_ids{};
  195. Core::System& system;
  196. protected:
  197. void showEvent(QShowEvent* event) override;
  198. bool eventFilter(QObject* object, QEvent* event) override;
  199. };