awful.client: remove useless vars
[awesome.git] / keybinding.c
blobec88a2b0965fd297087317e79873c5d2a906da4e
1 /*
2 * keybinding.c - Key bindings configuration management
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* XStringToKeysym() */
24 #include <X11/Xlib.h>
26 #include "structs.h"
27 #include "common/refcount.h"
28 #include "common/array.h"
29 #include "keybinding.h"
31 ARRAY_TYPE(keybinding_t *, keybinding)
33 extern awesome_t globalconf;
35 static struct {
36 keybinding_array_t by_code;
37 keybinding_array_t by_sym;
38 } keys_g;
40 static void
41 keybinding_delete(keybinding_t **kbp)
43 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*kbp)->fct);
44 p_delete(kbp);
47 DO_RCNT(keybinding_t, keybinding, keybinding_delete)
48 ARRAY_FUNCS(keybinding_t *, keybinding, keybinding_unref)
49 DO_LUA_NEW(static, keybinding_t, keybinding, "keybinding", keybinding_ref)
50 DO_LUA_GC(keybinding_t, keybinding, "keybinding", keybinding_unref)
52 static int
53 keybinding_ev_cmp(xcb_keysym_t keysym, xcb_keycode_t keycode,
54 unsigned long mod, const keybinding_t *k)
56 if (k->keysym) {
57 if (k->keysym != keysym)
58 return k->keysym > keysym ? 1 : -1;
60 if (k->keycode) {
61 if (k->keycode != keycode)
62 return k->keycode > keycode ? 1 : -1;
64 return k->mod == mod ? 0 : (k->mod > mod ? 1 : -1);
67 static int
68 keybinding_cmp(const keybinding_t *k1, const keybinding_t *k2)
70 assert ((k1->keysym && k2->keysym) || (k1->keycode && k2->keycode));
71 assert ((!k1->keysym && !k2->keysym) || (!k1->keycode && !k2->keycode));
73 if (k1->keysym != k2->keysym)
74 return k2->keysym > k1->keysym ? 1 : -1;
75 if (k1->keycode != k2->keycode)
76 return k2->keycode > k1->keycode ? 1 : -1;
77 return k1->mod == k2->mod ? 0 : (k2->mod > k1->mod ? 1 : -1);
80 /** Grab key on the root windows.
81 * \param k The keybinding.
83 static void
84 window_root_grabkey(keybinding_t *k)
86 int phys_screen = 0;
87 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
88 xcb_screen_t *s;
89 xcb_keycode_t kc;
91 if((kc = k->keycode)
92 || (k->keysym
93 && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
96 s = xutil_screen_get(globalconf.connection, phys_screen);
97 xcb_grab_key(globalconf.connection, true, s->root,
98 k->mod, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
99 xcb_grab_key(globalconf.connection, true, s->root,
100 k->mod | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
101 xcb_grab_key(globalconf.connection, true, s->root,
102 k->mod | globalconf.numlockmask, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
103 xcb_grab_key(globalconf.connection, true, s->root,
104 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC,
105 XCB_GRAB_MODE_ASYNC);
106 phys_screen++;
107 } while(phys_screen < nscreen);
110 /** Ungrab key on the root windows.
111 * \param k The keybinding.
113 static void
114 window_root_ungrabkey(keybinding_t *k)
116 int phys_screen = 0;
117 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
118 xcb_screen_t *s;
119 xcb_keycode_t kc;
121 if((kc = k->keycode)
122 || (k->keysym && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
125 s = xutil_screen_get(globalconf.connection, phys_screen);
126 xcb_ungrab_key(globalconf.connection, kc, s->root,
127 k->mod);
128 xcb_ungrab_key(globalconf.connection, kc, s->root,
129 k->mod | XCB_MOD_MASK_LOCK);
130 xcb_ungrab_key(globalconf.connection, kc, s->root,
131 k->mod | globalconf.numlockmask);
132 xcb_ungrab_key(globalconf.connection, kc, s->root,
133 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
134 phys_screen++;
135 } while(phys_screen < nscreen);
138 static void
139 keybinding_register_root(keybinding_t *k)
141 keybinding_array_t *arr = k->keysym ? &keys_g.by_sym : &keys_g.by_code;
142 int l = 0, r = arr->len;
144 keybinding_ref(&k);
146 while (l < r) {
147 int i = (r + l) / 2;
148 switch (keybinding_cmp(k, arr->tab[i])) {
149 case -1: /* k < arr->tab[i] */
150 r = i;
151 break;
152 case 0: /* k == arr->tab[i] */
153 keybinding_unref(&arr->tab[i]);
154 arr->tab[i] = k;
155 return;
156 case 1: /* k > arr->tab[i] */
157 l = i + 1;
158 break;
162 keybinding_array_splice(arr, r, 0, &k, 1);
163 window_root_grabkey(k);
166 static void
167 keybinding_unregister_root(keybinding_t **k)
169 keybinding_array_t *arr = (*k)->keysym ? &keys_g.by_sym : &keys_g.by_code;
170 int l = 0, r = arr->len;
172 while (l < r) {
173 int i = (r + l) / 2;
174 switch (keybinding_cmp(*k, arr->tab[i])) {
175 case -1: /* k < arr->tab[i] */
176 r = i;
177 break;
178 case 0: /* k == arr->tab[i] */
179 keybinding_array_take(arr, i);
180 window_root_ungrabkey(*k);
181 keybinding_unref(k);
182 return;
183 case 1: /* k > arr->tab[i] */
184 l = i + 1;
185 break;
190 /** Return the keysym from keycode.
191 * \param detail The keycode received.
192 * \param state The modifier state.
193 * \return A keysym.
195 xcb_keysym_t
196 key_getkeysym(xcb_keycode_t detail, uint16_t state)
198 xcb_keysym_t k0, k1;
200 /* 'col' (third parameter) is used to get the proper KeySym
201 * according to modifier (XCB doesn't provide an equivalent to
202 * XLookupString()).
204 * If Mod5 is ON we look into second group.
206 if(state & XCB_MOD_MASK_5)
208 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
209 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
211 else
213 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
214 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
217 /* The numlock modifier is on and the second KeySym is a keypad
218 * KeySym */
219 if((state & globalconf.numlockmask) && xcb_is_keypad_key(k1))
221 /* The Shift modifier is on, or if the Lock modifier is on and
222 * is interpreted as ShiftLock, use the first KeySym */
223 if((state & XCB_MOD_MASK_SHIFT) ||
224 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
225 return k0;
226 else
227 return k1;
230 /* The Shift and Lock modifers are both off, use the first
231 * KeySym */
232 else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
233 return k0;
235 /* The Shift modifier is off and the Lock modifier is on and is
236 * interpreted as CapsLock */
237 else if(!(state & XCB_MOD_MASK_SHIFT) &&
238 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
239 /* The first Keysym is used but if that KeySym is lowercase
240 * alphabetic, then the corresponding uppercase KeySym is used
241 * instead */
242 return k1;
244 /* The Shift modifier is on, and the Lock modifier is on and is
245 * interpreted as CapsLock */
246 else if((state & XCB_MOD_MASK_SHIFT) &&
247 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
248 /* The second Keysym is used but if that KeySym is lowercase
249 * alphabetic, then the corresponding uppercase KeySym is used
250 * instead */
251 return k1;
253 /* The Shift modifer is on, or the Lock modifier is on and is
254 * interpreted as ShiftLock, or both */
255 else if((state & XCB_MOD_MASK_SHIFT) ||
256 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
257 return k1;
259 return XCB_NO_SYMBOL;
263 keybinding_t *
264 keybinding_find(const xcb_key_press_event_t *ev)
266 const keybinding_array_t *arr = &keys_g.by_sym;
267 int l, r, mod = XUTIL_MASK_CLEAN(ev->state);
268 xcb_keysym_t keysym;
270 /* get keysym ignoring shift and mod5 */
271 keysym = key_getkeysym(ev->detail, ev->state & ~(XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_5 | XCB_MOD_MASK_LOCK));
273 again:
274 l = 0;
275 r = arr->len;
276 while (l < r)
278 int i = (r + l) / 2;
279 switch (keybinding_ev_cmp(keysym, ev->detail, mod, arr->tab[i]))
281 case -1: /* ev < arr->tab[i] */
282 r = i;
283 break;
284 case 0: /* ev == arr->tab[i] */
285 return arr->tab[i];
286 case 1: /* ev > arr->tab[i] */
287 l = i + 1;
288 break;
291 if (arr != &keys_g.by_code)
293 arr = &keys_g.by_code;
294 goto again;
296 return NULL;
299 static void
300 luaA_keystore(keybinding_t *key, const char *str, ssize_t len)
302 if(len)
304 if(*str != '#')
306 key->keysym = XStringToKeysym(str);
307 if (!key->keysym)
309 if (len == 1)
310 key->keysym = *str;
311 else
312 warn("there's no keysym named \"%s\"", str);
315 else
316 key->keycode = atoi(str + 1);
320 /** Define a global key binding. This key binding will always be available.
321 * \param L The Lua VM state.
323 * \luastack
324 * \lparam A table with modifier keys.
325 * \lparam A key name.
326 * \lparam A function to execute.
327 * \lreturn The keybinding.
329 static int
330 luaA_keybinding_new(lua_State *L)
332 size_t i, len;
333 keybinding_t *k;
334 const char *key;
336 /* arg 2 is key mod table */
337 luaA_checktable(L, 2);
338 /* arg 3 is key */
339 key = luaL_checklstring(L, 3, &len);
340 /* arg 4 is cmd to run */
341 luaA_checkfunction(L, 4);
343 /* get the last arg as function */
344 k = p_new(keybinding_t, 1);
345 luaA_keystore(k, key, len);
346 luaA_registerfct(L, 4, &k->fct);
348 len = lua_objlen(L, 2);
349 for(i = 1; i <= len; i++)
351 lua_rawgeti(L, 2, i);
352 k->mod |= xutil_key_mask_fromstr(luaL_checkstring(L, -1));
355 return luaA_keybinding_userdata_new(L, k);
358 /** Add a global key binding. This key binding will always be available.
359 * \param L The Lua VM state.
361 * \luastack
362 * \lvalue A keybinding.
364 static int
365 luaA_keybinding_add(lua_State *L)
367 keybinding_t **k = luaA_checkudata(L, 1, "keybinding");
368 keybinding_register_root(*k);
369 return 0;
372 /** Remove a global key binding.
373 * \param L The Lua VM state.
375 * \luastack
376 * \lvalue A keybinding.
378 static int
379 luaA_keybinding_remove(lua_State *L)
381 keybinding_t **k = luaA_checkudata(L, 1, "keybinding");
382 keybinding_unregister_root(k);
383 return 0;
386 /** Convert a keybinding to a printable string.
387 * \return A string.
389 static int
390 luaA_keybinding_tostring(lua_State *L)
392 keybinding_t **p = luaA_checkudata(L, 1, "keybinding");
393 lua_pushfstring(L, "[keybinding udata(%p)]", *p);
394 return 1;
397 const struct luaL_reg awesome_keybinding_methods[] =
399 { "__call", luaA_keybinding_new },
400 { NULL, NULL }
402 const struct luaL_reg awesome_keybinding_meta[] =
404 { "add", luaA_keybinding_add },
405 { "remove", luaA_keybinding_remove },
406 { "__tostring", luaA_keybinding_tostring },
407 { "__gc", luaA_keybinding_gc },
408 { NULL, NULL },