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::bootBIOS() {
251 closeGame();
252 m_fname = QString();
253 m_dirmode = false;
254 openGame(true);
255}
256
257void GameController::openGame(bool biosOnly) {
258 if (biosOnly && (!m_useBios || m_bios.isNull())) {
259 return;
260 }
261
262 m_gameOpen = true;
263
264 m_pauseAfterFrame = false;
265
266 if (m_turbo) {
267 m_threadContext.sync.videoFrameWait = false;
268 m_threadContext.sync.audioWait = false;
269 } else {
270 m_threadContext.sync.videoFrameWait = m_videoSync;
271 m_threadContext.sync.audioWait = m_audioSync;
272 }
273
274 m_threadContext.gameDir = 0;
275 m_threadContext.bootBios = biosOnly;
276 if (biosOnly) {
277 m_threadContext.fname = nullptr;
278 } else {
279 m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
280 if (m_dirmode) {
281 m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
282 m_threadContext.stateDir = m_threadContext.gameDir;
283 } else {
284 m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
285#if USE_LIBZIP
286 if (!m_threadContext.gameDir) {
287 m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
288 }
289#endif
290#if USE_LZMA
291 if (!m_threadContext.gameDir) {
292 m_threadContext.gameDir = VDirOpen7z(m_threadContext.fname, 0);
293 }
294#endif
295 }
296 }
297
298 if (!m_bios.isNull() && m_useBios) {
299 m_threadContext.bios = VFileDevice::open(m_bios, O_RDONLY);
300 } else {
301 m_threadContext.bios = nullptr;
302 }
303
304 if (!m_patch.isNull()) {
305 m_threadContext.patch = VFileDevice::open(m_patch, O_RDONLY);
306 }
307
308 m_inputController->recalibrateAxes();
309
310 if (!GBAThreadStart(&m_threadContext)) {
311 m_gameOpen = false;
312 emit gameFailed();
313 }
314}
315
316void GameController::loadBIOS(const QString& path) {
317 if (m_bios == path) {
318 return;
319 }
320 m_bios = path;
321 if (m_gameOpen) {
322 closeGame();
323 openGame();
324 }
325}
326
327void GameController::loadPatch(const QString& path) {
328 if (m_gameOpen) {
329 closeGame();
330 m_patch = path;
331 openGame();
332 } else {
333 m_patch = path;
334 }
335}
336
337void GameController::importSharkport(const QString& path) {
338 if (!m_gameOpen) {
339 return;
340 }
341 VFile* vf = VFileDevice::open(path, O_RDONLY);
342 if (!vf) {
343 return;
344 }
345 threadInterrupt();
346 GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
347 threadContinue();
348 vf->close(vf);
349}
350
351void GameController::exportSharkport(const QString& path) {
352 if (!m_gameOpen) {
353 return;
354 }
355 VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
356 if (!vf) {
357 return;
358 }
359 threadInterrupt();
360 GBASavedataExportSharkPort(m_threadContext.gba, vf);
361 threadContinue();
362 vf->close(vf);
363}
364
365void GameController::closeGame() {
366 if (!m_gameOpen) {
367 return;
368 }
369 m_rewindTimer.stop();
370 if (GBAThreadIsPaused(&m_threadContext)) {
371 GBAThreadUnpause(&m_threadContext);
372 }
373 GBAThreadEnd(&m_threadContext);
374 GBAThreadJoin(&m_threadContext);
375 if (m_threadContext.fname) {
376 free(const_cast<char*>(m_threadContext.fname));
377 m_threadContext.fname = nullptr;
378 }
379
380 m_patch = QString();
381
382 for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
383 GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
384 GBACheatSetDeinit(set);
385 delete set;
386 }
387 GBACheatSetsClear(&m_cheatDevice.cheats);
388
389 m_gameOpen = false;
390 emit gameStopped(&m_threadContext);
391}
392
393void GameController::crashGame(const QString& crashMessage) {
394 closeGame();
395 emit gameCrashed(crashMessage);
396 emit gameStopped(&m_threadContext);
397}
398
399bool GameController::isPaused() {
400 if (!m_gameOpen) {
401 return false;
402 }
403 return GBAThreadIsPaused(&m_threadContext);
404}
405
406void GameController::setPaused(bool paused) {
407 if (!m_gameOpen || m_rewindTimer.isActive() || paused == GBAThreadIsPaused(&m_threadContext)) {
408 return;
409 }
410 if (paused) {
411 GBAThreadPause(&m_threadContext);
412 emit gamePaused(&m_threadContext);
413 } else {
414 GBAThreadUnpause(&m_threadContext);
415 emit gameUnpaused(&m_threadContext);
416 }
417}
418
419void GameController::reset() {
420 GBAThreadReset(&m_threadContext);
421}
422
423void GameController::threadInterrupt() {
424 if (m_gameOpen) {
425 GBAThreadInterrupt(&m_threadContext);
426 }
427}
428
429void GameController::threadContinue() {
430 if (m_gameOpen) {
431 GBAThreadContinue(&m_threadContext);
432 }
433}
434
435void GameController::frameAdvance() {
436 if (m_rewindTimer.isActive()) {
437 return;
438 }
439 if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
440 setPaused(false);
441 }
442}
443
444void GameController::setRewind(bool enable, int capacity, int interval) {
445 if (m_gameOpen) {
446 threadInterrupt();
447 GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
448 threadContinue();
449 } else {
450 if (enable) {
451 m_threadContext.rewindBufferInterval = interval;
452 m_threadContext.rewindBufferCapacity = capacity;
453 } else {
454 m_threadContext.rewindBufferInterval = 0;
455 m_threadContext.rewindBufferCapacity = 0;
456 }
457 }
458}
459
460void GameController::rewind(int states) {
461 threadInterrupt();
462 if (!states) {
463 GBARewindAll(&m_threadContext);
464 } else {
465 GBARewind(&m_threadContext, states);
466 }
467 threadContinue();
468 emit rewound(&m_threadContext);
469 emit frameAvailable(m_drawContext);
470}
471
472void GameController::startRewinding() {
473 if (!m_gameOpen || m_rewindTimer.isActive()) {
474 return;
475 }
476 m_wasPaused = isPaused();
477 setPaused(true);
478 m_rewindTimer.start();
479}
480
481void GameController::stopRewinding() {
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 GBALoadState(context, context->stateDir, controller->m_stateSlot);
561 controller->stateLoaded(context);
562 controller->frameAvailable(controller->m_drawContext);
563 });
564}
565
566void GameController::saveState(int slot) {
567 if (slot > 0) {
568 m_stateSlot = slot;
569 }
570 GBARunOnThread(&m_threadContext, [](GBAThread* context) {
571 GameController* controller = static_cast<GameController*>(context->userData);
572 GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
573 });
574}
575
576void GameController::setVideoSync(bool set) {
577 m_videoSync = set;
578 if (!m_turbo) {
579 threadInterrupt();
580 m_threadContext.sync.videoFrameWait = set;
581 threadContinue();
582 }
583}
584
585void GameController::setAudioSync(bool set) {
586 m_audioSync = set;
587 if (!m_turbo) {
588 threadInterrupt();
589 m_threadContext.sync.audioWait = set;
590 threadContinue();
591 }
592}
593
594void GameController::setFrameskip(int skip) {
595 m_threadContext.frameskip = skip;
596}
597
598void GameController::setVolume(int volume) {
599 threadInterrupt();
600 m_threadContext.volume = volume;
601 if (m_gameOpen) {
602 m_threadContext.gba->audio.masterVolume = volume;
603 }
604 threadContinue();
605}
606
607void GameController::setMute(bool mute) {
608 threadInterrupt();
609 m_threadContext.mute = mute;
610 if (m_gameOpen) {
611 m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
612 }
613 threadContinue();
614}
615
616void GameController::setTurbo(bool set, bool forced) {
617 if (m_turboForced && !forced) {
618 return;
619 }
620 if (m_turbo == set && m_turboForced == forced) {
621 // Don't interrupt the thread if we don't need to
622 return;
623 }
624 m_turbo = set;
625 m_turboForced = set && forced;
626 enableTurbo();
627}
628
629void GameController::setTurboSpeed(float ratio) {
630 m_turboSpeed = ratio;
631 enableTurbo();
632}
633
634void GameController::enableTurbo() {
635 threadInterrupt();
636 if (!m_turbo) {
637 m_threadContext.fpsTarget = m_fpsTarget;
638 m_threadContext.sync.audioWait = m_audioSync;
639 m_threadContext.sync.videoFrameWait = m_videoSync;
640 } else if (m_turboSpeed <= 0) {
641 m_threadContext.fpsTarget = m_fpsTarget;
642 m_threadContext.sync.audioWait = false;
643 m_threadContext.sync.videoFrameWait = false;
644 } else {
645 m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
646 m_threadContext.sync.audioWait = true;
647 m_threadContext.sync.videoFrameWait = false;
648 }
649 redoSamples(m_audioProcessor->getBufferSamples());
650 threadContinue();
651}
652
653void GameController::setAVStream(GBAAVStream* stream) {
654 threadInterrupt();
655 m_threadContext.stream = stream;
656 if (m_gameOpen) {
657 m_threadContext.gba->stream = stream;
658 }
659 threadContinue();
660}
661
662void GameController::clearAVStream() {
663 threadInterrupt();
664 m_threadContext.stream = nullptr;
665 if (m_gameOpen) {
666 m_threadContext.gba->stream = nullptr;
667 }
668 threadContinue();
669}
670
671#ifdef USE_PNG
672void GameController::screenshot() {
673 GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
674}
675#endif
676
677void GameController::reloadAudioDriver() {
678 QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
679 int samples = m_audioProcessor->getBufferSamples();
680 delete m_audioProcessor;
681 m_audioProcessor = AudioProcessor::create();
682 m_audioProcessor->setBufferSamples(samples);
683 m_audioProcessor->moveToThread(m_audioThread);
684 connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
685 connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
686 connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
687 connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
688 if (isLoaded()) {
689 m_audioProcessor->setInput(&m_threadContext);
690 QMetaObject::invokeMethod(m_audioProcessor, "start");
691 }
692}
693
694void GameController::setLuminanceValue(uint8_t value) {
695 m_luxValue = value;
696 value = std::max<int>(value - 0x16, 0);
697 m_luxLevel = 10;
698 for (int i = 0; i < 10; ++i) {
699 if (value < LUX_LEVELS[i]) {
700 m_luxLevel = i;
701 break;
702 }
703 }
704 emit luminanceValueChanged(m_luxValue);
705}
706
707void GameController::setLuminanceLevel(int level) {
708 int value = 0x16;
709 level = std::max(0, std::min(10, level));
710 if (level > 0) {
711 value += LUX_LEVELS[level - 1];
712 }
713 setLuminanceValue(value);
714}
715
716void GameController::setRealTime() {
717 m_rtc.override = GameControllerRTC::NO_OVERRIDE;
718}
719
720void GameController::setFixedTime(const QDateTime& time) {
721 m_rtc.override = GameControllerRTC::FIXED;
722 m_rtc.value = time.toMSecsSinceEpoch() / 1000;
723}
724
725void GameController::setFakeEpoch(const QDateTime& time) {
726 m_rtc.override = GameControllerRTC::FAKE_EPOCH;
727 m_rtc.value = time.toMSecsSinceEpoch() / 1000;
728}
729
730void GameController::updateKeys() {
731 int activeKeys = m_activeKeys;
732 activeKeys |= m_activeButtons;
733 activeKeys &= ~m_inactiveKeys;
734 m_threadContext.activeKeys = activeKeys;
735}
736
737void GameController::redoSamples(int samples) {
738#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
739 float sampleRate = 0x8000;
740 float ratio;
741 if (m_threadContext.gba) {
742 sampleRate = m_threadContext.gba->audio.sampleRate;
743 }
744 ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
745 m_threadContext.audioBuffers = ceil(samples / ratio);
746#else
747 m_threadContext.audioBuffers = samples;
748#endif
749 if (m_threadContext.gba) {
750 GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
751 }
752 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
753}
754
755void GameController::setLogLevel(int levels) {
756 threadInterrupt();
757 m_logLevels = levels;
758 threadContinue();
759}
760
761void GameController::enableLogLevel(int levels) {
762 threadInterrupt();
763 m_logLevels |= levels;
764 threadContinue();
765}
766
767void GameController::disableLogLevel(int levels) {
768 threadInterrupt();
769 m_logLevels &= ~levels;
770 threadContinue();
771}
772
773void GameController::pollEvents() {
774 if (!m_inputController) {
775 return;
776 }
777
778 m_activeButtons = m_inputController->pollEvents();
779 updateKeys();
780}