key: use new Lua object system

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-04-11 12:40:56 +02:00
parent 4531a3a17c
commit 5f6aed4e3d
2 changed files with 34 additions and 25 deletions

53
key.c
View File

@ -25,18 +25,29 @@
#include "structs.h" #include "structs.h"
LUA_OBJECT_FUNCS(keyb_t, key, "key")
static void static void
key_delete(keyb_t **kbp) key_unref_simplified(keyb_t **b)
{ {
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*kbp)->press); key_unref(globalconf.L, *b);
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*kbp)->release);
p_delete(kbp);
} }
DO_RCNT(keyb_t, key, key_delete) ARRAY_FUNCS(keyb_t *, key, key_unref_simplified)
ARRAY_FUNCS(keyb_t *, key, key_unref) DO_LUA_TOSTRING(keyb_t, key, "key")
DO_LUA_NEW(static, keyb_t, key, "key", key_ref)
DO_LUA_GC(keyb_t, key, "key", key_unref) /** 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;
}
static void static void
keybindings_init(keybindings_t *kb) keybindings_init(keybindings_t *kb)
@ -115,14 +126,16 @@ window_grabkeys(xcb_window_t win, keybindings_t *keys)
window_grabkey(win, keys->by_sym.tab[i]); window_grabkey(win, keys->by_sym.tab[i]);
} }
/** Register a key which on top of the stack.
* \param keys The keybinding array where to put the key.
*/
static void static void
key_register(keybindings_t *keys, keyb_t *k) key_register(keybindings_t *keys)
{ {
keyb_t *k = key_ref(globalconf.L);
key_array_t *arr = k->keysym ? &keys->by_sym : &keys->by_code; key_array_t *arr = k->keysym ? &keys->by_sym : &keys->by_code;
int l = 0, r = arr->len; int l = 0, r = arr->len;
key_ref(&k);
while(l < r) { while(l < r) {
int i = (r + l) / 2; int i = (r + l) / 2;
switch(key_cmp(k, arr->tab[i])) switch(key_cmp(k, arr->tab[i]))
@ -131,7 +144,7 @@ key_register(keybindings_t *keys, keyb_t *k)
r = i; r = i;
break; break;
case 0: /* k == arr->tab[i] */ case 0: /* k == arr->tab[i] */
key_unref(&arr->tab[i]); key_unref(globalconf.L, arr->tab[i]);
arr->tab[i] = k; arr->tab[i] = k;
return; return;
case 1: /* k > arr->tab[i] */ case 1: /* k > arr->tab[i] */
@ -306,8 +319,7 @@ luaA_key_new(lua_State *L)
if(lua_gettop(L) == 5 && !lua_isnil(L, 5)) if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
luaA_registerfct(L, 5, &release); luaA_registerfct(L, 5, &release);
/* get the last arg as function */ k = key_new(L);
k = p_new(keyb_t, 1);
luaA_keystore(k, key, len); luaA_keystore(k, key, len);
k->press = press; k->press = press;
@ -319,10 +331,11 @@ luaA_key_new(lua_State *L)
size_t blen; size_t blen;
lua_rawgeti(L, 2, i); lua_rawgeti(L, 2, i);
key = luaL_checklstring(L, -1, &blen); key = luaL_checklstring(L, -1, &blen);
lua_pop(L, 1);
k->mod |= xutil_key_mask_fromstr(key, blen); k->mod |= xutil_key_mask_fromstr(key, blen);
} }
return luaA_key_userdata_new(L, k); return 1;
} }
/** Set a key array with a Lua table. /** Set a key array with a Lua table.
@ -340,11 +353,7 @@ luaA_key_array_set(lua_State *L, int idx, keybindings_t *keys)
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, idx)) while(lua_next(L, idx))
{ key_register(keys);
keyb_t **k = luaA_checkudata(L, -1, "key");
key_register(keys, *k);
lua_pop(L, 1);
}
} }
/** Push an array of key as an Lua table onto the stack. /** Push an array of key as an Lua table onto the stack.
@ -358,12 +367,12 @@ luaA_key_array_get(lua_State *L, keybindings_t *keys)
lua_createtable(L, keys->by_code.len + keys->by_sym.len, 0); lua_createtable(L, keys->by_code.len + keys->by_sym.len, 0);
for(int i = 0; i < keys->by_code.len; i++) for(int i = 0; i < keys->by_code.len; i++)
{ {
luaA_key_userdata_new(L, keys->by_code.tab[i]); key_push(L, keys->by_code.tab[i]);
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }
for(int i = 0; i < keys->by_sym.len; i++) for(int i = 0; i < keys->by_sym.len; i++)
{ {
luaA_key_userdata_new(L, keys->by_sym.tab[i]); key_push(L, keys->by_sym.tab[i]);
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }
return 1; return 1;

6
key.h
View File

@ -24,12 +24,12 @@
#include <xcb/xcb.h> #include <xcb/xcb.h>
#include "luaa.h" #include "luaa.h"
#include "common/array.h" #include "common/luaobject.h"
typedef struct keyb_t typedef struct keyb_t
{ {
/** Ref count */ /** Lua references */
int refcount; luaA_ref_array_t refs;
/** Key modifier */ /** Key modifier */
unsigned long mod; unsigned long mod;
/** Keysym */ /** Keysym */