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 "openmsx.hh"
8 #include <string>
9 #include <memory>
10 #include <mutex>
11 #include <vector>
12 
13 #include <iostream>
14 
15 namespace openmsx {
16 
17 class RTScheduler;
18 class EventDistributor;
19 class CommandController;
20 class InfoCommand;
21 class GlobalCliComm;
22 class GlobalCommandController;
23 class GlobalSettings;
24 class CliComm;
25 class Interpreter;
26 class Display;
27 class Mixer;
28 class InputEventGenerator;
29 class DiskFactory;
30 class DiskManipulator;
31 class DiskChanger;
32 class FilePool;
33 class UserSettings;
34 class RomDatabase;
35 class TclCallbackMessages;
36 class MSXMotherBoard;
37 class Setting;
38 class CommandLineParser;
39 class AfterCommand;
40 class ExitCommand;
41 class MessageCommand;
42 class MachineCommand;
43 class TestMachineCommand;
44 class CreateMachineCommand;
45 class DeleteMachineCommand;
46 class ListMachinesCommand;
47 class ActivateMachineCommand;
48 class StoreMachineCommand;
49 class RestoreMachineCommand;
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<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  Boards boards; // unordered
179  Boards 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:374
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:110
static std::vector< std::string > getHwConfigs(string_view type)
Definition: Reactor.cc:300
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:84
CommandController & getCommandController()
Definition: Reactor.cc:290
EventDistributor & getEventDistributor()
Definition: Reactor.hh:81
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:342
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:89
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:272
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:493
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:295
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:15
void unblock()
Definition: Reactor.cc:574
Mixer & getMixer()
Definition: Reactor.hh:86
void enterMainLoop()
Definition: Reactor.cc:477
CliComm & getCliComm()
Definition: Reactor.cc:280
Board createEmptyMotherBoard()
Definition: Reactor.cc:369
Display & getDisplay()
Definition: Reactor.hh:85
void switchMachine(const std::string &machine)
Definition: Reactor.cc:398
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:348
Interpreter & getInterpreter()
Definition: Reactor.cc:285
int exitCode
Definition: Reactor.cc:59