luaa: add XDG_CONFIG_DIR as include path
[awesome.git] / keybinding.c
blobd17018d42046ae4d69d61d9e470cadb1633a06f0
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"
28 extern awesome_t globalconf;
30 static void
31 keybinding_delete(keybinding_t **kbp)
33 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*kbp)->fct);
34 p_delete(kbp);
37 DO_RCNT(keybinding_t, keybinding, keybinding_delete)
38 ARRAY_FUNCS(keybinding_t *, keybinding, keybinding_unref)
39 DO_LUA_NEW(static, keybinding_t, keybinding, "keybinding", keybinding_ref)
40 DO_LUA_GC(keybinding_t, keybinding, "keybinding", keybinding_unref)
42 static int
43 keybinding_ev_cmp(xcb_keysym_t keysym, xcb_keycode_t keycode,
44 unsigned long mod, const keybinding_t *k)
46 if (k->keysym) {
47 if (k->keysym != keysym)
48 return k->keysym > keysym ? 1 : -1;
50 if (k->keycode) {
51 if (k->keycode != keycode)
52 return k->keycode > keycode ? 1 : -1;
54 return k->mod == mod ? 0 : (k->mod > mod ? 1 : -1);
57 static int
58 keybinding_cmp(const keybinding_t *k1, const keybinding_t *k2)
60 assert ((k1->keysym && k2->keysym) || (k1->keycode && k2->keycode));
61 assert ((!k1->keysym && !k2->keysym) || (!k1->keycode && !k2->keycode));
63 if (k1->keysym != k2->keysym)
64 return k2->keysym > k1->keysym ? 1 : -1;
65 if (k1->keycode != k2->keycode)
66 return k2->keycode > k1->keycode ? 1 : -1;
67 return k1->mod == k2->mod ? 0 : (k2->mod > k1->mod ? 1 : -1);
70 /** Grab key on the root windows.
71 * \param k The keybinding.
73 void
74 window_root_grabkey(keybinding_t *k)
76 int phys_screen = 0;
77 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
78 xcb_screen_t *s;
79 xcb_keycode_t kc;
81 if((kc = k->keycode)
82 || (k->keysym
83 && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
86 s = xutil_screen_get(globalconf.connection, phys_screen);
87 xcb_grab_key(globalconf.connection, true, s->root,
88 k->mod, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
89 xcb_grab_key(globalconf.connection, true, s->root,
90 k->mod | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
91 xcb_grab_key(globalconf.connection, true, s->root,
92 k->mod | globalconf.numlockmask, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
93 xcb_grab_key(globalconf.connection, true, s->root,
94 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC,
95 XCB_GRAB_MODE_ASYNC);
96 phys_screen++;
97 } while(phys_screen < nscreen);
100 /** Ungrab key on the root windows.
101 * \param k The keybinding.
103 static void
104 window_root_ungrabkey(keybinding_t *k)
106 int phys_screen = 0;
107 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
108 xcb_screen_t *s;
109 xcb_keycode_t kc;
111 if((kc = k->keycode)
112 || (k->keysym && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
115 s = xutil_screen_get(globalconf.connection, phys_screen);
116 xcb_ungrab_key(globalconf.connection, kc, s->root,
117 k->mod);
118 xcb_ungrab_key(globalconf.connection, kc, s->root,
119 k->mod | XCB_MOD_MASK_LOCK);
120 xcb_ungrab_key(globalconf.connection, kc, s->root,
121 k->mod | globalconf.numlockmask);
122 xcb_ungrab_key(globalconf.connection, kc, s->root,
123 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
124 phys_screen++;
125 } while(phys_screen < nscreen);
128 static void
129 keybinding_register_root(keybinding_t *k)
131 keybinding_array_t *arr = k->keysym ? &globalconf.keys.by_sym : &globalconf.keys.by_code;
132 int l = 0, r = arr->len;
134 keybinding_ref(&k);
136 while (l < r) {
137 int i = (r + l) / 2;
138 switch (keybinding_cmp(k, arr->tab[i])) {
139 case -1: /* k < arr->tab[i] */
140 r = i;
141 break;
142 case 0: /* k == arr->tab[i] */
143 keybinding_unref(&arr->tab[i]);
144 arr->tab[i] = k;
145 return;
146 case 1: /* k > arr->tab[i] */
147 l = i + 1;
148 break;
152 keybinding_array_splice(arr, r, 0, &k, 1);
153 window_root_grabkey(k);
156 static void
157 keybinding_unregister_root(keybinding_t **k)
159 keybinding_array_t *arr = (*k)->keysym ? &globalconf.keys.by_sym : &globalconf.keys.by_code;
160 int l = 0, r = arr->len;
162 while (l < r) {
163 int i = (r + l) / 2;
164 switch (keybinding_cmp(*k, arr->tab[i])) {
165 case -1: /* k < arr->tab[i] */
166 r = i;
167 break;
168 case 0: /* k == arr->tab[i] */
169 keybinding_array_take(arr, i);
170 window_root_ungrabkey(*k);
171 keybinding_unref(k);
172 return;
173 case 1: /* k > arr->tab[i] */
174 l = i + 1;
175 break;
180 /** Return the keysym from keycode.
181 * \param detail The keycode received.
182 * \param state The modifier state.
183 * \return A keysym.
185 xcb_keysym_t
186 key_getkeysym(xcb_keycode_t detail, uint16_t state)
188 xcb_keysym_t k0, k1;
190 /* 'col' (third parameter) is used to get the proper KeySym
191 * according to modifier (XCB doesn't provide an equivalent to
192 * XLookupString()).
194 * If Mod5 is ON we look into second group.
196 if(state & XCB_MOD_MASK_5)
198 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
199 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
201 else
203 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
204 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
207 /* The numlock modifier is on and the second KeySym is a keypad
208 * KeySym */
209 if((state & globalconf.numlockmask) && xcb_is_keypad_key(k1))
211 /* The Shift modifier is on, or if the Lock modifier is on and
212 * is interpreted as ShiftLock, use the first KeySym */
213 if((state & XCB_MOD_MASK_SHIFT) ||
214 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
215 return k0;
216 else
217 return k1;
220 /* The Shift and Lock modifers are both off, use the first
221 * KeySym */
222 else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
223 return k0;
225 /* The Shift modifier is off and the Lock modifier is on and is
226 * interpreted as CapsLock */
227 else if(!(state & XCB_MOD_MASK_SHIFT) &&
228 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
229 /* The first Keysym is used but if that KeySym is lowercase
230 * alphabetic, then the corresponding uppercase KeySym is used
231 * instead */
232 return k1;
234 /* The Shift modifier is on, and the Lock modifier is on and is
235 * interpreted as CapsLock */
236 else if((state & XCB_MOD_MASK_SHIFT) &&
237 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
238 /* The second Keysym is used but if that KeySym is lowercase
239 * alphabetic, then the corresponding uppercase KeySym is used
240 * instead */
241 return k1;
243 /* The Shift modifer is on, or the Lock modifier is on and is
244 * interpreted as ShiftLock, or both */
245 else if((state & XCB_MOD_MASK_SHIFT) ||
246 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
247 return k1;
249 return XCB_NO_SYMBOL;
253 keybinding_t *
254 keybinding_find(const xcb_key_press_event_t *ev)
256 const keybinding_array_t *arr = &globalconf.keys.by_sym;
257 int l, r, mod = XUTIL_MASK_CLEAN(ev->state);
258 xcb_keysym_t keysym;
260 /* get keysym ignoring shift and mod5 */
261 keysym = key_getkeysym(ev->detail, ev->state & ~(XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_5 | XCB_MOD_MASK_LOCK));
263 again:
264 l = 0;
265 r = arr->len;
266 while (l < r)
268 int i = (r + l) / 2;
269 switch (keybinding_ev_cmp(keysym, ev->detail, mod, arr->tab[i]))
271 case -1: /* ev < arr->tab[i] */
272 r = i;
273 break;
274 case 0: /* ev == arr->tab[i] */
275 return arr->tab[i];
276 case 1: /* ev > arr->tab[i] */
277 l = i + 1;
278 break;
281 if (arr != &globalconf.keys.by_code)
283 arr = &globalconf.keys.by_code;
284 goto again;
286 return NULL;
289 static void
290 luaA_keystore(keybinding_t *key, const char *str, ssize_t len)
292 if(len)
294 if(*str != '#')
296 key->keysym = XStringToKeysym(str);
297 if (!key->keysym)
299 if (len == 1)
300 key->keysym = *str;
301 else
302 warn("there's no keysym named \"%s\"", str);
305 else
306 key->keycode = atoi(str + 1);
310 /** Define a global key binding. This key binding will always be available.
311 * \param L The Lua VM state.
313 * \luastack
314 * \lparam A table with modifier keys.
315 * \lparam A key name.
316 * \lparam A function to execute.
317 * \lreturn The keybinding.
319 static int
320 luaA_keybinding_new(lua_State *L)
322 size_t i, len;
323 keybinding_t *k;
324 const char *key;
326 /* arg 2 is key mod table */
327 luaA_checktable(L, 2);
328 /* arg 3 is key */
329 key = luaL_checklstring(L, 3, &len);
330 /* arg 4 is cmd to run */
331 luaA_checkfunction(L, 4);
333 /* get the last arg as function */
334 k = p_new(keybinding_t, 1);
335 luaA_keystore(k, key, len);
336 luaA_registerfct(L, 4, &k->fct);
338 len = lua_objlen(L, 2);
339 for(i = 1; i <= len; i++)
341 size_t blen;
342 lua_rawgeti(L, 2, i);
343 key = luaL_checklstring(L, -1, &blen);
344 k->mod |= xutil_key_mask_fromstr(key, blen);
347 return luaA_keybinding_userdata_new(L, k);
350 /** Add a global key binding. This key binding will always be available.
351 * \param L The Lua VM state.
353 * \luastack
354 * \lvalue A keybinding.
356 static int
357 luaA_keybinding_add(lua_State *L)
359 keybinding_t **k = luaA_checkudata(L, 1, "keybinding");
360 keybinding_register_root(*k);
361 return 0;
364 /** Remove a global key binding.
365 * \param L The Lua VM state.
367 * \luastack
368 * \lvalue A keybinding.
370 static int
371 luaA_keybinding_remove(lua_State *L)
373 keybinding_t **k = luaA_checkudata(L, 1, "keybinding");
374 keybinding_unregister_root(k);
375 return 0;
378 const struct luaL_reg awesome_keybinding_methods[] =
380 { "__call", luaA_keybinding_new },
381 { NULL, NULL }
383 const struct luaL_reg awesome_keybinding_meta[] =
385 { "add", luaA_keybinding_add },
386 { "remove", luaA_keybinding_remove },
387 { "__tostring", luaA_keybinding_tostring },
388 { "__gc", luaA_keybinding_gc },
389 { NULL, NULL },