OXYGENHUB
◆ main openmsx / src / commands / GlobalCommandController.hh
5.5 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef GLOBALCOMMANDCONTROLLER_HH
2
#define GLOBALCOMMANDCONTROLLER_HH
3
4
#include "Command.hh"
5
#include "CommandController.hh"
6
#include "InfoCommand.hh"
7
#include "InfoTopic.hh"
8
#include "Interpreter.hh"
9
#include "TclObject.hh"
10
11
#include "HotKey.hh"
12
#include "RomInfoTopic.hh"
13
#include "SettingsConfig.hh"
14
15
#include "hash_map.hh"
16
#include "xxhash.hh"
17
18
#include <memory>
19
#include <string>
20
#include <vector>
21
22
namespace openmsx {
23
24
class EventDistributor;
25
class Reactor;
26
class GlobalCliComm;
27
class ProxyCmd;
28
class ProxySetting;
29
30
class GlobalCommandControllerBase
31
{
32
protected:
33
	~GlobalCommandControllerBase();
34
35
#ifdef DEBUG
36
	hash_map<std::string, Command*,          XXHasher> commands;
37
#endif
38
	hash_map<std::string, CommandCompleter*, XXHasher> commandCompleters;
39
};
40
41
class GlobalCommandController final : private GlobalCommandControllerBase
42
                                    , public CommandController
43
{
44
public:
45
	GlobalCommandController(EventDistributor& eventDistributor,
46
	                        GlobalCliComm& cliComm,
47
	                        Reactor& reactor);
48
	GlobalCommandController(const GlobalCommandController&) = delete;
49
	GlobalCommandController(GlobalCommandController&&) = delete;
50
	GlobalCommandController& operator=(const GlobalCommandController&) = delete;
51
	GlobalCommandController& operator=(GlobalCommandController&&) = delete;
52
	~GlobalCommandController();
53
54
	[[nodiscard]] InfoCommand& getOpenMSXInfoCommand() { return openMSXInfoCommand; }
55
56
	/**
57
	 * Executes all defined auto commands
58
	 */
59
	void source(const std::string& script);
60
61
	void registerProxyCommand(std::string_view name);
62
	void unregisterProxyCommand(std::string_view name);
63
64
	void registerProxySetting(const Setting& setting);
65
	void unregisterProxySetting(const Setting& setting);
66
67
	// CommandController
68
	void   registerCompleter(CommandCompleter& completer,
69
	                         std::string_view str) override;
70
	void unregisterCompleter(CommandCompleter& completer,
71
	                         std::string_view str) override;
72
	void   registerCommand(Command& command,
73
	                       zstring_view str) override;
74
	void unregisterCommand(Command& command,
75
	                       std::string_view str) override;
76
	TclObject executeCommand(zstring_view command,
77
	                         CliConnection* connection = nullptr) override;
78
	void registerSetting(Setting& setting) override;
79
	void unregisterSetting(Setting& setting) override;
80
	[[nodiscard]] CliComm& getCliComm() override;
81
	[[nodiscard]] Interpreter& getInterpreter() override;
82
83
	/**
84
	 * Complete the given command.
85
	 */
86
	[[nodiscard]] std::string tabCompletion(std::string_view command);
87
	[[nodiscard]] std::string tabCompletionReplace(
88
		std::string_view command, const CompletionCandidate& replacement);
89
90
	/**
91
	 * Returns true iff the command is complete (all braces, quotes etc. are
92
	 * balanced).
93
	 */
94
	[[nodiscard]] bool isComplete(zstring_view command);
95
96
	[[nodiscard]] SettingsConfig& getSettingsConfig() { return settingsConfig; }
97
	[[nodiscard]] SettingsManager& getSettingsManager() { return settingsConfig.getSettingsManager(); }
98
	[[nodiscard]] CliConnection* getConnection() const { return connection; }
99
	[[nodiscard]] Reactor& getReactor() const { return reactor; }
100
	[[nodiscard]] const HotKey& getHotKey() const { return hotKey; }
101
102
private:
103
	void tabCompletion(std::vector<std::string>& tokens);
104
105
	using ProxySettings =
106
		std::vector<std::pair<std::unique_ptr<ProxySetting>, unsigned>>;
107
	ProxySettings::iterator findProxySetting(std::string_view name);
108
109
	GlobalCliComm& cliComm;
110
	CliConnection* connection = nullptr;
111
112
	Reactor& reactor;
113
114
	Interpreter interpreter;
115
	InfoCommand openMSXInfoCommand;
116
	HotKey hotKey;
117
	SettingsConfig settingsConfig;
118
119
	struct HelpCmd final : Command {
120
		explicit HelpCmd(GlobalCommandController& controller);
121
		void execute(std::span<const TclObject> tokens, TclObject& result) override;
122
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
123
		void tabCompletion(std::vector<std::string>& tokens) const override;
124
	} helpCmd;
125
126
	struct TabCompletionCmd final : Command {
127
		explicit TabCompletionCmd(GlobalCommandController& controller);
128
		void execute(std::span<const TclObject> tokens, TclObject& result) override;
129
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
130
	} tabCompletionCmd;
131
132
	struct UpdateCmd final : Command {
133
		explicit UpdateCmd(CommandController& commandController);
134
		void execute(std::span<const TclObject> tokens, TclObject& result) override;
135
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
136
		void tabCompletion(std::vector<std::string>& tokens) const override;
137
	private:
138
		CliConnection& getConnection();
139
	} updateCmd;
140
141
	struct PlatformInfo final : InfoTopic {
142
		explicit PlatformInfo(InfoCommand& openMSXInfoCommand);
143
		void execute(std::span<const TclObject> tokens,
144
			     TclObject& result) const override;
145
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
146
	} platformInfo;
147
148
	struct VersionInfo final : InfoTopic {
149
		explicit VersionInfo(InfoCommand& openMSXInfoCommand);
150
		void execute(std::span<const TclObject> tokens,
151
			     TclObject& result) const override;
152
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
153
	} versionInfo;
154
155
	RomInfoTopic romInfoTopic;
156
157
	struct NameFromProxy {
158
		template<typename Pair>
159
		[[nodiscard]] const std::string& operator()(const Pair& p) const {
160
			return p.second->getName();
161
		}
162
	};
163
	hash_set<std::pair<unsigned, std::unique_ptr<ProxyCmd>>, NameFromProxy, XXHasher> proxyCommandMap;
164
	ProxySettings proxySettings;
165
};
166
167
} // namespace openmsx
168
169
#endif