all repos — mgba @ 4469a9a05f4be9a4c46f36de66398eb61f901c6e

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