[lua] Add object as argument to mouse click function

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-06-04 19:21:21 +02:00
parent c5237444a9
commit 4c41361d6c
11 changed files with 94 additions and 32 deletions

View file

@ -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", "<bg color=\"#555555\"/> <title/> ")
-- 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/> ")

View file

@ -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 */

21
lua.c
View file

@ -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[],

10
lua.h
View file

@ -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

View file

@ -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.
*/

View file

@ -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)

View file

@ -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 */

View file

@ -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.

View file

@ -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

View file

@ -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;
}
}

View file

@ -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);
}
}