ewmh: add missing window types
[awesome.git] / key.c
blob58680564516d56c7107a206fd65b3df008193c4a
1 /*
2 * key.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"
28 extern awesome_t globalconf;
30 static void
31 key_delete(keyb_t **kbp)
33 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*kbp)->press);
34 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*kbp)->release);
35 p_delete(kbp);
38 DO_RCNT(keyb_t, key, key_delete)
39 ARRAY_FUNCS(keyb_t *, key, key_unref)
40 DO_LUA_NEW(static, keyb_t, key, "key", key_ref)
41 DO_LUA_GC(keyb_t, key, "key", key_unref)
43 static void
44 keybindings_init(keybindings_t *kb)
46 key_array_init(&kb->by_code);
47 key_array_init(&kb->by_sym);
50 static void
51 keybindings_wipe(keybindings_t *kb)
53 key_array_wipe(&kb->by_code);
54 key_array_wipe(&kb->by_sym);
57 static int
58 key_ev_cmp(xcb_keysym_t keysym, xcb_keycode_t keycode,
59 unsigned long mod, const keyb_t *k)
61 if (k->keysym) {
62 if (k->keysym != keysym)
63 return k->keysym > keysym ? 1 : -1;
65 if (k->keycode) {
66 if (k->keycode != keycode)
67 return k->keycode > keycode ? 1 : -1;
69 return k->mod == mod ? 0 : (k->mod > mod ? 1 : -1);
72 static int
73 key_cmp(const keyb_t *k1, const keyb_t *k2)
75 assert ((k1->keysym && k2->keysym) || (k1->keycode && k2->keycode));
76 assert ((!k1->keysym && !k2->keysym) || (!k1->keycode && !k2->keycode));
78 if (k1->keysym != k2->keysym)
79 return k2->keysym > k1->keysym ? 1 : -1;
80 if (k1->keycode != k2->keycode)
81 return k2->keycode > k1->keycode ? 1 : -1;
82 return k1->mod == k2->mod ? 0 : (k2->mod > k1->mod ? 1 : -1);
85 /** Grab key on a window.
86 * \param win The window.
87 * \param k The key.
89 static void
90 window_grabkey(xcb_window_t win, keyb_t *k)
92 xcb_keycode_t kc;
94 if((kc = k->keycode)
95 || (k->keysym
96 && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
98 xcb_grab_key(globalconf.connection, true, win,
99 k->mod, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
100 xcb_grab_key(globalconf.connection, true, win,
101 k->mod | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
102 xcb_grab_key(globalconf.connection, true, win,
103 k->mod | globalconf.numlockmask, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
104 xcb_grab_key(globalconf.connection, true, win,
105 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC,
106 XCB_GRAB_MODE_ASYNC);
110 void
111 window_grabkeys(xcb_window_t win, keybindings_t *keys)
113 for(int i = 0; i < keys->by_code.len; i++)
114 window_grabkey(win, keys->by_code.tab[i]);
116 for(int i = 0; i < keys->by_sym.len; i++)
117 window_grabkey(win, keys->by_sym.tab[i]);
120 static void
121 key_register(keybindings_t *keys, keyb_t *k)
123 key_array_t *arr = k->keysym ? &keys->by_sym : &keys->by_code;
124 int l = 0, r = arr->len;
126 key_ref(&k);
128 while (l < r) {
129 int i = (r + l) / 2;
130 switch (key_cmp(k, arr->tab[i])) {
131 case -1: /* k < arr->tab[i] */
132 r = i;
133 break;
134 case 0: /* k == arr->tab[i] */
135 key_unref(&arr->tab[i]);
136 arr->tab[i] = k;
137 return;
138 case 1: /* k > arr->tab[i] */
139 l = i + 1;
140 break;
144 key_array_splice(arr, r, 0, &k, 1);
147 /** Return the keysym from keycode.
148 * \param detail The keycode received.
149 * \param state The modifier state.
150 * \return A keysym.
152 xcb_keysym_t
153 key_getkeysym(xcb_keycode_t detail, uint16_t state)
155 xcb_keysym_t k0, k1;
157 /* 'col' (third parameter) is used to get the proper KeySym
158 * according to modifier (XCB doesn't provide an equivalent to
159 * XLookupString()).
161 * If Mod5 is ON we look into second group.
163 if(state & XCB_MOD_MASK_5)
165 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
166 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
168 else
170 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
171 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
174 /* If the second column does not exists use the first one. */
175 if(k1 == XCB_NONE)
176 k1 = k0;
178 /* The numlock modifier is on and the second KeySym is a keypad
179 * KeySym */
180 if((state & globalconf.numlockmask) && xcb_is_keypad_key(k1))
182 /* The Shift modifier is on, or if the Lock modifier is on and
183 * is interpreted as ShiftLock, use the first KeySym */
184 if((state & XCB_MOD_MASK_SHIFT) ||
185 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
186 return k0;
187 else
188 return k1;
191 /* The Shift and Lock modifers are both off, use the first
192 * KeySym */
193 else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
194 return k0;
196 /* The Shift modifier is off and the Lock modifier is on and is
197 * interpreted as CapsLock */
198 else if(!(state & XCB_MOD_MASK_SHIFT) &&
199 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
200 /* The first Keysym is used but if that KeySym is lowercase
201 * alphabetic, then the corresponding uppercase KeySym is used
202 * instead */
203 return k1;
205 /* The Shift modifier is on, and the Lock modifier is on and is
206 * interpreted as CapsLock */
207 else if((state & XCB_MOD_MASK_SHIFT) &&
208 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
209 /* The second Keysym is used but if that KeySym is lowercase
210 * alphabetic, then the corresponding uppercase KeySym is used
211 * instead */
212 return k1;
214 /* The Shift modifer is on, or the Lock modifier is on and is
215 * interpreted as ShiftLock, or both */
216 else if((state & XCB_MOD_MASK_SHIFT) ||
217 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
218 return k1;
220 return XCB_NO_SYMBOL;
224 keyb_t *
225 key_find(keybindings_t *keys, const xcb_key_press_event_t *ev)
227 const key_array_t *arr = &keys->by_sym;
228 int l, r, mod = XUTIL_MASK_CLEAN(ev->state);
229 xcb_keysym_t keysym;
231 /* get keysym ignoring shift and mod5 */
232 keysym = key_getkeysym(ev->detail, ev->state & ~(XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_5 | XCB_MOD_MASK_LOCK));
234 again:
235 l = 0;
236 r = arr->len;
237 while (l < r)
239 int i = (r + l) / 2;
240 switch (key_ev_cmp(keysym, ev->detail, mod, arr->tab[i]))
242 case -1: /* ev < arr->tab[i] */
243 r = i;
244 break;
245 case 0: /* ev == arr->tab[i] */
246 return arr->tab[i];
247 case 1: /* ev > arr->tab[i] */
248 l = i + 1;
249 break;
252 if(arr != &keys->by_code)
254 arr = &keys->by_code;
255 goto again;
257 return NULL;
260 static void
261 luaA_keystore(keyb_t *key, const char *str, ssize_t len)
263 if(len)
265 if(*str != '#')
267 key->keysym = XStringToKeysym(str);
268 if (!key->keysym)
270 if (len == 1)
271 key->keysym = *str;
272 else
273 warn("there's no keysym named \"%s\"", str);
276 else
277 key->keycode = atoi(str + 1);
281 /** Define a global key binding. This key binding will always be available.
282 * \param L The Lua VM state.
284 * \luastack
285 * \lparam A table with modifier keys.
286 * \lparam A key name.
287 * \lparam A function to execute on key press.
288 * \lparam A function to execute on key release.
289 * \lreturn The key.
292 luaA_key_new(lua_State *L)
294 size_t i, len;
295 keyb_t *k;
296 const char *key;
297 luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
299 /* arg 2 is key mod table */
300 luaA_checktable(L, 2);
301 /* arg 3 is key */
302 key = luaL_checklstring(L, 3, &len);
304 if(!lua_isnil(L, 4))
305 luaA_registerfct(L, 4, &press);
307 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
308 luaA_registerfct(L, 5, &release);
310 /* get the last arg as function */
311 k = p_new(keyb_t, 1);
312 luaA_keystore(k, key, len);
314 k->press = press;
315 k->release = release;
317 len = lua_objlen(L, 2);
318 for(i = 1; i <= len; i++)
320 size_t blen;
321 lua_rawgeti(L, 2, i);
322 key = luaL_checklstring(L, -1, &blen);
323 k->mod |= xutil_key_mask_fromstr(key, blen);
326 return luaA_key_userdata_new(L, k);
329 /** Set a key array with a Lua table.
330 * \param L The Lua VM state.
331 * \param idx The index of the Lua table.
332 * \param keys The array key to fill.
334 void
335 luaA_key_array_set(lua_State *L, int idx, keybindings_t *keys)
337 luaA_checktable(L, idx);
339 for(int i = 0; i < keys->by_code.len; i++);
341 for(int i = 0; i < keys->by_sym.len; i++);
343 keybindings_wipe(keys);
345 keybindings_init(keys);
347 lua_pushnil(L);
348 while(lua_next(L, idx))
350 keyb_t **k = luaA_checkudata(L, -1, "key");
351 key_register(keys, *k);
352 lua_pop(L, 1);
356 /** Push an array of key as an Lua table onto the stack.
357 * \param L The Lua VM state.
358 * \param keys The key array to push.
359 * \return The number of elements pushed on stack.
362 luaA_key_array_get(lua_State *L, keybindings_t *keys)
364 luaA_otable_new(L);
365 for(int i = 0; i < keys->by_code.len; i++)
367 luaA_key_userdata_new(L, keys->by_code.tab[i]);
368 lua_rawseti(L, -2, i + 1);
370 for(int i = 0; i < keys->by_sym.len; i++)
372 luaA_key_userdata_new(L, keys->by_sym.tab[i]);
373 lua_rawseti(L, -2, i + 1);
375 return 1;
378 /** Add a global key binding. This key binding will always be available.
379 * \param L The Lua VM state.
381 * \luastack
382 * \lvalue A key.
384 static int
385 luaA_key_add(lua_State *L)
387 luaA_deprecate(L, "root.keys");
388 keyb_t **k = luaA_checkudata(L, 1, "key");
389 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
391 key_register(&globalconf.keys, *k);
393 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
395 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
396 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
397 window_grabkeys(s->root, &globalconf.keys);
400 return 0;
403 /** Remove a global key binding.
404 * \param L The Lua VM state.
406 * \luastack
407 * \lvalue A key.
409 static int
410 luaA_key_remove(lua_State *L)
412 luaA_deprecate(L, "root.keys");
413 return 0;
416 const struct luaL_reg awesome_key_methods[] =
418 { "__call", luaA_key_new },
419 { NULL, NULL }
421 const struct luaL_reg awesome_key_meta[] =
423 { "add", luaA_key_add },
424 { "remove", luaA_key_remove },
425 { "__tostring", luaA_key_tostring },
426 { "__gc", luaA_key_gc },
427 { NULL, NULL },