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  // 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  bool doOneIteration();
132 
133  void unpause();
134  void pause();
135 
136  std::mutex mbMutex; // this should come first, because it's still used by
137  // the destructors of the unique_ptr below
138 
139  // note: order of unique_ptr's is important
140  std::unique_ptr<RTScheduler> rtScheduler;
141  std::unique_ptr<EventDistributor> eventDistributor;
142  std::unique_ptr<GlobalCliComm> globalCliComm;
143  std::unique_ptr<GlobalCommandController> globalCommandController;
144  std::unique_ptr<GlobalSettings> globalSettings;
145  std::unique_ptr<InputEventGenerator> inputEventGenerator;
146  std::unique_ptr<Display> display;
147  std::unique_ptr<Mixer> mixer;
148  std::unique_ptr<DiskFactory> diskFactory;
149  std::unique_ptr<DiskManipulator> diskManipulator;
150  std::unique_ptr<DiskChanger> virtualDrive;
151  std::unique_ptr<FilePool> filePool;
152 
153  std::unique_ptr<EnumSetting<int>> machineSetting;
154  std::unique_ptr<UserSettings> userSettings;
155  std::unique_ptr<RomDatabase> softwareDatabase;
156 
157  std::unique_ptr<AfterCommand> afterCommand;
158  std::unique_ptr<ExitCommand> exitCommand;
159  std::unique_ptr<MessageCommand> messageCommand;
160  std::unique_ptr<MachineCommand> machineCommand;
161  std::unique_ptr<TestMachineCommand> testMachineCommand;
162  std::unique_ptr<CreateMachineCommand> createMachineCommand;
163  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
164  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
165  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
166  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
167  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
168  std::unique_ptr<GetClipboardCommand> getClipboardCommand;
169  std::unique_ptr<SetClipboardCommand> setClipboardCommand;
170  std::unique_ptr<AviRecorder> aviRecordCommand;
171  std::unique_ptr<ConfigInfo> extensionInfo;
172  std::unique_ptr<ConfigInfo> machineInfo;
173  std::unique_ptr<RealTimeInfo> realTimeInfo;
174  std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
175  std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
176 
177  // Locking rules for activeBoard access:
178  // - main thread can always access activeBoard without taking a lock
179  // - changing activeBoard handle can only be done in the main thread
180  // and needs to take the mbMutex lock
181  // - non-main thread can only access activeBoard via specific
182  // member functions (atm only via enterMainLoop()), it needs to take
183  // the mbMutex lock
184  std::vector<Board> boards; // unordered
185  std::vector<Board> garbageBoards;
186  MSXMotherBoard* activeBoard = nullptr; // 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
openmsx::EnumSetting
Definition: Reactor.hh:54
openmsx::CommandController
Definition: CommandController.hh:17
openmsx::StoreMachineCommand
Definition: Reactor.cc:137
openmsx::Reactor::getDisplay
Display & getDisplay()
Definition: Reactor.hh:85
openmsx::DiskFactory
Definition: DiskFactory.hh:14
openmsx::Reactor::getOpenMSXInfoCommand
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:321
openmsx::Reactor::getDiskManipulator
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:88
openmsx::Reactor::unblock
void unblock()
Definition: Reactor.cc:614
openmsx::CreateMachineCommand
Definition: Reactor.cc:95
openmsx::InputEventGenerator
Definition: InputEventGenerator.hh:17
openmsx::Reactor::getFilePool
FilePool & getFilePool()
Definition: Reactor.hh:90
EventListener.hh
openmsx::Reactor::getEventDistributor
EventDistributor & getEventDistributor()
Definition: Reactor.hh:81
openmsx::Reactor::block
void block()
Definition: Reactor.cc:607
openmsx::RTScheduler
Definition: RTScheduler.hh:19
openmsx::Reactor::Board
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:110
openmsx::DiskChanger
DiskChanger
Definition: DiskChanger.cc:415
openmsx::Setting
Definition: Setting.hh:119
openmsx::EventDistributor
Definition: EventDistributor.hh:16
openmsx::Reactor::replaceBoard
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:400
openmsx::GlobalCliComm
Definition: GlobalCliComm.hh:15
openmsx::Reactor::createEmptyMotherBoard
Board createEmptyMotherBoard()
Definition: Reactor.cc:395
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:66
openmsx::MachineCommand
Definition: Reactor.cc:73
Observer.hh
openmsx::Reactor::getMotherBoard
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:368
openmsx::Reactor::getMachineID
std::string_view getMachineID() const
Definition: Reactor.cc:374
openmsx::Reactor::getMixer
Mixer & getMixer()
Definition: Reactor.hh:86
openmsx::TestMachineCommand
Definition: Reactor.cc:84
openmsx::Reactor::Reactor
Reactor()
openmsx::Reactor::getInterpreter
Interpreter & getInterpreter()
Definition: Reactor.cc:311
openmsx::InfoCommand
Definition: InfoCommand.hh:11
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:59
openmsx::Reactor::getInputEventGenerator
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:84
openmsx::Reactor::getCommandController
CommandController & getCommandController()
Definition: Reactor.cc:316
openmsx::Mixer
Definition: Mixer.hh:18
openmsx::Reactor::enterMainLoop
void enterMainLoop()
Definition: Reactor.cc:503
openmsx::ActivateMachineCommand
Definition: Reactor.cc:126
openmsx::EventListener
Definition: EventListener.hh:10
openmsx::DiskManipulator
Definition: DiskManipulator.hh:18
openmsx::CommandLineParser
Definition: CommandLineParser.hh:34
openmsx::Reactor::run
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:519
openmsx::Reactor::getGlobalSettings
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:103
openmsx::Reactor::switchMachine
void switchMachine(const std::string &machine)
Definition: Reactor.cc:424
openmsx::Reactor::getGlobalCliComm
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:82
openmsx::GlobalSettings
This class contains settings that are used by several other class (including some singletons).
Definition: GlobalSettings.hh:24
openmsx::Reactor::getCliComm
CliComm & getCliComm()
Definition: Reactor.cc:306
openmsx::Reactor::getDiskFactory
DiskFactory & getDiskFactory()
Definition: Reactor.hh:87
openmsx::Reactor::~Reactor
~Reactor()
Definition: Reactor.cc:284
openmsx::Display
Represents the output window/screen of openMSX.
Definition: Display.hh:31
openmsx::RomDatabase
Definition: RomDatabase.hh:14
openmsx::CliComm
Definition: CliComm.hh:10
openmsx::FilePool
Definition: FilePool.hh:25
openmsx::Reactor::getHwConfigs
static std::vector< std::string > getHwConfigs(std::string_view type)
Definition: Reactor.cc:326
openmsx::Reactor::getMachineSetting
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:89
openmsx::RestoreMachineCommand
Definition: Reactor.cc:148
openmsx::Interpreter
Definition: Interpreter.hh:16
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:35
openmsx::Reactor::getGlobalCommandController
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:83
openmsx::DeleteMachineCommand
Definition: Reactor.cc:105
openmsx::Reactor::getSoftwareDatabase
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:298
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::exitCode
int exitCode
Definition: Reactor.cc:61
openmsx::Reactor::init
void init()
Definition: Reactor.cc:212
openmsx::Observer
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
openmsx::Reactor::getRTScheduler
RTScheduler & getRTScheduler()
Definition: Reactor.hh:80
openmsx::ListMachinesCommand
Definition: Reactor.cc:116