bootmanager.cpp 9.4 KB

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