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 <QFile>
10#include <QList>
11#include <QMutex>
12#include <QObject>
13#include <QSize>
14
15#include "VFileDevice.h"
16
17#include <functional>
18#include <memory>
19
20#include <mgba/core/core.h>
21#include <mgba/core/interface.h>
22#include <mgba/core/thread.h>
23#include <mgba/core/cache-set.h>
24
25#ifdef M_CORE_GB
26#include <mgba/internal/gb/sio/printer.h>
27#endif
28
29#ifdef M_CORE_GBA
30#include <mgba/gba/interface.h>
31#endif
32
33struct mCore;
34
35namespace QGBA {
36
37class ConfigController;
38class InputController;
39class LogController;
40class MultiplayerController;
41class Override;
42
43class CoreController : public QObject {
44Q_OBJECT
45
46public:
47 static const bool VIDEO_SYNC = false;
48 static const bool AUDIO_SYNC = true;
49
50 enum class Feature {
51 OPENGL = mCORE_FEATURE_OPENGL,
52 };
53
54 class Interrupter {
55 public:
56 Interrupter(CoreController*);
57 Interrupter(std::shared_ptr<CoreController>);
58 Interrupter(const Interrupter&);
59 ~Interrupter();
60
61 private:
62 CoreController* m_parent;
63 };
64
65 CoreController(mCore* core, QObject* parent = nullptr);
66 ~CoreController();
67
68 mCoreThread* thread() { return &m_threadContext; }
69
70 const color_t* drawContext();
71 QImage getPixels();
72
73 bool isPaused();
74 bool hasStarted();
75
76 mPlatform platform() const;
77 QSize screenDimensions() 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, int flags = -1);
124 void loadState(QIODevice* iodev, int flags = -1);
125 void saveState(int slot = 0);
126 void saveState(const QString& path, int flags = -1);
127 void saveState(QIODevice* iodev, int flags = -1);
128 void loadBackupState();
129 void saveBackupState();
130
131 void loadSave(const QString&, bool temporary);
132 void loadPatch(const QString&);
133 void scanCard(const QString&);
134 void replaceGame(const QString&);
135 void yankPak();
136
137 void addKey(int key);
138 void clearKey(int key);
139 void setAutofire(int key, bool enable);
140
141#ifdef USE_PNG
142 void screenshot();
143#endif
144
145 void setRealTime();
146 void setFixedTime(const QDateTime& time);
147 void setFakeEpoch(const QDateTime& time);
148
149 void importSharkport(const QString& path);
150 void exportSharkport(const QString& path);
151
152#ifdef M_CORE_GB
153 void attachPrinter();
154 void detachPrinter();
155 void endPrint();
156#endif
157
158#ifdef M_CORE_GBA
159 void attachBattleChipGate();
160 void detachBattleChipGate();
161 void setBattleChipId(uint16_t id);
162 void setBattleChipFlavor(int flavor);
163#endif
164
165 void setAVStream(mAVStream*);
166 void clearAVStream();
167
168 void clearOverride();
169
170 void startVideoLog(const QString& path, bool compression = true);
171 void startVideoLog(VFile* vf, bool compression = true);
172 void endVideoLog(bool closeVf = true);
173
174 void setFramebufferHandle(int fb);
175
176signals:
177 void started();
178 void paused();
179 void unpaused();
180 void stopping();
181 void crashed(const QString& errorMessage);
182 void failed();
183 void frameAvailable();
184 void didReset();
185 void stateLoaded();
186 void rewound();
187
188 void rewindChanged(bool);
189 void fastForwardChanged(bool);
190
191 void unimplementedBiosCall(int);
192 void statusPosted(const QString& message);
193 void logPosted(int level, int category, const QString& log);
194
195 void imagePrinted(const QImage&);
196
197private:
198 void updateKeys();
199 int updateAutofire();
200 void finishFrame();
201
202 void updateFastForward();
203
204 mCoreThread m_threadContext{};
205
206 bool m_patched = false;
207
208 QByteArray m_activeBuffer;
209 QByteArray m_completeBuffer;
210 bool m_hwaccel = false;
211
212 std::unique_ptr<mCacheSet> m_cacheSet;
213 std::unique_ptr<Override> m_override;
214
215 QList<std::function<void()>> m_resetActions;
216 QList<std::function<void()>> m_frameActions;
217 QMutex m_actionMutex{QMutex::Recursive};
218 int m_moreFrames = -1;
219 QMutex m_bufferMutex;
220
221 int m_activeKeys = 0;
222 bool m_autofire[32] = {};
223 int m_autofireStatus[32] = {};
224 int m_autofireThreshold = 1;
225
226 VFileDevice m_backupLoadState;
227 QByteArray m_backupSaveState{nullptr};
228 int m_stateSlot = 1;
229 QString m_statePath;
230 VFile* m_stateVf;
231 int m_loadStateFlags;
232 int m_saveStateFlags;
233
234 bool m_audioSync = AUDIO_SYNC;
235 bool m_videoSync = VIDEO_SYNC;
236
237 bool m_autosave;
238 bool m_autoload;
239 int m_autosaveCounter = 0;
240
241 int m_fastForward = false;
242 int m_fastForwardForced = false;
243 int m_fastForwardVolume = -1;
244 int m_fastForwardMute = -1;
245 float m_fastForwardRatio = -1.f;
246 float m_fastForwardHeldRatio = -1.f;
247 float m_fpsTarget;
248
249 InputController* m_inputController = nullptr;
250 LogController* m_log = nullptr;
251 MultiplayerController* m_multiplayer = nullptr;
252
253 mVideoLogContext* m_vl = nullptr;
254 VFile* m_vlVf = nullptr;
255
256#ifdef M_CORE_GB
257 struct QGBPrinter {
258 GBPrinter d;
259 CoreController* parent;
260 } m_printer;
261#endif
262
263#ifdef M_CORE_GBA
264 GBASIOBattlechipGate m_battlechip;
265 QByteArray m_eReaderData;
266#endif
267};
268
269}