tasklist: use a callback for label

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-07-09 12:12:52 +02:00
parent 5072484f93
commit 3f46ec2152
12 changed files with 62 additions and 81 deletions

View File

@ -91,7 +91,12 @@ mytasklist = widget({ type = "tasklist", name = "mytasklist" })
mytasklist:mouse_add(mouse({ }, 1, function (object, c) c:focus_set(); c:raise() end)) mytasklist:mouse_add(mouse({ }, 1, function (object, c) c:focus_set(); c:raise() end))
mytasklist:mouse_add(mouse({ }, 4, function () awful.client.focus(1) end)) mytasklist:mouse_add(mouse({ }, 4, function () awful.client.focus(1) end))
mytasklist:mouse_add(mouse({ }, 5, function () awful.client.focus(-1) end)) mytasklist:mouse_add(mouse({ }, 5, function () awful.client.focus(-1) end))
mytasklist.text_focus = "<bg color='"..bg_focus.."'/> <span color='"..fg_focus.."'><title/></span> " function mytasklist.label(c)
if client.focus_get() == c then
return " <bg color='"..bg_focus.."'/><span color='"..fg_focus.."'><title/></span> "
end
return " <title/> "
end
-- Create a textbox widget -- Create a textbox widget
mytextbox = widget({ type = "textbox", name = "mytextbox", align = "right" }) mytextbox = widget({ type = "textbox", name = "mytextbox", align = "right" })

View File

@ -166,7 +166,7 @@ client_updatetitle(client_t *c)
/* call hook */ /* call hook */
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.titleupdate, 1); luaA_dofunction(globalconf.L, globalconf.hooks.titleupdate, 1, 0);
widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS); widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
@ -181,7 +181,7 @@ client_unfocus(client_t *c)
{ {
/* Call hook */ /* Call hook */
luaA_client_userdata_new(globalconf.L, globalconf.focus->client); luaA_client_userdata_new(globalconf.L, globalconf.focus->client);
luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1); luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
focus_client_push(NULL); focus_client_push(NULL);
widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS); widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
@ -240,7 +240,7 @@ client_focus(client_t *c, int screen)
/* execute hook */ /* execute hook */
luaA_client_userdata_new(globalconf.L, globalconf.focus->client); luaA_client_userdata_new(globalconf.L, globalconf.focus->client);
luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1); luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
} }
else else
{ {
@ -433,7 +433,7 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int screen)
/* call hook */ /* call hook */
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.manage, 1); luaA_dofunction(globalconf.L, globalconf.hooks.manage, 1, 0);
if(c->floating_placement if(c->floating_placement
&& !retloadprops && !retloadprops
@ -668,7 +668,7 @@ client_unmanage(client_t *c)
/* call hook */ /* call hook */
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1); luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
/* The server grab construct avoids race conditions. */ /* The server grab construct avoids race conditions. */
xcb_grab_server(globalconf.connection); xcb_grab_server(globalconf.connection);
@ -723,7 +723,7 @@ client_updatewmhints(client_t *c)
{ {
/* execute hook */ /* execute hook */
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.urgent, 1); luaA_dofunction(globalconf.L, globalconf.hooks.urgent, 1, 0);
widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS); widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
} }

View File

@ -27,6 +27,7 @@ hide
honorsizehints honorsizehints
icon_path icon_path
image image
label
layout layout
left left
line line

View File

