31static constexpr bool META_HOT_KEYS =
42 , bindCmd (commandController_, *this, false)
43 , bindDefaultCmd (commandController_, *this, true)
44 , unbindCmd (commandController_, *this, false)
45 , unbindDefaultCmd(commandController_, *this, true)
46 , activateCmd (commandController_)
47 , deactivateCmd (commandController_)
48 , commandController(commandController_)
49 , eventDistributor(eventDistributor_)
51 initDefaultBindings();
101void HotKey::initDefaultBindings()
105 if constexpr (META_HOT_KEYS) {
107 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
109 "screenshot -guess-name"));
110 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
113 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
115 "toggle fastforward"));
116 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
119 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
122 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
124 "toggle fullscreen"));
125 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
130 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
Keys::K_PRINT),
131 "screenshot -guess-name"));
132 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
Keys::K_PAUSE),
134 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
Keys::K_F9),
135 "toggle fastforward"));
136 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
Keys::K_F10),
138 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
Keys::K_F11),
139 "toggle fullscreen"));
140 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
Keys::K_F12),
142 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
145 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
148 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
150 "toggle fullscreen"));
152 bindDefault(HotKeyInfo(Event::create<KeyDownEvent>(
Keys::K_BACK),
153 "quitmenu::quit_menu"));
157static Event createEvent(
const TclObject& obj, Interpreter& interp)
167 throw CommandException(
"Unsupported event type");
171static Event createEvent(std::string_view str, Interpreter& interp)
173 return createEvent(TclObject(str), interp);
187 std::string(cmd),
repeat, event));
201 return event == info.
event;
206static bool contains(
auto&& range,
const Event& event)
212static void erase(std::vector<T>& v,
const Event& event)
224 set.push_back(event);
228template<
typename HotKeyInfo>
232 *it = std::forward<HotKeyInfo>(info);
234 map.push_back(std::forward<HotKeyInfo>(info));
238void HotKey::bind(HotKeyInfo&& info)
240 erase(unboundKeys, info.event);
241 erase(defaultMap, info.event);
242 insert(boundKeys, info.event);
243 insert(cmdMap, std::move(info));
246void HotKey::unbind(
const Event& event)
249 it1 ==
end(boundKeys)) {
251 insert(unboundKeys, event);
257 erase(defaultMap, event);
258 erase(cmdMap, event);
261void HotKey::bindDefault(HotKeyInfo&& info)
263 if (!contains( boundKeys, info.event) &&
264 !contains(unboundKeys, info.event)) {
266 insert(cmdMap, info);
268 insert(defaultMap, std::move(info));
271void HotKey::unbindDefault(
const Event& event)
273 if (!contains( boundKeys, event) &&
274 !contains(unboundKeys, event)) {
276 erase(cmdMap, event);
278 erase(defaultMap, event);
281void HotKey::bindLayer(HotKeyInfo&& info,
const string& layer)
283 insert(layerMap[layer], std::move(info));
286void HotKey::unbindLayer(
const Event& event,
const string& layer)
288 erase(layerMap[layer], event);
291void HotKey::unbindFullLayer(
const string& layer)
293 layerMap.erase(layer);
296void HotKey::activateLayer(std::string layer,
bool blocking)
302 activeLayers.push_back({std::move(layer), blocking});
305void HotKey::deactivateLayer(std::string_view layer)
310 it != activeLayers.rend()) {
312 activeLayers.erase((it + 1).base());
316static HotKey::BindMap::const_iterator findMatch(
320 return matches(p.event, event);
324void HotKey::executeRT()
326 if (lastEvent) executeEvent(lastEvent);
329int HotKey::signalEvent(
const Event& event)
331 if (lastEvent.
getPtr() != event.getPtr()) {
344 return executeEvent(event);
347int HotKey::executeEvent(
const Event& event)
350 bool blocking =
false;
352 auto& cmap = layerMap[info.layer];
353 if (
auto it = findMatch(cmap, event); it !=
end(cmap)) {
354 executeBinding(event, *it);
359 blocking = info.blocking;
364 if (
auto it = findMatch(cmdMap, event); it !=
end(cmdMap)) {
365 executeBinding(event, *it);
374void HotKey::executeBinding(
const Event& event,
const HotKeyInfo& info)
390 TclObject command(info.command);
391 if (info.passEvent) {
396 command.addListElement(
toTclList(event));
401 }
catch (CommandException&
e) {
403 "Error executing hot key command: ",
e.getMessage());
407void HotKey::startRepeat(
const Event& event)
418 static constexpr unsigned PERIOD = 30;
420 unsigned delay = (lastEvent ? PERIOD : DELAY) * 1000;
425void HotKey::stopRepeat()
434static constexpr std::string_view getBindCmdName(
bool defaultCmd)
436 return defaultCmd ?
"bind_default" :
"bind";
439HotKey::BindCmd::BindCmd(CommandController& commandController_, HotKey& hotKey_,
441 : Command(commandController_, getBindCmdName(defaultCmd_))
443 , defaultCmd(defaultCmd_)
447static string formatBinding(
const HotKey::HotKeyInfo& info)
449 return strCat(
toString(info.event), (info.repeat ?
" [repeat]" :
""),
450 (info.passEvent ?
" [event]" :
""),
": ", info.command,
'\n');
453void HotKey::BindCmd::execute(std::span<const TclObject> tokens, TclObject& result)
458 bool passEvent =
false;
459 std::array parserInfo = {
465 auto arguments =
parseTclArgs(getInterpreter(), tokens.subspan<1>(), parserInfo);
466 if (defaultCmd && !layer.empty()) {
467 throw CommandException(
"Layers are not supported for default bindings");
470 auto& cMap = defaultCmd
472 : layer.empty() ? hotKey.cmdMap
473 : hotKey.layerMap[layer];
476 for (
const auto& [layerName, bindings] : hotKey.layerMap) {
479 if (!bindings.empty()) {
480 result.addListElement(layerName);
486 switch (arguments.size()) {
490 for (
auto& p : cMap) {
491 r += formatBinding(p);
499 EqualEvent(createEvent(arguments[0], getInterpreter())));
500 if (it ==
end(cMap)) {
501 throw CommandException(
"Key not bound");
503 result = formatBinding(*it);
508 string command(arguments[1].getString());
509 for (
const auto& arg :
view::drop(arguments, 2)) {
510 strAppend(command,
' ', arg.getString());
512 HotKey::HotKeyInfo info(
513 createEvent(arguments[0], getInterpreter()),
514 command,
repeat, passEvent);
516 hotKey.bindDefault(std::move(info));
517 }
else if (layer.empty()) {
518 hotKey.bind(std::move(info));
520 hotKey.bindLayer(std::move(info), layer);
526string HotKey::BindCmd::help(std::span<const TclObject> )
const
528 auto cmd = getBindCmdName(defaultCmd);
530 cmd,
" : show all bounded keys\n",
531 cmd,
" <key> : show binding for this key\n",
532 cmd,
" <key> [-repeat] [-event] <cmd> : bind key to command, optionally "
533 "repeat command while key remains pressed and also optionally "
534 "give back the event as argument (a list) to <cmd>\n"
535 "These 3 take an optional '-layer <layername>' option, "
536 "see activate_input_layer.\n",
537 cmd,
" -layers : show a list of layers with bound keys\n");
543static constexpr std::string_view getUnbindCmdName(
bool defaultCmd)
545 return defaultCmd ?
"unbind_default" :
"unbind";
548HotKey::UnbindCmd::UnbindCmd(CommandController& commandController_,
549 HotKey& hotKey_,
bool defaultCmd_)
550 : Command(commandController_, getUnbindCmdName(defaultCmd_))
552 , defaultCmd(defaultCmd_)
556void HotKey::UnbindCmd::execute(std::span<const TclObject> tokens, TclObject& )
559 std::array info = {
valueArg(
"-layer", layer)};
560 auto arguments =
parseTclArgs(getInterpreter(), tokens.subspan<1>(), info);
561 if (defaultCmd && !layer.empty()) {
562 throw CommandException(
"Layers are not supported for default bindings");
565 if ((arguments.size() > 1) || (layer.empty() && (arguments.size() != 1))) {
570 if (arguments.size() == 1) {
571 event = createEvent(arguments[0], getInterpreter());
576 hotKey.unbindDefault(event);
577 }
else if (layer.empty()) {
579 hotKey.unbind(event);
582 hotKey.unbindLayer(event, layer);
584 hotKey.unbindFullLayer(layer);
588string HotKey::UnbindCmd::help(std::span<const TclObject> )
const
590 auto cmd = getUnbindCmdName(defaultCmd);
592 cmd,
" <key> : unbind this key\n",
593 cmd,
" -layer <layername> <key> : unbind key in a specific layer\n",
594 cmd,
" -layer <layername> : unbind all keys in this layer\n");
600HotKey::ActivateCmd::ActivateCmd(CommandController& commandController_)
601 : Command(commandController_,
"activate_input_layer")
605void HotKey::ActivateCmd::execute(std::span<const TclObject> tokens, TclObject& result)
607 bool blocking =
false;
608 std::array info = {
flagArg(
"-blocking", blocking)};
609 auto args =
parseTclArgs(getInterpreter(), tokens.subspan(1), info);
611 auto& hotKey =
OUTER(HotKey, activateCmd);
612 switch (args.size()) {
616 r += layerInfo.layer;
617 if (layerInfo.blocking) {
626 std::string_view layer = args[0].getString();
627 hotKey.activateLayer(
string(layer), blocking);
635string HotKey::ActivateCmd::help(std::span<const TclObject> )
const
637 return "activate_input_layer "
638 ": show list of active layers (most recent on top)\n"
639 "activate_input_layer [-blocking] <layername> "
640 ": activate new layer, optionally in blocking mode\n";
646HotKey::DeactivateCmd::DeactivateCmd(CommandController& commandController_)
647 : Command(commandController_,
"deactivate_input_layer")
651void HotKey::DeactivateCmd::execute(std::span<const TclObject> tokens, TclObject& )
653 checkNumArgs(tokens, 2,
"layer");
654 auto& hotKey =
OUTER(HotKey, deactivateCmd);
655 hotKey.deactivateLayer(tokens[1].getString());
658string HotKey::DeactivateCmd::help(std::span<const TclObject> )
const
660 return "deactivate_input_layer <layername> : deactivate the given input layer";
void printWarning(std::string_view message)
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
const RcEvent * getPtr() const
Interpreter & getInterpreter() override
CliComm & getCliComm() override
HotKey(RTScheduler &rtScheduler, GlobalCommandController &commandController, EventDistributor &eventDistributor)
void loadUnbind(std::string_view key)
std::vector< HotKeyInfo > BindMap
void loadBind(std::string_view key, std::string_view cmd, bool repeat, bool event)
std::vector< Event > KeySet
void scheduleRT(uint64_t delta)
KeyCode combine(KeyCode key, KeyCode modifier)
Convenience method to create key combinations (hides ugly casts).
This file implemented 3 utility functions:
bool isRepeatStopper(const Event &self, const Event &other)
Should 'bind -repeat' be stopped by 'other' event.
bool matches(const Event &self, const Event &other)
Does this event 'match' the given event.
ArgsInfo valueArg(std::string_view name, T &value)
std::vector< TclObject > parseTclArgs(Interpreter &interp, std::span< const TclObject > inArgs, std::span< const ArgsInfo > table)
TclObject toTclList(const Event &event)
Similar to toString(), but retains the structure of the event.
EventType getType(const Event &event)
ArgsInfo flagArg(std::string_view name, bool &flag)
std::string toString(const Event &event)
Get a string representation of this event.
bool any_of(InputRange &&range, UnaryPredicate pred)
auto find_if(InputRange &&range, UnaryPredicate pred)
auto find(InputRange &&range, const T &value)
constexpr auto reverse(Range &&range)
constexpr auto drop(Range &&range, size_t n)
#define OUTER(type, member)
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
std::string strCat(Ts &&...ts)
void strAppend(std::string &result, Ts &&...ts)
EqualEvent(const Event &event_)
bool operator()(const HotKey::HotKeyInfo &info) const
bool operator()(const Event &e) const
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
constexpr auto end(const zstring_view &x)