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 loadState(const QString& path);
107 void saveState(int slot = 0);
108 void saveState(const QString& path);
109 void loadBackupState();
110 void saveBackupState();
111
112 void loadSave(const QString&, bool temporary);
113 void loadPatch(const QString&);
114 void replaceGame(const QString&);
115 void yankPak();
116
117 void addKey(int key);
118 void clearKey(int key);
119 void setAutofire(int key, bool enable);
120
121#ifdef USE_PNG
122 void screenshot();
123#endif
124
125 void setRealTime();
126 void setFixedTime(const QDateTime& time);
127 void setFakeEpoch(const QDateTime& time);
128
129 void importSharkport(const QString& path);
130 void exportSharkport(const QString& path);
131
132 void attachPrinter();
133 void detachPrinter();
134 void endPrint();
135
136 void setAVStream(mAVStream*);
137 void clearAVStream();
138
139 void clearOverride();
140
141 void startVideoLog(const QString& path);
142 void endVideoLog();
143
144signals:
145 void started();
146 void paused();
147 void unpaused();
148 void stopping();
149 void crashed(const QString& errorMessage);
150 void failed();
151 void frameAvailable();
152 void stateLoaded();
153 void rewound();
154
155 void rewindChanged(bool);
156 void fastForwardChanged(bool);
157
158 void unimplementedBiosCall(int);
159 void statusPosted(const QString& message);
160 void logPosted(int level, int category, const QString& log);
161
162 void imagePrinted(const QImage&);
163
164private:
165 void updateKeys();
166 int updateAutofire();
167 void finishFrame();
168
169 void updateFastForward();
170
171 mCoreThread m_threadContext{};
172
173 bool m_patched = false;
174
175 QByteArray m_buffers[2];
176 QByteArray* m_activeBuffer;
177 QByteArray* m_completeBuffer = nullptr;
178
179 std::unique_ptr<mCacheSet> m_cacheSet;
180 std::unique_ptr<Override> m_override;
181
182 QList<std::function<void()>> m_resetActions;
183 QList<std::function<void()>> m_frameActions;
184 QMutex m_mutex;
185
186 int m_activeKeys = 0;
187 bool m_autofire[32] = {};
188 int m_autofireStatus[32] = {};
189 int m_autofireThreshold = 1;
190
191 VFileDevice m_backupLoadState;
192 QByteArray m_backupSaveState{nullptr};
193 int m_stateSlot = 1;
194 QString m_statePath;
195 int m_loadStateFlags;
196 int m_saveStateFlags;
197
198 bool m_audioSync = AUDIO_SYNC;
199 bool m_videoSync = VIDEO_SYNC;
200
201 bool m_autosave;
202 bool m_autoload;
203 int m_autosaveCounter;
204
205 int m_fastForward = false;
206 int m_fastForwardForced = false;
207 float m_fastForwardRatio = -1.f;
208 float m_fpsTarget;
209
210 InputController* m_inputController = nullptr;
211 LogController* m_log = nullptr;
212 MultiplayerController* m_multiplayer = nullptr;
213
214 mVideoLogContext* m_vl = nullptr;
215 VFile* m_vlVf = nullptr;
216
217#ifdef M_CORE_GB
218 struct QGBPrinter {
219 GBPrinter d;
220 CoreController* parent;
221 } m_printer;
222#endif
223};
224
225}