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 "string_view.hh"
7 #include <cassert>
8 #include <memory>
9 #include <mutex>
10 #include <string>
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(string_view type);
98 
99  void block();
100  void unblock();
101 
102  // convenience methods
103  GlobalSettings& getGlobalSettings() { return *globalSettings; }
106  CliComm& getCliComm();
108  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  using Boards = std::vector<Board>;
116 
117  void createMachineSetting();
118  void switchBoard(MSXMotherBoard* newBoard);
119  void deleteBoard(MSXMotherBoard* board);
120  MSXMotherBoard& getMachine(string_view machineID) const;
121  std::vector<string_view> getMachineIDs() const;
122 
123  // Observer<Setting>
124  void update(const Setting& setting) override;
125 
126  // EventListener
127  int signalEvent(const std::shared_ptr<const Event>& event) override;
128 
129  void unpause();
130  void pause();
131 
132  std::mutex mbMutex; // this should come first, because it's still used by
133  // the destructors of the unique_ptr below
134 
135  // note: order of unique_ptr's is important
136  std::unique_ptr<RTScheduler> rtScheduler;
137  std::unique_ptr<EventDistributor> eventDistributor;
138  std::unique_ptr<GlobalCliComm> globalCliComm;
139  std::unique_ptr<GlobalCommandController> globalCommandController;
140  std::unique_ptr<GlobalSettings> globalSettings;
141  std::unique_ptr<InputEventGenerator> inputEventGenerator;
142  std::unique_ptr<Display> display;
143  std::unique_ptr<Mixer> mixer;
144  std::unique_ptr<DiskFactory> diskFactory;
145  std::unique_ptr<DiskManipulator> diskManipulator;
146  std::unique_ptr<DiskChanger> virtualDrive;
147  std::unique_ptr<FilePool> filePool;
148 
149  std::unique_ptr<EnumSetting<int>> machineSetting;
150  std::unique_ptr<UserSettings> userSettings;
151  std::unique_ptr<RomDatabase> softwareDatabase;
152 
153  std::unique_ptr<AfterCommand> afterCommand;
154  std::unique_ptr<ExitCommand> exitCommand;
155  std::unique_ptr<MessageCommand> messageCommand;
156  std::unique_ptr<MachineCommand> machineCommand;
157  std::unique_ptr<TestMachineCommand> testMachineCommand;
158  std::unique_ptr<CreateMachineCommand> createMachineCommand;
159  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
160  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
161  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
162  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
163  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
164  std::unique_ptr<GetClipboardCommand> getClipboardCommand;
165  std::unique_ptr<SetClipboardCommand> setClipboardCommand;
166  std::unique_ptr<AviRecorder> aviRecordCommand;
167  std::unique_ptr<ConfigInfo> extensionInfo;
168  std::unique_ptr<ConfigInfo> machineInfo;
169  std::unique_ptr<RealTimeInfo> realTimeInfo;
170  std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
171  std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
172 
173  // Locking rules for activeBoard access:
174  // - main thread can always access activeBoard without taking a lock
175  // - changing activeBoard handle can only be done in the main thread
176  // and needs to take the mbMutex lock
177  // - non-main thread can only access activeBoard via specific
178  // member functions (atm only via enterMainLoop()), it needs to take
179  // the mbMutex lock
180  Boards boards; // unordered
181  Boards garbageBoards;
182  MSXMotherBoard* activeBoard = nullptr; // either nullptr or a board inside 'boards'
183 
184  int blockedCounter = 0;
185  bool paused = false;
186 
192  bool running = true;
193 
194  bool isInit = false; // has the init() method been run successfully
195 
196  friend class MachineCommand;
197  friend class TestMachineCommand;
198  friend class CreateMachineCommand;
199  friend class DeleteMachineCommand;
200  friend class ListMachinesCommand;
202  friend class StoreMachineCommand;
203  friend class RestoreMachineCommand;
204 };
205 
206 } // namespace openmsx
207 
208 #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:398
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:110
static std::vector< std::string > getHwConfigs(string_view type)
Definition: Reactor.cc:324
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:84
CommandController & getCommandController()
Definition: Reactor.cc:314
EventDistributor & getEventDistributor()
Definition: Reactor.hh:81
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:366
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:89
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:296
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:517
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:319
FilePool & getFilePool()
Definition: Reactor.hh:90
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:88
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void unblock()
Definition: Reactor.cc:598
Mixer & getMixer()
Definition: Reactor.hh:86
void enterMainLoop()
Definition: Reactor.cc:501
CliComm & getCliComm()
Definition: Reactor.cc:304
Board createEmptyMotherBoard()
Definition: Reactor.cc:393
Display & getDisplay()
Definition: Reactor.hh:85
void switchMachine(const std::string &machine)
Definition: Reactor.cc:422
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)...
string_view getMachineID() const
Definition: Reactor.cc:372
Interpreter & getInterpreter()
Definition: Reactor.cc:309
int exitCode
Definition: Reactor.cc:59