bootmanager.h 7.5 KB

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