bootmanager.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <QHBoxLayout>
  2. #include <QKeyEvent>
  3. #include <QApplication>
  4. #include "common/common.h"
  5. #include "bootmanager.hxx"
  6. #include "core/core.h"
  7. #include "core/loader/loader.h"
  8. #include "core/hw/hw.h"
  9. #include "video_core/video_core.h"
  10. #include "version.h"
  11. #define APP_NAME "citra"
  12. #define APP_VERSION "0.1-" VERSION
  13. #define APP_TITLE APP_NAME " " APP_VERSION
  14. #define COPYRIGHT "Copyright (C) 2013-2014 Citra Team"
  15. EmuThread::EmuThread(GRenderWindow* render_window) :
  16. exec_cpu_step(false), cpu_running(false),
  17. render_window(render_window), filename(""),
  18. stop_run(false)
  19. {
  20. }
  21. void EmuThread::SetFilename(std::string filename)
  22. {
  23. this->filename = filename;
  24. }
  25. void EmuThread::run()
  26. {
  27. stop_run = false;
  28. while (!stop_run)
  29. {
  30. for (int tight_loop = 0; tight_loop < 10000; ++tight_loop)
  31. {
  32. if (cpu_running || exec_cpu_step)
  33. {
  34. if (exec_cpu_step)
  35. exec_cpu_step = false;
  36. Core::SingleStep();
  37. if (!cpu_running) {
  38. emit CPUStepped();
  39. yieldCurrentThread();
  40. }
  41. }
  42. }
  43. }
  44. render_window->moveContext();
  45. Core::Stop();
  46. }
  47. void EmuThread::Stop()
  48. {
  49. if (!isRunning())
  50. {
  51. INFO_LOG(MASTER_LOG, "EmuThread::Stop called while emu thread wasn't running, returning...");
  52. return;
  53. }
  54. stop_run = true;
  55. //core::g_state = core::SYS_DIE;
  56. wait(500);
  57. if (isRunning())
  58. {
  59. WARN_LOG(MASTER_LOG, "EmuThread still running, terminating...");
  60. quit();
  61. wait(1000);
  62. if (isRunning())
  63. {
  64. WARN_LOG(MASTER_LOG, "EmuThread STILL running, something is wrong here...");
  65. terminate();
  66. }
  67. }
  68. INFO_LOG(MASTER_LOG, "EmuThread stopped");
  69. }
  70. // This class overrides paintEvent and resizeEvent to prevent the GUI thread from stealing GL context.
  71. // The corresponding functionality is handled in EmuThread instead
  72. class GGLWidgetInternal : public QGLWidget
  73. {
  74. public:
  75. GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent) : QGLWidget(fmt, parent)
  76. {
  77. parent_ = parent;
  78. }
  79. void paintEvent(QPaintEvent* ev)
  80. {
  81. }
  82. void resizeEvent(QResizeEvent* ev) {
  83. parent_->SetClientAreaWidth(size().width());
  84. parent_->SetClientAreaHeight(size().height());
  85. }
  86. private:
  87. GRenderWindow* parent_;
  88. };
  89. EmuThread& GRenderWindow::GetEmuThread()
  90. {
  91. return emu_thread;
  92. }
  93. GRenderWindow::GRenderWindow(QWidget* parent) : QWidget(parent), emu_thread(this)
  94. {
  95. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, WA_DontShowOnScreen, WA_DeleteOnClose
  96. QGLFormat fmt;
  97. fmt.setProfile(QGLFormat::CoreProfile);
  98. fmt.setVersion(3,2);
  99. fmt.setSampleBuffers(true);
  100. fmt.setSamples(4);
  101. child = new GGLWidgetInternal(fmt, this);
  102. QBoxLayout* layout = new QHBoxLayout(this);
  103. resize(VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight);
  104. layout->addWidget(child);
  105. layout->setMargin(0);
  106. setLayout(layout);
  107. QObject::connect(&emu_thread, SIGNAL(started()), this, SLOT(moveContext()));
  108. BackupGeometry();
  109. }
  110. void GRenderWindow::moveContext()
  111. {
  112. DoneCurrent();
  113. // We need to move GL context to the swapping thread in Qt5
  114. #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
  115. // If the thread started running, move the GL Context to the new thread. Otherwise, move it back.
  116. child->context()->moveToThread((QThread::currentThread() == qApp->thread()) ? &emu_thread : qApp->thread());
  117. #endif
  118. }
  119. GRenderWindow::~GRenderWindow()
  120. {
  121. if (emu_thread.isRunning())
  122. emu_thread.Stop();
  123. }
  124. void GRenderWindow::SwapBuffers()
  125. {
  126. // MakeCurrent is already called in renderer_opengl
  127. child->swapBuffers();
  128. }
  129. void GRenderWindow::closeEvent(QCloseEvent* event)
  130. {
  131. if (emu_thread.isRunning())
  132. emu_thread.Stop();
  133. QWidget::closeEvent(event);
  134. }
  135. void GRenderWindow::MakeCurrent()
  136. {
  137. child->makeCurrent();
  138. }
  139. void GRenderWindow::DoneCurrent()
  140. {
  141. child->doneCurrent();
  142. }
  143. void GRenderWindow::PollEvents() {
  144. // TODO(ShizZy): Does this belong here? This is a reasonable place to update the window title
  145. // from the main thread, but this should probably be in an event handler...
  146. /*
  147. static char title[128];
  148. sprintf(title, "%s (FPS: %02.02f)", window_title_.c_str(),
  149. video_core::g_renderer->current_fps());
  150. setWindowTitle(title);
  151. */
  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() == NULL)
  173. return ((QGLWidget*)this)->saveGeometry();
  174. else
  175. return geometry;
  176. }
  177. void GRenderWindow::keyPressEvent(QKeyEvent* event)
  178. {
  179. /*
  180. bool key_processed = false;
  181. for (unsigned int channel = 0; channel < 4 && controller_interface(); ++channel)
  182. if (controller_interface()->SetControllerStatus(channel, event->key(), input_common::GCController::PRESSED))
  183. key_processed = true;
  184. if (!key_processed)
  185. QWidget::keyPressEvent(event);
  186. */
  187. }
  188. void GRenderWindow::keyReleaseEvent(QKeyEvent* event)
  189. {
  190. /*
  191. bool key_processed = false;
  192. for (unsigned int channel = 0; channel < 4 && controller_interface(); ++channel)
  193. if (controller_interface()->SetControllerStatus(channel, event->key(), input_common::GCController::RELEASED))
  194. key_processed = true;
  195. if (!key_processed)
  196. QWidget::keyPressEvent(event);
  197. */
  198. }