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() */
28 extern awesome_t globalconf
;
31 keybinding_delete(keybinding_t
**kbp
)
33 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*kbp
)->fct
);
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
)
43 keybinding_ev_cmp(xcb_keysym_t keysym
, xcb_keycode_t keycode
,
44 unsigned long mod
, const keybinding_t
*k
)
47 if (k
->keysym
!= keysym
)
48 return k
->keysym
> keysym
? 1 : -1;
51 if (k
->keycode
!= keycode
)
52 return k
->keycode
> keycode
? 1 : -1;
54 return k
->mod
== mod
? 0 : (k
->mod
> mod
? 1 : -1);
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.
74 window_root_grabkey(keybinding_t
*k
)
77 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
,
97 } while(phys_screen
< nscreen
);
100 /** Ungrab key on the root windows.
101 * \param k The keybinding.
104 window_root_ungrabkey(keybinding_t
*k
)
107 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
,
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
);
125 } while(phys_screen
< nscreen
);
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
;
138 switch (keybinding_cmp(k
, arr
->tab
[i
])) {
139 case -1: /* k < arr->tab[i] */
142 case 0: /* k == arr->tab[i] */
143 keybinding_unref(&arr
->tab
[i
]);
146 case 1: /* k > arr->tab[i] */
152 keybinding_array_splice(arr
, r
, 0, &k
, 1);
153 window_root_grabkey(k
);
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
;
164 switch (keybinding_cmp(*k
, arr
->tab
[i
])) {
165 case -1: /* k < arr->tab[i] */
168 case 0: /* k == arr->tab[i] */
169 keybinding_array_take(arr
, i
);
170 window_root_ungrabkey(*k
);
173 case 1: /* k > arr->tab[i] */
180 /** Return the keysym from keycode.
181 * \param detail The keycode received.
182 * \param state The modifier state.
186 key_getkeysym(xcb_keycode_t detail
, uint16_t state
)
190 /* 'col' (third parameter) is used to get the proper KeySym
191 * according to modifier (XCB doesn't provide an equivalent to
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);
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
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
)))
220 /* The Shift and Lock modifers are both off, use the first
222 else if(!(state
& XCB_MOD_MASK_SHIFT
) && !(state
& XCB_MOD_MASK_LOCK
))
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
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
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
)))
249 return XCB_NO_SYMBOL
;
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
);
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
));
269 switch (keybinding_ev_cmp(keysym
, ev
->detail
, mod
, arr
->tab
[i
]))
271 case -1: /* ev < arr->tab[i] */
274 case 0: /* ev == arr->tab[i] */
276 case 1: /* ev > arr->tab[i] */
281 if (arr
!= &globalconf
.keys
.by_code
)
283 arr
= &globalconf
.keys
.by_code
;
290 luaA_keystore(keybinding_t
*key
, const char *str
, ssize_t len
)
296 key
->keysym
= XStringToKeysym(str
);
302 warn("there's no keysym named \"%s\"", str
);
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.
314 * \lparam A table with modifier keys.
315 * \lparam A key name.
316 * \lparam A function to execute.
317 * \lreturn The keybinding.
320 luaA_keybinding_new(lua_State
*L
)
326 /* arg 2 is key mod table */
327 luaA_checktable(L
, 2);
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
++)
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.
354 * \lvalue A keybinding.
357 luaA_keybinding_add(lua_State
*L
)
359 keybinding_t
**k
= luaA_checkudata(L
, 1, "keybinding");
360 keybinding_register_root(*k
);
364 /** Remove a global key binding.
365 * \param L The Lua VM state.
368 * \lvalue A keybinding.
371 luaA_keybinding_remove(lua_State
*L
)
373 keybinding_t
**k
= luaA_checkudata(L
, 1, "keybinding");
374 keybinding_unregister_root(k
);
378 const struct luaL_reg awesome_keybinding_methods
[] =
380 { "__call", luaA_keybinding_new
},
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
},