diff --git a/luaa.c b/luaa.c index 78e30844..3e9455f7 100644 --- a/luaa.c +++ b/luaa.c @@ -80,9 +80,10 @@ extern const struct luaL_Reg awesome_dbus_lib[]; #endif extern const struct luaL_Reg awesome_keygrabber_lib[]; extern const struct luaL_Reg awesome_mousegrabber_lib[]; -extern const struct luaL_Reg awesome_root_lib[]; extern const struct luaL_Reg awesome_mouse_methods[]; extern const struct luaL_Reg awesome_mouse_meta[]; +extern const struct luaL_Reg awesome_root_methods[]; +extern const struct luaL_Reg awesome_root_meta[]; /** A call into the Lua code aborted with an error. * @@ -995,8 +996,7 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath) setup_awesome_signals(L); /* Export root lib */ - luaA_registerlib(L, "root", awesome_root_lib); - lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */ + luaA_openlib(L, "root", awesome_root_methods, awesome_root_meta); #ifdef WITH_DBUS /* Export D-Bus lib */ diff --git a/root.c b/root.c index 4da0f660..3c467a3e 100644 --- a/root.c +++ b/root.c @@ -31,6 +31,7 @@ #include "common/xcursor.h" #include "common/xutil.h" #include "objects/button.h" +#include "common/luaclass.h" #include "xwindow.h" #include "math.h" @@ -39,6 +40,10 @@ #include #include +static int miss_index_handler = LUA_REFNIL; +static int miss_newindex_handler = LUA_REFNIL; +static int miss_call_handler = LUA_REFNIL; + static void root_set_wallpaper_pixmap(xcb_connection_t *c, xcb_pixmap_t p) { @@ -525,7 +530,62 @@ luaA_root_tags(lua_State *L) return 1; } -const struct luaL_Reg awesome_root_lib[] = +/** +* Add a custom call handler. +*/ +static int +luaA_root_set_call_handler(lua_State *L) +{ + return luaA_registerfct(L, 1, &miss_call_handler); +} + +/** +* Add a custom property handler (getter). +*/ +static int +luaA_root_set_index_miss_handler(lua_State *L) +{ + return luaA_registerfct(L, 1, &miss_index_handler); +} + +/** +* Add a custom property handler (setter). +*/ +static int +luaA_root_set_newindex_miss_handler(lua_State *L) +{ + return luaA_registerfct(L, 1, &miss_newindex_handler); +} + +/** Root library. + * \param L The Lua VM state. + * \return The number of elements pushed on stack. + * \luastack + */ +static int +luaA_root_index(lua_State *L) +{ + if (miss_index_handler != LUA_REFNIL) + return luaA_call_handler(L, miss_index_handler); + + return luaA_default_index(L); +} + +/** Newindex for root. + * \param L The Lua VM state. + * \return The number of elements pushed on stack. + */ +static int +luaA_root_newindex(lua_State *L) +{ + /* Call the lua root property handler */ + if (miss_newindex_handler != LUA_REFNIL) + return luaA_call_handler(L, miss_newindex_handler); + + return luaA_default_newindex(L); +} + +const struct luaL_Reg awesome_root_methods[] = { { "buttons", luaA_root_buttons }, { "keys", luaA_root_keys }, @@ -536,8 +596,17 @@ const struct luaL_Reg awesome_root_lib[] = { "size", luaA_root_size }, { "size_mm", luaA_root_size_mm }, { "tags", luaA_root_tags }, - { "__index", luaA_default_index }, - { "__newindex", luaA_default_newindex }, + { "__index", luaA_root_index }, + { "__newindex", luaA_root_newindex }, + { "set_index_miss_handler", luaA_root_set_index_miss_handler}, + { "set_call_handler", luaA_root_set_call_handler}, + { "set_newindex_miss_handler", luaA_root_set_newindex_miss_handler}, + + { NULL, NULL } +}; + +const struct luaL_Reg awesome_root_meta[] = +{ { NULL, NULL } }; diff --git a/tests/examples/shims/root.lua b/tests/examples/shims/root.lua index e79e9652..6ceaf787 100644 --- a/tests/examples/shims/root.lua +++ b/tests/examples/shims/root.lua @@ -174,6 +174,15 @@ function root._write_string(string, c) end end + +function root.set_newindex_miss_handler(h) + rawset(mouse, "_ni_handler", h) +end + +function root.set_index_miss_handler(h) + rawset(mouse, "_i_handler", h) +end + return root -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80