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 "LogController.h"
11#include "MultiplayerController.h"
12#include "VFileDevice.h"
13
14#include <QCoreApplication>
15#include <QDateTime>
16#include <QThread>
17
18#include <ctime>
19
20extern "C" {
21#include "core/config.h"
22#include "core/directories.h"
23#ifdef M_CORE_GBA
24#include "gba/bios.h"
25#include "gba/core.h"
26#include "gba/gba.h"
27#include "gba/serialize.h"
28#include "gba/extra/sharkport.h"
29#endif
30#ifdef M_CORE_GB
31#include "gb/gb.h"
32#endif
33#include "util/vfs.h"
34}
35
36using namespace QGBA;
37using namespace std;
38
39GameController::GameController(QObject* parent)
40 : QObject(parent)
41 , m_drawContext(nullptr)
42 , m_frontBuffer(nullptr)
43 , m_threadContext()
44 , m_activeKeys(0)
45 , m_inactiveKeys(0)
46 , m_logLevels(0)
47 , m_gameOpen(false)
48 , m_useBios(false)
49 , m_audioThread(new QThread(this))
50 , m_audioProcessor(AudioProcessor::create())
51 , m_pauseAfterFrame(false)
52 , m_videoSync(VIDEO_SYNC)
53 , m_audioSync(AUDIO_SYNC)
54 , m_fpsTarget(-1)
55 , m_turbo(false)
56 , m_turboForced(false)
57 , m_turboSpeed(-1)
58 , m_wasPaused(false)
59 , m_audioChannels{ true, true, true, true, true, true }
60 , m_videoLayers{ true, true, true, true, true }
61 , m_autofire{}
62 , m_autofireStatus{}
63 , m_inputController(nullptr)
64 , m_multiplayer(nullptr)
65 , m_stream(nullptr)
66 , m_stateSlot(1)
67 , m_backupLoadState(nullptr)
68 , m_backupSaveState(nullptr)
69 , m_saveStateFlags(SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS)
70 , m_loadStateFlags(SAVESTATE_SCREENSHOT)
71{
72 GBACheatDeviceCreate(&m_cheatDevice);
73
74 m_lux.p = this;
75 m_lux.sample = [](GBALuminanceSource* context) {
76 GameControllerLux* lux = static_cast<GameControllerLux*>(context);
77 lux->value = 0xFF - lux->p->m_luxValue;
78 };
79
80 m_lux.readLuminance = [](GBALuminanceSource* context) {
81 GameControllerLux* lux = static_cast<GameControllerLux*>(context);
82 return lux->value;
83 };
84 setLuminanceLevel(0);
85
86 m_threadContext.startCallback = [](mCoreThread* context) {
87 GameController* controller = static_cast<GameController*>(context->userData);
88 if (controller->m_audioProcessor) {
89 controller->m_audioProcessor->setInput(context);
90 }
91 mRTCGenericSourceInit(&controller->m_rtc, context->core);
92 context->core->setRTC(context->core, &controller->m_rtc.d);
93 context->core->setRotation(context->core, controller->m_inputController->rotationSource());
94 context->core->setRumble(context->core, controller->m_inputController->rumble());
95
96#ifdef M_CORE_GBA
97 GBA* gba = static_cast<GBA*>(context->core->board);
98#endif
99#ifdef M_CORE_GB
100 GB* gb = static_cast<GB*>(context->core->board);
101#endif
102 switch (context->core->platform(context->core)) {
103#ifdef M_CORE_GBA
104 case PLATFORM_GBA:
105 gba->luminanceSource = &controller->m_lux;
106 gba->audio.psg.forceDisableCh[0] = !controller->m_audioChannels[0];
107 gba->audio.psg.forceDisableCh[1] = !controller->m_audioChannels[1];
108 gba->audio.psg.forceDisableCh[2] = !controller->m_audioChannels[2];
109 gba->audio.psg.forceDisableCh[3] = !controller->m_audioChannels[3];
110 gba->audio.forceDisableChA = !controller->m_audioChannels[4];
111 gba->audio.forceDisableChB = !controller->m_audioChannels[5];
112 gba->video.renderer->disableBG[0] = !controller->m_videoLayers[0];
113 gba->video.renderer->disableBG[1] = !controller->m_videoLayers[1];
114 gba->video.renderer->disableBG[2] = !controller->m_videoLayers[2];
115 gba->video.renderer->disableBG[3] = !controller->m_videoLayers[3];
116 gba->video.renderer->disableOBJ = !controller->m_videoLayers[4];
117 break;
118#endif
119#ifdef M_CORE_GB
120 case PLATFORM_GB:
121 gb->audio.forceDisableCh[0] = !controller->m_audioChannels[0];
122 gb->audio.forceDisableCh[1] = !controller->m_audioChannels[1];
123 gb->audio.forceDisableCh[2] = !controller->m_audioChannels[2];
124 gb->audio.forceDisableCh[3] = !controller->m_audioChannels[3];
125 break;
126#endif
127 default:
128 break;
129 }
130 controller->m_fpsTarget = context->sync.fpsTarget;
131
132 if (mCoreLoadState(context->core, 0, controller->m_loadStateFlags)) {
133 mCoreDeleteState(context->core, 0);
134 }
135 QMetaObject::invokeMethod(controller, "gameStarted", Q_ARG(mCoreThread*, context), Q_ARG(const QString&, controller->m_fname));
136 };
137
138 m_threadContext.cleanCallback = [](mCoreThread* context) {
139 GameController* controller = static_cast<GameController*>(context->userData);
140 QMetaObject::invokeMethod(controller, "gameStopped", Q_ARG(mCoreThread*, context));
141 };
142
143 m_threadContext.frameCallback = [](mCoreThread* context) {
144 GameController* controller = static_cast<GameController*>(context->userData);
145 unsigned width, height;
146 controller->m_threadContext.core->desiredVideoDimensions(controller->m_threadContext.core, &width, &height);
147 memcpy(controller->m_frontBuffer, controller->m_drawContext, width * height * BYTES_PER_PIXEL);
148 QMetaObject::invokeMethod(controller, "frameAvailable", Q_ARG(const uint32_t*, controller->m_frontBuffer));
149 if (controller->m_pauseAfterFrame.testAndSetAcquire(true, false)) {
150 mCoreThreadPauseFromThread(context);
151 QMetaObject::invokeMethod(controller, "gamePaused", Q_ARG(mCoreThread*, context));
152 }
153 };
154
155 /*m_threadContext.stopCallback = [](mCoreThread* context) {
156 if (!context) {
157 return false;
158 }
159 GameController* controller = static_cast<GameController*>(context->userData);
160 if (!mCoreSaveState(context->core, 0, controller->m_saveStateFlags)) {
161 return false;
162 }
163 QMetaObject::invokeMethod(controller, "closeGame");
164 return true;
165 };*/
166
167 m_threadContext.logger.d.log = [](mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) {
168 mThreadLogger* logContext = reinterpret_cast<mThreadLogger*>(logger);
169 mCoreThread* context = logContext->p;
170
171 static const char* savestateMessage = "State %i loaded";
172 static const char* savestateFailedMessage = "State %i failed to load";
173 if (!context) {
174 return;
175 }
176 GameController* controller = static_cast<GameController*>(context->userData);
177 if (level == mLOG_STUB && category == _mLOG_CAT_GBA_BIOS()) {
178 va_list argc;
179 va_copy(argc, args);
180 int immediate = va_arg(argc, int);
181 va_end(argc);
182 QMetaObject::invokeMethod(controller, "unimplementedBiosCall", Q_ARG(int, immediate));
183 } else if (category == _mLOG_CAT_STATUS()) {
184 // Slot 0 is reserved for suspend points
185 if (strncmp(savestateMessage, format, strlen(savestateMessage)) == 0) {
186 va_list argc;
187 va_copy(argc, args);
188 int slot = va_arg(argc, int);
189 va_end(argc);
190 if (slot == 0) {
191 format = "Loaded suspend state";
192 }
193 } else if (strncmp(savestateFailedMessage, format, strlen(savestateFailedMessage)) == 0) {
194 va_list argc;
195 va_copy(argc, args);
196 int slot = va_arg(argc, int);
197 va_end(argc);
198 if (slot == 0) {
199 return;
200 }
201 }
202 }
203 if (level == mLOG_FATAL) {
204 QMetaObject::invokeMethod(controller, "crashGame", Q_ARG(const QString&, QString().vsprintf(format, args)));
205 } else if (!(controller->m_logLevels & level)) {
206 return;
207 }
208 QString message(QString().vsprintf(format, args));
209 if (category == _mLOG_CAT_STATUS()) {
210 QMetaObject::invokeMethod(controller, "statusPosted", Q_ARG(const QString&, message));
211 }
212 QMetaObject::invokeMethod(controller, "postLog", Q_ARG(int, level), Q_ARG(int, category), Q_ARG(const QString&, message));
213 };
214
215 m_threadContext.userData = this;
216
217 connect(&m_rewindTimer, &QTimer::timeout, [this]() {
218 // TODO: Put rewind back
219 emit frameAvailable(m_drawContext);
220 emit rewound(&m_threadContext);
221 });
222 m_rewindTimer.setInterval(100);
223
224 m_audioThread->setObjectName("Audio Thread");
225 m_audioThread->start(QThread::TimeCriticalPriority);
226 m_audioProcessor->moveToThread(m_audioThread);
227 connect(this, SIGNAL(gamePaused(mCoreThread*)), m_audioProcessor, SLOT(pause()));
228 connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(pollEvents()));
229 connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updateAutofire()));
230}
231
232GameController::~GameController() {
233 m_audioThread->quit();
234 m_audioThread->wait();
235 disconnect();
236 clearMultiplayerController();
237 closeGame();
238 GBACheatDeviceDestroy(&m_cheatDevice);
239 delete m_backupLoadState;
240}
241
242void GameController::setMultiplayerController(MultiplayerController* controller) {
243 if (controller == m_multiplayer) {
244 return;
245 }
246 clearMultiplayerController();
247 m_multiplayer = controller;
248 controller->attachGame(this);
249}
250
251void GameController::clearMultiplayerController() {
252 if (!m_multiplayer) {
253 return;
254 }
255 m_multiplayer->detachGame(this);
256 m_multiplayer = nullptr;
257}
258
259void GameController::setOverride(const GBACartridgeOverride& override) {
260 // TODO: Put back overrides
261}
262
263void GameController::setConfig(const mCoreConfig* config) {
264 m_config = config;
265 if (isLoaded()) {
266 threadInterrupt();
267 mCoreLoadForeignConfig(m_threadContext.core, config);
268 m_audioProcessor->setInput(&m_threadContext);
269 threadContinue();
270 }
271}
272
273#ifdef USE_GDB_STUB
274mDebugger* GameController::debugger() {
275 if (!isLoaded()) {
276 return nullptr;
277 }
278 return m_threadContext.core->debugger;
279}
280
281void GameController::setDebugger(mDebugger* debugger) {
282 threadInterrupt();
283 if (debugger) {
284 mDebuggerAttach(debugger, m_threadContext.core);
285 } else {
286 m_threadContext.core->detachDebugger(m_threadContext.core);
287 }
288 threadContinue();
289}
290#endif
291
292void GameController::loadGame(const QString& path) {
293 closeGame();
294 QFile file(path);
295 if (!file.open(QIODevice::ReadOnly)) {
296 LOG(QT, ERROR) << tr("Failed to open game file: %1").arg(path);
297 return;
298 }
299 file.close();
300
301 m_fname = path;
302 openGame();
303}
304
305void GameController::bootBIOS() {
306 closeGame();
307 m_fname = QString();
308 openGame(true);
309}
310
311void GameController::openGame(bool biosOnly) {
312 if (biosOnly && (!m_useBios || m_bios.isNull())) {
313 return;
314 }
315
316 if (!biosOnly) {
317 m_threadContext.core = mCoreFind(m_fname.toUtf8().constData());
318 } else {
319 m_threadContext.core = GBACoreCreate();
320 }
321
322 if (!m_threadContext.core) {
323 return;
324 }
325
326 m_gameOpen = true;
327
328 m_pauseAfterFrame = false;
329
330 if (m_turbo) {
331 m_threadContext.sync.videoFrameWait = false;
332 m_threadContext.sync.audioWait = false;
333 } else {
334 m_threadContext.sync.videoFrameWait = m_videoSync;
335 m_threadContext.sync.audioWait = m_audioSync;
336 }
337 m_threadContext.core->init(m_threadContext.core);
338
339 unsigned width, height;
340 m_threadContext.core->desiredVideoDimensions(m_threadContext.core, &width, &height);
341 m_drawContext = new uint32_t[width * height];
342 m_frontBuffer = new uint32_t[width * height];
343
344 if (!biosOnly) {
345 mCoreLoadFile(m_threadContext.core, m_fname.toUtf8().constData());
346 }
347
348 m_threadContext.core->setVideoBuffer(m_threadContext.core, m_drawContext, width);
349
350 if (!m_bios.isNull() && m_useBios) {
351 VFile* bios = VFileDevice::open(m_bios, O_RDONLY);
352 if (bios) {
353 // TODO: Lifetime issues?
354 m_threadContext.core->loadBIOS(m_threadContext.core, bios, 0);
355 }
356 }
357
358 if (!m_patch.isNull()) {
359 VFile* patch = VFileDevice::open(m_patch, O_RDONLY);
360 if (patch) {
361 m_threadContext.core->loadPatch(m_threadContext.core, patch);
362 }
363 patch->close(patch);
364 }
365
366 m_inputController->recalibrateAxes();
367 memset(m_drawContext, 0xF8, width * height * 4);
368
369 m_threadContext.core->setAVStream(m_threadContext.core, m_stream);
370
371 if (m_config) {
372 mCoreLoadForeignConfig(m_threadContext.core, m_config);
373 }
374
375 if (!biosOnly) {
376 mCoreAutoloadSave(m_threadContext.core);
377 }
378
379 if (!mCoreThreadStart(&m_threadContext)) {
380 m_gameOpen = false;
381 emit gameFailed();
382 } else if (m_audioProcessor) {
383 startAudio();
384 }
385}
386
387void GameController::loadBIOS(const QString& path) {
388 if (m_bios == path) {
389 return;
390 }
391 m_bios = path;
392 if (m_gameOpen) {
393 closeGame();
394 openGame();
395 }
396}
397
398void GameController::yankPak() {
399 if (!m_gameOpen) {
400 return;
401 }
402 threadInterrupt();
403 GBAYankROM(static_cast<GBA*>(m_threadContext.core->board));
404 threadContinue();
405}
406
407void GameController::replaceGame(const QString& path) {
408 if (!m_gameOpen) {
409 return;
410 }
411
412 m_fname = path;
413 threadInterrupt();
414 mCoreLoadFile(m_threadContext.core, m_fname.toLocal8Bit().constData());
415 threadContinue();
416}
417
418void GameController::loadPatch(const QString& path) {
419 if (m_gameOpen) {
420 closeGame();
421 m_patch = path;
422 openGame();
423 } else {
424 m_patch = path;
425 }
426}
427
428void GameController::importSharkport(const QString& path) {
429 if (!isLoaded()) {
430 return;
431 }
432 VFile* vf = VFileDevice::open(path, O_RDONLY);
433 if (!vf) {
434 LOG(QT, ERROR) << tr("Failed to open snapshot file for reading: %1").arg(path);
435 return;
436 }
437 threadInterrupt();
438 GBASavedataImportSharkPort(static_cast<GBA*>(m_threadContext.core->board), vf, false);
439 threadContinue();
440 vf->close(vf);
441}
442
443void GameController::exportSharkport(const QString& path) {
444 if (!isLoaded()) {
445 return;
446 }
447 VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
448 if (!vf) {
449 LOG(QT, ERROR) << tr("Failed to open snapshot file for writing: %1").arg(path);
450 return;
451 }
452 threadInterrupt();
453 GBASavedataExportSharkPort(static_cast<GBA*>(m_threadContext.core->board), vf);
454 threadContinue();
455 vf->close(vf);
456}
457
458void GameController::closeGame() {
459 if (!m_gameOpen) {
460 return;
461 }
462 m_gameOpen = false;
463
464 m_rewindTimer.stop();
465 if (mCoreThreadIsPaused(&m_threadContext)) {
466 mCoreThreadUnpause(&m_threadContext);
467 }
468 m_audioProcessor->pause();
469 mCoreThreadEnd(&m_threadContext);
470 mCoreThreadJoin(&m_threadContext);
471 // Make sure the event queue clears out before the thread is reused
472 QCoreApplication::processEvents();
473
474 delete[] m_drawContext;
475 delete[] m_frontBuffer;
476
477 m_patch = QString();
478
479 for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
480 GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
481 GBACheatSetDeinit(set);
482 delete set;
483 }
484 GBACheatSetsClear(&m_cheatDevice.cheats);
485
486 m_threadContext.core->deinit(m_threadContext.core);
487}
488
489void GameController::crashGame(const QString& crashMessage) {
490 closeGame();
491 emit gameCrashed(crashMessage);
492 emit gameStopped(&m_threadContext);
493}
494
495bool GameController::isPaused() {
496 if (!m_gameOpen) {
497 return false;
498 }
499 return mCoreThreadIsPaused(&m_threadContext);
500}
501
502mPlatform GameController::platform() const {
503 if (!m_gameOpen) {
504 return PLATFORM_NONE;
505 }
506 return m_threadContext.core->platform(m_threadContext.core);
507}
508
509QSize GameController::screenDimensions() const {
510 if (!m_gameOpen) {
511 return QSize();
512 }
513 unsigned width, height;
514 m_threadContext.core->desiredVideoDimensions(m_threadContext.core, &width, &height);
515
516 return QSize(width, height);
517}
518
519void GameController::setPaused(bool paused) {
520 if (!isLoaded() || m_rewindTimer.isActive() || paused == mCoreThreadIsPaused(&m_threadContext)) {
521 return;
522 }
523 if (paused) {
524 m_pauseAfterFrame.testAndSetRelaxed(false, true);
525 } else {
526 mCoreThreadUnpause(&m_threadContext);
527 startAudio();
528 emit gameUnpaused(&m_threadContext);
529 }
530}
531
532void GameController::reset() {
533 if (!m_gameOpen) {
534 return;
535 }
536 bool wasPaused = isPaused();
537 setPaused(false);
538 mCoreThreadReset(&m_threadContext);
539 if (wasPaused) {
540 setPaused(true);
541 }
542}
543
544void GameController::threadInterrupt() {
545 if (m_gameOpen) {
546 mCoreThreadInterrupt(&m_threadContext);
547 }
548}
549
550void GameController::threadContinue() {
551 if (m_gameOpen) {
552 mCoreThreadContinue(&m_threadContext);
553 }
554}
555
556void GameController::frameAdvance() {
557 if (m_rewindTimer.isActive()) {
558 return;
559 }
560 if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
561 setPaused(false);
562 }
563}
564
565void GameController::setRewind(bool enable, int capacity, int interval) {
566 if (m_gameOpen) {
567 threadInterrupt();
568 // TODO: Put back rewind
569 threadContinue();
570 } else {
571 // TODO: Put back rewind
572 }
573}
574
575void GameController::rewind(int states) {
576 threadInterrupt();
577 if (!states) {
578 // TODO: Put back rewind
579 } else {
580 // TODO: Put back rewind
581 }
582 threadContinue();
583 emit frameAvailable(m_drawContext);
584 emit rewound(&m_threadContext);
585}
586
587void GameController::startRewinding() {
588 if (!m_gameOpen || m_rewindTimer.isActive()) {
589 return;
590 }
591 if (m_multiplayer && m_multiplayer->attached() > 1) {
592 return;
593 }
594 m_wasPaused = isPaused();
595 if (!mCoreThreadIsPaused(&m_threadContext)) {
596 mCoreThreadPause(&m_threadContext);
597 }
598 m_rewindTimer.start();
599}
600
601void GameController::stopRewinding() {
602 if (!m_rewindTimer.isActive()) {
603 return;
604 }
605 m_rewindTimer.stop();
606 bool signalsBlocked = blockSignals(true);
607 setPaused(m_wasPaused);
608 blockSignals(signalsBlocked);
609}
610
611void GameController::keyPressed(int key) {
612 int mappedKey = 1 << key;
613 m_activeKeys |= mappedKey;
614 if (!m_inputController->allowOpposing()) {
615 if ((m_activeKeys & 0x30) == 0x30) {
616 m_inactiveKeys |= mappedKey ^ 0x30;
617 m_activeKeys ^= mappedKey ^ 0x30;
618 }
619 if ((m_activeKeys & 0xC0) == 0xC0) {
620 m_inactiveKeys |= mappedKey ^ 0xC0;
621 m_activeKeys ^= mappedKey ^ 0xC0;
622 }
623 }
624 updateKeys();
625}
626
627void GameController::keyReleased(int key) {
628 int mappedKey = 1 << key;
629 m_activeKeys &= ~mappedKey;
630 if (!m_inputController->allowOpposing()) {
631 if (mappedKey & 0x30) {
632 m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
633 m_inactiveKeys &= ~0x30;
634 }
635 if (mappedKey & 0xC0) {
636 m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
637 m_inactiveKeys &= ~0xC0;
638 }
639 }
640 updateKeys();
641}
642
643void GameController::clearKeys() {
644 m_activeKeys = 0;
645 m_inactiveKeys = 0;
646 updateKeys();
647}
648
649void GameController::setAutofire(int key, bool enable) {
650 if (key >= GBA_KEY_MAX || key < 0) {
651 return;
652 }
653 m_autofire[key] = enable;
654 m_autofireStatus[key] = 0;
655}
656
657void GameController::setAudioBufferSamples(int samples) {
658 if (m_audioProcessor) {
659 threadInterrupt();
660 redoSamples(samples);
661 threadContinue();
662 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Qt::BlockingQueuedConnection, Q_ARG(int, samples));
663 }
664}
665
666void GameController::setAudioSampleRate(unsigned rate) {
667 if (!rate) {
668 return;
669 }
670 if (m_audioProcessor) {
671 threadInterrupt();
672 redoSamples(m_audioProcessor->getBufferSamples());
673 threadContinue();
674 QMetaObject::invokeMethod(m_audioProcessor, "requestSampleRate", Q_ARG(unsigned, rate));
675 }
676}
677
678void GameController::setAudioChannelEnabled(int channel, bool enable) {
679 if (channel > 5 || channel < 0) {
680 return;
681 }
682#ifdef M_CORE_GBA
683 GBA* gba = static_cast<GBA*>(m_threadContext.core->board);
684#endif
685#ifdef M_CORE_GB
686 GB* gb = static_cast<GB*>(m_threadContext.core->board);
687#endif
688 m_audioChannels[channel] = enable;
689 if (isLoaded()) {
690 switch (channel) {
691 case 0:
692 case 1:
693 case 2:
694 case 3:
695 switch (m_threadContext.core->platform(m_threadContext.core)) {
696#ifdef M_CORE_GBA
697 case PLATFORM_GBA:
698 gba->audio.psg.forceDisableCh[channel] = !enable;
699 break;
700#endif
701#ifdef M_CORE_GB
702 case PLATFORM_GB:
703 gb->audio.forceDisableCh[channel] = !enable;
704 break;
705#endif
706 default:
707 break;
708 }
709 break;
710#ifdef M_CORE_GBA
711 case 4:
712 if (m_threadContext.core->platform(m_threadContext.core) == PLATFORM_GBA) {
713 gba->audio.forceDisableChA = !enable;
714 }
715 break;
716 case 5:
717 if (m_threadContext.core->platform(m_threadContext.core) == PLATFORM_GBA) {
718 gba->audio.forceDisableChB = !enable;
719 }
720 break;
721#endif
722 }
723 }
724}
725
726void GameController::startAudio() {
727 bool started = false;
728 QMetaObject::invokeMethod(m_audioProcessor, "start", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, started));
729 if (!started) {
730 LOG(QT, ERROR) << tr("Failed to start audio processor");
731 // Don't freeze!
732 m_audioSync = false;
733 m_videoSync = true;
734 m_threadContext.sync.audioWait = false;
735 m_threadContext.sync.videoFrameWait = true;
736 }
737}
738
739void GameController::setVideoLayerEnabled(int layer, bool enable) {
740 if (layer > 4 || layer < 0) {
741 return;
742 }
743 m_videoLayers[layer] = enable;
744 if (isLoaded() && m_threadContext.core->platform(m_threadContext.core) == PLATFORM_GBA) {
745 GBA* gba = static_cast<GBA*>(m_threadContext.core->board);
746 switch (layer) {
747 case 0:
748 case 1:
749 case 2:
750 case 3:
751 gba->video.renderer->disableBG[layer] = !enable;
752 break;
753 case 4:
754 gba->video.renderer->disableOBJ = !enable;
755 break;
756 }
757 }
758}
759
760void GameController::setFPSTarget(float fps) {
761 threadInterrupt();
762 m_fpsTarget = fps;
763 m_threadContext.sync.fpsTarget = fps;
764 if (m_turbo && m_turboSpeed > 0) {
765 m_threadContext.sync.fpsTarget *= m_turboSpeed;
766 }
767 if (m_audioProcessor) {
768 redoSamples(m_audioProcessor->getBufferSamples());
769 }
770 threadContinue();
771}
772
773void GameController::setUseBIOS(bool use) {
774 if (use == m_useBios) {
775 return;
776 }
777 m_useBios = use;
778 if (m_gameOpen) {
779 closeGame();
780 openGame();
781 }
782}
783
784void GameController::loadState(int slot) {
785 if (m_fname.isEmpty()) {
786 // We're in the BIOS
787 return;
788 }
789 if (slot > 0 && slot != m_stateSlot) {
790 m_stateSlot = slot;
791 m_backupSaveState.clear();
792 }
793 mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
794 GameController* controller = static_cast<GameController*>(context->userData);
795 if (!controller->m_backupLoadState) {
796 controller->m_backupLoadState = VFileMemChunk(nullptr, 0);
797 }
798 context->core->saveState(context->core, controller->m_backupLoadState, controller->m_saveStateFlags);
799 if (mCoreLoadState(context->core, controller->m_stateSlot, controller->m_loadStateFlags)) {
800 controller->frameAvailable(controller->m_drawContext);
801 controller->stateLoaded(context);
802 }
803 });
804}
805
806void GameController::saveState(int slot) {
807 if (m_fname.isEmpty()) {
808 // We're in the BIOS
809 return;
810 }
811 if (slot > 0) {
812 m_stateSlot = slot;
813 }
814 mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
815 GameController* controller = static_cast<GameController*>(context->userData);
816 VFile* vf = mCoreGetState(context->core, controller->m_stateSlot, false);
817 if (vf) {
818 controller->m_backupSaveState.resize(vf->size(vf));
819 vf->read(vf, controller->m_backupSaveState.data(), controller->m_backupSaveState.size());
820 vf->close(vf);
821 }
822 mCoreSaveState(context->core, controller->m_stateSlot, controller->m_saveStateFlags);
823 });
824}
825
826void GameController::loadBackupState() {
827 if (!m_backupLoadState) {
828 return;
829 }
830
831 mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
832 GameController* controller = static_cast<GameController*>(context->userData);
833 controller->m_backupLoadState->seek(controller->m_backupLoadState, 0, SEEK_SET);
834 if (context->core->loadState(context->core, controller->m_backupLoadState, controller->m_loadStateFlags)) {
835 mLOG(STATUS, INFO, "Undid state load");
836 controller->frameAvailable(controller->m_drawContext);
837 controller->stateLoaded(context);
838 }
839 controller->m_backupLoadState->close(controller->m_backupLoadState);
840 controller->m_backupLoadState = nullptr;
841 });
842}
843
844void GameController::saveBackupState() {
845 if (m_backupSaveState.isEmpty()) {
846 return;
847 }
848
849 mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
850 GameController* controller = static_cast<GameController*>(context->userData);
851 VFile* vf = mCoreGetState(context->core, controller->m_stateSlot, true);
852 if (vf) {
853 vf->write(vf, controller->m_backupSaveState.constData(), controller->m_backupSaveState.size());
854 vf->close(vf);
855 mLOG(STATUS, INFO, "Undid state save");
856 }
857 controller->m_backupSaveState.clear();
858 });
859}
860
861void GameController::setTurbo(bool set, bool forced) {
862 if (m_turboForced && !forced) {
863 return;
864 }
865 if (m_turbo == set && m_turboForced == forced) {
866 // Don't interrupt the thread if we don't need to
867 return;
868 }
869 m_turbo = set;
870 m_turboForced = set && forced;
871 enableTurbo();
872}
873
874void GameController::setTurboSpeed(float ratio) {
875 m_turboSpeed = ratio;
876 enableTurbo();
877}
878
879void GameController::enableTurbo() {
880 threadInterrupt();
881 if (!m_turbo) {
882 m_threadContext.sync.fpsTarget = m_fpsTarget;
883 m_threadContext.sync.audioWait = m_audioSync;
884 m_threadContext.sync.videoFrameWait = m_videoSync;
885 } else if (m_turboSpeed <= 0) {
886 m_threadContext.sync.fpsTarget = m_fpsTarget;
887 m_threadContext.sync.audioWait = false;
888 m_threadContext.sync.videoFrameWait = false;
889 } else {
890 m_threadContext.sync.fpsTarget = m_fpsTarget * m_turboSpeed;
891 m_threadContext.sync.audioWait = true;
892 m_threadContext.sync.videoFrameWait = false;
893 }
894 if (m_audioProcessor) {
895 redoSamples(m_audioProcessor->getBufferSamples());
896 }
897 threadContinue();
898}
899
900void GameController::setAVStream(mAVStream* stream) {
901 threadInterrupt();
902 m_stream = stream;
903 if (isLoaded()) {
904 m_threadContext.core->setAVStream(m_threadContext.core, stream);
905 }
906 threadContinue();
907}
908
909void GameController::clearAVStream() {
910 threadInterrupt();
911 m_stream = nullptr;
912 if (isLoaded()) {
913 m_threadContext.core->setAVStream(m_threadContext.core, nullptr);
914 }
915 threadContinue();
916}
917
918#ifdef USE_PNG
919void GameController::screenshot() {
920 mCoreThreadRunFunction(&m_threadContext, [](mCoreThread* context) {
921 mCoreTakeScreenshot(context->core);
922 });
923}
924#endif
925
926void GameController::reloadAudioDriver() {
927 int samples = 0;
928 unsigned sampleRate = 0;
929 if (m_audioProcessor) {
930 QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
931 samples = m_audioProcessor->getBufferSamples();
932 sampleRate = m_audioProcessor->sampleRate();
933 delete m_audioProcessor;
934 }
935 m_audioProcessor = AudioProcessor::create();
936 if (samples) {
937 m_audioProcessor->setBufferSamples(samples);
938 }
939 if (sampleRate) {
940 m_audioProcessor->requestSampleRate(sampleRate);
941 }
942 m_audioProcessor->moveToThread(m_audioThread);
943 connect(this, SIGNAL(gamePaused(mCoreThread*)), m_audioProcessor, SLOT(pause()));
944 if (isLoaded()) {
945 m_audioProcessor->setInput(&m_threadContext);
946 startAudio();
947 }
948}
949
950void GameController::setSaveStateExtdata(int flags) {
951 m_saveStateFlags = flags;
952}
953
954void GameController::setLoadStateExtdata(int flags) {
955 m_loadStateFlags = flags;
956}
957
958void GameController::setLuminanceValue(uint8_t value) {
959 m_luxValue = value;
960 value = std::max<int>(value - 0x16, 0);
961 m_luxLevel = 10;
962 for (int i = 0; i < 10; ++i) {
963 if (value < GBA_LUX_LEVELS[i]) {
964 m_luxLevel = i;
965 break;
966 }
967 }
968 emit luminanceValueChanged(m_luxValue);
969}
970
971void GameController::setLuminanceLevel(int level) {
972 int value = 0x16;
973 level = std::max(0, std::min(10, level));
974 if (level > 0) {
975 value += GBA_LUX_LEVELS[level - 1];
976 }
977 setLuminanceValue(value);
978}
979
980void GameController::setRealTime() {
981 m_rtc.override = RTC_NO_OVERRIDE;
982}
983
984void GameController::setFixedTime(const QDateTime& time) {
985 m_rtc.override = RTC_FIXED;
986 m_rtc.value = time.toMSecsSinceEpoch() / 1000;
987}
988
989void GameController::setFakeEpoch(const QDateTime& time) {
990 m_rtc.override = RTC_FAKE_EPOCH;
991 m_rtc.value = time.toMSecsSinceEpoch() / 1000;
992}
993
994void GameController::updateKeys() {
995 int activeKeys = m_activeKeys;
996 activeKeys |= m_activeButtons;
997 activeKeys &= ~m_inactiveKeys;
998 if (isLoaded()) {
999 m_threadContext.core->setKeys(m_threadContext.core, activeKeys);
1000 }
1001}
1002
1003void GameController::redoSamples(int samples) {
1004 if (m_threadContext.core) {
1005 m_threadContext.core->setAudioBufferSize(m_threadContext.core, samples);
1006 }
1007 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
1008}
1009
1010void GameController::setLogLevel(int levels) {
1011 threadInterrupt();
1012 m_logLevels = levels;
1013 threadContinue();
1014}
1015
1016void GameController::enableLogLevel(int levels) {
1017 threadInterrupt();
1018 m_logLevels |= levels;
1019 threadContinue();
1020}
1021
1022void GameController::disableLogLevel(int levels) {
1023 threadInterrupt();
1024 m_logLevels &= ~levels;
1025 threadContinue();
1026}
1027
1028void GameController::pollEvents() {
1029 if (!m_inputController) {
1030 return;
1031 }
1032
1033 m_activeButtons = m_inputController->pollEvents();
1034 updateKeys();
1035}
1036
1037void GameController::updateAutofire() {
1038 // TODO: Move all key events onto the CPU thread...somehow
1039 for (int k = 0; k < GBA_KEY_MAX; ++k) {
1040 if (!m_autofire[k]) {
1041 continue;
1042 }
1043 m_autofireStatus[k] ^= 1;
1044 if (m_autofireStatus[k]) {
1045 keyPressed(k);
1046 } else {
1047 keyReleased(k);
1048 }
1049 }
1050}