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 
187 void MSXCommandController::signalEvent(
188  const std::shared_ptr<const Event>& event, EmuTime::param /*time*/)
189 {
190  if (event->getType() != OPENMSX_MACHINE_ACTIVATED) return;
191 
192  // simple way to synchronize proxy settings
193  for (auto& p : settingMap) {
194  try {
195  changeSetting(*p.second, p.second->getValueString());
196  } catch (MSXException&) {
197  // ignore
198  }
199  }
200 }
201 
203 {
204  return reactor.getMotherBoard() == &motherboard;
205 }
206 
208 {
209  for (auto& p : settingMap) {
210  if (auto* fromSetting = from.findSetting(p.first())) {
211  if (!fromSetting->needTransfer()) continue;
212  try {
213  changeSetting(*p.second, fromSetting->getValueString());
214  } catch (MSXException&) {
215  // ignore
216  }
217  }
218  }
219 }
220 
221 } // namespace openmsx