luaa: check that function is not NIL before pushing and calling

Otherwise we may push unused elements because dofunction() checked
_after_ if the function was nil, or not.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-11-20 17:48:23 +01:00
parent ee8bcd6c43
commit d1db6903fa
9 changed files with 137 additions and 74 deletions

View File

@ -161,8 +161,11 @@ client_unfocus(client_t *c)
globalconf.screens[c->phys_screen].client_focus = NULL;
/* Call hook */
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
if(globalconf.hooks.unfocus != LUA_REFNIL)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
}
ewmh_update_net_active_window(c->phys_screen);
}
@ -217,8 +220,11 @@ client_focus(client_t *c)
client_need_arrange(c);
/* execute hook */
luaA_client_userdata_new(globalconf.L, globalconf.screen_focus->client_focus);
luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
if(globalconf.hooks.focus != LUA_REFNIL)
{
luaA_client_userdata_new(globalconf.L, globalconf.screen_focus->client_focus);
luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
}
ewmh_update_net_active_window(c->phys_screen);
}
@ -491,11 +497,15 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen,
ewmh_update_net_client_list(c->phys_screen);
/* Call hook to notify list change */
luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
if(globalconf.hooks.clients != LUA_REFNIL)
luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
/* call hook */
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.manage, 1, 0);
if(globalconf.hooks.manage != LUA_REFNIL)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.manage, 1, 0);
}
}
/** Compute client geometry with respect to its geometry hints.
@ -838,11 +848,15 @@ client_unmanage(client_t *c)
untag_client(c, tags->tab[i]);
/* call hook */
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
if(globalconf.hooks.unmanage != LUA_REFNIL)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
}
/* Call hook to notify list change */
luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
if(globalconf.hooks.clients != LUA_REFNIL)
luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
/* The server grab construct avoids race conditions. */
xcb_grab_server(globalconf.connection);
@ -1054,7 +1068,8 @@ luaA_client_swap(lua_State *L)
client_need_arrange(*swap);
/* Call hook to notify list change */
luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
if(globalconf.hooks.clients != LUA_REFNIL)
luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
return 0;
}

70
event.c
View File

@ -60,21 +60,33 @@ event_handle_mouse_button(client_t *c,
for(int i = 0; i < buttons->len; i++)
if(button == buttons->tab[i]->button
&& XUTIL_MASK_CLEAN(state) == buttons->tab[i]->mod)
{
if(c)
switch(type)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L,
type == XCB_BUTTON_PRESS ?
buttons->tab[i]->press : buttons->tab[i]->release,
1, 0);
case XCB_BUTTON_PRESS:
if(buttons->tab[i]->press != LUA_REFNIL)
{
if(c)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, buttons->tab[i]->press, 1, 0);
}
else
luaA_dofunction(globalconf.L, buttons->tab[i]->press, 0, 0);
}
break;
case XCB_BUTTON_RELEASE:
if(buttons->tab[i]->release != LUA_REFNIL)
{
if(c)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, buttons->tab[i]->release, 1, 0);
}
else
luaA_dofunction(globalconf.L, buttons->tab[i]->release, 0, 0);
}
break;
}
else
luaA_dofunction(globalconf.L,
type == XCB_BUTTON_PRESS ?
buttons->tab[i]->press : buttons->tab[i]->release,
0, 0);
}
}
/** Get a widget node from a wibox by coords.
@ -340,16 +352,22 @@ event_handle_widget_motionnotify(void *object,
{
if(*mouse_over)
{
/* call mouse leave function on old widget */
luaA_wibox_userdata_new(globalconf.L, object);
luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
if((*mouse_over)->mouse_leave != LUA_REFNIL)
{
/* call mouse leave function on old widget */
luaA_wibox_userdata_new(globalconf.L, object);
luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
}
}
if(w)
{
/* call mouse enter function on new widget and register it */
*mouse_over = w->widget;
luaA_wibox_userdata_new(globalconf.L, object);
luaA_dofunction(globalconf.L, w->widget->mouse_enter, 1, 0);
if(w->widget->mouse_enter != LUA_REFNIL)
{
luaA_wibox_userdata_new(globalconf.L, object);
luaA_dofunction(globalconf.L, w->widget->mouse_enter, 1, 0);
}
}
}
}
@ -395,9 +413,12 @@ event_handle_leavenotify(void *data __attribute__ ((unused)),
if(wibox && wibox->mouse_over)
{
/* call mouse leave function on widget the mouse was over */
luaA_wibox_userdata_new(globalconf.L, wibox);
luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
{
/* call mouse leave function on widget the mouse was over */
luaA_wibox_userdata_new(globalconf.L, wibox);
luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
}
wibox->mouse_over = NULL;
}
@ -445,8 +466,11 @@ event_handle_enternotify(void *data __attribute__ ((unused)),
globalconf.pointer_x = ev->root_x;
globalconf.pointer_y = ev->root_y;
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
if(globalconf.hooks.mouse_enter != LUA_REFNIL)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
}
}
else if((emwin = xembed_getbywin(globalconf.embedded, ev->event)))
xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY,

4
ewmh.c
View File

@ -330,9 +330,7 @@ ewmh_process_state_atom(client_t *c, xcb_atom_t state, int set)
c->isurgent = !c->isurgent;
/* execute hook */
luaA_client_userdata_new(globalconf.L, c);
lua_pushliteral(globalconf.L, "urgent");
luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0);
hooks_property(c, "urgent");
}
}

