2008-12-18 11:33:19 +01:00
|
|
|
/*
|
|
|
|
* key.c - Key bindings configuration management
|
|
|
|
*
|
|
|
|
* Copyright © 2008 Julien Danjou <julien@danjou.info>
|
|
|
|
* Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-04-25 16:00:08 +02:00
|
|
|
/* XStringToKeysym() and XKeysymToString()*/
|
2008-12-18 11:33:19 +01:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
|
|
|
#include "structs.h"
|
2009-04-17 16:52:25 +02:00
|
|
|
#include "common/xutil.h"
|
2009-04-25 16:00:08 +02:00
|
|
|
#include "common/tokenize.h"
|
2008-12-18 11:33:19 +01:00
|
|
|
|
2009-04-11 12:40:56 +02:00
|
|
|
LUA_OBJECT_FUNCS(keyb_t, key, "key")
|
|
|
|
|
2008-12-18 11:33:19 +01:00
|
|
|
static void
|
2009-04-11 12:40:56 +02:00
|
|
|
key_unref_simplified(keyb_t **b)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2009-04-11 12:40:56 +02:00
|
|
|
key_unref(globalconf.L, *b);
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
|
|
|
|
2009-04-11 12:40:56 +02:00
|
|
|
ARRAY_FUNCS(keyb_t *, key, key_unref_simplified)
|
|
|
|
DO_LUA_TOSTRING(keyb_t, key, "key")
|
|
|
|
|
|
|
|
/** Garbage collect a key.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return 0.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_key_gc(lua_State *L)
|
|
|
|
{
|
|
|
|
keyb_t *kbp = luaL_checkudata(L, 1, "key");
|
|
|
|
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, kbp->press);
|
|
|
|
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, kbp->release);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-18 11:33:19 +01:00
|
|
|
|
2009-01-05 16:59:20 +01:00
|
|
|
static void
|
|
|
|
keybindings_init(keybindings_t *kb)
|
|
|
|
{
|
|
|
|
key_array_init(&kb->by_code);
|
|
|
|
key_array_init(&kb->by_sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keybindings_wipe(keybindings_t *kb)
|
|
|
|
{
|
|
|
|
key_array_wipe(&kb->by_code);
|
|
|
|
key_array_wipe(&kb->by_sym);
|
|
|
|
}
|
|
|
|
|
2008-12-18 11:33:19 +01:00
|
|
|
static int
|
|
|
|
key_ev_cmp(xcb_keysym_t keysym, xcb_keycode_t keycode,
|
|
|
|
unsigned long mod, const keyb_t *k)
|
|
|
|
{
|
2009-03-29 20:26:39 +02:00
|
|
|
if(k->keysym) {
|
|
|
|
if(k->keysym != keysym)
|
2008-12-18 11:33:19 +01:00
|
|
|
return k->keysym > keysym ? 1 : -1;
|
|
|
|
}
|
2009-03-29 20:26:39 +02:00
|
|
|
if(k->keycode) {
|
|
|
|
if(k->keycode != keycode)
|
2008-12-18 11:33:19 +01:00
|
|
|
return k->keycode > keycode ? 1 : -1;
|
|
|
|
}
|
2009-04-15 13:59:22 +02:00
|
|
|
return ((k->mod == mod || k->mod == XCB_BUTTON_MASK_ANY) ?
|
|
|
|
0 : (k->mod > mod ? 1 : -1));
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
key_cmp(const keyb_t *k1, const keyb_t *k2)
|
|
|
|
{
|
|
|
|
assert ((k1->keysym && k2->keysym) || (k1->keycode && k2->keycode));
|
|
|
|
assert ((!k1->keysym && !k2->keysym) || (!k1->keycode && !k2->keycode));
|
|
|
|
|
2009-03-29 20:26:39 +02:00
|
|
|
if(k1->keysym != k2->keysym)
|
2008-12-18 11:33:19 +01:00
|
|
|
return k2->keysym > k1->keysym ? 1 : -1;
|
2009-03-29 20:26:39 +02:00
|
|
|
if(k1->keycode != k2->keycode)
|
2008-12-18 11:33:19 +01:00
|
|
|
return k2->keycode > k1->keycode ? 1 : -1;
|
|
|
|
return k1->mod == k2->mod ? 0 : (k2->mod > k1->mod ? 1 : -1);
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:59:20 +01:00
|
|
|
/** Grab key on a window.
|
|
|
|
* \param win The window.
|
2008-12-18 11:33:19 +01:00
|
|
|
* \param k The key.
|
|
|
|
*/
|
2009-01-05 16:59:20 +01:00
|
|
|
static void
|
|
|
|
window_grabkey(xcb_window_t win, keyb_t *k)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2009-04-11 18:02:31 +02:00
|
|
|
if(k->keycode)
|
2009-04-14 17:53:22 +02:00
|
|
|
xcb_grab_key(globalconf.connection, true, win,
|
|
|
|
k->mod, k->keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
|
2009-04-11 18:02:31 +02:00
|
|
|
else if(k->keysym)
|
2009-01-05 16:59:20 +01:00
|
|
|
{
|
2009-04-11 18:02:31 +02:00
|
|
|
xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym);
|
|
|
|
if(keycodes)
|
|
|
|
{
|
|
|
|
for(xcb_keycode_t *kc = keycodes; *kc; kc++)
|
2009-04-14 17:53:22 +02:00
|
|
|
xcb_grab_key(globalconf.connection, true, win,
|
|
|
|
k->mod, *kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
|
2009-04-11 18:02:31 +02:00
|
|
|
p_delete(&keycodes);
|
|
|
|
}
|
2009-01-05 16:59:20 +01:00
|
|
|
}
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:59:20 +01:00
|
|
|
void
|
|
|
|
window_grabkeys(xcb_window_t win, keybindings_t *keys)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2009-01-05 16:59:20 +01:00
|
|
|
for(int i = 0; i < keys->by_code.len; i++)
|
|
|
|
window_grabkey(win, keys->by_code.tab[i]);
|
2008-12-18 11:33:19 +01:00
|
|
|
|
2009-01-05 16:59:20 +01:00
|
|
|
for(int i = 0; i < keys->by_sym.len; i++)
|
|
|
|
window_grabkey(win, keys->by_sym.tab[i]);
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
|
|
|
|
2009-04-11 12:40:56 +02:00
|
|
|
/** Register a key which on top of the stack.
|
|
|
|
* \param keys The keybinding array where to put the key.
|
|
|
|
*/
|
2008-12-18 11:33:19 +01:00
|
|
|
static void
|
2009-04-11 12:40:56 +02:00
|
|
|
key_register(keybindings_t *keys)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2009-04-11 12:40:56 +02:00
|
|
|
keyb_t *k = key_ref(globalconf.L);
|
2009-01-05 16:59:20 +01:00
|
|
|
key_array_t *arr = k->keysym ? &keys->by_sym : &keys->by_code;
|
2008-12-18 11:33:19 +01:00
|
|
|
int l = 0, r = arr->len;
|
|
|
|
|
2009-03-29 20:26:39 +02:00
|
|
|
while(l < r) {
|
2008-12-18 11:33:19 +01:00
|
|
|
int i = (r + l) / 2;
|
2009-03-29 20:26:39 +02:00
|
|
|
switch(key_cmp(k, arr->tab[i]))
|
|
|
|
{
|
2008-12-18 11:33:19 +01:00
|
|
|
case -1: /* k < arr->tab[i] */
|
|
|
|
r = i;
|
|
|
|
break;
|
|
|
|
case 0: /* k == arr->tab[i] */
|
2009-04-11 12:40:56 +02:00
|
|
|
key_unref(globalconf.L, arr->tab[i]);
|
2008-12-18 11:33:19 +01:00
|
|
|
arr->tab[i] = k;
|
|
|
|
return;
|
|
|
|
case 1: /* k > arr->tab[i] */
|
|
|
|
l = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
key_array_splice(arr, r, 0, &k, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the keysym from keycode.
|
|
|
|
* \param detail The keycode received.
|
|
|
|
* \param state The modifier state.
|
|
|
|
* \return A keysym.
|
|
|
|
*/
|
|
|
|
xcb_keysym_t
|
|
|
|
key_getkeysym(xcb_keycode_t detail, uint16_t state)
|
|
|
|
{
|
|
|
|
xcb_keysym_t k0, k1;
|
|
|
|
|
|
|
|
/* 'col' (third parameter) is used to get the proper KeySym
|
|
|
|
* according to modifier (XCB doesn't provide an equivalent to
|
|
|
|
* XLookupString()).
|
|
|
|
*
|
|
|
|
* If Mod5 is ON we look into second group.
|
|
|
|
*/
|
|
|
|
if(state & XCB_MOD_MASK_5)
|
|
|
|
{
|
|
|
|
k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
|
|
|
|
k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
|
|
|
|
k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
|
|
|
|
}
|
|
|
|
|
2009-01-04 17:38:40 +01:00
|
|
|
/* If the second column does not exists use the first one. */
|
|
|
|
if(k1 == XCB_NONE)
|
|
|
|
k1 = k0;
|
|
|
|
|
2008-12-18 11:33:19 +01:00
|
|
|
/* The numlock modifier is on and the second KeySym is a keypad
|
|
|
|
* KeySym */
|
2009-04-11 18:02:31 +02:00
|
|
|
if((state & XCB_MOD_MASK_2) && xcb_is_keypad_key(k1))
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
|
|
|
/* The Shift modifier is on, or if the Lock modifier is on and
|
|
|
|
* is interpreted as ShiftLock, use the first KeySym */
|
2009-04-11 18:02:31 +02:00
|
|
|
if((state & XCB_MOD_MASK_SHIFT) || (state & XCB_MOD_MASK_LOCK))
|
2008-12-18 11:33:19 +01:00
|
|
|
return k0;
|
|
|
|
else
|
|
|
|
return k1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The Shift and Lock modifers are both off, use the first
|
|
|
|
* KeySym */
|
|
|
|
else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
|
|
|
|
return k0;
|
|
|
|
|
|
|
|
/* The Shift modifier is off and the Lock modifier is on and is
|
|
|
|
* interpreted as CapsLock */
|
2009-04-11 18:02:31 +02:00
|
|
|
else if(!(state & XCB_MOD_MASK_SHIFT) && (state & XCB_MOD_MASK_LOCK))
|
2008-12-18 11:33:19 +01:00
|
|
|
/* The first Keysym is used but if that KeySym is lowercase
|
|
|
|
* alphabetic, then the corresponding uppercase KeySym is used
|
|
|
|
* instead */
|
|
|
|
return k1;
|
|
|
|
|
|
|
|
/* The Shift modifier is on, and the Lock modifier is on and is
|
|
|
|
* interpreted as CapsLock */
|
2009-04-11 18:02:31 +02:00
|
|
|
else if((state & XCB_MOD_MASK_SHIFT) && (state & XCB_MOD_MASK_LOCK))
|
2008-12-18 11:33:19 +01:00
|
|
|
/* The second Keysym is used but if that KeySym is lowercase
|
|
|
|
* alphabetic, then the corresponding uppercase KeySym is used
|
|
|
|
* instead */
|
|
|
|
return k1;
|
|
|
|
|
|
|
|
/* The Shift modifer is on, or the Lock modifier is on and is
|
|
|
|
* interpreted as ShiftLock, or both */
|
2009-04-11 18:02:31 +02:00
|
|
|
else if((state & XCB_MOD_MASK_SHIFT) || (state & XCB_MOD_MASK_LOCK))
|
2008-12-18 11:33:19 +01:00
|
|
|
return k1;
|
|
|
|
|
|
|
|
return XCB_NO_SYMBOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
keyb_t *
|
2009-01-05 16:59:20 +01:00
|
|
|
key_find(keybindings_t *keys, const xcb_key_press_event_t *ev)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2009-01-05 16:59:20 +01:00
|
|
|
const key_array_t *arr = &keys->by_sym;
|
2009-04-14 17:53:22 +02:00
|
|
|
int l, r;
|
2008-12-18 11:33:19 +01:00
|
|
|
xcb_keysym_t keysym;
|
|
|
|
|
|
|
|
/* get keysym ignoring shift and mod5 */
|
|
|
|
keysym = key_getkeysym(ev->detail, ev->state & ~(XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_5 | XCB_MOD_MASK_LOCK));
|
|
|
|
|
|
|
|
again:
|
|
|
|
l = 0;
|
|
|
|
r = arr->len;
|
2009-03-29 20:26:39 +02:00
|
|
|
while(l < r)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
|
|
|
int i = (r + l) / 2;
|
2009-04-14 17:53:22 +02:00
|
|
|
switch(key_ev_cmp(keysym, ev->detail, ev->state, arr->tab[i]))
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
|
|
|
case -1: /* ev < arr->tab[i] */
|
|
|
|
r = i;
|
|
|
|
break;
|
|
|
|
case 0: /* ev == arr->tab[i] */
|
|
|
|
return arr->tab[i];
|
|
|
|
case 1: /* ev > arr->tab[i] */
|
|
|
|
l = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-05 16:59:20 +01:00
|
|
|
if(arr != &keys->by_code)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2009-01-05 16:59:20 +01:00
|
|
|
arr = &keys->by_code;
|
2008-12-18 11:33:19 +01:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
luaA_keystore(keyb_t *key, const char *str, ssize_t len)
|
|
|
|
{
|
|
|
|
if(len)
|
|
|
|
{
|
|
|
|
if(*str != '#')
|
|
|
|
{
|
|
|
|
key->keysym = XStringToKeysym(str);
|
2009-03-29 20:26:39 +02:00
|
|
|
if(!key->keysym)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2009-03-29 20:26:39 +02:00
|
|
|
if(len == 1)
|
2008-12-18 11:33:19 +01:00
|
|
|
key->keysym = *str;
|
|
|
|
else
|
|
|
|
warn("there's no keysym named \"%s\"", str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
key->keycode = atoi(str + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Define a global key binding. This key binding will always be available.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
*
|
|
|
|
* \luastack
|
2009-04-15 13:59:22 +02:00
|
|
|
* \lparam A table with modifier keys: can be Control or Ctrl, Shift, Lock,
|
|
|
|
* Mod1, Mod2, Mod3, Mod4, Mod5 or Any.
|
2008-12-18 11:33:19 +01:00
|
|
|
* \lparam A key name.
|
|
|
|
* \lparam A function to execute on key press.
|
|
|
|
* \lparam A function to execute on key release.
|
|
|
|
* \lreturn The key.
|
|
|
|
*/
|
2009-04-11 11:59:10 +02:00
|
|
|
static int
|
2008-12-18 11:33:19 +01:00
|
|
|
luaA_key_new(lua_State *L)
|
|
|
|
{
|
2009-04-27 14:12:24 +02:00
|
|
|
size_t len;
|
2008-12-18 11:33:19 +01:00
|
|
|
keyb_t *k;
|
|
|
|
const char *key;
|
|
|
|
luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
|
|
|
|
|
|
|
|
/* arg 2 is key mod table */
|
|
|
|
luaA_checktable(L, 2);
|
|
|
|
/* arg 3 is key */
|
|
|
|
key = luaL_checklstring(L, 3, &len);
|
|
|
|
|
|
|
|
if(!lua_isnil(L, 4))
|
|
|
|
luaA_registerfct(L, 4, &press);
|
|
|
|
|
|
|
|
if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
|
|
|
|
luaA_registerfct(L, 5, &release);
|
|
|
|
|
2009-04-11 12:40:56 +02:00
|
|
|
k = key_new(L);
|
2008-12-18 11:33:19 +01:00
|
|
|
luaA_keystore(k, key, len);
|
|
|
|
|
|
|
|
k->press = press;
|
|
|
|
k->release = release;
|
|
|
|
|
2009-04-27 14:12:24 +02:00
|
|
|
luaA_setmodifiers(L, 2, &k->mod);
|
2008-12-18 11:33:19 +01:00
|
|
|
|
2009-04-11 12:40:56 +02:00
|
|
|
return 1;
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:59:20 +01:00
|
|
|
/** Set a key array with a Lua table.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param idx The index of the Lua table.
|
|
|
|
* \param keys The array key to fill.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
luaA_key_array_set(lua_State *L, int idx, keybindings_t *keys)
|
|
|
|
{
|
|
|
|
luaA_checktable(L, idx);
|
|
|
|
|
|
|
|
keybindings_wipe(keys);
|
|
|
|
keybindings_init(keys);
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, idx))
|
2009-04-11 12:40:56 +02:00
|
|
|
key_register(keys);
|
2009-01-05 16:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Push an array of key as an Lua table onto the stack.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param keys The key array to push.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_key_array_get(lua_State *L, keybindings_t *keys)
|
|
|
|
{
|
2009-03-14 13:38:53 +01:00
|
|
|
lua_createtable(L, keys->by_code.len + keys->by_sym.len, 0);
|
2009-01-05 16:59:20 +01:00
|
|
|
for(int i = 0; i < keys->by_code.len; i++)
|
|
|
|
{
|
2009-04-11 12:40:56 +02:00
|
|
|
key_push(L, keys->by_code.tab[i]);
|
2009-01-05 16:59:20 +01:00
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
for(int i = 0; i < keys->by_sym.len; i++)
|
|
|
|
{
|
2009-04-11 12:40:56 +02:00
|
|
|
key_push(L, keys->by_sym.tab[i]);
|
2009-01-05 16:59:20 +01:00
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-26 19:41:53 +02:00
|
|
|
/** Push a modifier set to a Lua table.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param mod The modifier.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_pushmodifiers(lua_State *L, uint16_t modifiers)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
for(uint32_t maski = XCB_MOD_MASK_SHIFT; maski <= XCB_BUTTON_MASK_ANY; maski <<= 1)
|
|
|
|
if(maski & modifiers)
|
|
|
|
{
|
|
|
|
const char *mod;
|
|
|
|
size_t slen;
|
|
|
|
xutil_key_mask_tostr(maski, &mod, &slen);
|
|
|
|
lua_pushlstring(L, mod, slen);
|
|
|
|
lua_rawseti(L, -2, i++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-27 14:12:24 +02:00
|
|
|
/** Take a modifier table from the stack and set modifiers in mod.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param ud The index of the table.
|
|
|
|
* \param mod Where to set the modifiers.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
luaA_setmodifiers(lua_State *L, int ud, uint16_t *mod)
|
|
|
|
{
|
2009-04-27 14:13:54 +02:00
|
|
|
luaA_checktable(L, ud);
|
2009-04-27 14:12:24 +02:00
|
|
|
ssize_t len = lua_objlen(L, ud);
|
|
|
|
for(int i = 1; i <= len; i++)
|
|
|
|
{
|
|
|
|
lua_rawgeti(L, ud, i);
|
|
|
|
size_t blen;
|
|
|
|
const char *key = luaL_checklstring(L, -1, &blen);
|
|
|
|
*mod |= xutil_key_mask_fromstr(key, blen);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 16:00:08 +02:00
|
|
|
/** Key object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lfield key The key to press to triggers an event.
|
|
|
|
* \lfield modifiers The modifier key that should be pressed while the key is
|
|
|
|
* pressed. An array with all the modifiers. Valid modifiers are: Any, Mod1,
|
|
|
|
* Mod2, Mod3, Mod4, Mod5, Shift, Lock and Control.
|
|
|
|
* \lfield press The function which is called when the key combination is pressed.
|
|
|
|
* \lfield release The function which is called when the key combination is released.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_key_index(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
keyb_t *k = luaL_checkudata(L, 1, "key");
|
|
|
|
const char *attr = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
|
|
|
case A_TK_KEY:
|
|
|
|
if(k->keycode)
|
|
|
|
{
|
|
|
|
char buf[12];
|
|
|
|
int slen = snprintf(buf, sizeof(buf), "#%u", k->keycode);
|
|
|
|
lua_pushlstring(L, buf, slen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pushstring(L, XKeysymToString(k->keysym));
|
|
|
|
break;
|
|
|
|
case A_TK_MODIFIERS:
|
2009-04-26 19:41:53 +02:00
|
|
|
luaA_pushmodifiers(L, k->mod);
|
2009-04-25 16:00:08 +02:00
|
|
|
break;
|
|
|
|
case A_TK_PRESS:
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, k->press);
|
|
|
|
break;
|
|
|
|
case A_TK_RELEASE:
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, k->release);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-27 14:31:57 +02:00
|
|
|
/** Key object newindex.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_key_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
keyb_t *k = luaL_checkudata(L, 1, "key");
|
|
|
|
const char *attr = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
|
|
|
case A_TK_KEY:
|
|
|
|
{
|
|
|
|
size_t klen;
|
|
|
|
const char *key = luaL_checklstring(L, 3, &klen);
|
|
|
|
luaA_keystore(k, key, klen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case A_TK_MODIFIERS:
|
|
|
|
luaA_setmodifiers(L, 3, &k->mod);
|
|
|
|
break;
|
|
|
|
case A_TK_PRESS:
|
|
|
|
luaA_registerfct(L, 3, &k->press);
|
|
|
|
break;
|
|
|
|
case A_TK_RELEASE:
|
|
|
|
luaA_registerfct(L, 3, &k->release);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-18 11:33:19 +01:00
|
|
|
const struct luaL_reg awesome_key_methods[] =
|
|
|
|
{
|
|
|
|
{ "__call", luaA_key_new },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
const struct luaL_reg awesome_key_meta[] =
|
|
|
|
{
|
|
|
|
{ "__tostring", luaA_key_tostring },
|
2009-04-25 16:00:08 +02:00
|
|
|
{ "__index", luaA_key_index },
|
2009-04-27 14:31:57 +02:00
|
|
|
{ "__newindex", luaA_key_newindex },
|
2008-12-18 11:33:19 +01:00
|
|
|
{ "__gc", luaA_key_gc },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|