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#ifndef QGBA_CORE_CONTROLLER
7#define QGBA_CORE_CONTROLLER
8
9#include <QByteArray>
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/tile-cache.h>
24
25#ifdef M_CORE_GB
26#include <mgba/internal/gb/sio/printer.h>
27#endif
28
29struct mCore;
30
31namespace QGBA {
32
33class ConfigController;
34class InputController;
35class LogController;
36class MultiplayerController;
37class Override;
38
39class CoreController : public QObject {
40Q_OBJECT
41
42public:
43 static const bool VIDEO_SYNC = false;
44 static const bool AUDIO_SYNC = true;
45
46 class Interrupter {
47 public:
48 Interrupter(CoreController*, bool fromThread = false);
49 Interrupter(std::shared_ptr<CoreController>, bool fromThread = false);
50 Interrupter(const Interrupter&);
51 ~Interrupter();
52
53 private:
54 CoreController* m_parent;
55 };
56
57 CoreController(mCore* core, QObject* parent = nullptr);
58 ~CoreController();
59
60 mCoreThread* thread() { return &m_threadContext; }
61
62 color_t* drawContext();
63
64 bool isPaused();
65
66 mPlatform platform() const;
67 QSize screenDimensions() const;
68
69 void loadConfig(ConfigController*);
70
71 mCheatDevice* cheatDevice() { return m_threadContext.core->cheatDevice(m_threadContext.core); }
72
73#ifdef USE_DEBUGGERS
74 mDebugger* debugger() { return m_threadContext.core->debugger; }
75 void setDebugger(mDebugger*);
76#endif
77
78 void setMultiplayerController(MultiplayerController*);
79 void clearMultiplayerController();
80 MultiplayerController* multiplayerController() { return m_multiplayer; }
81
82 mTileCache* tileCache();
83 int stateSlot() const { return m_stateSlot; }
84
85 void setOverride(std::unique_ptr<Override> override);
86 Override* override() { return m_override.get(); }
87
88 void setInputController(InputController*);
89 void setLogger(LogController*);
90
91public slots:
92 void start();
93 void stop();
94 void reset();
95 void setPaused(bool paused);
96 void frameAdvance();
97 void setSync(bool enable);
98
99 void setRewinding(bool);
100 void rewind(int count = 0);
101
102 void setFastForward(bool);
103 void forceFastForward(bool);
104
105 void loadState(int slot = 0);
106 void saveState(int slot = 0);
107 void loadBackupState();
108 void saveBackupState();
109
110 void loadSave(const QString&, bool temporary);
111 void loadPatch(const QString&);
112 void replaceGame(const QString&);
113 void yankPak();
114
115 void addKey(int key);
116 void clearKey(int key);
117 void setAutofire(int key, bool enable);
118
119#ifdef USE_PNG
120 void screenshot();
121#endif
122
123 void setRealTime();
124 void setFixedTime(const QDateTime& time);
125 void setFakeEpoch(const QDateTime& time);
126
127 void importSharkport(const QString& path);
128 void exportSharkport(const QString& path);
129
130 void attachPrinter();
131 void detachPrinter();
132 void endPrint();
133
134 void setAVStream(mAVStream*);
135 void clearAVStream();
136
137 void clearOverride();
138
139 void startVideoLog(const QString& path);
140 void endVideoLog();
141
142signals:
143 void started();
144 void paused();
145 void unpaused();
146 void stopping();
147 void crashed(const QString& errorMessage);
148 void failed();
149 void frameAvailable();
150 void stateLoaded();
151 void rewound();
152
153 void rewindChanged(bool);
154 void fastForwardChanged(bool);
155
156 void unimplementedBiosCall(int);
157 void statusPosted(const QString& message);
158 void logPosted(int level, int category, const QString& log);
159
160 void imagePrinted(const QImage&);
161
162private:
163 void updateKeys();
164 int updateAutofire();
165 void finishFrame();
166
167 void updateFastForward();
168
169 mCoreThread m_threadContext{};
170
171 bool m_patched = false;
172
173 QByteArray m_buffers[2];
174 QByteArray* m_activeBuffer;
175 QByteArray* m_completeBuffer = nullptr;
176
177 std::unique_ptr<mTileCache> m_tileCache;
178 std::unique_ptr<Override> m_override;
179
180 QList<std::function<void()>> m_resetActions;
181 QList<std::function<void()>> m_frameActions;
182 QMutex m_mutex;
183
184 int m_activeKeys = 0;
185 bool m_autofire[32] = {};
186 int m_autofireStatus[32] = {};
187
188 VFileDevice m_backupLoadState;
189 QByteArray m_backupSaveState{nullptr};
190 int m_stateSlot = 1;
191 int m_loadStateFlags;
192 int m_saveStateFlags;
193
194 bool m_audioSync = AUDIO_SYNC;
195 bool m_videoSync = VIDEO_SYNC;
196
197 int m_fastForward = false;
198 int m_fastForwardForced = false;
199 float m_fastForwardRatio = -1.f;
200 float m_fpsTarget;
201
202 InputController* m_inputController = nullptr;
203 LogController* m_log = nullptr;
204 MultiplayerController* m_multiplayer = nullptr;
205
206 mVideoLogContext* m_vl = nullptr;
207 VFile* m_vlVf = nullptr;
208
209#ifdef M_CORE_GB
210 struct QGBPrinter {
211 GBPrinter d;
212 CoreController* parent;
213 } m_printer;
214#endif
215};
216
217}
218
219#endif