mirror of
https://github.com/awesomeWM/awesome
synced 2024-11-17 07:47:41 +01:00
Reduce code duplication with luaA_*_call_handler (#2321)
The functions luaA_class_call_handler() and luaA_mouse_call_handler() are basically identical. Fix this code duplication by moving this to luaA_call_handler() in lualib.h. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
40a55a3d2f
commit
ef110361dc
3 changed files with 39 additions and 71 deletions
|
@ -389,40 +389,6 @@ luaA_class_property_get(lua_State *L, lua_class_t *lua_class, int fieldidx)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Call a registered function.
|
|
||||||
* \param L The Lua VM state.
|
|
||||||
* \param handler The function to call.
|
|
||||||
* \return The number of elements pushed on stack.
|
|
||||||
*/
|
|
||||||
static
|
|
||||||
int luaA_class_call_handler(lua_State *L, int handler)
|
|
||||||
{
|
|
||||||
/* This is based on luaA_dofunction, but allows multiple return values */
|
|
||||||
assert(handler != LUA_REFNIL);
|
|
||||||
|
|
||||||
int nargs = lua_gettop(L);
|
|
||||||
|
|
||||||
/* Push error handling function and move it before args */
|
|
||||||
lua_pushcfunction(L, luaA_dofunction_error);
|
|
||||||
lua_insert(L, - nargs - 1);
|
|
||||||
int error_func_pos = 1;
|
|
||||||
|
|
||||||
/* push function and move it before args */
|
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, handler);
|
|
||||||
lua_insert(L, - nargs - 1);
|
|
||||||
|
|
||||||
if(lua_pcall(L, nargs, LUA_MULTRET, error_func_pos))
|
|
||||||
{
|
|
||||||
warn("%s", lua_tostring(L, -1));
|
|
||||||
/* Remove error function and error string */
|
|
||||||
lua_pop(L, 2);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/* Remove error function */
|
|
||||||
lua_remove(L, error_func_pos);
|
|
||||||
return lua_gettop(L);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Generic index meta function for objects.
|
/** Generic index meta function for objects.
|
||||||
* \param L The Lua VM state.
|
* \param L The Lua VM state.
|
||||||
* \return The number of elements pushed on stack.
|
* \return The number of elements pushed on stack.
|
||||||
|
@ -471,7 +437,7 @@ luaA_class_index(lua_State *L)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(class->index_miss_handler != LUA_REFNIL)
|
if(class->index_miss_handler != LUA_REFNIL)
|
||||||
return luaA_class_call_handler(L, class->index_miss_handler);
|
return luaA_call_handler(L, class->index_miss_handler);
|
||||||
if(class->index_miss_property)
|
if(class->index_miss_property)
|
||||||
return class->index_miss_property(L, luaA_checkudata(L, 1, class));
|
return class->index_miss_property(L, luaA_checkudata(L, 1, class));
|
||||||
}
|
}
|
||||||
|
@ -503,7 +469,7 @@ luaA_class_newindex(lua_State *L)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(class->newindex_miss_handler != LUA_REFNIL)
|
if(class->newindex_miss_handler != LUA_REFNIL)
|
||||||
return luaA_class_call_handler(L, class->newindex_miss_handler);
|
return luaA_call_handler(L, class->newindex_miss_handler);
|
||||||
if(class->newindex_miss_property)
|
if(class->newindex_miss_property)
|
||||||
return class->newindex_miss_property(L, luaA_checkudata(L, 1, class));
|
return class->newindex_miss_property(L, luaA_checkudata(L, 1, class));
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#define AWESOME_COMMON_LUALIB
|
#define AWESOME_COMMON_LUALIB
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
|
||||||
#include "common/util.h"
|
#include "common/util.h"
|
||||||
|
|
||||||
|
@ -84,6 +85,40 @@ luaA_dofunction(lua_State *L, int nargs, int nret)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Call a registered function. Its arguments are the complete stack contents.
|
||||||
|
* \param L The Lua VM state.
|
||||||
|
* \param handler The function to call.
|
||||||
|
* \return The number of elements pushed on stack.
|
||||||
|
*/
|
||||||
|
static inline
|
||||||
|
int luaA_call_handler(lua_State *L, int handler)
|
||||||
|
{
|
||||||
|
/* This is based on luaA_dofunction, but allows multiple return values */
|
||||||
|
assert(handler != LUA_REFNIL);
|
||||||
|
|
||||||
|
int nargs = lua_gettop(L);
|
||||||
|
|
||||||
|
/* Push error handling function and move it before args */
|
||||||
|
lua_pushcfunction(L, luaA_dofunction_error);
|
||||||
|
lua_insert(L, - nargs - 1);
|
||||||
|
int error_func_pos = 1;
|
||||||
|
|
||||||
|
/* push function and move it before args */
|
||||||
|
lua_rawgeti(L, LUA_REGISTRYINDEX, handler);
|
||||||
|
lua_insert(L, - nargs - 1);
|
||||||
|
|
||||||
|
if(lua_pcall(L, nargs, LUA_MULTRET, error_func_pos))
|
||||||
|
{
|
||||||
|
warn("%s", lua_tostring(L, -1));
|
||||||
|
/* Remove error function and error string */
|
||||||
|
lua_pop(L, 2);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* Remove error function */
|
||||||
|
lua_remove(L, error_func_pos);
|
||||||
|
return lua_gettop(L);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||||
|
|
37
mouse.c
37
mouse.c
|
@ -142,39 +142,6 @@ mouse_warp_pointer(xcb_window_t window, int16_t x, int16_t y)
|
||||||
0, 0, 0, 0, x, y);
|
0, 0, 0, 0, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Allow the a Lua handler to be implemented for custom properties and
|
|
||||||
* functions.
|
|
||||||
* \param L A lua state
|
|
||||||
* \param handler A function on the LUA_REGISTRYINDEX
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
luaA_mouse_call_handler(lua_State *L, int handler)
|
|
||||||
{
|
|
||||||
int nargs = lua_gettop(L);
|
|
||||||
|
|
||||||
/* Push error handling function and move it before args */
|
|
||||||
lua_pushcfunction(L, luaA_dofunction_error);
|
|
||||||
lua_insert(L, - nargs - 1);
|
|
||||||
int error_func_pos = 1;
|
|
||||||
|
|
||||||
/* push function and move it before args */
|
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, handler);
|
|
||||||
lua_insert(L, - nargs - 1);
|
|
||||||
|
|
||||||
if(lua_pcall(L, nargs, LUA_MULTRET, error_func_pos))
|
|
||||||
{
|
|
||||||
warn("%s", lua_tostring(L, -1));
|
|
||||||
/* Remove error function and error string */
|
|
||||||
lua_pop(L, 2);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/* Remove error function */
|
|
||||||
lua_remove(L, error_func_pos);
|
|
||||||
|
|
||||||
return lua_gettop(L);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Mouse library.
|
/** Mouse library.
|
||||||
* \param L The Lua VM state.
|
* \param L The Lua VM state.
|
||||||
* \return The number of elements pushed on stack.
|
* \return The number of elements pushed on stack.
|
||||||
|
@ -191,7 +158,7 @@ luaA_mouse_index(lua_State *L)
|
||||||
/* attr is not "screen"?! */
|
/* attr is not "screen"?! */
|
||||||
if (A_STRNEQ(attr, "screen")) {
|
if (A_STRNEQ(attr, "screen")) {
|
||||||
if (miss_index_handler != LUA_REFNIL) {
|
if (miss_index_handler != LUA_REFNIL) {
|
||||||
return luaA_mouse_call_handler(L, miss_index_handler);
|
return luaA_call_handler(L, miss_index_handler);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return luaA_default_index(L);
|
return luaA_default_index(L);
|
||||||
|
@ -226,7 +193,7 @@ luaA_mouse_newindex(lua_State *L)
|
||||||
if (A_STRNEQ(attr, "screen")) {
|
if (A_STRNEQ(attr, "screen")) {
|
||||||
/* Call the lua mouse property handler */
|
/* Call the lua mouse property handler */
|
||||||
if (miss_newindex_handler != LUA_REFNIL) {
|
if (miss_newindex_handler != LUA_REFNIL) {
|
||||||
return luaA_mouse_call_handler(L, miss_newindex_handler);
|
return luaA_call_handler(L, miss_newindex_handler);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return luaA_default_newindex(L);
|
return luaA_default_newindex(L);
|
||||||
|
|
Loading…
Reference in a new issue