commit 3b9fb4494c92bc998b82ffb58fa62c7d188c59cf
parent c360abdc72c361a791bf53f2f5be6ff3af134037
Author: garbeam <garbeam@localhost.localdomain>
Date: Thu, 8 Dec 2005 18:35:40 +0200
changed everything to use container struct
Diffstat:
14 files changed, 387 insertions(+), 508 deletions(-)
diff --git a/cmd/wm/area.c b/cmd/wm/area.c
@@ -53,10 +53,10 @@ void sel_area(Area *a)
{
Page *p = a->page;
Frame *f;
- Bool raise = cext_get_item_index(&p->areas, a) == 0;
+ Bool raise = cext_list_get_item_index(&p->areas, a) == 0;
if (raise)
cext_iterate(a->layout->get_frames(a), nil, iter_raise_frame);
- cext_top_item(&p->areas, a);
+ cext_stack_top_item(&p->areas, a);
p->file[P_SEL_AREA]->content = a->file[A_PREFIX]->content;
if ((f = get_sel_frame_of_area(a)))
sel_frame(f, raise);
@@ -91,7 +91,7 @@ Area *get_sel_area()
{
Page *p = get_sel_page();
- return p ? cext_get_top_item(&p->areas) : nil;
+ return p ? cext_stack_get_top_item(&p->areas) : nil;
}
void attach_frame_to_area(Area *a, Frame *f)
diff --git a/cmd/wm/client.c b/cmd/wm/client.c
@@ -34,7 +34,7 @@ void sel_client(Client * c)
Frame *f = 0;
/* sel client */
f = c->frame;
- cext_top_item(&f->clients, c);
+ cext_stack_top_item(&f->clients, c);
f->file[F_SEL_CLIENT]->content = c->file[C_PREFIX]->content;
XRaiseWindow(dpy, c->win);
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
@@ -220,7 +220,7 @@ void draw_client(void *item, void *aux)
tw = f->rect.width;
if (size)
tw /= size;
- i = cext_get_item_index(&f->clients, c);
+ i = cext_list_get_item_index(&f->clients, c);
if (i < size - 1)
draw_tab(f, c->file[C_NAME]->content, i * tw, 0, f->rect.width - (i * tw), tabh,
(f == get_sel_frame()) && (c == get_sel_client()));
@@ -325,5 +325,5 @@ void detach_client(Client *c) {
Client *get_sel_client()
{
Frame *f = get_sel_frame();
- return f ? cext_get_top_item(&f->clients) : nil;
+ return f ? cext_stack_get_top_item(&f->clients) : nil;
}
diff --git a/cmd/wm/frame.c b/cmd/wm/frame.c
@@ -102,8 +102,8 @@ Frame *alloc_frame(XRectangle * r)
void sel_frame(Frame * f, Bool raise)
{
Area *a = f->area;
- sel_client(cext_get_top_item(&f->clients));
- cext_top_item(a->layout->get_frames(a), f);
+ sel_client(cext_stack_get_top_item(&f->clients));
+ cext_stack_top_item(a->layout->get_frames(a), f);
a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content;
if (raise)
XRaiseWindow(dpy, f->win);
@@ -318,7 +318,7 @@ void handle_frame_buttonpress(XButtonEvent * e, Frame * f)
Align align;
size_t size = cext_sizeof(&f->clients);
int bindex, cindex = e->x / f->rect.width / size;
- Client *c = cext_get_item(&f->clients, cindex);
+ Client *c = cext_list_get_item(&f->clients, cindex);
XRaiseWindow(dpy, f->win);
if (get_sel_client() != c) {
sel_client(c);
@@ -366,7 +366,7 @@ void detach_client_from_frame(Client * c)
}
reparent_client(c, root, border_width(f), tab_height(f));
}
- if ((client = cext_get_top_item(&f->clients))) {
+ if ((client = cext_stack_get_top_item(&f->clients))) {
sel_client(client);
draw_frame(f, nil);
}
@@ -379,10 +379,10 @@ static void select_client(void *obj, char *cmd)
if (!f || !cmd || size == 1)
return;
if (!strncmp(cmd, "prev", 5))
- cext_top_item(&f->clients, cext_get_up_item(&f->clients, cext_get_top_item(&f->clients)));
+ cext_stack_top_item(&f->clients, cext_stack_get_up_item(&f->clients, cext_stack_get_top_item(&f->clients)));
else if (!strncmp(cmd, "next", 5))
- cext_top_item(&f->clients, cext_get_down_item(&f->clients, cext_get_top_item(&f->clients)));
- sel_client(cext_get_top_item(&f->clients));
+ cext_stack_top_item(&f->clients, cext_stack_get_down_item(&f->clients, cext_stack_get_top_item(&f->clients)));
+ sel_client(cext_stack_get_top_item(&f->clients));
draw_frame(f, nil);
}
@@ -436,7 +436,7 @@ static void handle_after_write_frame(IXPServer * s, File * f)
Frame *get_sel_frame_of_area(Area *a)
{
- return cext_get_top_item(a->layout->get_frames(a));
+ return cext_stack_get_top_item(a->layout->get_frames(a));
}
Frame *get_sel_frame()
diff --git a/cmd/wm/layout_column.c b/cmd/wm/layout_column.c
@@ -41,7 +41,7 @@ void init_layout_column()
static Column *get_sel_column(Acme *acme)
{
- return cext_get_top_item(&acme->columns);
+ return cext_stack_get_top_item(&acme->columns);
}
static void iter_arrange_column_frame(void *frame, void *height)
@@ -52,7 +52,7 @@ static void iter_arrange_column_frame(void *frame, void *height)
if (col->refresh) {
f->rect = col->rect;
f->rect.height = h;
- f->rect.y = cext_get_item_index(&col->frames, f) * h;
+ f->rect.y = cext_list_get_item_index(&col->frames, f) * h;
}
resize_frame(f, &f->rect, 0);
}
diff --git a/cmd/wm/mouse.c b/cmd/wm/mouse.c
@@ -598,19 +598,19 @@ void drop_move(Frame *f, XRectangle *new, XPoint *pt)
Area *a = f->area;
Container *frames = a->layout->get_frames(a);
int cx, cy;
- unsigned int i, idx = cext_get_item_index(frames, f);
+ unsigned int i, idx = cext_list_get_item_index(frames, f);
size_t size = cext_sizeof(frames);
if ((f->rect.x == new->x) && (f->rect.y == new->y))
return;
cx = (pt ? pt->x : new->x + new->width / 2);
cy = (pt ? pt->y : new->y + new->height / 2);
- f1 = cext_get_item(frames, idx);
+ f1 = cext_list_get_item(frames, idx);
for (i = 0; i < size; i++) {
- f2 = cext_get_item(frames, i);
+ f2 = cext_list_get_item(frames, i);
if ((i != idx) && blitz_ispointinrect(cx, cy, &f1->rect)) {
cext_swap_items(frames, f1, f2);
- cext_top_item(frames, f2);
+ cext_stack_top_item(frames, f2);
a->layout->arrange(a);
return;
}
diff --git a/cmd/wm/page.c b/cmd/wm/page.c
@@ -70,7 +70,7 @@ void sel_page(Page * p)
return;
if (p != sel) {
hide_page(sel);
- cext_top_item(&pages, p);
+ cext_stack_top_item(&pages, p);
show_page(p);
}
def[WM_SEL_PAGE]->content = p->file[P_PREFIX]->content;
@@ -148,11 +148,11 @@ static void select_frame(void *obj, char *cmd)
return;
a = f->area;
if (!strncmp(cmd, "prev", 5))
- cext_top_item(a->layout->get_frames(a), cext_get_up_item(a->layout->get_frames(a), f));
+ cext_stack_top_item(a->layout->get_frames(a), cext_stack_get_up_item(a->layout->get_frames(a), f));
else if (!strncmp(cmd, "next", 5))
- cext_top_item(a->layout->get_frames(a), cext_get_down_item(a->layout->get_frames(a), f));
+ cext_stack_top_item(a->layout->get_frames(a), cext_stack_get_down_item(a->layout->get_frames(a), f));
if (old != f) {
- sel_frame(f, cext_get_item_index(&a->page->areas, a) == 0);
+ sel_frame(f, cext_list_get_item_index(&a->page->areas, a) == 0);
center_pointer(f);
draw_frame(old, nil);
draw_frame(f, nil);
@@ -196,5 +196,5 @@ static void handle_after_write_page(IXPServer *s, File *f)
Page *get_sel_page()
{
- return cext_get_top_item(&pages);
+ return cext_stack_get_top_item(&pages);
}
diff --git a/cmd/wm/wm.c b/cmd/wm/wm.c
@@ -115,7 +115,7 @@ static void iter_draw_pager_frame(void *item, void *aux)
{
Draw *d = aux;
Frame *f = (Frame *)item;
- if (f == cext_get_top_item(f->area->layout->get_frames(f->area))) {
+ if (f == cext_stack_get_top_item(f->area->layout->get_frames(f->area))) {
d->bg = blitz_loadcolor(dpy, screen_num, def[WM_SEL_BG_COLOR]->content);
d->fg = blitz_loadcolor(dpy, screen_num, def[WM_SEL_FG_COLOR]->content);
d->border = blitz_loadcolor(dpy, screen_num, def[WM_SEL_BORDER_COLOR]->content);
@@ -124,7 +124,7 @@ static void iter_draw_pager_frame(void *item, void *aux)
d->fg = blitz_loadcolor(dpy, screen_num, def[WM_NORM_FG_COLOR]->content);
d->border = blitz_loadcolor(dpy, screen_num, def[WM_NORM_BORDER_COLOR]->content);
}
- d->data = ((Client *)cext_get_top_item(&f->clients))->file[C_NAME]->content;
+ d->data = ((Client *)cext_stack_get_top_item(&f->clients))->file[C_NAME]->content;
scale_rect(&rect, &initial_rect, &f->area->rect, &d->rect);
blitz_drawlabel(dpy, d);
XSync(dpy, False); /* do not clear upwards */
@@ -140,7 +140,7 @@ static void draw_pager_page(Page *p, Draw *d)
{
char name[4];
initial_rect = d->rect;
- if (p == cext_get_top_item(&pages)) {
+ if (p == cext_stack_get_top_item(&pages)) {
d->bg = blitz_loadcolor(dpy, screen_num, def[WM_SEL_BG_COLOR]->content);
d->fg = blitz_loadcolor(dpy, screen_num, def[WM_SEL_FG_COLOR]->content);
d->border = blitz_loadcolor(dpy, screen_num, def[WM_SEL_BORDER_COLOR]->content);
@@ -149,7 +149,7 @@ static void draw_pager_page(Page *p, Draw *d)
d->fg = blitz_loadcolor(dpy, screen_num, def[WM_NORM_FG_COLOR]->content);
d->border = blitz_loadcolor(dpy, screen_num, def[WM_NORM_BORDER_COLOR]->content);
}
- snprintf(name, sizeof(name), "%d", cext_get_item_index(&pages, p));
+ snprintf(name, sizeof(name), "%d", cext_list_get_item_index(&pages, p));
d->data = name;
blitz_drawlabel(dpy, d);
XSync(dpy, False);
@@ -180,7 +180,7 @@ static void draw_pager()
else
d.rect.y = ir * (rect.height - th) / (rows - 1);
d.rect.height = th;
- if (!(p = cext_get_item(&pages, i)))
+ if (!(p = cext_list_get_item(&pages, i)))
return;
draw_pager_page(p, &d);
i++;
@@ -212,7 +212,7 @@ static Page *xy_to_pager_page(int x, int y)
else
r.y = ir * (rect.height - th) / (rows - 1);
r.height = th;
- if (!(p = cext_get_item(&pages, i)))
+ if (!(p = cext_list_get_item(&pages, i)))
return nil;
if (blitz_ispointinrect(x, y, &r))
return p;
@@ -264,7 +264,7 @@ static void pager(void *obj, char *cmd)
XUnmapWindow(dpy, transient);
if ((i = handle_kpress(&ev.xkey)) != -1)
if (i < cext_sizeof(&pages))
- sel_page(cext_get_item(&pages, i));
+ sel_page(cext_list_get_item(&pages, i));
XUngrabKeyboard(dpy, CurrentTime);
return;
break;
@@ -299,7 +299,7 @@ static void draw_icons()
i = 0;
for (ir = 0; ir < rows; ir++) {
for (ic = 0; ic < cols; ic++) {
- Client *c = cext_get_item(&detached, i++);
+ Client *c = cext_list_get_item(&detached, i++);
XRectangle cr;
if (!c)
return;
@@ -347,15 +347,15 @@ static void icons(void *obj, char *cmd)
XUnmapWindow(dpy, transient);
if ((n = handle_kpress(&ev.xkey)) != -1) {
for (i = 0; i < size; i++)
- hide_client(cext_get_item(&detached, i));
+ hide_client(cext_list_get_item(&detached, i));
if (n - 1 < i) {
- c = cext_get_item(&detached, n);
+ c = cext_list_get_item(&detached, n);
cext_detach_item(&detached, c);
attach_client(c);
}
} else {
for (i = 0; i < size; i++)
- hide_client(cext_get_item(&detached, i));
+ hide_client(cext_list_get_item(&detached, i));
}
XUngrabKeyboard(dpy, CurrentTime);
return;
@@ -364,7 +364,7 @@ static void icons(void *obj, char *cmd)
if (ev.xbutton.button == Button1) {
XUnmapWindow(dpy, transient);
for (i = 0; i < size; i++)
- hide_client(cext_get_item(&detached, i));
+ hide_client(cext_list_get_item(&detached, i));
if ((c = win_to_client(ev.xbutton.window))) {
cext_detach_item(&detached, c);
attach_client(c);
@@ -381,13 +381,13 @@ static void _close_client(void *obj, char *cmd)
{
Frame *f = get_sel_frame();
if (f)
- close_client(cext_get_top_item(&f->clients));
+ close_client(cext_stack_get_top_item(&f->clients));
}
static void _attach_client(void *obj, char *cmd)
{
if (cext_sizeof(&detached)) {
- Client *c = cext_get_top_item(&detached);
+ Client *c = cext_stack_get_top_item(&detached);
cext_detach_item(&detached, c);
attach_client(c);
}
@@ -398,7 +398,7 @@ static void _detach_client(void *obj, char *cmd)
Frame *f = get_sel_frame();
if (!f)
return;
- f->area->layout->detach(f->area, cext_get_top_item(&f->clients));
+ f->area->layout->detach(f->area, cext_stack_get_top_item(&f->clients));
}
static void _select_page(void *obj, char *cmd)
@@ -407,11 +407,11 @@ static void _select_page(void *obj, char *cmd)
if (!p || !cmd)
return;
if (!strncmp(cmd, "prev", 5))
- p = cext_get_up_item(&pages, p);
+ p = cext_stack_get_up_item(&pages, p);
else if (!strncmp(cmd, "next", 5))
- p = cext_get_down_item(&pages, p);
+ p = cext_stack_get_down_item(&pages, p);
else
- p = cext_get_item(&pages, _strtonum(cmd, 0, cext_sizeof(&pages) - 1));
+ p = cext_list_get_item(&pages, _strtonum(cmd, 0, cext_sizeof(&pages) - 1));
sel_page(p);
}
diff --git a/cmd/wmibar.c b/cmd/wmibar.c
@@ -43,10 +43,11 @@ static XRectangle brect;
static int screen_num;
static int displayed = 0;
static char *sockfile = 0;
-static File *files[B_LAST];
-static Item **items = 0;
+static File *file[B_LAST];
+static Container items = {0};
static unsigned int id = 0;
static Pixmap pmap;
+static XFontStruct *font;
static Draw zero_draw = { 0 };
@@ -93,21 +94,21 @@ static void usage()
static void create_label(char *path)
{
File *f;
- char file[MAX_BUF];
+ char buf[MAX_BUF];
int i;
- snprintf(file, MAX_BUF, "%s/data", path);
- f = ixp_create(ixps, file);
+ snprintf(buf, MAX_BUF, "%s/data", path);
+ f = ixp_create(ixps, buf);
f->after_write = handle_after_write;
- snprintf(file, MAX_BUF, "%s/fgcolor", path);
- wmii_create_ixpfile(ixps, file, files[B_FG_COLOR]->content);
- snprintf(file, MAX_BUF, "%s/bgcolor", path);
- wmii_create_ixpfile(ixps, file, files[B_BG_COLOR]->content);
- snprintf(file, MAX_BUF, "%s/bordercolor", path);
- wmii_create_ixpfile(ixps, file, files[B_BORDER_COLOR]->content);
+ 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);
for (i = 1; i < 6; i++) { /* 5 buttons events */
- snprintf(file, MAX_BUF, "%s/b%dpress", path, i);
- ixp_create(ixps, file);
+ snprintf(buf, MAX_BUF, "%s/b%dpress", path, i);
+ ixp_create(ixps, buf);
}
}
@@ -152,7 +153,7 @@ static void display(void *obj, char *arg)
}
}
-static void init_draw_label(char *path, Draw * d)
+static void init_draw_label(char *path, Draw *d)
{
char buf[MAX_BUF];
File *f;
@@ -173,7 +174,7 @@ static void init_draw_label(char *path, Draw * d)
d->border = blitz_loadcolor(dpy, screen_num, f->content);
}
-static void init_item(char *path, Item * i)
+static void init_item(char *path, Item *i)
{
i->d = zero_draw;
i->root = ixp_walk(ixps, path);
@@ -191,58 +192,60 @@ static int comp_str(const void *s1, const void *s2)
static void draw()
{
- unsigned int n = 0, i, w, xoff = 0;
- XFontStruct *font;
+ Item *item;
+ unsigned int i, w, xoff = 0;
unsigned expandable = 0;
char buf[32];
+ size_t size = cext_sizeof(&items);
- if (!items)
+ if (!size)
return;
- n = count_items((void **) items);
- font = blitz_getfont(dpy, files[B_FONT]->content);
- expandable = _strtonum(files[B_EXPANDABLE]->content, 1, id);
+ expandable = _strtonum(file[B_EXPANDABLE]->content, 0, id);
snprintf(buf, sizeof(buf), "/%d", expandable);
if (!ixp_walk(ixps, buf))
expandable = 0;
w = 0;
/* precalc */
- for (i = 0; expandable && items[i]; i++)
- if (i + 1 != expandable) {
- items[i]->d.rect.width = brect.height;
- if (items[i]->d.data) {
- if (!strncmp(items[i]->d.data, "%m:", 3))
+ 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))
/* meter */
- items[i]->d.rect.width = brect.height / 2;
+ item->d.rect.width = brect.height / 2;
else
- items[i]->d.rect.width +=
- XTextWidth(font, items[i]->d.data,
- strlen(items[i]->d.data));
+ item->d.rect.width += XTextWidth(font, item->d.data, strlen(item->d.data));
}
- w += items[i]->d.rect.width;
+ w += item->d.rect.width;
}
- if (!expandable || w > brect.width) {
+ if (w > brect.width) {
/* failsafe mode, give all labels same width */
- w = brect.width / n;
- for (i = 0; items[i]; i++)
- items[i]->d.rect.width = w;
- items[i - 1]->d.rect.width = brect.width - items[i - 1]->d.rect.x;
- } else
- items[expandable - 1]->d.rect.width = brect.width - w;
-
- for (i = 0; items[i]; i++) {
- items[i]->d.font = font;
- items[i]->d.rect.x = xoff;
- xoff += items[i]->d.rect.width;
- if (items[i]->d.data && !strncmp(items[i]->d.data, "%m:", 3))
- blitz_drawmeter(dpy, &items[i]->d);
+ 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);
else
- blitz_drawlabel(dpy, &items[i]->d);
+ blitz_drawlabel(dpy, &item->d);
}
XCopyArea(dpy, pmap, win, gc, 0, 0, brect.width, brect.height, 0, 0);
XSync(dpy, False);
- XFreeFont(dpy, font);
}
static void draw_bar(void *obj, char *arg)
@@ -254,14 +257,11 @@ static void draw_bar(void *obj, char *arg)
if (!displayed)
return;
- if (items) {
- for (i = 0; items[i]; i++) {
- free(items[i]);
- }
- free(items);
+ while ((item = cext_stack_get_top_item(&items))) {
+ cext_detach_item(&items, item);
+ free(item);
}
- items = 0;
- snprintf(buf, sizeof(buf), "%s", "/1");
+ snprintf(buf, sizeof(buf), "%s", "/0");
label = ixp_walk(ixps, buf);
if (!label) {
Draw d = { 0 };
@@ -270,13 +270,9 @@ static void draw_bar(void *obj, char *arg)
d.drawable = pmap;
d.rect.width = brect.width;
d.rect.height = brect.height;
- d.bg =
- blitz_loadcolor(dpy, screen_num, files[B_BG_COLOR]->content);
- d.fg =
- blitz_loadcolor(dpy, screen_num, files[B_FG_COLOR]->content);
- d.border =
- blitz_loadcolor(dpy, screen_num,
- files[B_BORDER_COLOR]->content);
+ 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);
blitz_drawlabelnoborder(dpy, &d);
} else {
File *f;
@@ -296,43 +292,51 @@ static void draw_bar(void *obj, char *arg)
for (i = 0; i < n; i++) {
snprintf(buf, sizeof(buf), "/%s", paths[i]);
item = cext_emallocz(sizeof(Item));
- items = (Item **) attach_item_end((void **) items, item, sizeof(Item *));
init_item(buf, item);
+ cext_attach_item(&items, item);
}
draw();
free(paths);
}
}
-static Item *get_item_for_file(File * f)
+static Item *get_item_for_file(File *f)
{
- int i;
- for (i = 0; items && items[i]; i++)
- if (items[i]->root == f)
- return items[i];
- return 0;
+ unsigned int i;
+ size_t size = cext_sizeof(&items);
+ Item *item;
+ for (i = 0; i < size; i++) {
+ item = cext_list_get_item(&items, i);
+ if (f == item->root)
+ return item;
+ }
+ return nil;
}
-static void handle_buttonpress(XButtonPressedEvent * e)
+static void iter_buttonpress(void *item, void *bpress)
{
+ XButtonPressedEvent *e = bpress;
+ Item *i = item;
File *p;
char buf[MAX_BUF];
char path[512];
- int i;
- for (i = 0; items && items[i]; i++) {
- if (blitz_ispointinrect(e->x, e->y, &items[i]->d.rect)) {
- path[0] = 0;
- wmii_get_ixppath(items[i]->root, path, sizeof(path));
- snprintf(buf, MAX_BUF, "%s/b%upress", path, e->button);
- if ((p = ixp_walk(ixps, buf)))
- if (p->content)
- spawn(dpy, p->content);
- return;
- }
+ 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)
+ spawn(dpy, p->content);
+ return;
}
}
+static void handle_buttonpress(XButtonPressedEvent *e)
+{
+ cext_iterate(&items, e, iter_buttonpress);
+}
+
static void check_event(Connection * e)
{
XEvent ev;
@@ -378,8 +382,8 @@ static void handle_after_write(IXPServer * s, File * f)
init_draw_label(buf, &item->d);
draw();
}
- } else if (files[B_GEOMETRY] == f) {
- char *geom = files[B_GEOMETRY]->content;
+ } else if (file[B_GEOMETRY] == f) {
+ char *geom = file[B_GEOMETRY]->content;
if (geom && strrchr(geom, ',')) {
update_geometry(geom);
XMoveResizeWindow(dpy, win, brect.x, brect.y,
@@ -390,7 +394,7 @@ static void handle_after_write(IXPServer * s, File * f)
XSync(dpy, False);
draw_bar(0, 0);
}
- } else if (files[B_CTL] == f) {
+ } else if (file[B_CTL] == f) {
for (i = 0; acttbl[i].name; i++) {
len = strlen(acttbl[i].name);
if (!strncmp(acttbl[i].name, (char *) f->content, len)) {
@@ -402,6 +406,9 @@ static void handle_after_write(IXPServer * s, File * f)
break;
}
}
+ } else if (file[B_FONT] == f) {
+ XFreeFont(dpy, font);
+ font = blitz_getfont(dpy, file[B_FONT]->content);
}
check_event(0);
}
@@ -409,14 +416,14 @@ static void handle_after_write(IXPServer * s, File * f)
static void handle_before_read(IXPServer * s, File * f)
{
char buf[64];
- if (f == files[B_GEOMETRY]) {
+ if (f == file[B_GEOMETRY]) {
snprintf(buf, sizeof(buf), "%d,%d,%d,%d", brect.x, brect.y,
brect.width, brect.height);
if (f->content)
free(f->content);
f->content = strdup(buf);
f->size = strlen(buf);
- } else if (f == files[B_NEW]) {
+ } else if (f == file[B_NEW]) {
snprintf(buf, sizeof(buf), "%d", ++id);
if (f->content)
free(f->content);
@@ -433,30 +440,27 @@ static void run(char *geom)
XGCValues gcv;
/* init */
- if (!(files[B_CTL] = ixp_create(ixps, "/ctl"))) {
+ if (!(file[B_CTL] = ixp_create(ixps, "/ctl"))) {
perror("wmibar: cannot connect IXP server");
exit(1);
}
- files[B_CTL]->after_write = handle_after_write;
- files[B_NEW] = ixp_create(ixps, "/new");
- files[B_NEW]->before_read = handle_before_read;
- files[B_FONT] = wmii_create_ixpfile(ixps, "/font", BLITZ_FONT);
- files[B_BG_COLOR] =
- wmii_create_ixpfile(ixps, "/bgcolor", BLITZ_NORM_BG_COLOR);
- files[B_FG_COLOR] =
- wmii_create_ixpfile(ixps, "/fgcolor", BLITZ_NORM_FG_COLOR);
- files[B_BORDER_COLOR] =
- wmii_create_ixpfile(ixps, "/bordercolor", BLITZ_NORM_BORDER_COLOR);
- files[B_GEOMETRY] = ixp_create(ixps, "/geometry");
- files[B_GEOMETRY]->before_read = handle_before_read;
- files[B_GEOMETRY]->after_write = handle_after_write;
- files[B_EXPANDABLE] = ixp_create(ixps, "/expandable");
+ 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");
wa.override_redirect = 1;
wa.background_pixmap = ParentRelative;
- wa.event_mask =
- ExposureMask | ButtonPressMask | SubstructureRedirectMask |
- SubstructureNotifyMask;
+ wa.event_mask = ExposureMask | ButtonPressMask | SubstructureRedirectMask | SubstructureNotifyMask;
brect.x = brect.y = brect.width = brect.height = 0;
rect.x = rect.y = 0;
@@ -465,11 +469,9 @@ static void run(char *geom)
update_geometry(geom);
win = XCreateWindow(dpy, RootWindow(dpy, screen_num), brect.x, brect.y,
- brect.width, brect.height, 0, DefaultDepth(dpy,
- screen_num),
+ brect.width, brect.height, 0, DefaultDepth(dpy, screen_num),
CopyFromParent, DefaultVisual(dpy, screen_num),
- CWOverrideRedirect | CWBackPixmap | CWEventMask,
- &wa);
+ CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_left_ptr));
XSync(dpy, False);
@@ -477,13 +479,10 @@ static void run(char *geom)
gcv.graphics_exposures = False;
gc = XCreateGC(dpy, win, 0, 0);
- pmap =
- XCreatePixmap(dpy, win, brect.width, brect.height,
- DefaultDepth(dpy, screen_num));
+ pmap = XCreatePixmap(dpy, win, brect.width, brect.height, DefaultDepth(dpy, screen_num));
/* main event loop */
- run_server_with_fd_support(ixps, ConnectionNumber(dpy),
- check_event, 0);
+ run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, 0);
deinit_server(ixps);
XFreePixmap(dpy, pmap);
XFreeGC(dpy, gc);
diff --git a/cmd/wmibar2.c b/cmd/wmibar2.c
@@ -88,7 +88,7 @@ typedef struct {
char event[5][256];
} Item;
-static Item **items = 0;
+static Container items = {0};
static char *sockfile = 0;
static pid_t mypid = 0;
static IXPServer srv = { 0 };
@@ -155,63 +155,67 @@ qpath_file(u64 path)
}
*/
-static Map *fid_to_map(Map ** maps, u32 fid)
+static int comp_fid(void *fid, void *map)
{
- u32 i;
- for (i = 0; maps && maps[i]; i++)
- if (maps[i]->fid == fid)
- return maps[i];
- return nil;
+ return ((Map *)map)->fid == *(u32 *)fid;
}
-static int qfile_index(char *name, u16 * index)
+static Map *fid_to_map(Container *maps, u32 fid)
+{
+ return cext_find_item(maps, &fid, comp_fid);
+}
+
+static Bool qfile_index(char *name, u16 * index)
{
int i;
for (i = 0; qfilelist[i].name; i++)
if (!strncmp(name, qfilelist[i].name, strlen(qfilelist[i].name))) {
*index = i;
- return TRUE;
+ return True;
}
- return FALSE;
+ return False;
}
-static int make_qid(Qid * dir, char *wname, Qid * new)
+static Bool make_qid(Qid * dir, char *wname, Qid * new)
{
u16 idx;
const char *errstr;
if (dir->type != IXP_QTDIR)
- return FALSE;
+ return False;
new->version = 0;
if (!qfile_index(wname, &idx)) {
new->type = IXP_QTDIR;
if (!strncmp(wname, "..", 3)) {
*new = root_qid;
- return TRUE;
+ return True;
} else if (!strncmp(wname, "default", 8)) {
new->path = make_qpath(Ditem, 0, NONE);
- return TRUE;
+ return True;
}
/* check if wname is a number, otherwise file not found */
idx = (u16) cext_strtonum(wname, 1, 0xffff, &errstr);
- if (errstr || count_items((void **) items) < idx)
- return FALSE;
+ if (errstr || cext_sizeof(&items) < idx)
+ return False;
/* found */
new->path = make_qpath(Ditem, idx, NONE);
} else {
new->type = IXP_QTFILE;
- new->path =
- make_qpath(qfilelist[idx].type, qpath_item(dir->path), idx);
+ new->path = make_qpath(qfilelist[idx].type, qpath_item(dir->path), idx);
}
- return TRUE;
+ return True;
}
-static int attach(IXPServer * s, IXPConn * c)
+static int attach(IXPServer *s, IXPConn *c)
{
+ Container *cont = c->aux;
Map *map = cext_emallocz(sizeof(Map));
+
+ if (!cont)
+ cont = c->aux = cext_emallocz(sizeof(Container));
fprintf(stderr, "attaching %d %s %s\n", s->fcall.afid, s->fcall.uname, s->fcall.aname);
map->qid = root_qid;
map->fid = s->fcall.fid;
- c->aux = (Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
+ cext_attach_item(cont, map);
s->fcall.id = RATTACH;
s->fcall.qid = root_qid;
return TRUE;
@@ -235,9 +239,7 @@ static int walk(IXPServer * s, IXPConn * c)
}
if (s->fcall.nwname) {
qid = map->qid;
- for (nwqid = 0; (nwqid < s->fcall.nwname)
- && make_qid(&qid, s->fcall.wname[nwqid],
- &s->fcall.wqid[nwqid]); nwqid++)
+ for (nwqid = 0; (nwqid < s->fcall.nwname) && make_qid(&qid, s->fcall.wname[nwqid], &s->fcall.wqid[nwqid]); nwqid++)
qid = s->fcall.wqid[nwqid];
if (!nwqid) {
s->errstr = "file not found";
@@ -249,14 +251,15 @@ static int walk(IXPServer * s, IXPConn * c)
* the walk was complete
*/
if (nwqid == s->fcall.nwname) {
+ Container *cont = c->aux;
if (s->fcall.fid == s->fcall.newfid) {
- c->aux = (Map **) detach_item((void **) c->aux, map, sizeof(Map *));
+ cext_detach_item(cont, map);
free(map);
}
map = cext_emallocz(sizeof(Map));
map->qid = qid;
map->fid = s->fcall.newfid;
- c->aux = (Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
+ cext_attach_item(cont, map);
}
s->fcall.id = RWALK;
s->fcall.nwqid = nwqid;
@@ -345,22 +348,22 @@ static int _read(IXPServer * s, IXPConn * c)
return TRUE;
}
-static int _write(IXPServer * s, IXPConn * c)
+static int _write(IXPServer *s, IXPConn *c)
{
-
return FALSE;
}
-static int clunk(IXPServer * s, IXPConn * c)
+static int clunk(IXPServer *s, IXPConn *c)
{
- Map *map = fid_to_map(c->aux, s->fcall.fid);
+ Container *cont = c->aux;
+ Map *map = fid_to_map(cont, s->fcall.fid);
if (!map) {
s->errstr = "invalid fid";
return FALSE;
}
- c->aux = (Map **) detach_item((void **) c->aux, map, sizeof(Map *));
+ cext_detach_item(cont, map);
free(map);
s->fcall.id = RCLUNK;
return TRUE;
@@ -368,12 +371,10 @@ static int clunk(IXPServer * s, IXPConn * c)
static void freeconn(IXPServer * s, IXPConn * c)
{
- Map **maps = c->aux;
- if (maps) {
- int i;
- for (i = 0; maps[i]; i++)
- free(maps[i]);
- free(maps);
+ Container *cont = c->aux;
+ if (cont) {
+ free(cont);
+ c->aux = 0;
}
}
diff --git a/cmd/wmifs.c b/cmd/wmifs.c
@@ -33,12 +33,11 @@ static Display *dpy;
static IXPServer *ixps;
static char *sockfile = 0;
static File *files[F_LAST];
-static Bind **bindings = 0;
+static Container bindings = {0};
static void quit(void *obj, char *arg);
static void bind(void *obj, char *arg);
static void unbind(void *obj, char *arg);
-static Bind *path_to_bind(char *path);
static Action acttbl[] = {
{"quit", quit},
@@ -60,54 +59,48 @@ static void usage()
exit(1);
}
-static void quit(void *obj, char *arg)
-{
- int i;
- for (i = 0; bindings && bindings[i]; i++) {
- if (bindings[i]->mount) {
- bindings[i]->mount->content = 0;
- ixp_remove(ixps, bindings[i]->prefix);
- if (ixps->errstr)
- fprintf(stderr, "wmifs: error on quit: remove %s: %s\n",
- bindings[i]->prefix, ixps->errstr);
- }
- /* free stuff */
- if (bindings[i]->prefix)
- free(bindings[i]->prefix);
- free(bindings[i]);
- }
- free(bindings);
- bindings = 0;
- ixps->runlevel = SHUTDOWN;
-}
-
-static void _unbind(Bind * b)
+static void iter_unbind(void *bind, void *aux)
{
- bindings =
- (Bind **) detach_item((void **) bindings, b, sizeof(Bind *));
-
+ Bind *b = bind;
if (b->mount) {
b->mount->content = 0;
ixp_remove(ixps, b->prefix);
if (ixps->errstr)
- fprintf(stderr, "wmifs: error on _unbind: remove %s: %s\n",
- b->prefix, ixps->errstr);
+ fprintf(stderr, "wmifs: error on unbind %s: %s\n", b->prefix, ixps->errstr);
}
+}
+
+static void quit(void *obj, char *arg)
+{
+ cext_iterate(&bindings, nil, iter_unbind);
+ ixps->runlevel = SHUTDOWN;
+}
+
+static void do_unbind(Bind * b)
+{
+ cext_detach_item(&bindings, b);
+ iter_unbind(b, nil);
/* free stuff */
deinit_client(b->client);
free(b->prefix);
free(b);
}
+static int comp_bindpath(void *path, void *bind)
+{
+ Bind *b = bind;
+ return !strncmp(b->prefix, path, strlen(b->prefix));
+}
+
static void unbind(void *obj, char *arg)
{
- Bind *b = path_to_bind(arg);
+ Bind *b = cext_find_item(&bindings, arg, comp_bindpath);
if (!b) {
fprintf(stderr, "wmifs: unbind: '%s' no such path\n", arg);
return;
}
- _unbind(b);
+ do_unbind(b);
}
static void bind(void *obj, char *arg)
@@ -121,26 +114,20 @@ static void bind(void *obj, char *arg)
cext_strlcpy(cmd, arg, sizeof(cmd));
sfile = strchr(cmd, ' ');
if (!sfile) {
- fprintf(stderr,
- "wmifs: bind: '%s' without socket argument, ignoring\n",
- arg);
+ fprintf(stderr, "wmifs: bind: '%s' without socket argument, ignoring\n", arg);
return; /* shortcut with empty argument */
}
*sfile = 0;
sfile++;
if (*sfile == 0) {
- fprintf(stderr,
- "wmifs: bind: '%s' without socket argument, ignoring\n",
- arg);
+ fprintf(stderr, "wmifs: bind: '%s' without socket argument, ignoring\n", arg);
return; /* shortcut with empty argument */
}
b = cext_emallocz(sizeof(Bind));
b->client = init_ixp_client(sfile);
if (!b->client) {
- fprintf(stderr,
- "wmifs: bind: cannot connect to server '%s', ignoring\n",
- sfile);
+ fprintf(stderr, "wmifs: bind: cannot connect to server '%s', ignoring\n", sfile);
free(b);
return;
}
@@ -148,8 +135,7 @@ static void bind(void *obj, char *arg)
b->mount = ixp_create(ixps, b->prefix);
b->mount->content = b->mount; /* shall be a directory */
- bindings =
- (Bind **) attach_item_end((void **) bindings, b, sizeof(Bind *));
+ cext_attach_item(&bindings, b);
}
static void handle_after_write(IXPServer * s, File * f)
@@ -170,37 +156,30 @@ static void handle_after_write(IXPServer * s, File * f)
}
}
-static Bind *path_to_bind(char *path)
-{
- int i;
- for (i = 0; bindings && bindings[i]; i++)
- if (!strncmp
- (bindings[i]->prefix, path, strlen(bindings[i]->prefix)))
- return bindings[i];
- return 0;
-}
-
static Bind *fd_to_bind(int fd, int *client_fd)
{
File *f = fd_to_file(ixps, fd);
- int i, j;
+ unsigned int i, j;
+ Bind *b;
+ size_t size = cext_sizeof(&bindings);
if (!f)
- return 0;
- for (i = 0; bindings && bindings[i]; i++) {
+ return nil;
+ for (i = 0; i < size; i++) {
+ b = cext_list_get_item(&bindings, i);
for (j = 0; j < MAX_CONN * MAX_OPEN_FILES; j++) {
- if (&bindings[i]->route[j].dest == f) {
- *client_fd = bindings[i]->route[j].src;
- return bindings[i];
+ if (&b->route[j].dest == f) {
+ *client_fd = b->route[j].src;
+ return b;
}
}
}
- return 0;
+ return nil;
}
static File *fixp_create(IXPServer * s, char *path)
{
- Bind *b = path_to_bind(path);
+ Bind *b = cext_find_item(&bindings, path, comp_bindpath);
size_t len;
if (!b) {
@@ -212,14 +191,14 @@ static File *fixp_create(IXPServer * s, char *path)
b->client->create(b->client, path[len] == 0 ? "/" : &path[len]);
if (b->client->errstr) {
if (!strcmp(b->client->errstr, DEAD_SERVER))
- _unbind(b);
+ do_unbind(b);
}
- return 0;
+ return nil;
}
static File *fixp_open(IXPServer * s, char *path)
{
- Bind *b = path_to_bind(path);
+ Bind *b = cext_find_item(&bindings, path, comp_bindpath);
int fd;
size_t len;
@@ -233,8 +212,8 @@ static File *fixp_open(IXPServer * s, char *path)
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
- _unbind(b);
- return 0;
+ do_unbind(b);
+ return nil;
}
b->route[fd].src = fd;
return &b->route[fd].dest;
@@ -257,7 +236,7 @@ fixp_read(IXPServer * s, int fd, size_t offset, void *out_buf,
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
- _unbind(b);
+ do_unbind(b);
}
return result;
}
@@ -278,7 +257,7 @@ fixp_write(IXPServer * s, int fd, size_t offset, void *content,
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
- _unbind(b);
+ do_unbind(b);
}
}
@@ -296,13 +275,13 @@ static void fixp_close(IXPServer * s, int fd)
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
- _unbind(b);
+ do_unbind(b);
}
}
static void fixp_remove(IXPServer * s, char *path)
{
- Bind *b = path_to_bind(path);
+ Bind *b = cext_find_item(&bindings, path, comp_bindpath);
size_t len;
if (!b) {
@@ -315,7 +294,7 @@ static void fixp_remove(IXPServer * s, char *path)
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
- _unbind(b);
+ do_unbind(b);
}
}
diff --git a/cmd/wmikeys.c b/cmd/wmikeys.c
@@ -47,7 +47,7 @@ static XRectangle krect;
static XRectangle rect;
static int screen_num;
static char *sockfile = 0;
-static Shortcut **shortcuts = 0;
+static Container shortcuts = {0};
static File *files[K_LAST];
static int grabkb = 0;
static unsigned int num_lock_mask, valid_mask;
@@ -140,9 +140,7 @@ static void create_shortcut(File * f)
}
if (r) {
s->cmdfile = f;
- shortcuts =
- (Shortcut **) attach_item_end((void **) shortcuts, r,
- sizeof(Shortcut *));
+ cext_attach_item(&shortcuts, r);
grab_shortcut(r);
}
}
@@ -223,38 +221,43 @@ handle_shortcut_chain(Window w, Shortcut * processed, char *prefix,
}
}
+static int comp_shortcut(void *comp_short, void *shortcut)
+{
+ Shortcut *comp = comp_short;
+ Shortcut *s = shortcut;
+ return (s->mod == comp->mod) && (s->key == comp->key);
+}
+
static void handle_shortcut_gkb(Window w, unsigned long mod, KeyCode key)
{
- int i;
- Shortcut *s;
+ Shortcut comp, *s;
if (!files[K_LOOKUP]->content)
return;
- for (i = 0; shortcuts && shortcuts[i]; i++) {
- s = shortcuts[i];
- if ((s->mod == mod) && (s->key == key)) {
- if (s->cmdfile && s->cmdfile->content)
- spawn(dpy, s->cmdfile->content);
- return;
- }
+
+ comp.mod = mod;
+ comp.key = key;
+
+ s = cext_find_item(&shortcuts, &s, comp_shortcut);
+ if (s && s->cmdfile && s->cmdfile->content) {
+ spawn(dpy, s->cmdfile->content);
+ return;
}
XBell(dpy, 0);
}
static void handle_shortcut(Window w, unsigned long mod, KeyCode key)
{
- int i;
- Shortcut *s;
+ Shortcut comp, *s;
if (!files[K_LOOKUP]->content)
return;
- for (i = 0; shortcuts && shortcuts[i]; i++) {
- s = shortcuts[i];
- if ((s->mod == mod) && (s->key == key)) {
- if (s->cmdfile && s->cmdfile->content) {
- spawn(dpy, s->cmdfile->content);
- return;
- }
- break;
- }
+
+ comp.mod = mod;
+ comp.key = key;
+
+ s = cext_find_item(&shortcuts, &s, comp_shortcut);
+ if (s && s->cmdfile && s->cmdfile->content) {
+ spawn(dpy, s->cmdfile->content);
+ return;
}
if (s->next)
handle_shortcut_chain(w, s, s->name, 1);
@@ -267,7 +270,7 @@ static void quit(void *obj, char *arg)
static void update()
{
- int i;
+ Shortcut *s;
File *f, *p;
if (!files[K_LOOKUP]->content)
return;
@@ -278,10 +281,10 @@ static void update()
return; /* cannot update */
/* destroy existing shortcuts if any */
- for (i = 0; shortcuts && shortcuts[i]; i++)
- destroy_shortcut(shortcuts[i], 1);
- free(shortcuts);
- shortcuts = 0;
+ while ((s = cext_stack_get_top_item(&shortcuts))) {
+ cext_detach_item(&shortcuts, s);
+ destroy_shortcut(s, 1);
+ }
if (grabkb) {
XGrabKeyboard(dpy, root, True, GrabModeAsync,
diff --git a/cmd/wmimenu.c b/cmd/wmimenu.c
@@ -33,7 +33,6 @@ typedef enum {
M_NORM_BG_COLOR,
M_NORM_TEXT_COLOR,
M_NORM_BORDER_COLOR,
- M_RETARDED,
M_LAST
} InputIndexes;
@@ -50,13 +49,10 @@ static XRectangle mrect;
static int screen_num;
static char *sockfile = 0;
static File *files[M_LAST];
-static File **items = 0;
-static size_t item_size = 0;
-static int item = 0;
+static Container items = {0};
+static Container history = {0};
static int offset[OFF_LAST];
static unsigned int cmdw = 0;
-static File **history = 0;
-static int sel_history = 0;
static Pixmap pmap;
static const int seek = 30; /* 30px */
static XFontStruct *font;
@@ -92,23 +88,21 @@ static void add_history(char *cmd)
{
char buf[MAX_BUF];
snprintf(buf, MAX_BUF, "/history/%ld", (long) time(0));
- history = (File **) attach_item_begin((void **) history, wmii_create_ixpfile(ixps, buf, cmd), sizeof(File *));
+ cext_attach_item(&history, wmii_create_ixpfile(ixps, buf, cmd));
}
-static void _exec(char *cmd)
+static void exec_item(char *cmd)
{
+ File *item = cext_stack_get_top_item(&items);
char *rc = cmd;
if (!cmd || cmd[0] == 0)
return;
- if (items && items[0]) {
- if ((item >= 0) && items[item] && items[item]->size)
- rc = cmd = items[item]->content;
- else if ((item == -1) && items[0]->size) /* autolight */
- rc = cmd = items[0]->content;
- }
+ if (item && item->size)
+ rc = cmd = item->content;
add_history(cmd);
+
if (files[M_PRE_COMMAND]->content) {
size_t len = strlen(cmd) + files[M_PRE_COMMAND]->size + 2;
rc = cext_emallocz(len);
@@ -172,17 +166,15 @@ void set_text(char *text)
static void update_offsets()
{
- int i;
- unsigned int w = cmdw + 2 * seek;
+ File *item;
+ unsigned int i, w = cmdw + 2 * seek;
- if (!items)
+ if (!cext_stack_get_top_item(&items))
return;
-
/* calc next offset */
- for (i = offset[OFF_CURR]; items[i]; i++) {
- w += XTextWidth(font, items[i]->content,
- strlen(items[i]->content)) + mrect.height;
+ for (i = offset[OFF_CURR]; (item = cext_list_get_item(&items, i)); i++) {
+ w += XTextWidth(font, item->content, strlen(item->content)) + mrect.height;
if (w > mrect.width)
break;
}
@@ -191,8 +183,8 @@ static void update_offsets()
w = cmdw + 2 * seek;
for (i = offset[OFF_CURR] - 1; i >= 0; i--) {
- w += XTextWidth(font, items[i]->content,
- strlen(items[i]->content)) + mrect.height;
+ item = cext_list_get_item(&items, i);
+ w += XTextWidth(font, item->content, strlen(item->content)) + mrect.height;
if (w > mrect.width)
break;
}
@@ -201,12 +193,11 @@ static void update_offsets()
static int update_items(char *pattern)
{
- size_t plen = pattern ? strlen(pattern) : 0, size = 0, max = 0, len;
+ size_t plen = pattern ? strlen(pattern) : 0, len, max = 0, size;
int matched = pattern ? plen == 0 : 1;
- File *f, *p, *maxitem = 0;
+ File *f, *p, *maxitem;
cmdw = 0;
- item = -1;
offset[OFF_CURR] = offset[OFF_PREV] = offset[OFF_NEXT] = 0;
if (!files[M_LOOKUP]->content)
@@ -215,39 +206,26 @@ static int update_items(char *pattern)
if (!f || !is_directory(f))
return 0;
+ while ((p = cext_stack_get_top_item(&items)))
+ cext_detach_item(&items, p);
+
/* build new items */
for (p = f->content; p; p = p->next) {
- size++;
len = strlen(p->name);
if (max < len) {
maxitem = p;
max = len;
}
}
-
- if (maxitem) {
- if (files[M_RETARDED]->content)
- free(files[M_RETARDED]->content);
- files[M_RETARDED]->content = strdup(maxitem->name);
- files[M_RETARDED]->size = max;
+
+ if (maxitem)
cmdw = XTextWidth(font, maxitem->name, max) + mrect.height;
- }
- if (size > item_size) {
- /* stores always the biggest amount of items in memory */
- if (items)
- free((File **) items);
- items = 0;
- item_size = size;
- if (item_size)
- items = (File **) cext_emallocz((item_size + 1) * sizeof(File *));
- }
- size = 0;
for (p = f->content; p; p = p->next) {
if (!p->content)
continue; /* ignore bogus files */
if (matched || !strncmp(pattern, p->name, plen)) {
- items[size++] = p;
+ cext_attach_item(&items, p);
p->parent = 0; /* HACK to prevent doubled items */
}
}
@@ -256,11 +234,11 @@ static int update_items(char *pattern)
if (!p->content)
continue; /* ignore bogus files */
if (p->parent && strstr(p->name, pattern))
- items[size++] = p;
+ cext_attach_item(&items, p);
else
p->parent = f; /* restore HACK */
}
- items[size] = 0;
+
update_offsets();
return size;
}
@@ -271,6 +249,7 @@ static void draw_menu()
Draw d = { 0 };
unsigned int offx = 0;
int i = 0;
+ File *item, *top = cext_stack_get_top_item(&items);
d.gc = gc;
d.font = font;
@@ -285,17 +264,15 @@ static void draw_menu()
/* print command */
d.align = WEST;
d.font = font;
- d.fg =
- blitz_loadcolor(dpy, screen_num,
- files[M_NORM_TEXT_COLOR]->content);
+ d.fg = blitz_loadcolor(dpy, screen_num, files[M_NORM_TEXT_COLOR]->content);
d.data = files[M_COMMAND]->content;
- if (cmdw && items && items[0])
+ if (cmdw && item)
d.rect.width = cmdw;
offx += d.rect.width;
blitz_drawlabelnoborder(dpy, &d);
d.align = CENTER;
- if (items && items[0]) {
+ if (top) {
d.bg = blitz_loadcolor(dpy, screen_num, files[M_NORM_BG_COLOR]->content);
d.fg = blitz_loadcolor(dpy, screen_num, files[M_NORM_TEXT_COLOR]->content);
d.data = offset[OFF_CURR] ? "<" : 0;
@@ -305,23 +282,16 @@ static void draw_menu()
blitz_drawlabelnoborder(dpy, &d);
/* determine maximum items */
- for (i = offset[OFF_CURR]; items[i] && (i < offset[OFF_NEXT]); i++) {
- d.data = items[i]->name;
+ for (i = offset[OFF_CURR]; (i < offset[OFF_NEXT]) && (item = cext_list_get_item(&items, i)); i++) {
+ d.data = item->name;
d.rect.x = offx;
- d.rect.width =
- XTextWidth(d.font, d.data, strlen(d.data)) + mrect.height;
+ d.rect.width = XTextWidth(d.font, d.data, strlen(d.data)) + mrect.height;
offx += d.rect.width;
- if (i == item) {
+ if (top == item) {
d.bg = blitz_loadcolor(dpy, screen_num, files[M_SEL_BG_COLOR]->content);
d.fg = blitz_loadcolor(dpy, screen_num, files[M_SEL_TEXT_COLOR]->content);
d.border = blitz_loadcolor(dpy, screen_num, files[M_SEL_BORDER_COLOR]-> content);
blitz_drawlabel(dpy, &d);
- } else if (!i && item == -1) {
- /* fg and bg are inverted */
- d.fg = blitz_loadcolor(dpy, screen_num, files[M_SEL_BG_COLOR]->content);
- d.bg = blitz_loadcolor(dpy, screen_num, files[M_SEL_TEXT_COLOR]->content);
- d.border = blitz_loadcolor(dpy, screen_num, files[M_SEL_BORDER_COLOR]->content);
- blitz_drawlabel(dpy, &d);
} else {
d.bg = blitz_loadcolor(dpy, screen_num, files[M_NORM_BG_COLOR]->content);
d.fg = blitz_loadcolor(dpy, screen_num, files[M_NORM_TEXT_COLOR]->content);
@@ -332,7 +302,7 @@ static void draw_menu()
d.bg = blitz_loadcolor(dpy, screen_num, files[M_NORM_BG_COLOR]->content);
d.fg = blitz_loadcolor(dpy, screen_num, files[M_NORM_TEXT_COLOR]->content);
- d.data = items[i] ? ">" : 0;
+ d.data = item ? ">" : 0;
d.rect.x = mrect.width - seek;
d.rect.width = seek;
blitz_drawlabelnoborder(dpy, &d);
@@ -345,9 +315,11 @@ static void handle_kpress(XKeyEvent * e)
{
KeySym ksym;
char buf[32];
- int idx, num;
+ int num;
static char text[4096];
- size_t len = 0;
+ size_t len = 0, size = cext_sizeof(&items);
+ File *top = cext_stack_get_top_item(&items);
+ File *hist = cext_stack_get_top_item(&history);
text[0] = 0;
if (files[M_COMMAND]->content) {
@@ -394,58 +366,51 @@ static void handle_kpress(XKeyEvent * e)
}
switch (ksym) {
case XK_Left:
- if (!items || !items[0])
+ if (!top)
return;
- if (item > 0) {
- item--;
- set_text(items[item]->name);
+ if (top != cext_list_get_item(&items, 0)) {
+ top = cext_list_get_prev_item(&items, top);
+ cext_stack_top_item(&items, top);
+ set_text(top->name);
} else
return;
break;
case XK_Right:
case XK_Tab:
- if (!items || !items[0])
+ if (!top)
return;
- if (items[item + 1]) {
- item++;
- set_text(items[item]->name);
+ if (top != cext_list_get_item(&items, size - 1)) {
+ top = cext_list_get_next_item(&items, top);
+ cext_stack_top_item(&items, top);
+ set_text(top->name);
} else
return;
break;
case XK_Down:
- if (history) {
- set_text(history[sel_history]->content);
- idx = index_prev_item((void **) history, history[sel_history]);
- if (idx >= 0)
- sel_history = idx;
+ if (hist) {
+ set_text(hist->content);
+ cext_stack_top_item(&history, cext_list_get_next_item(&items, hist));
}
update_items(files[M_COMMAND]->content);
break;
case XK_Up:
- if (history) {
- set_text(history[sel_history]->content);
- idx = index_next_item((void **) history, history[sel_history]);
- if (idx >= 0)
- sel_history = idx;
+ if (hist) {
+ set_text(hist->content);
+ cext_stack_top_item(&history, cext_list_get_prev_item(&items, hist));
}
update_items(files[M_COMMAND]->content);
break;
case XK_Return:
- if (items && items[0]) {
- if (item >= 0)
- _exec(items[item]->name);
- else
- _exec(items[0]->name);
+ if (top) {
+ exec_item(top->name);
} else if (text)
- _exec(text);
+ exec_item(text);
case XK_Escape:
hide();
break;
case XK_BackSpace:
if (len) {
- int size = 0;
size_t i = len;
- for (size = 0; items && items[size]; size++);
if (i) {
do
text[--i] = 0;
@@ -466,11 +431,12 @@ static void handle_kpress(XKeyEvent * e)
update_items(files[M_COMMAND]->content);
}
}
- if (items && item > 0) {
- if (item < offset[OFF_CURR]) {
+ if (top) {
+ int idx = cext_list_get_item_index(&items, top);
+ if (idx < offset[OFF_CURR]) {
offset[OFF_CURR] = offset[OFF_PREV];
update_offsets();
- } else if (item >= offset[OFF_NEXT]) {
+ } else if (idx >= offset[OFF_NEXT]) {
offset[OFF_CURR] = offset[OFF_NEXT];
update_offsets();
}
@@ -579,7 +545,6 @@ static void run(char *size)
files[M_NORM_BG_COLOR] = wmii_create_ixpfile(ixps, "/nstyle/bgcolor", BLITZ_NORM_BG_COLOR);
files[M_NORM_TEXT_COLOR] = wmii_create_ixpfile(ixps, "/nstyle/fgcolor", BLITZ_NORM_FG_COLOR);
files[M_NORM_BORDER_COLOR] = wmii_create_ixpfile(ixps, "/nstyle/bordercolor", BLITZ_NORM_BORDER_COLOR);
- files[M_RETARDED] = ixp_create(ixps, "/retarded");
wa.override_redirect = 1;
wa.background_pixmap = ParentRelative;
@@ -653,8 +618,6 @@ int main(int argc, char *argv[])
cext_strlcpy(size, argv[i], sizeof(size));
ixps = wmii_setup_server(sockfile);
- items = 0;
-
run(size);
return 0;
diff --git a/libcext/cext.h b/libcext/cext.h
@@ -34,23 +34,17 @@ struct Container {
void cext_attach_item(Container *c, void *item);
void cext_detach_item(Container *c, void *item);
void *cext_find_item(Container *c, void *pattern, int (*comp)(void *pattern, void *item));
-void cext_top_item(Container *c, void *item);
void cext_iterate(Container *c, void *aux, void (*iter)(void *, void *aux));
-void *cext_get_top_item(Container *c);
-void *cext_get_down_item(Container *c, void *item);
-void *cext_get_up_item(Container *c, void *item);
-void *cext_get_item(Container *c, size_t index);
-int cext_get_item_index(Container *c, void *item);
-size_t cext_sizeof(Container *c);
void cext_swap_items(Container *c, void *item1, void *item2);
-
-void **attach_item_begin(void **old, void *item, size_t size_item);
-void **attach_item_end(void **old, void *item, size_t size_item);
-void **detach_item(void **old, void *item, size_t size_item);
-int index_item(void **items, void *item);
-int count_items(void **items);
-int index_next_item(void **items, void *item);
-int index_prev_item(void **items, void *item);
+size_t cext_sizeof(Container *c);
+void cext_stack_top_item(Container *c, void *item);
+void *cext_stack_get_top_item(Container *c);
+void *cext_stack_get_down_item(Container *c, void *item);
+void *cext_stack_get_up_item(Container *c, void *item);
+void *cext_list_get_item(Container *c, size_t index);
+int cext_list_get_item_index(Container *c, void *item);
+void *cext_list_get_next_item(Container *c, void *item);
+void *cext_list_get_prev_item(Container *c, void *item);
/* emallocz.c */
void *cext_emallocz(size_t size);
diff --git a/libcext/container.c b/libcext/container.c
@@ -111,7 +111,29 @@ void cext_iterate(Container *c, void *aux, void (*iter)(void *, void *aux))
}
}
-void cext_top_item(Container *c, void *item)
+size_t cext_sizeof(Container *c)
+{
+ size_t idx = 0;
+ CItem *i;
+
+ fprintf(stderr, "XX %s\n", "cext_sizeof()");
+ for (i = c->list; i; i = i->next)
+ idx++;
+
+ return idx;
+}
+
+void cext_swap_items(Container *c, void *item1, void *item2)
+{
+ CItem *i1 = cext_find_citem(c, item1, comp_ptr);
+ CItem *i2 = cext_find_citem(c, item2, comp_ptr);
+ fprintf(stderr, "XX %s\n", "cext_swap_items()");
+
+ i1->item = item2;
+ i2->item = item1;
+}
+
+void cext_stack_top_item(Container *c, void *item)
{
CItem *i = cext_find_citem(c, item, comp_ptr);
fprintf(stderr, "XX %s\n", "cext_top_item()");
@@ -121,13 +143,13 @@ void cext_top_item(Container *c, void *item)
attach_to_stack(c, i);
}
-void *cext_get_top_item(Container *c)
+void *cext_stack_get_top_item(Container *c)
{
fprintf(stderr, "XX %s\n", "cext_get_top_item()");
return c->stack ? c->stack->item : nil;
}
-void *cext_get_down_item(Container *c, void *item)
+void *cext_stack_get_down_item(Container *c, void *item)
{
CItem *i = cext_find_citem(c, item, comp_ptr);
fprintf(stderr, "XX %s\n", "cext_get_down_item()");
@@ -136,7 +158,7 @@ void *cext_get_down_item(Container *c, void *item)
return i->down ? i->down->item : c->stack->item;
}
-void *cext_get_up_item(Container *c, void *item)
+void *cext_stack_get_up_item(Container *c, void *item)
{
CItem *i = cext_find_citem(c, item, comp_ptr);
CItem *bottom;
@@ -147,7 +169,7 @@ void *cext_get_up_item(Container *c, void *item)
return i->up ? i->up->item : bottom->item;
}
-void *cext_get_item(Container *c, size_t index)
+void *cext_list_get_item(Container *c, size_t index)
{
size_t idx = 0;
CItem *i;
@@ -159,7 +181,7 @@ void *cext_get_item(Container *c, size_t index)
return i ? i->item : nil;
}
-int cext_get_item_index(Container *c, void *item)
+int cext_list_get_item_index(Container *c, void *item)
{
int idx = 0;
CItem *i;
@@ -171,110 +193,28 @@ int cext_get_item_index(Container *c, void *item)
return i ? idx : -1;
}
-size_t cext_sizeof(Container *c)
+void *cext_list_get_next_item(Container *c, void *item)
{
- size_t idx = 0;
- CItem *i;
-
- fprintf(stderr, "XX %s\n", "cext_sizeof()");
- for (i = c->list; i; i = i->next)
- idx++;
-
- return idx;
-}
-
-void cext_swap_items(Container *c, void *item1, void *item2)
-{
- CItem *i1 = cext_find_citem(c, item1, comp_ptr);
- CItem *i2 = cext_find_citem(c, item2, comp_ptr);
- fprintf(stderr, "XX %s\n", "cext_swap_items()");
-
- i1->item = item2;
- i2->item = item1;
-}
-
-
-/* old obsolete stuff follows */
-
-void **attach_item_begin(void **old, void *item, size_t size_item)
-{
- int i, size_old;
- void **result = 0;
- for (size_old = 0; old && old[size_old]; size_old++);
- result = cext_emallocz(size_item * (size_old + 2));
- result[0] = item;
- for (i = 0; old && old[i]; i++)
- result[i + 1] = old[i];
- result[i + 1] = 0;
- if (old)
- free(old);
- return result;
-}
-
-void **attach_item_end(void **old, void *item, size_t size_item)
-{
- int i, size_old;
- void **result = 0;
- for (size_old = 0; old && old[size_old]; size_old++);
- result = cext_emallocz(size_item * (size_old + 2));
- for (i = 0; old && old[i]; i++)
- result[i] = old[i];
- result[i++] = item;
- result[i] = 0;
- if (old)
- free(old);
- return result;
-}
-
-void **detach_item(void **old, void *item, size_t size_item)
-{
- int size_old, i, j = 0;
- void **result = 0;
- for (size_old = 0; old && old[size_old]; size_old++);
- if (size_old != 1) {
- result = cext_emallocz(size_item * size_old);
- for (i = 0; old[i]; i++)
- if (old[i] != item)
- result[j++] = old[i];
- result[j] = 0;
- }
- if (old)
- free(old);
- return result;
-}
-
-int index_item(void **items, void *item)
-{
- int i = 0;
- for (i = 0; items && items[i] && (items[i] != item); i++);
- return items[i] ? i : -1;
-}
-
-int count_items(void **items)
-{
- int i;
- for (i = 0; items && items[i]; i++);
- return i;
-}
-
-int index_next_item(void **items, void *item)
-{
- int idx = index_item(items, item);
+ size_t size = cext_sizeof(c);
+ int idx = cext_list_get_item_index(c, item);
if (idx == -1)
- return idx;
- if (idx == count_items(items) - 1)
- return 0;
+ return nil;
+
+ if (idx + 1 < size)
+ return cext_list_get_item(c, idx + 1);
else
- return idx + 1;
+ return cext_list_get_item(c, 0);
}
-int index_prev_item(void **items, void *item)
+void *cext_list_get_prev_item(Container *c, void *item)
{
- int idx = index_item(items, item);
+ size_t size = cext_sizeof(c);
+ int idx = cext_list_get_item_index(c, item);
if (idx == -1)
- return idx;
- if (idx == 0)
- return count_items(items) - 1;
+ return nil;
+
+ if (idx - 1 < 0)
+ return cext_list_get_item(c, size - 1);
else
- return idx - 1;
+ return cext_list_get_item(c, idx - 1);
}