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 Interpreter;
26class Display;
27class Mixer;
28class InputEventGenerator;
29class DiskFactory;
30class DiskManipulator;
31class DiskChanger;
32class FilePool;
33class UserSettings;
34class RomDatabase;
35class TclCallbackMessages;
36class MSXMotherBoard;
37class Setting;
38class CommandLineParser;
39class AfterCommand;
40class ExitCommand;
41class MessageCommand;
42class MachineCommand;
43class TestMachineCommand;
44class CreateMachineCommand;
45class DeleteMachineCommand;
46class ListMachinesCommand;
47class ActivateMachineCommand;
48class StoreMachineCommand;
49class RestoreMachineCommand;
50class GetClipboardCommand;
51class SetClipboardCommand;
52class AviRecorder;
53class ConfigInfo;
54class RealTimeInfo;
55class SoftwareInfoTopic;
56
57extern int exitCode;
58
67class Reactor final : private Observer<Setting>, private EventListener
68{
69public:
71 void init();
72 ~Reactor();
73
77 void run(CommandLineParser& parser);
78
79 void enterMainLoop();
80
81 [[nodiscard]] RTScheduler& getRTScheduler() { return *rtScheduler; }
82 [[nodiscard]] EventDistributor& getEventDistributor() { return *eventDistributor; }
83 [[nodiscard]] GlobalCliComm& getGlobalCliComm() { return *globalCliComm; }
84 [[nodiscard]] GlobalCommandController& getGlobalCommandController() { return *globalCommandController; }
85 [[nodiscard]] InputEventGenerator& getInputEventGenerator() { return *inputEventGenerator; }
86 [[nodiscard]] Display& getDisplay() { assert(display); return *display; }
87 [[nodiscard]] Mixer& getMixer();
88 [[nodiscard]] DiskFactory& getDiskFactory() { return *diskFactory; }
89 [[nodiscard]] DiskManipulator& getDiskManipulator() { return *diskManipulator; }
90 [[nodiscard]] EnumSetting<int>& getMachineSetting() { return *machineSetting; }
91 [[nodiscard]] FilePool& getFilePool() { return *filePool; }
92
93 [[nodiscard]] RomDatabase& getSoftwareDatabase();
94
95 void switchMachine(const std::string& machine);
96 [[nodiscard]] MSXMotherBoard* getMotherBoard() const;
97
98 [[nodiscard]] static std::vector<std::string> getHwConfigs(std::string_view type);
99
100 void block();
101 void unblock();
102
103 // convenience methods
104 [[nodiscard]] GlobalSettings& getGlobalSettings() { return *globalSettings; }
105 [[nodiscard]] InfoCommand& getOpenMSXInfoCommand();
107 [[nodiscard]] CliComm& getCliComm();
108 [[nodiscard]] Interpreter& getInterpreter();
109 [[nodiscard]] std::string_view getMachineID() const;
110
111 using Board = std::shared_ptr<MSXMotherBoard>;
112 [[nodiscard]] Board createEmptyMotherBoard();
113 void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
114
115 [[nodiscard]] bool isFullyStarted() const { return fullyStarted; }
116
117private:
118 void createMachineSetting();
119 void switchBoard(Board newBoard);
120 void deleteBoard(Board board);
121 [[nodiscard]] Board getMachine(std::string_view machineID) const;
122 [[nodiscard]] auto getMachineIDs() const {
123 return view::transform(boards,
124 [](auto& b) -> std::string_view { return b->getMachineID(); });
125 }
126
127 // Observer<Setting>
128 void update(const Setting& setting) noexcept override;
129
130 // EventListener
131 int signalEvent(const Event& event) override;
132
133 // Run 1 iteration of the openMSX event loop. Typically this will
134 // emulate about 1 frame (but could be more or less depending on
135 // various factors). Returns true when openMSX wants to continue
136 // running.
137 [[nodiscard]] bool doOneIteration();
138
139 void unpause();
140 void pause();
141
142private:
143 std::mutex mbMutex; // this should come first, because it's still used by
144 // the destructors of the unique_ptr below
145
146 // note: order of unique_ptr's is important
147 std::unique_ptr<RTScheduler> rtScheduler;
148 std::unique_ptr<EventDistributor> eventDistributor;
149 std::unique_ptr<GlobalCliComm> globalCliComm;
150 std::unique_ptr<GlobalCommandController> globalCommandController;
151 std::unique_ptr<GlobalSettings> globalSettings;
152 std::unique_ptr<InputEventGenerator> inputEventGenerator;
153 std::unique_ptr<Display> display;
154 std::unique_ptr<Mixer> mixer; // lazy initialized
155 std::unique_ptr<DiskFactory> diskFactory;
156 std::unique_ptr<DiskManipulator> diskManipulator;
157 std::unique_ptr<DiskChanger> virtualDrive;
158 std::unique_ptr<FilePool> filePool;
159
160 std::unique_ptr<EnumSetting<int>> machineSetting;
161 std::unique_ptr<UserSettings> userSettings;
162 std::unique_ptr<RomDatabase> softwareDatabase; // lazy initialized
163
164 std::unique_ptr<AfterCommand> afterCommand;
165 std::unique_ptr<ExitCommand> exitCommand;
166 std::unique_ptr<MessageCommand> messageCommand;
167 std::unique_ptr<MachineCommand> machineCommand;
168 std::unique_ptr<TestMachineCommand> testMachineCommand;
169 std::unique_ptr<CreateMachineCommand> createMachineCommand;
170 std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
171 std::unique_ptr<ListMachinesCommand> listMachinesCommand;
172 std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
173 std::unique_ptr<StoreMachineCommand> storeMachineCommand;
174 std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
175 std::unique_ptr<GetClipboardCommand> getClipboardCommand;
176 std::unique_ptr<SetClipboardCommand> setClipboardCommand;
177 std::unique_ptr<AviRecorder> aviRecordCommand;
178 std::unique_ptr<ConfigInfo> extensionInfo;
179 std::unique_ptr<ConfigInfo> machineInfo;
180 std::unique_ptr<RealTimeInfo> realTimeInfo;
181 std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
182 std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
183
184 // Locking rules for activeBoard access:
185 // - main thread can always access activeBoard without taking a lock
186 // - changing activeBoard handle can only be done in the main thread
187 // and needs to take the mbMutex lock
188 // - non-main thread can only access activeBoard via specific
189 // member functions (atm only via enterMainLoop()), it needs to take
190 // the mbMutex lock
191 std::vector<Board> boards; // unordered
192 Board activeBoard; // either nullptr or a board inside 'boards'
193
194 int blockedCounter = 0;
195 bool paused = false;
196 bool fullyStarted = false; // all start up actions completed
197
203 bool running = true;
204
205 bool isInit = false; // has the init() method been run successfully
206
207 friend class MachineCommand;
208 friend class TestMachineCommand;
215};
216
217} // namespace openmsx
218
219#endif // REACTOR_HH
BaseSetting * setting
Definition: Interpreter.cc:28
Represents the output window/screen of openMSX.
Definition: Display.hh:33
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:68
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:104
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:375
std::shared_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:111
CommandController & getCommandController()
Definition: Reactor.cc:323
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:89
void enterMainLoop()
Definition: Reactor.cc:487
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:84
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:328
void switchMachine(const std::string &machine)
Definition: Reactor.cc:420
Display & getDisplay()
Definition: Reactor.hh:86
void unblock()
Definition: Reactor.cc:608
CliComm & getCliComm()
Definition: Reactor.cc:313
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:503
bool isFullyStarted() const
Definition: Reactor.hh:115
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:90
DiskFactory & getDiskFactory()
Definition: Reactor.hh:88
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:83
Board createEmptyMotherBoard()
Definition: Reactor.cc:395
RTScheduler & getRTScheduler()
Definition: Reactor.hh:81
Interpreter & getInterpreter()
Definition: Reactor.cc:318
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:400
std::string_view getMachineID() const
Definition: Reactor.cc:381
EventDistributor & getEventDistributor()
Definition: Reactor.hh:82
Mixer & getMixer()
Definition: Reactor.cc:297
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition: Reactor.cc:333
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:85
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:305
FilePool & getFilePool()
Definition: Reactor.hh:91
This file implemented 3 utility functions:
Definition: Autofire.cc:9
int exitCode
Definition: Reactor.cc:60
constexpr auto transform(Range &&range, UnaryOp op)
Definition: view.hh:458