luaa: merge tostring() with DO_LUA_NEW
[awesome.git] / keybinding.c
bloba53cce5c5eba568ed8243d5e1004c52a3445f26c
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 "keybinding.h"
29 ARRAY_TYPE(keybinding_t *, keybinding)
31 extern awesome_t globalconf;
33 static struct {
34 keybinding_array_t by_code;
35 keybinding_array_t by_sym;
36 } keys_g;
38 static void
39 keybinding_delete(keybinding_t **kbp)
41 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*kbp)->fct);
42 p_delete(kbp);
45 DO_RCNT(keybinding_t, keybinding, keybinding_delete)
46 ARRAY_FUNCS(keybinding_t *, keybinding, keybinding_unref)
47 DO_LUA_NEW(static, keybinding_t, keybinding, "keybinding", keybinding_ref)
48 DO_LUA_GC(keybinding_t, keybinding, "keybinding", keybinding_unref)
50 static int
51 keybinding_ev_cmp(xcb_keysym_t keysym, xcb_keycode_t keycode,
52 unsigned long mod, const keybinding_t *k)
54 if (k->keysym) {
55 if (k->keysym != keysym)
56 return k->keysym > keysym ? 1 : -1;
58 if (k->keycode) {
59 if (k->keycode != keycode)
60 return k->keycode > keycode ? 1 : -1;
62 return k->mod == mod ? 0 : (k->mod > mod ? 1 : -1);
65 static int
66 keybinding_cmp(const keybinding_t *k1, const keybinding_t *k2)
68 assert ((k1->keysym && k2->keysym) || (k1->keycode && k2->keycode));
69 assert ((!k1->keysym && !k2->keysym) || (!k1->keycode && !k2->keycode));
71 if (k1->keysym != k2->keysym)
72 return k2->keysym > k1->keysym ? 1 : -1;
73 if (k1->keycode != k2->keycode)
74 return k2->keycode > k1->keycode ? 1 : -1;
75 return k1->mod == k2->mod ? 0 : (k2->mod > k1->mod ? 1 : -1);
78 /** Grab key on the root windows.
79 * \param k The keybinding.
81 static void
82 window_root_grabkey(keybinding_t *k)
84 int phys_screen = 0;
85 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
86 xcb_screen_t *s;
87 xcb_keycode_t kc;
89 if((kc = k->keycode)
90 || (k->keysym
91 && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
94 s = xutil_screen_get(globalconf.connection, phys_screen);
95 xcb_grab_key(globalconf.connection, true, s->root,
96 k->mod, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
97 xcb_grab_key(globalconf.connection, true, s->root,
98 k->mod | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
99 xcb_grab_key(globalconf.connection, true, s->root,
100 k->mod | globalconf.numlockmask, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
101 xcb_grab_key(globalconf.connection, true, s->root,
102 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC,
103 XCB_GRAB_MODE_ASYNC);
104 phys_screen++;
105 } while(phys_screen < nscreen);
108 /** Ungrab key on the root windows.
109 * \param k The keybinding.
111 static void
112 window_root_ungrabkey(keybinding_t *k)
114 int phys_screen = 0;
115 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
116 xcb_screen_t *s;
117 xcb_keycode_t kc;
119 if((kc = k->keycode)
120 || (k->keysym && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
123 s = xutil_screen_get(globalconf.connection, phys_screen);
124 xcb_ungrab_key(globalconf.connection, kc, s->root,
125 k->mod);
126 xcb_ungrab_key(globalconf.connection, kc, s->root,
127 k->mod | XCB_MOD_MASK_LOCK);
128 xcb_ungrab_key(globalconf.connection, kc, s->root,
129 k->mod | globalconf.numlockmask);
130 xcb_ungrab_key(globalconf.connection, kc, s->root,
131 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
132 phys_screen++;
133 } while(phys_screen < nscreen);
136 static void
137 keybinding_register_root(keybinding_t *k)
139 keybinding_array_t *arr = k->keysym ? &keys_g.by_sym : &keys_g.by_code;
140 int l = 0, r = arr->len;
142 keybinding_ref(&k);
144 while (l < r) {
145 int i = (r + l) / 2;
146 switch (keybinding_cmp(k, arr->tab[i])) {
147 case -1: /* k < arr->tab[i] */
148 r = i;
149 break;
150 case 0: /* k == arr->tab[i] */
151 keybinding_unref(&arr->tab[i]);
152 arr->tab[i] = k;
153 return;
154 case 1: /* k > arr->tab[i] */
155 l = i + 1;
156 break;
160 keybinding_array_splice(arr, r, 0, &k, 1);
161 window_root_grabkey(k);
164 static void
165 keybinding_unregister_root(keybinding_t **k)
167 keybinding_array_t *arr = (*k)->keysym ? &keys_g.by_sym : &keys_g.by_code;
168 int l = 0, r = arr->len;
170 while (l < r) {
171 int i = (r + l) / 2;
172 switch (keybinding_cmp(*k, arr->tab[i])) {
173 case -1: /* k < arr->tab[i] */
174 r = i;
175 break;
176 case 0: /* k == arr->tab[i] */
177 keybinding_array_take(arr, i);
178 window_root_ungrabkey(*k);
179 keybinding_unref(k);
180 return;
181 case 1: /* k > arr->tab[i] */
182 l = i + 1;
183 break;
188 /** Return the keysym from keycode.
189 * \param detail The keycode received.
190 * \param state The modifier state.
191 * \return A keysym.
193 xcb_keysym_t
194 key_getkeysym(xcb_keycode_t detail, uint16_t state)
196 xcb_keysym_t k0, k1;
198 /* 'col' (third parameter) is used to get the proper KeySym
199 * according to modifier (XCB doesn't provide an equivalent to
200 * XLookupString()).
202 * If Mod5 is ON we look into second group.
204 if(state & XCB_MOD_MASK_5)
206 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
207 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
209 else
211 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
212 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
215 /* The numlock modifier is on and the second KeySym is a keypad
216 * KeySym */
217 if((state & globalconf.numlockmask) && xcb_is_keypad_key(k1))
219 /* The Shift modifier is on, or if the Lock modifier is on and
220 * is interpreted as ShiftLock, use the first KeySym */
221 if((state & XCB_MOD_MASK_SHIFT) ||
222 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
223 return k0;
224 else
225 return k1;
228 /* The Shift and Lock modifers are both off, use the first
229 * KeySym */
230 else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
231 return k0;
233 /* The Shift modifier is off and the Lock modifier is on and is
234 * interpreted as CapsLock */
235 else if(!(state & XCB_MOD_MASK_SHIFT) &&
236 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
237 /* The first Keysym is used but if that KeySym is lowercase
238 * alphabetic, then the corresponding uppercase KeySym is used
239 * instead */
240 return k1;
242 /* The Shift modifier is on, and the Lock modifier is on and is
243 * interpreted as CapsLock */
244 else if((state & XCB_MOD_MASK_SHIFT) &&
245 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
246 /* The second Keysym is used but if that KeySym is lowercase
247 * alphabetic, then the corresponding uppercase KeySym is used
248 * instead */
249 return k1;
251 /* The Shift modifer is on, or the Lock modifier is on and is
252 * interpreted as ShiftLock, or both */
253 else if((state & XCB_MOD_MASK_SHIFT) ||
254 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
255 return k1;
257 return XCB_NO_SYMBOL;
261 keybinding_t *
262 keybinding_find(const xcb_key_press_event_t *ev)
264 const keybinding_array_t *arr = &keys_g.by_sym;
265 int l, r, mod = XUTIL_MASK_CLEAN(ev->state);
266 xcb_keysym_t keysym;
268 /* get keysym ignoring shift and mod5 */
269 keysym = key_getkeysym(ev->detail, ev->state & ~(XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_5 | XCB_MOD_MASK_LOCK));
271 again:
272 l = 0;
273 r = arr->len;
274 while (l < r)
276 int i = (r + l) / 2;
277 switch (keybinding_ev_cmp(keysym, ev->detail, mod, arr->tab[i]))
279 case -1: /* ev < arr->tab[i] */
280 r = i;
281 break;
282 case 0: /* ev == arr->tab[i] */
283 return arr->tab[i];
284 case 1: /* ev > arr->tab[i] */
285 l = i + 1;
286 break;
289 if (arr != &keys_g.by_code)
291 arr = &keys_g.by_code;
292 goto again;
294 return NULL;
297 static void
298 luaA_keystore(keybinding_t *key, const char *str, ssize_t len)
300 if(len)
302 if(*str != '#')
304 key->keysym = XStringToKeysym(str);
305 if (!key->keysym)
307 if (len == 1)
308 key->keysym = *str;
309 else
310 warn("there's no keysym named \"%s\"", str);
313 else
314 key->keycode = atoi(str + 1);
318 /** Define a global key binding. This key binding will always be available.
319 * \param L The Lua VM state.
321 * \luastack
322 * \lparam A table with modifier keys.
323 * \lparam A key name.
324 * \lparam A function to execute.
325 * \lreturn The keybinding.
327 static int
328 luaA_keybinding_new(lua_State *L)
330 size_t i, len;
331 keybinding_t *k;
332 const char *key;
334 /* arg 2 is key mod table */
335 luaA_checktable(L, 2);
336 /* arg 3 is key */
337 key = luaL_checklstring(L, 3, &len);
338 /* arg 4 is cmd to run */
339 luaA_checkfunction(L, 4);
341 /* get the last arg as function */
342 k = p_new(keybinding_t, 1);
343 luaA_keystore(k, key, len);
344 luaA_registerfct(L, 4, &k->fct);
346 len = lua_objlen(L, 2);
347 for(i = 1; i <= len; i++)
349 size_t blen;
350 lua_rawgeti(L, 2, i);
351 key = luaL_checklstring(L, -1, &blen);
352 k->mod |= xutil_key_mask_fromstr(key, blen);
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 const struct luaL_reg awesome_keybinding_methods[] =
388 { "__call", luaA_keybinding_new },
389 { NULL, NULL }
391 const struct luaL_reg awesome_keybinding_meta[] =
393 { "add", luaA_keybinding_add },
394 { "remove", luaA_keybinding_remove },
395 { "__tostring", luaA_keybinding_tostring },
396 { "__gc", luaA_keybinding_gc },
397 { NULL, NULL },