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 bool supportsFeature(Feature feature) const { return m_threadContext.core->supportsFeature(m_threadContext.core, static_cast<mCoreFeature>(feature)); }
78 bool hardwareAccelerated() const { return m_hwaccel; }
79
80 void loadConfig(ConfigController*);
81
82 mCheatDevice* cheatDevice() { return m_threadContext.core->cheatDevice(m_threadContext.core); }
83
84#ifdef USE_DEBUGGERS
85 mDebugger* debugger() { return m_threadContext.core->debugger; }
86 void setDebugger(mDebugger*);
87#endif
88
89 void setMultiplayerController(MultiplayerController*);
90 void clearMultiplayerController();
91 MultiplayerController* multiplayerController() { return m_multiplayer; }
92
93 mCacheSet* graphicCaches();
94 int stateSlot() const { return m_stateSlot; }
95
96 void setOverride(std::unique_ptr<Override> override);
97 Override* override() { return m_override.get(); }
98
99 void setInputController(InputController*);
100 void setLogger(LogController*);
101
102 bool audioSync() const { return m_audioSync; }
103 bool videoSync() const { return m_videoSync; }
104
105public slots:
106 void start();
107 void stop();
108 void reset();
109 void setPaused(bool paused);
110 void frameAdvance();
111 void setSync(bool enable);
112
113 void setRewinding(bool);
114 void rewind(int count = 0);
115
116 void setFastForward(bool);
117 void forceFastForward(bool);
118
119 void loadState(int slot = 0);
120 void loadState(const QString& path);
121 void saveState(int slot = 0);
122 void saveState(const QString& path);
123 void loadBackupState();
124 void saveBackupState();
125
126 void loadSave(const QString&, bool temporary);
127 void loadPatch(const QString&);
128 void replaceGame(const QString&);
129 void yankPak();
130
131 void addKey(int key);
132 void clearKey(int key);
133 void setAutofire(int key, bool enable);
134
135#ifdef USE_PNG
136 void screenshot();
137#endif
138
139 void setRealTime();
140 void setFixedTime(const QDateTime& time);
141 void setFakeEpoch(const QDateTime& time);
142
143 void importSharkport(const QString& path);
144 void exportSharkport(const QString& path);
145
146#ifdef M_CORE_GB
147 void attachPrinter();
148 void detachPrinter();
149 void endPrint();
150#endif
151
152#ifdef M_CORE_GBA
153 void attachBattleChipGate();
154 void detachBattleChipGate();
155 void setBattleChipId(uint16_t id);
156 void setBattleChipFlavor(int flavor);
157#endif
158
159 void setAVStream(mAVStream*);
160 void clearAVStream();
161
162 void clearOverride();
163
164 void startVideoLog(const QString& path, bool compression = true);
165 void startVideoLog(VFile* vf, bool compression = true);
166 void endVideoLog(bool closeVf = true);
167
168 void setFramebufferHandle(int fb);
169
170signals:
171 void started();
172 void paused();
173 void unpaused();
174 void stopping();
175 void crashed(const QString& errorMessage);
176 void failed();
177 void frameAvailable();
178 void didReset();
179 void stateLoaded();
180 void rewound();
181
182 void rewindChanged(bool);
183 void fastForwardChanged(bool);
184
185 void unimplementedBiosCall(int);
186 void statusPosted(const QString& message);
187 void logPosted(int level, int category, const QString& log);
188
189 void imagePrinted(const QImage&);
190
191private:
192 void updateKeys();
193 int updateAutofire();
194 void finishFrame();
195
196 void updateFastForward();
197
198 mCoreThread m_threadContext{};
199
200 bool m_patched = false;
201
202 QByteArray m_buffers[2];
203 QByteArray* m_activeBuffer;
204 QByteArray m_completeBuffer;
205 bool m_hwaccel = false;
206
207 std::unique_ptr<mCacheSet> m_cacheSet;
208 std::unique_ptr<Override> m_override;
209
210 QList<std::function<void()>> m_resetActions;
211 QList<std::function<void()>> m_frameActions;
212 QMutex m_mutex;
213
214 int m_activeKeys = 0;
215 bool m_autofire[32] = {};
216 int m_autofireStatus[32] = {};
217 int m_autofireThreshold = 1;
218
219 VFileDevice m_backupLoadState;
220 QByteArray m_backupSaveState{nullptr};
221 int m_stateSlot = 1;
222 QString m_statePath;
223 int m_loadStateFlags;
224 int m_saveStateFlags;
225
226 bool m_audioSync = AUDIO_SYNC;
227 bool m_videoSync = VIDEO_SYNC;
228
229 bool m_autosave;
230 bool m_autoload;
231 int m_autosaveCounter;
232
233 int m_fastForward = false;
234 int m_fastForwardForced = false;
235 int m_fastForwardVolume = -1;
236 int m_fastForwardMute = -1;
237 float m_fastForwardRatio = -1.f;
238 float m_fpsTarget;
239
240 InputController* m_inputController = nullptr;
241 LogController* m_log = nullptr;
242 MultiplayerController* m_multiplayer = nullptr;
243
244 mVideoLogContext* m_vl = nullptr;
245 VFile* m_vlVf = nullptr;
246
247#ifdef M_CORE_GB
248 struct QGBPrinter {
249 GBPrinter d;
250 CoreController* parent;
251 } m_printer;
252#endif
253
254#ifdef M_CORE_GBA
255 GBASIOBattlechipGate m_battlechip;
256#endif
257};
258
259}