bootmanager.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/video_core.h"
  18. #include "video_core/debug_utils/debug_utils.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. }
  70. void resizeEvent(QResizeEvent* ev) override {
  71. parent->OnClientAreaResized(ev->size().width(), ev->size().height());
  72. parent->OnFramebufferSizeChanged();
  73. }
  74. private:
  75. GRenderWindow* parent;
  76. };
  77. GRenderWindow::GRenderWindow(QWidget* parent, EmuThread* emu_thread) :
  78. QWidget(parent), keyboard_id(0), emu_thread(emu_thread) {
  79. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  80. setWindowTitle(QString::fromStdString(window_title));
  81. keyboard_id = KeyMap::NewDeviceId();
  82. ReloadSetKeymaps();
  83. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, WA_DontShowOnScreen, WA_DeleteOnClose
  84. QGLFormat fmt;
  85. fmt.setVersion(3,3);
  86. fmt.setProfile(QGLFormat::CoreProfile);
  87. // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
  88. fmt.setOption(QGL::NoDeprecatedFunctions);
  89. child = new GGLWidgetInternal(fmt, this);
  90. QBoxLayout* layout = new QHBoxLayout(this);
  91. resize(VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight);
  92. layout->addWidget(child);
  93. layout->setMargin(0);
  94. setLayout(layout);
  95. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  96. OnFramebufferSizeChanged();
  97. NotifyClientAreaSizeChanged(std::pair<unsigned,unsigned>(child->width(), child->height()));
  98. BackupGeometry();
  99. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  100. connect(this->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(OnFramebufferSizeChanged()));
  101. #endif
  102. }
  103. void GRenderWindow::moveContext()
  104. {
  105. DoneCurrent();
  106. // We need to move GL context to the swapping thread in Qt5
  107. #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
  108. // If the thread started running, move the GL Context to the new thread. Otherwise, move it back.
  109. auto thread = (QThread::currentThread() == qApp->thread() && emu_thread != nullptr) ? emu_thread : qApp->thread();
  110. child->context()->moveToThread(thread);
  111. #endif
  112. }
  113. void GRenderWindow::SwapBuffers()
  114. {
  115. #if !defined(QT_NO_DEBUG)
  116. // Qt debug runtime prints a bogus warning on the console if you haven't called makeCurrent
  117. // since the last time you called swapBuffers. This presumably means something if you're using
  118. // QGLWidget the "regular" way, but in our multi-threaded use case is harmless since we never
  119. // call doneCurrent in this thread.
  120. child->makeCurrent();
  121. #endif
  122. child->swapBuffers();
  123. }
  124. void GRenderWindow::MakeCurrent()
  125. {
  126. child->makeCurrent();
  127. }
  128. void GRenderWindow::DoneCurrent()
  129. {
  130. child->doneCurrent();
  131. }
  132. void GRenderWindow::PollEvents() {
  133. }
  134. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  135. //
  136. // Older versions get the window size (density independent pixels),
  137. // and hence, do not support DPI scaling ("retina" displays).
  138. // The result will be a viewport that is smaller than the extent of the window.
  139. void GRenderWindow::OnFramebufferSizeChanged()
  140. {
  141. // Screen changes potentially incur a change in screen DPI, hence we should update the framebuffer size
  142. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  143. // windowHandle() might not be accessible until the window is displayed to screen.
  144. auto pixel_ratio = windowHandle() ? (windowHandle()->screen()->devicePixelRatio()) : 1.0;
  145. unsigned width = child->QPaintDevice::width() * pixel_ratio;
  146. unsigned height = child->QPaintDevice::height() * pixel_ratio;
  147. #else
  148. unsigned width = child->QPaintDevice::width();
  149. unsigned height = child->QPaintDevice::height();
  150. #endif
  151. NotifyFramebufferLayoutChanged(EmuWindow::FramebufferLayout::DefaultScreenLayout(width, height));
  152. }
  153. void GRenderWindow::BackupGeometry()
  154. {
  155. geometry = ((QGLWidget*)this)->saveGeometry();
  156. }
  157. void GRenderWindow::RestoreGeometry()
  158. {
  159. // We don't want to back up the geometry here (obviously)
  160. QWidget::restoreGeometry(geometry);
  161. }
  162. void GRenderWindow::restoreGeometry(const QByteArray& geometry)
  163. {
  164. // Make sure users of this class don't need to deal with backing up the geometry themselves
  165. QWidget::restoreGeometry(geometry);
  166. BackupGeometry();
  167. }
  168. QByteArray GRenderWindow::saveGeometry()
  169. {
  170. // If we are a top-level widget, store the current geometry
  171. // otherwise, store the last backup
  172. if (parent() == nullptr)
  173. return ((QGLWidget*)this)->saveGeometry();
  174. else
  175. return geometry;
  176. }
  177. void GRenderWindow::closeEvent(QCloseEvent* event) {
  178. emit Closed();
  179. QWidget::closeEvent(event);
  180. }
  181. void GRenderWindow::keyPressEvent(QKeyEvent* event)
  182. {
  183. this->KeyPressed({event->key(), keyboard_id});
  184. }
  185. void GRenderWindow::keyReleaseEvent(QKeyEvent* event)
  186. {
  187. this->KeyReleased({event->key(), keyboard_id});
  188. }
  189. void GRenderWindow::mousePressEvent(QMouseEvent *event)
  190. {
  191. if (event->button() == Qt::LeftButton)
  192. {
  193. auto pos = event->pos();
  194. this->TouchPressed(static_cast<unsigned>(pos.x()), static_cast<unsigned>(pos.y()));
  195. }
  196. }
  197. void GRenderWindow::mouseMoveEvent(QMouseEvent *event)
  198. {
  199. auto pos = event->pos();
  200. this->TouchMoved(static_cast<unsigned>(std::max(pos.x(), 0)), static_cast<unsigned>(std::max(pos.y(), 0)));
  201. }
  202. void GRenderWindow::mouseReleaseEvent(QMouseEvent *event)
  203. {
  204. if (event->button() == Qt::LeftButton)
  205. this->TouchReleased();
  206. }
  207. void GRenderWindow::ReloadSetKeymaps()
  208. {
  209. for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
  210. KeyMap::SetKeyMapping({Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id}, Service::HID::pad_mapping[i]);
  211. }
  212. }
  213. void GRenderWindow::OnClientAreaResized(unsigned width, unsigned height)
  214. {
  215. NotifyClientAreaSizeChanged(std::make_pair(width, height));
  216. }
  217. void GRenderWindow::OnMinimalClientAreaChangeRequest(const std::pair<unsigned,unsigned>& minimal_size) {
  218. setMinimumSize(minimal_size.first, minimal_size.second);
  219. }
  220. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  221. this->emu_thread = emu_thread;
  222. }
  223. void GRenderWindow::OnEmulationStopping() {
  224. emu_thread = nullptr;
  225. }