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