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* c : commandMap) {
50  std::cout << "Command not unregistered: " << c->getName() << std::endl;
51  }
52  for (auto* s : settingMap) {
53  std::cout << "Setting not unregistered: " << s->getName() << 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  assert(command.getName() == str);
71  commandMap.insert_noDuplicateCheck(&command);
72 
73  string fullname = getFullName(str);
74  globalCommandController.registerCommand(command, fullname);
75  globalCommandController.registerProxyCommand(str);
76 
77  command.setAllowedInEmptyMachine(false);
78 }
79 
81 {
82  assert(hasCommand(str));
83  assert(command.getName() == str);
84  commandMap.erase(str);
85 
86  globalCommandController.unregisterProxyCommand(str);
87  string fullname = getFullName(str);
88  globalCommandController.unregisterCommand(command, fullname);
89 }
90 
92  string_ref str)
93 {
94  string fullname = getFullName(str);
95  globalCommandController.registerCompleter(completer, fullname);
96 }
97 
99  string_ref str)
100 {
101  string fullname = getFullName(str);
102  globalCommandController.unregisterCompleter(completer, fullname);
103 }
104 
106 {
107  const string& name = setting.getName();
108  assert(!findSetting(name));
109  settingMap.insert_noDuplicateCheck(&setting);
110 
111  globalCommandController.registerProxySetting(setting);
112  string fullname = getFullName(name);
113  globalCommandController.getSettingsConfig().getSettingsManager()
114  .registerSetting(setting, fullname);
115  globalCommandController.getInterpreter().registerSetting(setting, fullname);
116 }
117 
119 {
120  const string& name = setting.getName();
121  assert(findSetting(name));
122  settingMap.erase(name);
123 
124  globalCommandController.unregisterProxySetting(setting);
125  string fullname = getFullName(name);
126  globalCommandController.getInterpreter().unregisterSetting(setting, fullname);
127  globalCommandController.getSettingsConfig().getSettingsManager()
128  .unregisterSetting(setting, fullname);
129 }
130 
132 {
133  string fullname = getFullName(setting.getName());
134  globalCommandController.changeSetting(fullname, value);
135 }
136 
138 {
139  auto it = commandMap.find(name);
140  return (it != end(commandMap)) ? *it : nullptr;
141 }
142 
144 {
145  auto it = settingMap.find(name);
146  return (it != end(settingMap)) ? *it : nullptr;
147 }
148 
150 {
151  return const_cast<MSXCommandController*>(this)->findSetting(setting);
152 }
153 
155 {
156  return findCommand(command) != nullptr;
157 }
158 
160  CliConnection* connection)
161 {
162  return globalCommandController.executeCommand(command, connection);
163 }
164 
166 {
167  return motherboard.getMSXCliComm();
168 }
169 
171 {
172  return globalCommandController.getInterpreter();
173 }
174 
175 void MSXCommandController::signalEvent(
176  const std::shared_ptr<const Event>& event, EmuTime::param /*time*/)
177 {
178  if (event->getType() != OPENMSX_MACHINE_ACTIVATED) return;
179 
180  // simple way to synchronize proxy settings
181  for (auto* s : settingMap) {
182  try {
183  changeSetting(*s, s->getValue());
184  } catch (MSXException&) {
185  // ignore
186  }
187  }
188 }
189 
191 {
192  return reactor.getMotherBoard() == &motherboard;
193 }
194 
196 {
197  for (auto* s : settingMap) {
198  if (auto* fromSetting = from.findSetting(s->getName())) {
199  if (!fromSetting->needTransfer()) continue;
200  try {
201  changeSetting(*s, fromSetting->getValue());
202  } catch (MSXException&) {
203  // ignore
204  }
205  }
206  }
207 }
208 
209 } // namespace openmsx
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:167
void unregisterSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:299
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)
const std::string & getName() const
Definition: Completer.hh:17
void deleteNamespace(const std::string &name)
Delete the global namespace with given name.
Definition: Interpreter.cc:411
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:329
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:5
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:406
void registerCommand(Command &command, const std::string &str) override
(Un)register a command
void registerSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:248
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