Widget-ise the statusbar.

We now have the beginnings of a flexible widget structure for the statusbar.
For now, there is no behavioural change, and the interface is a bit crude, but
watch this space!
This commit is contained in:
Aldo Cortesi 2007-12-15 17:53:53 +11:00 committed by Julien Danjou
parent c81b5cfc55
commit 76ddf235ac
10 changed files with 308 additions and 152 deletions

View File

@ -3,8 +3,8 @@
include config.mk include config.mk
SRC = focus.c client.c draw.c event.c layout.c awesome.c tag.c util.c xutil.c config.c screen.c statusbar.c uicb.c window.c rules.c mouse.c awesome-client-common.c SRC = focus.c client.c draw.c event.c layout.c awesome.c tag.c util.c xutil.c config.c screen.c statusbar.c uicb.c window.c rules.c mouse.c awesome-client-common.c widget.c
OBJ = ${SRC:.c=.o} ${LAYOUTS:.c=.o} OBJ = ${SRC:.c=.o} ${LAYOUTS:.c=.o} ${WIDGETS:.c=.o}
DOCS = awesome.1.txt awesome-client.1.txt awesomerc.1.txt DOCS = awesome.1.txt awesome-client.1.txt awesomerc.1.txt
SRCCLIENT = awesome-client.c awesome-client-common.c util.c SRCCLIENT = awesome-client.c awesome-client-common.c util.c
@ -15,6 +15,7 @@ all: options awesome awesome-client
options: options:
@echo awesome build options: @echo awesome build options:
@echo "LAYOUTS = ${LAYOUTS}" @echo "LAYOUTS = ${LAYOUTS}"
@echo "WIDGETS = ${WIDGETS}"
@echo "CFLAGS = ${CFLAGS}" @echo "CFLAGS = ${CFLAGS}"
@echo "LDFLAGS = ${LDFLAGS}" @echo "LDFLAGS = ${LDFLAGS}"
@echo "CC = ${CC}" @echo "CC = ${CC}"
@ -59,10 +60,12 @@ dist: clean
@echo creating dist tarball @echo creating dist tarball
@mkdir awesome-${VERSION} @mkdir awesome-${VERSION}
@mkdir awesome-${VERSION}/layouts @mkdir awesome-${VERSION}/layouts
@mkdir awesome-${VERSION}/widgets
@cp -fR STYLE LICENSE AUTHORS Makefile README awesomerc config.mk \ @cp -fR STYLE LICENSE AUTHORS Makefile README awesomerc config.mk \
awesome-client.1.txt awesome.1.txt awesomerc.1.txt ${SRCCLIENT} ${SRCCLIENT:.c=.h} ${SRC} ${SRC:.c=.h} \ awesome-client.1.txt awesome.1.txt awesomerc.1.txt ${SRCCLIENT} ${SRCCLIENT:.c=.h} ${SRC} ${SRC:.c=.h} \
common.h awesome-${VERSION} || true common.h awesome-${VERSION} || true
@cp -R ${LAYOUTS} ${LAYOUTS:.c=.h} awesome-${VERSION}/layouts @cp -R ${LAYOUTS} ${LAYOUTS:.c=.h} awesome-${VERSION}/layouts
@cp -R ${WIDGETS} ${WIDGETS:.c=.h} awesome-${VERSION}/layouts
@tar -cf awesome-${VERSION}.tar awesome-${VERSION} @tar -cf awesome-${VERSION}.tar awesome-${VERSION}
@gzip -9 awesome-${VERSION}.tar @gzip -9 awesome-${VERSION}.tar
@rm -rf awesome-${VERSION} @rm -rf awesome-${VERSION}

View File

