bootmanager.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include <QHBoxLayout>
  2. #include <QKeyEvent>
  3. #include <QApplication>
  4. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  5. // Required for screen DPI information
  6. #include <QScreen>
  7. #include <QWindow>
  8. #endif
  9. #include "bootmanager.h"
  10. #include "main.h"
  11. #include "common/string_util.h"
  12. #include "common/scm_rev.h"
  13. #include "common/key_map.h"
  14. #include "common/microprofile.h"
  15. #include "core/core.h"
  16. #include "core/settings.h"
  17. #include "core/system.h"
  18. #include "video_core/debug_utils/debug_utils.h"
  19. #include "video_core/video_core.h"
  20. #include "citra_qt/version.h"
  21. #define APP_NAME "citra"
  22. #define APP_VERSION "0.1-" VERSION
  23. #define APP_TITLE APP_NAME " " APP_VERSION
  24. #define COPYRIGHT "Copyright (C) 2013-2014 Citra Team"
  25. EmuThread::EmuThread(GRenderWindow* render_window) :
  26. exec_step(false), running(false), stop_run(false), render_window(render_window) {
  27. }
  28. void EmuThread::run() {
  29. render_window->MakeCurrent();
  30. MicroProfileOnThreadCreate("EmuThread");
  31. stop_run = false;
  32. // holds whether the cpu was running during the last iteration,
  33. // so that the DebugModeLeft signal can be emitted before the
  34. // next execution step
  35. bool was_active = false;
  36. while (!stop_run) {
  37. if (running) {
  38. if (!was_active)
  39. emit DebugModeLeft();
  40. Core::RunLoop();
  41. was_active = running || exec_step;
  42. if (!was_active && !stop_run)
  43. emit DebugModeEntered();
  44. } else if (exec_step) {
  45. if (!was_active)
  46. emit DebugModeLeft();
  47. exec_step = false;
  48. Core::SingleStep();
  49. emit DebugModeEntered();
  50. yieldCurrentThread();
  51. was_active = false;
  52. } else {
  53. std::unique_lock<std::mutex> lock(running_mutex);
  54. running_cv.wait(lock, [this]{ return IsRunning() || exec_step || stop_run; });
  55. }
  56. }
  57. // Shutdown the core emulation
  58. System::Shutdown();
  59. MicroProfileOnThreadExit();
  60. render_window->moveContext();
  61. }
  62. // This class overrides paintEvent and resizeEvent to prevent the GUI thread from stealing GL context.
  63. // The corresponding functionality is handled in EmuThread instead
  64. class GGLWidgetInternal : public QGLWidget
  65. {
  66. public:
  67. GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent)
  68. : QGLWidget(fmt, parent), parent(parent) {
  69. }
  70. void paintEvent(QPaintEvent* ev) override {
  71. }
  72. void resizeEvent(QResizeEvent* ev) override {
  73. parent->OnClientAreaResized(ev->size().width(), ev->size().height());
  74. parent->OnFramebufferSizeChanged();
  75. }
  76. private:
  77. GRenderWindow* parent;
  78. };
  79. GRenderWindow::GRenderWindow(QWidget* parent, EmuThread* emu_thread) :
  80. QWidget(parent), keyboard_id(0), emu_thread(emu_thread) {
  81. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  82. setWindowTitle(QString::fromStdString(window_title));
  83. keyboard_id = KeyMap::NewDeviceId();
  84. ReloadSetKeymaps();
  85. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, WA_DontShowOnScreen, WA_DeleteOnClose
  86. QGLFormat fmt;
  87. fmt.setVersion(3,3);
  88. fmt.setProfile(QGLFormat::CoreProfile);
  89. // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
  90. fmt.setOption(QGL::NoDeprecatedFunctions);
  91. child = new GGLWidgetInternal(fmt, this);
  92. QBoxLayout* layout = new QHBoxLayout(this);
  93. resize(VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight);
  94. layout->addWidget(child);
  95. layout->setMargin(0);
  96. setLayout(layout);
  97. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  98. OnFramebufferSizeChanged();
  99. NotifyClientAreaSizeChanged(std::pair<unsigned,unsigned>(child->width(), child->height()));
  100. BackupGeometry();
  101. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  102. connect(this->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(OnFramebufferSizeChanged()));
  103. #endif
  104. }
  105. void GRenderWindow::moveContext()
  106. {
  107. DoneCurrent();
  108. // We need to move GL context to the swapping thread in Qt5
  109. #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
  110. // If the thread started running, move the GL Context to the new thread. Otherwise, move it back.
  111. auto thread = (QThread::currentThread() == qApp->thread() && emu_thread != nullptr) ? emu_thread : qApp->thread();
  112. child->context()->moveToThread(thread);
  113. #endif
  114. }
  115. void GRenderWindow::SwapBuffers()
  116. {
  117. #if !defined(QT_NO_DEBUG)
  118. // Qt debug runtime prints a bogus warning on the console if you haven't called makeCurrent
  119. // since the last time you called swapBuffers. This presumably means something if you're using
  120. // QGLWidget the "regular" way, but in our multi-threaded use case is harmless since we never
  121. // call doneCurrent in this thread.
  122. child->makeCurrent();
  123. #endif
  124. child->swapBuffers();
  125. }
  126. void GRenderWindow::MakeCurrent()
  127. {
  128. child->makeCurrent();
  129. }
  130. void GRenderWindow::DoneCurrent()
  131. {
  132. child->doneCurrent();
  133. }
  134. void GRenderWindow::PollEvents() {
  135. }
  136. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  137. //
  138. // Older versions get the window size (density independent pixels),
  139. // and hence, do not support DPI scaling ("retina" displays).
  140. // The result will be a viewport that is smaller than the extent of the window.
  141. void GRenderWindow::OnFramebufferSizeChanged()
  142. {
  143. // Screen changes potentially incur a change in screen DPI, hence we should update the framebuffer size
  144. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  145. // windowHandle() might not be accessible until the window is displayed to screen.
  146. auto pixel_ratio = windowHandle() ? (windowHandle()->screen()->devicePixelRatio()) : 1.0;
  147. unsigned width = child->QPaintDevice::width() * pixel_ratio;
  148. unsigned height = child->QPaintDevice::height() * pixel_ratio;
  149. #else
  150. unsigned width = child->QPaintDevice::width();
  151. unsigned height = child->QPaintDevice::height();
  152. #endif
  153. NotifyFramebufferLayoutChanged(EmuWindow::FramebufferLayout::DefaultScreenLayout(width, height));
  154. }
  155. void GRenderWindow::BackupGeometry()
  156. {
  157. geometry = ((QGLWidget*)this)->saveGeometry();
  158. }
  159. void GRenderWindow::RestoreGeometry()
  160. {
  161. // We don't want to back up the geometry here (obviously)
  162. QWidget::restoreGeometry(geometry);
  163. }
  164. void GRenderWindow::restoreGeometry(const QByteArray& geometry)
  165. {
  166. // Make sure users of this class don't need to deal with backing up the geometry themselves
  167. QWidget::restoreGeometry(geometry);
  168. BackupGeometry();
  169. }
  170. QByteArray GRenderWindow::saveGeometry()
  171. {
  172. // If we are a top-level widget, store the current geometry
  173. // otherwise, store the last backup
  174. if (parent() == nullptr)
  175. return ((QGLWidget*)this)->saveGeometry();
  176. else
  177. return geometry;
  178. }
  179. void GRenderWindow::closeEvent(QCloseEvent* event) {
  180. emit Closed();
  181. QWidget::closeEvent(event);
  182. }
  183. void GRenderWindow::keyPressEvent(QKeyEvent* event)
  184. {
  185. this->KeyPressed({event->key(), keyboard_id});
  186. }
  187. void GRenderWindow::keyReleaseEvent(QKeyEvent* event)
  188. {
  189. this->KeyReleased({event->key(), keyboard_id});
  190. }
  191. void GRenderWindow::mousePressEvent(QMouseEvent *event)
  192. {
  193. if (event->button() == Qt::LeftButton)
  194. {
  195. auto pos = event->pos();
  196. this->TouchPressed(static_cast<unsigned>(pos.x()), static_cast<unsigned>(pos.y()));
  197. }
  198. }
  199. void GRenderWindow::mouseMoveEvent(QMouseEvent *event)
  200. {
  201. auto pos = event->pos();
  202. this->TouchMoved(static_cast<unsigned>(std::max(pos.x(), 0)), static_cast<unsigned>(std::max(pos.y(), 0)));
  203. }
  204. void GRenderWindow::mouseReleaseEvent(QMouseEvent *event)
  205. {
  206. if (event->button() == Qt::LeftButton)
  207. this->TouchReleased();
  208. }
  209. void GRenderWindow::ReloadSetKeymaps()
  210. {
  211. for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
  212. KeyMap::SetKeyMapping({Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id}, Service::HID::pad_mapping[i]);
  213. }
  214. }
  215. void GRenderWindow::OnClientAreaResized(unsigned width, unsigned height)
  216. {
  217. NotifyClientAreaSizeChanged(std::make_pair(width, height));
  218. }
  219. void GRenderWindow::OnMinimalClientAreaChangeRequest(const std::pair<unsigned,unsigned>& minimal_size) {
  220. setMinimumSize(minimal_size.first, minimal_size.second);
  221. }
  222. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  223. this->emu_thread = emu_thread;
  224. }
  225. void GRenderWindow::OnEmulationStopping() {
  226. emu_thread = nullptr;
  227. }