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:
69  Reactor();
70  void init();
71  ~Reactor();
72 
76  void run(CommandLineParser& parser);
77 
78  void enterMainLoop();
79 
80  RTScheduler& getRTScheduler() { return *rtScheduler; }
81  EventDistributor& getEventDistributor() { return *eventDistributor; }
82  GlobalCliComm& getGlobalCliComm() { return *globalCliComm; }
83  GlobalCommandController& getGlobalCommandController() { return *globalCommandController; }
84  InputEventGenerator& getInputEventGenerator() { return *inputEventGenerator; }
85  Display& getDisplay() { assert(display); return *display; }
86  Mixer& getMixer() { return *mixer; }
87  DiskFactory& getDiskFactory() { return *diskFactory; }
88  DiskManipulator& getDiskManipulator() { return *diskManipulator; }
89  EnumSetting<int>& getMachineSetting() { return *machineSetting; }
90  FilePool& getFilePool() { return *filePool; }
91 
93 
94  void switchMachine(const std::string& machine);
96 
97  static std::vector<std::string> getHwConfigs(std::string_view type);
98 
99  void block();
100  void unblock();
101 
102  // convenience methods
103  GlobalSettings& getGlobalSettings() { return *globalSettings; }
106  CliComm& getCliComm();
108  std::string_view getMachineID() const;
109 
110  using Board = std::unique_ptr<MSXMotherBoard>;
112  void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
113 
114 private:
115  void createMachineSetting();
116  void switchBoard(MSXMotherBoard* newBoard);
117  void deleteBoard(MSXMotherBoard* board);
118  MSXMotherBoard& getMachine(std::string_view machineID) const;
119  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  void unpause();
128  void pause();
129 
130  std::mutex mbMutex; // this should come first, because it's still used by
131  // the destructors of the unique_ptr below
132 
133  // note: order of unique_ptr's is important
134  std::unique_ptr<RTScheduler> rtScheduler;
135  std::unique_ptr<EventDistributor> eventDistributor;
136  std::unique_ptr<GlobalCliComm> globalCliComm;
137  std::unique_ptr<GlobalCommandController> globalCommandController;
138  std::unique_ptr<GlobalSettings> globalSettings;
139  std::unique_ptr<InputEventGenerator> inputEventGenerator;
140  std::unique_ptr<Display> display;
141  std::unique_ptr<Mixer> mixer;
142  std::unique_ptr<DiskFactory> diskFactory;
143  std::unique_ptr<DiskManipulator> diskManipulator;
144  std::unique_ptr<DiskChanger> virtualDrive;
145  std::unique_ptr<FilePool> filePool;
146 
147  std::unique_ptr<EnumSetting<int>> machineSetting;
148  std::unique_ptr<UserSettings> userSettings;
149  std::unique_ptr<RomDatabase> softwareDatabase;
150 
151  std::unique_ptr<AfterCommand> afterCommand;
152  std::unique_ptr<ExitCommand> exitCommand;
153  std::unique_ptr<MessageCommand> messageCommand;
154  std::unique_ptr<MachineCommand> machineCommand;
155  std::unique_ptr<TestMachineCommand> testMachineCommand;
156  std::unique_ptr<CreateMachineCommand> createMachineCommand;
157  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
158  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
159  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
160  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
161  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
162  std::unique_ptr<GetClipboardCommand> getClipboardCommand;
163  std::unique_ptr<SetClipboardCommand> setClipboardCommand;
164  std::unique_ptr<AviRecorder> aviRecordCommand;
165  std::unique_ptr<ConfigInfo> extensionInfo;
166  std::unique_ptr<ConfigInfo> machineInfo;
167  std::unique_ptr<RealTimeInfo> realTimeInfo;
168  std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
169  std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
170 
171  // Locking rules for activeBoard access:
172  // - main thread can always access activeBoard without taking a lock
173  // - changing activeBoard handle can only be done in the main thread
174  // and needs to take the mbMutex lock
175  // - non-main thread can only access activeBoard via specific
176  // member functions (atm only via enterMainLoop()), it needs to take
177  // the mbMutex lock
178  std::vector<Board> boards; // unordered
179  std::vector<Board> garbageBoards;
180  MSXMotherBoard* activeBoard = nullptr; // either nullptr or a board inside 'boards'
181 
182  int blockedCounter = 0;
183  bool paused = false;
184 
190  bool running = true;
191 
192  bool isInit = false; // has the init() method been run successfully
193 
194  friend class MachineCommand;
195  friend class TestMachineCommand;
196  friend class CreateMachineCommand;
197  friend class DeleteMachineCommand;
198  friend class ListMachinesCommand;
200  friend class StoreMachineCommand;
201  friend class RestoreMachineCommand;
202 };
203 
204 } // namespace openmsx
205 
206 #endif // REACTOR_HH
Contains the main loop of openMSX.
Definition: Reactor.hh:66
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:83
Represents the output window/screen of openMSX.
Definition: Display.hh:31
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:400
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:110
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition: Reactor.cc:326
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:84
CommandController & getCommandController()
Definition: Reactor.cc:316
EventDistributor & getEventDistributor()
Definition: Reactor.hh:81
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:368
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:89
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:298
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:519
RTScheduler & getRTScheduler()
Definition: Reactor.hh:80
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:103
DiskFactory & getDiskFactory()
Definition: Reactor.hh:87
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:82
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:321
FilePool & getFilePool()
Definition: Reactor.hh:90
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:88
void unblock()
Definition: Reactor.cc:600
Mixer & getMixer()
Definition: Reactor.hh:86
void enterMainLoop()
Definition: Reactor.cc:503
CliComm & getCliComm()
Definition: Reactor.cc:306
Board createEmptyMotherBoard()
Definition: Reactor.cc:395
Display & getDisplay()
Definition: Reactor.hh:85
void switchMachine(const std::string &machine)
Definition: Reactor.cc:424
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
This class contains settings that are used by several other class (including some singletons)...
std::string_view getMachineID() const
Definition: Reactor.cc:374
Interpreter & getInterpreter()
Definition: Reactor.cc:311
int exitCode
Definition: Reactor.cc:61