image: set alpha when creating ARGB32 image
[awesome.git] / key.c
blob3055d8dadd1950f5317389f33922696293d9e590
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 int
44 key_ev_cmp(xcb_keysym_t keysym, xcb_keycode_t keycode,
45 unsigned long mod, const keyb_t *k)
47 if (k->keysym) {
48 if (k->keysym != keysym)
49 return k->keysym > keysym ? 1 : -1;
51 if (k->keycode) {
52 if (k->keycode != keycode)
53 return k->keycode > keycode ? 1 : -1;
55 return k->mod == mod ? 0 : (k->mod > mod ? 1 : -1);
58 static int
59 key_cmp(const keyb_t *k1, const keyb_t *k2)
61 assert ((k1->keysym && k2->keysym) || (k1->keycode && k2->keycode));
62 assert ((!k1->keysym && !k2->keysym) || (!k1->keycode && !k2->keycode));
64 if (k1->keysym != k2->keysym)
65 return k2->keysym > k1->keysym ? 1 : -1;
66 if (k1->keycode != k2->keycode)
67 return k2->keycode > k1->keycode ? 1 : -1;
68 return k1->mod == k2->mod ? 0 : (k2->mod > k1->mod ? 1 : -1);
71 /** Grab key on the root windows.
72 * \param k The key.
74 void
75 window_root_grabkey(keyb_t *k)
77 int phys_screen = 0;
78 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
79 xcb_screen_t *s;
80 xcb_keycode_t kc;
82 if((kc = k->keycode)
83 || (k->keysym
84 && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
87 s = xutil_screen_get(globalconf.connection, phys_screen);
88 xcb_grab_key(globalconf.connection, true, s->root,
89 k->mod, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
90 xcb_grab_key(globalconf.connection, true, s->root,
91 k->mod | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
92 xcb_grab_key(globalconf.connection, true, s->root,
93 k->mod | globalconf.numlockmask, kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
94 xcb_grab_key(globalconf.connection, true, s->root,
95 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK, kc, XCB_GRAB_MODE_ASYNC,
96 XCB_GRAB_MODE_ASYNC);
97 phys_screen++;
98 } while(phys_screen < nscreen);
101 /** Ungrab key on the root windows.
102 * \param k The key.
104 static void
105 window_root_ungrabkey(keyb_t *k)
107 int phys_screen = 0;
108 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
109 xcb_screen_t *s;
110 xcb_keycode_t kc;
112 if((kc = k->keycode)
113 || (k->keysym && (kc = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym))))
116 s = xutil_screen_get(globalconf.connection, phys_screen);
117 xcb_ungrab_key(globalconf.connection, kc, s->root,
118 k->mod);
119 xcb_ungrab_key(globalconf.connection, kc, s->root,
120 k->mod | XCB_MOD_MASK_LOCK);
121 xcb_ungrab_key(globalconf.connection, kc, s->root,
122 k->mod | globalconf.numlockmask);
123 xcb_ungrab_key(globalconf.connection, kc, s->root,
124 k->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
125 phys_screen++;
126 } while(phys_screen < nscreen);
129 static void
130 key_register_root(keyb_t *k)
132 key_array_t *arr = k->keysym ? &globalconf.keys.by_sym : &globalconf.keys.by_code;
133 int l = 0, r = arr->len;
135 key_ref(&k);
137 while (l < r) {
138 int i = (r + l) / 2;
139 switch (key_cmp(k, arr->tab[i])) {
140 case -1: /* k < arr->tab[i] */
141 r = i;
142 break;
143 case 0: /* k == arr->tab[i] */
144 key_unref(&arr->tab[i]);
145 arr->tab[i] = k;
146 return;
147 case 1: /* k > arr->tab[i] */
148 l = i + 1;
149 break;
153 key_array_splice(arr, r, 0, &k, 1);
154 window_root_grabkey(k);
157 static void
158 key_unregister_root(keyb_t **k)
160 key_array_t *arr = (*k)->keysym ? &globalconf.keys.by_sym : &globalconf.keys.by_code;
161 int l = 0, r = arr->len;
163 while (l < r) {
164 int i = (r + l) / 2;
165 switch (key_cmp(*k, arr->tab[i])) {
166 case -1: /* k < arr->tab[i] */
167 r = i;
168 break;
169 case 0: /* k == arr->tab[i] */
170 key_array_take(arr, i);
171 window_root_ungrabkey(*k);
172 key_unref(k);
173 return;
174 case 1: /* k > arr->tab[i] */
175 l = i + 1;
176 break;
181 /** Return the keysym from keycode.
182 * \param detail The keycode received.
183 * \param state The modifier state.
184 * \return A keysym.
186 xcb_keysym_t
187 key_getkeysym(xcb_keycode_t detail, uint16_t state)
189 xcb_keysym_t k0, k1;
191 /* 'col' (third parameter) is used to get the proper KeySym
192 * according to modifier (XCB doesn't provide an equivalent to
193 * XLookupString()).
195 * If Mod5 is ON we look into second group.
197 if(state & XCB_MOD_MASK_5)
199 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
200 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
202 else
204 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
205 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
208 /* The numlock modifier is on and the second KeySym is a keypad
209 * KeySym */
210 if((state & globalconf.numlockmask) && xcb_is_keypad_key(k1))
212 /* The Shift modifier is on, or if the Lock modifier is on and
213 * is interpreted as ShiftLock, use the first KeySym */
214 if((state & XCB_MOD_MASK_SHIFT) ||
215 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
216 return k0;
217 else
218 return k1;
221 /* The Shift and Lock modifers are both off, use the first
222 * KeySym */
223 else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
224 return k0;
226 /* The Shift modifier is off and the Lock modifier is on and is
227 * interpreted as CapsLock */
228 else if(!(state & XCB_MOD_MASK_SHIFT) &&
229 (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
230 /* The first Keysym is used but if that KeySym is lowercase
231 * alphabetic, then the corresponding uppercase KeySym is used
232 * instead */
233 return k1;
235 /* The Shift modifier is on, 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 second 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 modifer is on, or the Lock modifier is on and is
245 * interpreted as ShiftLock, or both */
246 else if((state & XCB_MOD_MASK_SHIFT) ||
247 (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
248 return k1;
250 return XCB_NO_SYMBOL;
254 keyb_t *
255 key_find(const xcb_key_press_event_t *ev)
257 const key_array_t *arr = &globalconf.keys.by_sym;
258 int l, r, mod = XUTIL_MASK_CLEAN(ev->state);
259 xcb_keysym_t keysym;
261 /* get keysym ignoring shift and mod5 */
262 keysym = key_getkeysym(ev->detail, ev->state & ~(XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_5 | XCB_MOD_MASK_LOCK));
264 again:
265 l = 0;
266 r = arr->len;
267 while (l < r)
269 int i = (r + l) / 2;
270 switch (key_ev_cmp(keysym, ev->detail, mod, arr->tab[i]))
272 case -1: /* ev < arr->tab[i] */
273 r = i;
274 break;
275 case 0: /* ev == arr->tab[i] */
276 return arr->tab[i];
277 case 1: /* ev > arr->tab[i] */
278 l = i + 1;
279 break;
282 if (arr != &globalconf.keys.by_code)
284 arr = &globalconf.keys.by_code;
285 goto again;
287 return NULL;
290 static void
291 luaA_keystore(keyb_t *key, const char *str, ssize_t len)
293 if(len)
295 if(*str != '#')
297 key->keysym = XStringToKeysym(str);
298 if (!key->keysym)
300 if (len == 1)
301 key->keysym = *str;
302 else
303 warn("there's no keysym named \"%s\"", str);
306 else
307 key->keycode = atoi(str + 1);
311 /** Define a global key binding. This key binding will always be available.
312 * \param L The Lua VM state.
314 * \luastack
315 * \lparam A table with modifier keys.
316 * \lparam A key name.
317 * \lparam A function to execute on key press.
318 * \lparam A function to execute on key release.
319 * \lreturn The key.
322 luaA_key_new(lua_State *L)
324 size_t i, len;
325 keyb_t *k;
326 const char *key;
327 luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
329 /* arg 2 is key mod table */
330 luaA_checktable(L, 2);
331 /* arg 3 is key */
332 key = luaL_checklstring(L, 3, &len);
334 if(!lua_isnil(L, 4))
335 luaA_registerfct(L, 4, &press);
337 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
338 luaA_registerfct(L, 5, &release);
340 /* get the last arg as function */
341 k = p_new(keyb_t, 1);
342 luaA_keystore(k, key, len);
344 k->press = press;
345 k->release = release;
347 len = lua_objlen(L, 2);
348 for(i = 1; i <= len; i++)
350 size_t blen;
351 lua_rawgeti(L, 2, i);
352 key = luaL_checklstring(L, -1, &blen);
353 k->mod |= xutil_key_mask_fromstr(key, blen);
356 return luaA_key_userdata_new(L, k);
359 /** Add a global key binding. This key binding will always be available.
360 * \param L The Lua VM state.
362 * \luastack
363 * \lvalue A key.
365 static int
366 luaA_key_add(lua_State *L)
368 keyb_t **k = luaA_checkudata(L, 1, "key");
369 key_register_root(*k);
370 return 0;
373 /** Remove a global key binding.
374 * \param L The Lua VM state.
376 * \luastack
377 * \lvalue A key.
379 static int
380 luaA_key_remove(lua_State *L)
382 keyb_t **k = luaA_checkudata(L, 1, "key");
383 key_unregister_root(k);
384 return 0;
387 const struct luaL_reg awesome_key_methods[] =
389 { "__call", luaA_key_new },
390 { NULL, NULL }
392 const struct luaL_reg awesome_key_meta[] =
394 { "add", luaA_key_add },
395 { "remove", luaA_key_remove },
396 { "__tostring", luaA_key_tostring },
397 { "__gc", luaA_key_gc },
398 { NULL, NULL },