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/tile-cache.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
65 mPlatform platform() const;
66 QSize screenDimensions() const;
67
68 void loadConfig(ConfigController*);
69
70 mCheatDevice* cheatDevice() { return m_threadContext.core->cheatDevice(m_threadContext.core); }
71
72#ifdef USE_DEBUGGERS
73 mDebugger* debugger() { return m_threadContext.core->debugger; }
74 void setDebugger(mDebugger*);
75#endif
76
77 void setMultiplayerController(MultiplayerController*);
78 void clearMultiplayerController();
79 MultiplayerController* multiplayerController() { return m_multiplayer; }
80
81 mTileCache* tileCache();
82 int stateSlot() const { return m_stateSlot; }
83
84 void setOverride(std::unique_ptr<Override> override);
85 Override* override() { return m_override.get(); }
86
87 void setInputController(InputController*);
88 void setLogger(LogController*);
89
90public slots:
91 void start();
92 void stop();
93 void reset();
94 void setPaused(bool paused);
95 void frameAdvance();
96 void setSync(bool enable);
97
98 void setRewinding(bool);
99 void rewind(int count = 0);
100
101 void setFastForward(bool);
102 void forceFastForward(bool);
103
104 void loadState(int slot = 0);
105 void saveState(int slot = 0);
106 void loadBackupState();
107 void saveBackupState();
108
109 void loadSave(const QString&, bool temporary);
110 void loadPatch(const QString&);
111 void replaceGame(const QString&);
112 void yankPak();
113
114 void addKey(int key);
115 void clearKey(int key);
116 void setAutofire(int key, bool enable);
117
118#ifdef USE_PNG
119 void screenshot();
120#endif
121
122 void setRealTime();
123 void setFixedTime(const QDateTime& time);
124 void setFakeEpoch(const QDateTime& time);
125
126 void importSharkport(const QString& path);
127 void exportSharkport(const QString& path);
128
129 void attachPrinter();
130 void detachPrinter();
131 void endPrint();
132
133 void setAVStream(mAVStream*);
134 void clearAVStream();
135
136 void clearOverride();
137
138 void startVideoLog(const QString& path);
139 void endVideoLog();
140
141signals:
142 void started();
143 void paused();
144 void unpaused();
145 void stopping();
146 void crashed(const QString& errorMessage);
147 void failed();
148 void frameAvailable();
149 void stateLoaded();
150 void rewound();
151
152 void rewindChanged(bool);
153 void fastForwardChanged(bool);
154
155 void unimplementedBiosCall(int);
156 void statusPosted(const QString& message);
157 void logPosted(int level, int category, const QString& log);
158
159 void imagePrinted(const QImage&);
160
161private:
162 void updateKeys();
163 int updateAutofire();
164 void finishFrame();
165
166 void updateFastForward();
167
168 mCoreThread m_threadContext{};
169
170 bool m_patched = false;
171
172 QByteArray m_buffers[2];
173 QByteArray* m_activeBuffer;
174 QByteArray* m_completeBuffer = nullptr;
175
176 std::unique_ptr<mTileCache> m_tileCache;
177 std::unique_ptr<Override> m_override;
178
179 QList<std::function<void()>> m_resetActions;
180 QList<std::function<void()>> m_frameActions;
181 QMutex m_mutex;
182
183 int m_activeKeys = 0;
184 bool m_autofire[32] = {};
185 int m_autofireStatus[32] = {};
186 int m_autofireThreshold = 1;
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}