From 4c41361d6c7dfd8c8109568af274ac9d66f04a23 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 4 Jun 2008 19:21:21 +0200 Subject: [PATCH] [lua] Add object as argument to mouse click function Signed-off-by: Julien Danjou --- awesomerc.lua.in | 4 ++-- event.c | 6 ++++-- lua.c | 21 +++++++++++++++++++++ lua.h | 10 +++++++++- statusbar.c | 12 ++++++++++++ statusbar.h | 2 ++ structs.h | 2 +- titlebar.c | 32 +++++++++++++------------------- widget.c | 11 ++++++++--- widgets/taglist.c | 13 +++++++++++-- widgets/tasklist.c | 13 +++++++++++-- 11 files changed, 94 insertions(+), 32 deletions(-) diff --git a/awesomerc.lua.in b/awesomerc.lua.in index 9310cbb6..270efe96 100644 --- a/awesomerc.lua.in +++ b/awesomerc.lua.in @@ -44,7 +44,7 @@ mytaglist = widget.new({ type = "taglist", name = "mytaglist" }) mytaglist:mouse({}, 1, awful.tag.viewonly) mytaglist:mouse({ modkey }, 1, awful.client.toggletag) mytaglist:mouse({}, 3, - function (tag) + function (object, tag) tag:view(not tag:isselected()) end) mytaglist:mouse({ modkey }, 3, awful.client.toggletag) @@ -54,7 +54,7 @@ mytaglist:set("text_focus", " ") -- Create a tasklist widget mytasklist = widget.new({ type = "tasklist", name = "mytasklist" }) -mytasklist:mouse({ }, 1, function (c) c:focus_set(); c:raise() end) +mytasklist:mouse({ }, 1, function (object, c) c:focus_set(); c:raise() end) mytasklist:mouse({ }, 4, function () awful.client.focus(1) end) mytasklist:mouse({ }, 5, function () awful.client.focus(-1) end) mytasklist:set("text_focus", "<bg color=\"#555555\"/> <title/> ") diff --git a/event.c b/event.c index 47931c1c..25742091 100644 --- a/event.c +++ b/event.c @@ -115,7 +115,8 @@ event_handle_buttonpress(void *data __attribute__ ((unused)), if(ev->event_x >= w->area.x && ev->event_x < w->area.x + w->area.width && ev->event_y >= w->area.y && ev->event_y < w->area.y + w->area.height) { - w->widget->button_press(w, ev, statusbar->screen, statusbar); + w->widget->button_press(w, ev, statusbar->screen, + statusbar, AWESOME_TYPE_STATUSBAR); return 0; } /* return if no widget match */ @@ -145,7 +146,8 @@ event_handle_buttonpress(void *data __attribute__ ((unused)), if(ev->event_x >= w->area.x && ev->event_x < w->area.x + w->area.width && ev->event_y >= w->area.y && ev->event_y < w->area.y + w->area.height) { - w->widget->button_press(w, ev, c->screen, c->titlebar); + w->widget->button_press(w, ev, c->screen, + c->titlebar, AWESOME_TYPE_TITLEBAR); return 0; } /* return if no widget match */ diff --git a/lua.c b/lua.c index 9e241a30..eee36ee3 100644 --- a/lua.c +++ b/lua.c @@ -35,6 +35,8 @@ #include "tag.h" #include "client.h" #include "window.h" +#include "statusbar.h" +#include "titlebar.h" #include "layouts/tile.h" extern awesome_t globalconf; @@ -367,6 +369,25 @@ luaA_colors_set(lua_State *L) return 0; } +/** Push a pointer onto the stack according to its type. + * \param L The Lua state. + * \param p The pointer. + * \param type Its type. + */ +void +luaA_pushpointer(void *p, awesome_type_t type) +{ + switch(type) + { + case AWESOME_TYPE_STATUSBAR: + luaA_statusbar_userdata_new(p); + break; + case AWESOME_TYPE_TITLEBAR: + luaA_titlebar_userdata_new(p); + break; + } +} + static void luaA_openlib(lua_State *L, const char *name, const struct luaL_reg methods[], diff --git a/lua.h b/lua.h index e37cc107..2e035f97 100644 --- a/lua.h +++ b/lua.h @@ -27,6 +27,13 @@ #include "common/util.h" +/** Object types */ +typedef enum +{ + AWESOME_TYPE_STATUSBAR = 1, + AWESOME_TYPE_TITLEBAR +} awesome_type_t; + /** Type for Lua function */ typedef int luaA_function; @@ -126,8 +133,9 @@ luaA_checkboolean(lua_State *L, int n) return lua_toboolean(L, n); } - bool luaA_parserc(const char *); void luaA_docmd(char *); +void luaA_pushpointer(void *, awesome_type_t); + #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/statusbar.c b/statusbar.c index 84af50ce..ea9f1265 100644 --- a/statusbar.c +++ b/statusbar.c @@ -473,6 +473,18 @@ luaA_statusbar_new(lua_State *L) return luaA_settype(L, "statusbar"); } +/** Create a new statusbar userdata. + * \param t The statusbar. + */ +int +luaA_statusbar_userdata_new(statusbar_t *t) +{ + statusbar_t **sb = lua_newuserdata(globalconf.L, sizeof(statusbar_t *)); + *sb = t; + statusbar_ref(sb); + return luaA_settype(globalconf.L, "statusbar"); +} + /** Get all widget from a statusbar. * \return A table with all widgets from the statusbar. */ diff --git a/statusbar.h b/statusbar.h index 3c5ef650..580c8b34 100644 --- a/statusbar.h +++ b/statusbar.h @@ -37,6 +37,8 @@ statusbar_delete(statusbar_t **statusbar) void statusbar_refresh(void); +int luaA_statusbar_userdata_new(statusbar_t *); + DO_RCNT(statusbar_t, statusbar, statusbar_delete) DO_SLIST(statusbar_t, statusbar, statusbar_delete) diff --git a/structs.h b/structs.h index 8dec9f35..bf061e04 100644 --- a/structs.h +++ b/structs.h @@ -99,7 +99,7 @@ struct widget_t /** Update function */ widget_tell_status_t (*tell)(widget_t *, const char *, const char *); /** ButtonPressedEvent handler */ - void (*button_press)(widget_node_t *, xcb_button_press_event_t *, int, void *); + void (*button_press)(widget_node_t *, xcb_button_press_event_t *, int, void *, awesome_type_t); /** Alignement */ alignment_t align; /** Misc private data */ diff --git a/titlebar.c b/titlebar.c index ff2ab574..324e2db1 100644 --- a/titlebar.c +++ b/titlebar.c @@ -406,44 +406,38 @@ titlebar_init(client_t *c) static int luaA_titlebar_new(lua_State *L) { - titlebar_t **tb; - int objpos; + titlebar_t *tb; const char *color; luaA_checktable(L, 1); - tb = lua_newuserdata(L, sizeof(titlebar_t *)); - *tb = p_new(titlebar_t, 1); - objpos = lua_gettop(L); + tb = p_new(titlebar_t, 1); - (*tb)->align = draw_align_get_from_str(luaA_getopt_string(L, 1, "align", "left")); + tb->align = draw_align_get_from_str(luaA_getopt_string(L, 1, "align", "left")); - (*tb)->width = luaA_getopt_number(L, 1, "width", 0); - (*tb)->height = luaA_getopt_number(L, 1, "height", 0); - if((*tb)->height <= 0) + tb->width = luaA_getopt_number(L, 1, "width", 0); + tb->height = luaA_getopt_number(L, 1, "height", 0); + if(tb->height <= 0) /* 1.5 as default factor, it fits nice but no one knows why */ - (*tb)->height = 1.5 * globalconf.font->height; + tb->height = 1.5 * globalconf.font->height; - (*tb)->position = position_get_from_str(luaA_getopt_string(L, 1, "position", "top")); + tb->position = position_get_from_str(luaA_getopt_string(L, 1, "position", "top")); lua_getfield(L, 1, "fg"); if((color = luaL_optstring(L, -1, NULL))) xcolor_new(globalconf.connection, globalconf.default_screen, - color, &(*tb)->colors.fg); + color, &tb->colors.fg); else - (*tb)->colors.fg = globalconf.colors.fg; + tb->colors.fg = globalconf.colors.fg; lua_getfield(L, 1, "bg"); if((color = luaL_optstring(L, -1, NULL))) xcolor_new(globalconf.connection, globalconf.default_screen, - color, &(*tb)->colors.bg); + color, &tb->colors.bg); else - (*tb)->colors.bg = globalconf.colors.bg; + tb->colors.bg = globalconf.colors.bg; - titlebar_ref(tb); - - lua_pushvalue(L, objpos); - return luaA_settype(L, "titlebar"); + return luaA_titlebar_userdata_new(tb); } /** Add a widget to a titlebar. diff --git a/widget.c b/widget.c index 1739b14c..e4973a03 100644 --- a/widget.c +++ b/widget.c @@ -80,18 +80,23 @@ widget_getbyname(const char *name) * \param ev The button press event the widget received. * \param screen The screen number. * \param p The object where user clicked. + * \param type The object type. */ static void widget_common_button_press(widget_node_t *w, xcb_button_press_event_t *ev, int screen __attribute__ ((unused)), - void *p __attribute__ ((unused))) + void *p, + awesome_type_t type) { button_t *b; - + for(b = w->widget->buttons; b; b = b->next) if(ev->detail == b->button && CLEANMASK(ev->state) == b->mod && b->fct) - luaA_dofunction(globalconf.L, b->fct, 0); + { + luaA_pushpointer(p, type); + luaA_dofunction(globalconf.L, b->fct, 1); + } } /** Common tell function for widget, which only warn user that widget diff --git a/widgets/taglist.c b/widgets/taglist.c index ab309d65..402fd284 100644 --- a/widgets/taglist.c +++ b/widgets/taglist.c @@ -190,11 +190,19 @@ taglist_draw(draw_context_t *ctx, int screen, widget_node_t *w, return w->area.width; } +/** Handle button click on tasklist. + * \param w The widget node. + * \param ev The button press event. + * \param screen The screen where the click was. + * \param object The object we're onto. + * \param type The type object. + */ static void taglist_button_press(widget_node_t *w, xcb_button_press_event_t *ev, int screen, - void *object) + void *object, + awesome_type_t type) { screen_t *vscreen = &globalconf.screens[screen]; button_t *b; @@ -213,8 +221,9 @@ taglist_button_press(widget_node_t *w, if(ev->event_x >= AREA_LEFT(*area) && ev->event_x < AREA_RIGHT(*area)) { + luaA_pushpointer(object, type); luaA_tag_userdata_new(tag); - luaA_dofunction(globalconf.L, b->fct, 1); + luaA_dofunction(globalconf.L, b->fct, 2); return; } } diff --git a/widgets/tasklist.c b/widgets/tasklist.c index 736971a3..ef202143 100644 --- a/widgets/tasklist.c +++ b/widgets/tasklist.c @@ -171,11 +171,19 @@ tasklist_draw(draw_context_t *ctx, int screen, return w->area.width; } +/** Handle button click on tasklist. + * \param w The widget node. + * \param ev The button press event. + * \param screen The screen where the click was. + * \param object The object we're onto. + * \param type The type object. + */ static void tasklist_button_press(widget_node_t *w, xcb_button_press_event_t *ev, int screen, - void *p __attribute__ ((unused))) + void *object, + awesome_type_t type) { button_t *b; client_t *c; @@ -207,8 +215,9 @@ tasklist_button_press(widget_node_t *w, for(b = w->widget->buttons; b; b = b->next) if(ev->detail == b->button && CLEANMASK(ev->state) == b->mod && b->fct) { + luaA_pushpointer(object, type); luaA_client_userdata_new(c); - luaA_dofunction(globalconf.L, b->fct, 1); + luaA_dofunction(globalconf.L, b->fct, 2); } }