bootmanager.cpp 8.9 KB

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