all repos — mgba @ 041b58ebced4a8d4481f2c6f335a2efdf4e30dd1

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