key: port to new object system

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-06-24 17:44:06 +02:00
parent 6cfaafbab3
commit 04e55739fe
4 changed files with 113 additions and 130 deletions

View File

@ -118,7 +118,7 @@ event_key_match(xcb_key_press_event_t *ev, keyb_t *k, void *data)
xcb_keysym_t keysym = *(xcb_keysym_t *) data; xcb_keysym_t keysym = *(xcb_keysym_t *) data;
return (((k->keycode && ev->detail == k->keycode) return (((k->keycode && ev->detail == k->keycode)
|| (k->keysym && keysym == k->keysym)) || (k->keysym && keysym == k->keysym))
&& (k->mod == XCB_BUTTON_MASK_ANY || k->mod == ev->state)); && (k->modifiers == XCB_BUTTON_MASK_ANY || k->modifiers == ev->state));
} }
DO_EVENT_HOOK_CALLBACK(button_t, button, button, XCB_BUTTON, button_array_t, event_button_match) DO_EVENT_HOOK_CALLBACK(button_t, button, button, XCB_BUTTON, button_array_t, event_button_match)

158
key.c
View File

@ -40,7 +40,7 @@ window_grabkey(xcb_window_t win, keyb_t *k)
{ {
if(k->keycode) if(k->keycode)
xcb_grab_key(globalconf.connection, true, win, xcb_grab_key(globalconf.connection, true, win,
k->mod, k->keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC); k->modifiers, k->keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
else if(k->keysym) else if(k->keysym)
{ {
xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym); xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym);
@ -48,7 +48,7 @@ window_grabkey(xcb_window_t win, keyb_t *k)
{ {
for(xcb_keycode_t *kc = keycodes; *kc; kc++) for(xcb_keycode_t *kc = keycodes; *kc; kc++)
xcb_grab_key(globalconf.connection, true, win, xcb_grab_key(globalconf.connection, true, win,
k->mod, *kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC); k->modifiers, *kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
p_delete(&keycodes); p_delete(&keycodes);
} }
} }
@ -966,8 +966,9 @@ key_getkeysym(xcb_keycode_t detail, uint16_t state)
} }
static void static void
luaA_keystore(keyb_t *key, const char *str, ssize_t len) luaA_keystore(lua_State *L, int ud, const char *str, ssize_t len)
{ {
keyb_t *key = luaL_checkudata(L, ud, "key");
if(len) if(len)
{ {
if(*str != '#') if(*str != '#')
@ -980,22 +981,20 @@ luaA_keystore(keyb_t *key, const char *str, ssize_t len)
else else
warn("there's no keysym named \"%s\"", str); warn("there's no keysym named \"%s\"", str);
} }
key->keycode = 0;
} }
else else
{
key->keycode = atoi(str + 1); key->keycode = atoi(str + 1);
key->keysym = 0;
}
luaA_object_emit_signal(L, ud, "property::key", 0);
} }
} }
/** Define a global key binding. This key binding will always be available. /** Create a new key object.
* \param L The Lua VM state. * \param L The Lua VM state.
* * \return The number of elements pushed on stack.
* \luastack
* \lparam A table with modifier keys: can be Control or Ctrl, Shift, Lock,
* Mod1, Mod2, Mod3, Mod4, Mod5 or Any.
* \lparam A key name.
* \lparam A function to execute on key press.
* \lparam A function to execute on key release.
* \lreturn The key.
*/ */
static int static int
luaA_key_new(lua_State *L) luaA_key_new(lua_State *L)
@ -1004,6 +1003,11 @@ luaA_key_new(lua_State *L)
keyb_t *k; keyb_t *k;
const char *key; const char *key;
/* compat code */
if(lua_istable(L, 2) && lua_isstring(L, 3))
{
luaA_deprecate(L, "new syntax");
/* be sure there's 5 arguments */ /* be sure there's 5 arguments */
lua_settop(L, 5); lua_settop(L, 5);
@ -1021,12 +1025,27 @@ luaA_key_new(lua_State *L)
k = key_new(L); k = key_new(L);
k->press = luaA_object_ref_item(L, -1, 4); k->press = luaA_object_ref_item(L, -1, 4);
k->release = luaA_object_ref_item(L, -1, 4); k->release = luaA_object_ref_item(L, -1, 4);
luaA_keystore(k, key, len); luaA_keystore(L, -1, key, len);
k->mod = luaA_tomodifiers(L, 2); k->modifiers = luaA_tomodifiers(L, 2);
if(!lua_isnil(L, 4))
{
lua_pushvalue(L, 4);
luaA_object_add_signal(L, -2, "press", -1);
}
if(!lua_isnil(L, 5))
{
lua_pushvalue(L, 5);
luaA_object_add_signal(L, -2, "release", -1);
}
return 1; return 1;
} }
return luaA_class_new(L, &key_class);
}
/** Set a key array with a Lua table. /** Set a key array with a Lua table.
* \param L The Lua VM state. * \param L The Lua VM state.
* \param oidx The index of the object to store items into. * \param oidx The index of the object to store items into.
@ -1116,32 +1135,19 @@ luaA_tomodifiers(lua_State *L, int ud)
return mod; return mod;
} }
/** 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 keysym Same as key, but return the name of the key symbol. It can
* be identical to key, but for characters like '.' it will return 'period'.
* \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 static int
luaA_key_index(lua_State *L) luaA_key_set_modifiers(lua_State *L, keyb_t *k)
{ {
size_t len; k->modifiers = luaA_tomodifiers(L, -1);
keyb_t *k = luaL_checkudata(L, 1, "key"); luaA_object_emit_signal(L, -3, "property::modifiers", 0);
const char *attr = luaL_checklstring(L, 2, &len); return 0;
}
if(luaA_usemetatable(L, 1, 2)) LUA_OBJECT_EXPORT_PROPERTY(key, keyb_t, modifiers, luaA_pushmodifiers)
return 1;
switch(a_tokenize(attr, len)) static int
luaA_key_get_key(lua_State *L, keyb_t *k)
{ {
case A_TK_KEY:
if(k->keycode) if(k->keycode)
{ {
char buf[12]; char buf[12];
@ -1156,77 +1162,55 @@ luaA_key_index(lua_State *L)
lua_pushstring(L, buf); lua_pushstring(L, buf);
} }
break;
case A_TK_KEYSYM:
if(k->keysym)
lua_pushstring(L, XKeysymToString(k->keysym));
else
return 0;
break;
case A_TK_MODIFIERS:
luaA_pushmodifiers(L, k->mod);
break;
case A_TK_PRESS:
return luaA_object_push_item(L, 1, k->press);
case A_TK_RELEASE:
return luaA_object_push_item(L, 1, k->release);
default:
break;
}
return 1; return 1;
} }
/** Key object newindex.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int static int
luaA_key_newindex(lua_State *L) luaA_key_get_keysym(lua_State *L, keyb_t *k)
{ {
size_t len; lua_pushstring(L, XKeysymToString(k->keysym));
keyb_t *k = luaL_checkudata(L, 1, "key"); return 1;
const char *attr = luaL_checklstring(L, 2, &len); }
switch(a_tokenize(attr, len)) static int
{ luaA_key_set_key(lua_State *L, keyb_t *k)
case A_TK_KEY:
{ {
size_t klen; size_t klen;
const char *key = luaL_checklstring(L, 3, &klen); const char *key = luaL_checklstring(L, -1, &klen);
luaA_keystore(k, key, klen); luaA_keystore(L, -3, key, klen);
}
break;
case A_TK_MODIFIERS:
k->mod = luaA_tomodifiers(L, 3);
break;
case A_TK_PRESS:
luaA_checkfunction(L, 3);
luaA_object_unref_item(L, 1, k->press);
k->press = luaA_object_ref_item(L, 1, 3);
break;
case A_TK_RELEASE:
luaA_checkfunction(L, 3);
luaA_object_unref_item(L, 1, k->release);
k->release = luaA_object_ref_item(L, 1, 3);
break;
default:
break;
}
return 0; return 0;
} }
const struct luaL_reg awesome_key_methods[] = void
key_class_setup(lua_State *L)
{
static const struct luaL_reg key_methods[] =
{ {
LUA_CLASS_METHODS(key) LUA_CLASS_METHODS(key)
{ "__call", luaA_key_new }, { "__call", luaA_key_new },
{ NULL, NULL } { NULL, NULL }
}; };
const struct luaL_reg awesome_key_meta[] =
static const struct luaL_reg key_meta[] =
{ {
LUA_OBJECT_META(key) LUA_OBJECT_META(key)
{ "__index", luaA_key_index }, LUA_CLASS_META
{ "__newindex", luaA_key_newindex },
{ "__gc", luaA_object_gc }, { "__gc", luaA_object_gc },
{ NULL, NULL }, { NULL, NULL },
}; };
luaA_class_setup(L, &key_class, "key", (lua_class_allocator_t) key_new,
key_methods, key_meta);
luaA_class_add_property(&key_class, A_TK_KEY,
(lua_class_propfunc_t) luaA_key_set_key,
(lua_class_propfunc_t) luaA_key_get_key,
(lua_class_propfunc_t) luaA_key_set_key);
luaA_class_add_property(&key_class, A_TK_KEY,
NULL,
(lua_class_propfunc_t) luaA_key_get_keysym,
NULL);
luaA_class_add_property(&key_class, A_TK_MODIFIERS,
(lua_class_propfunc_t) luaA_key_set_modifiers,
(lua_class_propfunc_t) luaA_key_get_modifiers,
(lua_class_propfunc_t) luaA_key_set_modifiers);
}

4
key.h
View File

@ -28,7 +28,7 @@ typedef struct keyb_t
{ {
LUA_OBJECT_HEADER LUA_OBJECT_HEADER
/** Key modifier */ /** Key modifier */
uint16_t mod; uint16_t modifiers;
/** Keysym */ /** Keysym */
xcb_keysym_t keysym; xcb_keysym_t keysym;
/** Keycode */ /** Keycode */
@ -43,6 +43,8 @@ lua_class_t key_class;
LUA_OBJECT_FUNCS(key_class, keyb_t, key, "key") LUA_OBJECT_FUNCS(key_class, keyb_t, key, "key")
DO_ARRAY(keyb_t *, key, DO_NOTHING) DO_ARRAY(keyb_t *, key, DO_NOTHING)
void key_class_setup(lua_State *);
bool key_press_lookup_string(xcb_keysym_t, char *, ssize_t); bool key_press_lookup_string(xcb_keysym_t, char *, ssize_t);
xcb_keysym_t key_getkeysym(xcb_keycode_t, uint16_t); xcb_keysym_t key_getkeysym(xcb_keycode_t, uint16_t);

5
luaa.c
View File

@ -67,8 +67,6 @@ extern const struct luaL_reg awesome_widget_methods[];
extern const struct luaL_reg awesome_widget_meta[]; extern const struct luaL_reg awesome_widget_meta[];
extern const struct luaL_reg awesome_wibox_methods[]; extern const struct luaL_reg awesome_wibox_methods[];
extern const struct luaL_reg awesome_wibox_meta[]; extern const struct luaL_reg awesome_wibox_meta[];
extern const struct luaL_reg awesome_key_methods[];
extern const struct luaL_reg awesome_key_meta[];
/** Quit awesome. /** Quit awesome.
* \param L The Lua VM state. * \param L The Lua VM state.
@ -766,8 +764,7 @@ luaA_init(xdgHandle* xdg)
awesome_client_methods, awesome_client_meta); awesome_client_methods, awesome_client_meta);
/* Export keys */ /* Export keys */
luaA_class_setup(L, &key_class, "key", (lua_class_allocator_t) key_new, key_class_setup(L);
awesome_key_methods, awesome_key_meta);
/* Export timer */ /* Export timer */
timer_class_setup(L); timer_class_setup(L);