all repos — mgba @ d3678471f681215765877d4d8be91ac4bbe768ff

mGBA Game Boy Advance Emulator

src/platform/qt/GameController.cpp (view raw)

   1/* Copyright (c) 2013-2014 Jeffrey Pfau
   2 *
   3 * This Source Code Form is subject to the terms of the Mozilla Public
   4 * License, v. 2.0. If a copy of the MPL was not distributed with this
   5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
   6#include "GameController.h"
   7
   8#include "AudioProcessor.h"
   9#include "InputController.h"
  10#include "LogController.h"
  11#include "MultiplayerController.h"
  12#include "VFileDevice.h"
  13
  14#include <QCoreApplication>
  15#include <QDateTime>
  16
  17#include <ctime>
  18
  19#include <mgba/core/config.h>
  20#include <mgba/core/directories.h>
  21#include <mgba/core/serialize.h>
  22#include <mgba/core/tile-cache.h>
  23#ifdef M_CORE_GBA
  24#include <mgba/gba/interface.h>
  25#include <mgba/internal/gba/gba.h>
  26#include <mgba/gba/core.h>
  27#include <mgba/internal/gba/renderers/tile-cache.h>
  28#include <mgba/internal/gba/sharkport.h>
  29#endif
  30#ifdef M_CORE_GB
  31#include <mgba/internal/gb/gb.h>
  32#include <mgba/internal/gb/renderers/tile-cache.h>
  33#endif
  34#include <mgba-util/vfs.h>
  35#include <mgba/feature/video-logger.h>
  36
  37using namespace QGBA;
  38using namespace std;
  39
  40GameController::GameController(QObject* parent)
  41	: QObject(parent)
  42	, m_audioProcessor(AudioProcessor::create())
  43	, m_saveStateFlags(SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS | SAVESTATE_RTC | SAVESTATE_METADATA)
  44	, m_loadStateFlags(SAVESTATE_SCREENSHOT | SAVESTATE_RTC)
  45{
  46#ifdef M_CORE_GBA
  47	m_lux.p = this;
  48	m_lux.sample = [](GBALuminanceSource* context) {
  49		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
  50		lux->value = 0xFF - lux->p->m_luxValue;
  51	};
  52
  53	m_lux.readLuminance = [](GBALuminanceSource* context) {
  54		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
  55		return lux->value;
  56	};
  57	setLuminanceLevel(0);
  58#endif
  59
  60	m_threadContext.startCallback = [](mCoreThread* context) {
  61		GameController* controller = static_cast<GameController*>(context->userData);
  62		context->core->setPeripheral(context->core, mPERIPH_ROTATION, controller->m_inputController->rotationSource());
  63		context->core->setPeripheral(context->core, mPERIPH_RUMBLE, controller->m_inputController->rumble());
  64
  65		for (size_t i = 0; i < controller->m_audioChannels.size(); ++i) {
  66			context->core->enableAudioChannel(context->core, i, controller->m_audioChannels[i]);
  67		}
  68		for (size_t i = 0; i < controller->m_videoLayers.size(); ++i) {
  69			context->core->enableVideoLayer(context->core, i, controller->m_videoLayers[i]);
  70		}
  71
  72		switch (context->core->platform(context->core)) {
  73#ifdef M_CORE_GBA
  74		case PLATFORM_GBA:
  75			context->core->setPeripheral(context->core, mPERIPH_GBA_LUMINANCE, &controller->m_lux);
  76			break;
  77#endif
  78		default:
  79			break;
  80		}
  81		controller->m_fpsTarget = context->sync.fpsTarget;
  82
  83		if (controller->m_override) {
  84			controller->m_override->identify(context->core);
  85			controller->m_override->apply(context->core);
  86		}
  87
  88		if (mCoreLoadState(context->core, 0, controller->m_loadStateFlags)) {
  89			mCoreDeleteState(context->core, 0);
  90		}
  91
  92		controller->m_gameOpen = true;
  93		if (controller->m_multiplayer) {
  94			controller->m_multiplayer->attachGame(controller);
  95		}
  96
  97		QString path = controller->m_fname;
  98		if (!controller->m_fsub.isEmpty()) {
  99			path += QDir::separator() + controller->m_fsub;
 100		}
 101		QMetaObject::invokeMethod(controller, "gameStarted", Q_ARG(mCoreThread*, context), Q_ARG(const QString&, path));
 102		QMetaObject::invokeMethod(controller, "startAudio");
 103	};
 104
 105	m_threadContext.resetCallback = [](mCoreThread* context) {
 106		GameController* controller = static_cast<GameController*>(context->userData);
 107		for (auto action : controller->m_resetActions) {
 108			action();
 109		}
 110		controller->m_resetActions.clear();
 111
 112		unsigned width, height;
 113		controller->m_threadContext.core->desiredVideoDimensions(controller->m_threadContext.core, &width, &height);
 114		memset(controller->m_frontBuffer, 0xFF, width * height * BYTES_PER_PIXEL);
 115		QMetaObject::invokeMethod(controller, "frameAvailable", Q_ARG(const uint32_t*, controller->m_frontBuffer));
 116		if (controller->m_pauseAfterFrame.testAndSetAcquire(true, false)) {
 117			mCoreThreadPauseFromThread(context);
 118			QMetaObject::invokeMethod(controller, "gamePaused", Q_ARG(mCoreThread*, context));
 119		}
 120	};
 121
 122	m_threadContext.cleanCallback = [](mCoreThread* context) {
 123		GameController* controller = static_cast<GameController*>(context->userData);
 124
 125		if (controller->m_multiplayer) {
 126			controller->m_multiplayer->detachGame(controller);
 127		}
 128		controller->m_patch = QString();
 129		controller->clearOverride();
 130		controller->endVideoLog();
 131
 132		QMetaObject::invokeMethod(controller->m_audioProcessor, "pause");
 133
 134		QMetaObject::invokeMethod(controller, "gameStopped", Q_ARG(mCoreThread*, context));
 135		QMetaObject::invokeMethod(controller, "cleanGame");
 136	};
 137
 138	m_threadContext.frameCallback = [](mCoreThread* context) {
 139		GameController* controller = static_cast<GameController*>(context->userData);
 140		unsigned width, height;
 141		controller->m_threadContext.core->desiredVideoDimensions(controller->m_threadContext.core, &width, &height);
 142		memcpy(controller->m_frontBuffer, controller->m_drawContext, width * height * BYTES_PER_PIXEL);
 143		QMetaObject::invokeMethod(controller, "frameAvailable", Q_ARG(const uint32_t*, controller->m_frontBuffer));
 144
 145		// If no one is using the tile cache, disable it
 146		if (controller->m_tileCache && controller->m_tileCache.unique()) {
 147			switch (controller->platform()) {
 148#ifdef M_CORE_GBA
 149			case PLATFORM_GBA: {
 150				GBA* gba = static_cast<GBA*>(context->core->board);
 151				gba->video.renderer->cache = nullptr;
 152				break;
 153			}
 154#endif
 155#ifdef M_CORE_GB
 156			case PLATFORM_GB: {
 157				GB* gb = static_cast<GB*>(context->core->board);
 158				gb->video.renderer->cache = nullptr;
 159				break;
 160			}
 161#endif
 162			default:
 163				break;
 164			}
 165			mTileCacheDeinit(controller->m_tileCache.get());
 166			controller->m_tileCache.reset();
 167		}
 168
 169
 170		if (controller->m_pauseAfterFrame.testAndSetAcquire(true, false)) {
 171			mCoreThreadPauseFromThread(context);
 172			QMetaObject::invokeMethod(controller, "gamePaused", Q_ARG(mCoreThread*, context));
 173		}
 174	};
 175
 176	m_threadContext.sleepCallback = [](mCoreThread* context) {
 177		if (!context) {
 178			return;
 179		}
 180		GameController* controller = static_cast<GameController*>(context->userData);
 181		if (!mCoreSaveState(context->core, 0, controller->m_saveStateFlags)) {
 182			return;
 183		}
 184		QMetaObject::invokeMethod(controller, "closeGame");
 185	};
 186
 187	m_threadContext.logger.d.log = [](mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) {
 188		mThreadLogger* logContext = reinterpret_cast<mThreadLogger*>(logger);
 189		mCoreThread* context = logContext->p;
 190
 191		static const char* savestateMessage = "State %i loaded";
 192		static const char* savestateFailedMessage = "State %i failed to load";
 193		static int biosCat = -1;
 194		static int statusCat = -1;
 195		if (!context) {
 196			return;
 197		}
 198		GameController* controller = static_cast<GameController*>(context->userData);
 199		QString message;
 200		if (biosCat < 0) {
 201			biosCat = mLogCategoryById("gba.bios");
 202		}
 203		if (statusCat < 0) {
 204			statusCat = mLogCategoryById("core.status");
 205		}
 206#ifdef M_CORE_GBA
 207		if (level == mLOG_STUB && category == biosCat) {
 208			va_list argc;
 209			va_copy(argc, args);
 210			int immediate = va_arg(argc, int);
 211			va_end(argc);
 212			QMetaObject::invokeMethod(controller, "unimplementedBiosCall", Q_ARG(int, immediate));
 213		} else
 214#endif
 215		if (category == statusCat) {
 216			// Slot 0 is reserved for suspend points
 217			if (strncmp(savestateMessage, format, strlen(savestateMessage)) == 0) {
 218				va_list argc;
 219				va_copy(argc, args);
 220				int slot = va_arg(argc, int);
 221				va_end(argc);
 222				if (slot == 0) {
 223					format = "Loaded suspend state";
 224				}
 225			} else if (strncmp(savestateFailedMessage, format, strlen(savestateFailedMessage)) == 0) {
 226				va_list argc;
 227				va_copy(argc, args);
 228				int slot = va_arg(argc, int);
 229				va_end(argc);
 230				if (slot == 0) {
 231					return;
 232				}
 233			}
 234			message = QString().vsprintf(format, args);
 235			QMetaObject::invokeMethod(controller, "statusPosted", Q_ARG(const QString&, message));
 236		}
 237		if (level == mLOG_FATAL) {
 238			mCoreThreadMarkCrashed(controller->thread());
 239			QMetaObject::invokeMethod(controller, "crashGame", Q_ARG(const QString&, QString().vsprintf(format, args)));
 240		} else if (!(controller->m_logLevels & level)) {
 241			return;
 242		}
 243		message = QString().vsprintf(format, args);
 244		QMetaObject::invokeMethod(controller, "postLog", Q_ARG(int, level), Q_ARG(int, category), Q_ARG(const QString&, message));
 245	};
 246
 247	m_threadContext.userData = this;
 248
 249	connect(this, &GameController::gamePaused, m_audioProcessor, &AudioProcessor::pause);
 250	connect(this, &GameController::gameStarted, m_audioProcessor, &AudioProcessor::setInput);
 251	connect(this, &GameController::frameAvailable, this, &GameController::pollEvents);
 252	connect(this, &GameController::frameAvailable, this, &GameController::updateAutofire);
 253}
 254
 255GameController::~GameController() {
 256	disconnect();
 257	closeGame();
 258	clearMultiplayerController();
 259	delete m_backupLoadState;
 260}
 261
 262void GameController::setMultiplayerController(MultiplayerController* controller) {
 263	if (controller == m_multiplayer) {
 264		return;
 265	}
 266	clearMultiplayerController();
 267	m_multiplayer = controller;
 268	if (isLoaded()) {
 269		mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* thread) {
 270			GameController* controller = static_cast<GameController*>(thread->userData);
 271			controller->m_multiplayer->attachGame(controller);
 272		});
 273	}
 274}
 275
 276void GameController::clearMultiplayerController() {
 277	if (!m_multiplayer) {
 278		return;
 279	}
 280	m_multiplayer->detachGame(this);
 281	m_multiplayer = nullptr;
 282}
 283
 284void GameController::setOverride(Override* override) {
 285	m_override = override;
 286	if (isLoaded()) {
 287		Interrupter interrupter(this);
 288		m_override->identify(m_threadContext.core);
 289	}
 290}
 291
 292void GameController::clearOverride() {
 293	delete m_override;
 294	m_override = nullptr;
 295}
 296
 297void GameController::setConfig(const mCoreConfig* config) {
 298	m_config = config;
 299	if (isLoaded()) {
 300		Interrupter interrupter(this);
 301		mCoreLoadForeignConfig(m_threadContext.core, config);
 302		m_audioSync = m_threadContext.sync.audioWait;
 303		m_videoSync = m_threadContext.sync.videoFrameWait;
 304		m_audioProcessor->setInput(&m_threadContext);
 305	}
 306}
 307
 308#ifdef USE_DEBUGGERS
 309mDebugger* GameController::debugger() {
 310	if (!isLoaded()) {
 311		return nullptr;
 312	}
 313	return m_threadContext.core->debugger;
 314}
 315
 316void GameController::setDebugger(mDebugger* debugger) {
 317	Interrupter interrupter(this);
 318	if (debugger) {
 319		mDebuggerAttach(debugger, m_threadContext.core);
 320	} else {
 321		m_threadContext.core->detachDebugger(m_threadContext.core);
 322	}
 323}
 324#endif
 325
 326void GameController::loadGame(const QString& path) {
 327	closeGame();
 328	QFileInfo info(path);
 329	if (!info.isReadable()) {
 330		QString fname = info.fileName();
 331		QString base = info.path();
 332		if (base.endsWith("/") || base.endsWith(QDir::separator())) {
 333			base.chop(1);
 334		}
 335		VDir* dir = VDirOpenArchive(base.toUtf8().constData());
 336		if (dir) {
 337			VFile* vf = dir->openFile(dir, fname.toUtf8().constData(), O_RDONLY);
 338			if (vf) {
 339				struct VFile* vfclone = VFileMemChunk(NULL, vf->size(vf));
 340				uint8_t buffer[2048];
 341				ssize_t read;
 342				while ((read = vf->read(vf, buffer, sizeof(buffer))) > 0) {
 343					vfclone->write(vfclone, buffer, read);
 344				}
 345				vf->close(vf);
 346				vf = vfclone;
 347			}
 348			dir->close(dir);
 349			loadGame(vf, fname, base);
 350		} else {
 351			LOG(QT, ERROR) << tr("Failed to open game file: %1").arg(path);
 352		}
 353		return;
 354	} else {
 355		m_fname = info.canonicalFilePath();
 356		m_fsub = QString();
 357	}
 358	m_vf = nullptr;
 359	openGame();
 360}
 361
 362void GameController::loadGame(VFile* vf, const QString& path, const QString& base) {
 363	closeGame();
 364	QFileInfo info(base);
 365	if (info.isDir()) {
 366		m_fname = QFileInfo(base + '/' + path).canonicalFilePath();
 367		m_fsub = QString();
 368	} else {
 369		m_fname = info.canonicalFilePath();
 370		m_fsub = path;
 371	}
 372	m_vf = vf;
 373	openGame();
 374}
 375
 376void GameController::bootBIOS() {
 377	closeGame();
 378	m_fname = QString();
 379	openGame(true);
 380}
 381
 382void GameController::openGame(bool biosOnly) {
 383	if (m_fname.isEmpty()) {
 384		biosOnly = true;
 385	}
 386	if (isLoaded()) {
 387		// We need to delay if the game is still cleaning up
 388		QTimer::singleShot(10, this, SLOT(openGame()));
 389		return;
 390	} else if(m_gameOpen) {
 391		cleanGame();
 392	}
 393
 394	m_threadContext.core = nullptr;
 395	if (!biosOnly) {
 396		if (m_vf) {
 397			m_threadContext.core = mCoreFindVF(m_vf);
 398		} else {
 399			m_threadContext.core = mCoreFind(m_fname.toUtf8().constData());
 400		}
 401#ifdef M_CORE_GBA
 402	} else {
 403		m_threadContext.core = GBACoreCreate();
 404#endif
 405	}
 406
 407	if (!m_threadContext.core) {
 408		return;
 409	}
 410
 411	m_pauseAfterFrame = false;
 412
 413	if (m_turbo) {
 414		m_threadContext.sync.videoFrameWait = false;
 415		m_threadContext.sync.audioWait = false;
 416	} else {
 417		m_threadContext.sync.videoFrameWait = m_videoSync;
 418		m_threadContext.sync.audioWait = m_audioSync;
 419	}
 420	m_threadContext.core->init(m_threadContext.core);
 421	mCoreInitConfig(m_threadContext.core, nullptr);
 422
 423	unsigned width, height;
 424	m_threadContext.core->desiredVideoDimensions(m_threadContext.core, &width, &height);
 425	m_drawContext = new uint32_t[width * height];
 426	m_frontBuffer = new uint32_t[width * height];
 427
 428	if (m_config) {
 429		mCoreLoadForeignConfig(m_threadContext.core, m_config);
 430	}
 431
 432	QByteArray bytes;
 433	if (!biosOnly) {
 434		bytes = m_fname.toUtf8();
 435		if (m_preload) {
 436			if (m_vf) {
 437				mCorePreloadVF(m_threadContext.core, m_vf);
 438			} else {
 439				mCorePreloadFile(m_threadContext.core, bytes.constData());
 440				mDirectorySetDetachBase(&m_threadContext.core->dirs);
 441			}
 442		} else {
 443			if (m_vf) {
 444				m_threadContext.core->loadROM(m_threadContext.core, m_vf);
 445			} else {
 446				mCoreLoadFile(m_threadContext.core, bytes.constData());
 447				mDirectorySetDetachBase(&m_threadContext.core->dirs);
 448			}
 449		}
 450	} else {
 451		bytes = m_bios.toUtf8();
 452	}
 453	if (bytes.isNull()) {
 454		return;
 455	}
 456
 457	char dirname[PATH_MAX];
 458	separatePath(bytes.constData(), dirname, m_threadContext.core->dirs.baseName, 0);
 459	mDirectorySetAttachBase(&m_threadContext.core->dirs, VDirOpen(dirname));
 460
 461	m_threadContext.core->setVideoBuffer(m_threadContext.core, m_drawContext, width);
 462
 463	m_inputController->recalibrateAxes();
 464	memset(m_drawContext, 0xF8, width * height * 4);
 465
 466	m_threadContext.core->setAVStream(m_threadContext.core, m_stream);
 467
 468	if (!biosOnly) {
 469		mCoreAutoloadSave(m_threadContext.core);
 470		if (!m_patch.isNull()) {
 471			VFile* patch = VFileDevice::open(m_patch, O_RDONLY);
 472			if (patch) {
 473				m_threadContext.core->loadPatch(m_threadContext.core, patch);
 474			}
 475			patch->close(patch);
 476		} else {
 477			mCoreAutoloadPatch(m_threadContext.core);
 478		}
 479	}
 480	m_vf = nullptr;
 481
 482	if (!mCoreThreadStart(&m_threadContext)) {
 483		emit gameFailed();
 484	}
 485}
 486
 487void GameController::loadBIOS(int platform, const QString& path) {
 488	if (m_bios == path) {
 489		return;
 490	}
 491	if (!m_bios.isNull() && m_gameOpen && this->platform() == platform) {
 492		closeGame();
 493		m_bios = path;
 494		openGame();
 495	} else if (!m_gameOpen || m_bios.isNull()) {
 496		m_bios = path;
 497	}
 498}
 499
 500void GameController::loadSave(const QString& path, bool temporary) {
 501	if (!isLoaded()) {
 502		return;
 503	}
 504	m_resetActions.append([this, path, temporary]() {
 505		VFile* vf = VFileDevice::open(path, temporary ? O_RDONLY : O_RDWR);
 506		if (!vf) {
 507			LOG(QT, ERROR) << tr("Failed to open save file: %1").arg(path);
 508			return;
 509		}
 510
 511		if (temporary) {
 512			m_threadContext.core->loadTemporarySave(m_threadContext.core, vf);
 513		} else {
 514			m_threadContext.core->loadSave(m_threadContext.core, vf);
 515		}
 516	});
 517	reset();
 518}
 519
 520void GameController::yankPak() {
 521	if (!m_gameOpen) {
 522		return;
 523	}
 524	Interrupter interrupter(this);
 525	GBAYankROM(static_cast<GBA*>(m_threadContext.core->board));
 526}
 527
 528void GameController::replaceGame(const QString& path) {
 529	if (!m_gameOpen) {
 530		return;
 531	}
 532
 533	QFileInfo info(path);
 534	if (!info.isReadable()) {
 535		LOG(QT, ERROR) << tr("Failed to open game file: %1").arg(path);
 536		return;
 537	}
 538	m_fname = info.canonicalFilePath();
 539	Interrupter interrupter(this);
 540	mDirectorySetDetachBase(&m_threadContext.core->dirs);
 541	mCoreLoadFile(m_threadContext.core, m_fname.toLocal8Bit().constData());
 542}
 543
 544void GameController::loadPatch(const QString& path) {
 545	if (m_gameOpen) {
 546		closeGame();
 547		m_patch = path;
 548		openGame();
 549	} else {
 550		m_patch = path;
 551	}
 552}
 553
 554void GameController::importSharkport(const QString& path) {
 555	if (!isLoaded()) {
 556		return;
 557	}
 558#ifdef M_CORE_GBA
 559	if (platform() != PLATFORM_GBA) {
 560		return;
 561	}
 562	VFile* vf = VFileDevice::open(path, O_RDONLY);
 563	if (!vf) {
 564		LOG(QT, ERROR) << tr("Failed to open snapshot file for reading: %1").arg(path);
 565		return;
 566	}
 567	threadInterrupt();
 568	GBASavedataImportSharkPort(static_cast<GBA*>(m_threadContext.core->board), vf, false);
 569	threadContinue();
 570	vf->close(vf);
 571#endif
 572}
 573
 574void GameController::exportSharkport(const QString& path) {
 575	if (!isLoaded()) {
 576		return;
 577	}
 578#ifdef M_CORE_GBA
 579	if (platform() != PLATFORM_GBA) {
 580		return;
 581	}
 582	VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
 583	if (!vf) {
 584		LOG(QT, ERROR) << tr("Failed to open snapshot file for writing: %1").arg(path);
 585		return;
 586	}
 587	threadInterrupt();
 588	GBASavedataExportSharkPort(static_cast<GBA*>(m_threadContext.core->board), vf);
 589	threadContinue();
 590	vf->close(vf);
 591#endif
 592}
 593
 594void GameController::closeGame() {
 595	if (!m_gameOpen) {
 596		return;
 597	}
 598#ifdef USE_DEBUGGERS
 599	setDebugger(nullptr);
 600#endif
 601	if (mCoreThreadIsPaused(&m_threadContext)) {
 602		mCoreThreadUnpause(&m_threadContext);
 603	}
 604	mCoreThreadEnd(&m_threadContext);
 605}
 606
 607void GameController::cleanGame() {
 608	if (!m_gameOpen || mCoreThreadIsActive(&m_threadContext)) {
 609		return;
 610	}
 611
 612	m_audioProcessor->pause();
 613	mCoreThreadJoin(&m_threadContext);
 614
 615	if (m_tileCache) {
 616		mTileCacheDeinit(m_tileCache.get());
 617		m_tileCache.reset();
 618	}
 619
 620	delete[] m_drawContext;
 621	delete[] m_frontBuffer;
 622
 623	mCoreConfigDeinit(&m_threadContext.core->config);
 624	m_threadContext.core->deinit(m_threadContext.core);
 625	m_threadContext.core = nullptr;
 626	m_gameOpen = false;
 627}
 628
 629void GameController::crashGame(const QString& crashMessage) {
 630	closeGame();
 631	emit gameCrashed(crashMessage);
 632}
 633
 634bool GameController::isPaused() {
 635	if (!m_gameOpen) {
 636		return false;
 637	}
 638	return mCoreThreadIsPaused(&m_threadContext);
 639}
 640
 641mPlatform GameController::platform() const {
 642	if (!m_gameOpen) {
 643		return PLATFORM_NONE;
 644	}
 645	return m_threadContext.core->platform(m_threadContext.core);
 646}
 647
 648QSize GameController::screenDimensions() const {
 649	if (!m_gameOpen) {
 650		return QSize();
 651	}
 652	unsigned width, height;
 653	m_threadContext.core->desiredVideoDimensions(m_threadContext.core, &width, &height);
 654
 655	return QSize(width, height);
 656}
 657
 658void GameController::setPaused(bool paused) {
 659	if (!isLoaded() || paused == mCoreThreadIsPaused(&m_threadContext)) {
 660		return;
 661	}
 662	m_wasPaused = paused;
 663	if (paused) {
 664		m_pauseAfterFrame.testAndSetRelaxed(false, true);
 665	} else {
 666		mCoreThreadUnpause(&m_threadContext);
 667		startAudio();
 668		emit gameUnpaused(&m_threadContext);
 669	}
 670}
 671
 672void GameController::reset() {
 673	if (!m_gameOpen) {
 674		return;
 675	}
 676	bool wasPaused = isPaused();
 677	setPaused(false);
 678	Interrupter interrupter(this);
 679	mCoreThreadReset(&m_threadContext);
 680	if (wasPaused) {
 681		setPaused(true);
 682	}
 683}
 684
 685void GameController::threadInterrupt() {
 686	if (m_gameOpen) {
 687		mCoreThreadInterrupt(&m_threadContext);
 688	}
 689}
 690
 691void GameController::threadContinue() {
 692	if (m_gameOpen) {
 693		mCoreThreadContinue(&m_threadContext);
 694	}
 695}
 696
 697void GameController::frameAdvance() {
 698	if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
 699		setPaused(false);
 700		m_wasPaused = true;
 701	}
 702}
 703
 704void GameController::setRewind(bool enable, int capacity, bool rewindSave) {
 705	if (m_gameOpen) {
 706		Interrupter interrupter(this);
 707		if (m_threadContext.core->opts.rewindEnable && m_threadContext.core->opts.rewindBufferCapacity > 0) {
 708			mCoreRewindContextDeinit(&m_threadContext.rewind);
 709		}
 710		m_threadContext.core->opts.rewindEnable = enable;
 711		m_threadContext.core->opts.rewindBufferCapacity = capacity;
 712		m_threadContext.core->opts.rewindSave = rewindSave;
 713		if (enable && capacity > 0) {
 714			mCoreRewindContextInit(&m_threadContext.rewind, capacity, true);
 715			m_threadContext.rewind.stateFlags = rewindSave ? SAVESTATE_SAVEDATA : 0;
 716		}
 717	}
 718}
 719
 720void GameController::rewind(int states) {
 721	threadInterrupt();
 722	if (!states) {
 723		states = INT_MAX;
 724	}
 725	for (int i = 0; i < states; ++i) {
 726		if (!mCoreRewindRestore(&m_threadContext.rewind, m_threadContext.core)) {
 727			break;
 728		}
 729	}
 730	threadContinue();
 731	emit frameAvailable(m_drawContext);
 732	emit rewound(&m_threadContext);
 733}
 734
 735void GameController::startRewinding() {
 736	if (!isLoaded()) {
 737		return;
 738	}
 739	if (!m_threadContext.core->opts.rewindEnable) {
 740		return;
 741	}
 742	if (m_multiplayer && m_multiplayer->attached() > 1) {
 743		return;
 744	}
 745	if (m_wasPaused) {
 746		setPaused(false);
 747		m_wasPaused = true;
 748	}
 749	mCoreThreadSetRewinding(&m_threadContext, true);
 750}
 751
 752void GameController::stopRewinding() {
 753	if (!isLoaded()) {
 754		return;
 755	}
 756	mCoreThreadSetRewinding(&m_threadContext, false);
 757	bool signalsBlocked = blockSignals(true);
 758	setPaused(m_wasPaused);
 759	blockSignals(signalsBlocked);
 760}
 761
 762void GameController::keyPressed(int key) {
 763	int mappedKey = 1 << key;
 764	m_activeKeys |= mappedKey;
 765	if (!m_inputController->allowOpposing()) {
 766		if ((m_activeKeys & 0x30) == 0x30) {
 767			m_inactiveKeys |= mappedKey ^ 0x30;
 768			m_activeKeys ^= mappedKey ^ 0x30;
 769		}
 770		if ((m_activeKeys & 0xC0) == 0xC0) {
 771			m_inactiveKeys |= mappedKey ^ 0xC0;
 772			m_activeKeys ^= mappedKey ^ 0xC0;
 773		}
 774	}
 775	updateKeys();
 776}
 777
 778void GameController::keyReleased(int key) {
 779	int mappedKey = 1 << key;
 780	m_activeKeys &= ~mappedKey;
 781	if (!m_inputController->allowOpposing()) {
 782		if (mappedKey & 0x30) {
 783			m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
 784			m_inactiveKeys &= ~0x30;
 785		}
 786		if (mappedKey & 0xC0) {
 787			m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
 788			m_inactiveKeys &= ~0xC0;
 789		}
 790	}
 791	updateKeys();
 792}
 793
 794void GameController::clearKeys() {
 795	m_activeKeys = 0;
 796	m_inactiveKeys = 0;
 797	updateKeys();
 798}
 799
 800void GameController::setAutofire(int key, bool enable) {
 801	if (key >= GBA_KEY_MAX || key < 0) {
 802		return;
 803	}
 804
 805	if (!enable && m_autofireStatus[key]) {
 806		keyReleased(key);
 807	}
 808
 809	m_autofire[key] = enable;
 810	m_autofireStatus[key] = 0;
 811}
 812
 813void GameController::setAudioBufferSamples(int samples) {
 814	if (m_audioProcessor) {
 815		threadInterrupt();
 816		redoSamples(samples);
 817		threadContinue();
 818		m_audioProcessor->setBufferSamples(samples);
 819	}
 820}
 821
 822void GameController::setAudioSampleRate(unsigned rate) {
 823	if (!rate) {
 824		return;
 825	}
 826	if (m_audioProcessor) {
 827		threadInterrupt();
 828		redoSamples(m_audioProcessor->getBufferSamples());
 829		threadContinue();
 830		m_audioProcessor->requestSampleRate(rate);
 831	}
 832}
 833
 834void GameController::setAudioChannelEnabled(int channel, bool enable) {
 835	if (channel > 5 || channel < 0) {
 836		return;
 837	}
 838	m_audioChannels.reserve(channel + 1);
 839	while (m_audioChannels.size() <= channel) {
 840		m_audioChannels.append(true);
 841	}
 842	m_audioChannels[channel] = enable;
 843	if (isLoaded()) {
 844		m_threadContext.core->enableAudioChannel(m_threadContext.core, channel, enable);
 845	}
 846}
 847
 848void GameController::startAudio() {
 849	if (!m_audioProcessor->start()) {
 850		LOG(QT, ERROR) << tr("Failed to start audio processor");
 851		// Don't freeze!
 852		m_audioSync = false;
 853		m_videoSync = true;
 854		m_threadContext.sync.audioWait = false;
 855		m_threadContext.sync.videoFrameWait = true;
 856	}
 857}
 858
 859void GameController::setVideoLayerEnabled(int layer, bool enable) {
 860	if (layer > 4 || layer < 0) {
 861		return;
 862	}
 863	m_videoLayers.reserve(layer + 1);
 864	while (m_videoLayers.size() <= layer) {
 865		m_videoLayers.append(true);
 866	}
 867	m_videoLayers[layer] = enable;
 868	if (isLoaded()) {
 869		m_threadContext.core->enableVideoLayer(m_threadContext.core, layer, enable);
 870	}
 871}
 872
 873void GameController::setFPSTarget(float fps) {
 874	Interrupter interrupter(this);
 875	m_fpsTarget = fps;
 876	m_threadContext.sync.fpsTarget = fps;
 877	if (m_turbo && m_turboSpeed > 0) {
 878		m_threadContext.sync.fpsTarget *= m_turboSpeed;
 879	}
 880	if (m_audioProcessor) {
 881		redoSamples(m_audioProcessor->getBufferSamples());
 882	}
 883}
 884
 885void GameController::setUseBIOS(bool use) {
 886	if (use == m_useBios) {
 887		return;
 888	}
 889	m_useBios = use;
 890	if (m_gameOpen) {
 891		closeGame();
 892		openGame();
 893	}
 894}
 895
 896void GameController::loadState(int slot) {
 897	if (m_fname.isEmpty()) {
 898		// We're in the BIOS
 899		return;
 900	}
 901	if (slot > 0 && slot != m_stateSlot) {
 902		m_stateSlot = slot;
 903		m_backupSaveState.clear();
 904	}
 905	mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
 906		GameController* controller = static_cast<GameController*>(context->userData);
 907		if (!controller->m_backupLoadState) {
 908			controller->m_backupLoadState = VFileMemChunk(nullptr, 0);
 909		}
 910		mCoreLoadStateNamed(context->core, controller->m_backupLoadState, controller->m_saveStateFlags);
 911		if (mCoreLoadState(context->core, controller->m_stateSlot, controller->m_loadStateFlags)) {
 912			emit controller->frameAvailable(controller->m_drawContext);
 913			emit controller->stateLoaded(context);
 914		}
 915	});
 916}
 917
 918void GameController::saveState(int slot) {
 919	if (m_fname.isEmpty()) {
 920		// We're in the BIOS
 921		return;
 922	}
 923	if (slot > 0) {
 924		m_stateSlot = slot;
 925	}
 926	mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
 927		GameController* controller = static_cast<GameController*>(context->userData);
 928		VFile* vf = mCoreGetState(context->core, controller->m_stateSlot, false);
 929		if (vf) {
 930			controller->m_backupSaveState.resize(vf->size(vf));
 931			vf->read(vf, controller->m_backupSaveState.data(), controller->m_backupSaveState.size());
 932			vf->close(vf);
 933		}
 934		mCoreSaveState(context->core, controller->m_stateSlot, controller->m_saveStateFlags);
 935	});
 936}
 937
 938void GameController::loadBackupState() {
 939	if (!m_backupLoadState) {
 940		return;
 941	}
 942
 943	mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
 944		GameController* controller = static_cast<GameController*>(context->userData);
 945		controller->m_backupLoadState->seek(controller->m_backupLoadState, 0, SEEK_SET);
 946		if (mCoreLoadStateNamed(context->core, controller->m_backupLoadState, controller->m_loadStateFlags)) {
 947			mLOG(STATUS, INFO, "Undid state load");
 948			controller->frameAvailable(controller->m_drawContext);
 949			controller->stateLoaded(context);
 950		}
 951		controller->m_backupLoadState->close(controller->m_backupLoadState);
 952		controller->m_backupLoadState = nullptr;
 953	});
 954}
 955
 956void GameController::saveBackupState() {
 957	if (m_backupSaveState.isEmpty()) {
 958		return;
 959	}
 960
 961	mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
 962		GameController* controller = static_cast<GameController*>(context->userData);
 963		VFile* vf = mCoreGetState(context->core, controller->m_stateSlot, true);
 964		if (vf) {
 965			vf->write(vf, controller->m_backupSaveState.constData(), controller->m_backupSaveState.size());
 966			vf->close(vf);
 967			mLOG(STATUS, INFO, "Undid state save");
 968		}
 969		controller->m_backupSaveState.clear();
 970	});
 971}
 972
 973void GameController::setTurbo(bool set, bool forced) {
 974	if (m_turboForced && !forced) {
 975		return;
 976	}
 977	if (m_turbo == set && m_turboForced == (set && forced)) {
 978		// Don't interrupt the thread if we don't need to
 979		return;
 980	}
 981	if (!m_sync) {
 982		return;
 983	}
 984	m_turbo = set;
 985	m_turboForced = set && forced;
 986	enableTurbo();
 987}
 988
 989void GameController::setTurboSpeed(float ratio) {
 990	m_turboSpeed = ratio;
 991	enableTurbo();
 992}
 993
 994void GameController::enableTurbo() {
 995	Interrupter interrupter(this);
 996	bool shouldRedoSamples = false;
 997	if (!m_turbo) {
 998		shouldRedoSamples = m_threadContext.sync.fpsTarget != m_fpsTarget;
 999		m_threadContext.sync.fpsTarget = m_fpsTarget;
1000		m_threadContext.sync.audioWait = m_audioSync;
1001		m_threadContext.sync.videoFrameWait = m_videoSync;
1002	} else if (m_turboSpeed <= 0) {
1003		shouldRedoSamples = m_threadContext.sync.fpsTarget != m_fpsTarget;
1004		m_threadContext.sync.fpsTarget = m_fpsTarget;
1005		m_threadContext.sync.audioWait = false;
1006		m_threadContext.sync.videoFrameWait = false;
1007	} else {
1008		shouldRedoSamples = m_threadContext.sync.fpsTarget != m_fpsTarget * m_turboSpeed;
1009		m_threadContext.sync.fpsTarget = m_fpsTarget * m_turboSpeed;
1010		m_threadContext.sync.audioWait = true;
1011		m_threadContext.sync.videoFrameWait = false;
1012	}
1013	if (m_audioProcessor && shouldRedoSamples) {
1014		redoSamples(m_audioProcessor->getBufferSamples());
1015	}
1016}
1017
1018void GameController::setSync(bool enable) {
1019	m_turbo = false;
1020	m_turboForced = false;
1021	if (!enable) {
1022		m_threadContext.sync.audioWait = false;
1023		m_threadContext.sync.videoFrameWait = false;
1024	} else {
1025		m_threadContext.sync.audioWait = m_audioSync;
1026		m_threadContext.sync.videoFrameWait = m_videoSync;
1027	}
1028	m_sync = enable;
1029}
1030
1031void GameController::setAudioSync(bool enable) {
1032	m_audioSync = enable;
1033	m_threadContext.sync.audioWait = enable;
1034}
1035
1036void GameController::setVideoSync(bool enable) {
1037	m_videoSync = enable;
1038	m_threadContext.sync.videoFrameWait = enable;
1039}
1040
1041void GameController::setAVStream(mAVStream* stream) {
1042	Interrupter interrupter(this);
1043	m_stream = stream;
1044	if (isLoaded()) {
1045		m_threadContext.core->setAVStream(m_threadContext.core, stream);
1046	}
1047}
1048
1049void GameController::clearAVStream() {
1050	Interrupter interrupter(this);
1051	m_stream = nullptr;
1052	if (isLoaded()) {
1053		m_threadContext.core->setAVStream(m_threadContext.core, nullptr);
1054	}
1055}
1056
1057#ifdef USE_PNG
1058void GameController::screenshot() {
1059	mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
1060		mCoreTakeScreenshot(context->core);
1061	});
1062}
1063#endif
1064
1065void GameController::reloadAudioDriver() {
1066	int samples = 0;
1067	unsigned sampleRate = 0;
1068	if (m_audioProcessor) {
1069		m_audioProcessor->pause();
1070		samples = m_audioProcessor->getBufferSamples();
1071		sampleRate = m_audioProcessor->sampleRate();
1072		delete m_audioProcessor;
1073	}
1074	m_audioProcessor = AudioProcessor::create();
1075	if (samples) {
1076		m_audioProcessor->setBufferSamples(samples);
1077	}
1078	if (sampleRate) {
1079		m_audioProcessor->requestSampleRate(sampleRate);
1080	}
1081	connect(this, &GameController::gamePaused, m_audioProcessor, &AudioProcessor::pause);
1082	connect(this, &GameController::gameStarted, m_audioProcessor, &AudioProcessor::setInput);
1083	if (isLoaded()) {
1084		m_audioProcessor->setInput(&m_threadContext);
1085		startAudio();
1086	}
1087}
1088
1089void GameController::setSaveStateExtdata(int flags) {
1090	m_saveStateFlags = flags;
1091}
1092
1093void GameController::setLoadStateExtdata(int flags) {
1094	m_loadStateFlags = flags;
1095}
1096
1097void GameController::setPreload(bool preload) {
1098	m_preload = preload;
1099}
1100
1101void GameController::setLuminanceValue(uint8_t value) {
1102	m_luxValue = value;
1103	value = std::max<int>(value - 0x16, 0);
1104	m_luxLevel = 10;
1105	for (int i = 0; i < 10; ++i) {
1106		if (value < GBA_LUX_LEVELS[i]) {
1107			m_luxLevel = i;
1108			break;
1109		}
1110	}
1111	emit luminanceValueChanged(m_luxValue);
1112}
1113
1114void GameController::setLuminanceLevel(int level) {
1115	int value = 0x16;
1116	level = std::max(0, std::min(10, level));
1117	if (level > 0) {
1118		value += GBA_LUX_LEVELS[level - 1];
1119	}
1120	setLuminanceValue(value);
1121}
1122
1123void GameController::setRealTime() {
1124	if (!isLoaded()) {
1125		return;
1126	}
1127	m_threadContext.core->rtc.override = RTC_NO_OVERRIDE;
1128}
1129
1130void GameController::setFixedTime(const QDateTime& time) {
1131	if (!isLoaded()) {
1132		return;
1133	}
1134	m_threadContext.core->rtc.override = RTC_FIXED;
1135	m_threadContext.core->rtc.value = time.toMSecsSinceEpoch();
1136}
1137
1138void GameController::setFakeEpoch(const QDateTime& time) {
1139	if (!isLoaded()) {
1140		return;
1141	}
1142	m_threadContext.core->rtc.override = RTC_FAKE_EPOCH;
1143	m_threadContext.core->rtc.value = time.toMSecsSinceEpoch();
1144}
1145
1146void GameController::updateKeys() {
1147	int activeKeys = m_activeKeys;
1148	activeKeys |= m_activeButtons;
1149	activeKeys &= ~m_inactiveKeys;
1150	if (isLoaded()) {
1151		m_threadContext.core->setKeys(m_threadContext.core, activeKeys);
1152	}
1153}
1154
1155void GameController::redoSamples(int samples) {
1156	if (m_gameOpen && m_threadContext.core) {
1157		m_threadContext.core->setAudioBufferSize(m_threadContext.core, samples);
1158	}
1159	m_audioProcessor->inputParametersChanged();
1160}
1161
1162void GameController::setLogLevel(int levels) {
1163	Interrupter interrupter(this);
1164	m_logLevels = levels;
1165}
1166
1167void GameController::enableLogLevel(int levels) {
1168	Interrupter interrupter(this);
1169	m_logLevels |= levels;
1170}
1171
1172void GameController::disableLogLevel(int levels) {
1173	Interrupter interrupter(this);
1174	m_logLevels &= ~levels;
1175}
1176
1177void GameController::startVideoLog(const QString& path) {
1178	if (!isLoaded() || m_vl) {
1179		return;
1180	}
1181
1182	Interrupter interrupter(this);
1183	m_vl = mVideoLogContextCreate(m_threadContext.core);
1184	m_vlVf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
1185	mVideoLogContextSetOutput(m_vl, m_vlVf);
1186	mVideoLogContextWriteHeader(m_vl, m_threadContext.core);
1187}
1188
1189void GameController::endVideoLog() {
1190	if (!m_vl) {
1191		return;
1192	}
1193
1194	Interrupter interrupter(this);
1195	mVideoLogContextDestroy(m_threadContext.core, m_vl);
1196	if (m_vlVf) {
1197		m_vlVf->close(m_vlVf);
1198		m_vlVf = nullptr;
1199	}
1200	m_vl = nullptr;
1201}
1202
1203void GameController::pollEvents() {
1204	if (!m_inputController) {
1205		return;
1206	}
1207
1208	m_activeButtons = m_inputController->pollEvents();
1209	updateKeys();
1210}
1211
1212void GameController::updateAutofire() {
1213	// TODO: Move all key events onto the CPU thread...somehow
1214	for (int k = 0; k < GBA_KEY_MAX; ++k) {
1215		if (!m_autofire[k]) {
1216			continue;
1217		}
1218		m_autofireStatus[k] ^= 1;
1219		if (m_autofireStatus[k]) {
1220			keyPressed(k);
1221		} else {
1222			keyReleased(k);
1223		}
1224	}
1225}
1226
1227std::shared_ptr<mTileCache> GameController::tileCache() {
1228	if (m_tileCache) {
1229		return m_tileCache;
1230	}
1231	Interrupter interrupter(this);
1232	switch (platform()) {
1233#ifdef M_CORE_GBA
1234	case PLATFORM_GBA: {
1235		GBA* gba = static_cast<GBA*>(m_threadContext.core->board);
1236		m_tileCache = std::make_shared<mTileCache>();
1237		GBAVideoTileCacheInit(m_tileCache.get());
1238		GBAVideoTileCacheAssociate(m_tileCache.get(), &gba->video);
1239		mTileCacheSetPalette(m_tileCache.get(), 0);
1240		break;
1241	}
1242#endif
1243#ifdef M_CORE_GB
1244	case PLATFORM_GB: {
1245		GB* gb = static_cast<GB*>(m_threadContext.core->board);
1246		m_tileCache = std::make_shared<mTileCache>();
1247		GBVideoTileCacheInit(m_tileCache.get());
1248		GBVideoTileCacheAssociate(m_tileCache.get(), &gb->video);
1249		mTileCacheSetPalette(m_tileCache.get(), 0);
1250		break;
1251	}
1252#endif
1253	default:
1254		return nullptr;
1255	}
1256	return m_tileCache;
1257}
1258
1259GameController::Interrupter::Interrupter(GameController* parent, bool fromThread)
1260	: m_parent(parent)
1261	, m_fromThread(fromThread)
1262{
1263	if (!m_fromThread) {
1264		m_parent->threadInterrupt();
1265	} else {
1266		mCoreThreadInterruptFromThread(m_parent->thread());
1267	}
1268}
1269
1270GameController::Interrupter::~Interrupter() {
1271	if (!m_fromThread) {
1272		m_parent->threadContinue();
1273	} else {
1274		mCoreThreadContinue(m_parent->thread());
1275	}
1276}