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() */
27 #include "common/refcount.h"
28 #include "common/array.h"
29 #include "keybinding.h"
31 ARRAY_TYPE(keybinding_t
*, keybinding
)
33 extern awesome_t globalconf
;
36 keybinding_array_t by_code
;
37 keybinding_array_t by_sym
;
41 keybinding_delete(keybinding_t
**kbp
)
43 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*kbp
)->fct
);
47 DO_RCNT(keybinding_t
, keybinding
, keybinding_delete
)
48 ARRAY_FUNCS(keybinding_t
*, keybinding
, keybinding_unref
)
49 DO_LUA_NEW(static, keybinding_t
, keybinding
, "keybinding", keybinding_ref
)
50 DO_LUA_GC(keybinding_t
, keybinding
, "keybinding", keybinding_unref
)
53 keybinding_ev_cmp(xcb_keysym_t keysym
, xcb_keycode_t keycode
,
54 unsigned long mod
, const keybinding_t
*k
)
57 if (k
->keysym
!= keysym
)
58 return k
->keysym
> keysym
? 1 : -1;
61 if (k
->keycode
!= keycode
)
62 return k
->keycode
> keycode
? 1 : -1;
64 return k
->mod
== mod
? 0 : (k
->mod
> mod
? 1 : -1);
68 keybinding_cmp(const keybinding_t
*k1
, const keybinding_t
*k2
)
70 assert ((k1
->keysym
&& k2
->keysym
) || (k1
->keycode
&& k2
->keycode
));
71 assert ((!k1
->keysym
&& !k2
->keysym
) || (!k1
->keycode
&& !k2
->keycode
));
73 if (k1
->keysym
!= k2
->keysym
)
74 return k2
->keysym
> k1
->keysym
? 1 : -1;
75 if (k1
->keycode
!= k2
->keycode
)
76 return k2
->keycode
> k1
->keycode
? 1 : -1;
77 return k1
->mod
== k2
->mod
? 0 : (k2
->mod
> k1
->mod
? 1 : -1);
80 /** Grab key on the root windows.
81 * \param k The keybinding.
84 window_root_grabkey(keybinding_t
*k
)
87 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
93 && (kc
= xcb_key_symbols_get_keycode(globalconf
.keysyms
, k
->keysym
))))
96 s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
97 xcb_grab_key(globalconf
.connection
, true, s
->root
,
98 k
->mod
, kc
, XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
);
99 xcb_grab_key(globalconf
.connection
, true, s
->root
,
100 k
->mod
| XCB_MOD_MASK_LOCK
, kc
, XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
);
101 xcb_grab_key(globalconf
.connection
, true, s
->root
,
102 k
->mod
| globalconf
.numlockmask
, kc
, XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
);
103 xcb_grab_key(globalconf
.connection
, true, s
->root
,
104 k
->mod
| globalconf
.numlockmask
| XCB_MOD_MASK_LOCK
, kc
, XCB_GRAB_MODE_ASYNC
,
105 XCB_GRAB_MODE_ASYNC
);
107 } while(phys_screen
< nscreen
);
110 /** Ungrab key on the root windows.
111 * \param k The keybinding.
114 window_root_ungrabkey(keybinding_t
*k
)
117 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
122 || (k
->keysym
&& (kc
= xcb_key_symbols_get_keycode(globalconf
.keysyms
, k
->keysym
))))
125 s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
126 xcb_ungrab_key(globalconf
.connection
, kc
, s
->root
,
128 xcb_ungrab_key(globalconf
.connection
, kc
, s
->root
,
129 k
->mod
| XCB_MOD_MASK_LOCK
);
130 xcb_ungrab_key(globalconf
.connection
, kc
, s
->root
,
131 k
->mod
| globalconf
.numlockmask
);
132 xcb_ungrab_key(globalconf
.connection
, kc
, s
->root
,
133 k
->mod
| globalconf
.numlockmask
| XCB_MOD_MASK_LOCK
);
135 } while(phys_screen
< nscreen
);
139 keybinding_register_root(keybinding_t
*k
)
141 keybinding_array_t
*arr
= k
->keysym
? &keys_g
.by_sym
: &keys_g
.by_code
;
142 int l
= 0, r
= arr
->len
;
148 switch (keybinding_cmp(k
, arr
->tab
[i
])) {
149 case -1: /* k < arr->tab[i] */
152 case 0: /* k == arr->tab[i] */
153 keybinding_unref(&arr
->tab
[i
]);
156 case 1: /* k > arr->tab[i] */
162 keybinding_array_splice(arr
, r
, 0, &k
, 1);
163 window_root_grabkey(k
);
167 keybinding_unregister_root(keybinding_t
**k
)
169 keybinding_array_t
*arr
= (*k
)->keysym
? &keys_g
.by_sym
: &keys_g
.by_code
;
170 int l
= 0, r
= arr
->len
;
174 switch (keybinding_cmp(*k
, arr
->tab
[i
])) {
175 case -1: /* k < arr->tab[i] */
178 case 0: /* k == arr->tab[i] */
179 keybinding_array_take(arr
, i
);
180 window_root_ungrabkey(*k
);
183 case 1: /* k > arr->tab[i] */
190 /** Return the keysym from keycode.
191 * \param detail The keycode received.
192 * \param state The modifier state.
196 key_getkeysym(xcb_keycode_t detail
, uint16_t state
)
200 /* 'col' (third parameter) is used to get the proper KeySym
201 * according to modifier (XCB doesn't provide an equivalent to
204 * If Mod5 is ON we look into second group.
206 if(state
& XCB_MOD_MASK_5
)
208 k0
= xcb_key_symbols_get_keysym(globalconf
.keysyms
, detail
, 2);
209 k1
= xcb_key_symbols_get_keysym(globalconf
.keysyms
, detail
, 3);
213 k0
= xcb_key_symbols_get_keysym(globalconf
.keysyms
, detail
, 0);
214 k1
= xcb_key_symbols_get_keysym(globalconf
.keysyms
, detail
, 1);
217 /* The numlock modifier is on and the second KeySym is a keypad
219 if((state
& globalconf
.numlockmask
) && xcb_is_keypad_key(k1
))
221 /* The Shift modifier is on, or if the Lock modifier is on and
222 * is interpreted as ShiftLock, use the first KeySym */
223 if((state
& XCB_MOD_MASK_SHIFT
) ||
224 (state
& XCB_MOD_MASK_LOCK
&& (state
& globalconf
.shiftlockmask
)))
230 /* The Shift and Lock modifers are both off, use the first
232 else if(!(state
& XCB_MOD_MASK_SHIFT
) && !(state
& XCB_MOD_MASK_LOCK
))
235 /* The Shift modifier is off 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 first Keysym is used but if that KeySym is lowercase
240 * alphabetic, then the corresponding uppercase KeySym is used
244 /* The Shift modifier is on, and the Lock modifier is on and is
245 * interpreted as CapsLock */
246 else if((state
& XCB_MOD_MASK_SHIFT
) &&
247 (state
& XCB_MOD_MASK_LOCK
&& (state
& globalconf
.capslockmask
)))
248 /* The second Keysym is used but if that KeySym is lowercase
249 * alphabetic, then the corresponding uppercase KeySym is used
253 /* The Shift modifer is on, or the Lock modifier is on and is
254 * interpreted as ShiftLock, or both */
255 else if((state
& XCB_MOD_MASK_SHIFT
) ||
256 (state
& XCB_MOD_MASK_LOCK
&& (state
& globalconf
.shiftlockmask
)))
259 return XCB_NO_SYMBOL
;
264 keybinding_find(const xcb_key_press_event_t
*ev
)
266 const keybinding_array_t
*arr
= &keys_g
.by_sym
;
267 int l
, r
, mod
= XUTIL_MASK_CLEAN(ev
->state
);
270 /* get keysym ignoring shift and mod5 */
271 keysym
= key_getkeysym(ev
->detail
, ev
->state
& ~(XCB_MOD_MASK_SHIFT
| XCB_MOD_MASK_5
| XCB_MOD_MASK_LOCK
));
279 switch (keybinding_ev_cmp(keysym
, ev
->detail
, mod
, arr
->tab
[i
]))
281 case -1: /* ev < arr->tab[i] */
284 case 0: /* ev == arr->tab[i] */
286 case 1: /* ev > arr->tab[i] */
291 if (arr
!= &keys_g
.by_code
)
293 arr
= &keys_g
.by_code
;
300 luaA_keystore(keybinding_t
*key
, const char *str
, ssize_t len
)
306 key
->keysym
= XStringToKeysym(str
);
312 warn("there's no keysym named \"%s\"", str
);
316 key
->keycode
= atoi(str
+ 1);
320 /** Define a global key binding. This key binding will always be available.
321 * \param L The Lua VM state.
324 * \lparam A table with modifier keys.
325 * \lparam A key name.
326 * \lparam A function to execute.
327 * \lreturn The keybinding.
330 luaA_keybinding_new(lua_State
*L
)
336 /* arg 2 is key mod table */
337 luaA_checktable(L
, 2);
339 key
= luaL_checklstring(L
, 3, &len
);
340 /* arg 4 is cmd to run */
341 luaA_checkfunction(L
, 4);
343 /* get the last arg as function */
344 k
= p_new(keybinding_t
, 1);
345 luaA_keystore(k
, key
, len
);
346 luaA_registerfct(L
, 4, &k
->fct
);
348 len
= lua_objlen(L
, 2);
349 for(i
= 1; i
<= len
; i
++)
352 lua_rawgeti(L
, 2, i
);
353 key
= luaL_checklstring(L
, -1, &blen
);
354 k
->mod
|= xutil_key_mask_fromstr(key
, blen
);
357 return luaA_keybinding_userdata_new(L
, k
);
360 /** Add a global key binding. This key binding will always be available.
361 * \param L The Lua VM state.
364 * \lvalue A keybinding.
367 luaA_keybinding_add(lua_State
*L
)
369 keybinding_t
**k
= luaA_checkudata(L
, 1, "keybinding");
370 keybinding_register_root(*k
);
374 /** Remove a global key binding.
375 * \param L The Lua VM state.
378 * \lvalue A keybinding.
381 luaA_keybinding_remove(lua_State
*L
)
383 keybinding_t
**k
= luaA_checkudata(L
, 1, "keybinding");
384 keybinding_unregister_root(k
);
388 /** Convert a keybinding to a printable string.
392 luaA_keybinding_tostring(lua_State
*L
)
394 keybinding_t
**p
= luaA_checkudata(L
, 1, "keybinding");
395 lua_pushfstring(L
, "[keybinding udata(%p)]", *p
);
399 const struct luaL_reg awesome_keybinding_methods
[] =
401 { "__call", luaA_keybinding_new
},
404 const struct luaL_reg awesome_keybinding_meta
[] =
406 { "add", luaA_keybinding_add
},
407 { "remove", luaA_keybinding_remove
},
408 { "__tostring", luaA_keybinding_tostring
},
409 { "__gc", luaA_keybinding_gc
},