all repos — mgba @ a0624b8b4fdad91e813ec840cd8fda8c18d85453

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
 12#include <QDateTime>
 13#include <QThread>
 14
 15#include <ctime>
 16
 17extern "C" {
 18#include "gba/audio.h"
 19#include "gba/gba.h"
 20#include "gba/serialize.h"
 21#include "gba/sharkport.h"
 22#include "gba/renderers/video-software.h"
 23#include "gba/supervisor/config.h"
 24#include "util/vfs.h"
 25}
 26
 27using namespace QGBA;
 28using namespace std;
 29
 30const int GameController::LUX_LEVELS[10] = { 5, 11, 18, 27, 42, 62, 84, 109, 139, 183 };
 31
 32GameController::GameController(QObject* parent)
 33	: QObject(parent)
 34	, m_drawContext(new uint32_t[256 * 256])
 35	, m_threadContext()
 36	, m_activeKeys(0)
 37	, m_inactiveKeys(0)
 38	, m_logLevels(0)
 39	, m_gameOpen(false)
 40	, m_audioThread(new QThread(this))
 41	, m_audioProcessor(AudioProcessor::create())
 42	, m_videoSync(VIDEO_SYNC)
 43	, m_audioSync(AUDIO_SYNC)
 44	, m_fpsTarget(-1)
 45	, m_turbo(false)
 46	, m_turboForced(false)
 47	, m_turboSpeed(-1)
 48	, m_inputController(nullptr)
 49	, m_multiplayer(nullptr)
 50	, m_stateSlot(1)
 51{
 52	m_renderer = new GBAVideoSoftwareRenderer;
 53	GBAVideoSoftwareRendererCreate(m_renderer);
 54	m_renderer->outputBuffer = (color_t*) m_drawContext;
 55	m_renderer->outputBufferStride = 256;
 56
 57	GBACheatDeviceCreate(&m_cheatDevice);
 58
 59	m_threadContext.state = THREAD_INITIALIZED;
 60	m_threadContext.debugger = 0;
 61	m_threadContext.frameskip = 0;
 62	m_threadContext.bios = 0;
 63	m_threadContext.renderer = &m_renderer->d;
 64	m_threadContext.userData = this;
 65	m_threadContext.rewindBufferCapacity = 0;
 66	m_threadContext.cheats = &m_cheatDevice;
 67	m_threadContext.logLevel = GBA_LOG_ALL;
 68
 69	m_lux.p = this;
 70	m_lux.sample = [] (GBALuminanceSource* context) {
 71		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 72		lux->value = 0xFF - lux->p->m_luxValue;
 73	};
 74
 75	m_lux.readLuminance = [] (GBALuminanceSource* context) {
 76		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 77		return lux->value;
 78	};
 79	setLuminanceLevel(0);
 80
 81	m_rtc.p = this;
 82	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
 83	m_rtc.sample = [] (GBARTCSource* context) { };
 84	m_rtc.unixTime = [] (GBARTCSource* context) -> time_t {
 85		GameControllerRTC* rtc = static_cast<GameControllerRTC*>(context);
 86		switch (rtc->override) {
 87		case GameControllerRTC::NO_OVERRIDE:
 88		default:
 89			return time(nullptr);
 90		case GameControllerRTC::FIXED:
 91			return rtc->value;
 92		case GameControllerRTC::FAKE_EPOCH:
 93			return rtc->value + rtc->p->m_threadContext.gba->video.frameCounter * (int64_t) VIDEO_TOTAL_LENGTH / GBA_ARM7TDMI_FREQUENCY;
 94		}
 95	};
 96
 97	m_threadContext.startCallback = [] (GBAThread* context) {
 98		GameController* controller = static_cast<GameController*>(context->userData);
 99		controller->m_audioProcessor->setInput(context);
100		context->gba->luminanceSource = &controller->m_lux;
101		context->gba->rtcSource = &controller->m_rtc;
102		context->gba->rumble = controller->m_inputController->rumble();
103		context->gba->rotationSource = controller->m_inputController->rotationSource();
104		controller->m_fpsTarget = context->fpsTarget;
105		controller->gameStarted(context);
106	};
107
108	m_threadContext.cleanCallback = [] (GBAThread* context) {
109		GameController* controller = static_cast<GameController*>(context->userData);
110		controller->gameStopped(context);
111	};
112
113	m_threadContext.frameCallback = [] (GBAThread* context) {
114		GameController* controller = static_cast<GameController*>(context->userData);
115		controller->m_pauseMutex.lock();
116		if (controller->m_pauseAfterFrame) {
117			GBAThreadPauseFromThread(context);
118			controller->m_pauseAfterFrame = false;
119			controller->gamePaused(&controller->m_threadContext);
120		}
121		controller->m_pauseMutex.unlock();
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(std::shared_ptr<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.reset();
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::openGame() {
251	m_gameOpen = true;
252
253	m_pauseAfterFrame = false;
254
255	if (m_turbo) {
256		m_threadContext.sync.videoFrameWait = false;
257		m_threadContext.sync.audioWait = false;
258	} else {
259		m_threadContext.sync.videoFrameWait = m_videoSync;
260		m_threadContext.sync.audioWait = m_audioSync;
261	}
262
263	m_threadContext.gameDir = 0;
264	m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
265	if (m_dirmode) {
266		m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
267		m_threadContext.stateDir = m_threadContext.gameDir;
268	} else {
269		m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
270#if USE_LIBZIP
271		if (!m_threadContext.gameDir) {
272			m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
273		}
274#endif
275#if USE_LZMA
276		if (!m_threadContext.gameDir) {
277			m_threadContext.gameDir = VDirOpen7z(m_threadContext.fname, 0);
278		}
279#endif
280	}
281
282	if (!m_bios.isNull() &&m_useBios) {
283		m_threadContext.bios = VFileOpen(m_bios.toLocal8Bit().constData(), O_RDONLY);
284	} else {
285		m_threadContext.bios = nullptr;
286	}
287
288	if (!m_patch.isNull()) {
289		m_threadContext.patch = VFileOpen(m_patch.toLocal8Bit().constData(), O_RDONLY);
290	}
291
292	m_inputController->recalibrateAxes();
293
294	if (!GBAThreadStart(&m_threadContext)) {
295		m_gameOpen = false;
296		emit gameFailed();
297	}
298}
299
300void GameController::loadBIOS(const QString& path) {
301	if (m_bios == path) {
302		return;
303	}
304	m_bios = path;
305	if (m_gameOpen) {
306		closeGame();
307		openGame();
308	}
309}
310
311void GameController::loadPatch(const QString& path) {
312	if (m_gameOpen) {
313		closeGame();
314		m_patch = path;
315		openGame();
316	} else {
317		m_patch = path;
318	}
319}
320
321void GameController::importSharkport(const QString& path) {
322	if (!m_gameOpen) {
323		return;
324	}
325	VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY);
326	if (!vf) {
327		return;
328	}
329	threadInterrupt();
330	GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
331	threadContinue();
332	vf->close(vf);
333}
334
335void GameController::exportSharkport(const QString& path) {
336	if (!m_gameOpen) {
337		return;
338	}
339	VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_WRONLY | O_CREAT | O_TRUNC);
340	if (!vf) {
341		return;
342	}
343	threadInterrupt();
344	GBASavedataExportSharkPort(m_threadContext.gba, vf);
345	threadContinue();
346	vf->close(vf);
347}
348
349void GameController::closeGame() {
350	if (!m_gameOpen) {
351		return;
352	}
353	if (GBAThreadIsPaused(&m_threadContext)) {
354		GBAThreadUnpause(&m_threadContext);
355	}
356	GBAThreadEnd(&m_threadContext);
357	GBAThreadJoin(&m_threadContext);
358	if (m_threadContext.fname) {
359		free(const_cast<char*>(m_threadContext.fname));
360		m_threadContext.fname = nullptr;
361	}
362
363	m_patch = QString();
364
365	for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
366		GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
367		GBACheatSetDeinit(set);
368		delete set;
369	}
370	GBACheatSetsClear(&m_cheatDevice.cheats);
371
372	m_gameOpen = false;
373	emit gameStopped(&m_threadContext);
374}
375
376void GameController::crashGame(const QString& crashMessage) {
377	closeGame();
378	emit gameCrashed(crashMessage);
379	emit gameStopped(&m_threadContext);
380}
381
382bool GameController::isPaused() {
383	if (!m_gameOpen) {
384		return false;
385	}
386	return GBAThreadIsPaused(&m_threadContext);
387}
388
389void GameController::setPaused(bool paused) {
390	if (!m_gameOpen || paused == GBAThreadIsPaused(&m_threadContext)) {
391		return;
392	}
393	if (paused) {
394		GBAThreadPause(&m_threadContext);
395		emit gamePaused(&m_threadContext);
396	} else {
397		GBAThreadUnpause(&m_threadContext);
398		emit gameUnpaused(&m_threadContext);
399	}
400}
401
402void GameController::reset() {
403	GBAThreadReset(&m_threadContext);
404}
405
406void GameController::threadInterrupt() {
407	if (m_gameOpen) {
408		GBAThreadInterrupt(&m_threadContext);
409	}
410}
411
412void GameController::threadContinue() {
413	if (m_gameOpen) {
414		GBAThreadContinue(&m_threadContext);
415	}
416}
417
418void GameController::frameAdvance() {
419	m_pauseMutex.lock();
420	m_pauseAfterFrame = true;
421	setPaused(false);
422	m_pauseMutex.unlock();
423}
424
425void GameController::setRewind(bool enable, int capacity, int interval) {
426	if (m_gameOpen) {
427		threadInterrupt();
428		GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
429		threadContinue();
430	} else {
431		if (enable) {
432			m_threadContext.rewindBufferInterval = interval;
433			m_threadContext.rewindBufferCapacity = capacity;
434		} else {
435			m_threadContext.rewindBufferInterval = 0;
436			m_threadContext.rewindBufferCapacity = 0;
437		}
438	}
439}
440
441void GameController::rewind(int states) {
442	threadInterrupt();
443	if (!states) {
444		GBARewindAll(&m_threadContext);
445	} else {
446		GBARewind(&m_threadContext, states);
447	}
448	threadContinue();
449	emit rewound(&m_threadContext);
450	emit frameAvailable(m_drawContext);
451}
452
453void GameController::startRewinding() {
454	threadInterrupt();
455	m_rewindTimer.start();
456}
457
458void GameController::stopRewinding() {
459	m_rewindTimer.stop();
460	threadContinue();
461}
462
463void GameController::keyPressed(int key) {
464	int mappedKey = 1 << key;
465	m_activeKeys |= mappedKey;
466	if (!m_inputController->allowOpposing()) {
467		if ((m_activeKeys & 0x30) == 0x30) {
468			m_inactiveKeys |= mappedKey ^ 0x30;
469			m_activeKeys ^= mappedKey ^ 0x30;
470		}
471		if ((m_activeKeys & 0xC0) == 0xC0) {
472			m_inactiveKeys |= mappedKey ^ 0xC0;
473			m_activeKeys ^= mappedKey ^ 0xC0;
474		}
475	}
476	updateKeys();
477}
478
479void GameController::keyReleased(int key) {
480	int mappedKey = 1 << key;
481	m_activeKeys &= ~mappedKey;
482	if (!m_inputController->allowOpposing()) {
483		if (mappedKey & 0x30) {
484			m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
485			m_inactiveKeys &= ~0x30;
486		}
487		if (mappedKey & 0xC0) {
488			m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
489			m_inactiveKeys &= ~0xC0;
490		}
491	}
492	updateKeys();
493}
494
495void GameController::clearKeys() {
496	m_activeKeys = 0;
497	m_inactiveKeys = 0;
498	updateKeys();
499}
500
501void GameController::setAudioBufferSamples(int samples) {
502	threadInterrupt();
503	redoSamples(samples);
504	threadContinue();
505	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
506}
507
508void GameController::setFPSTarget(float fps) {
509	threadInterrupt();
510	m_fpsTarget = fps;
511	m_threadContext.fpsTarget = fps;
512	if (m_turbo && m_turboSpeed > 0) {
513		m_threadContext.fpsTarget *= m_turboSpeed;
514	}
515	redoSamples(m_audioProcessor->getBufferSamples());
516	threadContinue();
517	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
518}
519
520void GameController::setSkipBIOS(bool set) {
521	threadInterrupt();
522	m_threadContext.skipBios = set;
523	threadContinue();
524}
525
526void GameController::setUseBIOS(bool use) {
527	threadInterrupt();
528	m_useBios = use;
529	threadContinue();
530}
531
532void GameController::loadState(int slot) {
533	if (slot > 0) {
534		m_stateSlot = slot;
535	}
536	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
537		GameController* controller = static_cast<GameController*>(context->userData);
538		GBALoadState(context, context->stateDir, controller->m_stateSlot);
539		controller->stateLoaded(context);
540		controller->frameAvailable(controller->m_drawContext);
541	});
542}
543
544void GameController::saveState(int slot) {
545	if (slot > 0) {
546		m_stateSlot = slot;
547	}
548	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
549		GameController* controller = static_cast<GameController*>(context->userData);
550		GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
551	});
552}
553
554void GameController::setVideoSync(bool set) {
555	m_videoSync = set;
556	if (!m_turbo) {
557		threadInterrupt();
558		m_threadContext.sync.videoFrameWait = set;
559		threadContinue();
560	}
561}
562
563void GameController::setAudioSync(bool set) {
564	m_audioSync = set;
565	if (!m_turbo) {
566		threadInterrupt();
567		m_threadContext.sync.audioWait = set;
568		threadContinue();
569	}
570}
571
572void GameController::setFrameskip(int skip) {
573	m_threadContext.frameskip = skip;
574}
575
576void GameController::setVolume(int volume) {
577	threadInterrupt();
578	m_threadContext.volume = volume;
579	if (m_gameOpen) {
580		m_threadContext.gba->audio.masterVolume = volume;
581	}
582	threadContinue();
583}
584
585void GameController::setMute(bool mute) {
586	threadInterrupt();
587	m_threadContext.mute = mute;
588	if (m_gameOpen) {
589		m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
590	}
591	threadContinue();
592}
593
594void GameController::setTurbo(bool set, bool forced) {
595	if (m_turboForced && !forced) {
596		return;
597	}
598	if (m_turbo == set && m_turboForced == forced) {
599		// Don't interrupt the thread if we don't need to
600		return;
601	}
602	m_turbo = set;
603	m_turboForced = set && forced;
604	enableTurbo();
605}
606
607void GameController::setTurboSpeed(float ratio) {
608	m_turboSpeed = ratio;
609	enableTurbo();
610}
611
612void GameController::enableTurbo() {
613	threadInterrupt();
614	if (!m_turbo) {
615		m_threadContext.fpsTarget = m_fpsTarget;
616		m_threadContext.sync.audioWait = m_audioSync;
617		m_threadContext.sync.videoFrameWait = m_videoSync;
618		redoSamples(m_audioProcessor->getBufferSamples());
619	} else if (m_turboSpeed <= 0) {
620		m_threadContext.sync.audioWait = false;
621		m_threadContext.sync.videoFrameWait = false;
622	} else {
623		m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
624		m_threadContext.sync.audioWait = true;
625		m_threadContext.sync.videoFrameWait = false;
626		redoSamples(m_audioProcessor->getBufferSamples());
627	}
628	threadContinue();
629}
630
631void GameController::setAVStream(GBAAVStream* stream) {
632	threadInterrupt();
633	m_threadContext.stream = stream;
634	if (m_gameOpen) {
635		m_threadContext.gba->stream = stream;
636	}
637	threadContinue();
638}
639
640void GameController::clearAVStream() {
641	threadInterrupt();
642	m_threadContext.stream = nullptr;
643	if (m_gameOpen) {
644		m_threadContext.gba->stream = nullptr;
645	}
646	threadContinue();
647}
648
649#ifdef USE_PNG
650void GameController::screenshot() {
651	GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
652}
653#endif
654
655void GameController::reloadAudioDriver() {
656	QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
657	int samples = m_audioProcessor->getBufferSamples();
658	delete m_audioProcessor;
659	m_audioProcessor = AudioProcessor::create();
660	m_audioProcessor->setBufferSamples(samples);
661	m_audioProcessor->moveToThread(m_audioThread);
662	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
663	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
664	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
665	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
666	if (isLoaded()) {
667		m_audioProcessor->setInput(&m_threadContext);
668		QMetaObject::invokeMethod(m_audioProcessor, "start");
669	}
670}
671
672void GameController::setLuminanceValue(uint8_t value) {
673	m_luxValue = value;
674	value = std::max<int>(value - 0x16, 0);
675	m_luxLevel = 10;
676	for (int i = 0; i < 10; ++i) {
677		if (value < LUX_LEVELS[i]) {
678			m_luxLevel = i;
679			break;
680		}
681	}
682	emit luminanceValueChanged(m_luxValue);
683}
684
685void GameController::setLuminanceLevel(int level) {
686	int value = 0x16;
687	level = std::max(0, std::min(10, level));
688	if (level > 0) {
689		value += LUX_LEVELS[level - 1];
690	}
691	setLuminanceValue(value);
692}
693
694void GameController::setRealTime() {
695	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
696}
697
698void GameController::setFixedTime(const QDateTime& time) {
699	m_rtc.override = GameControllerRTC::FIXED;
700	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
701}
702
703void GameController::setFakeEpoch(const QDateTime& time) {
704	m_rtc.override = GameControllerRTC::FAKE_EPOCH;
705	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
706}
707
708void GameController::updateKeys() {
709	int activeKeys = m_activeKeys;
710	activeKeys |= m_activeButtons;
711	activeKeys &= ~m_inactiveKeys;
712	m_threadContext.activeKeys = activeKeys;
713}
714
715void GameController::redoSamples(int samples) {
716#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
717	float sampleRate = 0x8000;
718	float ratio;
719	if (m_threadContext.gba) {
720		sampleRate = m_threadContext.gba->audio.sampleRate;
721	}
722	ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
723	m_threadContext.audioBuffers = ceil(samples / ratio);
724#else
725	m_threadContext.audioBuffers = samples;
726#endif
727	if (m_threadContext.gba) {
728		GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
729	}
730}
731
732void GameController::setLogLevel(int levels) {
733	threadInterrupt();
734	m_logLevels = levels;
735	threadContinue();
736}
737
738void GameController::enableLogLevel(int levels) {
739	threadInterrupt();
740	m_logLevels |= levels;
741	threadContinue();
742}
743
744void GameController::disableLogLevel(int levels) {
745	threadInterrupt();
746	m_logLevels &= ~levels;
747	threadContinue();
748}
749
750void GameController::pollEvents() {
751	if (!m_inputController) {
752		return;
753	}
754
755	m_activeButtons = m_inputController->pollEvents();
756	updateKeys();
757}