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