openMSX
MSXCommandController.cc
Go to the documentation of this file.
3#include "Reactor.hh"
5#include "MSXMotherBoard.hh"
6#include "SettingsManager.hh"
7#include "Interpreter.hh"
8#include "Setting.hh"
9#include "Event.hh"
10#include "MSXException.hh"
11#include "TemporaryString.hh"
12#include "stl.hh"
13#include <iostream>
14#include <memory>
15
16namespace openmsx {
17
19 GlobalCommandController& globalCommandController_,
20 Reactor& reactor_,
21 MSXMotherBoard& motherboard_,
22 MSXEventDistributor& msxEventDistributor_,
23 const std::string& machineID_)
24 : globalCommandController(globalCommandController_)
25 , reactor(reactor_)
26 , motherboard(motherboard_)
27 , msxEventDistributor(msxEventDistributor_)
28 , machineID(strCat("::", machineID_, "::"))
29{
30 globalCommandController.getInterpreter().createNamespace(machineID);
31
32 machineInfoCommand.emplace(*this, "machine_info");
33 machineInfoCommand->setAllowedInEmptyMachine(true);
34
35 msxEventDistributor.registerEventListener(*this);
36}
37
39{
40 msxEventDistributor.unregisterEventListener(*this);
41
42 machineInfoCommand.reset();
43
44 #ifndef NDEBUG
45 for (auto* c : commandMap) {
46 std::cout << "Command not unregistered: " << c->getName() << '\n';
47 }
48 for (auto* s : settings) {
49 std::cout << "Setting not unregistered: " << s->getFullName() << '\n';
50 }
51 assert(commandMap.empty());
52 assert(settings.empty());
53 #endif
54
55 globalCommandController.getInterpreter().deleteNamespace(machineID);
56}
57
58TemporaryString MSXCommandController::getFullName(std::string_view name)
59{
60 return tmpStrCat(machineID, name);
61}
62
64{
65 assert(!hasCommand(str));
66 assert(command.getName() == str);
67 commandMap.insert_noDuplicateCheck(&command);
68
69 auto fullname = getFullName(str);
70 globalCommandController.registerCommand(command, fullname);
71 globalCommandController.registerProxyCommand(str);
72
73 command.setAllowedInEmptyMachine(false);
74}
75
76void MSXCommandController::unregisterCommand(Command& command, std::string_view str)
77{
78 assert(hasCommand(str));
79 assert(command.getName() == str);
80 commandMap.erase(str);
81
82 globalCommandController.unregisterProxyCommand(str);
83 auto fullname = getFullName(str);
84 globalCommandController.unregisterCommand(command, fullname);
85}
86
88 std::string_view str)
89{
90 auto fullname = getFullName(str);
91 globalCommandController.registerCompleter(completer, fullname);
92}
93
95 std::string_view str)
96{
97 auto fullname = getFullName(str);
98 globalCommandController.unregisterCompleter(completer, fullname);
99}
100
102{
103 setting.setPrefix(machineID);
104
105 settings.push_back(&setting);
106
107 globalCommandController.registerProxySetting(setting);
108 globalCommandController.getSettingsManager().registerSetting(setting);
109 globalCommandController.getInterpreter().registerSetting(setting);
110}
111
113{
114 move_pop_back(settings, rfind_unguarded(settings, &setting));
115
116 globalCommandController.unregisterProxySetting(setting);
117 globalCommandController.getInterpreter().unregisterSetting(setting);
118 globalCommandController.getSettingsManager().unregisterSetting(setting);
119}
120
121Setting* MSXCommandController::findSetting(std::string_view name) const
122{
123 auto it = ranges::find(settings, name, &Setting::getBaseName);
124 return it != settings.end() ? *it : nullptr;
125}
126
127Command* MSXCommandController::findCommand(std::string_view name) const
128{
129 auto it = commandMap.find(name);
130 return (it != end(commandMap)) ? *it : nullptr;
131}
132
133bool MSXCommandController::hasCommand(std::string_view command) const
134{
135 return findCommand(command) != nullptr;
136}
137
139 CliConnection* connection)
140{
141 return globalCommandController.executeCommand(command, connection);
142}
143
145{
146 return motherboard.getMSXCliComm();
147}
148
150{
151 return globalCommandController.getInterpreter();
152}
153
154void MSXCommandController::signalMSXEvent(
155 const Event& event, EmuTime::param /*time*/) noexcept
156{
157 if (getType(event) != EventType::MACHINE_ACTIVATED) return;
158
159 // simple way to synchronize proxy settings
160 for (auto* s : settings) {
161 try {
162 getInterpreter().setVariable(
163 s->getFullNameObj(), s->getValue());
164 } catch (MSXException&) {
165 // ignore
166 }
167 }
168}
169
171{
172 return reactor.getMotherBoard() == &motherboard;
173}
174
176{
177 const auto& fromPrefix = from.getPrefix();
178 auto& manager = globalCommandController.getSettingsManager();
179 for (auto* s : settings) {
180 if (auto* fromSetting = manager.findSetting(fromPrefix, s->getBaseName())) {
181 if (!fromSetting->needTransfer()) continue;
182 try {
184 s->getFullNameObj(), fromSetting->getValue());
185 } catch (MSXException&) {
186 // ignore
187 }
188 }
189 }
190}
191
192} // namespace openmsx
BaseSetting * setting
TemporaryString.
std::string_view getBaseName() const
Definition Setting.hh:38
void setAllowedInEmptyMachine(bool value)
Definition Command.hh:65
const std::string & getName() const
Definition Completer.hh:25
void registerProxySetting(const Setting &setting)
void unregisterProxySetting(const Setting &setting)
void registerProxyCommand(std::string_view name)
void unregisterProxyCommand(std::string_view name)
void registerCommand(Command &command, zstring_view str) override
(Un)register a command
void registerCompleter(CommandCompleter &completer, std::string_view str) override
(Un)register a command completer, used to complete build-in Tcl cmds
TclObject executeCommand(zstring_view command, CliConnection *connection=nullptr) override
Execute the given command.
void unregisterCompleter(CommandCompleter &completer, std::string_view str) override
void unregisterCommand(Command &command, std::string_view str) override
void deleteNamespace(const std::string &name)
Delete the global namespace with given name.
void registerSetting(BaseSetting &variable)
void createNamespace(const std::string &name)
Create the global namespace with given name.
void setVariable(const TclObject &name, const TclObject &value)
void unregisterSetting(BaseSetting &variable)
bool hasCommand(std::string_view command) const
Interpreter & getInterpreter() override
void registerCompleter(CommandCompleter &completer, std::string_view str) override
(Un)register a command completer, used to complete build-in Tcl cmds
TclObject executeCommand(zstring_view command, CliConnection *connection=nullptr) override
Execute the given command.
void unregisterCompleter(CommandCompleter &completer, std::string_view str) override
void registerCommand(Command &command, zstring_view str) override
(Un)register a command
Setting * findSetting(std::string_view name) const
void transferSettings(const MSXCommandController &from)
Transfer setting values from one machine to another, used for during 'reverse'.
void unregisterSetting(Setting &setting) override
const std::string & getPrefix() const
bool isActive() const
Returns true iff the machine this controller belongs to is currently active.
MSXCommandController(const MSXCommandController &)=delete
void unregisterCommand(Command &command, std::string_view str) override
void registerSetting(Setting &setting) override
TODO.
Command * findCommand(std::string_view name) const
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
Contains the main loop of openMSX.
Definition Reactor.hh:72
MSXMotherBoard * getMotherBoard() const
Definition Reactor.cc:410
void unregisterSetting(BaseSetting &setting)
void registerSetting(BaseSetting &setting)
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
This file implemented 3 utility functions:
Definition Autofire.cc:11
EventType getType(const Event &event)
Definition Event.hh:516
std::variant< KeyUpEvent, KeyDownEvent, MouseMotionEvent, MouseButtonUpEvent, MouseButtonDownEvent, MouseWheelEvent, JoystickAxisMotionEvent, JoystickHatEvent, JoystickButtonUpEvent, JoystickButtonDownEvent, OsdControlReleaseEvent, OsdControlPressEvent, WindowEvent, TextEvent, FileDropEvent, QuitEvent, FinishFrameEvent, CliCommandEvent, GroupEvent, BootEvent, FrameDrawnEvent, BreakEvent, SwitchRendererEvent, TakeReverseSnapshotEvent, AfterTimedEvent, MachineLoadedEvent, MachineActivatedEvent, MachineDeactivatedEvent, MidiInReaderEvent, MidiInWindowsEvent, MidiInCoreMidiEvent, MidiInCoreMidiVirtualEvent, MidiInALSAEvent, Rs232TesterEvent, Rs232NetEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition Event.hh:444
auto find(InputRange &&range, const T &value)
Definition ranges.hh:160
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition stl.hh:134
auto rfind_unguarded(RANGE &range, const VAL &val, Proj proj={})
Similar to the find(_if)_unguarded functions above, but searches from the back to front.
Definition stl.hh:109
std::string strCat()
Definition strCat.hh:703
TemporaryString tmpStrCat(Ts &&... ts)
Definition strCat.hh:742
constexpr auto end(const zstring_view &x)