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
115private:
116 void createMachineSetting();
117 void switchBoard(Board newBoard);
118 void deleteBoard(Board board);
119 [[nodiscard]] Board getMachine(std::string_view machineID) const;
120 [[nodiscard]] auto getMachineIDs() const {
121 return view::transform(boards,
122 [](auto& b) -> std::string_view { return b->getMachineID(); });
123 }
124
125 // Observer<Setting>
126 void update(const Setting& setting) noexcept override;
127
128 // EventListener
129 int signalEvent(const Event& event) override;
130
131 // Run 1 iteration of the openMSX event loop. Typically this will
132 // emulate about 1 frame (but could be more or less depending on
133 // various factors). Returns true when openMSX wants to continue
134 // running.
135 [[nodiscard]] bool doOneIteration();
136
137 void unpause();
138 void pause();
139
140private:
141 std::mutex mbMutex; // this should come first, because it's still used by
142 // the destructors of the unique_ptr below
143
144 // note: order of unique_ptr's is important
145 std::unique_ptr<RTScheduler> rtScheduler;
146 std::unique_ptr<EventDistributor> eventDistributor;
147 std::unique_ptr<GlobalCliComm> globalCliComm;
148 std::unique_ptr<GlobalCommandController> globalCommandController;
149 std::unique_ptr<GlobalSettings> globalSettings;
150 std::unique_ptr<InputEventGenerator> inputEventGenerator;
151 std::unique_ptr<Display> display;
152 std::unique_ptr<Mixer> mixer; // lazy initialized
153 std::unique_ptr<DiskFactory> diskFactory;
154 std::unique_ptr<DiskManipulator> diskManipulator;
155 std::unique_ptr<DiskChanger> virtualDrive;
156 std::unique_ptr<FilePool> filePool;
157
158 std::unique_ptr<EnumSetting<int>> machineSetting;
159 std::unique_ptr<UserSettings> userSettings;
160 std::unique_ptr<RomDatabase> softwareDatabase; // lazy initialized
161
162 std::unique_ptr<AfterCommand> afterCommand;
163 std::unique_ptr<ExitCommand> exitCommand;
164 std::unique_ptr<MessageCommand> messageCommand;
165 std::unique_ptr<MachineCommand> machineCommand;
166 std::unique_ptr<TestMachineCommand> testMachineCommand;
167 std::unique_ptr<CreateMachineCommand> createMachineCommand;
168 std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
169 std::unique_ptr<ListMachinesCommand> listMachinesCommand;
170 std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
171 std::unique_ptr<StoreMachineCommand> storeMachineCommand;
172 std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
173 std::unique_ptr<GetClipboardCommand> getClipboardCommand;
174 std::unique_ptr<SetClipboardCommand> setClipboardCommand;
175 std::unique_ptr<AviRecorder> aviRecordCommand;
176 std::unique_ptr<ConfigInfo> extensionInfo;
177 std::unique_ptr<ConfigInfo> machineInfo;
178 std::unique_ptr<RealTimeInfo> realTimeInfo;
179 std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
180 std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
181
182 // Locking rules for activeBoard access:
183 // - main thread can always access activeBoard without taking a lock
184 // - changing activeBoard handle can only be done in the main thread
185 // and needs to take the mbMutex lock
186 // - non-main thread can only access activeBoard via specific
187 // member functions (atm only via enterMainLoop()), it needs to take
188 // the mbMutex lock
189 std::vector<Board> boards; // unordered
190 Board activeBoard; // either nullptr or a board inside 'boards'
191
192 int blockedCounter = 0;
193 bool paused = false;
194
200 bool running = true;
201
202 bool isInit = false; // has the init() method been run successfully
203
204 friend class MachineCommand;
205 friend class TestMachineCommand;
212};
213
214} // namespace openmsx
215
216#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:602
CliComm & getCliComm()
Definition: Reactor.cc:313
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:503
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