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 "keybinding.h"
29 ARRAY_TYPE(keybinding_t
*, keybinding
)
31 extern awesome_t globalconf
;
34 keybinding_array_t by_code
;
35 keybinding_array_t by_sym
;
39 keybinding_delete(keybinding_t
**kbp
)
41 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*kbp
)->fct
);
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
)
51 keybinding_ev_cmp(xcb_keysym_t keysym
, xcb_keycode_t keycode
,
52 unsigned long mod
, const keybinding_t
*k
)
55 if (k
->keysym
!= keysym
)
56 return k
->keysym
> keysym
? 1 : -1;
59 if (k
->keycode
!= keycode
)
60 return k
->keycode
> keycode
? 1 : -1;
62 return k
->mod
== mod
? 0 : (k
->mod
> mod
? 1 : -1);
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.
82 window_root_grabkey(keybinding_t
*k
)
85 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
);
105 } while(phys_screen
< nscreen
);
108 /** Ungrab key on the root windows.
109 * \param k The keybinding.
112 window_root_ungrabkey(keybinding_t
*k
)
115 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
,
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
);
133 } while(phys_screen
< nscreen
);
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
;
146 switch (keybinding_cmp(k
, arr
->tab
[i
])) {
147 case -1: /* k < arr->tab[i] */
150 case 0: /* k == arr->tab[i] */
151 keybinding_unref(&arr
->tab
[i
]);
154 case 1: /* k > arr->tab[i] */
160 keybinding_array_splice(arr
, r
, 0, &k
, 1);
161 window_root_grabkey(k
);
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
;
172 switch (keybinding_cmp(*k
, arr
->tab
[i
])) {
173 case -1: /* k < arr->tab[i] */
176 case 0: /* k == arr->tab[i] */
177 keybinding_array_take(arr
, i
);
178 window_root_ungrabkey(*k
);
181 case 1: /* k > arr->tab[i] */
188 /** Return the keysym from keycode.
189 * \param detail The keycode received.
190 * \param state The modifier state.
194 key_getkeysym(xcb_keycode_t detail
, uint16_t state
)
198 /* 'col' (third parameter) is used to get the proper KeySym
199 * according to modifier (XCB doesn't provide an equivalent to
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);
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
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
)))
228 /* The Shift and Lock modifers are both off, use the first
230 else if(!(state
& XCB_MOD_MASK_SHIFT
) && !(state
& XCB_MOD_MASK_LOCK
))
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
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
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
)))
257 return XCB_NO_SYMBOL
;
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
);
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
));
277 switch (keybinding_ev_cmp(keysym
, ev
->detail
, mod
, arr
->tab
[i
]))
279 case -1: /* ev < arr->tab[i] */
282 case 0: /* ev == arr->tab[i] */
284 case 1: /* ev > arr->tab[i] */
289 if (arr
!= &keys_g
.by_code
)
291 arr
= &keys_g
.by_code
;
298 luaA_keystore(keybinding_t
*key
, const char *str
, ssize_t len
)
304 key
->keysym
= XStringToKeysym(str
);
310 warn("there's no keysym named \"%s\"", str
);
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.
322 * \lparam A table with modifier keys.
323 * \lparam A key name.
324 * \lparam A function to execute.
325 * \lreturn The keybinding.
328 luaA_keybinding_new(lua_State
*L
)
334 /* arg 2 is key mod table */
335 luaA_checktable(L
, 2);
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
++)
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.
362 * \lvalue A keybinding.
365 luaA_keybinding_add(lua_State
*L
)
367 keybinding_t
**k
= luaA_checkudata(L
, 1, "keybinding");
368 keybinding_register_root(*k
);
372 /** Remove a global key binding.
373 * \param L The Lua VM state.
376 * \lvalue A keybinding.
379 luaA_keybinding_remove(lua_State
*L
)
381 keybinding_t
**k
= luaA_checkudata(L
, 1, "keybinding");
382 keybinding_unregister_root(k
);
386 const struct luaL_reg awesome_keybinding_methods
[] =
388 { "__call", luaA_keybinding_new
},
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
},