-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard.c
202 lines (177 loc) · 5.54 KB
/
keyboard.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "extern.h"
#include <X11/keysym.h>
#include <err.h>
#define DONT_CHECK_MASK -1
struct binding {
KeySym keysym;
int modifiermask;
int op;
int target;
};
#if 0
#define STICKYKEYS
#endif
/*
* Assuming normal PC keyboard where Mod4Mask corresponds to Win key,
* and Mod1Mask to Alt key.
*/
#ifndef STICKYKEYS
static const struct binding keybinding[] = {
/* Focus */
{ XK_Right, Mod4Mask, FocusColumn, Forward },
{ XK_Left, Mod4Mask, FocusColumn, Backward },
{ XK_Down, Mod4Mask, FocusPane, Forward },
{ XK_Up, Mod4Mask, FocusPane, Backward },
{ XK_Tab, Mod4Mask, PrevFocus, 0 },
/* Moving */
{ XK_Right, Mod4Mask | ControlMask, MoveColumn, Forward },
{ XK_Left, Mod4Mask | ControlMask, MoveColumn, Backward },
{ XK_Down, Mod4Mask | ControlMask, MovePane, Forward },
{ XK_Up, Mod4Mask | ControlMask, MovePane, Backward },
/* Actions */
{ XK_o, Mod4Mask, Maximize, 0 },
{ XK_m, Mod4Mask, Minimize, 0 },
{ XK_h, Mod4Mask, Minimize, Others },
{ XK_f, Mod4Mask, Fullscreen, 0 },
{ XK_r, Mod4Mask, RestartCommand, 0 },
{ XK_space, Mod4Mask, EditCommand, 0 },
{ XK_Return, Mod4Mask, NewCommand, 0 },
{ XK_q, Mod4Mask, KillPane, 0 },
/* Window manager */
{ XK_F11, 0, RestartManager, 0 },
{ XK_F12, 0, QuitManager, 0 },
{ 0 }
};
#else
static const struct binding keybinding[] = {
{ XK_F3, 0, KillPane, 0 },
{ XK_F4, 0, RestartManager, 0 },
{ XK_Right, 0, FocusColumn, Forward },
{ XK_Left, 0, FocusColumn, Backward },
{ XK_Down, 0, FocusPane, Forward },
{ XK_Up, 0, FocusPane, Backward },
{ XK_Right, ControlMask, MoveColumn, Forward },
{ XK_Left, ControlMask, MoveColumn, Backward },
{ XK_Down, ControlMask, MovePane, Forward },
{ XK_Up, ControlMask, MovePane, Backward },
{ XK_F2, 0, Maximize, 0 },
{ XK_F12, 0, QuitManager, 0 },
{ XK_F1, 0, Minimize, 0 },
{ XK_f, ControlMask, Fullscreen, 0 },
{ XK_Tab, 0, PrevFocus, 0 },
{ XK_F5, 0, RestartCommand, 0 },
{ XK_Super_R, DONT_CHECK_MASK, ToggleMode, 0 },
{ XK_Super_L, DONT_CHECK_MASK, ToggleMode, 0 },
{ 0 }
};
#endif
#ifdef STICKYKEYS
static const struct binding modebind[] = {
{ XK_Super_R, DONT_CHECK_MASK, ToggleMode, 0 },
{ XK_Super_L, DONT_CHECK_MASK, ToggleMode, 0 },
{ 0 }
};
static int _mode;
#endif
static void _unbind_keys(Display *, Window, const struct binding *);
static void _bind_keys(Display *, Window, const struct binding *);
static int _find_binding(XKeyEvent *, int *target, const struct binding *);
#include <stdlib.h>
#ifdef STICKYKEYS
void toggle_keybind_mode(Display *display)
{
TRACE("togglemode %d", _mode);
_mode ^= 1;
TRACE("togglemode now %d", _mode);
if (_mode == 1) {
_unbind_keys(display, DefaultRootWindow(display), modebind);
bind_keys(display, DefaultRootWindow(display));
system("xsetroot -solid red");
} else {
unbind_keys(display, DefaultRootWindow(display));
_bind_keys(display, DefaultRootWindow(display), modebind);
system("xsetroot -solid black");
}
}
#endif
int
find_binding(XKeyEvent *xkey, int *target)
{
#ifdef STICKYKEYS
if (_mode == 0) {
TRACE("finding modebind");
return _find_binding(xkey, target, modebind);
}
#endif
TRACE("finding keybinding");
return _find_binding(xkey, target, keybinding);
}
static int
_find_binding(XKeyEvent *xkey, int *target, const struct binding *bindings)
{
int i, mask;
KeySym sym;
const struct binding *kb;
sym = XLookupKeysym(xkey, 0);
mask = xkey->state;
for (i = 0; bindings[i].keysym != 0; i++) {
kb = &(bindings[i]);
TRACE("checking binding sym vs sym %d %d", (int) kb->keysym, (int) sym);
TRACE("checking mask vs mask %d %d", (int) kb->modifiermask, (int) mask);
if (kb->keysym == sym &&
(kb->modifiermask == mask ||
kb->modifiermask == DONT_CHECK_MASK)) {
*target = kb->target;
return kb->op;
}
}
warnx("binding not found");
return -1;
}
static void
_bind_keys(Display *display, Window root, const struct binding *bindings)
{
int i, code, mask, mouse, kbd;
Bool owner_events;
for (i = 0; bindings[i].keysym != 0; i++) {
code = XKeysymToKeycode(display, bindings[i].keysym);
if (bindings[i].modifiermask == DONT_CHECK_MASK)
mask = 0;
else
mask = bindings[i].modifiermask;
mouse = GrabModeSync;
kbd = GrabModeAsync;
owner_events = False;
XGrabKey(display, code, mask, root, owner_events, mouse, kbd);
}
}
static void
_unbind_keys(Display *display, Window root, const struct binding *bindings)
{
int i, code, mask;
for (i = 0; bindings[i].keysym != 0; i++) {
code = XKeysymToKeycode(display, bindings[i].keysym);
if (bindings[i].modifiermask == DONT_CHECK_MASK)
mask = 0;
else
mask = bindings[i].modifiermask;
XUngrabKey(display, code, mask, root);
}
}
void
unbind_keys(Display *display, Window root)
{
_unbind_keys(display, root, keybinding);
}
void
bind_keys(Display *display, Window root)
{
_bind_keys(display, root, keybinding);
}
#ifdef STICKYKEYS
void
bind_mode_keys(Display *display, Window root)
{
_bind_keys(display, root, modebind);
}
#endif