all repos — mgba @ 910ff621b32c461a6210aca06f94f38de95b76b3

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