summaryrefslogtreecommitdiff
path: root/xutil.h
blob: 337a190b3dd6f93215d89bd4029a989023daf89f (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once

#include <functional>
#include <X11/Xlib.h>


namespace x {

	// Typed wrapper for an X keycode
	class Keycode {
	public:
		Keycode();
		Keycode(unsigned int code);
		explicit operator unsigned int() const;
		bool operator==(Keycode other) const;
	private:
		unsigned int code;
	};

	// Typed wrapper for an X keysym
	class Keysym {
	public:
		Keysym();
		Keysym(unsigned int sym);
		explicit operator unsigned int() const;
		Keycode toCode(Display *dpy) const;
		bool operator==(Keysym other) const;
	private:
		unsigned int sym;
	};

	// Play the system bell
	void bel(Display *dpy);

	// Run a handler on scope exit
	template <typename Cleanup>
	class UponExit {
	public:
		UponExit(Cleanup cleanup) : cleanup{cleanup} {}
		~UponExit() {
			if (cleanup) (*cleanup)();
		}
		UponExit(const UponExit&) = delete;
		UponExit(UponExit &&other) : cleanup{move(other.cleanup)} {
			other.cleanup.reset();
		}
		UponExit& operator=(const UponExit&) = delete;
		UponExit& operator=(UponExit &&other) {
			cleanup = move(other.cleanup);
			other.cleanup.reset();
		}

	private:
		std::optional<Cleanup> cleanup;
	};

	// Grab a single key (code + modifier) on the X server in the given window.
	// Drop the UponExit to ungrab. For a global grab, use
	// DefaultRootWindow(dpy) as the window.
	UponExit<std::function<void()>> XGrabKeyRAII(Display *dpy, Keycode code, int modifier, Window win);

	// Grab a the whole keyboard on the X server in the given window. Drop the
	// UponExit to ungrab. For a global grab, use DefaultRootWindow(dpy) as the
	// window.
	UponExit<std::function<void()>> XGrabKeyboardRAII(Display *dpy, Window win);

	// Open the specified display (pass nullptr to use $DISPLAY). Drop the UponExit to close.
	std::pair<Display*, UponExit<std::function<void()>>> XOpenDisplayRAII(const char *name);

	// Grab the specified key globally for any modifier combination, and run
	// the callback on every hit of that key. Ungrabs and returns when the
	// callback returns true.
	void globalKeyWatch(Display *dpy, Keysym keysym, std::function<bool(const XKeyEvent&)> callback);

	// Grab the whole keyboard globally, and run the callback on every keyboard
	// key event. Ungrabs and returns when the callback returns true.
	void globalKeyboardGrab(Display *dpy, std::function<bool(const XKeyEvent&)> callback);

}

template <>
struct std::hash<x::Keycode> {
	size_t operator()(x::Keycode code) const {
		return std::hash<unsigned int>{}((unsigned int)code);
	}
};

template <>
struct std::hash<x::Keysym> {
	size_t operator()(x::Keysym sym) const {
		return std::hash<unsigned int>{}((unsigned int)sym);
	}
};