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