all repos — mgba @ ca0fb2ede6714d31708a8d4e052f6c1a51af0ce1

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