View File

@ -72,8 +72,11 @@ arrange(int screen)
globalconf.screens[screen].need_arrange = false;
/* call hook */
lua_pushnumber(globalconf.L, screen + 1);
luaA_dofunction(globalconf.L, globalconf.hooks.arrange, 1, 0);
if(globalconf.hooks.arrange != LUA_REFNIL)
{
lua_pushnumber(globalconf.L, screen + 1);
luaA_dofunction(globalconf.L, globalconf.hooks.arrange, 1, 0);
}
}
/** Refresh the screen disposition

3
luaa.c
View File

@ -1251,7 +1251,8 @@ luaA_cs_cleanup(void)
void
luaA_on_timer(EV_P_ ev_timer *w, int revents)
{
luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
if(globalconf.hooks.timer != LUA_REFNIL)
luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
awesome_refresh(globalconf.connection);
}

31
luaa.h
View File

@ -314,21 +314,17 @@ luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
static inline bool
luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
{
if(f != LUA_REFNIL)
lua_rawgeti(L, LUA_REGISTRYINDEX, f);
if(nargs)
lua_insert(L, - (nargs + 1));
if(lua_pcall(L, nargs, nret, 0))
{
lua_rawgeti(L, LUA_REGISTRYINDEX, f);
if(nargs)
lua_insert(L, - (nargs + 1));
if(lua_pcall(L, nargs, nret, 0))
{
warn("error running function: %s",
lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
return true;
warn("error running function: %s",
lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
return false;
return true;
}
/** Print a warning about some Lua code.
@ -378,9 +374,12 @@ bool luaA_isloop(lua_State *, int);
#define hooks_property(c, prop) \
do { \
luaA_client_userdata_new(globalconf.L, c); \
lua_pushliteral(globalconf.L, prop); \
luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
if(globalconf.hooks.property != LUA_REFNIL) \
{ \
luaA_client_userdata_new(globalconf.L, c); \
lua_pushliteral(globalconf.L, prop); \
luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
} \
} while(0);
#endif

View File

@ -560,7 +560,8 @@ mouse_client_move(client_t *c, int snap, bool infobox)
{
client_list_swap(&globalconf.clients, c, target);
globalconf.screens[c->screen].need_arrange = true;
luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
if(globalconf.hooks.clients != LUA_REFNIL)
luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
layout_refresh();
wibox_refresh();
xcb_flush(globalconf.connection);

32
tag.c
View File

@ -100,8 +100,11 @@ tag_append_to_screen(tag_t *tag, screen_t *s)
client_saveprops_tags(c);
/* call hook */
lua_pushnumber(globalconf.L, s->index + 1);
luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
if(globalconf.hooks.tags != LUA_REFNIL)
{
lua_pushnumber(globalconf.L, s->index + 1);
luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
}
}
/** Remove a tag from screen. Tag must be on a screen and have no clients.
@ -132,8 +135,11 @@ tag_remove_from_screen(tag_t *tag)
client_saveprops_tags(c);
/* call hook */
lua_pushnumber(globalconf.L, screen + 1);
luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
if(globalconf.hooks.tags != LUA_REFNIL)
{
lua_pushnumber(globalconf.L, screen + 1);
luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
}
}
/** Tag a client with specified tag.
@ -152,9 +158,12 @@ tag_client(client_t *c, tag_t *t)
client_saveprops_tags(c);
client_need_arrange(c);
/* call hook */
luaA_client_userdata_new(globalconf.L, c);
luaA_tag_userdata_new(globalconf.L, t);
luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
if(globalconf.hooks.tagged != LUA_REFNIL)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_tag_userdata_new(globalconf.L, t);
luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
}
}
/** Untag a client with specified tag.
@ -171,9 +180,12 @@ untag_client(client_t *c, tag_t *t)
client_array_take(&t->clients, i);
client_saveprops_tags(c);
/* call hook */
luaA_client_userdata_new(globalconf.L, c);
luaA_tag_userdata_new(globalconf.L, t);
luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
if(globalconf.hooks.tagged != LUA_REFNIL)
{
luaA_client_userdata_new(globalconf.L, c);
luaA_tag_userdata_new(globalconf.L, t);
luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
}
tag_unref(&t);
return;
}

View File

@ -88,13 +88,23 @@ widget_common_button(widget_node_t *w,
for(int i = 0; i < b->len; i++)
if(ev->detail == b->tab[i]->button
&& XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
{
luaA_wibox_userdata_new(globalconf.L, p);
luaA_dofunction(globalconf.L,
ev->response_type == XCB_BUTTON_PRESS ?
b->tab[i]->press : b->tab[i]->release,
1, 0);
}
switch(ev->response_type)
{
case XCB_BUTTON_PRESS:
if(b->tab[i]->press != LUA_REFNIL)
{
luaA_wibox_userdata_new(globalconf.L, p);
luaA_dofunction(globalconf.L, b->tab[i]->press, 1, 0);
}
break;
case XCB_BUTTON_RELEASE:
if(b->tab[i]->release != LUA_REFNIL)
{
luaA_wibox_userdata_new(globalconf.L, p);
luaA_dofunction(globalconf.L, b->tab[i]->release, 1, 0);
}
break;
}
}
/** Convert a Lua table to a list of widget nodet.