From f523b37e1d1f994c2c7928d1ce1b51cb8b766947 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Fri, 9 Oct 2009 20:32:33 +0200 Subject: [PATCH] lua{class,object}: {add,remove}_signal() take lua_CFunction as arg Signed-off-by: Julien Danjou --- common/luaclass.c | 15 +++++++++++---- common/luaclass.h | 12 +++++++----- common/luaobject.c | 28 ++++++++++++++++++++++------ common/luaobject.h | 6 ++++-- ewmh.c | 39 +++++++++++++++------------------------ objects/wibox.c | 3 +-- 6 files changed, 60 insertions(+), 43 deletions(-) diff --git a/common/luaclass.c b/common/luaclass.c index 5b3bdc22..69070abf 100644 --- a/common/luaclass.c +++ b/common/luaclass.c @@ -248,16 +248,23 @@ luaA_class_setup(lua_State *L, lua_class_t *class, } void -luaA_class_add_signal(lua_State *L, lua_class_t *lua_class, - const char *name, int ud) +luaA_class_add_signal(lua_State *L, lua_class_t *lua_class, const char *name, lua_CFunction fn) +{ + lua_pushcfunction(L, fn); + luaA_class_add_signal_from_stack(L, lua_class, name, -1); +} + +void +luaA_class_add_signal_from_stack(lua_State *L, lua_class_t *lua_class, + const char *name, int ud) { luaA_checkfunction(L, ud); signal_add(&lua_class->signals, name, luaA_object_ref(L, ud)); } void -luaA_class_remove_signal(lua_State *L, lua_class_t *lua_class, - const char *name, int ud) +luaA_class_remove_signal_from_stack(lua_State *L, lua_class_t *lua_class, + const char *name, int ud) { luaA_checkfunction(L, ud); void *ref = (void *) lua_topointer(L, ud); diff --git a/common/luaclass.h b/common/luaclass.h index dbcedba5..5f7628ae 100644 --- a/common/luaclass.h +++ b/common/luaclass.h @@ -73,8 +73,9 @@ struct lua_class_t const char * luaA_typename(lua_State *, int); lua_class_t * luaA_class_get(lua_State *, int); -void luaA_class_add_signal(lua_State *, lua_class_t *, const char *, int); -void luaA_class_remove_signal(lua_State *, lua_class_t *, const char *, int); +void luaA_class_add_signal(lua_State *, lua_class_t *, const char *, lua_CFunction); +void luaA_class_add_signal_from_stack(lua_State *, lua_class_t *, const char *, int); +void luaA_class_remove_signal_from_stack(lua_State *, lua_class_t *, const char *, int); void luaA_class_emit_signal(lua_State *, lua_class_t *, const char *, int); void luaA_openlib(lua_State *, const char *, const struct luaL_reg[], const struct luaL_reg[]); @@ -107,15 +108,16 @@ luaA_checkudataornil(lua_State *L, int udx, lua_class_t *class) static inline int \ luaA_##prefix##_class_add_signal(lua_State *L) \ { \ - luaA_class_add_signal(L, &(lua_class), luaL_checkstring(L, 1), 2); \ + luaA_class_add_signal_from_stack(L, &(lua_class), \ + luaL_checkstring(L, 1), 2); \ return 0; \ } \ \ static inline int \ luaA_##prefix##_class_remove_signal(lua_State *L) \ { \ - luaA_class_remove_signal(L, &(lua_class), \ - luaL_checkstring(L, 1), 2); \ + luaA_class_remove_signal_from_stack(L, &(lua_class), \ + luaL_checkstring(L, 1), 2); \ return 0; \ } \ \ diff --git a/common/luaobject.c b/common/luaobject.c index 4f43357e..72025ae1 100644 --- a/common/luaobject.c +++ b/common/luaobject.c @@ -148,6 +148,22 @@ luaA_settype(lua_State *L, lua_class_t *lua_class) return 1; } +void +luaA_object_add_signal(lua_State *L, int oud, + const char *name, lua_CFunction fn) +{ + lua_pushcfunction(L, fn); + luaA_object_add_signal_from_stack(L, oud, name, -1); +} + +void +luaA_object_remove_signal(lua_State *L, int oud, + const char *name, lua_CFunction fn) +{ + lua_pushcfunction(L, fn); + luaA_object_remove_signal_from_stack(L, oud, name, -1); +} + /** Add a signal to an object. * \param L The Lua VM state. * \param oud The object index on the stack. @@ -155,8 +171,8 @@ luaA_settype(lua_State *L, lua_class_t *lua_class) * \param ud The index of function to call when signal is emitted. */ void -luaA_object_add_signal(lua_State *L, int oud, - const char *name, int ud) +luaA_object_add_signal_from_stack(lua_State *L, int oud, + const char *name, int ud) { luaA_checkfunction(L, ud); lua_object_t *obj = lua_touserdata(L, oud); @@ -170,8 +186,8 @@ luaA_object_add_signal(lua_State *L, int oud, * \param ud The index of function to call when signal is emitted. */ void -luaA_object_remove_signal(lua_State *L, int oud, - const char *name, int ud) +luaA_object_remove_signal_from_stack(lua_State *L, int oud, + const char *name, int ud) { luaA_checkfunction(L, ud); lua_object_t *obj = lua_touserdata(L, oud); @@ -260,14 +276,14 @@ luaA_object_emit_signal(lua_State *L, int oud, int luaA_object_add_signal_simple(lua_State *L) { - luaA_object_add_signal(L, 1, luaL_checkstring(L, 2), 3); + luaA_object_add_signal_from_stack(L, 1, luaL_checkstring(L, 2), 3); return 0; } int luaA_object_remove_signal_simple(lua_State *L) { - luaA_object_remove_signal(L, 1, luaL_checkstring(L, 2), 3); + luaA_object_remove_signal_from_stack(L, 1, luaL_checkstring(L, 2), 3); return 0; } diff --git a/common/luaobject.h b/common/luaobject.h index dab3918e..9ecd9a22 100644 --- a/common/luaobject.h +++ b/common/luaobject.h @@ -150,8 +150,10 @@ luaA_object_push(lua_State *L, void *pointer) void signal_object_emit(lua_State *, signal_array_t *, const char *, int); -void luaA_object_add_signal(lua_State *, int, const char *, int); -void luaA_object_remove_signal(lua_State *, int, const char *, int); +void luaA_object_add_signal(lua_State *, int, const char *, lua_CFunction); +void luaA_object_remove_signal(lua_State *, int, const char *, lua_CFunction); +void luaA_object_add_signal_from_stack(lua_State *, int, const char *, int); +void luaA_object_remove_signal_from_stack(lua_State *, int, const char *, int); void luaA_object_emit_signal(lua_State *, int, const char *, int); int luaA_object_add_signal_simple(lua_State *); diff --git a/ewmh.c b/ewmh.c index 977a27e4..44be7d33 100644 --- a/ewmh.c +++ b/ewmh.c @@ -80,20 +80,16 @@ ewmh_client_update_hints(lua_State *L) static int ewmh_signal_on_client_new(lua_State *L) { -#define DO_ADD_SIGNAL_FOR_PROPERTY(prop) \ - lua_pushcfunction(L, ewmh_client_update_hints); \ - luaA_object_add_signal(L, 1, "property::" #prop , -1); - DO_ADD_SIGNAL_FOR_PROPERTY(modal) - DO_ADD_SIGNAL_FOR_PROPERTY(fullscreen) - DO_ADD_SIGNAL_FOR_PROPERTY(maximized_horizontal) - DO_ADD_SIGNAL_FOR_PROPERTY(maximized_vertical) - DO_ADD_SIGNAL_FOR_PROPERTY(sticky) - DO_ADD_SIGNAL_FOR_PROPERTY(skip_taskbar) - DO_ADD_SIGNAL_FOR_PROPERTY(above) - DO_ADD_SIGNAL_FOR_PROPERTY(below) - DO_ADD_SIGNAL_FOR_PROPERTY(minimized) - DO_ADD_SIGNAL_FOR_PROPERTY(urgent) -#undef DO_ADD_SIGNAL_FOR_PROPERTY + luaA_object_add_signal(L, 1, "property::modal" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::fullscreen" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::maximized_horizontal" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::maximized_vertical" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::sticky" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::skip_taskbar" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::above" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::below" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::minimized" , ewmh_client_update_hints); + luaA_object_add_signal(L, 1, "property::urgent" , ewmh_client_update_hints); return 0; } @@ -226,16 +222,11 @@ ewmh_init(void) ewmh_update_desktop_geometry(); - lua_pushcfunction(globalconf.L, ewmh_signal_on_client_new); - luaA_class_add_signal(globalconf.L, &client_class, "new", -1); - lua_pushcfunction(globalconf.L, ewmh_update_net_active_window); - luaA_class_add_signal(globalconf.L, &client_class, "focus", -1); - lua_pushcfunction(globalconf.L, ewmh_update_net_active_window); - luaA_class_add_signal(globalconf.L, &client_class, "unfocus", -1); - lua_pushcfunction(globalconf.L, ewmh_update_net_client_list); - luaA_class_add_signal(globalconf.L, &client_class, "manage", -1); - lua_pushcfunction(globalconf.L, ewmh_update_net_client_list); - luaA_class_add_signal(globalconf.L, &client_class, "unmanage", -1); + luaA_class_add_signal(globalconf.L, &client_class, "new", ewmh_signal_on_client_new); + luaA_class_add_signal(globalconf.L, &client_class, "focus", ewmh_update_net_active_window); + luaA_class_add_signal(globalconf.L, &client_class, "unfocus", ewmh_update_net_active_window); + luaA_class_add_signal(globalconf.L, &client_class, "manage", ewmh_update_net_client_list); + luaA_class_add_signal(globalconf.L, &client_class, "unmanage", ewmh_update_net_client_list); } /** Set the client list in stacking order, bottom to top. diff --git a/objects/wibox.c b/objects/wibox.c index 0743012b..16d5c22b 100644 --- a/objects/wibox.c +++ b/objects/wibox.c @@ -770,8 +770,7 @@ luaA_wibox_new(lua_State *L) if(!w->geometry.height) w->geometry.height = 1; - lua_pushcfunction(L, luaA_wibox_need_update); - luaA_object_add_signal(L, -2, "property::border_width", -1); + luaA_object_add_signal(L, -2, "property::border_width", luaA_wibox_need_update); return 1; }