all repos — mgba @ fc905657adeba406a2c280bbb94b09533a89e3f8

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#include <QThread>
  17
  18#include <ctime>
  19
  20extern "C" {
  21#include "core/config.h"
  22#include "core/directories.h"
  23#include "gba/audio.h"
  24#include "gba/gba.h"
  25#include "gba/serialize.h"
  26#include "gba/sharkport.h"
  27#include "gba/renderers/video-software.h"
  28#include "util/vfs.h"
  29}
  30
  31using namespace QGBA;
  32using namespace std;
  33
  34GameController::GameController(QObject* parent)
  35	: QObject(parent)
  36	, m_drawContext(new uint32_t[VIDEO_VERTICAL_PIXELS * VIDEO_HORIZONTAL_PIXELS])
  37	, m_frontBuffer(new uint32_t[VIDEO_VERTICAL_PIXELS * VIDEO_HORIZONTAL_PIXELS])
  38	, m_threadContext()
  39	, m_activeKeys(0)
  40	, m_inactiveKeys(0)
  41	, m_logLevels(0)
  42	, m_gameOpen(false)
  43	, m_audioThread(new QThread(this))
  44	, m_audioProcessor(AudioProcessor::create())
  45	, m_pauseAfterFrame(false)
  46	, m_videoSync(VIDEO_SYNC)
  47	, m_audioSync(AUDIO_SYNC)
  48	, m_fpsTarget(-1)
  49	, m_turbo(false)
  50	, m_turboForced(false)
  51	, m_turboSpeed(-1)
  52	, m_wasPaused(false)
  53	, m_audioChannels{ true, true, true, true, true, true }
  54	, m_videoLayers{ true, true, true, true, true }
  55	, m_autofire{}
  56	, m_autofireStatus{}
  57	, m_inputController(nullptr)
  58	, m_multiplayer(nullptr)
  59	, m_stateSlot(1)
  60	, m_backupLoadState(nullptr)
  61	, m_backupSaveState(nullptr)
  62	, m_saveStateFlags(SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS)
  63	, m_loadStateFlags(SAVESTATE_SCREENSHOT)
  64{
  65	m_renderer = new GBAVideoSoftwareRenderer;
  66	GBAVideoSoftwareRendererCreate(m_renderer);
  67	m_renderer->outputBuffer = (color_t*) m_drawContext;
  68	m_renderer->outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
  69
  70	GBACheatDeviceCreate(&m_cheatDevice);
  71
  72	m_threadContext.state = THREAD_INITIALIZED;
  73	m_threadContext.debugger = 0;
  74	m_threadContext.frameskip = 0;
  75	m_threadContext.bios = 0;
  76	m_threadContext.renderer = &m_renderer->d;
  77	m_threadContext.userData = this;
  78	m_threadContext.rewindBufferCapacity = 0;
  79	m_threadContext.cheats = &m_cheatDevice;
  80	m_threadContext.logLevel = GBA_LOG_ALL;
  81	mDirectorySetInit(&m_threadContext.dirs);
  82
  83	m_lux.p = this;
  84	m_lux.sample = [](GBALuminanceSource* context) {
  85		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
  86		lux->value = 0xFF - lux->p->m_luxValue;
  87	};
  88
  89	m_lux.readLuminance = [](GBALuminanceSource* context) {
  90		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
  91		return lux->value;
  92	};
  93	setLuminanceLevel(0);
  94
  95	m_threadContext.startCallback = [](GBAThread* context) {
  96		GameController* controller = static_cast<GameController*>(context->userData);
  97		if (controller->m_audioProcessor) {
  98			controller->m_audioProcessor->setInput(context);
  99		}
 100		context->gba->luminanceSource = &controller->m_lux;
 101		GBARTCGenericSourceInit(&controller->m_rtc, context->gba);
 102		context->gba->rtcSource = &controller->m_rtc.d;
 103		context->gba->rumble = controller->m_inputController->rumble();
 104		context->gba->rotationSource = controller->m_inputController->rotationSource();
 105		context->gba->audio.psg.forceDisableCh[0] = !controller->m_audioChannels[0];
 106		context->gba->audio.psg.forceDisableCh[1] = !controller->m_audioChannels[1];
 107		context->gba->audio.psg.forceDisableCh[2] = !controller->m_audioChannels[2];
 108		context->gba->audio.psg.forceDisableCh[3] = !controller->m_audioChannels[3];
 109		context->gba->audio.forceDisableChA = !controller->m_audioChannels[4];
 110		context->gba->audio.forceDisableChB = !controller->m_audioChannels[5];
 111		context->gba->video.renderer->disableBG[0] = !controller->m_videoLayers[0];
 112		context->gba->video.renderer->disableBG[1] = !controller->m_videoLayers[1];
 113		context->gba->video.renderer->disableBG[2] = !controller->m_videoLayers[2];
 114		context->gba->video.renderer->disableBG[3] = !controller->m_videoLayers[3];
 115		context->gba->video.renderer->disableOBJ = !controller->m_videoLayers[4];
 116		controller->m_fpsTarget = context->fpsTarget;
 117
 118		if (context->dirs.state && GBALoadState(context, context->dirs.state, 0, controller->m_loadStateFlags)) {
 119			GBADeleteState(context->gba, context->dirs.state, 0);
 120		}
 121		QMetaObject::invokeMethod(controller, "gameStarted", Q_ARG(GBAThread*, context));
 122	};
 123
 124	m_threadContext.cleanCallback = [](GBAThread* context) {
 125		GameController* controller = static_cast<GameController*>(context->userData);
 126		QMetaObject::invokeMethod(controller, "gameStopped", Q_ARG(GBAThread*, context));
 127	};
 128
 129	m_threadContext.frameCallback = [](GBAThread* context) {
 130		GameController* controller = static_cast<GameController*>(context->userData);
 131		memcpy(controller->m_frontBuffer, controller->m_drawContext, VIDEO_VERTICAL_PIXELS * VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
 132		QMetaObject::invokeMethod(controller, "frameAvailable", Q_ARG(const uint32_t*, controller->m_frontBuffer));
 133		if (controller->m_pauseAfterFrame.testAndSetAcquire(true, false)) {
 134			GBAThreadPauseFromThread(context);
 135			QMetaObject::invokeMethod(controller, "gamePaused", Q_ARG(GBAThread*, context));
 136		}
 137	};
 138
 139	m_threadContext.stopCallback = [](GBAThread* context) {
 140		if (!context) {
 141			return false;
 142		}
 143		GameController* controller = static_cast<GameController*>(context->userData);
 144		if (!GBASaveState(context, context->dirs.state, 0, controller->m_saveStateFlags)) {
 145			return false;
 146		}
 147		QMetaObject::invokeMethod(controller, "closeGame");
 148		return true;
 149	};
 150
 151	m_threadContext.logHandler = [](GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
 152		static const char* stubMessage = "Stub software interrupt: %02X";
 153		static const char* savestateMessage = "State %i loaded";
 154		static const char* savestateFailedMessage = "State %i failed to load";
 155		if (!context) {
 156			return;
 157		}
 158		GameController* controller = static_cast<GameController*>(context->userData);
 159		if (level == GBA_LOG_STUB && strncmp(stubMessage, format, strlen(stubMessage)) == 0) {
 160			va_list argc;
 161			va_copy(argc, args);
 162			int immediate = va_arg(argc, int);
 163			va_end(argc);
 164			QMetaObject::invokeMethod(controller, "unimplementedBiosCall", Q_ARG(int, immediate));
 165		} else if (level == GBA_LOG_STATUS) {
 166			// Slot 0 is reserved for suspend points
 167			if (strncmp(savestateMessage, format, strlen(savestateMessage)) == 0) {
 168				va_list argc;
 169				va_copy(argc, args);
 170				int slot = va_arg(argc, int);
 171				va_end(argc);
 172				if (slot == 0) {
 173					format = "Loaded suspend state";
 174				}
 175			} else if (strncmp(savestateFailedMessage, format, strlen(savestateFailedMessage)) == 0) {
 176				va_list argc;
 177				va_copy(argc, args);
 178				int slot = va_arg(argc, int);
 179				va_end(argc);
 180				if (slot == 0) {
 181					return;
 182				}
 183			}
 184		}
 185		if (level == GBA_LOG_FATAL) {
 186			QMetaObject::invokeMethod(controller, "crashGame", Q_ARG(const QString&, QString().vsprintf(format, args)));
 187		} else if (!(controller->m_logLevels & level)) {
 188			return;
 189		}
 190		QString message(QString().vsprintf(format, args));
 191		if (level == GBA_LOG_STATUS) {
 192			QMetaObject::invokeMethod(controller, "statusPosted", Q_ARG(const QString&, message));
 193		}
 194		QMetaObject::invokeMethod(controller, "postLog", Q_ARG(int, level), Q_ARG(const QString&, message));
 195	};
 196
 197	connect(&m_rewindTimer, &QTimer::timeout, [this]() {
 198		GBARewind(&m_threadContext, 1);
 199		emit frameAvailable(m_drawContext);
 200		emit rewound(&m_threadContext);
 201	});
 202	m_rewindTimer.setInterval(100);
 203
 204	m_audioThread->setObjectName("Audio Thread");
 205	m_audioThread->start(QThread::TimeCriticalPriority);
 206	m_audioProcessor->moveToThread(m_audioThread);
 207	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
 208	connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(pollEvents()));
 209	connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updateAutofire()));
 210}
 211
 212GameController::~GameController() {
 213	m_audioThread->quit();
 214	m_audioThread->wait();
 215	disconnect();
 216	clearMultiplayerController();
 217	closeGame();
 218	GBACheatDeviceDestroy(&m_cheatDevice);
 219	mDirectorySetDeinit(&m_threadContext.dirs);
 220	delete m_renderer;
 221	delete[] m_drawContext;
 222	delete[] m_frontBuffer;
 223	delete m_backupLoadState;
 224}
 225
 226void GameController::setMultiplayerController(MultiplayerController* controller) {
 227	if (controller == m_multiplayer) {
 228		return;
 229	}
 230	clearMultiplayerController();
 231	m_multiplayer = controller;
 232	controller->attachGame(this);
 233}
 234
 235void GameController::clearMultiplayerController() {
 236	if (!m_multiplayer) {
 237		return;
 238	}
 239	m_multiplayer->detachGame(this);
 240	m_multiplayer = nullptr;
 241}
 242
 243void GameController::setOverride(const GBACartridgeOverride& override) {
 244	m_threadContext.override = override;
 245	m_threadContext.hasOverride = true;
 246}
 247
 248void GameController::setOptions(const mCoreOptions* opts) {
 249	setFrameskip(opts->frameskip);
 250	setAudioSync(opts->audioSync);
 251	setVideoSync(opts->videoSync);
 252	setSkipBIOS(opts->skipBios);
 253	setUseBIOS(opts->useBios);
 254	setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
 255	setVolume(opts->volume);
 256	setMute(opts->mute);
 257
 258	threadInterrupt();
 259	mDirectorySetMapOptions(&m_threadContext.dirs, opts);
 260	// TODO: Put back idle optimization
 261	threadContinue();
 262}
 263
 264#ifdef USE_GDB_STUB
 265Debugger* GameController::debugger() {
 266	return m_threadContext.debugger;
 267}
 268
 269void GameController::setDebugger(Debugger* debugger) {
 270	threadInterrupt();
 271	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
 272		GBADetachDebugger(m_threadContext.gba);
 273	}
 274	m_threadContext.debugger = debugger;
 275	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
 276		GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
 277	}
 278	threadContinue();
 279}
 280#endif
 281
 282void GameController::loadGame(const QString& path) {
 283	closeGame();
 284	QFile file(path);
 285	if (!file.open(QIODevice::ReadOnly)) {
 286		postLog(GBA_LOG_ERROR, tr("Failed to open game file: %1").arg(path));
 287		return;
 288	}
 289	file.close();
 290
 291	m_fname = path;
 292	openGame();
 293}
 294
 295void GameController::bootBIOS() {
 296	closeGame();
 297	m_fname = QString();
 298	openGame(true);
 299}
 300
 301void GameController::openGame(bool biosOnly) {
 302	if (biosOnly && (!m_useBios || m_bios.isNull())) {
 303		return;
 304	}
 305
 306	m_gameOpen = true;
 307
 308	m_pauseAfterFrame = false;
 309
 310	if (m_turbo) {
 311		m_threadContext.sync.videoFrameWait = false;
 312		m_threadContext.sync.audioWait = false;
 313	} else {
 314		m_threadContext.sync.videoFrameWait = m_videoSync;
 315		m_threadContext.sync.audioWait = m_audioSync;
 316	}
 317
 318	m_threadContext.bootBios = biosOnly;
 319	if (biosOnly) {
 320		m_threadContext.fname = nullptr;
 321	} else {
 322		m_threadContext.fname = strdup(m_fname.toUtf8().constData());
 323		GBAThreadLoadROM(&m_threadContext, m_threadContext.fname);
 324	}
 325
 326	if (!m_bios.isNull() && m_useBios) {
 327		m_threadContext.bios = VFileDevice::open(m_bios, O_RDONLY);
 328	} else {
 329		m_threadContext.bios = nullptr;
 330	}
 331
 332	if (!m_patch.isNull()) {
 333		m_threadContext.patch = VFileDevice::open(m_patch, O_RDONLY);
 334	}
 335
 336	m_inputController->recalibrateAxes();
 337	memset(m_drawContext, 0xF8, VIDEO_VERTICAL_PIXELS * VIDEO_HORIZONTAL_PIXELS * 4);
 338
 339	if (!GBAThreadStart(&m_threadContext)) {
 340		m_gameOpen = false;
 341		emit gameFailed();
 342	} else if (m_audioProcessor) {
 343		startAudio();
 344	}
 345}
 346
 347void GameController::loadBIOS(const QString& path) {
 348	if (m_bios == path) {
 349		return;
 350	}
 351	m_bios = path;
 352	if (m_gameOpen) {
 353		closeGame();
 354		openGame();
 355	}
 356}
 357
 358void GameController::yankPak() {
 359	if (!m_gameOpen) {
 360		return;
 361	}
 362	threadInterrupt();
 363	GBAYankROM(m_threadContext.gba);
 364	threadContinue();
 365}
 366
 367void GameController::replaceGame(const QString& path) {
 368	if (!m_gameOpen) {
 369		return;
 370	}
 371
 372	m_fname = path;
 373	threadInterrupt();
 374	m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
 375	GBAThreadReplaceROM(&m_threadContext, m_threadContext.fname);
 376	threadContinue();
 377}
 378
 379void GameController::loadPatch(const QString& path) {
 380	if (m_gameOpen) {
 381		closeGame();
 382		m_patch = path;
 383		openGame();
 384	} else {
 385		m_patch = path;
 386	}
 387}
 388
 389void GameController::importSharkport(const QString& path) {
 390	if (!isLoaded()) {
 391		return;
 392	}
 393	VFile* vf = VFileDevice::open(path, O_RDONLY);
 394	if (!vf) {
 395		postLog(GBA_LOG_ERROR, tr("Failed to open snapshot file for reading: %1").arg(path));
 396		return;
 397	}
 398	threadInterrupt();
 399	GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
 400	threadContinue();
 401	vf->close(vf);
 402}
 403
 404void GameController::exportSharkport(const QString& path) {
 405	if (!isLoaded()) {
 406		return;
 407	}
 408	VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
 409	if (!vf) {
 410		postLog(GBA_LOG_ERROR, tr("Failed to open snapshot file for writing: %1").arg(path));
 411		return;
 412	}
 413	threadInterrupt();
 414	GBASavedataExportSharkPort(m_threadContext.gba, vf);
 415	threadContinue();
 416	vf->close(vf);
 417}
 418
 419void GameController::closeGame() {
 420	if (!m_gameOpen) {
 421		return;
 422	}
 423	m_gameOpen = false;
 424
 425	m_rewindTimer.stop();
 426	if (GBAThreadIsPaused(&m_threadContext)) {
 427		GBAThreadUnpause(&m_threadContext);
 428	}
 429	m_audioProcessor->pause();
 430	GBAThreadEnd(&m_threadContext);
 431	GBAThreadJoin(&m_threadContext);
 432	// Make sure the event queue clears out before the thread is reused
 433	QCoreApplication::processEvents();
 434	if (m_threadContext.fname) {
 435		free(const_cast<char*>(m_threadContext.fname));
 436		m_threadContext.fname = nullptr;
 437	}
 438
 439	m_patch = QString();
 440
 441	for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
 442		GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
 443		GBACheatSetDeinit(set);
 444		delete set;
 445	}
 446	GBACheatSetsClear(&m_cheatDevice.cheats);
 447
 448	m_gameOpen = false;
 449	emit gameStopped(&m_threadContext);
 450}
 451
 452void GameController::crashGame(const QString& crashMessage) {
 453	closeGame();
 454	emit gameCrashed(crashMessage);
 455	emit gameStopped(&m_threadContext);
 456}
 457
 458bool GameController::isPaused() {
 459	if (!m_gameOpen) {
 460		return false;
 461	}
 462	return GBAThreadIsPaused(&m_threadContext);
 463}
 464
 465void GameController::setPaused(bool paused) {
 466	if (!isLoaded() || m_rewindTimer.isActive() || paused == GBAThreadIsPaused(&m_threadContext)) {
 467		return;
 468	}
 469	if (paused) {
 470		m_pauseAfterFrame.testAndSetRelaxed(false, true);
 471	} else {
 472		GBAThreadUnpause(&m_threadContext);
 473		startAudio();
 474		emit gameUnpaused(&m_threadContext);
 475	}
 476}
 477
 478void GameController::reset() {
 479	if (!m_gameOpen) {
 480		return;
 481	}
 482	bool wasPaused = isPaused();
 483	setPaused(false);
 484	GBAThreadReset(&m_threadContext);
 485	if (wasPaused) {
 486		setPaused(true);
 487	}
 488}
 489
 490void GameController::threadInterrupt() {
 491	if (m_gameOpen) {
 492		GBAThreadInterrupt(&m_threadContext);
 493	}
 494}
 495
 496void GameController::threadContinue() {
 497	if (m_gameOpen) {
 498		GBAThreadContinue(&m_threadContext);
 499	}
 500}
 501
 502void GameController::frameAdvance() {
 503	if (m_rewindTimer.isActive()) {
 504		return;
 505	}
 506	if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
 507		setPaused(false);
 508	}
 509}
 510
 511void GameController::setRewind(bool enable, int capacity, int interval) {
 512	if (m_gameOpen) {
 513		threadInterrupt();
 514		GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
 515		threadContinue();
 516	} else {
 517		if (enable) {
 518			m_threadContext.rewindBufferInterval = interval;
 519			m_threadContext.rewindBufferCapacity = capacity;
 520		} else {
 521			m_threadContext.rewindBufferInterval = 0;
 522			m_threadContext.rewindBufferCapacity = 0;
 523		}
 524	}
 525}
 526
 527void GameController::rewind(int states) {
 528	threadInterrupt();
 529	if (!states) {
 530		GBARewindAll(&m_threadContext);
 531	} else {
 532		GBARewind(&m_threadContext, states);
 533	}
 534	threadContinue();
 535	emit frameAvailable(m_drawContext);
 536	emit rewound(&m_threadContext);
 537}
 538
 539void GameController::startRewinding() {
 540	if (!m_gameOpen || m_rewindTimer.isActive()) {
 541		return;
 542	}
 543	if (m_multiplayer && m_multiplayer->attached() > 1) {
 544		return;
 545	}
 546	m_wasPaused = isPaused();
 547	if (!GBAThreadIsPaused(&m_threadContext)) {
 548		GBAThreadPause(&m_threadContext);
 549	}
 550	m_rewindTimer.start();
 551}
 552
 553void GameController::stopRewinding() {
 554	if (!m_rewindTimer.isActive()) {
 555		return;
 556	}
 557	m_rewindTimer.stop();
 558	bool signalsBlocked = blockSignals(true);
 559	setPaused(m_wasPaused);
 560	blockSignals(signalsBlocked);
 561}
 562
 563void GameController::keyPressed(int key) {
 564	int mappedKey = 1 << key;
 565	m_activeKeys |= mappedKey;
 566	if (!m_inputController->allowOpposing()) {
 567		if ((m_activeKeys & 0x30) == 0x30) {
 568			m_inactiveKeys |= mappedKey ^ 0x30;
 569			m_activeKeys ^= mappedKey ^ 0x30;
 570		}
 571		if ((m_activeKeys & 0xC0) == 0xC0) {
 572			m_inactiveKeys |= mappedKey ^ 0xC0;
 573			m_activeKeys ^= mappedKey ^ 0xC0;
 574		}
 575	}
 576	updateKeys();
 577}
 578
 579void GameController::keyReleased(int key) {
 580	int mappedKey = 1 << key;
 581	m_activeKeys &= ~mappedKey;
 582	if (!m_inputController->allowOpposing()) {
 583		if (mappedKey & 0x30) {
 584			m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
 585			m_inactiveKeys &= ~0x30;
 586		}
 587		if (mappedKey & 0xC0) {
 588			m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
 589			m_inactiveKeys &= ~0xC0;
 590		}
 591	}
 592	updateKeys();
 593}
 594
 595void GameController::clearKeys() {
 596	m_activeKeys = 0;
 597	m_inactiveKeys = 0;
 598	updateKeys();
 599}
 600
 601void GameController::setAutofire(int key, bool enable) {
 602	if (key >= GBA_KEY_MAX || key < 0) {
 603		return;
 604	}
 605	m_autofire[key] = enable;
 606	m_autofireStatus[key] = 0;
 607}
 608
 609void GameController::setAudioBufferSamples(int samples) {
 610	if (m_audioProcessor) {
 611		threadInterrupt();
 612		redoSamples(samples);
 613		threadContinue();
 614		QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Qt::BlockingQueuedConnection, Q_ARG(int, samples));
 615	}
 616}
 617
 618void GameController::setAudioSampleRate(unsigned rate) {
 619	if (!rate) {
 620		return;
 621	}
 622	if (m_audioProcessor) {
 623		threadInterrupt();
 624		redoSamples(m_audioProcessor->getBufferSamples());
 625		threadContinue();
 626		QMetaObject::invokeMethod(m_audioProcessor, "requestSampleRate", Q_ARG(unsigned, rate));
 627	}
 628}
 629
 630void GameController::setAudioChannelEnabled(int channel, bool enable) {
 631	if (channel > 5 || channel < 0) {
 632		return;
 633	}
 634	m_audioChannels[channel] = enable;
 635	if (isLoaded()) {
 636		switch (channel) {
 637		case 0:
 638		case 1:
 639		case 2:
 640		case 3:
 641			m_threadContext.gba->audio.psg.forceDisableCh[channel] = !enable;
 642			break;
 643		case 4:
 644			m_threadContext.gba->audio.forceDisableChA = !enable;
 645			break;
 646		case 5:
 647			m_threadContext.gba->audio.forceDisableChB = !enable;
 648			break;
 649		}
 650	}
 651}
 652
 653void GameController::startAudio() {
 654	bool started = false;
 655	QMetaObject::invokeMethod(m_audioProcessor, "start", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, started));
 656	if (!started) {
 657		LOG(ERROR) << tr("Failed to start audio processor");
 658		// Don't freeze!
 659		m_audioSync = false;
 660		m_videoSync = true;
 661		m_threadContext.sync.audioWait = false;
 662		m_threadContext.sync.videoFrameWait = true;
 663	}
 664}
 665
 666void GameController::setVideoLayerEnabled(int layer, bool enable) {
 667	if (layer > 4 || layer < 0) {
 668		return;
 669	}
 670	m_videoLayers[layer] = enable;
 671	if (isLoaded()) {
 672		switch (layer) {
 673		case 0:
 674		case 1:
 675		case 2:
 676		case 3:
 677			m_threadContext.gba->video.renderer->disableBG[layer] = !enable;
 678			break;
 679		case 4:
 680			m_threadContext.gba->video.renderer->disableOBJ = !enable;
 681			break;
 682		}
 683	}
 684}
 685
 686void GameController::setFPSTarget(float fps) {
 687	threadInterrupt();
 688	m_fpsTarget = fps;
 689	m_threadContext.fpsTarget = fps;
 690	if (m_turbo && m_turboSpeed > 0) {
 691		m_threadContext.fpsTarget *= m_turboSpeed;
 692	}
 693	if (m_audioProcessor) {
 694		redoSamples(m_audioProcessor->getBufferSamples());
 695	}
 696	threadContinue();
 697}
 698
 699void GameController::setSkipBIOS(bool set) {
 700	threadInterrupt();
 701	m_threadContext.skipBios = set;
 702	threadContinue();
 703}
 704
 705void GameController::setUseBIOS(bool use) {
 706	if (use == m_useBios) {
 707		return;
 708	}
 709	m_useBios = use;
 710	if (m_gameOpen) {
 711		closeGame();
 712		openGame();
 713	}
 714}
 715
 716void GameController::loadState(int slot) {
 717	if (!m_threadContext.fname) {
 718		// We're in the BIOS
 719		return;
 720	}
 721	if (slot > 0 && slot != m_stateSlot) {
 722		m_stateSlot = slot;
 723		m_backupSaveState.clear();
 724	}
 725	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
 726		GameController* controller = static_cast<GameController*>(context->userData);
 727		if (!controller->m_backupLoadState) {
 728			controller->m_backupLoadState = new GBASerializedState;
 729		}
 730		GBASerialize(context->gba, controller->m_backupLoadState);
 731		if (GBALoadState(context, context->dirs.state, controller->m_stateSlot, controller->m_loadStateFlags)) {
 732			controller->frameAvailable(controller->m_drawContext);
 733			controller->stateLoaded(context);
 734		}
 735	});
 736}
 737
 738void GameController::saveState(int slot) {
 739	if (!m_threadContext.fname) {
 740		// We're in the BIOS
 741		return;
 742	}
 743	if (slot > 0) {
 744		m_stateSlot = slot;
 745	}
 746	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
 747		GameController* controller = static_cast<GameController*>(context->userData);
 748		VFile* vf = GBAGetState(context->gba, context->dirs.state, controller->m_stateSlot, false);
 749		if (vf) {
 750			controller->m_backupSaveState.resize(vf->size(vf));
 751			vf->read(vf, controller->m_backupSaveState.data(), controller->m_backupSaveState.size());
 752			vf->close(vf);
 753		}
 754		GBASaveState(context, context->dirs.state, controller->m_stateSlot, controller->m_saveStateFlags);
 755	});
 756}
 757
 758void GameController::loadBackupState() {
 759	if (!m_backupLoadState) {
 760		return;
 761	}
 762
 763	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
 764		GameController* controller = static_cast<GameController*>(context->userData);
 765		if (GBADeserialize(context->gba, controller->m_backupLoadState)) {
 766			GBALog(context->gba, GBA_LOG_STATUS, "Undid state load");
 767			controller->frameAvailable(controller->m_drawContext);
 768			controller->stateLoaded(context);
 769		}
 770		delete controller->m_backupLoadState;
 771		controller->m_backupLoadState = nullptr;
 772	});
 773}
 774
 775void GameController::saveBackupState() {
 776	if (m_backupSaveState.isEmpty()) {
 777		return;
 778	}
 779
 780	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
 781		GameController* controller = static_cast<GameController*>(context->userData);
 782		VFile* vf = GBAGetState(context->gba, context->dirs.state, controller->m_stateSlot, true);
 783		if (vf) {
 784			vf->write(vf, controller->m_backupSaveState.constData(), controller->m_backupSaveState.size());
 785			vf->close(vf);
 786			GBALog(context->gba, GBA_LOG_STATUS, "Undid state save");
 787		}
 788		controller->m_backupSaveState.clear();
 789	});
 790}
 791
 792void GameController::setVideoSync(bool set) {
 793	m_videoSync = set;
 794	if (!m_turbo) {
 795		threadInterrupt();
 796		m_threadContext.sync.videoFrameWait = set;
 797		threadContinue();
 798	}
 799}
 800
 801void GameController::setAudioSync(bool set) {
 802	m_audioSync = set;
 803	if (!m_turbo) {
 804		threadInterrupt();
 805		m_threadContext.sync.audioWait = set;
 806		threadContinue();
 807	}
 808}
 809
 810void GameController::setFrameskip(int skip) {
 811	threadInterrupt();
 812	m_threadContext.frameskip = skip;
 813	if (isLoaded()) {
 814		m_threadContext.gba->video.frameskip = skip;
 815	}
 816	threadContinue();
 817}
 818
 819void GameController::setVolume(int volume) {
 820	threadInterrupt();
 821	m_threadContext.volume = volume;
 822	if (isLoaded()) {
 823		m_threadContext.gba->audio.masterVolume = volume;
 824	}
 825	threadContinue();
 826}
 827
 828void GameController::setMute(bool mute) {
 829	threadInterrupt();
 830	m_threadContext.mute = mute;
 831	if (isLoaded()) {
 832		m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
 833	}
 834	threadContinue();
 835}
 836
 837void GameController::setTurbo(bool set, bool forced) {
 838	if (m_turboForced && !forced) {
 839		return;
 840	}
 841	if (m_turbo == set && m_turboForced == forced) {
 842		// Don't interrupt the thread if we don't need to
 843		return;
 844	}
 845	m_turbo = set;
 846	m_turboForced = set && forced;
 847	enableTurbo();
 848}
 849
 850void GameController::setTurboSpeed(float ratio) {
 851	m_turboSpeed = ratio;
 852	enableTurbo();
 853}
 854
 855void GameController::enableTurbo() {
 856	threadInterrupt();
 857	if (!m_turbo) {
 858		m_threadContext.fpsTarget = m_fpsTarget;
 859		m_threadContext.sync.audioWait = m_audioSync;
 860		m_threadContext.sync.videoFrameWait = m_videoSync;
 861	} else if (m_turboSpeed <= 0) {
 862		m_threadContext.fpsTarget = m_fpsTarget;
 863		m_threadContext.sync.audioWait = false;
 864		m_threadContext.sync.videoFrameWait = false;
 865	} else {
 866		m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
 867		m_threadContext.sync.audioWait = true;
 868		m_threadContext.sync.videoFrameWait = false;
 869	}
 870	if (m_audioProcessor) {
 871		redoSamples(m_audioProcessor->getBufferSamples());
 872	}
 873	threadContinue();
 874}
 875
 876void GameController::setAVStream(mAVStream* stream) {
 877	threadInterrupt();
 878	m_threadContext.stream = stream;
 879	if (isLoaded()) {
 880		m_threadContext.gba->stream = stream;
 881	}
 882	threadContinue();
 883}
 884
 885void GameController::clearAVStream() {
 886	threadInterrupt();
 887	m_threadContext.stream = nullptr;
 888	if (isLoaded()) {
 889		m_threadContext.gba->stream = nullptr;
 890	}
 891	threadContinue();
 892}
 893
 894#ifdef USE_PNG
 895void GameController::screenshot() {
 896	GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
 897}
 898#endif
 899
 900void GameController::reloadAudioDriver() {
 901	int samples = 0;
 902	unsigned sampleRate = 0;
 903	if (m_audioProcessor) {
 904		QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
 905		samples = m_audioProcessor->getBufferSamples();
 906		sampleRate = m_audioProcessor->sampleRate();
 907		delete m_audioProcessor;
 908	}
 909	m_audioProcessor = AudioProcessor::create();
 910	if (samples) {
 911		m_audioProcessor->setBufferSamples(samples);
 912	}
 913	if (sampleRate) {
 914		m_audioProcessor->requestSampleRate(sampleRate);
 915	}
 916	m_audioProcessor->moveToThread(m_audioThread);
 917	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
 918	if (isLoaded()) {
 919		m_audioProcessor->setInput(&m_threadContext);
 920		startAudio();
 921	}
 922}
 923
 924void GameController::setSaveStateExtdata(int flags) {
 925	m_saveStateFlags = flags;
 926}
 927
 928void GameController::setLoadStateExtdata(int flags) {
 929	m_loadStateFlags = flags;
 930}
 931
 932void GameController::setLuminanceValue(uint8_t value) {
 933	m_luxValue = value;
 934	value = std::max<int>(value - 0x16, 0);
 935	m_luxLevel = 10;
 936	for (int i = 0; i < 10; ++i) {
 937		if (value < GBA_LUX_LEVELS[i]) {
 938			m_luxLevel = i;
 939			break;
 940		}
 941	}
 942	emit luminanceValueChanged(m_luxValue);
 943}
 944
 945void GameController::setLuminanceLevel(int level) {
 946	int value = 0x16;
 947	level = std::max(0, std::min(10, level));
 948	if (level > 0) {
 949		value += GBA_LUX_LEVELS[level - 1];
 950	}
 951	setLuminanceValue(value);
 952}
 953
 954void GameController::setRealTime() {
 955	m_rtc.override = RTC_NO_OVERRIDE;
 956}
 957
 958void GameController::setFixedTime(const QDateTime& time) {
 959	m_rtc.override = RTC_FIXED;
 960	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
 961}
 962
 963void GameController::setFakeEpoch(const QDateTime& time) {
 964	m_rtc.override = RTC_FAKE_EPOCH;
 965	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
 966}
 967
 968void GameController::updateKeys() {
 969	int activeKeys = m_activeKeys;
 970	activeKeys |= m_activeButtons;
 971	activeKeys &= ~m_inactiveKeys;
 972	m_threadContext.activeKeys = activeKeys;
 973}
 974
 975void GameController::redoSamples(int samples) {
 976	m_threadContext.audioBuffers = samples;
 977	if (m_threadContext.gba) {
 978		GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
 979	}
 980	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
 981}
 982
 983void GameController::setLogLevel(int levels) {
 984	threadInterrupt();
 985	m_logLevels = levels;
 986	threadContinue();
 987}
 988
 989void GameController::enableLogLevel(int levels) {
 990	threadInterrupt();
 991	m_logLevels |= levels;
 992	threadContinue();
 993}
 994
 995void GameController::disableLogLevel(int levels) {
 996	threadInterrupt();
 997	m_logLevels &= ~levels;
 998	threadContinue();
 999}
1000
1001void GameController::pollEvents() {
1002	if (!m_inputController) {
1003		return;
1004	}
1005
1006	m_activeButtons = m_inputController->pollEvents();
1007	updateKeys();
1008}
1009
1010void GameController::updateAutofire() {
1011	// TODO: Move all key events onto the CPU thread...somehow
1012	for (int k = 0; k < GBA_KEY_MAX; ++k) {
1013		if (!m_autofire[k]) {
1014			continue;
1015		}
1016		m_autofireStatus[k] ^= 1;
1017		if (m_autofireStatus[k]) {
1018			keyPressed(k);
1019		} else {
1020			keyReleased(k);
1021		}
1022	}
1023}