@ -59,10 +59,10 @@ event_handle_mouse_button_press(client_t *c,
if(c) if(c)
{ {
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, b->fct, 1); luaA_dofunction(globalconf.L, b->fct, 1, 0);
} }
else else
luaA_dofunction(globalconf.L, b->fct, 0); luaA_dofunction(globalconf.L, b->fct, 0, 0);
} }
} }
@ -341,7 +341,7 @@ event_handle_enternotify(void *data __attribute__ ((unused)),
globalconf.pointer_y = ev->root_y; globalconf.pointer_y = ev->root_y;
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.mouseover, 1); luaA_dofunction(globalconf.L, globalconf.hooks.mouseover, 1, 0);
} }
else if((emwin = xembed_getbywin(globalconf.embedded, ev->event))) else if((emwin = xembed_getbywin(globalconf.embedded, ev->event)))
xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY,
@ -414,7 +414,7 @@ event_handle_keypress(void *data __attribute__ ((unused)),
{ {
keybinding_t *k = keybinding_find(ev); keybinding_t *k = keybinding_find(ev);
if (k && k->fct != LUA_REFNIL) if (k && k->fct != LUA_REFNIL)
luaA_dofunction(globalconf.L, k->fct, 0); luaA_dofunction(globalconf.L, k->fct, 0, 0);
} }
return 0; return 0;

2
ewmh.c
View File

@ -352,7 +352,7 @@ ewmh_process_state_atom(client_t *c, xcb_atom_t state, int set)
c->isurgent = true; c->isurgent = true;
/* execute hook */ /* execute hook */
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, globalconf.hooks.urgent, 1); luaA_dofunction(globalconf.L, globalconf.hooks.urgent, 1, 0);
widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS); widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
} }
} }

View File

