wmii/cmd/wmiibar.c

537 lines
12 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include "wmii.h"
/* array indexes for file pointers */
typedef enum {
B_CTL,
B_NEW,
B_EXPANDABLE,
B_GEOMETRY,
B_FONT,
B_FG_COLOR,
B_BORDER_COLOR,
B_BG_COLOR,
B_EVENT_RESIZE,
2005-11-18 18:54:58 +03:00
B_LAST
2005-12-05 01:45:59 +03:00
} BarIndexes;
2005-11-18 18:54:58 +03:00
typedef struct {
2005-12-05 01:45:59 +03:00
File *root;
Draw d;
} Item;
2005-11-18 18:54:58 +03:00
static unsigned int id = 0;
2005-11-18 18:54:58 +03:00
static IXPServer *ixps = 0;
static Display *dpy;
2005-12-05 01:45:59 +03:00
static GC gc;
static Window win;
2005-11-18 18:54:58 +03:00
static XRectangle rect;
static XRectangle brect;
2005-12-05 01:45:59 +03:00
static int screen_num;
static int displayed = 0;
static char *sockfile = 0;
static File *file[B_LAST];
static Container items = {0};
2005-12-05 01:45:59 +03:00
static Pixmap pmap;
static XFontStruct *font;
static Align align = SOUTH;
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
static Draw zero_draw = { 0 };
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
static void draw_bar(void *obj, char *arg);
static void quit(void *obj, char *arg);
static void display(void *obj, char *arg);
static void reset(void *obj, char *arg);
static void _destroy(void *obj, char *arg);
static void handle_after_write(IXPServer * s, File * f);
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
static Action acttbl[] = {
2005-11-18 18:54:58 +03:00
{"quit", quit},
{"display", display},
{"update", draw_bar},
{"reset", reset},
{"destroy", _destroy},
{0, 0}
};
2005-12-05 01:45:59 +03:00
static char *version[] = {
"wmiibar - window manager improved bar - " VERSION "\n"
2005-12-05 01:45:59 +03:00
" (C)opyright MMIV-MMV Anselm R. Garbe\n", 0
2005-11-18 18:54:58 +03:00
};
2005-12-05 01:45:59 +03:00
static void usage()
2005-11-18 18:54:58 +03:00
{
fprintf(stderr, "%s",
"usage: wmiibar -s <socket file> [-v]\n"
2005-12-05 01:45:59 +03:00
" -s socket file\n" " -v version info\n");
2005-11-18 18:54:58 +03:00
exit(1);
}
/**
* <path>/data "<txt value>"
* <path>/fgcolor "#RRGGBBAA"
* <path>/bgcolor "#RRGGBBAA"
* <path>/bordercolor "#RRGGBBAA"
* <path>/b1press "<command>"
* <path>/b2press "<command>"
* <path>/b3press "<command>"
* <path>/b4press "<command>"
* <path>/b5press "<command>"
*/
2005-12-05 01:45:59 +03:00
static void create_label(char *path)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
File *f;
char buf[MAX_BUF];
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
snprintf(buf, MAX_BUF, "%s/data", path);
f = ixp_create(ixps, buf);
2005-11-18 18:54:58 +03:00
f->after_write = handle_after_write;
snprintf(buf, MAX_BUF, "%s/fgcolor", path);
wmii_create_ixpfile(ixps, buf, file[B_FG_COLOR]->content);
snprintf(buf, MAX_BUF, "%s/bgcolor", path);
wmii_create_ixpfile(ixps, buf, file[B_BG_COLOR]->content);
snprintf(buf, MAX_BUF, "%s/bordercolor", path);
wmii_create_ixpfile(ixps, buf, file[B_BORDER_COLOR]->content);
2005-11-18 18:54:58 +03:00
for (i = 1; i < 6; i++) { /* 5 buttons events */
snprintf(buf, MAX_BUF, "%s/b%dpress", path, i);
ixp_create(ixps, buf);
2005-11-18 18:54:58 +03:00
}
}
2005-12-05 01:45:59 +03:00
static void _destroy(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
char buf[512];
2005-11-18 18:54:58 +03:00
if (!arg)
return;
snprintf(buf, sizeof(buf), "/%s", arg);
ixps->remove(ixps, buf);
draw_bar(0, 0);
}
2005-12-05 01:45:59 +03:00
static void reset(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
char buf[512];
for (i = 0; i < id; i++) {
snprintf(buf, sizeof(buf), "/%d", i);
2005-11-18 18:54:58 +03:00
ixps->remove(ixps, buf);
}
id = 0;
2005-11-18 18:54:58 +03:00
draw_bar(0, 0);
}
2005-12-05 01:45:59 +03:00
static void quit(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
ixps->runlevel = SHUTDOWN;
}
2005-12-05 01:45:59 +03:00
static void display(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
if (!arg)
return;
2005-12-11 17:47:23 +03:00
displayed = blitz_strtonum(arg, 0, 1);
2005-11-18 18:54:58 +03:00
if (displayed) {
XMapRaised(dpy, win);
draw_bar(0, 0);
} else {
XUnmapWindow(dpy, win);
XSync(dpy, False);
}
}
static void init_draw_label(char *path, Draw *d)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
char buf[MAX_BUF];
File *f;
2005-11-18 18:54:58 +03:00
/* text stuff */
snprintf(buf, MAX_BUF, "%s/data", path);
f = ixp_walk(ixps, buf);
d->data = f->content;
/* style stuff */
snprintf(buf, MAX_BUF, "%s/fgcolor", path);
f = ixp_walk(ixps, buf);
d->fg = blitz_loadcolor(dpy, screen_num, f->content);
snprintf(buf, MAX_BUF, "%s/bgcolor", path);
f = ixp_walk(ixps, buf);
d->bg = blitz_loadcolor(dpy, screen_num, f->content);
snprintf(buf, MAX_BUF, "%s/bordercolor", path);
f = ixp_walk(ixps, buf);
d->border = blitz_loadcolor(dpy, screen_num, f->content);
}
static void init_item(char *path, Item *i)
2005-11-18 18:54:58 +03:00
{
i->d = zero_draw;
i->root = ixp_walk(ixps, path);
i->d.gc = gc;
i->d.drawable = pmap;
i->d.rect = brect;
i->d.rect.y = 0;
init_draw_label(path, &i->d);
}
2005-12-05 01:45:59 +03:00
static int comp_str(const void *s1, const void *s2)
2005-11-18 18:54:58 +03:00
{
return strcmp(*(char **) s1, *(char **) s2);
}
2005-12-05 01:45:59 +03:00
static void draw()
2005-11-18 18:54:58 +03:00
{
Item *item;
unsigned int i, w, xoff = 0;
2005-12-05 01:45:59 +03:00
unsigned expandable = 0;
char buf[32];
size_t size = cext_sizeof_container(&items);
2005-11-18 18:54:58 +03:00
if (!size)
2005-11-18 18:54:58 +03:00
return;
2005-12-11 17:47:23 +03:00
expandable = blitz_strtonum(file[B_EXPANDABLE]->content, 0, size);
2005-11-18 18:54:58 +03:00
snprintf(buf, sizeof(buf), "/%d", expandable);
if (!ixp_walk(ixps, buf))
expandable = 0;
w = 0;
/* precalc */
for (i = 0; i < size; i++)
if (i != expandable) {
item = cext_list_get_item(&items, i);
item->d.rect.width = brect.height;
if (item->d.data) {
if (!strncmp(item->d.data, "%m:", 3))
2005-11-18 18:54:58 +03:00
/* meter */
item->d.rect.width = brect.height / 2;
2005-11-18 18:54:58 +03:00
else
item->d.rect.width += XTextWidth(font, item->d.data, strlen(item->d.data));
2005-11-18 18:54:58 +03:00
}
w += item->d.rect.width;
2005-11-18 18:54:58 +03:00
}
if (w > brect.width) {
2005-11-18 18:54:58 +03:00
/* failsafe mode, give all labels same width */
w = brect.width / size;
for (i = 0; i < size; i++) {
item = cext_list_get_item(&items, i);
item->d.rect.width = w;
}
item->d.rect.width = brect.width - item->d.rect.x;
}
else {
item = cext_list_get_item(&items, expandable);
item->d.rect.width = brect.width - w;
}
for (i = 0; i < size; i++) {
item = cext_list_get_item(&items, i);
item->d.font = font;
item->d.rect.x = xoff;
xoff += item->d.rect.width;
if (item->d.data && !strncmp(item->d.data, "%m:", 3))
blitz_drawmeter(dpy, &item->d);
2005-11-18 18:54:58 +03:00
else
blitz_drawlabel(dpy, &item->d);
2005-11-18 18:54:58 +03:00
}
XCopyArea(dpy, pmap, win, gc, 0, 0, brect.width, brect.height, 0, 0);
XSync(dpy, False);
}
2005-12-05 01:45:59 +03:00
static void draw_bar(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
File *label = 0;
unsigned int i = 0, n = 0;
Item *item;
char buf[512];
2005-11-18 18:54:58 +03:00
if (!displayed)
return;
while ((item = cext_stack_get_top_item(&items))) {
cext_detach_item(&items, item);
free(item);
2005-11-18 18:54:58 +03:00
}
snprintf(buf, sizeof(buf), "%s", "/0");
2005-11-18 18:54:58 +03:00
label = ixp_walk(ixps, buf);
if (!label) {
2005-12-05 01:45:59 +03:00
Draw d = { 0 };
2005-11-18 18:54:58 +03:00
/* default stuff */
d.gc = gc;
d.drawable = pmap;
d.rect.width = brect.width;
d.rect.height = brect.height;
d.bg = blitz_loadcolor(dpy, screen_num, file[B_BG_COLOR]->content);
d.fg = blitz_loadcolor(dpy, screen_num, file[B_FG_COLOR]->content);
d.border = blitz_loadcolor(dpy, screen_num, file[B_BORDER_COLOR]->content);
2005-11-18 18:54:58 +03:00
blitz_drawlabelnoborder(dpy, &d);
} else {
2005-12-05 01:45:59 +03:00
File *f;
char **paths = 0;
2005-11-18 18:54:58 +03:00
/*
* take order into account, directory names are used in
* alphabetical order
*/
n = 0;
for (f = label; f; f = f->next)
n++;
paths = cext_emallocz(sizeof(char *) * n);
2005-11-18 18:54:58 +03:00
i = 0;
for (f = label; f; f = f->next)
paths[i++] = f->name;
qsort(paths, n, sizeof(char *), comp_str);
for (i = 0; i < n; i++) {
snprintf(buf, sizeof(buf), "/%s", paths[i]);
item = cext_emallocz(sizeof(Item));
2005-11-18 18:54:58 +03:00
init_item(buf, item);
cext_attach_item(&items, item);
2005-11-18 18:54:58 +03:00
}
free(paths);
draw();
2005-11-18 18:54:58 +03:00
}
}
static int comp_item_root(void *file, void *item)
{
File *f = file;
Item *i = item;
return f == i->root;
}
static Item *get_item_for_file(File *f)
2005-11-18 18:54:58 +03:00
{
return cext_find_item(&items, f, comp_item_root);
2005-11-18 18:54:58 +03:00
}
static void iter_buttonpress(void *item, void *bpress)
2005-11-18 18:54:58 +03:00
{
XButtonPressedEvent *e = bpress;
Item *i = item;
2005-12-05 01:45:59 +03:00
File *p;
char buf[MAX_BUF];
char path[512];
2005-11-18 18:54:58 +03:00
if (blitz_ispointinrect(e->x, e->y, &i->d.rect)) {
path[0] = 0;
wmii_get_ixppath(i->root, path, sizeof(path));
snprintf(buf, MAX_BUF, "%s/b%upress", path, e->button);
if ((p = ixp_walk(ixps, buf)))
if (p->content)
wmii_spawn(dpy, p->content);
return;
2005-11-18 18:54:58 +03:00
}
}
static void handle_buttonpress(XButtonPressedEvent *e)
{
cext_list_iterate(&items, e, iter_buttonpress);
}
2005-12-05 01:45:59 +03:00
static void check_event(Connection * e)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
XEvent ev;
2005-11-18 18:54:58 +03:00
while (XPending(dpy)) {
XNextEvent(dpy, &ev);
switch (ev.type) {
case ButtonPress:
handle_buttonpress(&ev.xbutton);
break;
case Expose:
if (ev.xexpose.count == 0) {
/* XRaiseWindow(dpy, win); */
draw_bar(0, 0);
}
break;
default:
break;
}
}
}
static void update_geometry()
2005-11-18 18:54:58 +03:00
{
brect = rect;
brect.height = font->ascent + font->descent + 4;
if (align == SOUTH)
brect.y = rect.height - brect.height;
XMoveResizeWindow(dpy, win, brect.x, brect.y, brect.width, brect.height);
XSync(dpy, False);
XFreePixmap(dpy, pmap);
pmap = XCreatePixmap(dpy, win, brect.width, brect.height, DefaultDepth(dpy, screen_num));
XSync(dpy, False);
if (file[B_EVENT_RESIZE]->content)
wmii_spawn(dpy, file[B_EVENT_RESIZE]->content);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
static void handle_after_write(IXPServer * s, File * f)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
size_t len;
Item *item;
char buf[512];
2005-11-18 18:54:58 +03:00
buf[0] = 0;
2005-11-18 18:54:58 +03:00
if (!strncmp(f->name, "data", 5)) {
if ((item = get_item_for_file(f->parent))) {
wmii_get_ixppath(f->parent, buf, sizeof(buf));
init_draw_label(buf, &item->d);
draw();
}
} else if (file[B_GEOMETRY] == f) {
if (f->content) {
if (!strncmp(f->content, "south", 6))
align = SOUTH;
else if(!strncmp(f->content, "north", 6))
align = NORTH;
update_geometry();
2005-11-18 18:54:58 +03:00
draw_bar(0, 0);
}
} else if (file[B_CTL] == f) {
2005-11-18 18:54:58 +03:00
for (i = 0; acttbl[i].name; i++) {
len = strlen(acttbl[i].name);
if (!strncmp(acttbl[i].name, (char *) f->content, len)) {
if (strlen(f->content) > len) {
acttbl[i].func(0, &((char *) f->content)[len + 1]);
} else {
acttbl[i].func(0, 0);
}
break;
}
}
} else if (file[B_FONT] == f) {
XFreeFont(dpy, font);
font = blitz_getfont(dpy, file[B_FONT]->content);
update_geometry();
draw_bar(0, 0);
2005-11-18 18:54:58 +03:00
}
check_event(0);
}
2005-12-05 01:45:59 +03:00
static void handle_before_read(IXPServer * s, File * f)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
char buf[64];
if (f == file[B_GEOMETRY]) {
snprintf(buf, 64, "%d,%d,%d,%d", brect.x, brect.y, brect.width, brect.height);
2005-11-18 18:54:58 +03:00
if (f->content)
free(f->content);
f->content = cext_estrdup(buf);
f->size = strlen(buf);
} else if (f == file[B_NEW]) {
snprintf(buf, sizeof(buf), "%d", id++);
2005-11-18 18:54:58 +03:00
if (f->content)
free(f->content);
f->content = strdup(buf);
f->size = strlen(buf);
create_label(buf);
draw_bar(0, 0);
}
}
static int dummy_error_handler(Display * dpy, XErrorEvent * err)
{
return 0;
}
int main(int argc, char *argv[])
2005-11-18 18:54:58 +03:00
{
int i;
2005-11-18 18:54:58 +03:00
XSetWindowAttributes wa;
2005-12-05 01:45:59 +03:00
XGCValues gcv;
2005-11-18 18:54:58 +03:00
/* command line args */
for (i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v':
fprintf(stdout, "%s", version[0]);
exit(0);
break;
case 's':
if (i + 1 < argc)
sockfile = argv[++i];
else
usage();
break;
default:
usage();
break;
}
}
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "%s", "wmiibar: cannot open display\n");
exit(1);
}
XSetErrorHandler(dummy_error_handler);
screen_num = DefaultScreen(dpy);
ixps = wmii_setup_server(sockfile);
2005-11-18 18:54:58 +03:00
/* init */
if (!(file[B_CTL] = ixp_create(ixps, "/ctl"))) {
perror("wmiibar: cannot connect IXP server");
2005-11-18 18:54:58 +03:00
exit(1);
}
file[B_CTL]->after_write = handle_after_write;
file[B_NEW] = ixp_create(ixps, "/new");
file[B_NEW]->before_read = handle_before_read;
file[B_FONT] = wmii_create_ixpfile(ixps, "/font", BLITZ_FONT);
file[B_FONT]->after_write = handle_after_write;
font = blitz_getfont(dpy, file[B_FONT]->content);
file[B_BG_COLOR] = wmii_create_ixpfile(ixps, "/bgcolor", BLITZ_NORM_BG_COLOR);
file[B_FG_COLOR] = wmii_create_ixpfile(ixps, "/fgcolor", BLITZ_NORM_FG_COLOR);
file[B_BORDER_COLOR] = wmii_create_ixpfile(ixps, "/bordercolor", BLITZ_NORM_BORDER_COLOR);
file[B_GEOMETRY] = ixp_create(ixps, "/geometry");
file[B_GEOMETRY]->before_read = handle_before_read;
file[B_GEOMETRY]->after_write = handle_after_write;
file[B_EXPANDABLE] = ixp_create(ixps, "/expandable");
file[B_EVENT_RESIZE] = ixp_create(ixps, "/event/resize");
2005-11-18 18:54:58 +03:00
wa.override_redirect = 1;
wa.background_pixmap = ParentRelative;
wa.event_mask = ExposureMask | ButtonPressMask | SubstructureRedirectMask | SubstructureNotifyMask;
2005-11-18 18:54:58 +03:00
rect.x = rect.y = 0;
rect.width = DisplayWidth(dpy, screen_num);
rect.height = DisplayHeight(dpy, screen_num);
brect = rect;
brect.height = font->ascent + font->descent + 4;
brect.y = rect.height - brect.height;
2005-11-18 18:54:58 +03:00
win = XCreateWindow(dpy, RootWindow(dpy, screen_num), brect.x, brect.y,
brect.width, brect.height, 0, DefaultDepth(dpy, screen_num),
2005-12-05 01:45:59 +03:00
CopyFromParent, DefaultVisual(dpy, screen_num),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
2005-11-18 18:54:58 +03:00
XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_left_ptr));
XSync(dpy, False);
gcv.function = GXcopy;
gcv.graphics_exposures = False;
gc = XCreateGC(dpy, win, 0, 0);
pmap = XCreatePixmap(dpy, win, brect.width, brect.height, DefaultDepth(dpy, screen_num));
2005-11-18 18:54:58 +03:00
/* main event loop */
run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, 0);
2005-11-18 18:54:58 +03:00
deinit_server(ixps);
XFreePixmap(dpy, pmap);
XFreeGC(dpy, gc);
XCloseDisplay(dpy);
return 0;
}