all repos — mgba @ 8538e99a089118d0dc004eddbf48f453fbf9ebbc

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			m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
286#if USE_LIBZIP
287			if (!m_threadContext.gameDir) {
288				m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
289			}
290#endif
291#if USE_LZMA
292			if (!m_threadContext.gameDir) {
293				m_threadContext.gameDir = VDirOpen7z(m_threadContext.fname, 0);
294			}
295#endif
296		}
297	}
298
299	if (!m_bios.isNull() && m_useBios) {
300		m_threadContext.bios = VFileDevice::open(m_bios, O_RDONLY);
301	} else {
302		m_threadContext.bios = nullptr;
303	}
304
305	if (!m_patch.isNull()) {
306		m_threadContext.patch = VFileDevice::open(m_patch, O_RDONLY);
307	}
308
309	m_inputController->recalibrateAxes();
310
311	if (!GBAThreadStart(&m_threadContext)) {
312		m_gameOpen = false;
313		emit gameFailed();
314	}
315}
316
317void GameController::loadBIOS(const QString& path) {
318	if (m_bios == path) {
319		return;
320	}
321	m_bios = path;
322	if (m_gameOpen) {
323		closeGame();
324		openGame();
325	}
326}
327
328void GameController::yankPak() {
329	if (!m_gameOpen) {
330		return;
331	}
332	threadInterrupt();
333	GBAYankROM(m_threadContext.gba);
334	threadContinue();
335}
336
337void GameController::loadPatch(const QString& path) {
338	if (m_gameOpen) {
339		closeGame();
340		m_patch = path;
341		openGame();
342	} else {
343		m_patch = path;
344	}
345}
346
347void GameController::importSharkport(const QString& path) {
348	if (!m_gameOpen) {
349		return;
350	}
351	VFile* vf = VFileDevice::open(path, O_RDONLY);
352	if (!vf) {
353		return;
354	}
355	threadInterrupt();
356	GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
357	threadContinue();
358	vf->close(vf);
359}
360
361void GameController::exportSharkport(const QString& path) {
362	if (!m_gameOpen) {
363		return;
364	}
365	VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
366	if (!vf) {
367		return;
368	}
369	threadInterrupt();
370	GBASavedataExportSharkPort(m_threadContext.gba, vf);
371	threadContinue();
372	vf->close(vf);
373}
374
375void GameController::closeGame() {
376	if (!m_gameOpen) {
377		return;
378	}
379	m_rewindTimer.stop();
380	if (GBAThreadIsPaused(&m_threadContext)) {
381		GBAThreadUnpause(&m_threadContext);
382	}
383	GBAThreadEnd(&m_threadContext);
384	GBAThreadJoin(&m_threadContext);
385	if (m_threadContext.fname) {
386		free(const_cast<char*>(m_threadContext.fname));
387		m_threadContext.fname = nullptr;
388	}
389
390	m_patch = QString();
391
392	for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
393		GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
394		GBACheatSetDeinit(set);
395		delete set;
396	}
397	GBACheatSetsClear(&m_cheatDevice.cheats);
398
399	m_gameOpen = false;
400	emit gameStopped(&m_threadContext);
401}
402
403void GameController::crashGame(const QString& crashMessage) {
404	closeGame();
405	emit gameCrashed(crashMessage);
406	emit gameStopped(&m_threadContext);
407}
408
409bool GameController::isPaused() {
410	if (!m_gameOpen) {
411		return false;
412	}
413	return GBAThreadIsPaused(&m_threadContext);
414}
415
416void GameController::setPaused(bool paused) {
417	if (!m_gameOpen || m_rewindTimer.isActive() || paused == GBAThreadIsPaused(&m_threadContext)) {
418		return;
419	}
420	if (paused) {
421		GBAThreadPause(&m_threadContext);
422		emit gamePaused(&m_threadContext);
423	} else {
424		GBAThreadUnpause(&m_threadContext);
425		emit gameUnpaused(&m_threadContext);
426	}
427}
428
429void GameController::reset() {
430	GBAThreadReset(&m_threadContext);
431}
432
433void GameController::threadInterrupt() {
434	if (m_gameOpen) {
435		GBAThreadInterrupt(&m_threadContext);
436	}
437}
438
439void GameController::threadContinue() {
440	if (m_gameOpen) {
441		GBAThreadContinue(&m_threadContext);
442	}
443}
444
445void GameController::frameAdvance() {
446	if (m_rewindTimer.isActive()) {
447		return;
448	}
449	if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
450		setPaused(false);
451	}
452}
453
454void GameController::setRewind(bool enable, int capacity, int interval) {
455	if (m_gameOpen) {
456		threadInterrupt();
457		GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
458		threadContinue();
459	} else {
460		if (enable) {
461			m_threadContext.rewindBufferInterval = interval;
462			m_threadContext.rewindBufferCapacity = capacity;
463		} else {
464			m_threadContext.rewindBufferInterval = 0;
465			m_threadContext.rewindBufferCapacity = 0;
466		}
467	}
468}
469
470void GameController::rewind(int states) {
471	threadInterrupt();
472	if (!states) {
473		GBARewindAll(&m_threadContext);
474	} else {
475		GBARewind(&m_threadContext, states);
476	}
477	threadContinue();
478	emit rewound(&m_threadContext);
479	emit frameAvailable(m_drawContext);
480}
481
482void GameController::startRewinding() {
483	if (!m_gameOpen || m_rewindTimer.isActive()) {
484		return;
485	}
486	m_wasPaused = isPaused();
487	setPaused(true);
488	m_rewindTimer.start();
489}
490
491void GameController::stopRewinding() {
492	if (!m_rewindTimer.isActive()) {
493		return;
494	}
495	m_rewindTimer.stop();
496	setPaused(m_wasPaused);
497}
498
499void GameController::keyPressed(int key) {
500	int mappedKey = 1 << key;
501	m_activeKeys |= mappedKey;
502	if (!m_inputController->allowOpposing()) {
503		if ((m_activeKeys & 0x30) == 0x30) {
504			m_inactiveKeys |= mappedKey ^ 0x30;
505			m_activeKeys ^= mappedKey ^ 0x30;
506		}
507		if ((m_activeKeys & 0xC0) == 0xC0) {
508			m_inactiveKeys |= mappedKey ^ 0xC0;
509			m_activeKeys ^= mappedKey ^ 0xC0;
510		}
511	}
512	updateKeys();
513}
514
515void GameController::keyReleased(int key) {
516	int mappedKey = 1 << key;
517	m_activeKeys &= ~mappedKey;
518	if (!m_inputController->allowOpposing()) {
519		if (mappedKey & 0x30) {
520			m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
521			m_inactiveKeys &= ~0x30;
522		}
523		if (mappedKey & 0xC0) {
524			m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
525			m_inactiveKeys &= ~0xC0;
526		}
527	}
528	updateKeys();
529}
530
531void GameController::clearKeys() {
532	m_activeKeys = 0;
533	m_inactiveKeys = 0;
534	updateKeys();
535}
536
537void GameController::setAudioBufferSamples(int samples) {
538	threadInterrupt();
539	redoSamples(samples);
540	threadContinue();
541	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
542}
543
544void GameController::setFPSTarget(float fps) {
545	threadInterrupt();
546	m_fpsTarget = fps;
547	m_threadContext.fpsTarget = fps;
548	if (m_turbo && m_turboSpeed > 0) {
549		m_threadContext.fpsTarget *= m_turboSpeed;
550	}
551	redoSamples(m_audioProcessor->getBufferSamples());
552	threadContinue();
553}
554
555void GameController::setSkipBIOS(bool set) {
556	threadInterrupt();
557	m_threadContext.skipBios = set;
558	threadContinue();
559}
560
561void GameController::setUseBIOS(bool use) {
562	threadInterrupt();
563	m_useBios = use;
564	threadContinue();
565}
566
567void GameController::loadState(int slot) {
568	if (slot > 0) {
569		m_stateSlot = slot;
570	}
571	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
572		GameController* controller = static_cast<GameController*>(context->userData);
573		GBALoadState(context, context->stateDir, controller->m_stateSlot);
574		controller->stateLoaded(context);
575		controller->frameAvailable(controller->m_drawContext);
576	});
577}
578
579void GameController::saveState(int slot) {
580	if (slot > 0) {
581		m_stateSlot = slot;
582	}
583	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
584		GameController* controller = static_cast<GameController*>(context->userData);
585		GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
586	});
587}
588
589void GameController::setVideoSync(bool set) {
590	m_videoSync = set;
591	if (!m_turbo) {
592		threadInterrupt();
593		m_threadContext.sync.videoFrameWait = set;
594		threadContinue();
595	}
596}
597
598void GameController::setAudioSync(bool set) {
599	m_audioSync = set;
600	if (!m_turbo) {
601		threadInterrupt();
602		m_threadContext.sync.audioWait = set;
603		threadContinue();
604	}
605}
606
607void GameController::setFrameskip(int skip) {
608	m_threadContext.frameskip = skip;
609}
610
611void GameController::setVolume(int volume) {
612	threadInterrupt();
613	m_threadContext.volume = volume;
614	if (m_gameOpen) {
615		m_threadContext.gba->audio.masterVolume = volume;
616	}
617	threadContinue();
618}
619
620void GameController::setMute(bool mute) {
621	threadInterrupt();
622	m_threadContext.mute = mute;
623	if (m_gameOpen) {
624		m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
625	}
626	threadContinue();
627}
628
629void GameController::setTurbo(bool set, bool forced) {
630	if (m_turboForced && !forced) {
631		return;
632	}
633	if (m_turbo == set && m_turboForced == forced) {
634		// Don't interrupt the thread if we don't need to
635		return;
636	}
637	m_turbo = set;
638	m_turboForced = set && forced;
639	enableTurbo();
640}
641
642void GameController::setTurboSpeed(float ratio) {
643	m_turboSpeed = ratio;
644	enableTurbo();
645}
646
647void GameController::enableTurbo() {
648	threadInterrupt();
649	if (!m_turbo) {
650		m_threadContext.fpsTarget = m_fpsTarget;
651		m_threadContext.sync.audioWait = m_audioSync;
652		m_threadContext.sync.videoFrameWait = m_videoSync;
653	} else if (m_turboSpeed <= 0) {
654		m_threadContext.fpsTarget = m_fpsTarget;
655		m_threadContext.sync.audioWait = false;
656		m_threadContext.sync.videoFrameWait = false;
657	} else {
658		m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
659		m_threadContext.sync.audioWait = true;
660		m_threadContext.sync.videoFrameWait = false;
661	}
662	redoSamples(m_audioProcessor->getBufferSamples());
663	threadContinue();
664}
665
666void GameController::setAVStream(GBAAVStream* stream) {
667	threadInterrupt();
668	m_threadContext.stream = stream;
669	if (m_gameOpen) {
670		m_threadContext.gba->stream = stream;
671	}
672	threadContinue();
673}
674
675void GameController::clearAVStream() {
676	threadInterrupt();
677	m_threadContext.stream = nullptr;
678	if (m_gameOpen) {
679		m_threadContext.gba->stream = nullptr;
680	}
681	threadContinue();
682}
683
684#ifdef USE_PNG
685void GameController::screenshot() {
686	GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
687}
688#endif
689
690void GameController::reloadAudioDriver() {
691	QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
692	int samples = m_audioProcessor->getBufferSamples();
693	delete m_audioProcessor;
694	m_audioProcessor = AudioProcessor::create();
695	m_audioProcessor->setBufferSamples(samples);
696	m_audioProcessor->moveToThread(m_audioThread);
697	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
698	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
699	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
700	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
701	if (isLoaded()) {
702		m_audioProcessor->setInput(&m_threadContext);
703		QMetaObject::invokeMethod(m_audioProcessor, "start");
704	}
705}
706
707void GameController::setLuminanceValue(uint8_t value) {
708	m_luxValue = value;
709	value = std::max<int>(value - 0x16, 0);
710	m_luxLevel = 10;
711	for (int i = 0; i < 10; ++i) {
712		if (value < LUX_LEVELS[i]) {
713			m_luxLevel = i;
714			break;
715		}
716	}
717	emit luminanceValueChanged(m_luxValue);
718}
719
720void GameController::setLuminanceLevel(int level) {
721	int value = 0x16;
722	level = std::max(0, std::min(10, level));
723	if (level > 0) {
724		value += LUX_LEVELS[level - 1];
725	}
726	setLuminanceValue(value);
727}
728
729void GameController::setRealTime() {
730	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
731}
732
733void GameController::setFixedTime(const QDateTime& time) {
734	m_rtc.override = GameControllerRTC::FIXED;
735	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
736}
737
738void GameController::setFakeEpoch(const QDateTime& time) {
739	m_rtc.override = GameControllerRTC::FAKE_EPOCH;
740	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
741}
742
743void GameController::updateKeys() {
744	int activeKeys = m_activeKeys;
745	activeKeys |= m_activeButtons;
746	activeKeys &= ~m_inactiveKeys;
747	m_threadContext.activeKeys = activeKeys;
748}
749
750void GameController::redoSamples(int samples) {
751#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
752	float sampleRate = 0x8000;
753	float ratio;
754	if (m_threadContext.gba) {
755		sampleRate = m_threadContext.gba->audio.sampleRate;
756	}
757	ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
758	m_threadContext.audioBuffers = ceil(samples / ratio);
759#else
760	m_threadContext.audioBuffers = samples;
761#endif
762	if (m_threadContext.gba) {
763		GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
764	}
765	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
766}
767
768void GameController::setLogLevel(int levels) {
769	threadInterrupt();
770	m_logLevels = levels;
771	threadContinue();
772}
773
774void GameController::enableLogLevel(int levels) {
775	threadInterrupt();
776	m_logLevels |= levels;
777	threadContinue();
778}
779
780void GameController::disableLogLevel(int levels) {
781	threadInterrupt();
782	m_logLevels &= ~levels;
783	threadContinue();
784}
785
786void GameController::pollEvents() {
787	if (!m_inputController) {
788		return;
789	}
790
791	m_activeButtons = m_inputController->pollEvents();
792	updateKeys();
793}