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 "KeyRange.hh"
14 #include "memory.hh"
15 #include <iostream>
16 
17 using std::string;
18 using std::vector;
19 
20 namespace openmsx {
21 
23  GlobalCommandController& globalCommandController_,
24  Reactor& reactor_,
25  MSXMotherBoard& motherboard_,
26  MSXEventDistributor& msxEventDistributor_,
27  const std::string& machineID_)
28  : globalCommandController(globalCommandController_)
29  , reactor(reactor_)
30  , motherboard(motherboard_)
31  , msxEventDistributor(msxEventDistributor_)
32  , machineID(machineID_)
33 {
34  globalCommandController.getInterpreter().createNamespace(machineID);
35 
36  machineInfoCommand = make_unique<InfoCommand>(*this, "machine_info");
37  machineInfoCommand->setAllowedInEmptyMachine(true);
38 
39  msxEventDistributor.registerEventListener(*this);
40 }
41 
43 {
44  msxEventDistributor.unregisterEventListener(*this);
45 
46  machineInfoCommand.reset();
47 
48  #ifndef NDEBUG
49  for (auto& p : commandMap) {
50  std::cout << "Command not unregistered: " << p.first() << std::endl;
51  }
52  for (auto& p : settingMap) {
53  std::cout << "Setting not unregistered: " << p.first() << std::endl;
54  }
55  assert(commandMap.empty());
56  assert(settingMap.empty());
57  #endif
58 
59  globalCommandController.getInterpreter().deleteNamespace(machineID);
60 }
61 
62 string MSXCommandController::getFullName(string_ref name)
63 {
64  return "::" + machineID + "::" + name;
65 }
66 
67 void MSXCommandController::registerCommand(Command& command, const string& str)
68 {
69  assert(!hasCommand(str));
70  commandMap[str] = &command;
71 
72  string fullname = getFullName(str);
73  globalCommandController.registerCommand(command, fullname);
74  globalCommandController.registerProxyCommand(str);
75 
76  command.setAllowedInEmptyMachine(false);
77 }
78 
80 {
81  assert(hasCommand(str));
82  commandMap.erase(str);
83 
84  globalCommandController.unregisterProxyCommand(str);
85  string fullname = getFullName(str);
86  globalCommandController.unregisterCommand(command, fullname);
87 }
88 
90  string_ref str)
91 {
92  string fullname = getFullName(str);
93  globalCommandController.registerCompleter(completer, fullname);
94 }
95 
97  string_ref str)
98 {
99  string fullname = getFullName(str);
100  globalCommandController.unregisterCompleter(completer, fullname);
101 }
102 
104 {
105  const string& name = setting.getName();
106  assert(!findSetting(name));
107  settingMap[name] = &setting;
108 
109  globalCommandController.registerProxySetting(setting);
110  string fullname = getFullName(name);
111  globalCommandController.getSettingsConfig().getSettingsManager()
112  .registerSetting(setting, fullname);
113  globalCommandController.getInterpreter().registerSetting(setting, fullname);
114 }
115 
117 {
118  const string& name = setting.getName();
119  assert(findSetting(name));
120  settingMap.erase(name);
121 
122  globalCommandController.unregisterProxySetting(setting);
123  string fullname = getFullName(name);
124  globalCommandController.getInterpreter().unregisterSetting(setting, fullname);
125  globalCommandController.getSettingsConfig().getSettingsManager()
126  .unregisterSetting(setting, fullname);
127 }
128 
130 {
131  string fullname = getFullName(setting.getName());
132  globalCommandController.changeSetting(fullname, value);
133 }
134 
136 {
137  auto it = commandMap.find(name);
138  return (it != end(commandMap)) ? it->second : nullptr;
139 }
140 
142 {
143  auto it = settingMap.find(name);
144  return (it != end(settingMap)) ? it->second : nullptr;
145 }
146 
148 {
149  return const_cast<MSXCommandController*>(this)->findSetting(setting);
150 }
151 
153 {
154  return findCommand(command) != nullptr;
155 }
156 
158  CliConnection* connection)
159 {
160  return globalCommandController.executeCommand(command, connection);
161 }
162 
164 {
165  return motherboard.getMSXCliComm();
166 }
167 
169 {
170  return globalCommandController.getInterpreter();
171 }
172 
173 void MSXCommandController::signalEvent(
174  const std::shared_ptr<const Event>& event, EmuTime::param /*time*/)
175 {
176  if (event->getType() != OPENMSX_MACHINE_ACTIVATED) return;
177 
178  // simple way to synchronize proxy settings
179  for (auto* s : values(settingMap)) {
180  try {
181  changeSetting(*s, s->getValue());
182  } catch (MSXException&) {
183  // ignore
184  }
185  }
186 }
187 
189 {
190  return reactor.getMotherBoard() == &motherboard;
191 }
192 
194 {
195  for (auto& p : settingMap) {
196  if (auto* fromSetting = from.findSetting(p.first())) {
197  if (!fromSetting->needTransfer()) continue;
198  try {
199  changeSetting(*p.second, fromSetting->getValue());
200  } catch (MSXException&) {
201  // ignore
202  }
203  }
204  }
205 }
206 
207 } // namespace openmsx
detail::KeyRange< MAP, 1 > values(const MAP &map)
Definition: KeyRange.hh:101
BaseSetting * findSetting(string_ref name) override
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:62
void changeSetting(Setting &setting, const TclObject &value) override
TclObject executeCommand(const std::string &command, CliConnection *connection=nullptr) override
Execute the given command.
string_ref::const_iterator end(const string_ref &x)
Definition: string_ref.hh:150
void unregisterSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:302
bool isActive() const
Returns true iff the machine this controller belongs to is currently active.
void unregisterCommand(Command &command, string_ref str) override
void changeSetting(const std::string &name, const TclObject &value)
void registerProxyCommand(const std::string &name)
void deleteNamespace(const std::string &name)
Delete the global namespace with given name.
Definition: Interpreter.cc:414
void unregisterSetting(Setting &setting) override
Send when a machine is (de)activated.
Definition: Event.hh:56
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
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)
void unregisterCommand(Command &command, string_ref str) override
void registerCommand(Command &command, const std::string &str) override
(Un)register a command
SettingsManager & getSettingsManager()
Interpreter & getInterpreter() override
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:331
void unregisterCompleter(CommandCompleter &completer, string_ref str) override
void setAllowedInEmptyMachine(bool value)
Definition: Command.hh:57
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
MSXCommandController(GlobalCommandController &globalCommandController, Reactor &reactor, MSXMotherBoard &motherboard, MSXEventDistributor &msxEventDistributor, const std::string &machineID)
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:7
void registerCompleter(CommandCompleter &completer, string_ref str) override
(Un)register a command completer, used to complete build-in Tcl cmds
void registerCompleter(CommandCompleter &completer, string_ref str) override
(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.hh:26
void createNamespace(const std::string &name)
Create the global namespace with given name.
Definition: Interpreter.cc:409
void registerCommand(Command &command, const std::string &str) override
(Un)register a command
void registerSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:251
void registerSetting(Setting &setting) override
TODO.
bool hasCommand(string_ref command) const override
Does a command with this name already exist?
TclObject executeCommand(const std::string &command, CliConnection *connection=nullptr) override
Execute the given command.
void unregisterCompleter(CommandCompleter &completer, string_ref str) override