all repos — mgba @ 67e13114ef407dfe6a7cc27fc88ddcf4301a6859

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_wasPaused(false)
 50	, m_inputController(nullptr)
 51	, m_multiplayer(nullptr)
 52	, m_stateSlot(1)
 53{
 54	m_renderer = new GBAVideoSoftwareRenderer;
 55	GBAVideoSoftwareRendererCreate(m_renderer);
 56	m_renderer->outputBuffer = (color_t*) m_drawContext;
 57	m_renderer->outputBufferStride = 256;
 58
 59	GBACheatDeviceCreate(&m_cheatDevice);
 60
 61	m_threadContext.state = THREAD_INITIALIZED;
 62	m_threadContext.debugger = 0;
 63	m_threadContext.frameskip = 0;
 64	m_threadContext.bios = 0;
 65	m_threadContext.renderer = &m_renderer->d;
 66	m_threadContext.userData = this;
 67	m_threadContext.rewindBufferCapacity = 0;
 68	m_threadContext.cheats = &m_cheatDevice;
 69	m_threadContext.logLevel = GBA_LOG_ALL;
 70
 71	m_lux.p = this;
 72	m_lux.sample = [] (GBALuminanceSource* context) {
 73		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 74		lux->value = 0xFF - lux->p->m_luxValue;
 75	};
 76
 77	m_lux.readLuminance = [] (GBALuminanceSource* context) {
 78		GameControllerLux* lux = static_cast<GameControllerLux*>(context);
 79		return lux->value;
 80	};
 81	setLuminanceLevel(0);
 82
 83	m_rtc.p = this;
 84	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
 85	m_rtc.sample = [] (GBARTCSource* context) { };
 86	m_rtc.unixTime = [] (GBARTCSource* context) -> time_t {
 87		GameControllerRTC* rtc = static_cast<GameControllerRTC*>(context);
 88		switch (rtc->override) {
 89		case GameControllerRTC::NO_OVERRIDE:
 90		default:
 91			return time(nullptr);
 92		case GameControllerRTC::FIXED:
 93			return rtc->value;
 94		case GameControllerRTC::FAKE_EPOCH:
 95			return rtc->value + rtc->p->m_threadContext.gba->video.frameCounter * (int64_t) VIDEO_TOTAL_LENGTH / GBA_ARM7TDMI_FREQUENCY;
 96		}
 97	};
 98
 99	m_threadContext.startCallback = [] (GBAThread* context) {
100		GameController* controller = static_cast<GameController*>(context->userData);
101		controller->m_audioProcessor->setInput(context);
102		context->gba->luminanceSource = &controller->m_lux;
103		context->gba->rtcSource = &controller->m_rtc;
104		context->gba->rumble = controller->m_inputController->rumble();
105		context->gba->rotationSource = controller->m_inputController->rotationSource();
106		controller->m_fpsTarget = context->fpsTarget;
107		controller->gameStarted(context);
108	};
109
110	m_threadContext.cleanCallback = [] (GBAThread* context) {
111		GameController* controller = static_cast<GameController*>(context->userData);
112		controller->gameStopped(context);
113	};
114
115	m_threadContext.frameCallback = [] (GBAThread* context) {
116		GameController* controller = static_cast<GameController*>(context->userData);
117		controller->m_pauseMutex.lock();
118		if (controller->m_pauseAfterFrame) {
119			GBAThreadPauseFromThread(context);
120			controller->m_pauseAfterFrame = false;
121			controller->gamePaused(&controller->m_threadContext);
122		}
123		controller->m_pauseMutex.unlock();
124		if (GBASyncDrawingFrame(&controller->m_threadContext.sync)) {
125			controller->frameAvailable(controller->m_drawContext);
126		} else {
127			controller->frameAvailable(nullptr);
128		}
129	};
130
131	m_threadContext.logHandler = [] (GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
132		static const char* stubMessage = "Stub software interrupt";
133		if (!context) {
134			return;
135		}
136		GameController* controller = static_cast<GameController*>(context->userData);
137		if (level == GBA_LOG_STUB && strncmp(stubMessage, format, strlen(stubMessage)) == 0) {
138			va_list argc;
139			va_copy(argc, args);
140			int immediate = va_arg(argc, int);
141			controller->unimplementedBiosCall(immediate);
142		}
143		if (level == GBA_LOG_FATAL) {
144			QMetaObject::invokeMethod(controller, "crashGame", Q_ARG(const QString&, QString().vsprintf(format, args)));
145		} else if (!(controller->m_logLevels & level)) {
146			return;
147		}
148		QString message(QString().vsprintf(format, args));
149		if (level == GBA_LOG_STATUS) {
150			controller->statusPosted(message);
151		}
152		controller->postLog(level, message);
153	};
154
155	connect(&m_rewindTimer, &QTimer::timeout, [this]() {
156		GBARewind(&m_threadContext, 1);
157		emit rewound(&m_threadContext);
158		emit frameAvailable(m_drawContext);
159	});
160	m_rewindTimer.setInterval(100);
161
162	m_audioThread->start(QThread::TimeCriticalPriority);
163	m_audioProcessor->moveToThread(m_audioThread);
164	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
165	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
166	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
167	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
168	connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(pollEvents()));
169}
170
171GameController::~GameController() {
172	m_audioThread->quit();
173	m_audioThread->wait();
174	disconnect();
175	clearMultiplayerController();
176	closeGame();
177	GBACheatDeviceDestroy(&m_cheatDevice);
178	delete m_renderer;
179	delete[] m_drawContext;
180}
181
182void GameController::setMultiplayerController(MultiplayerController* controller) {
183	if (controller == m_multiplayer) {
184		return;
185	}
186	clearMultiplayerController();
187	m_multiplayer = controller;
188	controller->attachGame(this);
189}
190
191void GameController::clearMultiplayerController() {
192	if (!m_multiplayer) {
193		return;
194	}
195	m_multiplayer->detachGame(this);
196	m_multiplayer = nullptr;
197}
198
199void GameController::setOverride(const GBACartridgeOverride& override) {
200	m_threadContext.override = override;
201	m_threadContext.hasOverride = true;
202}
203
204void GameController::setOptions(const GBAOptions* opts) {
205	setFrameskip(opts->frameskip);
206	setAudioSync(opts->audioSync);
207	setVideoSync(opts->videoSync);
208	setSkipBIOS(opts->skipBios);
209	setUseBIOS(opts->useBios);
210	setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
211	setVolume(opts->volume);
212	setMute(opts->mute);
213
214	threadInterrupt();
215	m_threadContext.idleOptimization = opts->idleOptimization;
216	threadContinue();
217}
218
219#ifdef USE_GDB_STUB
220ARMDebugger* GameController::debugger() {
221	return m_threadContext.debugger;
222}
223
224void GameController::setDebugger(ARMDebugger* debugger) {
225	threadInterrupt();
226	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
227		GBADetachDebugger(m_threadContext.gba);
228	}
229	m_threadContext.debugger = debugger;
230	if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
231		GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
232	}
233	threadContinue();
234}
235#endif
236
237void GameController::loadGame(const QString& path, bool dirmode) {
238	closeGame();
239	if (!dirmode) {
240		QFile file(path);
241		if (!file.open(QIODevice::ReadOnly)) {
242			return;
243		}
244		file.close();
245	}
246
247	m_fname = path;
248	m_dirmode = dirmode;
249	openGame();
250}
251
252void GameController::openGame() {
253	m_gameOpen = true;
254
255	m_pauseAfterFrame = false;
256
257	if (m_turbo) {
258		m_threadContext.sync.videoFrameWait = false;
259		m_threadContext.sync.audioWait = false;
260	} else {
261		m_threadContext.sync.videoFrameWait = m_videoSync;
262		m_threadContext.sync.audioWait = m_audioSync;
263	}
264
265	m_threadContext.gameDir = 0;
266	m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
267	if (m_dirmode) {
268		m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
269		m_threadContext.stateDir = m_threadContext.gameDir;
270	} else {
271		m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
272#if USE_LIBZIP
273		if (!m_threadContext.gameDir) {
274			m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
275		}
276#endif
277#if USE_LZMA
278		if (!m_threadContext.gameDir) {
279			m_threadContext.gameDir = VDirOpen7z(m_threadContext.fname, 0);
280		}
281#endif
282	}
283
284	if (!m_bios.isNull() &&m_useBios) {
285		m_threadContext.bios = VFileDevice::open(m_bios, O_RDONLY);
286	} else {
287		m_threadContext.bios = nullptr;
288	}
289
290	if (!m_patch.isNull()) {
291		m_threadContext.patch = VFileDevice::open(m_patch, O_RDONLY);
292	}
293
294	m_inputController->recalibrateAxes();
295
296	if (!GBAThreadStart(&m_threadContext)) {
297		m_gameOpen = false;
298		emit gameFailed();
299	}
300}
301
302void GameController::loadBIOS(const QString& path) {
303	if (m_bios == path) {
304		return;
305	}
306	m_bios = path;
307	if (m_gameOpen) {
308		closeGame();
309		openGame();
310	}
311}
312
313void GameController::loadPatch(const QString& path) {
314	if (m_gameOpen) {
315		closeGame();
316		m_patch = path;
317		openGame();
318	} else {
319		m_patch = path;
320	}
321}
322
323void GameController::importSharkport(const QString& path) {
324	if (!m_gameOpen) {
325		return;
326	}
327	VFile* vf = VFileDevice::open(path, O_RDONLY);
328	if (!vf) {
329		return;
330	}
331	threadInterrupt();
332	GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
333	threadContinue();
334	vf->close(vf);
335}
336
337void GameController::exportSharkport(const QString& path) {
338	if (!m_gameOpen) {
339		return;
340	}
341	VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
342	if (!vf) {
343		return;
344	}
345	threadInterrupt();
346	GBASavedataExportSharkPort(m_threadContext.gba, vf);
347	threadContinue();
348	vf->close(vf);
349}
350
351void GameController::closeGame() {
352	if (!m_gameOpen) {
353		return;
354	}
355	m_rewindTimer.stop();
356	if (GBAThreadIsPaused(&m_threadContext)) {
357		GBAThreadUnpause(&m_threadContext);
358	}
359	GBAThreadEnd(&m_threadContext);
360	GBAThreadJoin(&m_threadContext);
361	if (m_threadContext.fname) {
362		free(const_cast<char*>(m_threadContext.fname));
363		m_threadContext.fname = nullptr;
364	}
365
366	m_patch = QString();
367
368	for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
369		GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
370		GBACheatSetDeinit(set);
371		delete set;
372	}
373	GBACheatSetsClear(&m_cheatDevice.cheats);
374
375	m_gameOpen = false;
376	emit gameStopped(&m_threadContext);
377}
378
379void GameController::crashGame(const QString& crashMessage) {
380	closeGame();
381	emit gameCrashed(crashMessage);
382	emit gameStopped(&m_threadContext);
383}
384
385bool GameController::isPaused() {
386	if (!m_gameOpen) {
387		return false;
388	}
389	return GBAThreadIsPaused(&m_threadContext);
390}
391
392void GameController::setPaused(bool paused) {
393	if (!m_gameOpen || m_rewindTimer.isActive() || paused == GBAThreadIsPaused(&m_threadContext)) {
394		return;
395	}
396	if (paused) {
397		GBAThreadPause(&m_threadContext);
398		emit gamePaused(&m_threadContext);
399	} else {
400		GBAThreadUnpause(&m_threadContext);
401		emit gameUnpaused(&m_threadContext);
402	}
403}
404
405void GameController::reset() {
406	GBAThreadReset(&m_threadContext);
407}
408
409void GameController::threadInterrupt() {
410	if (m_gameOpen) {
411		GBAThreadInterrupt(&m_threadContext);
412	}
413}
414
415void GameController::threadContinue() {
416	if (m_gameOpen) {
417		GBAThreadContinue(&m_threadContext);
418	}
419}
420
421void GameController::frameAdvance() {
422	m_pauseMutex.lock();
423	m_pauseAfterFrame = true;
424	setPaused(false);
425	m_pauseMutex.unlock();
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) {
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	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
525}
526
527void GameController::setSkipBIOS(bool set) {
528	threadInterrupt();
529	m_threadContext.skipBios = set;
530	threadContinue();
531}
532
533void GameController::setUseBIOS(bool use) {
534	threadInterrupt();
535	m_useBios = use;
536	threadContinue();
537}
538
539void GameController::loadState(int slot) {
540	if (slot > 0) {
541		m_stateSlot = slot;
542	}
543	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
544		GameController* controller = static_cast<GameController*>(context->userData);
545		GBALoadState(context, context->stateDir, controller->m_stateSlot);
546		controller->stateLoaded(context);
547		controller->frameAvailable(controller->m_drawContext);
548	});
549}
550
551void GameController::saveState(int slot) {
552	if (slot > 0) {
553		m_stateSlot = slot;
554	}
555	GBARunOnThread(&m_threadContext, [](GBAThread* context) {
556		GameController* controller = static_cast<GameController*>(context->userData);
557		GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
558	});
559}
560
561void GameController::setVideoSync(bool set) {
562	m_videoSync = set;
563	if (!m_turbo) {
564		threadInterrupt();
565		m_threadContext.sync.videoFrameWait = set;
566		threadContinue();
567	}
568}
569
570void GameController::setAudioSync(bool set) {
571	m_audioSync = set;
572	if (!m_turbo) {
573		threadInterrupt();
574		m_threadContext.sync.audioWait = set;
575		threadContinue();
576	}
577}
578
579void GameController::setFrameskip(int skip) {
580	m_threadContext.frameskip = skip;
581}
582
583void GameController::setVolume(int volume) {
584	threadInterrupt();
585	m_threadContext.volume = volume;
586	if (m_gameOpen) {
587		m_threadContext.gba->audio.masterVolume = volume;
588	}
589	threadContinue();
590}
591
592void GameController::setMute(bool mute) {
593	threadInterrupt();
594	m_threadContext.mute = mute;
595	if (m_gameOpen) {
596		m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
597	}
598	threadContinue();
599}
600
601void GameController::setTurbo(bool set, bool forced) {
602	if (m_turboForced && !forced) {
603		return;
604	}
605	if (m_turbo == set && m_turboForced == forced) {
606		// Don't interrupt the thread if we don't need to
607		return;
608	}
609	m_turbo = set;
610	m_turboForced = set && forced;
611	enableTurbo();
612}
613
614void GameController::setTurboSpeed(float ratio) {
615	m_turboSpeed = ratio;
616	enableTurbo();
617}
618
619void GameController::enableTurbo() {
620	threadInterrupt();
621	if (!m_turbo) {
622		m_threadContext.fpsTarget = m_fpsTarget;
623		m_threadContext.sync.audioWait = m_audioSync;
624		m_threadContext.sync.videoFrameWait = m_videoSync;
625		redoSamples(m_audioProcessor->getBufferSamples());
626	} else if (m_turboSpeed <= 0) {
627		m_threadContext.sync.audioWait = false;
628		m_threadContext.sync.videoFrameWait = false;
629	} else {
630		m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
631		m_threadContext.sync.audioWait = true;
632		m_threadContext.sync.videoFrameWait = false;
633		redoSamples(m_audioProcessor->getBufferSamples());
634	}
635	threadContinue();
636}
637
638void GameController::setAVStream(GBAAVStream* stream) {
639	threadInterrupt();
640	m_threadContext.stream = stream;
641	if (m_gameOpen) {
642		m_threadContext.gba->stream = stream;
643	}
644	threadContinue();
645}
646
647void GameController::clearAVStream() {
648	threadInterrupt();
649	m_threadContext.stream = nullptr;
650	if (m_gameOpen) {
651		m_threadContext.gba->stream = nullptr;
652	}
653	threadContinue();
654}
655
656#ifdef USE_PNG
657void GameController::screenshot() {
658	GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
659}
660#endif
661
662void GameController::reloadAudioDriver() {
663	QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
664	int samples = m_audioProcessor->getBufferSamples();
665	delete m_audioProcessor;
666	m_audioProcessor = AudioProcessor::create();
667	m_audioProcessor->setBufferSamples(samples);
668	m_audioProcessor->moveToThread(m_audioThread);
669	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
670	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
671	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
672	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
673	if (isLoaded()) {
674		m_audioProcessor->setInput(&m_threadContext);
675		QMetaObject::invokeMethod(m_audioProcessor, "start");
676	}
677}
678
679void GameController::setLuminanceValue(uint8_t value) {
680	m_luxValue = value;
681	value = std::max<int>(value - 0x16, 0);
682	m_luxLevel = 10;
683	for (int i = 0; i < 10; ++i) {
684		if (value < LUX_LEVELS[i]) {
685			m_luxLevel = i;
686			break;
687		}
688	}
689	emit luminanceValueChanged(m_luxValue);
690}
691
692void GameController::setLuminanceLevel(int level) {
693	int value = 0x16;
694	level = std::max(0, std::min(10, level));
695	if (level > 0) {
696		value += LUX_LEVELS[level - 1];
697	}
698	setLuminanceValue(value);
699}
700
701void GameController::setRealTime() {
702	m_rtc.override = GameControllerRTC::NO_OVERRIDE;
703}
704
705void GameController::setFixedTime(const QDateTime& time) {
706	m_rtc.override = GameControllerRTC::FIXED;
707	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
708}
709
710void GameController::setFakeEpoch(const QDateTime& time) {
711	m_rtc.override = GameControllerRTC::FAKE_EPOCH;
712	m_rtc.value = time.toMSecsSinceEpoch() / 1000;
713}
714
715void GameController::updateKeys() {
716	int activeKeys = m_activeKeys;
717	activeKeys |= m_activeButtons;
718	activeKeys &= ~m_inactiveKeys;
719	m_threadContext.activeKeys = activeKeys;
720}
721
722void GameController::redoSamples(int samples) {
723#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
724	float sampleRate = 0x8000;
725	float ratio;
726	if (m_threadContext.gba) {
727		sampleRate = m_threadContext.gba->audio.sampleRate;
728	}
729	ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
730	m_threadContext.audioBuffers = ceil(samples / ratio);
731#else
732	m_threadContext.audioBuffers = samples;
733#endif
734	if (m_threadContext.gba) {
735		GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
736	}
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}