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) {
108 "screenshot -guess-name"));
112 "toggle fastforward"));
118 "toggle fullscreen"));
124 "screenshot -guess-name"));
128 "toggle fastforward"));
132 "toggle fullscreen"));
140 "toggle fullscreen"));
144static Event createEvent(
const TclObject& obj, Interpreter& interp)
154 throw CommandException(
"Unsupported event type");
158static Event createEvent(std::string_view str, Interpreter& interp)
160 return createEvent(TclObject(str), interp);
174 std::string(cmd),
repeat, event));
188 return event == info.
event;
193static bool contains(
auto&& range,
const Event& event)
199static void erase(std::vector<T>& v,
const Event& event)
211 set.push_back(event);
215template<
typename HotKeyInfo>
219 *it = std::forward<HotKeyInfo>(info);
221 map.push_back(std::forward<HotKeyInfo>(info));
225void HotKey::bind(HotKeyInfo&& info)
227 erase(unboundKeys, info.event);
228 erase(defaultMap, info.event);
229 insert(boundKeys, info.event);
230 insert(cmdMap, std::move(info));
233void HotKey::unbind(
const Event& event)
236 it1 ==
end(boundKeys)) {
238 insert(unboundKeys, event);
244 erase(defaultMap, event);
245 erase(cmdMap, event);
248void HotKey::bindDefault(HotKeyInfo&& info)
250 if (!contains( boundKeys, info.event) &&
251 !contains(unboundKeys, info.event)) {
253 insert(cmdMap, info);
255 insert(defaultMap, std::move(info));
258void HotKey::unbindDefault(
const Event& event)
260 if (!contains( boundKeys, event) &&
261 !contains(unboundKeys, event)) {
263 erase(cmdMap, event);
265 erase(defaultMap, event);
268void HotKey::bindLayer(HotKeyInfo&& info,
const string& layer)
270 insert(layerMap[layer], std::move(info));
273void HotKey::unbindLayer(
const Event& event,
const string& layer)
275 erase(layerMap[layer], event);
278void HotKey::unbindFullLayer(
const string& layer)
280 layerMap.erase(layer);
283void HotKey::activateLayer(std::string layer,
bool blocking)
289 activeLayers.push_back({std::move(layer), blocking});
292void HotKey::deactivateLayer(std::string_view layer)
297 it != activeLayers.rend()) {
299 activeLayers.erase((it + 1).base());
303static HotKey::BindMap::const_iterator findMatch(
307 return matches(p.event, event);
311void HotKey::executeRT()
313 if (lastEvent) executeEvent(*lastEvent);
316int HotKey::signalEvent(
const Event& event)
318 if (lastEvent && *lastEvent != event) {
329 return executeEvent(event);
332int HotKey::executeEvent(
const Event& event)
335 bool blocking =
false;
337 auto& cmap = layerMap[info.layer];
338 if (
auto it = findMatch(cmap, event); it !=
end(cmap)) {
339 executeBinding(event, *it);
344 blocking = info.blocking;
349 if (
auto it = findMatch(cmdMap, event); it !=
end(cmdMap)) {
350 executeBinding(event, *it);
359void HotKey::executeBinding(
const Event& event,
const HotKeyInfo& info)
375 TclObject command(info.command);
376 if (info.passEvent) {
381 command.addListElement(
toTclList(event));
386 }
catch (CommandException& e) {
388 "Error executing hot key command: ",
e.getMessage());
392void HotKey::startRepeat(
const Event& event)
403 static constexpr unsigned PERIOD = 30;
405 unsigned delay = (lastEvent ? PERIOD : DELAY) * 1000;
410void HotKey::stopRepeat()
419static constexpr std::string_view getBindCmdName(
bool defaultCmd)
421 return defaultCmd ?
"bind_default" :
"bind";
424HotKey::BindCmd::BindCmd(CommandController& commandController_, HotKey& hotKey_,
426 : Command(commandController_, getBindCmdName(defaultCmd_))
428 , defaultCmd(defaultCmd_)
432static string formatBinding(
const HotKey::HotKeyInfo& info)
434 return strCat(
toString(info.event), (info.repeat ?
" [repeat]" :
""),
435 (info.passEvent ?
" [event]" :
""),
": ", info.command,
'\n');
438void HotKey::BindCmd::execute(std::span<const TclObject> tokens, TclObject& result)
443 bool passEvent =
false;
444 std::array parserInfo = {
450 auto arguments =
parseTclArgs(getInterpreter(), tokens.subspan<1>(), parserInfo);
451 if (defaultCmd && !layer.empty()) {
452 throw CommandException(
"Layers are not supported for default bindings");
455 auto& cMap = defaultCmd
457 : layer.empty() ? hotKey.cmdMap
458 : hotKey.layerMap[layer];
461 for (
const auto& [layerName, bindings] : hotKey.layerMap) {
464 if (!bindings.empty()) {
465 result.addListElement(layerName);
471 switch (arguments.size()) {
475 for (
auto& p : cMap) {
476 r += formatBinding(p);
484 EqualEvent(createEvent(arguments[0], getInterpreter())));
485 if (it ==
end(cMap)) {
486 throw CommandException(
"Key not bound");
488 result = formatBinding(*it);
493 string command(arguments[1].getString());
494 for (
const auto& arg :
view::
drop(arguments, 2)) {
495 strAppend(command,
' ', arg.getString());
497 HotKey::HotKeyInfo info(
498 createEvent(arguments[0], getInterpreter()),
499 command,
repeat, passEvent);
501 hotKey.bindDefault(std::move(info));
502 }
else if (layer.empty()) {
503 hotKey.bind(std::move(info));
505 hotKey.bindLayer(std::move(info), layer);
511string HotKey::BindCmd::help(std::span<const TclObject> )
const
513 auto cmd = getBindCmdName(defaultCmd);
515 cmd,
" : show all bounded keys\n",
516 cmd,
" <key> : show binding for this key\n",
517 cmd,
" <key> [-repeat] [-event] <cmd> : bind key to command, optionally "
518 "repeat command while key remains pressed and also optionally "
519 "give back the event as argument (a list) to <cmd>\n"
520 "These 3 take an optional '-layer <layername>' option, "
521 "see activate_input_layer.\n",
522 cmd,
" -layers : show a list of layers with bound keys\n");
528static constexpr std::string_view getUnbindCmdName(
bool defaultCmd)
530 return defaultCmd ?
"unbind_default" :
"unbind";
533HotKey::UnbindCmd::UnbindCmd(CommandController& commandController_,
534 HotKey& hotKey_,
bool defaultCmd_)
535 : Command(commandController_, getUnbindCmdName(defaultCmd_))
537 , defaultCmd(defaultCmd_)
541void HotKey::UnbindCmd::execute(std::span<const TclObject> tokens, TclObject& )
544 std::array info = {
valueArg(
"-layer", layer)};
545 auto arguments =
parseTclArgs(getInterpreter(), tokens.subspan<1>(), info);
546 if (defaultCmd && !layer.empty()) {
547 throw CommandException(
"Layers are not supported for default bindings");
550 if ((arguments.size() > 1) || (layer.empty() && (arguments.size() != 1))) {
554 std::optional<Event> event;
555 if (arguments.size() == 1) {
556 event = createEvent(arguments[0], getInterpreter());
561 hotKey.unbindDefault(*event);
562 }
else if (layer.empty()) {
564 hotKey.unbind(*event);
567 hotKey.unbindLayer(*event, layer);
569 hotKey.unbindFullLayer(layer);
573string HotKey::UnbindCmd::help(std::span<const TclObject> )
const
575 auto cmd = getUnbindCmdName(defaultCmd);
577 cmd,
" <key> : unbind this key\n",
578 cmd,
" -layer <layername> <key> : unbind key in a specific layer\n",
579 cmd,
" -layer <layername> : unbind all keys in this layer\n");
585HotKey::ActivateCmd::ActivateCmd(CommandController& commandController_)
586 : Command(commandController_,
"activate_input_layer")
590void HotKey::ActivateCmd::execute(std::span<const TclObject> tokens, TclObject& result)
592 bool blocking =
false;
593 std::array info = {
flagArg(
"-blocking", blocking)};
594 auto args =
parseTclArgs(getInterpreter(), tokens.subspan(1), info);
596 auto& hotKey =
OUTER(HotKey, activateCmd);
597 switch (args.size()) {
600 for (
auto& layerInfo :
view::
reverse(hotKey.activeLayers)) {
601 r += layerInfo.layer;
602 if (layerInfo.blocking) {
611 std::string_view layer = args[0].getString();
612 hotKey.activateLayer(
string(layer), blocking);
620string HotKey::ActivateCmd::help(std::span<const TclObject> )
const
622 return "activate_input_layer "
623 ": show list of active layers (most recent on top)\n"
624 "activate_input_layer [-blocking] <layername> "
625 ": activate new layer, optionally in blocking mode\n";
631HotKey::DeactivateCmd::DeactivateCmd(CommandController& commandController_)
632 : Command(commandController_,
"deactivate_input_layer")
636void HotKey::DeactivateCmd::execute(std::span<const TclObject> tokens, TclObject& )
638 checkNumArgs(tokens, 2,
"layer");
639 auto& hotKey =
OUTER(HotKey, deactivateCmd);
640 hotKey.deactivateLayer(tokens[1].getString());
643string HotKey::DeactivateCmd::help(std::span<const TclObject> )
const
645 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.
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
static KeyDownEvent create(SDL_Keycode code, SDL_Keymod mod=KMOD_NONE)
void scheduleRT(uint64_t delta)
This file implemented 3 utility functions:
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)
std::string toString(const BooleanInput &input)
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
ArgsInfo flagArg(std::string_view name, bool &flag)
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.
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)