@ -25,6 +25,7 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include <regex.h> #include <regex.h>
#include <draw.h>
/** Bar possible position */ /** Bar possible position */
enum enum
@ -79,6 +80,7 @@ struct Button
}; };
/** Status bar */ /** Status bar */
typedef struct Widget Widget;
typedef struct typedef struct
{ {
/** Bar width */ /** Bar width */
@ -95,6 +97,8 @@ typedef struct
Window window; Window window;
/** Screen */ /** Screen */
int screen; int screen;
/** Screen */
Widget *widgets;
} Statusbar; } Statusbar;
typedef struct Client Client; typedef struct Client Client;
@ -216,6 +220,19 @@ typedef struct
XftFont *font; XftFont *font;
} VirtScreen; } VirtScreen;
struct Widget
{
char *name;
int (*draw)(DrawCtx *,
awesome_config *,
VirtScreen, int, int, int, int);
Statusbar *statusbar;
int alignment;
Widget *next;
};
/** Main configuration structure */ /** Main configuration structure */
struct awesome_config struct awesome_config
{ {

View File

@ -7,6 +7,8 @@ RELEASE = "Productivity Breaker"
# additional layouts # additional layouts
LAYOUTS = layouts/tile.c layouts/floating.c layouts/max.c layouts/fibonacci.c LAYOUTS = layouts/tile.c layouts/floating.c layouts/max.c layouts/fibonacci.c
WIDGETS = widgets/taglist.c widgets/layoutinfo.c widgets/textbox.c widgets/focustitle.c
# paths # paths
PREFIX = /usr/local PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man MANPREFIX = ${PREFIX}/share/man

View File

@ -28,158 +28,17 @@
#include "screen.h" #include "screen.h"
#include "util.h" #include "util.h"
#include "tag.h" #include "tag.h"
#include "widget.h"
#include "layouts/tile.h" #include "layouts/tile.h"
#include "widget.h"
enum { AlignLeft, AlignRight, AlignFlex };
/** Check if at least a client is tagged with tag number t and is on screen
* screen
* \param t tag number
* \param screen screen number
* \return True or False
*/
static Bool
isoccupied(TagClientLink *tc, int screen, Client *head, Tag *t)
{
Client *c;
for(c = head; c; c = c->next)
if(c->screen == screen && is_client_tagged(tc, c, t))
return True;
return False;
}
static int
calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
{
if (alignment == AlignLeft || alignment == AlignFlex)
return offset;
else
return barwidth - offset - widgetwidth;
}
static int
tagwidget_draw(DrawCtx *ctx,
awesome_config *awesomeconf,
VirtScreen vscreen,
int screen,
int offset,
int used __attribute__ ((unused)),
int align __attribute__ ((unused)))
{
Tag *tag;
Client *sel = awesomeconf->focus->client;
int w = 0, width = 0, location;
int flagsize;
XColor *colors;
flagsize = (vscreen.font->height + 2) / 4;
for(tag = vscreen.tags; tag; tag = tag->next)
width += textwidth(ctx, vscreen.font, tag->name);
location = calculate_offset(vscreen.statusbar.width, width, offset, align);
width = 0;
for(tag = vscreen.tags; tag; tag = tag->next)
{
w = textwidth(ctx, vscreen.font, tag->name);
if(tag->selected)
colors = vscreen.colors_selected;
else
colors = vscreen.colors_normal;
drawtext(ctx, location + width, 0, w,
vscreen.statusbar.height, vscreen.font, tag->name, colors);
if(isoccupied(vscreen.tclink, screen, awesomeconf->clients, tag))
drawrectangle(ctx, location + width, 0, flagsize, flagsize,
sel && is_client_tagged(vscreen.tclink, sel, tag),
colors[ColFG]);
width += w;
}
return width;
}
static int
layoutsymbol_draw(DrawCtx *ctx,
awesome_config *awesomeconf __attribute__ ((unused)),
VirtScreen vscreen,
int screen __attribute__ ((unused)),
int offset,
int used __attribute__ ((unused)),
int align __attribute__ ((unused)))
{
int width = 0, location;
Layout *l;
for(l = vscreen.layouts ; l; l = l->next)
width = MAX(width, (textwidth(ctx, vscreen.font, l->symbol)));
location = calculate_offset(vscreen.statusbar.width, width, offset, align);
drawtext(ctx, location, 0,
width,
vscreen.statusbar.height,
vscreen.font,
get_current_layout(vscreen)->symbol,
vscreen.colors_normal);
return width;
}
static int
statustext_draw(DrawCtx *ctx,
awesome_config *awesomeconf __attribute__ ((unused)),
VirtScreen vscreen,
int screen __attribute__ ((unused)),
int offset,
int used __attribute__ ((unused)),
int align)
{
int width = textwidth(ctx, vscreen.font, vscreen.statustext);
int location = calculate_offset(vscreen.statusbar.width, width, offset, align);
drawtext(ctx, location, 0, width, vscreen.statusbar.height,
vscreen.font, vscreen.statustext, vscreen.colors_normal);
return width;
}
static int
focustitle_draw(DrawCtx *ctx,
awesome_config *awesomeconf __attribute__ ((unused)),
VirtScreen vscreen,
int screen __attribute__ ((unused)),
int offset,
int used,
int align)
{
Client *sel = awesomeconf->focus->client;
int location = calculate_offset(vscreen.statusbar.width, 0, offset, align);
if(sel)
{
drawtext(ctx, location, 0, vscreen.statusbar.width - used,
vscreen.statusbar.height, vscreen.font, sel->name,
vscreen.colors_selected);
if(sel->isfloating)
drawcircle(ctx, location, 0,
(vscreen.font->height + 2) / 4,
sel->ismax,
vscreen.colors_selected[ColFG]);
}
else
drawtext(ctx, location, 0, vscreen.statusbar.width - used,
vscreen.statusbar.height, vscreen.font, NULL,
vscreen.colors_normal);
return vscreen.statusbar.width - used;
}
void void
statusbar_draw(awesome_config *awesomeconf, int screen) statusbar_draw(awesome_config *awesomeconf, int screen)
{ {
int x = 0, split;
int phys_screen = get_phys_screen(awesomeconf->display, screen); int phys_screen = get_phys_screen(awesomeconf->display, screen);
VirtScreen vscreen; VirtScreen vscreen;
Widget *widget;
int left = 0, right = 0;
vscreen = awesomeconf->screens[screen]; vscreen = awesomeconf->screens[screen];
/* don't waste our time */ /* don't waste our time */
@ -188,11 +47,16 @@ statusbar_draw(awesome_config *awesomeconf, int screen)
DrawCtx *ctx = draw_get_context(awesomeconf->display, phys_screen, DrawCtx *ctx = draw_get_context(awesomeconf->display, phys_screen,
vscreen.statusbar.width, vscreen.statusbar.height); vscreen.statusbar.width, vscreen.statusbar.height);
x += tagwidget_draw(ctx, awesomeconf, vscreen, screen, x, x, AlignLeft);
x += layoutsymbol_draw(ctx, awesomeconf, vscreen, screen, x, x, AlignLeft); for(widget = vscreen.statusbar.widgets; widget; widget = widget->next)
split = x; if (widget->alignment == AlignLeft)
x += statustext_draw(ctx, awesomeconf, vscreen, screen, 0, x, AlignRight); left += widget->draw(ctx, awesomeconf, vscreen, screen, left, (left + right), AlignLeft);
focustitle_draw(ctx, awesomeconf, vscreen, screen, split, x, AlignFlex); else if (widget->alignment == AlignRight)
right += widget->draw(ctx, awesomeconf, vscreen, screen, right, (left + right), AlignRight);
for(widget = vscreen.statusbar.widgets; widget; widget = widget->next)
if (widget->alignment == AlignFlex)
left += widget->draw(ctx, awesomeconf, vscreen, screen, left, (left + right), AlignFlex);
if(vscreen.statusbar.position == BarRight || vscreen.statusbar.position == BarLeft) if(vscreen.statusbar.position == BarRight || vscreen.statusbar.position == BarLeft)
{ {
@ -224,6 +88,7 @@ statusbar_init(Display *disp, int screen, Statusbar *statusbar, Cursor cursor, X
XSetWindowAttributes wa; XSetWindowAttributes wa;
int phys_screen = get_phys_screen(disp, screen); int phys_screen = get_phys_screen(disp, screen);
ScreenInfo *si = get_screen_info(disp, screen, NULL, padding); ScreenInfo *si = get_screen_info(disp, screen, NULL, padding);
Widget *widget;
statusbar->height = font->height * 1.5; statusbar->height = font->height * 1.5;
@ -257,6 +122,15 @@ statusbar_init(Display *disp, int screen, Statusbar *statusbar, Cursor cursor, X
DefaultVisual(disp, phys_screen), DefaultVisual(disp, phys_screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
XDefineCursor(disp, statusbar->window, cursor); XDefineCursor(disp, statusbar->window, cursor);
/* For now, we just create a standard set of widgets by hand. As the refactoring continues,
* these will become configurable. */
statusbar->widgets = widget = taglist_new(statusbar);
widget->next = layoutinfo_new(statusbar); widget = widget->next;
widget->next = focustitle_new(statusbar); widget = widget->next;
widget->next = textbox_new(statusbar);
calculate_alignments(statusbar->widgets);
statusbar_update_position(disp, *statusbar, padding); statusbar_update_position(disp, *statusbar, padding);
XMapRaised(disp, statusbar->window); XMapRaised(disp, statusbar->window);
} }
@ -336,4 +210,5 @@ uicb_statusbar_set_text(awesome_config *awesomeconf, int screen, const char *arg
statusbar_draw(awesomeconf, screen); statusbar_draw(awesomeconf, screen);
} }
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99

34
widget.c Normal file
View File

@ -0,0 +1,34 @@
#include "widget.h"
void
calculate_alignments(Widget *widget)
{
while (widget){
printf("%s\n", widget->name);
if (widget->alignment == AlignFlex){
widget = widget->next;
break;
}
widget->alignment = AlignLeft;
widget = widget->next;
}
if (widget){
while (widget){
widget->alignment = AlignRight;
widget = widget->next;
}
}
}
int
calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
{
if (alignment == AlignLeft || alignment == AlignFlex)
return offset;
else
return barwidth - offset - widgetwidth;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99

19
widget.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef AWESOME_WIDGET_H
#define AWESOME_WIDGET_H
#include "config.h"
#include "draw.h"
enum { AlignLeft, AlignRight, AlignFlex };
int calculate_offset(int, int, int, int);
void calculate_alignments(Widget *widget);
Widget *layoutinfo_new(Statusbar*);
Widget *taglist_new(Statusbar*);
Widget *textbox_new(Statusbar*);
Widget *focustitle_new(Statusbar*);
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99

48
widgets/focustitle.c Normal file
View File

@ -0,0 +1,48 @@
#include "util.h"
#include "widget.h"
static char name[] = "focustitle";
static int
focustitle_draw(DrawCtx *ctx,
awesome_config *awesomeconf __attribute__ ((unused)),
VirtScreen vscreen,
int screen __attribute__ ((unused)),
int offset,
int used,
int align)
{
Client *sel = awesomeconf->focus->client;
int location = calculate_offset(vscreen.statusbar.width, 0, offset, align);
if(sel)
{
drawtext(ctx, location, 0, vscreen.statusbar.width - used,
vscreen.statusbar.height, vscreen.font, sel->name,
vscreen.colors_selected);
if(sel->isfloating)
drawcircle(ctx, location, 0,
(vscreen.font->height + 2) / 4,
sel->ismax,
vscreen.colors_selected[ColFG]);
}
else
drawtext(ctx, location, 0, vscreen.statusbar.width - used,
vscreen.statusbar.height, vscreen.font, NULL,
vscreen.colors_normal);
return vscreen.statusbar.width - used;
}
Widget *
focustitle_new(Statusbar *statusbar)
{
Widget *w;
w = p_new(Widget, 1);
w->draw = (void*) focustitle_draw;
w->statusbar = statusbar;
w->name = name;
w->alignment = AlignFlex;
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99

42
widgets/layoutinfo.c Normal file
View File

@ -0,0 +1,42 @@
#include "widget.h"
#include "util.h"
#include "layout.h"
static char name[] = "layoutinfo";
static int
layoutinfo_draw(DrawCtx *ctx,
awesome_config *awesomeconf __attribute__ ((unused)),
VirtScreen vscreen,
int screen __attribute__ ((unused)),
int offset,
int used __attribute__ ((unused)),
int align __attribute__ ((unused)))
{
int width = 0, location;
Layout *l;
for(l = vscreen.layouts ; l; l = l->next)
width = MAX(width, (textwidth(ctx, vscreen.font, l->symbol)));
location = calculate_offset(vscreen.statusbar.width, width, offset, align);
drawtext(ctx, location, 0,
width,
vscreen.statusbar.height,
vscreen.font,
get_current_layout(vscreen)->symbol,
vscreen.colors_normal);
return width;
}
Widget *
layoutinfo_new(Statusbar *statusbar)
{
Widget *w;
w = p_new(Widget, 1);
w->draw = (void*) layoutinfo_draw;
w->statusbar = statusbar;
w->name = name;
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99

81
widgets/taglist.c Normal file
View File

@ -0,0 +1,81 @@
#include "config.h"
#include "util.h"
#include "widget.h"
#include "tag.h"
static char name[] = "taglist";
/** Check if at least a client is tagged with tag number t and is on screen
* screen
* \param t tag number
* \param screen screen number
* \return True or False
*/
static Bool
isoccupied(TagClientLink *tc, int screen, Client *head, Tag *t)
{
Client *c;
for(c = head; c; c = c->next)
if(c->screen == screen && is_client_tagged(tc, c, t))
return True;
return False;
}
static int
taglist_draw(DrawCtx *ctx,
awesome_config *awesomeconf,
VirtScreen vscreen,
int screen,
int offset,
int used __attribute__ ((unused)),
int align __attribute__ ((unused)))
{
Tag *tag;
Client *sel = awesomeconf->focus->client;
int w = 0, width = 0, location;
int flagsize;
XColor *colors;
flagsize = (vscreen.font->height + 2) / 4;
for(tag = vscreen.tags; tag; tag = tag->next)
{
width += textwidth(ctx, vscreen.font, tag->name);
}
location = calculate_offset(vscreen.statusbar.width, width, offset, align);
width = 0;
for(tag = vscreen.tags; tag; tag = tag->next)
{
w = textwidth(ctx, vscreen.font, tag->name);
if(tag->selected)
colors = vscreen.colors_selected;
else
colors = vscreen.colors_normal;
drawtext(ctx, location + width, 0, w,
vscreen.statusbar.height, vscreen.font, tag->name, colors);
if(isoccupied(vscreen.tclink, screen, awesomeconf->clients, tag))
drawrectangle(ctx, location + width, 0, flagsize, flagsize,
sel && is_client_tagged(vscreen.tclink, sel, tag),
colors[ColFG]);
width += w;
}
return width;
}
Widget *
taglist_new(Statusbar *statusbar)
{
Widget *w;
w = p_new(Widget, 1);
w->draw = (void*) taglist_draw;
w->statusbar = statusbar;
w->name = name;
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99

35
widgets/textbox.c Normal file
View File

@ -0,0 +1,35 @@
#include "util.h"
#include "widget.h"
static char name[] = "textbox";
static int
textbox_draw(DrawCtx *ctx,
awesome_config *awesomeconf __attribute__ ((unused)),
VirtScreen vscreen,
int screen __attribute__ ((unused)),
int offset,
int used __attribute__ ((unused)),
int align)
{
int width = textwidth(ctx, vscreen.font, vscreen.statustext);
int location = calculate_offset(vscreen.statusbar.width, width, offset, align);
drawtext(ctx, location, 0, width, vscreen.statusbar.height,
vscreen.font, vscreen.statustext, vscreen.colors_normal);
return width;
}
Widget *
textbox_new(Statusbar *statusbar)
{
Widget *w;
w = p_new(Widget, 1);
w->draw = (void*) textbox_draw;
w->statusbar = statusbar;
w->name = name;
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99