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_ref.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 QuitCommand;
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 template <typename T> class EnumSetting;
54 
63 class Reactor final : private Observer<Setting>, private EventListener
64 {
65 public:
66  Reactor();
67  void init();
68  ~Reactor();
69 
73  void run(CommandLineParser& parser);
74 
75  void enterMainLoop();
76 
77  RTScheduler& getRTScheduler() { return *rtScheduler; }
78  EventDistributor& getEventDistributor() { return *eventDistributor; }
79  GlobalCliComm& getGlobalCliComm() { return *globalCliComm; }
80  GlobalCommandController& getGlobalCommandController() { return *globalCommandController; }
81  InputEventGenerator& getInputEventGenerator() { return *inputEventGenerator; }
82  Display& getDisplay() { assert(display); return *display; }
83  Mixer& getMixer() { return *mixer; }
84  DiskFactory& getDiskFactory() { return *diskFactory; }
85  DiskManipulator& getDiskManipulator() { return *diskManipulator; }
86  EnumSetting<int>& getMachineSetting() { return *machineSetting; }
87  FilePool& getFilePool() { return *filePool; }
88 
90 
91  void switchMachine(const std::string& machine);
93 
94  static std::vector<std::string> getHwConfigs(string_ref type);
95 
96  void block();
97  void unblock();
98 
99  // convenience methods
100  GlobalSettings& getGlobalSettings() { return *globalSettings; }
103  CliComm& getCliComm();
105  std::string getMachineID() const;
106 
107  using Board = std::unique_ptr<MSXMotherBoard>;
109  void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
110 
111 private:
112  using Boards = std::vector<Board>;
113 
114  void createMachineSetting();
115  void switchBoard(MSXMotherBoard* newBoard);
116  void deleteBoard(MSXMotherBoard* board);
117  MSXMotherBoard& getMachine(string_ref machineID) const;
118  std::vector<string_ref> getMachineIDs() const;
119 
120  // Observer<Setting>
121  void update(const Setting& setting) override;
122 
123  // EventListener
124  int signalEvent(const std::shared_ptr<const Event>& event) override;
125 
126  void unpause();
127  void pause();
128 
129  std::mutex mbMutex; // this should come first, because it's still used by
130  // the destructors of the unique_ptr below
131 
132  // note: order of unique_ptr's is important
133  std::unique_ptr<RTScheduler> rtScheduler;
134  std::unique_ptr<EventDistributor> eventDistributor;
135  std::unique_ptr<GlobalCliComm> globalCliComm;
136  std::unique_ptr<GlobalCommandController> globalCommandController;
137  std::unique_ptr<GlobalSettings> globalSettings;
138  std::unique_ptr<InputEventGenerator> inputEventGenerator;
139 #if UNIQUE_PTR_BUG // see openmsx.hh
140  std::unique_ptr<Display> display2;
141  Display* display;
142 #else
143  std::unique_ptr<Display> display;
144 #endif
145  std::unique_ptr<Mixer> mixer;
146  std::unique_ptr<DiskFactory> diskFactory;
147  std::unique_ptr<DiskManipulator> diskManipulator;
148  std::unique_ptr<DiskChanger> virtualDrive;
149  std::unique_ptr<FilePool> filePool;
150 
151  std::unique_ptr<EnumSetting<int>> machineSetting;
152  std::unique_ptr<UserSettings> userSettings;
153  std::unique_ptr<RomDatabase> softwareDatabase;
154 
155  std::unique_ptr<AfterCommand> afterCommand;
156  std::unique_ptr<QuitCommand> quitCommand;
157  std::unique_ptr<MessageCommand> messageCommand;
158  std::unique_ptr<MachineCommand> machineCommand;
159  std::unique_ptr<TestMachineCommand> testMachineCommand;
160  std::unique_ptr<CreateMachineCommand> createMachineCommand;
161  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
162  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
163  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
164  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
165  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
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<TclCallbackMessages> tclCallbackMessages;
171 
172  // Locking rules for activeBoard access:
173  // - main thread can always access activeBoard without taking a lock
174  // - changing activeBoard handle can only be done in the main thread
175  // and needs to take the mbMutex lock
176  // - non-main thread can only access activeBoard via specific
177  // member functions (atm only via enterMainLoop()), it needs to take
178  // the mbMutex lock
179  Boards boards; // unordered
180  Boards garbageBoards;
181  MSXMotherBoard* activeBoard; // either nullptr or a board inside 'boards'
182 
183  int blockedCounter;
184  bool paused;
185 
191  bool running;
192 
193  bool isInit; // has the init() method been run successfully
194 
195  friend class MachineCommand;
196  friend class TestMachineCommand;
197  friend class CreateMachineCommand;
198  friend class DeleteMachineCommand;
199  friend class ListMachinesCommand;
201  friend class StoreMachineCommand;
202  friend class RestoreMachineCommand;
203 };
204 
205 } // namespace openmsx
206 
207 #endif // REACTOR_HH
Contains the main loop of openMSX.
Definition: Reactor.hh:63
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:80
Represents the output window/screen of openMSX.
Definition: Display.hh:32
static std::vector< std::string > getHwConfigs(string_ref type)
Definition: Reactor.cc:292
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:370
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:107
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:81
CommandController & getCommandController()
Definition: Reactor.cc:282
EventDistributor & getEventDistributor()
Definition: Reactor.hh:78
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:335
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:86
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:263
std::string getMachineID() const
Definition: Reactor.cc:341
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:496
RTScheduler & getRTScheduler()
Definition: Reactor.hh:77
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:100
DiskFactory & getDiskFactory()
Definition: Reactor.hh:84
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:79
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:287
FilePool & getFilePool()
Definition: Reactor.hh:87
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:85
void unblock()
Definition: Reactor.cc:577
Mixer & getMixer()
Definition: Reactor.hh:83
void enterMainLoop()
Definition: Reactor.cc:480
CliComm & getCliComm()
Definition: Reactor.cc:272
Board createEmptyMotherBoard()
Definition: Reactor.cc:365
Display & getDisplay()
Definition: Reactor.hh:82
void switchMachine(const std::string &machine)
Definition: Reactor.cc:394
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)...
Interpreter & getInterpreter()
Definition: Reactor.cc:277