button: use new Lua object system
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
5f6aed4e3d
commit
29b5fd1f25
109
mouse.c
109
mouse.c
|
@ -27,19 +27,26 @@
|
||||||
#include "wibox.h"
|
#include "wibox.h"
|
||||||
#include "common/xcursor.h"
|
#include "common/xcursor.h"
|
||||||
|
|
||||||
DO_LUA_NEW(static, button_t, button, "button", button_ref)
|
DO_LUA_TOSTRING(button_t, button, "button")
|
||||||
DO_LUA_GC(button_t, button, "button", button_unref)
|
LUA_OBJECT_FUNCS(button_t, button, "button")
|
||||||
DO_LUA_EQ(button_t, button, "button")
|
|
||||||
|
|
||||||
/** Delete a button.
|
|
||||||
* \param button The button to destroy.
|
|
||||||
*/
|
|
||||||
void
|
void
|
||||||
button_delete(button_t **button)
|
button_unref_simplified(button_t **b)
|
||||||
{
|
{
|
||||||
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
|
button_unref(globalconf.L, *b);
|
||||||
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
|
}
|
||||||
p_delete(button);
|
|
||||||
|
/** Collect a button.
|
||||||
|
* \param L The Lua VM state.
|
||||||
|
* \return 0.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
luaA_button_gc(lua_State *L)
|
||||||
|
{
|
||||||
|
button_t *button = luaL_checkudata(L, 1, "button");
|
||||||
|
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, button->press);
|
||||||
|
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, button->release);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the pointer position.
|
/** Get the pointer position.
|
||||||
|
@ -126,51 +133,54 @@ static int
|
||||||
luaA_button_new(lua_State *L)
|
luaA_button_new(lua_State *L)
|
||||||
{
|
{
|
||||||
int i, len;
|
int i, len;
|
||||||
button_t *button, **orig;
|
button_t *button, *orig;
|
||||||
|
luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
|
||||||
|
|
||||||
if((orig = luaA_toudata(L, 2, "button")))
|
if((orig = luaA_toudata2(L, 2, "button")))
|
||||||
{
|
{
|
||||||
button_t *copy = p_new(button_t, 1);
|
button_t *copy = button_new(L);
|
||||||
copy->mod = (*orig)->mod;
|
copy->mod = orig->mod;
|
||||||
copy->button = (*orig)->button;
|
copy->button = orig->button;
|
||||||
if((*orig)->press != LUA_REFNIL)
|
if(orig->press != LUA_REFNIL)
|
||||||
{
|
{
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, orig->press);
|
||||||
luaA_registerfct(L, -1, ©->press);
|
luaA_registerfct(L, -1, ©->press);
|
||||||
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
copy->press = LUA_REFNIL;
|
copy->press = LUA_REFNIL;
|
||||||
if((*orig)->release != LUA_REFNIL)
|
if(orig->release != LUA_REFNIL)
|
||||||
{
|
{
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, orig->release);
|
||||||
luaA_registerfct(L, -1, ©->release);
|
luaA_registerfct(L, -1, ©->release);
|
||||||
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
copy->release = LUA_REFNIL;
|
copy->release = LUA_REFNIL;
|
||||||
return luaA_button_userdata_new(L, copy);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
luaA_checktable(L, 2);
|
luaA_checktable(L, 2);
|
||||||
/* arg 3 is mouse button */
|
/* arg 3 is mouse button */
|
||||||
i = luaL_checknumber(L, 3);
|
i = luaL_checknumber(L, 3);
|
||||||
/* arg 4 and 5 are callback functions */
|
|
||||||
|
/* arg 4 and 5 are callback functions, check they are functions... */
|
||||||
if(!lua_isnil(L, 4))
|
if(!lua_isnil(L, 4))
|
||||||
luaA_checkfunction(L, 4);
|
luaA_checkfunction(L, 4);
|
||||||
if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
|
if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
|
||||||
luaA_checkfunction(L, 5);
|
luaA_checkfunction(L, 5);
|
||||||
|
|
||||||
button = p_new(button_t, 1);
|
/* ... then register (can't register before since 5 maybe not nil but not a
|
||||||
button->button = xutil_button_fromint(i);
|
* function */
|
||||||
|
if(!lua_isnil(L, 4))
|
||||||
if(lua_isnil(L, 4))
|
luaA_registerfct(L, 4, &press);
|
||||||
button->press = LUA_REFNIL;
|
|
||||||
else
|
|
||||||
luaA_registerfct(L, 4, &button->press);
|
|
||||||
|
|
||||||
if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
|
if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
|
||||||
luaA_registerfct(L, 5, &button->release);
|
luaA_registerfct(L, 5, &release);
|
||||||
else
|
|
||||||
button->release = LUA_REFNIL;
|
button = button_new(L);
|
||||||
|
button->press = press;
|
||||||
|
button->release = release;
|
||||||
|
button->button = xutil_button_fromint(i);
|
||||||
|
|
||||||
len = lua_objlen(L, 2);
|
len = lua_objlen(L, 2);
|
||||||
for(i = 1; i <= len; i++)
|
for(i = 1; i <= len; i++)
|
||||||
|
@ -180,9 +190,10 @@ luaA_button_new(lua_State *L)
|
||||||
lua_rawgeti(L, 2, i);
|
lua_rawgeti(L, 2, i);
|
||||||
buf = luaL_checklstring(L, -1, &blen);
|
buf = luaL_checklstring(L, -1, &blen);
|
||||||
button->mod |= xutil_key_mask_fromstr(buf, blen);
|
button->mod |= xutil_key_mask_fromstr(buf, blen);
|
||||||
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return luaA_button_userdata_new(L, button);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set a button array with a Lua table.
|
/** Set a button array with a Lua table.
|
||||||
|
@ -193,19 +204,12 @@ luaA_button_new(lua_State *L)
|
||||||
void
|
void
|
||||||
luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
|
luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
|
||||||
{
|
{
|
||||||
button_t **b;
|
|
||||||
|
|
||||||
luaA_checktable(L, idx);
|
luaA_checktable(L, idx);
|
||||||
button_array_wipe(buttons);
|
button_array_wipe(buttons);
|
||||||
button_array_init(buttons);
|
button_array_init(buttons);
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
while(lua_next(L, idx))
|
while(lua_next(L, idx))
|
||||||
{
|
button_array_append(buttons, button_ref(L));
|
||||||
b = luaA_checkudata(L, -1, "button");
|
|
||||||
button_array_append(buttons, *b);
|
|
||||||
button_ref(b);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Push an array of button as an Lua table onto the stack.
|
/** Push an array of button as an Lua table onto the stack.
|
||||||
|
@ -219,7 +223,7 @@ luaA_button_array_get(lua_State *L, button_array_t *buttons)
|
||||||
lua_createtable(L, buttons->len, 0);
|
lua_createtable(L, buttons->len, 0);
|
||||||
for(int i = 0; i < buttons->len; i++)
|
for(int i = 0; i < buttons->len; i++)
|
||||||
{
|
{
|
||||||
luaA_button_userdata_new(L, buttons->tab[i]);
|
button_push(L, buttons->tab[i]);
|
||||||
lua_rawseti(L, -2, i + 1);
|
lua_rawseti(L, -2, i + 1);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -239,26 +243,26 @@ luaA_button_index(lua_State *L)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
size_t len;
|
size_t len;
|
||||||
button_t **button = luaA_checkudata(L, 1, "button");
|
button_t *button = luaL_checkudata(L, 1, "button");
|
||||||
const char *attr = luaL_checklstring(L, 2, &len);
|
const char *attr = luaL_checklstring(L, 2, &len);
|
||||||
|
|
||||||
switch(a_tokenize(attr, len))
|
switch(a_tokenize(attr, len))
|
||||||
{
|
{
|
||||||
case A_TK_PRESS:
|
case A_TK_PRESS:
|
||||||
if((*button)->press != LUA_REFNIL)
|
if(button->press != LUA_REFNIL)
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, button->press);
|
||||||
else
|
else
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
break;
|
break;
|
||||||
case A_TK_RELEASE:
|
case A_TK_RELEASE:
|
||||||
if((*button)->release != LUA_REFNIL)
|
if(button->release != LUA_REFNIL)
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, button->release);
|
||||||
else
|
else
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
break;
|
break;
|
||||||
case A_TK_BUTTON:
|
case A_TK_BUTTON:
|
||||||
/* works fine, but not *really* neat */
|
/* works fine, but not *really* neat */
|
||||||
lua_pushnumber(L, (*button)->button);
|
lua_pushnumber(L, button->button);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -279,19 +283,19 @@ luaA_button_newindex(lua_State *L)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
size_t len;
|
size_t len;
|
||||||
button_t **button = luaA_checkudata(L, 1, "button");
|
button_t *button = luaL_checkudata(L, 1, "button");
|
||||||
const char *attr = luaL_checklstring(L, 2, &len);
|
const char *attr = luaL_checklstring(L, 2, &len);
|
||||||
|
|
||||||
switch(a_tokenize(attr, len))
|
switch(a_tokenize(attr, len))
|
||||||
{
|
{
|
||||||
case A_TK_PRESS:
|
case A_TK_PRESS:
|
||||||
luaA_registerfct(L, 3, &(*button)->press);
|
luaA_registerfct(L, 3, &button->press);
|
||||||
break;
|
break;
|
||||||
case A_TK_RELEASE:
|
case A_TK_RELEASE:
|
||||||
luaA_registerfct(L, 3, &(*button)->release);
|
luaA_registerfct(L, 3, &button->release);
|
||||||
break;
|
break;
|
||||||
case A_TK_BUTTON:
|
case A_TK_BUTTON:
|
||||||
(*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
|
button->button = xutil_button_fromint(luaL_checknumber(L, 3));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -310,7 +314,6 @@ const struct luaL_reg awesome_button_meta[] =
|
||||||
{ "__index", luaA_button_index },
|
{ "__index", luaA_button_index },
|
||||||
{ "__newindex", luaA_button_newindex },
|
{ "__newindex", luaA_button_newindex },
|
||||||
{ "__gc", luaA_button_gc },
|
{ "__gc", luaA_button_gc },
|
||||||
{ "__eq", luaA_button_eq },
|
|
||||||
{ "__tostring", luaA_button_tostring },
|
{ "__tostring", luaA_button_tostring },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
10
mouse.h
10
mouse.h
|
@ -27,8 +27,8 @@
|
||||||
/** Mouse buttons bindings */
|
/** Mouse buttons bindings */
|
||||||
struct button_t
|
struct button_t
|
||||||
{
|
{
|
||||||
/** Ref count */
|
/** Lua references */
|
||||||
int refcount;
|
luaA_ref_array_t refs;
|
||||||
/** Key modifiers */
|
/** Key modifiers */
|
||||||
unsigned long mod;
|
unsigned long mod;
|
||||||
/** Mouse button number */
|
/** Mouse button number */
|
||||||
|
@ -39,13 +39,11 @@ struct button_t
|
||||||
luaA_ref release;
|
luaA_ref release;
|
||||||
};
|
};
|
||||||
|
|
||||||
void button_delete(button_t **);
|
|
||||||
|
|
||||||
bool mouse_query_pointer(xcb_window_t, int16_t *, int16_t *, xcb_window_t *, uint16_t *);
|
bool mouse_query_pointer(xcb_window_t, int16_t *, int16_t *, xcb_window_t *, uint16_t *);
|
||||||
int luaA_mouse_pushstatus(lua_State *, int, int, uint16_t);
|
int luaA_mouse_pushstatus(lua_State *, int, int, uint16_t);
|
||||||
|
|
||||||
DO_RCNT(button_t, button, button_delete)
|
void button_unref_simplified(button_t **);
|
||||||
ARRAY_FUNCS(button_t *, button, button_unref)
|
ARRAY_FUNCS(button_t *, button, button_unref_simplified)
|
||||||
|
|
||||||
int luaA_button_array_get(lua_State *, button_array_t *);
|
int luaA_button_array_get(lua_State *, button_array_t *);
|
||||||
void luaA_button_array_set(lua_State *, int idx, button_array_t *);
|
void luaA_button_array_set(lua_State *, int idx, button_array_t *);
|
||||||
|
|
Loading…
Reference in New Issue