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() */
28 extern awesome_t globalconf
;
31 key_delete(keyb_t
**kbp
)
33 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*kbp
)->press
);
34 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*kbp
)->release
);
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
)
44 key_ev_cmp(xcb_keysym_t keysym
, xcb_keycode_t keycode
,
45 unsigned long mod
, const keyb_t
*k
)
48 if (k
->keysym
!= keysym
)
49 return k
->keysym
> keysym
? 1 : -1;
52 if (k
->keycode
!= keycode
)
53 return k
->keycode
> keycode
? 1 : -1;
55 return k
->mod
== mod
? 0 : (k
->mod
> mod
? 1 : -1);
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.
75 window_root_grabkey(keyb_t
*k
)
78 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
,
98 } while(phys_screen
< nscreen
);
101 /** Ungrab key on the root windows.
105 window_root_ungrabkey(keyb_t
*k
)
108 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
,
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
);
126 } while(phys_screen
< nscreen
);
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
;
139 switch (key_cmp(k
, arr
->tab
[i
])) {
140 case -1: /* k < arr->tab[i] */
143 case 0: /* k == arr->tab[i] */
144 key_unref(&arr
->tab
[i
]);
147 case 1: /* k > arr->tab[i] */
153 key_array_splice(arr
, r
, 0, &k
, 1);
154 window_root_grabkey(k
);
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
;
165 switch (key_cmp(*k
, arr
->tab
[i
])) {
166 case -1: /* k < arr->tab[i] */
169 case 0: /* k == arr->tab[i] */
170 key_array_take(arr
, i
);
171 window_root_ungrabkey(*k
);
174 case 1: /* k > arr->tab[i] */
181 /** Return the keysym from keycode.
182 * \param detail The keycode received.
183 * \param state The modifier state.
187 key_getkeysym(xcb_keycode_t detail
, uint16_t state
)
191 /* 'col' (third parameter) is used to get the proper KeySym
192 * according to modifier (XCB doesn't provide an equivalent to
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);
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
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
)))
221 /* The Shift and Lock modifers are both off, use the first
223 else if(!(state
& XCB_MOD_MASK_SHIFT
) && !(state
& XCB_MOD_MASK_LOCK
))
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
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
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
)))
250 return XCB_NO_SYMBOL
;
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
);
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
));
270 switch (key_ev_cmp(keysym
, ev
->detail
, mod
, arr
->tab
[i
]))
272 case -1: /* ev < arr->tab[i] */
275 case 0: /* ev == arr->tab[i] */
277 case 1: /* ev > arr->tab[i] */
282 if (arr
!= &globalconf
.keys
.by_code
)
284 arr
= &globalconf
.keys
.by_code
;
291 luaA_keystore(keyb_t
*key
, const char *str
, ssize_t len
)
297 key
->keysym
= XStringToKeysym(str
);
303 warn("there's no keysym named \"%s\"", str
);
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.
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.
322 luaA_key_new(lua_State
*L
)
327 luaA_ref press
= LUA_REFNIL
, release
= LUA_REFNIL
;
329 /* arg 2 is key mod table */
330 luaA_checktable(L
, 2);
332 key
= luaL_checklstring(L
, 3, &len
);
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
);
345 k
->release
= release
;
347 len
= lua_objlen(L
, 2);
348 for(i
= 1; i
<= len
; i
++)
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.
366 luaA_key_add(lua_State
*L
)
368 keyb_t
**k
= luaA_checkudata(L
, 1, "key");
369 key_register_root(*k
);
373 /** Remove a global key binding.
374 * \param L The Lua VM state.
380 luaA_key_remove(lua_State
*L
)
382 keyb_t
**k
= luaA_checkudata(L
, 1, "key");
383 key_unregister_root(k
);
387 const struct luaL_reg awesome_key_methods
[] =
389 { "__call", luaA_key_new
},
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
},