all repos — mgba @ 1a4ed6fe5e3c35d2fb8d6df95ec2dcb80ee53b96

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::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 = VFileDevice::open(m_bios, O_RDONLY);
284	} else {
285		m_threadContext.bios = nullptr;
286	}
287
288	if (!m_patch.isNull()) {
289		m_threadContext.patch = VFileDevice::open(m_patch, 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 = VFileDevice::open(path, 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 = VFileDevice::open(path, 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	m_rewindTimer.stop();
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 || m_rewindTimer.isActive() || 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	if (m_rewindTimer.isActive()) {
421		return;
422	}
423	if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
424		setPaused(false);
425	}
426}
427
428void GameController::setRewind(bool enable, int capacity, int interval) {
429	if (m_gameOpen) {
430		threadInterrupt();
431		GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
432		threadContinue();
433	} else {
434		if (enable) {
435			m_threadContext.rewindBufferInterval = interval;
436			m_threadContext.rewindBufferCapacity = capacity;
437		} else {
438			m_threadContext.rewindBufferInterval = 0;
439			m_threadContext.rewindBufferCapacity = 0;
440		}
441	}
442}
443
444void GameController::rewind(int states) {
445	threadInterrupt();
446	if (!states) {
447		GBARewindAll(&m_threadContext);
448	} else {
449		GBARewind(&m_threadContext, states);
450	}
451	threadContinue();
452	emit rewound(&m_threadContext);
453	emit frameAvailable(m_drawContext);
454}
455
456void GameController::startRewinding() {
457	if (!m_gameOpen || m_rewindTimer.isActive()) {
458		return;
459	}
460	m_wasPaused = isPaused();
461	setPaused(true);
462	m_rewindTimer.start();
463}
464
465void GameController::stopRewinding() {
466	m_rewindTimer.stop();
467	setPaused(m_wasPaused);
468}
469
470void GameController::keyPressed(int key) {
471	int mappedKey = 1 << key;
472	m_activeKeys |= mappedKey;
473	if (!m_inputController->allowOpposing()) {
474		if ((m_activeKeys & 0x30) == 0x30) {
475			m_inactiveKeys |= mappedKey ^ 0x30;
476			m_activeKeys ^= mappedKey ^ 0x30;
477		}
478		if ((m_activeKeys & 0xC0) == 0xC0) {
479			m_inactiveKeys |= mappedKey ^ 0xC0;
480			m_activeKeys ^= mappedKey ^ 0xC0;
481		}
482	}
483	updateKeys();
484}
485
486void GameController::keyReleased(int key) {
487	int mappedKey = 1 << key;
488	m_activeKeys &= ~mappedKey;
489	if (!m_inputController->allowOpposing()) {
490		if (mappedKey & 0x30) {
491			m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
492			m_inactiveKeys &= ~0x30;
493		}
494		if (mappedKey & 0xC0) {
495			m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
496			m_inactiveKeys &= ~0xC0;
497		}
498	}
499	updateKeys();
500}
501
502void GameController::clearKeys() {
503	m_activeKeys = 0;
504	m_inactiveKeys = 0;
505	updateKeys();
506}
507
508void GameController::setAudioBufferSamples(int samples) {
509	threadInterrupt();
510	redoSamples(samples);
511	threadContinue();
512	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
513}
514
515void GameController::setFPSTarget(float fps) {
516	threadInterrupt();
517	m_fpsTarget = fps;
518	m_threadContext.fpsTarget = fps;
519	if (m_turbo && m_turboSpeed > 0) {
520		m_threadContext.fpsTarget *= m_turboSpeed;
521	}
522	redoSamples(m_audioProcessor->getBufferSamples());
523	threadContinue();
524}
525
526void GameController::setSkipBIOS(bool set) {
527	threadInterrupt();
528	m_threadContext.skipBios = set;
529	threadContinue();
530}
531
532void GameController::setUseBIOS(bool use) {
533	threadInterrupt();
534	m_useBios = use;
535	threadContinue();
536}
537
538void GameController::loadState(int slot) {
539	if (slot > 0) {
540		m_stateSlot = slot;
541	}
542	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
543		GameController* controller = static_cast<GameController*>(context->userData);
544		GBALoadState(context, context->stateDir, controller->m_stateSlot);
545		controller->stateLoaded(context);
546		controller->frameAvailable(controller->m_drawContext);
547	});
548}
549
550void GameController::saveState(int slot) {
551	if (slot > 0) {
552		m_stateSlot = slot;
553	}
554	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
555		GameController* controller = static_cast<GameController*>(context->userData);
556		GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
557	});
558}
559
560void GameController::setVideoSync(bool set) {
561	m_videoSync = set;
562	if (!m_turbo) {
563		threadInterrupt();
564		m_threadContext.sync.videoFrameWait = set;
565		threadContinue();
566	}
567}
568
569void GameController::setAudioSync(bool set) {
570	m_audioSync = set;
571	if (!m_turbo) {
572		threadInterrupt();
573		m_threadContext.sync.audioWait = set;
574		threadContinue();
575	}
576}
577
578void GameController::setFrameskip(int skip) {
579	m_threadContext.frameskip = skip;
580}
581
582void GameController::setVolume(int volume) {
583	threadInterrupt();
584	m_threadContext.volume = volume;
585	if (m_gameOpen) {
586		m_threadContext.gba->audio.masterVolume = volume;
587	}
588	threadContinue();
589}
590
591void GameController::setMute(bool mute) {
592	threadInterrupt();
593	m_threadContext.mute = mute;
594	if (m_gameOpen) {
595		m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
596	}
597	threadContinue();
598}
599
600void GameController::setTurbo(bool set, bool forced) {
601	if (m_turboForced && !forced) {
602		return;
603	}
604	if (m_turbo == set && m_turboForced == forced) {
605		// Don't interrupt the thread if we don't need to
606		return;
607	}
608	m_turbo = set;
609	m_turboForced = set && forced;
610	enableTurbo();
611}
612
613void GameController::setTurboSpeed(float ratio) {
614	m_turboSpeed = ratio;
615	enableTurbo();
616}
617
618void GameController::enableTurbo() {
619	threadInterrupt();
620	if (!m_turbo) {
621		m_threadContext.fpsTarget = m_fpsTarget;
622		m_threadContext.sync.audioWait = m_audioSync;
623		m_threadContext.sync.videoFrameWait = m_videoSync;
624	} else if (m_turboSpeed <= 0) {
625		m_threadContext.fpsTarget = m_fpsTarget;
626		m_threadContext.sync.audioWait = false;
627		m_threadContext.sync.videoFrameWait = false;
628	} else {
629		m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
630		m_threadContext.sync.audioWait = true;
631		m_threadContext.sync.videoFrameWait = false;
632	}
633	redoSamples(m_audioProcessor->getBufferSamples());
634	threadContinue();
635}
636
637void GameController::setAVStream(GBAAVStream* stream) {
638	threadInterrupt();
639	m_threadContext.stream = stream;
640	if (m_gameOpen) {
641		m_threadContext.gba->stream = stream;
642	}
643	threadContinue();
644}
645
646void GameController::clearAVStream() {
647	threadInterrupt();
648	m_threadContext.stream = nullptr;
649	if (m_gameOpen) {
650		m_threadContext.gba->stream = nullptr;
651	}
652	threadContinue();
653}
654
655#ifdef USE_PNG
656void GameController::screenshot() {
657	GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
658}
659#endif
660
661void GameController::reloadAudioDriver() {
662	QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
663	int samples = m_audioProcessor->getBufferSamples();
664	delete m_audioProcessor;
665	m_audioProcessor = AudioProcessor::create();
666	m_audioProcessor->setBufferSamples(samples);
667	m_audioProcessor->moveToThread(m_audioThread);
668	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
669	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
670	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
671	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
672	if (isLoaded()) {
673		m_audioProcessor->setInput(&m_threadContext);
674		QMetaObject::invokeMethod(m_audioProcessor, "start");
675	}
676}
677
678void GameController::setLuminanceValue(uint8_t value) {
679	m_luxValue = value;
680	value = std::max<int>(value - 0x16, 0);
681	m_luxLevel = 10;
682	for (int i = 0; i < 10; ++i) {
683		if (value < LUX_LEVELS[i]) {
684			m_luxLevel = i;
685			break;
686		}
687	}
688	emit luminanceValueChanged(m_luxValue);
689}
690
691void GameController::setLuminanceLevel(int level) {
692	int value = 0x16;
693	level = std::max(0, std::min(10, level));
694	if (level > 0) {
695		value += LUX_LEVELS[level - 1];
696	}
697	setLuminanceValue(value);
698}
699
700void GameController::setRealTime() {
701	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
702}
703
704void GameController::setFixedTime(const QDateTime& time) {
705	m_rtc.override = GameControllerRTC::FIXED;
706	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
707}
708
709void GameController::setFakeEpoch(const QDateTime& time) {
710	m_rtc.override = GameControllerRTC::FAKE_EPOCH;
711	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
712}
713
714void GameController::updateKeys() {
715	int activeKeys = m_activeKeys;
716	activeKeys |= m_activeButtons;
717	activeKeys &= ~m_inactiveKeys;
718	m_threadContext.activeKeys = activeKeys;
719}
720
721void GameController::redoSamples(int samples) {
722#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
723	float sampleRate = 0x8000;
724	float ratio;
725	if (m_threadContext.gba) {
726		sampleRate = m_threadContext.gba->audio.sampleRate;
727	}
728	ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
729	m_threadContext.audioBuffers = ceil(samples / ratio);
730#else
731	m_threadContext.audioBuffers = samples;
732#endif
733	if (m_threadContext.gba) {
734		GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
735	}
736	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
737}
738
739void GameController::setLogLevel(int levels) {
740	threadInterrupt();
741	m_logLevels = levels;
742	threadContinue();
743}
744
745void GameController::enableLogLevel(int levels) {
746	threadInterrupt();
747	m_logLevels |= levels;
748	threadContinue();
749}
750
751void GameController::disableLogLevel(int levels) {
752	threadInterrupt();
753	m_logLevels &= ~levels;
754	threadContinue();
755}
756
757void GameController::pollEvents() {
758	if (!m_inputController) {
759		return;
760	}
761
762	m_activeButtons = m_inputController->pollEvents();
763	updateKeys();
764}