awesome/widgets/tasklist.c

314 lines
9.2 KiB
C
Raw Normal View History

2008-01-03 12:39:28 +01:00
/*
* tasklist.c - task list widget
*
* Copyright © 2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "widget.h"
#include "client.h"
#include "focus.h"
#include "screen.h"
#include "event.h"
#include "ewmh.h"
#include "tag.h"
#include "common/configopts.h"
#include "common/markup.h"
2008-01-03 12:39:28 +01:00
extern awesome_t globalconf;
2008-01-03 12:39:28 +01:00
typedef enum
{
ShowFocus,
ShowTags,
ShowAll,
} showclient_t;
2008-01-03 12:39:28 +01:00
typedef struct
{
showclient_t show;
2008-03-21 16:50:17 +01:00
bool show_icons;
char *text_normal, *text_urgent, *text_focus;
} tasklist_data_t;
2008-01-03 12:39:28 +01:00
2008-03-21 16:50:17 +01:00
static inline bool
tasklist_isvisible(client_t *c, int screen, showclient_t show)
{
2008-02-12 10:29:41 +01:00
if(c->skip || c->skiptb)
2008-03-21 16:50:17 +01:00
return false;
switch(show)
{
case ShowAll:
return (c->screen == screen);
case ShowTags:
return client_isvisible(c, screen);
case ShowFocus:
return (c == focus_get_current_client(screen));
}
2008-03-21 16:50:17 +01:00
return false;
}
2008-01-03 12:39:28 +01:00
static int
tasklist_draw(draw_context_t *ctx, int screen,
widget_node_t *w,
int offset, int used, void *q __attribute__ ((unused)))
2008-01-03 12:39:28 +01:00
{
client_t *c;
tasklist_data_t *d = w->widget->data;
2008-03-14 09:37:25 +01:00
area_t area;
char *text;
int n = 0, i = 0, box_width = 0, icon_width = 0, box_width_rest = 0, j = 0;
netwm_icon_t *icon;
markup_parser_data_t p;
const char *elements[] = { "bg", NULL };
xcolor_t bg_color;
draw_image_t *image;
2008-01-03 12:39:28 +01:00
if(used >= ctx->width)
return (w->area.width = 0);
2008-01-22 17:52:12 +01:00
2008-01-03 12:39:28 +01:00
for(c = globalconf.clients; c; c = c->next)
if(tasklist_isvisible(c, screen, d->show))
2008-01-03 12:39:28 +01:00
n++;
2008-01-03 12:39:28 +01:00
if(!n)
return (w->area.width = 0);
2008-01-03 12:39:28 +01:00
box_width = (ctx->width - used) / n;
2008-01-20 16:25:06 +01:00
/* compute how many pixel we left empty */
box_width_rest = (ctx->width - used) % n;
2008-01-03 12:39:28 +01:00
w->area.x = widget_calculate_offset(ctx->width,
0, offset, w->widget->align);
w->area.y = 0;
2008-01-03 12:39:28 +01:00
for(c = globalconf.clients; c; c = c->next)
if(tasklist_isvisible(c, screen, d->show))
2008-01-03 12:39:28 +01:00
{
icon_width = 0;
if(globalconf.focus->client == c)
text = d->text_focus;
else if(c->isurgent)
text = d->text_urgent;
else
text = d->text_normal;
text = client_markup_parse(c, text, a_strlen(text));
if(d->show_icons)
{
/* draw a background for icons */
area.x = w->area.x + box_width * i;
area.y = w->area.y;
area.height = ctx->height;
area.width = box_width;
/* Actually look for the proper background color, since
* otherwise the background statusbar color is used instead */
markup_parser_data_init(&p, elements, NULL, countof(elements));
if(markup_parse(&p, text, a_strlen(text)) && p.attribute_names[0])
for(j = 0; p.attribute_names[0][j]; j++)
if(!a_strcmp(p.attribute_names[0][j], "color"))
{
xcolor_new(ctx->connection, ctx->phys_screen, p.attribute_values[0][j], &bg_color);
draw_rectangle(ctx, area, 1.0, true, bg_color);
break;
}
markup_parser_data_wipe(&p);
if((image = draw_image_new(c->icon_path)))
2008-01-12 22:59:13 +01:00
{
icon_width = ((double) ctx->height / (double) image->height) * image->width;
draw_image(ctx, w->area.x + box_width * i,
w->area.y, ctx->height, image);
draw_image_delete(&image);
2008-01-12 22:59:13 +01:00
}
if(!icon_width && (icon = ewmh_get_window_icon(c->win)))
{
icon_width = ((double) ctx->height / (double) icon->height)
* icon->width;
draw_image_from_argb_data(ctx,
w->area.x + box_width * i,
w->area.y,
icon->width, icon->height,
ctx->height, icon->image);
2008-01-04 15:59:17 +01:00
p_delete(&icon->image);
p_delete(&icon);
}
}
area.x = w->area.x + icon_width + box_width * i;
area.y = w->area.y;
2008-01-12 23:47:03 +01:00
area.width = box_width - icon_width;
area.height = ctx->height;
2008-01-20 16:25:06 +01:00
/* if we're on last elem, it has the last pixels left */
if(i == n - 1)
area.width += box_width_rest;
draw_text(ctx, globalconf.font,
area, text);
2008-01-12 23:47:03 +01:00
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);
2008-01-03 12:39:28 +01:00
i++;
}
w->area.width = ctx->width - used;
w->area.height = ctx->height;
2008-01-03 12:39:28 +01:00
return w->area.width;
2008-01-03 12:39:28 +01:00
}
/** 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.
*/
2008-01-03 12:39:28 +01:00
static void
tasklist_button_press(widget_node_t *w,
xcb_button_press_event_t *ev,
int screen,
void *object,
awesome_type_t type)
2008-01-03 12:39:28 +01:00
{
button_t *b;
client_t *c;
tasklist_data_t *d = w->widget->data;
int n = 0, box_width = 0, i, ci = 0;
2008-01-03 12:39:28 +01:00
for(c = globalconf.clients; c; c = c->next)
if(tasklist_isvisible(c, screen, d->show))
n++;
if(!n)
return;
2008-01-03 12:39:28 +01:00
box_width = w->area.width / n;
2008-01-03 12:39:28 +01:00
ci = (ev->event_x - w->area.x) / box_width;
/* found first visible client */
for(c = globalconf.clients;
c && !tasklist_isvisible(c, screen, d->show);
c = c->next);
/* found ci-th visible client */
for(i = 0; c ; c = c->next)
if(tasklist_isvisible(c, screen, d->show))
if(i++ >= ci)
2008-01-04 19:12:07 +01:00
break;
if(c)
for(b = w->widget->buttons; b; b = b->next)
if(ev->detail == b->button && CLEANMASK(ev->state) == b->mod && b->fct)
{
luaA_pushpointer(globalconf.L, object, type);
luaA_client_userdata_new(globalconf.L, c);
luaA_dofunction(globalconf.L, b->fct, 2);
}
}
2008-01-07 11:20:24 +01:00
static widget_tell_status_t
tasklist_tell(widget_t *widget, const char *property, const char *new_value)
{
tasklist_data_t *d = widget->data;
if(!a_strcmp(property, "text_normal"))
{
p_delete(&d->text_normal);
d->text_normal = a_strdup(new_value);
2008-01-03 12:39:28 +01:00
}
else if(!a_strcmp(property, "text_focus"))
{
p_delete(&d->text_focus);
d->text_focus = a_strdup(new_value);
}
else if(!a_strcmp(property, "text_urgent"))
{
p_delete(&d->text_urgent);
d->text_urgent = a_strdup(new_value);
}
else if(!a_strcmp(property, "show_icons"))
d->show_icons = a_strtobool(new_value);
else if(!a_strcmp(property, "show"))
{
if(!a_strcmp(new_value, "tags"))
d->show = ShowTags;
else if(!a_strcmp(new_value, "focus"))
d->show = ShowFocus;
else if(!a_strcmp(new_value, "all"))
d->show = ShowAll;
else
return WIDGET_ERROR;
}
else
return WIDGET_ERROR;
2008-01-03 12:39:28 +01:00
return WIDGET_NOERROR;
2008-01-03 12:39:28 +01:00
}
static void
tasklist_destructor(widget_t *widget)
{
tasklist_data_t *d = widget->data;
p_delete(&d->text_normal);
p_delete(&d->text_focus);
p_delete(&d->text_urgent);
p_delete(&d);
}
widget_t *
tasklist_new(alignment_t align __attribute__ ((unused)))
2008-01-03 12:39:28 +01:00
{
widget_t *w;
tasklist_data_t *d;
2008-01-03 12:39:28 +01:00
w = p_new(widget_t, 1);
widget_common_new(w);
2008-01-03 12:39:28 +01:00
w->draw = tasklist_draw;
w->button_press = tasklist_button_press;
w->align = AlignFlex;
w->data = d = p_new(tasklist_data_t, 1);
w->tell = tasklist_tell;
w->destructor = tasklist_destructor;
2008-01-03 12:39:28 +01:00
d->text_normal = a_strdup(" <title/> ");
d->text_focus = a_strdup(" <title/> ");
d->text_urgent = a_strdup(" <title/> ");
d->show_icons = true;
d->show = ShowTags;
2008-01-03 16:05:39 +01:00
2008-01-07 18:12:38 +01:00
/* Set cache property */
w->cache_flags = WIDGET_CACHE_CLIENTS;
2008-01-07 18:12:38 +01:00
2008-01-03 12:39:28 +01:00
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80