openMSX
Reactor.hh
Go to the documentation of this file.
1#ifndef REACTOR_HH
2#define REACTOR_HH
3
4#include "EnumSetting.hh"
5#include "EventListener.hh"
6#include "Observer.hh"
7
8#include "view.hh"
9
10#include <cassert>
11#include <memory>
12#include <mutex>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace openmsx {
18
19class ActivateMachineCommand;
20class AfterCommand;
21class AviRecorder;
22class CliComm;
23class CommandController;
24class CommandLineParser;
25class ConfigInfo;
26class CreateMachineCommand;
27class DeleteMachineCommand;
28class DiskChanger;
29class DiskFactory;
30class DiskManipulator;
31class Display;
32class EventDistributor;
33class ExitCommand;
34class FilePool;
35class GetClipboardCommand;
36class GlobalCliComm;
37class GlobalCommandController;
38class GlobalSettings;
39class HotKey;
40class ImGuiManager;
41class InfoCommand;
42class InputEventGenerator;
43class Interpreter;
44class ListMachinesCommand;
45class MSXMotherBoard;
46class MachineCommand;
47class MessageCommand;
48class Mixer;
49class MsxChar2Unicode;
50class RTScheduler;
51class RealTimeInfo;
52class RestoreMachineCommand;
53class RomDatabase;
54class SetClipboardCommand;
55class Setting;
56class Shortcuts;
57class SoftwareInfoTopic;
58class StoreMachineCommand;
59class SymbolManager;
60class TclCallbackMessages;
61class TestMachineCommand;
62class UserSettings;
63
64extern int exitCode;
65
74class Reactor final : private Observer<Setting>, private EventListener
75{
76public:
78 void init();
79 ~Reactor();
80
84 void run(const CommandLineParser& parser);
85
86 void enterMainLoop();
87
88 [[nodiscard]] Shortcuts& getShortcuts() { return *shortcuts; }
89 [[nodiscard]] RTScheduler& getRTScheduler() { return *rtScheduler; }
90 [[nodiscard]] EventDistributor& getEventDistributor() { return *eventDistributor; }
91 [[nodiscard]] GlobalCliComm& getGlobalCliComm() { return *globalCliComm; }
92 [[nodiscard]] GlobalCommandController& getGlobalCommandController() { return *globalCommandController; }
93 [[nodiscard]] InputEventGenerator& getInputEventGenerator() { return *inputEventGenerator; }
94 [[nodiscard]] Display& getDisplay() { assert(display); return *display; }
95 [[nodiscard]] Mixer& getMixer();
96 [[nodiscard]] DiskFactory& getDiskFactory() { return *diskFactory; }
97 [[nodiscard]] DiskManipulator& getDiskManipulator() { return *diskManipulator; }
98 [[nodiscard]] EnumSetting<int>& getMachineSetting() { return *machineSetting; }
99 [[nodiscard]] FilePool& getFilePool() { return *filePool; }
100 [[nodiscard]] ImGuiManager& getImGuiManager() { return *imGuiManager; }
101 [[nodiscard]] const HotKey& getHotKey() const;
102 [[nodiscard]] SymbolManager& getSymbolManager() const { return *symbolManager; }
103 [[nodiscard]] AviRecorder& getRecorder() const { return *aviRecordCommand; }
104
105 [[nodiscard]] RomDatabase& getSoftwareDatabase();
106
107 void switchMachine(const std::string& machine);
108 [[nodiscard]] MSXMotherBoard* getMotherBoard() const;
109
110 [[nodiscard]] static std::vector<std::string> getHwConfigs(std::string_view type);
111
112 [[nodiscard]] const MsxChar2Unicode& getMsxChar2Unicode() const;
113
114 void block();
115 void unblock();
116
117 // convenience methods
118 [[nodiscard]] GlobalSettings& getGlobalSettings() { return *globalSettings; }
119 [[nodiscard]] InfoCommand& getOpenMSXInfoCommand();
121 [[nodiscard]] CliComm& getCliComm();
122 [[nodiscard]] Interpreter& getInterpreter();
123 [[nodiscard]] std::string_view getMachineID() const;
124
125 using Board = std::shared_ptr<MSXMotherBoard>;
126 [[nodiscard]] Board createEmptyMotherBoard();
127 void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
128 [[nodiscard]] Board getMachine(std::string_view machineID) const;
129
130 [[nodiscard]] bool isFullyStarted() const { return fullyStarted; }
131
132 [[nodiscard]] auto getMachineIDs() const {
133 return view::transform(boards,
134 [](auto& b) -> std::string_view { return b->getMachineID(); });
135 }
136private:
137 void createMachineSetting();
138 void switchBoard(Board newBoard);
139 void deleteBoard(Board board);
140
141 // Observer<Setting>
142 void update(const Setting& setting) noexcept override;
143
144 // EventListener
145 bool signalEvent(const Event& event) override;
146
147 // Run 1 iteration of the openMSX event loop. Typically this will
148 // emulate about 1 frame (but could be more or less depending on
149 // various factors). Returns true when openMSX wants to continue
150 // running.
151 [[nodiscard]] bool doOneIteration();
152
153 void unpause();
154 void pause();
155
156private:
157 std::mutex mbMutex; // this should come first, because it's still used by
158 // the destructors of the unique_ptr below
159
160 // note: order of unique_ptr's is important
161 std::unique_ptr<Shortcuts> shortcuts; // before globalCommandController
162 std::unique_ptr<RTScheduler> rtScheduler;
163 std::unique_ptr<EventDistributor> eventDistributor;
164 std::unique_ptr<GlobalCliComm> globalCliComm;
165 std::unique_ptr<GlobalCommandController> globalCommandController;
166 std::unique_ptr<GlobalSettings> globalSettings;
167 std::unique_ptr<InputEventGenerator> inputEventGenerator;
168 std::unique_ptr<SymbolManager> symbolManager; // before imGuiManager
169 std::unique_ptr<ImGuiManager> imGuiManager; // before display
170 std::unique_ptr<Display> display;
171 std::unique_ptr<Mixer> mixer; // lazy initialized
172 std::unique_ptr<DiskFactory> diskFactory;
173 std::unique_ptr<DiskManipulator> diskManipulator;
174 std::unique_ptr<DiskChanger> virtualDrive;
175 std::unique_ptr<FilePool> filePool;
176
177 std::unique_ptr<EnumSetting<int>> machineSetting;
178 std::unique_ptr<UserSettings> userSettings;
179 std::unique_ptr<RomDatabase> softwareDatabase; // lazy initialized
180
181 std::unique_ptr<AfterCommand> afterCommand;
182 std::unique_ptr<ExitCommand> exitCommand;
183 std::unique_ptr<MessageCommand> messageCommand;
184 std::unique_ptr<MachineCommand> machineCommand;
185 std::unique_ptr<TestMachineCommand> testMachineCommand;
186 std::unique_ptr<CreateMachineCommand> createMachineCommand;
187 std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
188 std::unique_ptr<ListMachinesCommand> listMachinesCommand;
189 std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
190 std::unique_ptr<StoreMachineCommand> storeMachineCommand;
191 std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
192 std::unique_ptr<GetClipboardCommand> getClipboardCommand;
193 std::unique_ptr<SetClipboardCommand> setClipboardCommand;
194 std::unique_ptr<AviRecorder> aviRecordCommand;
195 std::unique_ptr<ConfigInfo> extensionInfo;
196 std::unique_ptr<ConfigInfo> machineInfo;
197 std::unique_ptr<RealTimeInfo> realTimeInfo;
198 std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
199 std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
200
201 // Locking rules for activeBoard access:
202 // - main thread can always access activeBoard without taking a lock
203 // - changing activeBoard handle can only be done in the main thread
204 // and needs to take the mbMutex lock
205 // - non-main thread can only access activeBoard via specific
206 // member functions (atm only via enterMainLoop()), it needs to take
207 // the mbMutex lock
208 std::vector<Board> boards; // unordered
209 Board activeBoard; // either nullptr or a board inside 'boards'
210
211 int blockedCounter = 0;
212 bool paused = false;
213 bool fullyStarted = false; // all start up actions completed
214
220 bool running = true;
221
222 bool isInit = false; // has the init() method been run successfully
223
224 friend class MachineCommand;
225 friend class TestMachineCommand;
232};
233
234} // namespace openmsx
235
236#endif // REACTOR_HH
BaseSetting * setting
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:75
GlobalSettings & getGlobalSettings()
Definition Reactor.hh:118
ImGuiManager & getImGuiManager()
Definition Reactor.hh:100
MSXMotherBoard * getMotherBoard() const
Definition Reactor.cc:409
AviRecorder & getRecorder() const
Definition Reactor.hh:103
std::shared_ptr< MSXMotherBoard > Board
Definition Reactor.hh:125
CommandController & getCommandController()
Definition Reactor.cc:333
DiskManipulator & getDiskManipulator()
Definition Reactor.hh:97
void enterMainLoop()
Definition Reactor.cc:520
GlobalCommandController & getGlobalCommandController()
Definition Reactor.hh:92
InfoCommand & getOpenMSXInfoCommand()
Definition Reactor.cc:338
void switchMachine(const std::string &machine)
Definition Reactor.cc:454
void run(const CommandLineParser &parser)
Main loop.
Definition Reactor.cc:536
Display & getDisplay()
Definition Reactor.hh:94
CliComm & getCliComm()
Definition Reactor.cc:323
bool isFullyStarted() const
Definition Reactor.hh:130
EnumSetting< int > & getMachineSetting()
Definition Reactor.hh:98
DiskFactory & getDiskFactory()
Definition Reactor.hh:96
GlobalCliComm & getGlobalCliComm()
Definition Reactor.hh:91
Board createEmptyMotherBoard()
Definition Reactor.cc:429
Shortcuts & getShortcuts()
Definition Reactor.hh:88
RTScheduler & getRTScheduler()
Definition Reactor.hh:89
Interpreter & getInterpreter()
Definition Reactor.cc:328
const HotKey & getHotKey() const
Definition Reactor.cc:343
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition Reactor.cc:434
std::string_view getMachineID() const
Definition Reactor.cc:415
EventDistributor & getEventDistributor()
Definition Reactor.hh:90
Mixer & getMixer()
Definition Reactor.cc:307
SymbolManager & getSymbolManager() const
Definition Reactor.hh:102
const MsxChar2Unicode & getMsxChar2Unicode() const
Definition Reactor.cc:374
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition Reactor.cc:348
InputEventGenerator & getInputEventGenerator()
Definition Reactor.hh:93
RomDatabase & getSoftwareDatabase()
Definition Reactor.cc:315
FilePool & getFilePool()
Definition Reactor.hh:99
Board getMachine(std::string_view machineID) const
Definition Reactor.cc:420
auto getMachineIDs() const
Definition Reactor.hh:132
This file implemented 3 utility functions:
Definition Autofire.cc:11
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, Rs232NetEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition Event.hh:445
int exitCode
Definition Reactor.cc:67
constexpr auto transform(Range &&range, UnaryOp op)
Definition view.hh:520