bootmanager.cpp 9.6 KB

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