summaryrefslogtreecommitdiff
path: root/seqmatcher.h
blob: 7ce6bf4fd9e92b6cda6b9ae8c9fd0faa14bd18e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
};