awful.widget.taglist: remove needless taglist_squares conditions
[awesome.git] / key.c
blobfe0425444f2e6c9a2b66f085ccd6e4134bf82d9c
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 LUA_OBJECT_FUNCS(keyb_t, key, "key")
30 static void
31 key_unref_simplified(keyb_t **b)
33 key_unref(globalconf.L, *b);
36 ARRAY_FUNCS(keyb_t *, key, key_unref_simplified)
37 DO_LUA_TOSTRING(keyb_t, key, "key")
39 /** Garbage collect a key.
40 * \param L The Lua VM state.
41 * \return 0.
43 static int
44 luaA_key_gc(lua_State *L)
46 keyb_t *kbp = luaL_checkudata(L, 1, "key");
47 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, kbp->press);
48 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, kbp->release);
49 return 0;
52 static void
53 keybindings_init(keybindings_t *kb)
55 key_array_init(&kb->by_code);
56 key_array_init(&kb->by_sym);
59 static void
60 keybindings_wipe(keybindings_t *kb)
62 key_array_wipe(&kb->by_code);
63 key_array_wipe(&kb->by_sym);
66 static int
67 key_ev_cmp(xcb_keysym_t keysym, xcb_keycode_t keycode,
68 unsigned long mod, const keyb_t *k)
70 if(k->keysym) {
71 if(k->keysym != keysym)
72 return k->keysym > keysym ? 1 : -1;
74 if(k->keycode) {
75 if(k->keycode != keycode)
76 return k->keycode > keycode ? 1 : -1;
78 return k->mod == mod ? 0 : (k->mod > mod ? 1 : -1);
81 static int
82 key_cmp(const keyb_t *k1, const keyb_t *k2)
84 assert ((k1->keysym && k2->keysym) || (k1->keycode && k2->keycode));
85 assert ((!k1->keysym && !k2->keysym) || (!k1->keycode && !k2->keycode));
87 if(k1->keysym != k2->keysym)
88 return k2->keysym > k1->keysym ? 1 : -1;
89 if(k1->keycode != k2->keycode)
90 return k2->keycode > k1->keycode ? 1 : -1;
91 return k1->mod == k2->mod ? 0 : (k2->mod > k1->mod ? 1 : -1);
94 /** Grab key on a window.
95 * \param win The window.
96 * \param k The key.
98 static void
99 window_grabkey(xcb_window_t win, keyb_t *k)
101 xcb_keycode_t kc;
103 if((kc = k->keycode)
104 || (k->keysym
105 && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
107 xcb_grab_key(globalconf.connection, true, win,
108 k->mod, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
109 xcb_grab_key(globalconf.connection, true, win,
110 k->mod | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
111 xcb_grab_key(globalconf.connection, true, win,
112 k->mod | globalconf.numlockmask, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
113 xcb_grab_key(globalconf.connection, true, win,
114 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC,
115 XCB_GRAB_MODE_ASYNC);
119 void
120 window_grabkeys(xcb_window_t win, keybindings_t *keys)
122 for(int i = 0; i < keys->by_code.len; i++)
123 window_grabkey(win, keys->by_code.tab[i]);
125 for(int i = 0; i < keys->by_sym.len; i++)
126 window_grabkey(win, keys->by_sym.tab[i]);
129 /** Register a key which on top of the stack.
130 * \param keys The keybinding array where to put the key.
132 static void
133 key_register(keybindings_t *keys)
135 keyb_t *k = key_ref(globalconf.L);
136 key_array_t *arr = k->keysym ? &keys->by_sym : &keys->by_code;
137 int l = 0, r = arr->len;
139 while(l < r) {
140 int i = (r + l) / 2;
141 switch(key_cmp(k, arr->tab[i]))
143 case -1: /* k < arr->tab[i] */
144 r = i;
145 break;
146 case 0: /* k == arr->tab[i] */
147 key_unref(globalconf.L, arr->tab[i]);
148 arr->tab[i] = k;
149 return;
150 case 1: /* k > arr->tab[i] */
151 l = i + 1;
152 break;
156 key_array_splice(arr, r, 0, &k, 1);
159 /** Return the keysym from keycode.
160 * \param detail The keycode received.
161 * \param state The modifier state.
162 * \return A keysym.
164 xcb_keysym_t
165 key_getkeysym(xcb_keycode_t detail, uint16_t state)
167 xcb_keysym_t k0, k1;
169 /* 'col' (third parameter) is used to get the proper KeySym
170 * according to modifier (XCB doesn't provide an equivalent to
171 * XLookupString()).
173 * If Mod5 is ON we look into second group.
175 if(state & XCB_MOD_MASK_5)
177 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
178 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
180 else
182 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
183 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
186 /* If the second column does not exists use the first one. */
187 if(k1 == XCB_NONE)
188 k1 = k0;
190 /* The numlock modifier is on and the second KeySym is a keypad
191 * KeySym */
192 if((state & globalconf.numlockmask) && xcb_is_keypad_key(k1))
194 /* The Shift modifier is on, or if the Lock modifier is on and
195 * is interpreted as ShiftLock, use the first KeySym */
196 if((state & XCB_MOD_MASK_SHIFT) ||
197 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
198 return k0;
199 else
200 return k1;
203 /* The Shift and Lock modifers are both off, use the first
204 * KeySym */
205 else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
206 return k0;
208 /* The Shift modifier is off and the Lock modifier is on and is
209 * interpreted as CapsLock */
210 else if(!(state & XCB_MOD_MASK_SHIFT) &&
211 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
212 /* The first Keysym is used but if that KeySym is lowercase
213 * alphabetic, then the corresponding uppercase KeySym is used
214 * instead */
215 return k1;
217 /* The Shift modifier is on, and the Lock modifier is on and is
218 * interpreted as CapsLock */
219 else if((state & XCB_MOD_MASK_SHIFT) &&
220 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
221 /* The second Keysym is used but if that KeySym is lowercase
222 * alphabetic, then the corresponding uppercase KeySym is used
223 * instead */
224 return k1;
226 /* The Shift modifer is on, or the Lock modifier is on and is
227 * interpreted as ShiftLock, or both */
228 else if((state & XCB_MOD_MASK_SHIFT) ||
229 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
230 return k1;
232 return XCB_NO_SYMBOL;
236 keyb_t *
237 key_find(keybindings_t *keys, const xcb_key_press_event_t *ev)
239 const key_array_t *arr = &keys->by_sym;
240 int l, r, mod = XUTIL_MASK_CLEAN(ev->state);
241 xcb_keysym_t keysym;
243 /* get keysym ignoring shift and mod5 */
244 keysym = key_getkeysym(ev->detail, ev->state & ~(XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_5 | XCB_MOD_MASK_LOCK));
246 again:
247 l = 0;
248 r = arr->len;
249 while(l < r)
251 int i = (r + l) / 2;
252 switch(key_ev_cmp(keysym, ev->detail, mod, arr->tab[i]))
254 case -1: /* ev < arr->tab[i] */
255 r = i;
256 break;
257 case 0: /* ev == arr->tab[i] */
258 return arr->tab[i];
259 case 1: /* ev > arr->tab[i] */
260 l = i + 1;
261 break;
264 if(arr != &keys->by_code)
266 arr = &keys->by_code;
267 goto again;
269 return NULL;
272 static void
273 luaA_keystore(keyb_t *key, const char *str, ssize_t len)
275 if(len)
277 if(*str != '#')
279 key->keysym = XStringToKeysym(str);
280 if(!key->keysym)
282 if(len == 1)
283 key->keysym = *str;
284 else
285 warn("there's no keysym named \"%s\"", str);
288 else
289 key->keycode = atoi(str + 1);
293 /** Define a global key binding. This key binding will always be available.
294 * \param L The Lua VM state.
296 * \luastack
297 * \lparam A table with modifier keys.
298 * \lparam A key name.
299 * \lparam A function to execute on key press.
300 * \lparam A function to execute on key release.
301 * \lreturn The key.
303 static int
304 luaA_key_new(lua_State *L)
306 size_t i, len;
307 keyb_t *k;
308 const char *key;
309 luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
311 /* arg 2 is key mod table */
312 luaA_checktable(L, 2);
313 /* arg 3 is key */
314 key = luaL_checklstring(L, 3, &len);
316 if(!lua_isnil(L, 4))
317 luaA_registerfct(L, 4, &press);
319 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
320 luaA_registerfct(L, 5, &release);
322 k = key_new(L);
323 luaA_keystore(k, key, len);
325 k->press = press;
326 k->release = release;
328 len = lua_objlen(L, 2);
329 for(i = 1; i <= len; i++)
331 size_t blen;
332 lua_rawgeti(L, 2, i);
333 key = luaL_checklstring(L, -1, &blen);
334 lua_pop(L, 1);
335 k->mod |= xutil_key_mask_fromstr(key, blen);
338 return 1;
341 /** Set a key array with a Lua table.
342 * \param L The Lua VM state.
343 * \param idx The index of the Lua table.
344 * \param keys The array key to fill.
346 void
347 luaA_key_array_set(lua_State *L, int idx, keybindings_t *keys)
349 luaA_checktable(L, idx);
351 keybindings_wipe(keys);
352 keybindings_init(keys);
354 lua_pushnil(L);
355 while(lua_next(L, idx))
356 key_register(keys);
359 /** Push an array of key as an Lua table onto the stack.
360 * \param L The Lua VM state.
361 * \param keys The key array to push.
362 * \return The number of elements pushed on stack.
365 luaA_key_array_get(lua_State *L, keybindings_t *keys)
367 lua_createtable(L, keys->by_code.len + keys->by_sym.len, 0);
368 for(int i = 0; i < keys->by_code.len; i++)
370 key_push(L, keys->by_code.tab[i]);
371 lua_rawseti(L, -2, i + 1);
373 for(int i = 0; i < keys->by_sym.len; i++)
375 key_push(L, keys->by_sym.tab[i]);
376 lua_rawseti(L, -2, i + 1);
378 return 1;
381 const struct luaL_reg awesome_key_methods[] =
383 { "__call", luaA_key_new },
384 { NULL, NULL }
386 const struct luaL_reg awesome_key_meta[] =
388 { "__tostring", luaA_key_tostring },
389 { "__gc", luaA_key_gc },
390 { NULL, NULL },