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