all repos — mgba @ 52c66de6945adefa72d87cf5c461af4b30fd1d68

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 "MultiplayerController.h"
 11#include "VFileDevice.h"
 12
 13#include <QDateTime>
 14#include <QThread>
 15
 16#include <ctime>
 17
 18extern "C" {
 19#include "gba/audio.h"
 20#include "gba/gba.h"
 21#include "gba/serialize.h"
 22#include "gba/sharkport.h"
 23#include "gba/renderers/video-software.h"
 24#include "gba/supervisor/config.h"
 25#include "util/vfs.h"
 26}
 27
 28using namespace QGBA;
 29using namespace std;
 30
 31const int GameController::LUX_LEVELS[10] = { 5, 11, 18, 27, 42, 62, 84, 109, 139, 183 };
 32
 33GameController::GameController(QObject* parent)
 34	: QObject(parent)
 35	, m_drawContext(new uint32_t[256 * 256])
 36	, m_threadContext()
 37	, m_activeKeys(0)
 38	, m_inactiveKeys(0)
 39	, m_logLevels(0)
 40	, m_gameOpen(false)
 41	, m_audioThread(new QThread(this))
 42	, m_audioProcessor(AudioProcessor::create())
 43	, m_pauseAfterFrame(false)
 44	, m_videoSync(VIDEO_SYNC)
 45	, m_audioSync(AUDIO_SYNC)
 46	, m_fpsTarget(-1)
 47	, m_turbo(false)
 48	, m_turboForced(false)
 49	, m_turboSpeed(-1)
 50	, m_wasPaused(false)
 51	, m_inputController(nullptr)
 52	, m_multiplayer(nullptr)
 53	, m_stateSlot(1)
 54{
 55	m_renderer = new GBAVideoSoftwareRenderer;
 56	GBAVideoSoftwareRendererCreate(m_renderer);
 57	m_renderer->outputBuffer = (color_t*) m_drawContext;
 58	m_renderer->outputBufferStride = 256;
 59
 60	GBACheatDeviceCreate(&m_cheatDevice);
 61
 62	m_threadContext.state = THREAD_INITIALIZED;
 63	m_threadContext.debugger = 0;
 64	m_threadContext.frameskip = 0;
 65	m_threadContext.bios = 0;
 66	m_threadContext.renderer = &m_renderer->d;
 67	m_threadContext.userData = this;
 68	m_threadContext.rewindBufferCapacity = 0;
 69	m_threadContext.cheats = &m_cheatDevice;
 70	m_threadContext.logLevel = GBA_LOG_ALL;
 71
 72	m_lux.p = this;
 73	m_lux.sample = [] (GBALuminanceSource* context) {
 74		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 75		lux->value = 0xFF - lux->p->m_luxValue;
 76	};
 77
 78	m_lux.readLuminance = [] (GBALuminanceSource* context) {
 79		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 80		return lux->value;
 81	};
 82	setLuminanceLevel(0);
 83
 84	m_rtc.p = this;
 85	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
 86	m_rtc.sample = [] (GBARTCSource* context) { };
 87	m_rtc.unixTime = [] (GBARTCSource* context) -> time_t {
 88		GameControllerRTC* rtc = static_cast<GameControllerRTC*>(context);
 89		switch (rtc->override) {
 90		case GameControllerRTC::NO_OVERRIDE:
 91		default:
 92			return time(nullptr);
 93		case GameControllerRTC::FIXED:
 94			return rtc->value;
 95		case GameControllerRTC::FAKE_EPOCH:
 96			return rtc->value + rtc->p->m_threadContext.gba->video.frameCounter * (int64_t) VIDEO_TOTAL_LENGTH / GBA_ARM7TDMI_FREQUENCY;
 97		}
 98	};
 99
100	m_threadContext.startCallback = [] (GBAThread* context) {
101		GameController* controller = static_cast<GameController*>(context->userData);
102		controller->m_audioProcessor->setInput(context);
103		context->gba->luminanceSource = &controller->m_lux;
104		context->gba->rtcSource = &controller->m_rtc;
105		context->gba->rumble = controller->m_inputController->rumble();
106		context->gba->rotationSource = controller->m_inputController->rotationSource();
107		controller->m_fpsTarget = context->fpsTarget;
108		controller->gameStarted(context);
109	};
110
111	m_threadContext.cleanCallback = [] (GBAThread* context) {
112		GameController* controller = static_cast<GameController*>(context->userData);
113		controller->gameStopped(context);
114	};
115
116	m_threadContext.frameCallback = [] (GBAThread* context) {
117		GameController* controller = static_cast<GameController*>(context->userData);
118		if (controller->m_pauseAfterFrame.testAndSetAcquire(true, false)) {
119			GBAThreadPauseFromThread(context);
120			controller->gamePaused(&controller->m_threadContext);
121		}
122		if (GBASyncDrawingFrame(&controller->m_threadContext.sync)) {
123			controller->frameAvailable(controller->m_drawContext);
124		} else {
125			controller->frameAvailable(nullptr);
126		}
127	};
128
129	m_threadContext.logHandler = [] (GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
130		static const char* stubMessage = "Stub software interrupt";
131		if (!context) {
132			return;
133		}
134		GameController* controller = static_cast<GameController*>(context->userData);
135		if (level == GBA_LOG_STUB && strncmp(stubMessage, format, strlen(stubMessage)) == 0) {
136			va_list argc;
137			va_copy(argc, args);
138			int immediate = va_arg(argc, int);
139			controller->unimplementedBiosCall(immediate);
140		}
141		if (level == GBA_LOG_FATAL) {
142			QMetaObject::invokeMethod(controller, "crashGame", Q_ARG(const QString&, QString().vsprintf(format, args)));
143		} else if (!(controller->m_logLevels & level)) {
144			return;
145		}
146		QString message(QString().vsprintf(format, args));
147		if (level == GBA_LOG_STATUS) {
148			controller->statusPosted(message);
149		}
150		controller->postLog(level, message);
151	};
152
153	connect(&m_rewindTimer, &QTimer::timeout, [this]() {
154		GBARewind(&m_threadContext, 1);
155		emit rewound(&m_threadContext);
156		emit frameAvailable(m_drawContext);
157	});
158	m_rewindTimer.setInterval(100);
159
160	m_audioThread->setObjectName("Audio Thread");
161	m_audioThread->start(QThread::TimeCriticalPriority);
162	m_audioProcessor->moveToThread(m_audioThread);
163	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
164	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
165	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
166	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
167	connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(pollEvents()));
168}
169
170GameController::~GameController() {
171	m_audioThread->quit();
172	m_audioThread->wait();
173	disconnect();
174	clearMultiplayerController();
175	closeGame();
176	GBACheatDeviceDestroy(&m_cheatDevice);
177	delete m_renderer;
178	delete[] m_drawContext;
179}
180
181void GameController::setMultiplayerController(MultiplayerController* controller) {
182	if (controller == m_multiplayer) {
183		return;
184	}
185	clearMultiplayerController();
186	m_multiplayer = controller;
187	controller->attachGame(this);
188}
189
190void GameController::clearMultiplayerController() {
191	if (!m_multiplayer) {
192		return;
193	}
194	m_multiplayer->detachGame(this);
195	m_multiplayer = nullptr;
196}
197
198void GameController::setOverride(const GBACartridgeOverride& override) {
199	m_threadContext.override = override;
200	m_threadContext.hasOverride = true;
201}
202
203void GameController::setOptions(const GBAOptions* opts) {
204	setFrameskip(opts->frameskip);
205	setAudioSync(opts->audioSync);
206	setVideoSync(opts->videoSync);
207	setSkipBIOS(opts->skipBios);
208	setUseBIOS(opts->useBios);
209	setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
210	setVolume(opts->volume);
211	setMute(opts->mute);
212
213	threadInterrupt();
214	m_threadContext.idleOptimization = opts->idleOptimization;
215	threadContinue();
216}
217
218#ifdef USE_GDB_STUB
219ARMDebugger* GameController::debugger() {
220	return m_threadContext.debugger;
221}
222
223void GameController::setDebugger(ARMDebugger* debugger) {
224	threadInterrupt();
225	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
226		GBADetachDebugger(m_threadContext.gba);
227	}
228	m_threadContext.debugger = debugger;
229	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
230		GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
231	}
232	threadContinue();
233}
234#endif
235
236void GameController::loadGame(const QString& path, bool dirmode) {
237	closeGame();
238	if (!dirmode) {
239		QFile file(path);
240		if (!file.open(QIODevice::ReadOnly)) {
241			return;
242		}
243		file.close();
244	}
245
246	m_fname = path;
247	m_dirmode = dirmode;
248	openGame();
249}
250
251void GameController::bootBIOS() {
252	closeGame();
253	m_fname = QString();
254	m_dirmode = false;
255	openGame(true);
256}
257
258void GameController::openGame(bool biosOnly) {
259	if (biosOnly && (!m_useBios || m_bios.isNull())) {
260		return;
261	}
262
263	m_gameOpen = true;
264
265	m_pauseAfterFrame = false;
266
267	if (m_turbo) {
268		m_threadContext.sync.videoFrameWait = false;
269		m_threadContext.sync.audioWait = false;
270	} else {
271		m_threadContext.sync.videoFrameWait = m_videoSync;
272		m_threadContext.sync.audioWait = m_audioSync;
273	}
274
275	m_threadContext.gameDir = 0;
276	m_threadContext.bootBios = biosOnly;
277	if (biosOnly) {
278		m_threadContext.fname = nullptr;
279	} else {
280		m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
281		if (m_dirmode) {
282			m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
283			m_threadContext.stateDir = m_threadContext.gameDir;
284		} else {
285			GBAThreadLoadROM(&m_threadContext, m_threadContext.fname);
286		}
287	}
288
289	if (!m_bios.isNull() && m_useBios) {
290		m_threadContext.bios = VFileDevice::open(m_bios, O_RDONLY);
291	} else {
292		m_threadContext.bios = nullptr;
293	}
294
295	if (!m_patch.isNull()) {
296		m_threadContext.patch = VFileDevice::open(m_patch, O_RDONLY);
297	}
298
299	m_inputController->recalibrateAxes();
300
301	if (!GBAThreadStart(&m_threadContext)) {
302		m_gameOpen = false;
303		emit gameFailed();
304	}
305}
306
307void GameController::loadBIOS(const QString& path) {
308	if (m_bios == path) {
309		return;
310	}
311	m_bios = path;
312	if (m_gameOpen) {
313		closeGame();
314		openGame();
315	}
316}
317
318void GameController::yankPak() {
319	if (!m_gameOpen) {
320		return;
321	}
322	threadInterrupt();
323	GBAYankROM(m_threadContext.gba);
324	threadContinue();
325}
326
327
328void GameController::replaceGame(const QString& path) {
329	if (!m_gameOpen) {
330		return;
331	}
332
333	m_fname = path;
334	threadInterrupt();
335	m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
336	GBAThreadReplaceROM(&m_threadContext, m_threadContext.fname);
337	threadContinue();
338}
339
340void GameController::loadPatch(const QString& path) {
341	if (m_gameOpen) {
342		closeGame();
343		m_patch = path;
344		openGame();
345	} else {
346		m_patch = path;
347	}
348}
349
350void GameController::importSharkport(const QString& path) {
351	if (!m_gameOpen) {
352		return;
353	}
354	VFile* vf = VFileDevice::open(path, O_RDONLY);
355	if (!vf) {
356		return;
357	}
358	threadInterrupt();
359	GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
360	threadContinue();
361	vf->close(vf);
362}
363
364void GameController::exportSharkport(const QString& path) {
365	if (!m_gameOpen) {
366		return;
367	}
368	VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
369	if (!vf) {
370		return;
371	}
372	threadInterrupt();
373	GBASavedataExportSharkPort(m_threadContext.gba, vf);
374	threadContinue();
375	vf->close(vf);
376}
377
378void GameController::closeGame() {
379	if (!m_gameOpen) {
380		return;
381	}
382	m_rewindTimer.stop();
383	if (GBAThreadIsPaused(&m_threadContext)) {
384		GBAThreadUnpause(&m_threadContext);
385	}
386	GBAThreadEnd(&m_threadContext);
387	GBAThreadJoin(&m_threadContext);
388	if (m_threadContext.fname) {
389		free(const_cast<char*>(m_threadContext.fname));
390		m_threadContext.fname = nullptr;
391	}
392
393	m_patch = QString();
394
395	for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
396		GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
397		GBACheatSetDeinit(set);
398		delete set;
399	}
400	GBACheatSetsClear(&m_cheatDevice.cheats);
401
402	m_gameOpen = false;
403	emit gameStopped(&m_threadContext);
404}
405
406void GameController::crashGame(const QString& crashMessage) {
407	closeGame();
408	emit gameCrashed(crashMessage);
409	emit gameStopped(&m_threadContext);
410}
411
412bool GameController::isPaused() {
413	if (!m_gameOpen) {
414		return false;
415	}
416	return GBAThreadIsPaused(&m_threadContext);
417}
418
419void GameController::setPaused(bool paused) {
420	if (!m_gameOpen || m_rewindTimer.isActive() || paused == GBAThreadIsPaused(&m_threadContext)) {
421		return;
422	}
423	if (paused) {
424		GBAThreadPause(&m_threadContext);
425		emit gamePaused(&m_threadContext);
426	} else {
427		GBAThreadUnpause(&m_threadContext);
428		emit gameUnpaused(&m_threadContext);
429	}
430}
431
432void GameController::reset() {
433	GBAThreadReset(&m_threadContext);
434}
435
436void GameController::threadInterrupt() {
437	if (m_gameOpen) {
438		GBAThreadInterrupt(&m_threadContext);
439	}
440}
441
442void GameController::threadContinue() {
443	if (m_gameOpen) {
444		GBAThreadContinue(&m_threadContext);
445	}
446}
447
448void GameController::frameAdvance() {
449	if (m_rewindTimer.isActive()) {
450		return;
451	}
452	if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
453		setPaused(false);
454	}
455}
456
457void GameController::setRewind(bool enable, int capacity, int interval) {
458	if (m_gameOpen) {
459		threadInterrupt();
460		GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
461		threadContinue();
462	} else {
463		if (enable) {
464			m_threadContext.rewindBufferInterval = interval;
465			m_threadContext.rewindBufferCapacity = capacity;
466		} else {
467			m_threadContext.rewindBufferInterval = 0;
468			m_threadContext.rewindBufferCapacity = 0;
469		}
470	}
471}
472
473void GameController::rewind(int states) {
474	threadInterrupt();
475	if (!states) {
476		GBARewindAll(&m_threadContext);
477	} else {
478		GBARewind(&m_threadContext, states);
479	}
480	threadContinue();
481	emit rewound(&m_threadContext);
482	emit frameAvailable(m_drawContext);
483}
484
485void GameController::startRewinding() {
486	if (!m_gameOpen || m_rewindTimer.isActive()) {
487		return;
488	}
489	m_wasPaused = isPaused();
490	setPaused(true);
491	m_rewindTimer.start();
492}
493
494void GameController::stopRewinding() {
495	if (!m_rewindTimer.isActive()) {
496		return;
497	}
498	m_rewindTimer.stop();
499	setPaused(m_wasPaused);
500}
501
502void GameController::keyPressed(int key) {
503	int mappedKey = 1 << key;
504	m_activeKeys |= mappedKey;
505	if (!m_inputController->allowOpposing()) {
506		if ((m_activeKeys & 0x30) == 0x30) {
507			m_inactiveKeys |= mappedKey ^ 0x30;
508			m_activeKeys ^= mappedKey ^ 0x30;
509		}
510		if ((m_activeKeys & 0xC0) == 0xC0) {
511			m_inactiveKeys |= mappedKey ^ 0xC0;
512			m_activeKeys ^= mappedKey ^ 0xC0;
513		}
514	}
515	updateKeys();
516}
517
518void GameController::keyReleased(int key) {
519	int mappedKey = 1 << key;
520	m_activeKeys &= ~mappedKey;
521	if (!m_inputController->allowOpposing()) {
522		if (mappedKey & 0x30) {
523			m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
524			m_inactiveKeys &= ~0x30;
525		}
526		if (mappedKey & 0xC0) {
527			m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
528			m_inactiveKeys &= ~0xC0;
529		}
530	}
531	updateKeys();
532}
533
534void GameController::clearKeys() {
535	m_activeKeys = 0;
536	m_inactiveKeys = 0;
537	updateKeys();
538}
539
540void GameController::setAudioBufferSamples(int samples) {
541	threadInterrupt();
542	redoSamples(samples);
543	threadContinue();
544	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
545}
546
547void GameController::setFPSTarget(float fps) {
548	threadInterrupt();
549	m_fpsTarget = fps;
550	m_threadContext.fpsTarget = fps;
551	if (m_turbo && m_turboSpeed > 0) {
552		m_threadContext.fpsTarget *= m_turboSpeed;
553	}
554	redoSamples(m_audioProcessor->getBufferSamples());
555	threadContinue();
556}
557
558void GameController::setSkipBIOS(bool set) {
559	threadInterrupt();
560	m_threadContext.skipBios = set;
561	threadContinue();
562}
563
564void GameController::setUseBIOS(bool use) {
565	threadInterrupt();
566	m_useBios = use;
567	threadContinue();
568}
569
570void GameController::loadState(int slot) {
571	if (slot > 0) {
572		m_stateSlot = slot;
573	}
574	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
575		GameController* controller = static_cast<GameController*>(context->userData);
576		if (GBALoadState(context, context->stateDir, controller->m_stateSlot)) {
577			controller->stateLoaded(context);
578			controller->frameAvailable(controller->m_drawContext);
579		}
580	});
581}
582
583void GameController::saveState(int slot) {
584	if (slot > 0) {
585		m_stateSlot = slot;
586	}
587	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
588		GameController* controller = static_cast<GameController*>(context->userData);
589		GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
590	});
591}
592
593void GameController::setVideoSync(bool set) {
594	m_videoSync = set;
595	if (!m_turbo) {
596		threadInterrupt();
597		m_threadContext.sync.videoFrameWait = set;
598		threadContinue();
599	}
600}
601
602void GameController::setAudioSync(bool set) {
603	m_audioSync = set;
604	if (!m_turbo) {
605		threadInterrupt();
606		m_threadContext.sync.audioWait = set;
607		threadContinue();
608	}
609}
610
611void GameController::setFrameskip(int skip) {
612	m_threadContext.frameskip = skip;
613}
614
615void GameController::setVolume(int volume) {
616	threadInterrupt();
617	m_threadContext.volume = volume;
618	if (m_gameOpen) {
619		m_threadContext.gba->audio.masterVolume = volume;
620	}
621	threadContinue();
622}
623
624void GameController::setMute(bool mute) {
625	threadInterrupt();
626	m_threadContext.mute = mute;
627	if (m_gameOpen) {
628		m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
629	}
630	threadContinue();
631}
632
633void GameController::setTurbo(bool set, bool forced) {
634	if (m_turboForced && !forced) {
635		return;
636	}
637	if (m_turbo == set && m_turboForced == forced) {
638		// Don't interrupt the thread if we don't need to
639		return;
640	}
641	m_turbo = set;
642	m_turboForced = set && forced;
643	enableTurbo();
644}
645
646void GameController::setTurboSpeed(float ratio) {
647	m_turboSpeed = ratio;
648	enableTurbo();
649}
650
651void GameController::enableTurbo() {
652	threadInterrupt();
653	if (!m_turbo) {
654		m_threadContext.fpsTarget = m_fpsTarget;
655		m_threadContext.sync.audioWait = m_audioSync;
656		m_threadContext.sync.videoFrameWait = m_videoSync;
657	} else if (m_turboSpeed <= 0) {
658		m_threadContext.fpsTarget = m_fpsTarget;
659		m_threadContext.sync.audioWait = false;
660		m_threadContext.sync.videoFrameWait = false;
661	} else {
662		m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
663		m_threadContext.sync.audioWait = true;
664		m_threadContext.sync.videoFrameWait = false;
665	}
666	redoSamples(m_audioProcessor->getBufferSamples());
667	threadContinue();
668}
669
670void GameController::setAVStream(GBAAVStream* stream) {
671	threadInterrupt();
672	m_threadContext.stream = stream;
673	if (m_gameOpen) {
674		m_threadContext.gba->stream = stream;
675	}
676	threadContinue();
677}
678
679void GameController::clearAVStream() {
680	threadInterrupt();
681	m_threadContext.stream = nullptr;
682	if (m_gameOpen) {
683		m_threadContext.gba->stream = nullptr;
684	}
685	threadContinue();
686}
687
688#ifdef USE_PNG
689void GameController::screenshot() {
690	GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
691}
692#endif
693
694void GameController::reloadAudioDriver() {
695	QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
696	int samples = m_audioProcessor->getBufferSamples();
697	delete m_audioProcessor;
698	m_audioProcessor = AudioProcessor::create();
699	m_audioProcessor->setBufferSamples(samples);
700	m_audioProcessor->moveToThread(m_audioThread);
701	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
702	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
703	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
704	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
705	if (isLoaded()) {
706		m_audioProcessor->setInput(&m_threadContext);
707		QMetaObject::invokeMethod(m_audioProcessor, "start");
708	}
709}
710
711void GameController::setLuminanceValue(uint8_t value) {
712	m_luxValue = value;
713	value = std::max<int>(value - 0x16, 0);
714	m_luxLevel = 10;
715	for (int i = 0; i < 10; ++i) {
716		if (value < LUX_LEVELS[i]) {
717			m_luxLevel = i;
718			break;
719		}
720	}
721	emit luminanceValueChanged(m_luxValue);
722}
723
724void GameController::setLuminanceLevel(int level) {
725	int value = 0x16;
726	level = std::max(0, std::min(10, level));
727	if (level > 0) {
728		value += LUX_LEVELS[level - 1];
729	}
730	setLuminanceValue(value);
731}
732
733void GameController::setRealTime() {
734	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
735}
736
737void GameController::setFixedTime(const QDateTime& time) {
738	m_rtc.override = GameControllerRTC::FIXED;
739	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
740}
741
742void GameController::setFakeEpoch(const QDateTime& time) {
743	m_rtc.override = GameControllerRTC::FAKE_EPOCH;
744	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
745}
746
747void GameController::updateKeys() {
748	int activeKeys = m_activeKeys;
749	activeKeys |= m_activeButtons;
750	activeKeys &= ~m_inactiveKeys;
751	m_threadContext.activeKeys = activeKeys;
752}
753
754void GameController::redoSamples(int samples) {
755#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
756	float sampleRate = 0x8000;
757	float ratio;
758	if (m_threadContext.gba) {
759		sampleRate = m_threadContext.gba->audio.sampleRate;
760	}
761	ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
762	m_threadContext.audioBuffers = ceil(samples / ratio);
763#else
764	m_threadContext.audioBuffers = samples;
765#endif
766	if (m_threadContext.gba) {
767		GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
768	}
769	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
770}
771
772void GameController::setLogLevel(int levels) {
773	threadInterrupt();
774	m_logLevels = levels;
775	threadContinue();
776}
777
778void GameController::enableLogLevel(int levels) {
779	threadInterrupt();
780	m_logLevels |= levels;
781	threadContinue();
782}
783
784void GameController::disableLogLevel(int levels) {
785	threadInterrupt();
786	m_logLevels &= ~levels;
787	threadContinue();
788}
789
790void GameController::pollEvents() {
791	if (!m_inputController) {
792		return;
793	}
794
795	m_activeButtons = m_inputController->pollEvents();
796	updateKeys();
797}