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