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
28struct mCore;
29
30namespace QGBA {
31
32class ConfigController;
33class InputController;
34class LogController;
35class MultiplayerController;
36class Override;
37
38class CoreController : public QObject {
39Q_OBJECT
40
41public:
42 static const bool VIDEO_SYNC = false;
43 static const bool AUDIO_SYNC = true;
44
45 class Interrupter {
46 public:
47 Interrupter(CoreController*, bool fromThread = false);
48 Interrupter(std::shared_ptr<CoreController>, bool fromThread = false);
49 Interrupter(const Interrupter&);
50 ~Interrupter();
51
52 private:
53 CoreController* m_parent;
54 };
55
56 CoreController(mCore* core, QObject* parent = nullptr);
57 ~CoreController();
58
59 mCoreThread* thread() { return &m_threadContext; }
60
61 color_t* drawContext();
62
63 bool isPaused();
64 bool hasStarted();
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 mCacheSet* graphicCaches();
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<mCacheSet> m_cacheSet;
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 int m_autofireThreshold = 1;
188
189 VFileDevice m_backupLoadState;
190 QByteArray m_backupSaveState{nullptr};
191 int m_stateSlot = 1;
192 int m_loadStateFlags;
193 int m_saveStateFlags;
194
195 bool m_audioSync = AUDIO_SYNC;
196 bool m_videoSync = VIDEO_SYNC;
197
198 bool m_autosave;
199 bool m_autoload;
200 int m_autosaveCounter;
201
202 int m_fastForward = false;
203 int m_fastForwardForced = false;
204 float m_fastForwardRatio = -1.f;
205 float m_fpsTarget;
206
207 InputController* m_inputController = nullptr;
208 LogController* m_log = nullptr;
209 MultiplayerController* m_multiplayer = nullptr;
210
211 mVideoLogContext* m_vl = nullptr;
212 VFile* m_vlVf = nullptr;
213
214#ifdef M_CORE_GB
215 struct QGBPrinter {
216 GBPrinter d;
217 CoreController* parent;
218 } m_printer;
219#endif
220};
221
222}