openMSX
MSXCommandController.cc
Go to the documentation of this file.
3 #include "Reactor.hh"
4 #include "MSXEventDistributor.hh"
5 #include "MSXMotherBoard.hh"
6 #include "SettingsConfig.hh"
7 #include "SettingsManager.hh"
8 #include "InfoCommand.hh"
9 #include "Interpreter.hh"
10 #include "Setting.hh"
11 #include "Event.hh"
12 #include "MSXException.hh"
13 #include "memory.hh"
14 #include <iostream>
15 
16 using std::string;
17 using std::vector;
18 
19 namespace openmsx {
20 
22  GlobalCommandController& globalCommandController_,
23  Reactor& reactor_,
24  MSXMotherBoard& motherboard_,
25  MSXEventDistributor& msxEventDistributor_,
26  const std::string& machineID_)
27  : globalCommandController(globalCommandController_)
28  , reactor(reactor_)
29  , motherboard(motherboard_)
30  , msxEventDistributor(msxEventDistributor_)
31  , machineID(machineID_)
32 {
33  globalCommandController.getInterpreter().createNamespace(machineID);
34 
35  machineInfoCommand = make_unique<InfoCommand>(*this, "machine_info");
36  machineInfoCommand->setAllowedInEmptyMachine(true);
37 
38  msxEventDistributor.registerEventListener(*this);
39 }
40 
42 {
43  msxEventDistributor.unregisterEventListener(*this);
44 
45  machineInfoCommand.reset();
46 
47  #ifndef NDEBUG
48  for (auto& p : commandMap) {
49  std::cout << "Command not unregistered: " << p.first() << std::endl;
50  }
51  for (auto& p : settingMap) {
52  std::cout << "Setting not unregistered: " << p.first() << std::endl;
53  }
54  assert(commandMap.empty());
55  assert(settingMap.empty());
56  #endif
57 
58  globalCommandController.getInterpreter().deleteNamespace(machineID);
59 }
60 
62 {
63  return globalCommandController;
64 }
65 
67 {
68  return *machineInfoCommand;
69 }
70 
72 {
73  return motherboard;
74 }
75 
76 string MSXCommandController::getFullName(string_ref name)
77 {
78  return "::" + machineID + "::" + name;
79 }
80 
81 void MSXCommandController::registerCommand(Command& command, const string& str)
82 {
83  assert(!hasCommand(str));
84  commandMap[str] = &command;
85 
86  string fullname = getFullName(str);
87  globalCommandController.registerCommand(command, fullname);
88  globalCommandController.registerProxyCommand(str);
89 
90  command.setAllowedInEmptyMachine(false);
91 }
92 
94 {
95  assert(hasCommand(str));
96  commandMap.erase(str);
97 
98  globalCommandController.unregisterProxyCommand(str);
99  string fullname = getFullName(str);
100  globalCommandController.unregisterCommand(command, fullname);
101 }
102 
104  string_ref str)
105 {
106  string fullname = getFullName(str);
107  globalCommandController.registerCompleter(completer, fullname);
108 }
109 
111  string_ref str)
112 {
113  string fullname = getFullName(str);
114  globalCommandController.unregisterCompleter(completer, fullname);
115 }
116 
118 {
119  const string& name = setting.getName();
120  assert(!findSetting(name));
121  settingMap[name] = &setting;
122 
123  globalCommandController.registerProxySetting(setting);
124  string fullname = getFullName(name);
125  globalCommandController.getSettingsConfig().getSettingsManager()
126  .registerSetting(setting, fullname);
127  globalCommandController.getInterpreter().registerSetting(setting, fullname);
128 }
129 
131 {
132  const string& name = setting.getName();
133  assert(findSetting(name));
134  settingMap.erase(name);
135 
136  globalCommandController.unregisterProxySetting(setting);
137  string fullname = getFullName(name);
138  globalCommandController.getInterpreter().unregisterSetting(setting, fullname);
139  globalCommandController.getSettingsConfig().getSettingsManager()
140  .unregisterSetting(setting, fullname);
141 }
142 
143 void MSXCommandController::changeSetting(Setting& setting, const string& value)
144 {
145  string fullname = getFullName(setting.getName());
146  globalCommandController.changeSetting(fullname, value);
147 }
148 
150 {
151  auto it = commandMap.find(name);
152  return (it != commandMap.end()) ? it->second : nullptr;
153 }
154 
156 {
157  auto it = settingMap.find(name);
158  return (it != settingMap.end()) ? it->second : nullptr;
159 }
160 
162 {
163  return const_cast<MSXCommandController*>(this)->findSetting(setting);
164 }
165 
167 {
168  return findCommand(command) != nullptr;
169 }
170 
171 string MSXCommandController::executeCommand(const string& command,
172  CliConnection* connection)
173 {
174  return globalCommandController.executeCommand(command, connection);
175 }
176 
177 vector<string> MSXCommandController::splitList(const string& list)
178 {
179  return globalCommandController.splitList(list);
180 }
181 
183 {
184  return motherboard.getMSXCliComm();
185 }
186 
188 {
189  return globalCommandController.getInterpreter();
190 }
191 
192 void MSXCommandController::signalEvent(
193  const std::shared_ptr<const Event>& event, EmuTime::param /*time*/)
194 {
195  if (event->getType() != OPENMSX_MACHINE_ACTIVATED) return;
196 
197  // simple way to synchronize proxy settings
198  for (auto& p : settingMap) {
199  try {
200  changeSetting(*p.second, p.second->getString());
201  } catch (MSXException&) {
202  // ignore
203  }
204  }
205 }
206 
208 {
209  return reactor.getMotherBoard() == &motherboard;
210 }
211 
213 {
214  for (auto& p : settingMap) {
215  if (auto* fromSetting = from.findSetting(p.first())) {
216  if (!fromSetting->needTransfer()) continue;
217  try {
218  changeSetting(*p.second, fromSetting->getString());
219  } catch (MSXException&) {
220  // ignore
221  }
222  }
223  }
224 }
225 
226 } // namespace openmsx
void transferSettings(const MSXCommandController &from)
Transfer setting values from one machine to another, used for during 'reverse'.
Contains the main loop of openMSX.
Definition: Reactor.hh:61
virtual BaseSetting * findSetting(string_ref name)
void unregisterSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:319
bool isActive() const
Returns true iff the machine this controller belongs to is currently active.
void registerProxyCommand(const std::string &name)
virtual void unregisterCompleter(CommandCompleter &completer, string_ref str)
void deleteNamespace(const std::string &name)
Delete the global namespace with given name.
Definition: Interpreter.cc:434
virtual bool hasCommand(string_ref command) const
Does a command with this name already exist?
virtual std::vector< std::string > splitList(const std::string &list)
Send when a machine is (de)activated.
Definition: Event.hh:71
virtual void registerCompleter(CommandCompleter &completer, string_ref str)
(Un)register a command completer, used to complete build-in Tcl cmds
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
SettingsManager & getSettingsManager()
Command * findCommand(string_ref name) const
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void unregisterSetting(BaseSetting &setting, string_ref name)
void registerSetting(BaseSetting &setting, string_ref name)
virtual std::vector< std::string > splitList(const std::string &list)
virtual void registerCompleter(CommandCompleter &completer, string_ref str)
(Un)register a command completer, used to complete build-in Tcl cmds
const std::string & getName() const
Get the name of this setting.
Definition: Setting.cc:23
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:397
void setAllowedInEmptyMachine(bool value)
Definition: Command.hh:64
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
MSXCommandController(GlobalCommandController &globalCommandController, Reactor &reactor, MSXMotherBoard &motherboard, MSXEventDistributor &msxEventDistributor, const std::string &machineID)
virtual void registerCommand(Command &command, const std::string &str)
(Un)register a command
virtual SettingsConfig & getSettingsConfig()
GlobalCommandController & getGlobalCommandController()
virtual void unregisterCompleter(CommandCompleter &completer, string_ref str)
virtual Interpreter & getInterpreter()
virtual void unregisterCommand(Command &command, string_ref str)
void createNamespace(const std::string &name)
Create the global namespace with given name.
Definition: Interpreter.cc:429
virtual void unregisterSetting(Setting &setting)
virtual std::string executeCommand(const std::string &command, CliConnection *connection=nullptr)
Execute the given command.
void registerSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:268
virtual std::string executeCommand(const std::string &command, CliConnection *connection=nullptr)
Execute the given command.
MSXMotherBoard & getMSXMotherBoard() const
virtual void unregisterCommand(Command &command, string_ref str)
virtual void registerSetting(Setting &setting)
TODO.
virtual void changeSetting(Setting &setting, const std::string &value)
void changeSetting(const std::string &name, const std::string &value)
virtual void registerCommand(Command &command, const std::string &str)
(Un)register a command