src/platform/qt/CoreController.h (view raw)
1/* Copyright (c) 2013-2017 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#pragma once
7
8#include <QByteArray>
9#include <QList>
10#include <QMutex>
11#include <QObject>
12#include <QSize>
13
14#include "VFileDevice.h"
15
16#include <functional>
17#include <memory>
18
19#include <mgba/core/core.h>
20#include <mgba/core/interface.h>
21#include <mgba/core/thread.h>
22#include <mgba/core/cache-set.h>
23
24#ifdef M_CORE_GB
25#include <mgba/internal/gb/sio/printer.h>
26#endif
27
28#ifdef M_CORE_GBA
29#include <mgba/gba/interface.h>
30#endif
31
32struct mCore;
33
34namespace QGBA {
35
36class ConfigController;
37class InputController;
38class LogController;
39class MultiplayerController;
40class Override;
41
42class CoreController : public QObject {
43Q_OBJECT
44
45public:
46 static const bool VIDEO_SYNC = false;
47 static const bool AUDIO_SYNC = true;
48
49 enum class Feature {
50 OPENGL = mCORE_FEATURE_OPENGL,
51 };
52
53 class Interrupter {
54 public:
55 Interrupter(CoreController*, bool fromThread = false);
56 Interrupter(std::shared_ptr<CoreController>, bool fromThread = false);
57 Interrupter(const Interrupter&);
58 ~Interrupter();
59
60 private:
61 CoreController* m_parent;
62 };
63
64 CoreController(mCore* core, QObject* parent = nullptr);
65 ~CoreController();
66
67 mCoreThread* thread() { return &m_threadContext; }
68
69 const color_t* drawContext();
70 QImage getPixels();
71
72 bool isPaused();
73 bool hasStarted();
74
75 mPlatform platform() const;
76 QSize screenDimensions() const;
77 QPair<unsigned, unsigned> frameRate() const;
78 bool supportsFeature(Feature feature) const { return m_threadContext.core->supportsFeature(m_threadContext.core, static_cast<mCoreFeature>(feature)); }
79 bool hardwareAccelerated() const { return m_hwaccel; }
80
81 void loadConfig(ConfigController*);
82
83 mCheatDevice* cheatDevice() { return m_threadContext.core->cheatDevice(m_threadContext.core); }
84
85#ifdef USE_DEBUGGERS
86 mDebugger* debugger() { return m_threadContext.core->debugger; }
87 void setDebugger(mDebugger*);
88#endif
89
90 void setMultiplayerController(MultiplayerController*);
91 void clearMultiplayerController();
92 MultiplayerController* multiplayerController() { return m_multiplayer; }
93
94 mCacheSet* graphicCaches();
95 int stateSlot() const { return m_stateSlot; }
96
97 void setOverride(std::unique_ptr<Override> override);
98 Override* override() { return m_override.get(); }
99
100 void setInputController(InputController*);
101 void setLogger(LogController*);
102
103 bool audioSync() const { return m_audioSync; }
104 bool videoSync() const { return m_videoSync; }
105
106 void addFrameAction(std::function<void ()> callback);
107
108public slots:
109 void start();
110 void stop();
111 void reset();
112 void setPaused(bool paused);
113 void frameAdvance();
114 void setSync(bool enable);
115
116 void setRewinding(bool);
117 void rewind(int count = 0);
118
119 void setFastForward(bool);
120 void forceFastForward(bool);
121
122 void loadState(int slot = 0);
123 void loadState(const QString& path);
124 void saveState(int slot = 0);
125 void saveState(const QString& path);
126 void loadBackupState();
127 void saveBackupState();
128
129 void loadSave(const QString&, bool temporary);
130 void loadPatch(const QString&);
131 void replaceGame(const QString&);
132 void yankPak();
133
134#ifdef USE_PNG
135 void screenshot();
136#endif
137
138 void setRealTime();
139 void setFixedTime(const QDateTime& time);
140 void setFakeEpoch(const QDateTime& time);
141
142 void importSharkport(const QString& path);
143 void exportSharkport(const QString& path);
144
145#ifdef M_CORE_GB
146 void attachPrinter();
147 void detachPrinter();
148 void endPrint();
149#endif
150
151#ifdef M_CORE_GBA
152 void attachBattleChipGate();
153 void detachBattleChipGate();
154 void setBattleChipId(uint16_t id);
155 void setBattleChipFlavor(int flavor);
156#endif
157
158 void setAVStream(mAVStream*);
159 void clearAVStream();
160
161 void clearOverride();
162
163 void startVideoLog(const QString& path, bool compression = true);
164 void startVideoLog(VFile* vf, bool compression = true);
165 void endVideoLog(bool closeVf = true);
166
167 void setFramebufferHandle(int fb);
168
169signals:
170 void started();
171 void paused();
172 void unpaused();
173 void stopping();
174 void crashed(const QString& errorMessage);
175 void failed();
176 void frameAvailable();
177 void didReset();
178 void stateLoaded();
179 void rewound();
180
181 void rewindChanged(bool);
182 void fastForwardChanged(bool);
183
184 void unimplementedBiosCall(int);
185 void statusPosted(const QString& message);
186 void logPosted(int level, int category, const QString& log);
187
188 void imagePrinted(const QImage&);
189
190private:
191 void updateKeys();
192 void finishFrame();
193
194 void updateFastForward();
195
196 mCoreThread m_threadContext{};
197
198 bool m_patched = false;
199
200 QByteArray m_activeBuffer;
201 QByteArray m_completeBuffer;
202 bool m_hwaccel = false;
203
204 std::unique_ptr<mCacheSet> m_cacheSet;
205 std::unique_ptr<Override> m_override;
206
207 QList<std::function<void()>> m_resetActions;
208 QList<std::function<void()>> m_frameActions;
209 QMutex m_actionMutex{QMutex::Recursive};
210 QMutex m_bufferMutex;
211
212 int m_activeKeys = 0;
213 bool m_autofire[32] = {};
214 int m_autofireStatus[32] = {};
215 int m_autofireThreshold = 1;
216
217 VFileDevice m_backupLoadState;
218 QByteArray m_backupSaveState{nullptr};
219 int m_stateSlot = 1;
220 QString m_statePath;
221 int m_loadStateFlags;
222 int m_saveStateFlags;
223
224 bool m_audioSync = AUDIO_SYNC;
225 bool m_videoSync = VIDEO_SYNC;
226
227 bool m_autosave;
228 bool m_autoload;
229 int m_autosaveCounter;
230
231 int m_fastForward = false;
232 int m_fastForwardForced = false;
233 int m_fastForwardVolume = -1;
234 int m_fastForwardMute = -1;
235 float m_fastForwardRatio = -1.f;
236 float m_fastForwardHeldRatio = -1.f;
237 float m_fpsTarget;
238
239 InputController* m_inputController = nullptr;
240 LogController* m_log = nullptr;
241 MultiplayerController* m_multiplayer = nullptr;
242
243 mVideoLogContext* m_vl = nullptr;
244 VFile* m_vlVf = nullptr;
245
246#ifdef M_CORE_GB
247 struct QGBPrinter {
248 GBPrinter d;
249 CoreController* parent;
250 } m_printer;
251#endif
252
253#ifdef M_CORE_GBA
254 GBASIOBattlechipGate m_battlechip;
255#endif
256};
257
258}