diff options
Diffstat (limited to 'seqmatcher.h')
-rw-r--r-- | seqmatcher.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/seqmatcher.h b/seqmatcher.h new file mode 100644 index 0000000..7ce6bf4 --- /dev/null +++ b/seqmatcher.h @@ -0,0 +1,44 @@ +#pragma once + +#include <functional> +#include <vector> +#include <variant> +#include <unordered_map> +#include <memory> +#include "xutil.h" + + +class SeqMatcher { +public: + using Callback = std::function<void()>; + + struct SymSequence { + std::vector<x::Keysym> syms; + Callback callback; + }; + + SeqMatcher(Display *dpy); + + // Initialise with an initial list of sequences. + SeqMatcher(Display *dpy, std::vector<SymSequence> seqs); + + // Register a key sequence + void addSequence(const std::vector<x::Keysym> &syms, Callback callback); + + // Returns bel()-running callback if unrecognised sequence is given + std::optional<Callback> observe(const XKeyEvent &ev); + + // Cancel any running sequence and restart from the beginning + void reset(); + +private: + struct Node; + using NodeMap = std::unordered_map<x::Keycode, std::unique_ptr<Node>>; + struct Node { + std::variant<NodeMap, Callback> v; + }; + + Display *const dpy; + Node rootNode; + Node *curNode = &rootNode; +}; |