@ -87,7 +87,7 @@ arrange(int screen)
/* call hook */ /* call hook */
lua_pushnumber(globalconf.L, screen + 1); lua_pushnumber(globalconf.L, screen + 1);
luaA_dofunction(globalconf.L, globalconf.hooks.arrange, 1); luaA_dofunction(globalconf.L, globalconf.hooks.arrange, 1, 0);
} }
/** Refresh the screen disposition /** Refresh the screen disposition

12
lua.c
View File

@ -208,16 +208,6 @@ luaA_screen_coords_get(lua_State *L)
return 1; return 1;
} }
static int
luaA_registerfct(lua_State *L, luaA_function *fct)
{
luaA_checkfunction(L, -1);
if(*fct != LUA_REFNIL)
luaL_unref(L, LUA_REGISTRYINDEX, *fct);
*fct = luaL_ref(L, LUA_REGISTRYINDEX);
return 0;
}
/** Set the function called each time a client gets focus. This function is /** Set the function called each time a client gets focus. This function is
* called with the client object as argument. * called with the client object as argument.
* \param L The Lua VM state. * \param L The Lua VM state.
@ -693,7 +683,7 @@ luaA_cs_cleanup(void)
void void
luaA_on_timer(EV_P_ ev_timer *w, int revents) luaA_on_timer(EV_P_ ev_timer *w, int revents)
{ {
luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0); luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
} }
void void

14
lua.h
View File

@ -77,14 +77,14 @@ typedef int luaA_function;
lua_tostring(L, -1)); \ lua_tostring(L, -1)); \
} while(0) } while(0)
#define luaA_dofunction(L, f, n) \ #define luaA_dofunction(L, f, n, r) \
do { \ do { \
if(f) \ if(f) \
{ \ { \
lua_rawgeti(L, LUA_REGISTRYINDEX, f); \ lua_rawgeti(L, LUA_REGISTRYINDEX, f); \
if(n) \ if(n) \
lua_insert(L, - (n + 1)); \ lua_insert(L, - (n + 1)); \
if(lua_pcall(L, n, 0, 0)) \ if(lua_pcall(L, n, r, 0)) \
warn("error running function: %s", \ warn("error running function: %s", \
lua_tostring(L, -1)); \ lua_tostring(L, -1)); \
} \ } \
@ -189,6 +189,16 @@ luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
return 0; return 0;
} }
static inline int
luaA_registerfct(lua_State *L, luaA_function *fct)
{
luaA_checkfunction(L, -1);
if(*fct != LUA_REFNIL)
luaL_unref(L, LUA_REGISTRYINDEX, *fct);
*fct = luaL_ref(L, LUA_REGISTRYINDEX);
return 0;
}
void luaA_init(void); void luaA_init(void);
void luaA_parserc(const char *); void luaA_parserc(const char *);
void luaA_pushpointer(lua_State *, void *, awesome_type_t); void luaA_pushpointer(lua_State *, void *, awesome_type_t);

View File

@ -75,7 +75,7 @@ widget_common_button_press(widget_node_t *w,
if(ev->detail == b->button && CLEANMASK(ev->state) == b->mod && b->fct) if(ev->detail == b->button && CLEANMASK(ev->state) == b->mod && b->fct)
{ {
luaA_pushpointer(globalconf.L, p, type); luaA_pushpointer(globalconf.L, p, type);
luaA_dofunction(globalconf.L, b->fct, 1); luaA_dofunction(globalconf.L, b->fct, 1, 0);
} }
} }

View File

@ -283,7 +283,7 @@ taglist_button_press(widget_node_t *w,
{ {
luaA_pushpointer(globalconf.L, object, type); luaA_pushpointer(globalconf.L, object, type);
luaA_tag_userdata_new(globalconf.L, tag); luaA_tag_userdata_new(globalconf.L, tag);
luaA_dofunction(globalconf.L, b->fct, 2); luaA_dofunction(globalconf.L, b->fct, 2, 0);
return; return;
} }
} }

View File

@ -41,7 +41,7 @@ typedef struct
{ {
showclient_t show; showclient_t show;
bool show_icons; bool show_icons;
char *text_normal, *text_urgent, *text_focus; luaA_function label;
} tasklist_data_t; } tasklist_data_t;
struct tasklist_hook_data struct tasklist_hook_data
@ -115,9 +115,11 @@ tasklist_draw(draw_context_t *ctx, int screen,
tasklist_data_t *d = w->widget->data; tasklist_data_t *d = w->widget->data;
area_t area; area_t area;
char *text; char *text;
const char *buf;
int n = 0, i = 0, box_width = 0, icon_width = 0, box_width_rest = 0; int n = 0, i = 0, box_width = 0, icon_width = 0, box_width_rest = 0;
netwm_icon_t *icon; netwm_icon_t *icon;
draw_image_t *image; draw_image_t *image;
size_t len;
if(used >= ctx->width) if(used >= ctx->width)
return (w->area.width = 0); return (w->area.width = 0);
@ -143,14 +145,21 @@ tasklist_draw(draw_context_t *ctx, int screen,
{ {
icon_width = 0; icon_width = 0;
if(globalconf.focus->client == c) /* push client */
text = d->text_focus; luaA_client_userdata_new(globalconf.L, c);
else if(c->isurgent) /* call label function with client as argument and wait for one
text = d->text_urgent; * result */
else luaA_dofunction(globalconf.L, d->label, 1, 1);
text = d->text_normal;
text = client_markup_parse(c, text, a_strlen(text)); if(lua_isstring(globalconf.L, -1))
{
buf = lua_tolstring(globalconf.L, -1, &len);
text = client_markup_parse(c, buf, len);
}
else
text = NULL;
lua_pop(globalconf.L, 1);
if(d->show_icons) if(d->show_icons)
{ {
@ -209,12 +218,6 @@ tasklist_draw(draw_context_t *ctx, int screen,
draw_text(ctx, globalconf.font, area, text, NULL); draw_text(ctx, globalconf.font, area, text, NULL);
p_delete(&text); p_delete(&text);
if(c->isfloating || c->ismax)
draw_circle(ctx, w->area.x + icon_width + box_width * i,
w->area.y,
(globalconf.font->height + 2) / 4,
c->ismax, &ctx->fg);
i++; i++;
} }
@ -270,7 +273,7 @@ tasklist_button_press(widget_node_t *w,
{ {
luaA_pushpointer(globalconf.L, object, type); luaA_pushpointer(globalconf.L, object, type);
luaA_client_userdata_new(globalconf.L, c); luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, b->fct, 2); luaA_dofunction(globalconf.L, b->fct, 2, 0);
} }
} }
@ -287,15 +290,6 @@ luaA_tasklist_index(lua_State *L, awesome_token_t token)
switch(token) switch(token)
{ {
case A_TK_TEXT_NORMAL:
lua_pushstring(L, d->text_normal);
return 1;
case A_TK_TEXT_FOCUS:
lua_pushstring(L, d->text_focus);
return 1;
case A_TK_TEXT_URGENT:
lua_pushstring(L, d->text_urgent);
return 1;
case A_TK_SHOW_ICONS: case A_TK_SHOW_ICONS:
lua_pushboolean(L, d->show_icons); lua_pushboolean(L, d->show_icons);
return 1; return 1;
@ -315,6 +309,9 @@ luaA_tasklist_index(lua_State *L, awesome_token_t token)
return 0; return 0;
} }
return 1; return 1;
case A_TK_LABEL:
lua_rawgeti(L, LUA_REGISTRYINDEX, d->label);
return 1;
default: default:
return 0; return 0;
} }
@ -335,27 +332,6 @@ luaA_tasklist_newindex(lua_State *L, awesome_token_t token)
switch(token) switch(token)
{ {
case A_TK_TEXT_NORMAL:
if((buf = luaL_checkstring(L, 3)))
{
p_delete(&d->text_normal);
d->text_normal = a_strdup(buf);
}
break;
case A_TK_TEXT_FOCUS:
if((buf = luaL_checkstring(L, 3)))
{
p_delete(&d->text_focus);
d->text_focus = a_strdup(buf);
}
break;
case A_TK_TEXT_URGENT:
if((buf = luaL_checkstring(L, 3)))
{
p_delete(&d->text_urgent);
d->text_urgent = a_strdup(buf);
}
break;
case A_TK_SHOW_ICONS: case A_TK_SHOW_ICONS:
d->show_icons = luaA_checkboolean(L, 3); d->show_icons = luaA_checkboolean(L, 3);
break; break;
@ -376,6 +352,9 @@ luaA_tasklist_newindex(lua_State *L, awesome_token_t token)
break; break;
} }
break; break;
case A_TK_LABEL:
luaA_registerfct(L, &d->label);
break;
default: default:
return 0; return 0;
} }
@ -392,10 +371,6 @@ static void
tasklist_destructor(widget_t *widget) tasklist_destructor(widget_t *widget)
{ {
tasklist_data_t *d = widget->data; tasklist_data_t *d = widget->data;
p_delete(&d->text_normal);
p_delete(&d->text_focus);
p_delete(&d->text_urgent);
p_delete(&d); p_delete(&d);
} }
@ -418,12 +393,10 @@ tasklist_new(alignment_t align __attribute__ ((unused)))
w->newindex = luaA_tasklist_newindex; w->newindex = luaA_tasklist_newindex;
w->data = d = p_new(tasklist_data_t, 1); w->data = d = p_new(tasklist_data_t, 1);
w->destructor = tasklist_destructor; w->destructor = tasklist_destructor;
d->text_normal = a_strdup(" <title/> ");
d->text_focus = a_strdup(" <title/> ");
d->text_urgent = a_strdup(" <title/> ");
d->show_icons = true; d->show_icons = true;
d->show = ShowTags; d->show = ShowTags;
d->label = LUA_REFNIL;
/* Set cache property */ /* Set cache property */
w->cache_flags = WIDGET_CACHE_CLIENTS; w->cache_flags = WIDGET_CACHE_CLIENTS;

View File

@ -60,8 +60,10 @@ textbox_draw(draw_context_t *ctx, int screen __attribute__ ((unused)),
draw_parser_data_init(&pdata); draw_parser_data_init(&pdata);
w->area.width = draw_text_extents(ctx->connection, ctx->phys_screen, w->area.width = draw_text_extents(ctx->connection, ctx->phys_screen,
globalconf.font, d->text, &pdata).width; globalconf.font, d->text, &pdata).width;
if(w->area.width > ctx->width - used) if(w->area.width > ctx->width - used)
w->area.width = ctx->width - used; w->area.width = ctx->width - used;
if(pdata.bg_image) if(pdata.bg_image)
w->area.width = MAX(w->area.width, pdata.bg_resize ? w->area.height : pdata.bg_image->width); w->area.width = MAX(w->area.width, pdata.bg_resize ? w->area.height : pdata.bg_image->width);