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 "EventListener.hh"
6 #include "view.hh"
7 #include <cassert>
8 #include <memory>
9 #include <mutex>
10 #include <string>
11 #include <string_view>
12 #include <vector>
13 
14 namespace openmsx {
15 
16 class RTScheduler;
17 class EventDistributor;
18 class CommandController;
19 class InfoCommand;
20 class GlobalCliComm;
21 class GlobalCommandController;
22 class GlobalSettings;
23 class CliComm;
24 class Interpreter;
25 class Display;
26 class Mixer;
27 class InputEventGenerator;
28 class DiskFactory;
29 class DiskManipulator;
30 class DiskChanger;
31 class FilePool;
32 class UserSettings;
33 class RomDatabase;
34 class TclCallbackMessages;
35 class MSXMotherBoard;
36 class Setting;
37 class CommandLineParser;
38 class AfterCommand;
39 class ExitCommand;
40 class MessageCommand;
41 class MachineCommand;
42 class TestMachineCommand;
43 class CreateMachineCommand;
44 class DeleteMachineCommand;
45 class ListMachinesCommand;
46 class ActivateMachineCommand;
47 class StoreMachineCommand;
48 class RestoreMachineCommand;
49 class GetClipboardCommand;
50 class SetClipboardCommand;
51 class AviRecorder;
52 class ConfigInfo;
53 class RealTimeInfo;
54 class SoftwareInfoTopic;
55 template<typename T> class EnumSetting;
56 
57 extern int exitCode;
58 
67 class Reactor final : private Observer<Setting>, private EventListener
68 {
69 public:
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();
106  [[nodiscard]] CommandController& getCommandController();
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 private:
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) noexcept 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 
140 private:
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;
206  friend class CreateMachineCommand;
207  friend class DeleteMachineCommand;
208  friend class ListMachinesCommand;
210  friend class StoreMachineCommand;
211  friend class RestoreMachineCommand;
212 };
213 
214 } // namespace openmsx
215 
216 #endif // REACTOR_HH
BaseSetting * setting
Definition: Interpreter.cc:27
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
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:374
std::shared_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:111
CommandController & getCommandController()
Definition: Reactor.cc:322
void enterMainLoop()
Definition: Reactor.cc:487
EventDistributor & getEventDistributor()
Definition: Reactor.hh:82
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:327
void switchMachine(const std::string &machine)
Definition: Reactor.cc:420
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:104
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:83
Display & getDisplay()
Definition: Reactor.hh:86
void unblock()
Definition: Reactor.cc:602
CliComm & getCliComm()
Definition: Reactor.cc:312
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:503
DiskFactory & getDiskFactory()
Definition: Reactor.hh:88
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:90
RTScheduler & getRTScheduler()
Definition: Reactor.hh:81
Board createEmptyMotherBoard()
Definition: Reactor.cc:395
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:85
Interpreter & getInterpreter()
Definition: Reactor.cc:317
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:84
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:400
std::string_view getMachineID() const
Definition: Reactor.cc:380
Mixer & getMixer()
Definition: Reactor.cc:296
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition: Reactor.cc:332
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:89
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:304
FilePool & getFilePool()
Definition: Reactor.hh:91
This file implemented 3 utility functions:
Definition: Autofire.cc:9
int exitCode
Definition: Reactor.cc:59
constexpr auto transform(Range &&range, UnaryOp op)
Definition: view.hh:392