openMSX
Reactor.hh
Go to the documentation of this file.
1#ifndef REACTOR_HH
2#define REACTOR_HH
3
4#include "Observer.hh"
5#include "EnumSetting.hh"
6#include "EventListener.hh"
7#include "view.hh"
8#include <cassert>
9#include <memory>
10#include <mutex>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace openmsx {
16
17class RTScheduler;
18class EventDistributor;
19class CommandController;
20class InfoCommand;
21class GlobalCliComm;
22class GlobalCommandController;
23class GlobalSettings;
24class CliComm;
25class ImGuiManager;
26class Interpreter;
27class Display;
28class Mixer;
29class InputEventGenerator;
30class DiskFactory;
31class DiskManipulator;
32class DiskChanger;
33class FilePool;
34class HotKey;
35class UserSettings;
36class RomDatabase;
37class TclCallbackMessages;
38class MsxChar2Unicode;
39class MSXMotherBoard;
40class Setting;
41class CommandLineParser;
42class AfterCommand;
43class ExitCommand;
44class MessageCommand;
45class MachineCommand;
46class TestMachineCommand;
47class CreateMachineCommand;
48class DeleteMachineCommand;
49class ListMachinesCommand;
50class ActivateMachineCommand;
51class StoreMachineCommand;
52class RestoreMachineCommand;
53class GetClipboardCommand;
54class SetClipboardCommand;
55class AviRecorder;
56class ConfigInfo;
57class RealTimeInfo;
58class SoftwareInfoTopic;
59class SymbolManager;
60
61extern int exitCode;
62
71class Reactor final : private Observer<Setting>, private EventListener
72{
73public:
75 void init();
76 ~Reactor();
77
81 void run(CommandLineParser& parser);
82
83 void enterMainLoop();
84
85 [[nodiscard]] RTScheduler& getRTScheduler() { return *rtScheduler; }
86 [[nodiscard]] EventDistributor& getEventDistributor() { return *eventDistributor; }
87 [[nodiscard]] GlobalCliComm& getGlobalCliComm() { return *globalCliComm; }
88 [[nodiscard]] GlobalCommandController& getGlobalCommandController() { return *globalCommandController; }
89 [[nodiscard]] InputEventGenerator& getInputEventGenerator() { return *inputEventGenerator; }
90 [[nodiscard]] Display& getDisplay() { assert(display); return *display; }
91 [[nodiscard]] Mixer& getMixer();
92 [[nodiscard]] DiskFactory& getDiskFactory() { return *diskFactory; }
93 [[nodiscard]] DiskManipulator& getDiskManipulator() { return *diskManipulator; }
94 [[nodiscard]] EnumSetting<int>& getMachineSetting() { return *machineSetting; }
95 [[nodiscard]] FilePool& getFilePool() { return *filePool; }
96 [[nodiscard]] ImGuiManager& getImGuiManager() { return *imGuiManager; }
97 [[nodiscard]] const HotKey& getHotKey() const;
98 [[nodiscard]] SymbolManager& getSymbolManager() const { return *symbolManager; }
99
100 [[nodiscard]] RomDatabase& getSoftwareDatabase();
101
102 void switchMachine(const std::string& machine);
103 [[nodiscard]] MSXMotherBoard* getMotherBoard() const;
104
105 [[nodiscard]] static std::vector<std::string> getHwConfigs(std::string_view type);
106
107 [[nodiscard]] const MsxChar2Unicode& getMsxChar2Unicode() const;
108
109 void block();
110 void unblock();
111
112 // convenience methods
113 [[nodiscard]] GlobalSettings& getGlobalSettings() { return *globalSettings; }
114 [[nodiscard]] InfoCommand& getOpenMSXInfoCommand();
116 [[nodiscard]] CliComm& getCliComm();
117 [[nodiscard]] Interpreter& getInterpreter();
118 [[nodiscard]] std::string_view getMachineID() const;
119
120 using Board = std::shared_ptr<MSXMotherBoard>;
121 [[nodiscard]] Board createEmptyMotherBoard();
122 void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
123 [[nodiscard]] Board getMachine(std::string_view machineID) const;
124
125 [[nodiscard]] bool isFullyStarted() const { return fullyStarted; }
126
127 [[nodiscard]] auto getMachineIDs() const {
128 return view::transform(boards,
129 [](auto& b) -> std::string_view { return b->getMachineID(); });
130 }
131private:
132 void createMachineSetting();
133 void switchBoard(Board newBoard);
134 void deleteBoard(Board board);
135
136 // Observer<Setting>
137 void update(const Setting& setting) noexcept override;
138
139 // EventListener
140 int signalEvent(const Event& event) override;
141
142 // Run 1 iteration of the openMSX event loop. Typically this will
143 // emulate about 1 frame (but could be more or less depending on
144 // various factors). Returns true when openMSX wants to continue
145 // running.
146 [[nodiscard]] bool doOneIteration();
147
148 void unpause();
149 void pause();
150
151private:
152 std::mutex mbMutex; // this should come first, because it's still used by
153 // the destructors of the unique_ptr below
154
155 // note: order of unique_ptr's is important
156 std::unique_ptr<RTScheduler> rtScheduler;
157 std::unique_ptr<EventDistributor> eventDistributor;
158 std::unique_ptr<GlobalCliComm> globalCliComm;
159 std::unique_ptr<GlobalCommandController> globalCommandController;
160 std::unique_ptr<GlobalSettings> globalSettings;
161 std::unique_ptr<InputEventGenerator> inputEventGenerator;
162 std::unique_ptr<SymbolManager> symbolManager; // before imGuiManager
163 std::unique_ptr<ImGuiManager> imGuiManager; // before display
164 std::unique_ptr<Display> display;
165 std::unique_ptr<Mixer> mixer; // lazy initialized
166 std::unique_ptr<DiskFactory> diskFactory;
167 std::unique_ptr<DiskManipulator> diskManipulator;
168 std::unique_ptr<DiskChanger> virtualDrive;
169 std::unique_ptr<FilePool> filePool;
170
171 std::unique_ptr<EnumSetting<int>> machineSetting;
172 std::unique_ptr<UserSettings> userSettings;
173 std::unique_ptr<RomDatabase> softwareDatabase; // lazy initialized
174
175 std::unique_ptr<AfterCommand> afterCommand;
176 std::unique_ptr<ExitCommand> exitCommand;
177 std::unique_ptr<MessageCommand> messageCommand;
178 std::unique_ptr<MachineCommand> machineCommand;
179 std::unique_ptr<TestMachineCommand> testMachineCommand;
180 std::unique_ptr<CreateMachineCommand> createMachineCommand;
181 std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
182 std::unique_ptr<ListMachinesCommand> listMachinesCommand;
183 std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
184 std::unique_ptr<StoreMachineCommand> storeMachineCommand;
185 std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
186 std::unique_ptr<GetClipboardCommand> getClipboardCommand;
187 std::unique_ptr<SetClipboardCommand> setClipboardCommand;
188 std::unique_ptr<AviRecorder> aviRecordCommand;
189 std::unique_ptr<ConfigInfo> extensionInfo;
190 std::unique_ptr<ConfigInfo> machineInfo;
191 std::unique_ptr<RealTimeInfo> realTimeInfo;
192 std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
193 std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
194
195 // Locking rules for activeBoard access:
196 // - main thread can always access activeBoard without taking a lock
197 // - changing activeBoard handle can only be done in the main thread
198 // and needs to take the mbMutex lock
199 // - non-main thread can only access activeBoard via specific
200 // member functions (atm only via enterMainLoop()), it needs to take
201 // the mbMutex lock
202 std::vector<Board> boards; // unordered
203 Board activeBoard; // either nullptr or a board inside 'boards'
204
205 int blockedCounter = 0;
206 bool paused = false;
207 bool fullyStarted = false; // all start up actions completed
208
214 bool running = true;
215
216 bool isInit = false; // has the init() method been run successfully
217
218 friend class MachineCommand;
219 friend class TestMachineCommand;
226};
227
228} // namespace openmsx
229
230#endif // REACTOR_HH
BaseSetting * setting
Definition: Interpreter.cc:28
Represents the output window/screen of openMSX.
Definition: Display.hh:32
This class contains settings that are used by several other class (including some singletons).
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:10
Contains the main loop of openMSX.
Definition: Reactor.hh:72
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:113
ImGuiManager & getImGuiManager()
Definition: Reactor.hh:96
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:404
std::shared_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:120
CommandController & getCommandController()
Definition: Reactor.cc:329
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:93
void enterMainLoop()
Definition: Reactor.cc:515
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:88
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:334
void switchMachine(const std::string &machine)
Definition: Reactor.cc:449
Display & getDisplay()
Definition: Reactor.hh:90
void unblock()
Definition: Reactor.cc:636
CliComm & getCliComm()
Definition: Reactor.cc:319
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:531
bool isFullyStarted() const
Definition: Reactor.hh:125
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:94
DiskFactory & getDiskFactory()
Definition: Reactor.hh:92
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:87
Board createEmptyMotherBoard()
Definition: Reactor.cc:424
RTScheduler & getRTScheduler()
Definition: Reactor.hh:85
Interpreter & getInterpreter()
Definition: Reactor.cc:324
const HotKey & getHotKey() const
Definition: Reactor.cc:339
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:429
std::string_view getMachineID() const
Definition: Reactor.cc:410
EventDistributor & getEventDistributor()
Definition: Reactor.hh:86
Mixer & getMixer()
Definition: Reactor.cc:303
SymbolManager & getSymbolManager() const
Definition: Reactor.hh:98
const MsxChar2Unicode & getMsxChar2Unicode() const
Definition: Reactor.cc:370
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition: Reactor.cc:344
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:89
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:311
FilePool & getFilePool()
Definition: Reactor.hh:95
Board getMachine(std::string_view machineID) const
Definition: Reactor.cc:415
auto getMachineIDs() const
Definition: Reactor.hh:127
This file implemented 3 utility functions:
Definition: Autofire.cc:9
std::variant< KeyUpEvent, KeyDownEvent, MouseMotionEvent, MouseButtonUpEvent, MouseButtonDownEvent, MouseWheelEvent, JoystickAxisMotionEvent, JoystickHatEvent, JoystickButtonUpEvent, JoystickButtonDownEvent, OsdControlReleaseEvent, OsdControlPressEvent, WindowEvent, TextEvent, FileDropEvent, QuitEvent, FinishFrameEvent, CliCommandEvent, GroupEvent, BootEvent, FrameDrawnEvent, BreakEvent, SwitchRendererEvent, TakeReverseSnapshotEvent, AfterTimedEvent, MachineLoadedEvent, MachineActivatedEvent, MachineDeactivatedEvent, MidiInReaderEvent, MidiInWindowsEvent, MidiInCoreMidiEvent, MidiInCoreMidiVirtualEvent, MidiInALSAEvent, Rs232TesterEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition: Event.hh:450
int exitCode
Definition: Reactor.cc:63
constexpr auto transform(Range &&range, UnaryOp op)
Definition: view.hh:520