-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_key.c
292 lines (237 loc) · 8.38 KB
/
grab_key.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/***************************************************************************
xbindkeys : a program to bind keys to commands under X11.
-------------------
begin : Sat Oct 13 14:11:34 CEST 2001
copyright : (C) 2001 by Philippe Brochard
email : hocwp@free.fr
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
#include "xbindkeys.h"
#include "grab_key.h"
#include "options.h"
#include "keys.h"
unsigned int numlock_mask = 0;
unsigned int scrolllock_mask = 0;
unsigned int capslock_mask = 0;
static void my_grab_key (Display * dpy, KeyCode keycode,
unsigned int modifier, Window win);
void
get_offending_modifiers (Display * dpy)
{
int i;
XModifierKeymap *modmap;
KeyCode nlock, slock;
static int mask_table[8] = {
ShiftMask, LockMask, ControlMask, Mod1Mask,
Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
};
nlock = XKeysymToKeycode (dpy, XK_Num_Lock);
slock = XKeysymToKeycode (dpy, XK_Scroll_Lock);
/*
* Find out the masks for the NumLock and ScrollLock modifiers,
* so that we can bind the grabs for when they are enabled too.
*/
modmap = XGetModifierMapping (dpy);
if (modmap != NULL && modmap->max_keypermod > 0)
{
for (i = 0; i < 8 * modmap->max_keypermod; i++)
{
if (modmap->modifiermap[i] == nlock && nlock != 0)
numlock_mask = mask_table[i / modmap->max_keypermod];
else if (modmap->modifiermap[i] == slock && slock != 0)
scrolllock_mask = mask_table[i / modmap->max_keypermod];
}
}
capslock_mask = LockMask;
if (modmap)
XFreeModifiermap (modmap);
}
static void
my_grab_key (Display * dpy, KeyCode keycode, unsigned int modifier,
Window win)
{
modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);
XGrabKey (dpy, keycode, modifier, (win ? win : DefaultRootWindow (dpy)),
False, GrabModeAsync, GrabModeAsync);
if (modifier == AnyModifier)
return;
if (numlock_mask)
XGrabKey (dpy, keycode, modifier | numlock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, GrabModeAsync, GrabModeAsync);
if (capslock_mask)
XGrabKey (dpy, keycode, modifier | capslock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, GrabModeAsync, GrabModeAsync);
if (scrolllock_mask)
XGrabKey (dpy, keycode, modifier | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, GrabModeAsync, GrabModeAsync);
if (numlock_mask && capslock_mask)
XGrabKey (dpy, keycode, modifier | numlock_mask | capslock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, GrabModeAsync, GrabModeAsync);
if (numlock_mask && scrolllock_mask)
XGrabKey (dpy, keycode, modifier | numlock_mask | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, GrabModeAsync, GrabModeAsync);
if (capslock_mask && scrolllock_mask)
XGrabKey (dpy, keycode, modifier | capslock_mask | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, GrabModeAsync, GrabModeAsync);
if (numlock_mask && capslock_mask && scrolllock_mask)
XGrabKey (dpy, keycode,
modifier | numlock_mask | capslock_mask | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)), False, GrabModeAsync,
GrabModeAsync);
}
static void
my_grab_button (Display * dpy, unsigned int button, unsigned int modifier,
Window win)
{
modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);
XGrabButton (dpy, button, modifier, (win ? win : DefaultRootWindow (dpy)),
False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None);
if (modifier == AnyModifier)
return;
if (numlock_mask)
XGrabButton (dpy, button, modifier | numlock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None);
if (capslock_mask)
XGrabButton (dpy, button, modifier | capslock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None);
if (scrolllock_mask)
XGrabButton (dpy, button, modifier | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None);
if (numlock_mask && capslock_mask)
XGrabButton (dpy, button, modifier | numlock_mask | capslock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None);
if (numlock_mask && scrolllock_mask)
XGrabButton (dpy, button, modifier | numlock_mask | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None);
if (capslock_mask && scrolllock_mask)
XGrabButton (dpy, button, modifier | capslock_mask | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)),
False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None);
if (numlock_mask && capslock_mask && scrolllock_mask)
XGrabButton (dpy, button,
modifier | numlock_mask | capslock_mask | scrolllock_mask,
(win ? win : DefaultRootWindow (dpy)), False,
ButtonPressMask | ButtonReleaseMask, GrabModeAsync,
GrabModeAsync, None, None);
}
void
ungrab_all_keys (Display * dpy)
{
int screen;
for (screen = 0; screen < ScreenCount (dpy); screen++)
{
XUngrabKey (dpy, AnyKey, AnyModifier, RootWindow (dpy, screen));
XUngrabButton (dpy, AnyButton, AnyModifier, RootWindow (dpy, screen));
}
}
void
grab_keys (Display * dpy)
{
int i;
int min, max;
int screen;
XDisplayKeycodes (dpy, &min, &max);
if (verbose)
{
printf ("\n");
printf ("min_keycode=%d max_keycode=%d (ie: know keycodes)\n", min,
max);
}
ungrab_all_keys (dpy);
for (i = 0; i < nb_keys; i++)
{
print_key (dpy, &keys[i]);
if (keys[i].type == SYM)
{
for (screen = 0; screen < ScreenCount (dpy); screen++)
{
KeyCode code = XKeysymToKeycode (dpy, keys[i].key.sym);
if (code != 0)
{
my_grab_key (dpy, code,
keys[i].modifier, RootWindow (dpy, screen));
}
else
{
fprintf (stderr, "--- xbindkeys error ---\n");
if (!verbose)
{
verbose = 1;
print_key (dpy, &keys[i]);
verbose = 0;
}
fprintf (stderr,
" The key symbol '%s' cannot be used, as it's not mapped\n"
" on your keyboard.\n"
" xbindkeys will keep running, but will ignore this symbol.\n",
XKeysymToString (keys[i].key.sym));
}
}
}
else if (keys[i].type == BUTTON)
{
for (screen = 0; screen < ScreenCount (dpy); screen++)
{
my_grab_button (dpy, keys[i].key.button, keys[i].modifier,
RootWindow (dpy, screen));
}
}
else
{
if (keys[i].key.code >= min && keys[i].key.code <= max)
{
for (screen = 0; screen < ScreenCount (dpy); screen++)
{
my_grab_key (dpy, keys[i].key.code, keys[i].modifier,
RootWindow (dpy, screen));
}
}
else
{
fprintf (stderr, "--- xbindkeys error ---\n");
if (!verbose)
{
verbose = 1;
print_key (dpy, &keys[i]);
}
fprintf (stderr,
" The keycode %d cannot be used, as it's not between the\n"
" min(%d) and max(%d) keycode of your keyboard.\n"
" Please increase the 'maximum' value in\n"
" /usr/X11R6/lib/X11/xkb/keycodes/xfree86,\n"
" then restart X.\n", keys[i].key.code, min, max);
end_it_all (dpy);
exit (-1);
}
}
}
}