wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

commit 272cba1404917e0b14e5495c1f0a8600756cbb6f
parent 65917ff9a8c9dda0eae63b468cbe9c0152a46958
Author: garbeam <garbeam@mmv.wmii.de>
Date:   Wed, 21 Dec 2005 17:18:11 +0200

indentation, changed libixp2 types


Diffstat:
cmd/wm/area.c | 173+++++++++++++++++++++++++++++++++++++++++++------------------------------------
cmd/wm/client.c | 598++++++++++++++++++++++++++++++++++++++++++-------------------------------------
cmd/wm/event.c | 444++++++++++++++++++++++++++++++++++++++++++-------------------------------------
cmd/wm/frame.c | 746++++++++++++++++++++++++++++++++++++++++++-------------------------------------
cmd/wm/layout.c | 13+++++++------
cmd/wm/layout_column.c | 999++++++++++++++++++++++++++++++++++++++++---------------------------------------
cmd/wm/layout_float.c | 374+++++++++++++++++++++++++++++++++++++++++--------------------------------------
cmd/wm/mouse.c | 1138++++++++++++++++++++++++++++++++++++++++---------------------------------------
cmd/wm/page.c | 260+++++++++++++++++++++++++++++++++++++++++--------------------------------------
cmd/wm/wm.c | 1598++++++++++++++++++++++++++++++++++++++++++-------------------------------------
cmd/wm/wm.h | 262++++++++++++++++++++++++++++++++++++++++----------------------------------------
cmd/wmiibar.c | 854+++++++++++++++++++++++++++++++++++++++++--------------------------------------
cmd/wmiibar2.c | 682+++++++++++++++++++++++++++++++++++++++++--------------------------------------
cmd/wmiifs.c | 625+++++++++++++++++++++++++++++++++++++++++--------------------------------------
cmd/wmiikeys.c | 740+++++++++++++++++++++++++++++++++++++++++--------------------------------------
cmd/wmiimenu.c | 1150++++++++++++++++++++++++++++++++++++++++++-------------------------------------
cmd/wmiiplumb.c | 135++++++++++++++++++++++++++++++++++++++++---------------------------------------
cmd/wmiir.c | 396++++++++++++++++++++++++++++++++++++++++---------------------------------------
cmd/wmiir2.c | 361++++++++++++++++++++++++++++++++++++++++---------------------------------------
cmd/wmiiwarp.c | 69++++++++++++++++++++++++++++++++++++---------------------------------
libcext/cext.h | 3++-
libcext/emallocz.c | 17+++++++++--------
libcext/estrdup.c | 11++++++-----
libcext/strlcat.c | 43++++++++++++++++++++++---------------------
libcext/strlcpy.c | 37+++++++++++++++++++------------------
libcext/strtonum.c | 66++++++++++++++++++++++++++++++++++--------------------------------
libcext/tokenize.c | 43++++++++++++++++++++++---------------------
libixp2/client.c | 279++++++++++++++++++++++++++++++++++++++++++-------------------------------------
libixp2/convert.c | 238++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
libixp2/ixp.h | 307+++++++++++++++++++++++++++++++++++++++++--------------------------------------
libixp2/message.c | 543++++++++++++++++++++++++++++++++++++++++---------------------------------------
libixp2/server.c | 275+++++++++++++++++++++++++++++++++++++++++--------------------------------------
libixp2/socket.c | 105+++++++++++++++++++++++++++++++++++++++++--------------------------------------
libixp2/transport.c | 96++++++++++++++++++++++++++++++++++++++++++-------------------------------------
libwmii/ixputil.c | 119+++++++++++++++++++++++++++++++++++++++++--------------------------------------
libwmii/spawn.c | 27++++++++++++++-------------
libwmii/wm.c | 95++++++++++++++++++++++++++++++++++++++++++-------------------------------------
libwmii/wmii.h | 12+++++++-----
38 files changed, 7264 insertions(+), 6669 deletions(-)

diff --git a/cmd/wm/area.c b/cmd/wm/area.c @@ -9,108 +9,123 @@ #include "wm.h" -static void handle_after_write_area(IXPServer *s, File *file); +static void handle_after_write_area(IXPServer * s, File * file); -Area *alloc_area(Page *p, char *layout) +Area * +alloc_area(Page * p, char *layout) { - char buf[MAX_BUF], *name; - Area *a = (Area *) cext_emallocz(sizeof(Area)); + char buf[MAX_BUF], *name; + Area *a = (Area *) cext_emallocz(sizeof(Area)); - a->page = p; - if (strncmp(layout, "float", 6)) - name = "managed"; - else - name = "float"; - snprintf(buf, MAX_BUF, "/%s/layout/%s", p->file[P_PREFIX]->name, name); - a->file[A_PREFIX] = ixp_create(ixps, buf); - snprintf(buf, MAX_BUF, "/%s/layout/%s/frame", p->file[P_PREFIX]->name, name); - a->file[A_FRAME_PREFIX] = ixp_create(ixps, buf); - snprintf(buf, MAX_BUF, "/%s/layout/%s/frame/sel", p->file[P_PREFIX]->name, name); - a->file[A_SEL_FRAME] = ixp_create(ixps, buf); - a->file[A_SEL_FRAME]->bind = 1; - snprintf(buf, MAX_BUF, "/%s/layout/%s/ctl", p->file[P_PREFIX]->name, name); - a->file[A_CTL] = ixp_create(ixps, buf); - a->file[A_CTL]->after_write = handle_after_write_area; - snprintf(buf, MAX_BUF, "/%s/layout/%s/name", p->file[P_PREFIX]->name, name); - a->file[A_LAYOUT] = wmii_create_ixpfile(ixps, buf, layout); - a->file[A_LAYOUT]->after_write = handle_after_write_area; - a->layout = match_layout(layout); - fprintf(stderr, "matched '%s' layout\n", a->layout->name); - a->layout->init(a, nil); - p->file[P_SEL_AREA]->content = a->file[A_PREFIX]->content; - return a; + a->page = p; + if(strncmp(layout, "float", 6)) + name = "managed"; + else + name = "float"; + snprintf(buf, MAX_BUF, "/%s/layout/%s", p->file[P_PREFIX]->name, name); + a->file[A_PREFIX] = ixp_create(ixps, buf); + snprintf(buf, MAX_BUF, "/%s/layout/%s/frame", p->file[P_PREFIX]->name, + name); + a->file[A_FRAME_PREFIX] = ixp_create(ixps, buf); + snprintf(buf, MAX_BUF, "/%s/layout/%s/frame/sel", + p->file[P_PREFIX]->name, name); + a->file[A_SEL_FRAME] = ixp_create(ixps, buf); + a->file[A_SEL_FRAME]->bind = 1; + snprintf(buf, MAX_BUF, "/%s/layout/%s/ctl", p->file[P_PREFIX]->name, + name); + a->file[A_CTL] = ixp_create(ixps, buf); + a->file[A_CTL]->after_write = handle_after_write_area; + snprintf(buf, MAX_BUF, "/%s/layout/%s/name", p->file[P_PREFIX]->name, + name); + a->file[A_LAYOUT] = wmii_create_ixpfile(ixps, buf, layout); + a->file[A_LAYOUT]->after_write = handle_after_write_area; + a->layout = match_layout(layout); + fprintf(stderr, "matched '%s' layout\n", a->layout->name); + a->layout->init(a, nil); + p->file[P_SEL_AREA]->content = a->file[A_PREFIX]->content; + return a; } -void destroy_area(Area *a) +void +destroy_area(Area * a) { - a->layout->deinit(a); - ixp_remove_file(ixps, a->file[A_PREFIX]); - free(a); + a->layout->deinit(a); + ixp_remove_file(ixps, a->file[A_PREFIX]); + free(a); } -void focus_area(Area *a) +void +focus_area(Area * a) { - Page *p = a->page; - Frame *f; - p->sel = a; - p->file[P_SEL_AREA]->content = a->file[A_PREFIX]->content; - if ((f = a->layout->sel(a))) - a->layout->focus(f, False); + Page *p = a->page; + Frame *f; + p->sel = a; + p->file[P_SEL_AREA]->content = a->file[A_PREFIX]->content; + if((f = a->layout->sel(a))) + a->layout->focus(f, False); } -void hide_area(Area * a) +void +hide_area(Area * a) { - Frame *f; - for (f = a->layout->frames(a); f; f = f->next) - XMoveWindow(dpy, f->win, 2 * rect.width, 2 * rect.height); + Frame *f; + for(f = a->layout->frames(a); f; f = f->next) + XMoveWindow(dpy, f->win, 2 * rect.width, 2 * rect.height); } -void show_area(Area *a, Bool raise) +void +show_area(Area * a, Bool raise) { - Frame *f; - for (f = a->layout->frames(a); f; f = f->next) { - XMoveWindow(dpy, f->win, f->rect.x, f->rect.y); - if (raise) - XRaiseWindow(dpy, f->win); - } + Frame *f; + for(f = a->layout->frames(a); f; f = f->next) { + XMoveWindow(dpy, f->win, f->rect.x, f->rect.y); + if(raise) + XRaiseWindow(dpy, f->win); + } } -Area *sel_area() +Area * +sel_area() { - return selpage ? selpage->sel : nil; + return selpage ? selpage->sel : nil; } -void attach_frame_to_area(Area *a, Frame *f) +void +attach_frame_to_area(Area * a, Frame * f) { - wmii_move_ixpfile(f->file[F_PREFIX], a->file[A_FRAME_PREFIX]); - a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content; - f->area = a; + wmii_move_ixpfile(f->file[F_PREFIX], a->file[A_FRAME_PREFIX]); + a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content; + f->area = a; } -void detach_frame_from_area(Frame *f) { - f->area->file[A_SEL_FRAME]->content = 0; - f->area = 0; +void +detach_frame_from_area(Frame * f) +{ + f->area->file[A_SEL_FRAME]->content = 0; + f->area = 0; } -static void handle_after_write_area(IXPServer *s, File *file) { - Page *p; - for (p = pages; p; p = p->next) { - if (file == p->managed->file[A_CTL]) { - run_action(file, p->managed, p->managed->layout->actions(p->managed)); - return; - } - else if (file == p->floating->file[A_CTL]) { - run_action(file, p->floating, p->floating->layout->actions(p->floating)); - return; - } - else if (file == p->managed->file[A_LAYOUT]) { - Layout *l = match_layout(file->content); - if (l) { - Client *clients = p->managed->layout->deinit(p->managed); - p->managed->layout = l; - p->managed->layout->init(p->managed, clients); - invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); - } - } - } +static void +handle_after_write_area(IXPServer * s, File * file) +{ + Page *p; + for(p = pages; p; p = p->next) { + if(file == p->managed->file[A_CTL]) { + run_action(file, p->managed, + p->managed->layout->actions(p->managed)); + return; + } else if(file == p->floating->file[A_CTL]) { + run_action(file, p->floating, + p->floating->layout->actions(p->floating)); + return; + } else if(file == p->managed->file[A_LAYOUT]) { + Layout *l = match_layout(file->content); + if(l) { + Client *clients = p->managed->layout->deinit(p->managed); + p->managed->layout = l; + p->managed->layout->init(p->managed, clients); + invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); + } + } + } } diff --git a/cmd/wm/client.c b/cmd/wm/client.c @@ -10,349 +10,387 @@ #include "wm.h" -Client *alloc_client(Window w) +Client * +alloc_client(Window w) { - static int id = 0; - char buf[MAX_BUF]; - XTextProperty name; - Client *c = (Client *) cext_emallocz(sizeof(Client)); - - c->win = w; - snprintf(buf, MAX_BUF, "/detached/client/%d", id); - c->file[C_PREFIX] = ixp_create(ixps, buf); - XGetWMName(dpy, c->win, &name); - snprintf(buf, MAX_BUF, "/detached/client/%d/name", id); - c->file[C_NAME] = wmii_create_ixpfile(ixps, buf, (char *)name.value); - free(name.value); - id++; - return c; + static int id = 0; + char buf[MAX_BUF]; + XTextProperty name; + Client *c = (Client *) cext_emallocz(sizeof(Client)); + + c->win = w; + snprintf(buf, MAX_BUF, "/detached/client/%d", id); + c->file[C_PREFIX] = ixp_create(ixps, buf); + XGetWMName(dpy, c->win, &name); + snprintf(buf, MAX_BUF, "/detached/client/%d/name", id); + c->file[C_NAME] = wmii_create_ixpfile(ixps, buf, (char *) name.value); + free(name.value); + id++; + return c; } -void focus_client(Client * c) +void +focus_client(Client * c) { - Frame *f = 0; - /* sel client */ - f = c->frame; - f->sel = c; - f->file[F_SEL_CLIENT]->content = c->file[C_PREFIX]->content; - XRaiseWindow(dpy, c->win); - XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); - invoke_wm_event(def[WM_EVENT_CLIENT_UPDATE]); + Frame *f = 0; + /* sel client */ + f = c->frame; + f->sel = c; + f->file[F_SEL_CLIENT]->content = c->file[C_PREFIX]->content; + XRaiseWindow(dpy, c->win); + XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); + invoke_wm_event(def[WM_EVENT_CLIENT_UPDATE]); } -void set_client_state(Client * c, int state) +void +set_client_state(Client * c, int state) { - long data[2]; + long data[2]; - data[0] = (long) state; - data[1] = (long) None; - XChangeProperty(dpy, c->win, wm_state, wm_state, 32, PropModeReplace, (unsigned char *) data, 2); + data[0] = (long) state; + data[1] = (long) None; + XChangeProperty(dpy, c->win, wm_state, wm_state, 32, PropModeReplace, + (unsigned char *) data, 2); } -void show_client(Client * c) +void +show_client(Client * c) { - XMapWindow(dpy, c->win); - set_client_state(c, NormalState); - grab_client(c, Mod1Mask, Button1); - grab_client(c, Mod1Mask, Button3); + XMapWindow(dpy, c->win); + set_client_state(c, NormalState); + grab_client(c, Mod1Mask, Button1); + grab_client(c, Mod1Mask, Button3); } -void hide_client(Client * c) +void +hide_client(Client * c) { - ungrab_client(c, AnyModifier, AnyButton); - XUnmapWindow(dpy, c->win); - set_client_state(c, WithdrawnState); + ungrab_client(c, AnyModifier, AnyButton); + XUnmapWindow(dpy, c->win); + set_client_state(c, WithdrawnState); } -void reparent_client(Client * c, Window w, int x, int y) +void +reparent_client(Client * c, Window w, int x, int y) { - XReparentWindow(dpy, c->win, w, x, y); - c->ignore_unmap++; + XReparentWindow(dpy, c->win, w, x, y); + c->ignore_unmap++; } -void grab_client(Client * c, unsigned long mod, unsigned int button) +void +grab_client(Client * c, unsigned long mod, unsigned int button) { - XGrabButton(dpy, button, mod, c->win, False, - ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - if ((mod != AnyModifier) && num_lock_mask) { - XGrabButton(dpy, button, mod | num_lock_mask, c->win, False, ButtonPressMask, - GrabModeAsync, GrabModeAsync, None, None); - XGrabButton(dpy, button, mod | num_lock_mask | LockMask, c->win, False, ButtonPressMask, - GrabModeAsync, GrabModeAsync, None, None); - } + XGrabButton(dpy, button, mod, c->win, False, + ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); + if((mod != AnyModifier) && num_lock_mask) { + XGrabButton(dpy, button, mod | num_lock_mask, c->win, False, + ButtonPressMask, GrabModeAsync, GrabModeAsync, None, + None); + XGrabButton(dpy, button, mod | num_lock_mask | LockMask, c->win, + False, ButtonPressMask, GrabModeAsync, GrabModeAsync, + None, None); + } } -void ungrab_client(Client * c, unsigned long mod, unsigned int button) +void +ungrab_client(Client * c, unsigned long mod, unsigned int button) { - XUngrabButton(dpy, button, mod, c->win); - if (mod != AnyModifier && num_lock_mask) { - XUngrabButton(dpy, button, mod | num_lock_mask, c->win); - XUngrabButton(dpy, button, mod | num_lock_mask | LockMask, c->win); - } + XUngrabButton(dpy, button, mod, c->win); + if(mod != AnyModifier && num_lock_mask) { + XUngrabButton(dpy, button, mod | num_lock_mask, c->win); + XUngrabButton(dpy, button, mod | num_lock_mask | LockMask, c->win); + } } -void configure_client(Client * c) +void +configure_client(Client * c) { - XConfigureEvent e; - e.type = ConfigureNotify; - e.event = c->win; - e.window = c->win; - e.x = c->rect.x; - e.y = c->rect.y; - if (c->frame) { - e.x += c->frame->rect.x; - e.y += c->frame->rect.y; - } - e.width = c->rect.width; - e.height = c->rect.height; - e.border_width = c->border; - e.above = None; - e.override_redirect = False; - - XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask); - XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *) & e); - XSelectInput(dpy, c->win, CLIENT_MASK); - XSync(dpy, False); + XConfigureEvent e; + e.type = ConfigureNotify; + e.event = c->win; + e.window = c->win; + e.x = c->rect.x; + e.y = c->rect.y; + if(c->frame) { + e.x += c->frame->rect.x; + e.y += c->frame->rect.y; + } + e.width = c->rect.width; + e.height = c->rect.height; + e.border_width = c->border; + e.above = None; + e.override_redirect = False; + + XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask); + XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *) & e); + XSelectInput(dpy, c->win, CLIENT_MASK); + XSync(dpy, False); } -void close_client(Client * c) +void +close_client(Client * c) { - if (c->proto & PROTO_DEL) - wmii_send_message(dpy, c->win, wm_protocols, wm_delete); - else - XKillClient(dpy, c->win); + if(c->proto & PROTO_DEL) + wmii_send_message(dpy, c->win, wm_protocols, wm_delete); + else + XKillClient(dpy, c->win); } -void init_client(Client * c, XWindowAttributes * wa) +void +init_client(Client * c, XWindowAttributes * wa) { - long msize; - c->rect.x = wa->x; - c->rect.y = wa->y; - c->border = wa->border_width; - c->rect.width = wa->width + 2 * c->border; - c->rect.height = wa->height + 2 * c->border; - XSetWindowBorderWidth(dpy, c->win, 0); - c->proto = win_proto(c->win); - XGetTransientForHint(dpy, c->win, &c->trans); - if (!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags) - c->size.flags = PSize; - XAddToSaveSet(dpy, c->win); + long msize; + c->rect.x = wa->x; + c->rect.y = wa->y; + c->border = wa->border_width; + c->rect.width = wa->width + 2 * c->border; + c->rect.height = wa->height + 2 * c->border; + XSetWindowBorderWidth(dpy, c->win, 0); + c->proto = win_proto(c->win); + XGetTransientForHint(dpy, c->win, &c->trans); + if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags) + c->size.flags = PSize; + XAddToSaveSet(dpy, c->win); } -void handle_client_property(Client *c, XPropertyEvent *e) +void +handle_client_property(Client * c, XPropertyEvent * e) { - XTextProperty name; - long msize; - - if (e->atom == wm_protocols) { - /* update */ - c->proto = win_proto(c->win); - return; - } - switch (e->atom) { - case XA_WM_NAME: - XGetWMName(dpy, c->win, &name); - if (strlen((char *)name.value)) { - if (c->file[C_NAME]->content) - free(c->file[C_NAME]->content); - c->file[C_NAME]->content = cext_estrdup((char *)name.value); - c->file[C_NAME]->size = strlen((char *)name.value); - } - free(name.value); - if (c->frame) - draw_client(c); - invoke_wm_event(def[WM_EVENT_CLIENT_UPDATE]); - break; - case XA_WM_TRANSIENT_FOR: - XGetTransientForHint(dpy, c->win, &c->trans); - break; - case XA_WM_NORMAL_HINTS: - if (!XGetWMNormalHints(dpy, c->win, &c->size, &msize) - || !c->size.flags) { - c->size.flags = PSize; - } - break; - } + XTextProperty name; + long msize; + + if(e->atom == wm_protocols) { + /* update */ + c->proto = win_proto(c->win); + return; + } + switch (e->atom) { + case XA_WM_NAME: + XGetWMName(dpy, c->win, &name); + if(strlen((char *) name.value)) { + if(c->file[C_NAME]->content) + free(c->file[C_NAME]->content); + c->file[C_NAME]->content = cext_estrdup((char *) name.value); + c->file[C_NAME]->size = strlen((char *) name.value); + } + free(name.value); + if(c->frame) + draw_client(c); + invoke_wm_event(def[WM_EVENT_CLIENT_UPDATE]); + break; + case XA_WM_TRANSIENT_FOR: + XGetTransientForHint(dpy, c->win, &c->trans); + break; + case XA_WM_NORMAL_HINTS: + if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) + || !c->size.flags) { + c->size.flags = PSize; + } + break; + } } -void destroy_client(Client * c) +void +destroy_client(Client * c) { - detach_detached(c); - ixp_remove_file(ixps, c->file[C_PREFIX]); - free(c); + detach_detached(c); + ixp_remove_file(ixps, c->file[C_PREFIX]); + free(c); } /* speed reasoned function for client property change */ -void draw_client(Client *client) +void +draw_client(Client * client) { - Frame *f = client->frame; - unsigned int i = 0, tw, tabh = tab_height(f); - Draw d = { 0 }; - Client *c; - - if (!tabh) - return; - - tw = f->rect.width / f->nclients; - for (c = f->clients; c && c != client; c = c->next) - i++; - - d.drawable = f->win; - d.gc = f->gc; - d.rect.x = i * tw; - d.rect.y = 0; - d.rect.width = tw; - if (i && (i == f->nclients - 1)) - d.rect.width = f->rect.width - d.rect.x; - d.rect.height = tabh; - d.data = c->file[C_NAME]->content; - d.font = font; - - if ((f == sel_frame()) && (c == f->sel)) { - d.bg = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_BG_COLOR]->content); - d.fg = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_FG_COLOR]->content); - d.border = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_BORDER_COLOR]->content); - } else { - d.bg = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_BG_COLOR]->content); - d.fg = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_FG_COLOR]->content); - d.border = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_BORDER_COLOR]->content); - } - blitz_drawlabel(dpy, &d); - XSync(dpy, False); + Frame *f = client->frame; + unsigned int i = 0, tw, tabh = tab_height(f); + Draw d = { 0 }; + Client *c; + + if(!tabh) + return; + + tw = f->rect.width / f->nclients; + for(c = f->clients; c && c != client; c = c->next) + i++; + + d.drawable = f->win; + d.gc = f->gc; + d.rect.x = i * tw; + d.rect.y = 0; + d.rect.width = tw; + if(i && (i == f->nclients - 1)) + d.rect.width = f->rect.width - d.rect.x; + d.rect.height = tabh; + d.data = c->file[C_NAME]->content; + d.font = font; + + if((f == sel_frame()) && (c == f->sel)) { + d.bg = + blitz_loadcolor(dpy, screen_num, + f->file[F_SEL_BG_COLOR]->content); + d.fg = + blitz_loadcolor(dpy, screen_num, + f->file[F_SEL_FG_COLOR]->content); + d.border = + blitz_loadcolor(dpy, screen_num, + f->file[F_SEL_BORDER_COLOR]->content); + } else { + d.bg = + blitz_loadcolor(dpy, screen_num, + f->file[F_NORM_BG_COLOR]->content); + d.fg = + blitz_loadcolor(dpy, screen_num, + f->file[F_NORM_FG_COLOR]->content); + d.border = + blitz_loadcolor(dpy, screen_num, + f->file[F_NORM_BORDER_COLOR]->content); + } + blitz_drawlabel(dpy, &d); + XSync(dpy, False); } -void draw_clients(Frame * f) +void +draw_clients(Frame * f) { - Client *c; - for (c = f->clients; c; c = c->next) - draw_client(c); + Client *c; + for(c = f->clients; c; c = c->next) + draw_client(c); } -void gravitate(Client * c, unsigned int tabh, unsigned int bw, int invert) +void +gravitate(Client * c, unsigned int tabh, unsigned int bw, int invert) { - int dx = 0, dy = 0; - int gravity = NorthWestGravity; - - if (c->size.flags & PWinGravity) { - gravity = c->size.win_gravity; - } - /* y */ - switch (gravity) { - case StaticGravity: - case NorthWestGravity: - case NorthGravity: - case NorthEastGravity: - dy = tabh; - break; - case EastGravity: - case CenterGravity: - case WestGravity: - dy = -(c->rect.height / 2) + tabh; - break; - case SouthEastGravity: - case SouthGravity: - case SouthWestGravity: - dy = -c->rect.height; - break; - default: /* don't care */ - break; - } - - /* x */ - switch (gravity) { - case StaticGravity: - case NorthWestGravity: - case WestGravity: - case SouthWestGravity: - dx = bw; - break; - case NorthGravity: - case CenterGravity: - case SouthGravity: - dx = -(c->rect.width / 2) + bw; - break; - case NorthEastGravity: - case EastGravity: - case SouthEastGravity: - dx = -(c->rect.width + bw); - break; - default: /* don't care */ - break; - } - - if (invert) { - dx = -dx; - dy = -dy; - } - c->rect.x += dx; - c->rect.y += dy; + int dx = 0, dy = 0; + int gravity = NorthWestGravity; + + if(c->size.flags & PWinGravity) { + gravity = c->size.win_gravity; + } + /* y */ + switch (gravity) { + case StaticGravity: + case NorthWestGravity: + case NorthGravity: + case NorthEastGravity: + dy = tabh; + break; + case EastGravity: + case CenterGravity: + case WestGravity: + dy = -(c->rect.height / 2) + tabh; + break; + case SouthEastGravity: + case SouthGravity: + case SouthWestGravity: + dy = -c->rect.height; + break; + default: /* don't care */ + break; + } + + /* x */ + switch (gravity) { + case StaticGravity: + case NorthWestGravity: + case WestGravity: + case SouthWestGravity: + dx = bw; + break; + case NorthGravity: + case CenterGravity: + case SouthGravity: + dx = -(c->rect.width / 2) + bw; + break; + case NorthEastGravity: + case EastGravity: + case SouthEastGravity: + dx = -(c->rect.width + bw); + break; + default: /* don't care */ + break; + } + + if(invert) { + dx = -dx; + dy = -dy; + } + c->rect.x += dx; + c->rect.y += dy; } -void attach_client(Client * c) +void +attach_client(Client * c) { - Area *a = 0; - Page *p = selpage; - - if (!p) - p = alloc_page(); - /* transient stuff */ - a = p->sel; - if (c && c->trans) { - Client *t = win_to_client(c->trans); - if (t && t->frame) - a = p->floating; - } - a->layout->attach(a, c); - invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); + Area *a = 0; + Page *p = selpage; + + if(!p) + p = alloc_page(); + /* transient stuff */ + a = p->sel; + if(c && c->trans) { + Client *t = win_to_client(c->trans); + if(t && t->frame) + a = p->floating; + } + a->layout->attach(a, c); + invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); } -void detach_client(Client *c, Bool unmap) { - Frame *f = c->frame; - Area *a = f ? f->area : nil; - if (a) { - a->layout->detach(a, c, unmap); - } - if (c->destroyed) - destroy_client(c); - if (selpage) - focus_page(selpage); +void +detach_client(Client * c, Bool unmap) +{ + Frame *f = c->frame; + Area *a = f ? f->area : nil; + if(a) { + a->layout->detach(a, c, unmap); + } + if(c->destroyed) + destroy_client(c); + if(selpage) + focus_page(selpage); } -Client *sel_client() +Client * +sel_client() { - Frame *f = sel_frame(); - return f ? f->sel : nil; + Frame *f = sel_frame(); + return f ? f->sel : nil; } -Client *clientat(Client *clients, size_t idx) +Client * +clientat(Client * clients, size_t idx) { - size_t i = 0; - Client *c = clients; - for (; c && i != idx ; c = c->next) i++; - return c; + size_t i = 0; + Client *c = clients; + for(; c && i != idx; c = c->next) + i++; + return c; } -void detach_detached(Client *c) +void +detach_detached(Client * c) { - if (detached == c) { - if (c->next) - c->next->prev = nil; - detached = c->next; - } - else { - if (c->next) - c->next->prev = c->prev; - if (c->prev) - c->prev->next = c->next; - } - ndetached--; + if(detached == c) { + if(c->next) + c->next->prev = nil; + detached = c->next; + } else { + if(c->next) + c->next->prev = c->prev; + if(c->prev) + c->prev->next = c->next; + } + ndetached--; } -void attach_detached(Client *c) +void +attach_detached(Client * c) { - c->prev = nil; - c->next = detached; - if (detached) - detached->prev = c; - detached = c; + c->prev = nil; + c->next = detached; + if(detached) + detached->prev = c; + detached = c; } diff --git a/cmd/wm/event.c b/cmd/wm/event.c @@ -20,256 +20,280 @@ static void handle_motionnotify(XEvent * e); static void handle_propertynotify(XEvent * e); static void handle_unmapnotify(XEvent * e); static void handle_enternotify(XEvent * e); -static void handle_ignore_enternotify_crap(XEvent *e); +static void handle_ignore_enternotify_crap(XEvent * e); static unsigned int ignore_enternotify_crap = 0; void (*handler[LASTEvent]) (XEvent *); -void init_event_hander() +void +init_event_hander() { - int i; - /* init event handler */ - for (i = 0; i < LASTEvent; i++) { - handler[i] = 0; - } - handler[ButtonPress] = handle_buttonpress; - handler[ConfigureRequest] = handle_configurerequest; - handler[ConfigureNotify] = handle_ignore_enternotify_crap; - handler[CirculateNotify] = handle_ignore_enternotify_crap; - handler[DestroyNotify] = handle_destroynotify; - handler[EnterNotify] = handle_enternotify; - handler[Expose] = handle_expose; - handler[GravityNotify] = handle_ignore_enternotify_crap; - handler[MapRequest] = handle_maprequest; - handler[MotionNotify] = handle_motionnotify; - handler[PropertyNotify] = handle_propertynotify; - handler[MapNotify] = handle_ignore_enternotify_crap; - handler[UnmapNotify] = handle_unmapnotify; + int i; + /* init event handler */ + for(i = 0; i < LASTEvent; i++) { + handler[i] = 0; + } + handler[ButtonPress] = handle_buttonpress; + handler[ConfigureRequest] = handle_configurerequest; + handler[ConfigureNotify] = handle_ignore_enternotify_crap; + handler[CirculateNotify] = handle_ignore_enternotify_crap; + handler[DestroyNotify] = handle_destroynotify; + handler[EnterNotify] = handle_enternotify; + handler[Expose] = handle_expose; + handler[GravityNotify] = handle_ignore_enternotify_crap; + handler[MapRequest] = handle_maprequest; + handler[MotionNotify] = handle_motionnotify; + handler[PropertyNotify] = handle_propertynotify; + handler[MapNotify] = handle_ignore_enternotify_crap; + handler[UnmapNotify] = handle_unmapnotify; } -void check_event(Connection * c) +void +check_event(Connection * c) { - XEvent ev; - while (XPending(dpy)) { /* main evet loop */ - XNextEvent(dpy, &ev); - if (handler[ev.type]) - (handler[ev.type]) (&ev); /* call handler */ - } + XEvent ev; + while(XPending(dpy)) { /* main evet loop */ + XNextEvent(dpy, &ev); + if(handler[ev.type]) + (handler[ev.type]) (&ev); /* call handler */ + } } -static void handle_buttonpress(XEvent * e) +static void +handle_buttonpress(XEvent * e) { - Client *c; - XButtonPressedEvent *ev = &e->xbutton; - Frame *f = win_to_frame(ev->window); - - if (f) { - handle_frame_buttonpress(ev, f); - return; - } - if (ev->window == root) { - XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); - XSync(dpy, False); - return; - } - if ((c = win_to_client(ev->window))) { - if (c->frame) { /* client is attached */ - ev->state &= valid_mask; - if (ev->state & Mod1Mask) { - Align align; - XRaiseWindow(dpy, c->frame->win); - switch (ev->button) { - case Button1: - mouse_move(c->frame); - break; - case Button3: - align = xy_to_align(&c->rect, ev->x, ev->y); - if (align == CENTER) mouse_move(c->frame); - else mouse_resize(c->frame, align); - break; - default: - break; - } - } - } - } + Client *c; + XButtonPressedEvent *ev = &e->xbutton; + Frame *f = win_to_frame(ev->window); + + if(f) { + handle_frame_buttonpress(ev, f); + return; + } + if(ev->window == root) { + XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); + XSync(dpy, False); + return; + } + if((c = win_to_client(ev->window))) { + if(c->frame) { /* client is attached */ + ev->state &= valid_mask; + if(ev->state & Mod1Mask) { + Align align; + XRaiseWindow(dpy, c->frame->win); + switch (ev->button) { + case Button1: + mouse_move(c->frame); + break; + case Button3: + align = xy_to_align(&c->rect, ev->x, ev->y); + if(align == CENTER) + mouse_move(c->frame); + else + mouse_resize(c->frame, align); + break; + default: + break; + } + } + } + } } -static void handle_configurerequest(XEvent * e) +static void +handle_configurerequest(XEvent * e) { - XConfigureRequestEvent *ev = &e->xconfigurerequest; - XWindowChanges wc; - Client *c; - unsigned int bw = 0, tabh = 0; - Frame *f = 0; - - - c = win_to_client(ev->window); - ev->value_mask &= ~CWSibling; - - if (c) { - f = c->frame; - - if (f) { - Page *p = f->area->page; - if (f->area == p->managed) { - f->area->layout->detach(f->area, c, False); - p->floating->layout->attach(p->floating, c); - f = c->frame; - } - bw = border_width(f); - tabh = tab_height(f); - } - if (ev->value_mask & CWStackMode) { - if (wc.stack_mode == Above) XRaiseWindow(dpy, c->win); - else ev->value_mask &= ~CWStackMode; - } - gravitate(c, tabh ? tabh : bw, bw, 1); - - if (ev->value_mask & CWX) c->rect.x = ev->x; - if (ev->value_mask & CWY) c->rect.y = ev->y; - if (ev->value_mask & CWWidth) c->rect.width = ev->width; - if (ev->value_mask & CWHeight) c->rect.height = ev->height; - if (ev->value_mask & CWBorderWidth) c->border = ev->border_width; - - gravitate(c, tabh ? tabh : bw, bw, 0); - - if (f) { - f->rect.x = wc.x = c->rect.x - bw; - f->rect.y = wc.y = c->rect.y - (tabh ? tabh : bw); - f->rect.width = wc.width = c->rect.width + 2 * bw; - f->rect.height = wc.height = c->rect.height + bw + (tabh ? tabh : bw); - wc.border_width = 1; - wc.sibling = None; - wc.stack_mode = ev->detail; - XConfigureWindow(dpy, f->win, ev->value_mask, &wc); - configure_client(c); - } - } - wc.x = ev->x; - wc.y = ev->y; - - if (f) { - /* if so, then bw and tabh are already initialized */ - wc.x = bw; - wc.y = tabh ? tabh : bw; - } - wc.width = ev->width; - if (!wc.width) - wc.width = 1; /* borken app fix */ - wc.height = ev->height; - if (!wc.height) - wc.height = 1; /* borken app fix */ - wc.border_width = 0; - wc.sibling = None; - wc.stack_mode = Above; - ev->value_mask &= ~CWStackMode; - ev->value_mask |= CWBorderWidth; - XConfigureWindow(dpy, e->xconfigurerequest.window, ev->value_mask, &wc); - XSync(dpy, False); + XConfigureRequestEvent *ev = &e->xconfigurerequest; + XWindowChanges wc; + Client *c; + unsigned int bw = 0, tabh = 0; + Frame *f = 0; + + + c = win_to_client(ev->window); + ev->value_mask &= ~CWSibling; + + if(c) { + f = c->frame; + + if(f) { + Page *p = f->area->page; + if(f->area == p->managed) { + f->area->layout->detach(f->area, c, False); + p->floating->layout->attach(p->floating, c); + f = c->frame; + } + bw = border_width(f); + tabh = tab_height(f); + } + if(ev->value_mask & CWStackMode) { + if(wc.stack_mode == Above) + XRaiseWindow(dpy, c->win); + else + ev->value_mask &= ~CWStackMode; + } + gravitate(c, tabh ? tabh : bw, bw, 1); + + if(ev->value_mask & CWX) + c->rect.x = ev->x; + if(ev->value_mask & CWY) + c->rect.y = ev->y; + if(ev->value_mask & CWWidth) + c->rect.width = ev->width; + if(ev->value_mask & CWHeight) + c->rect.height = ev->height; + if(ev->value_mask & CWBorderWidth) + c->border = ev->border_width; + + gravitate(c, tabh ? tabh : bw, bw, 0); + + if(f) { + f->rect.x = wc.x = c->rect.x - bw; + f->rect.y = wc.y = c->rect.y - (tabh ? tabh : bw); + f->rect.width = wc.width = c->rect.width + 2 * bw; + f->rect.height = wc.height = + c->rect.height + bw + (tabh ? tabh : bw); + wc.border_width = 1; + wc.sibling = None; + wc.stack_mode = ev->detail; + XConfigureWindow(dpy, f->win, ev->value_mask, &wc); + configure_client(c); + } + } + wc.x = ev->x; + wc.y = ev->y; + + if(f) { + /* if so, then bw and tabh are already initialized */ + wc.x = bw; + wc.y = tabh ? tabh : bw; + } + wc.width = ev->width; + if(!wc.width) + wc.width = 1; /* borken app fix */ + wc.height = ev->height; + if(!wc.height) + wc.height = 1; /* borken app fix */ + wc.border_width = 0; + wc.sibling = None; + wc.stack_mode = Above; + ev->value_mask &= ~CWStackMode; + ev->value_mask |= CWBorderWidth; + XConfigureWindow(dpy, e->xconfigurerequest.window, ev->value_mask, + &wc); + XSync(dpy, False); } -static void handle_destroynotify(XEvent * e) +static void +handle_destroynotify(XEvent * e) { - XDestroyWindowEvent *ev = &e->xdestroywindow; - Client *c = win_to_client(ev->window); - if (c) { - c->destroyed = True; - detach_client(c, False); - } + XDestroyWindowEvent *ev = &e->xdestroywindow; + Client *c = win_to_client(ev->window); + if(c) { + c->destroyed = True; + detach_client(c, False); + } } -static void handle_expose(XEvent * e) +static void +handle_expose(XEvent * e) { - static Frame *f; - if (e->xexpose.count == 0) { - f = win_to_frame(e->xbutton.window); - if (f) - draw_frame(f); - } + static Frame *f; + if(e->xexpose.count == 0) { + f = win_to_frame(e->xbutton.window); + if(f) + draw_frame(f); + } } -static void handle_maprequest(XEvent * e) +static void +handle_maprequest(XEvent * e) { - XMapRequestEvent *ev = &e->xmaprequest; - static XWindowAttributes wa; - static Client *c; - - if (!XGetWindowAttributes(dpy, ev->window, &wa)) - return; - if (wa.override_redirect) { - XSelectInput(dpy, ev->window, (StructureNotifyMask | PropertyChangeMask)); - return; - } - - /* there're client which send map requests twice */ - c = win_to_client(ev->window); - if (!c) - c = alloc_client(ev->window); - if (!c->frame) { - init_client(c, &wa); - attach_client(c); - } + XMapRequestEvent *ev = &e->xmaprequest; + static XWindowAttributes wa; + static Client *c; + + if(!XGetWindowAttributes(dpy, ev->window, &wa)) + return; + if(wa.override_redirect) { + XSelectInput(dpy, ev->window, + (StructureNotifyMask | PropertyChangeMask)); + return; + } + + /* there're client which send map requests twice */ + c = win_to_client(ev->window); + if(!c) + c = alloc_client(ev->window); + if(!c->frame) { + init_client(c, &wa); + attach_client(c); + } } -static void handle_motionnotify(XEvent * e) +static void +handle_motionnotify(XEvent * e) { - Frame *f = win_to_frame(e->xmotion.window); - Cursor cursor; - if (f) { - cursor = cursor_for_motion(f, e->xmotion.x, e->xmotion.y); - if (cursor != f->cursor) { - f->cursor = cursor; - XDefineCursor(dpy, f->win, cursor); - } - } + Frame *f = win_to_frame(e->xmotion.window); + Cursor cursor; + if(f) { + cursor = cursor_for_motion(f, e->xmotion.x, e->xmotion.y); + if(cursor != f->cursor) { + f->cursor = cursor; + XDefineCursor(dpy, f->win, cursor); + } + } } -static void handle_propertynotify(XEvent * e) +static void +handle_propertynotify(XEvent * e) { - XPropertyEvent *ev = &e->xproperty; - Client *c; + XPropertyEvent *ev = &e->xproperty; + Client *c; - if (ev->state == PropertyDelete) - return; /* ignore */ + if(ev->state == PropertyDelete) + return; /* ignore */ - if ((c = win_to_client(ev->window))) - handle_client_property(c, ev); + if((c = win_to_client(ev->window))) + handle_client_property(c, ev); } -static void handle_unmapnotify(XEvent * e) +static void +handle_unmapnotify(XEvent * e) { - XUnmapEvent *ev = &e->xunmap; - Client *c; - handle_ignore_enternotify_crap(e); - if ((c = win_to_client(ev->window))) { - if (!c->ignore_unmap) - detach_client(c, True); - else - c->ignore_unmap--; - } + XUnmapEvent *ev = &e->xunmap; + Client *c; + handle_ignore_enternotify_crap(e); + if((c = win_to_client(ev->window))) { + if(!c->ignore_unmap) + detach_client(c, True); + else + c->ignore_unmap--; + } } -static void handle_enternotify(XEvent * e) +static void +handle_enternotify(XEvent * e) { - XCrossingEvent *ev = &e->xcrossing; - Client *c; - - if ((ev->mode != NotifyNormal) || (ev->detail == NotifyInferior) || (ev->serial == ignore_enternotify_crap)) - return; - - c = win_to_client(ev->window); - if (c && c->frame) { - focus_area(c->frame->area); - c->frame->area->layout->focus(c->frame, False); - } + XCrossingEvent *ev = &e->xcrossing; + Client *c; + + if((ev->mode != NotifyNormal) || (ev->detail == NotifyInferior) + || (ev->serial == ignore_enternotify_crap)) + return; + + c = win_to_client(ev->window); + if(c && c->frame) { + focus_area(c->frame->area); + c->frame->area->layout->focus(c->frame, False); + } } -static void handle_ignore_enternotify_crap(XEvent *e) +static void +handle_ignore_enternotify_crap(XEvent * e) { - ignore_enternotify_crap = e->xany.serial; - XSync(dpy, False); + ignore_enternotify_crap = e->xany.serial; + XSync(dpy, False); } - diff --git a/cmd/wm/frame.c b/cmd/wm/frame.c @@ -11,203 +11,231 @@ #include "wm.h" static void select_client(void *obj, char *arg); -static void handle_after_write_frame(IXPServer *s, File *file); -static void handle_before_read_frame(IXPServer *s, File *file); +static void handle_after_write_frame(IXPServer * s, File * file); +static void handle_before_read_frame(IXPServer * s, File * file); /* action table for /frame/?/ namespace */ Action frame_acttbl[] = { - {"select", select_client}, - {0, 0} + {"select", select_client}, + {0, 0} }; -Frame *alloc_frame(XRectangle * r) +Frame * +alloc_frame(XRectangle * r) { - XSetWindowAttributes wa; - static int id = 0; - char buf[MAX_BUF]; - Frame *f = (Frame *) cext_emallocz(sizeof(Frame)); - int bw, th; - - f->rect = *r; - f->cursor = normal_cursor; - - snprintf(buf, MAX_BUF, "/detached/frame/%d", id); - f->file[F_PREFIX] = ixp_create(ixps, buf); - snprintf(buf, MAX_BUF, "/detached/frame/%d/client", id); - f->file[F_CLIENT_PREFIX] = ixp_create(ixps, buf); - snprintf(buf, MAX_BUF, "/detached/frame/%d/client/sel", id); - f->file[F_SEL_CLIENT] = ixp_create(ixps, buf); - f->file[F_SEL_CLIENT]->bind = 1; - snprintf(buf, MAX_BUF, "/detached/frame/%d/ctl", id); - f->file[F_CTL] = ixp_create(ixps, buf); - f->file[F_CTL]->after_write = handle_after_write_frame; - snprintf(buf, MAX_BUF, "/detached/frame/%d/geometry", id); - f->file[F_GEOMETRY] = ixp_create(ixps, buf); - f->file[F_GEOMETRY]->before_read = handle_before_read_frame; - f->file[F_GEOMETRY]->after_write = handle_after_write_frame; - snprintf(buf, MAX_BUF, "/detached/frame/%d/border", id); - f->file[F_BORDER] = wmii_create_ixpfile(ixps, buf, def[WM_BORDER]->content); - f->file[F_BORDER]->after_write = handle_after_write_frame; - snprintf(buf, MAX_BUF, "/detached/frame/%d/tab", id); - f->file[F_TAB] = wmii_create_ixpfile(ixps, buf, def[WM_TAB]->content); - f->file[F_TAB]->after_write = handle_after_write_frame; - snprintf(buf, MAX_BUF, "/detached/frame/%d/handleinc", id); - f->file[F_HANDLE_INC] = wmii_create_ixpfile(ixps, buf, def[WM_HANDLE_INC]->content); - f->file[F_HANDLE_INC]->after_write = handle_after_write_frame; - snprintf(buf, MAX_BUF, "/detached/frame/%d/locked", id); - f->file[F_LOCKED] = wmii_create_ixpfile(ixps, buf, def[WM_LOCKED]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/sstyle/bgcolor", id); - f->file[F_SEL_BG_COLOR] = wmii_create_ixpfile(ixps, buf, def[WM_SEL_BG_COLOR]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/sstyle/fgcolor", id); - f->file[F_SEL_FG_COLOR] = wmii_create_ixpfile(ixps, buf, def[WM_SEL_FG_COLOR]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/sstyle/bordercolor", id); - f->file[F_SEL_BORDER_COLOR] = wmii_create_ixpfile(ixps, buf, def[WM_SEL_BORDER_COLOR]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/nstyle/bgcolor", id); - f->file[F_NORM_BG_COLOR] = wmii_create_ixpfile(ixps, buf, def[WM_NORM_BG_COLOR]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/nstyle/fgcolor", id); - f->file[F_NORM_FG_COLOR] = wmii_create_ixpfile(ixps, buf, def[WM_NORM_FG_COLOR]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/nstyle/bordercolor", id); - f->file[F_NORM_BORDER_COLOR] = wmii_create_ixpfile(ixps, buf, def[WM_NORM_BORDER_COLOR]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b2press", id); - f->file[F_EVENT_B2PRESS] = wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B2PRESS]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b3press", id); - f->file[F_EVENT_B3PRESS] = wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B3PRESS]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b4press", id); - f->file[F_EVENT_B4PRESS] = wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B4PRESS]->content); - snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b5press", id); - f->file[F_EVENT_B5PRESS] = wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B5PRESS]->content); - id++; - - wa.override_redirect = 1; - wa.background_pixmap = ParentRelative; - wa.event_mask = SubstructureRedirectMask | ExposureMask | ButtonPressMask | PointerMotionMask; - - bw = border_width(f); - th = tab_height(f); - f->rect.width += 2 * bw; - f->rect.height += bw + (th ? th : bw); - f->win = XCreateWindow(dpy, root, f->rect.x, f->rect.y, f->rect.width, - f->rect.height, 0, DefaultDepth(dpy, screen_num), - CopyFromParent, DefaultVisual(dpy, screen_num), - CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - XDefineCursor(dpy, f->win, f->cursor); - f->gc = XCreateGC(dpy, f->win, 0, 0); - XSync(dpy, False); - return f; + XSetWindowAttributes wa; + static int id = 0; + char buf[MAX_BUF]; + Frame *f = (Frame *) cext_emallocz(sizeof(Frame)); + int bw, th; + + f->rect = *r; + f->cursor = normal_cursor; + + snprintf(buf, MAX_BUF, "/detached/frame/%d", id); + f->file[F_PREFIX] = ixp_create(ixps, buf); + snprintf(buf, MAX_BUF, "/detached/frame/%d/client", id); + f->file[F_CLIENT_PREFIX] = ixp_create(ixps, buf); + snprintf(buf, MAX_BUF, "/detached/frame/%d/client/sel", id); + f->file[F_SEL_CLIENT] = ixp_create(ixps, buf); + f->file[F_SEL_CLIENT]->bind = 1; + snprintf(buf, MAX_BUF, "/detached/frame/%d/ctl", id); + f->file[F_CTL] = ixp_create(ixps, buf); + f->file[F_CTL]->after_write = handle_after_write_frame; + snprintf(buf, MAX_BUF, "/detached/frame/%d/geometry", id); + f->file[F_GEOMETRY] = ixp_create(ixps, buf); + f->file[F_GEOMETRY]->before_read = handle_before_read_frame; + f->file[F_GEOMETRY]->after_write = handle_after_write_frame; + snprintf(buf, MAX_BUF, "/detached/frame/%d/border", id); + f->file[F_BORDER] = + wmii_create_ixpfile(ixps, buf, def[WM_BORDER]->content); + f->file[F_BORDER]->after_write = handle_after_write_frame; + snprintf(buf, MAX_BUF, "/detached/frame/%d/tab", id); + f->file[F_TAB] = wmii_create_ixpfile(ixps, buf, def[WM_TAB]->content); + f->file[F_TAB]->after_write = handle_after_write_frame; + snprintf(buf, MAX_BUF, "/detached/frame/%d/handleinc", id); + f->file[F_HANDLE_INC] = + wmii_create_ixpfile(ixps, buf, def[WM_HANDLE_INC]->content); + f->file[F_HANDLE_INC]->after_write = handle_after_write_frame; + snprintf(buf, MAX_BUF, "/detached/frame/%d/locked", id); + f->file[F_LOCKED] = + wmii_create_ixpfile(ixps, buf, def[WM_LOCKED]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/sstyle/bgcolor", id); + f->file[F_SEL_BG_COLOR] = + wmii_create_ixpfile(ixps, buf, def[WM_SEL_BG_COLOR]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/sstyle/fgcolor", id); + f->file[F_SEL_FG_COLOR] = + wmii_create_ixpfile(ixps, buf, def[WM_SEL_FG_COLOR]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/sstyle/bordercolor", id); + f->file[F_SEL_BORDER_COLOR] = + wmii_create_ixpfile(ixps, buf, def[WM_SEL_BORDER_COLOR]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/nstyle/bgcolor", id); + f->file[F_NORM_BG_COLOR] = + wmii_create_ixpfile(ixps, buf, def[WM_NORM_BG_COLOR]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/nstyle/fgcolor", id); + f->file[F_NORM_FG_COLOR] = + wmii_create_ixpfile(ixps, buf, def[WM_NORM_FG_COLOR]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/nstyle/bordercolor", id); + f->file[F_NORM_BORDER_COLOR] = + wmii_create_ixpfile(ixps, buf, def[WM_NORM_BORDER_COLOR]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b2press", id); + f->file[F_EVENT_B2PRESS] = + wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B2PRESS]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b3press", id); + f->file[F_EVENT_B3PRESS] = + wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B3PRESS]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b4press", id); + f->file[F_EVENT_B4PRESS] = + wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B4PRESS]->content); + snprintf(buf, MAX_BUF, "/detached/frame/%d/event/b5press", id); + f->file[F_EVENT_B5PRESS] = + wmii_create_ixpfile(ixps, buf, def[WM_EVENT_B5PRESS]->content); + id++; + + wa.override_redirect = 1; + wa.background_pixmap = ParentRelative; + wa.event_mask = + SubstructureRedirectMask | ExposureMask | ButtonPressMask | + PointerMotionMask; + + bw = border_width(f); + th = tab_height(f); + f->rect.width += 2 * bw; + f->rect.height += bw + (th ? th : bw); + f->win = XCreateWindow(dpy, root, f->rect.x, f->rect.y, f->rect.width, + f->rect.height, 0, DefaultDepth(dpy, + screen_num), + CopyFromParent, DefaultVisual(dpy, screen_num), + CWOverrideRedirect | CWBackPixmap | CWEventMask, + &wa); + XDefineCursor(dpy, f->win, f->cursor); + f->gc = XCreateGC(dpy, f->win, 0, 0); + XSync(dpy, False); + return f; } -Frame *win_to_frame(Window w) +Frame * +win_to_frame(Window w) { - Page *p; - for (p = pages; p; p = p->next) { - Frame *f; - for (f = p->managed->layout->frames(p->managed); f; f = f->next) - if (f->win == w) - return f; - for (f = p->floating->layout->frames(p->floating); f; f = f->next) - if (f->win == w) - return f; - } - return nil; + Page *p; + for(p = pages; p; p = p->next) { + Frame *f; + for(f = p->managed->layout->frames(p->managed); f; f = f->next) + if(f->win == w) + return f; + for(f = p->floating->layout->frames(p->floating); f; f = f->next) + if(f->win == w) + return f; + } + return nil; } -void destroy_frame(Frame * f) +void +destroy_frame(Frame * f) { - XFreeGC(dpy, f->gc); - XDestroyWindow(dpy, f->win); - ixp_remove_file(ixps, f->file[F_PREFIX]); - free(f); + XFreeGC(dpy, f->gc); + XDestroyWindow(dpy, f->win); + ixp_remove_file(ixps, f->file[F_PREFIX]); + free(f); } -unsigned int tab_height(Frame * f) +unsigned int +tab_height(Frame * f) { - if (blitz_strtonum(f->file[F_TAB]->content, 0, 1)) - return font->ascent + font->descent + 4; - return 0; + if(blitz_strtonum(f->file[F_TAB]->content, 0, 1)) + return font->ascent + font->descent + 4; + return 0; } -unsigned int border_width(Frame * f) +unsigned int +border_width(Frame * f) { - if (blitz_strtonum(f->file[F_BORDER]->content, 0, 1)) - return BORDER_WIDTH; - return 0; + if(blitz_strtonum(f->file[F_BORDER]->content, 0, 1)) + return BORDER_WIDTH; + return 0; } -static void check_dimensions(Frame * f, unsigned int tabh, unsigned int bw) +static void +check_dimensions(Frame * f, unsigned int tabh, unsigned int bw) { - Client *c = sel_client(); - if (!c) - return; - - if (c->size.flags & PMinSize) { - if (f->rect.width - 2 * bw < c->size.min_width) { - f->rect.width = c->size.min_width + 2 * bw; - } - if (f->rect.height - bw - (tabh ? tabh : bw) < c->size.min_height) { - f->rect.height = c->size.min_height + bw + (tabh ? tabh : bw); - } - } - if (c->size.flags & PMaxSize) { - if (f->rect.width - 2 * bw > c->size.max_width) { - f->rect.width = c->size.max_width + 2 * bw; - } - if (f->rect.height - bw - (tabh ? tabh : bw) > c->size.max_height) { - f->rect.height = c->size.max_height + bw + (tabh ? tabh : bw); - } - } + Client *c = sel_client(); + if(!c) + return; + + if(c->size.flags & PMinSize) { + if(f->rect.width - 2 * bw < c->size.min_width) { + f->rect.width = c->size.min_width + 2 * bw; + } + if(f->rect.height - bw - (tabh ? tabh : bw) < c->size.min_height) { + f->rect.height = c->size.min_height + bw + (tabh ? tabh : bw); + } + } + if(c->size.flags & PMaxSize) { + if(f->rect.width - 2 * bw > c->size.max_width) { + f->rect.width = c->size.max_width + 2 * bw; + } + if(f->rect.height - bw - (tabh ? tabh : bw) > c->size.max_height) { + f->rect.height = c->size.max_height + bw + (tabh ? tabh : bw); + } + } } -static void resize_incremental(Frame * f, unsigned int tabh, unsigned int bw) +static void +resize_incremental(Frame * f, unsigned int tabh, unsigned int bw) { - Client *c = f->sel; - if (!c) - return; - /* increment stuff, see chapter 4.1.2.3 of the ICCCM Manual */ - if (c->size.flags & PResizeInc) { - int base_width = 0, base_height = 0; - - if (c->size.flags & PBaseSize) { - base_width = c->size.base_width; - base_height = c->size.base_height; - } else if (c->size.flags & PMinSize) { - /* base_{width,height} default to min_{width,height} */ - base_width = c->size.min_width; - base_height = c->size.min_height; - } - /* client_width = base_width + i * c->size.width_inc for an integer i */ - f->rect.width -= - (f->rect.width - 2 * bw - base_width) % c->size.width_inc; - f->rect.height -= - (f->rect.height - bw - (tabh ? tabh : bw) - - base_height) % c->size.height_inc; - } + Client *c = f->sel; + if(!c) + return; + /* increment stuff, see chapter 4.1.2.3 of the ICCCM Manual */ + if(c->size.flags & PResizeInc) { + int base_width = 0, base_height = 0; + + if(c->size.flags & PBaseSize) { + base_width = c->size.base_width; + base_height = c->size.base_height; + } else if(c->size.flags & PMinSize) { + /* base_{width,height} default to min_{width,height} */ + base_width = c->size.min_width; + base_height = c->size.min_height; + } + /* client_width = base_width + i * c->size.width_inc for an integer i */ + f->rect.width -= + (f->rect.width - 2 * bw - base_width) % c->size.width_inc; + f->rect.height -= + (f->rect.height - bw - (tabh ? tabh : bw) - + base_height) % c->size.height_inc; + } } -void resize_frame(Frame * f, XRectangle * r, XPoint * pt) +void +resize_frame(Frame * f, XRectangle * r, XPoint * pt) { - Area *a = f->area; - unsigned int tabh = tab_height(f); - unsigned int bw = border_width(f); - Client *c; - - a->layout->resize(f, r, pt); - - /* resize if client requests special size */ - check_dimensions(f, tabh, bw); - - if (f->file[F_HANDLE_INC]->content && ((char *) f->file[F_HANDLE_INC]->content)[0] == '1') - resize_incremental(f, tabh, bw); - - XMoveResizeWindow(dpy, f->win, f->rect.x, f->rect.y, f->rect.width, f->rect.height); - - for (c = f->clients; c; c = c->next) { - c->rect.x = bw; - c->rect.y = tabh ? tabh : bw; - c->rect.width = c->frame->rect.width - 2 * bw; - c->rect.height = c->frame->rect.height - bw - (tabh ? tabh : bw); - XMoveResizeWindow(dpy, c->win, c->rect.x, c->rect.y, c->rect.width, c->rect.height); - configure_client(c); - } + Area *a = f->area; + unsigned int tabh = tab_height(f); + unsigned int bw = border_width(f); + Client *c; + + a->layout->resize(f, r, pt); + + /* resize if client requests special size */ + check_dimensions(f, tabh, bw); + + if(f->file[F_HANDLE_INC]->content + && ((char *) f->file[F_HANDLE_INC]->content)[0] == '1') + resize_incremental(f, tabh, bw); + + XMoveResizeWindow(dpy, f->win, f->rect.x, f->rect.y, f->rect.width, + f->rect.height); + + for(c = f->clients; c; c = c->next) { + c->rect.x = bw; + c->rect.y = tabh ? tabh : bw; + c->rect.width = c->frame->rect.width - 2 * bw; + c->rect.height = c->frame->rect.height - bw - (tabh ? tabh : bw); + XMoveResizeWindow(dpy, c->win, c->rect.x, c->rect.y, c->rect.width, + c->rect.height); + configure_client(c); + } } /** @@ -224,210 +252,232 @@ void resize_frame(Frame * f, XRectangle * r, XPoint * pt) * ./norm-style/bg-color "#RRGGBBAA" * ./norm-style/border-color "#RRGGBBAA [#RRGGBBAA [#RRGGBBAA [#RRGGBBAA]]]" */ -void draw_frame(Frame *f) +void +draw_frame(Frame * f) { - Draw d = { 0 }; - int bw = border_width(f); - XRectangle notch; - if (bw) { - notch.x = bw; - notch.y = bw; - notch.width = f->rect.width - 2 * bw; - notch.height = f->rect.height - 2 * bw; - d.drawable = f->win; - d.font = font; - d.gc = f->gc; - - /* define ground plate (i = 0) */ - if (f == sel_frame()) { - d.bg = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_BG_COLOR]->content); - d.fg = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_FG_COLOR]->content); - d.border = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_BORDER_COLOR]->content); - } else { - d.bg = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_BG_COLOR]->content); - d.fg = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_FG_COLOR]->content); - d.border = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_BORDER_COLOR]->content); - } - d.rect = f->rect; - d.rect.x = d.rect.y = 0; - d.notch = &notch; - - blitz_drawlabel(dpy, &d); - } - draw_clients(f); - XSync(dpy, False); + Draw d = { 0 }; + int bw = border_width(f); + XRectangle notch; + if(bw) { + notch.x = bw; + notch.y = bw; + notch.width = f->rect.width - 2 * bw; + notch.height = f->rect.height - 2 * bw; + d.drawable = f->win; + d.font = font; + d.gc = f->gc; + + /* define ground plate (i = 0) */ + if(f == sel_frame()) { + d.bg = + blitz_loadcolor(dpy, screen_num, + f->file[F_SEL_BG_COLOR]->content); + d.fg = + blitz_loadcolor(dpy, screen_num, + f->file[F_SEL_FG_COLOR]->content); + d.border = + blitz_loadcolor(dpy, screen_num, + f->file[F_SEL_BORDER_COLOR]->content); + } else { + d.bg = + blitz_loadcolor(dpy, screen_num, + f->file[F_NORM_BG_COLOR]->content); + d.fg = + blitz_loadcolor(dpy, screen_num, + f->file[F_NORM_FG_COLOR]->content); + d.border = + blitz_loadcolor(dpy, screen_num, + f->file[F_NORM_BORDER_COLOR]->content); + } + d.rect = f->rect; + d.rect.x = d.rect.y = 0; + d.notch = &notch; + + blitz_drawlabel(dpy, &d); + } + draw_clients(f); + XSync(dpy, False); } -void handle_frame_buttonpress(XButtonEvent *e, Frame *f) +void +handle_frame_buttonpress(XButtonEvent * e, Frame * f) { - Align align; - int bindex, cindex = e->x / (f->rect.width / f->nclients); - f->sel = clientat(f->clients, cindex); - if (e->button == Button1) { - align = cursor_to_align(f->cursor); - if (align == CENTER) - mouse_move(f); - else - mouse_resize(f, align); - f->area->layout->focus(f, False); - return; - } - f->area->layout->focus(f, False); - bindex = F_EVENT_B2PRESS - 2 + e->button; - /* frame mouse handling */ - if (f->file[bindex]->content) - wmii_spawn(dpy, f->file[bindex]->content); + Align align; + int bindex, cindex = e->x / (f->rect.width / f->nclients); + f->sel = clientat(f->clients, cindex); + if(e->button == Button1) { + align = cursor_to_align(f->cursor); + if(align == CENTER) + mouse_move(f); + else + mouse_resize(f, align); + f->area->layout->focus(f, False); + return; + } + f->area->layout->focus(f, False); + bindex = F_EVENT_B2PRESS - 2 + e->button; + /* frame mouse handling */ + if(f->file[bindex]->content) + wmii_spawn(dpy, f->file[bindex]->content); } -void attach_client_to_frame(Frame *f, Client *client) +void +attach_client_to_frame(Frame * f, Client * client) { - Client *c; - wmii_move_ixpfile(client->file[C_PREFIX], f->file[F_CLIENT_PREFIX]); - f->file[F_SEL_CLIENT]->content = client->file[C_PREFIX]->content; - for (c = f->clients; c && c->next; c = c->next); - if (!c) { - f->clients = client; - client->prev = client->next = nil; - } - else { - client->prev = c; - client->next = nil; - c->next = client; - } - f->nclients++; - f->sel = client; - client->frame = f; - resize_frame(f, &f->rect, 0); - reparent_client(client, f->win, client->rect.x, client->rect.y); - show_client(client); - sel_client(client); + Client *c; + wmii_move_ixpfile(client->file[C_PREFIX], f->file[F_CLIENT_PREFIX]); + f->file[F_SEL_CLIENT]->content = client->file[C_PREFIX]->content; + for(c = f->clients; c && c->next; c = c->next); + if(!c) { + f->clients = client; + client->prev = client->next = nil; + } else { + client->prev = c; + client->next = nil; + c->next = client; + } + f->nclients++; + f->sel = client; + client->frame = f; + resize_frame(f, &f->rect, 0); + reparent_client(client, f->win, client->rect.x, client->rect.y); + show_client(client); + sel_client(client); } -void detach_client_from_frame(Client *c, Bool unmap) +void +detach_client_from_frame(Client * c, Bool unmap) { - Frame *f = c->frame; - - c->frame = nil; - f->file[F_SEL_CLIENT]->content = nil; - wmii_move_ixpfile(c->file[C_PREFIX], def[WM_DETACHED_CLIENT]); - - if (f->sel == c) { - if (c->prev) - f->sel = c->prev; - else - f->sel = nil; - } - - if (f->clients == c) { - if (c->next) - c->next->prev = nil; - f->clients = c->next; - } - else { - c->prev->next = c->next; - if (c->next) - c->next->prev = c->prev; - } - - f->nclients--; - if (!f->sel) - f->sel = f->clients; - - if (!c->destroyed) { - if (!unmap) { - attach_detached(c); - hide_client(c); - } - c->rect.x = f->rect.x; - c->rect.y = f->rect.y; - reparent_client(c, root, c->rect.x, c->rect.y); - } - if (f->sel) { - focus_client(f->sel); - f->area->layout->focus(f, False); - } + Frame *f = c->frame; + + c->frame = nil; + f->file[F_SEL_CLIENT]->content = nil; + wmii_move_ixpfile(c->file[C_PREFIX], def[WM_DETACHED_CLIENT]); + + if(f->sel == c) { + if(c->prev) + f->sel = c->prev; + else + f->sel = nil; + } + + if(f->clients == c) { + if(c->next) + c->next->prev = nil; + f->clients = c->next; + } else { + c->prev->next = c->next; + if(c->next) + c->next->prev = c->prev; + } + + f->nclients--; + if(!f->sel) + f->sel = f->clients; + + if(!c->destroyed) { + if(!unmap) { + attach_detached(c); + hide_client(c); + } + c->rect.x = f->rect.x; + c->rect.y = f->rect.y; + reparent_client(c, root, c->rect.x, c->rect.y); + } + if(f->sel) { + focus_client(f->sel); + f->area->layout->focus(f, False); + } } -static void select_client(void *obj, char *arg) +static void +select_client(void *obj, char *arg) { - Client *c; - Frame *f = obj; - if (!f || !arg || !f->clients->next) - return; - if (!strncmp(arg, "prev", 5)) - c = f->sel->prev; - else if (!strncmp(arg, "next", 5)) - c = f->sel->next; - else - c = clientat(f->clients, blitz_strtonum(arg, 0, f->nclients - 1)); - focus_client(c); - f->area->layout->focus(f, False); + Client *c; + Frame *f = obj; + if(!f || !arg || !f->clients->next) + return; + if(!strncmp(arg, "prev", 5)) + c = f->sel->prev; + else if(!strncmp(arg, "next", 5)) + c = f->sel->next; + else + c = clientat(f->clients, blitz_strtonum(arg, 0, f->nclients - 1)); + focus_client(c); + f->area->layout->focus(f, False); } -static Frame *handle_before_read_frames(IXPServer *s, File *file, Area *a) +static Frame * +handle_before_read_frames(IXPServer * s, File * file, Area * a) { - Frame *f; - char buf[64]; - for (f = a->layout->frames(a); f; f = f->next) - if (file == f->file[F_GEOMETRY]) { - snprintf(buf, 64, "%d,%d,%d,%d", f->rect.x, f->rect.y, f->rect.width, f->rect.height); - if (file->content) - free(file->content); - file->content = cext_estrdup(buf); - file->size = strlen(buf); - return f; - } - return nil; + Frame *f; + char buf[64]; + for(f = a->layout->frames(a); f; f = f->next) + if(file == f->file[F_GEOMETRY]) { + snprintf(buf, 64, "%d,%d,%d,%d", f->rect.x, f->rect.y, + f->rect.width, f->rect.height); + if(file->content) + free(file->content); + file->content = cext_estrdup(buf); + file->size = strlen(buf); + return f; + } + return nil; } -static void handle_before_read_frame(IXPServer *s, File *file) +static void +handle_before_read_frame(IXPServer * s, File * file) { - Page *p; - for (p = pages; p; p = p->next) { - if (handle_before_read_frames(s, file, p->managed) - || handle_before_read_frames(s, file, p->floating)) - return; - } + Page *p; + for(p = pages; p; p = p->next) { + if(handle_before_read_frames(s, file, p->managed) + || handle_before_read_frames(s, file, p->floating)) + return; + } } -static Frame *handle_after_write_frames(IXPServer *s, File *file, Area *a) +static Frame * +handle_after_write_frames(IXPServer * s, File * file, Area * a) { - Frame *f; - for (f = a->layout->frames(a); f; f = f->next) { - if (file == f->file[F_CTL]) { - run_action(file, f, frame_acttbl); - return f; - } - if (file == f->file[F_TAB] || file == f->file[F_BORDER] || file == f->file[F_HANDLE_INC]) { - f->area->layout->arrange(f->area); - return f; - } else if (file == f->file[F_GEOMETRY]) { - char *geom = f->file[F_GEOMETRY]->content; - if (geom && strrchr(geom, ',')) { - XRectangle frect = f->rect; - blitz_strtorect(&rect, &frect, geom); - resize_frame(f, &frect, 0); - } - return f; - } - } - return nil; + Frame *f; + for(f = a->layout->frames(a); f; f = f->next) { + if(file == f->file[F_CTL]) { + run_action(file, f, frame_acttbl); + return f; + } + if(file == f->file[F_TAB] || file == f->file[F_BORDER] + || file == f->file[F_HANDLE_INC]) { + f->area->layout->arrange(f->area); + return f; + } else if(file == f->file[F_GEOMETRY]) { + char *geom = f->file[F_GEOMETRY]->content; + if(geom && strrchr(geom, ',')) { + XRectangle frect = f->rect; + blitz_strtorect(&rect, &frect, geom); + resize_frame(f, &frect, 0); + } + return f; + } + } + return nil; } -static void handle_after_write_frame(IXPServer *s, File *file) +static void +handle_after_write_frame(IXPServer * s, File * file) { - Page *p; - for (p = pages; p; p = p->next) { - if (handle_after_write_frames(s, file, p->managed) - || handle_after_write_frames(s, file, p->floating)) - return; - } + Page *p; + for(p = pages; p; p = p->next) { + if(handle_after_write_frames(s, file, p->managed) + || handle_after_write_frames(s, file, p->floating)) + return; + } } -Frame *sel_frame() +Frame * +sel_frame() { - Area *a = sel_area(); - if (!a) - return nil; - return a->layout->sel(a); + Area *a = sel_area(); + if(!a) + return nil; + return a->layout->sel(a); } diff --git a/cmd/wm/layout.c b/cmd/wm/layout.c @@ -7,11 +7,12 @@ #include "wm.h" -Layout *match_layout(char *name) +Layout * +match_layout(char *name) { - Layout *l; - for (l = layouts; l; l = l->next) - if (!strncmp(name, l->name, strlen(l->name))) - return l; - return nil; + Layout *l; + for(l = layouts; l; l = l->next) + if(!strncmp(name, l->name, strlen(l->name))) + return l; + return nil; } diff --git a/cmd/wm/layout_column.c b/cmd/wm/layout_column.c @@ -15,575 +15,594 @@ typedef struct Column Column; typedef struct Cell Cell; struct Acme { - Column *sel; - Column *columns; - size_t ncolumns; - Frame *frames; - size_t nframes; + Column *sel; + Column *columns; + size_t ncolumns; + Frame *frames; + size_t nframes; }; struct Cell { - Frame *frame; - Cell *next; - Cell *prev; - Column *col; + Frame *frame; + Cell *next; + Cell *prev; + Column *col; }; struct Column { - Cell *sel; - Cell *cells; - size_t ncells; - XRectangle rect; - Column *prev; - Column *next; + Cell *sel; + Cell *cells; + size_t ncells; + XRectangle rect; + Column *prev; + Column *next; }; -static void init_col(Area *a, Client *clients); -static Client *deinit_col(Area *a); -static void arrange_col(Area *a); -static Bool attach_col(Area *a, Client *c); -static void detach_col(Area *a, Client *c, Bool unmap); -static void resize_col(Frame *f, XRectangle *new, XPoint *pt); -static void focus_col(Frame *f, Bool raise); -static Frame *frames_col(Area *a); -static Frame *sel_col(Area *a); -static Action *actions_col(Area *a); +static void init_col(Area * a, Client * clients); +static Client *deinit_col(Area * a); +static void arrange_col(Area * a); +static Bool attach_col(Area * a, Client * c); +static void detach_col(Area * a, Client * c, Bool unmap); +static void resize_col(Frame * f, XRectangle * new, XPoint * pt); +static void focus_col(Frame * f, Bool raise); +static Frame *frames_col(Area * a); +static Frame *sel_col(Area * a); +static Action *actions_col(Area * a); static void select_frame(void *obj, char *arg); static void swap_frame(void *obj, char *arg); static void new_col(void *obj, char *arg); static Action lcol_acttbl[] = { - {"select", select_frame}, - {"swap", swap_frame}, - {"new", new_col}, - {0, 0} + {"select", select_frame}, + {"swap", swap_frame}, + {"new", new_col}, + {0, 0} }; -void init_layout_column() +void +init_layout_column() { - Layout *lp, *l = cext_emallocz(sizeof(Layout)); - l->name = "column"; - l->init = init_col; - l->deinit = deinit_col; - l->arrange = arrange_col; - l->attach = attach_col; - l->detach = detach_col; - l->resize = resize_col; - l->focus = focus_col; - l->frames = frames_col; - l->sel = sel_col; - l->actions = actions_col; - - for (lp = layouts; lp && lp->next; lp = lp->next); - if (lp) - lp->next = l; - else - layouts = l; + Layout *lp, *l = cext_emallocz(sizeof(Layout)); + l->name = "column"; + l->init = init_col; + l->deinit = deinit_col; + l->arrange = arrange_col; + l->attach = attach_col; + l->detach = detach_col; + l->resize = resize_col; + l->focus = focus_col; + l->frames = frames_col; + l->sel = sel_col; + l->actions = actions_col; + + for(lp = layouts; lp && lp->next; lp = lp->next); + if(lp) + lp->next = l; + else + layouts = l; } -static void arrange_column(Column *col) +static void +arrange_column(Column * col) { - Cell *cell; - unsigned int i = 0, h = area_rect.height / col->ncells; - for (cell = col->cells; cell; cell = cell->next) { - Frame *f = cell->frame; - f->rect = col->rect; - f->rect.y = area_rect.y + i * h; - if (!cell->next) - f->rect.height = area_rect.height - f->rect.y + area_rect.y; - else - f->rect.height = h; - resize_frame(f, &f->rect, 0); - i++; - } + Cell *cell; + unsigned int i = 0, h = area_rect.height / col->ncells; + for(cell = col->cells; cell; cell = cell->next) { + Frame *f = cell->frame; + f->rect = col->rect; + f->rect.y = area_rect.y + i * h; + if(!cell->next) + f->rect.height = area_rect.height - f->rect.y + area_rect.y; + else + f->rect.height = h; + resize_frame(f, &f->rect, 0); + i++; + } } -static void arrange_col(Area *a) +static void +arrange_col(Area * a) { - Acme *acme = a->aux; - Column *col; - for (col = acme->columns; col; col = col->next) - arrange_column(col); - XSync(dpy, False); + Acme *acme = a->aux; + Column *col; + for(col = acme->columns; col; col = col->next) + arrange_column(col); + XSync(dpy, False); } -static void init_col(Area *a, Client *clients) +static void +init_col(Area * a, Client * clients) { - Client *n, *c = clients; - - a->aux = cext_emallocz(sizeof(Acme)); - while (c) { - n = c->next; - attach_col(a, c); - c = n; - } + Client *n, *c = clients; + + a->aux = cext_emallocz(sizeof(Acme)); + while(c) { + n = c->next; + attach_col(a, c); + c = n; + } } -static void attach_frame(Area *a, Column *col, Frame *new) +static void +attach_frame(Area * a, Column * col, Frame * new) { - Acme *acme = a->aux; - Cell *c, *cell = cext_emallocz(sizeof(Cell)); - Frame *f; - - cell->frame = new; - cell->col = col; - new->aux = cell; - for (c = col->cells; c && c->next; c = c->next); - if (!c) - col->cells = cell; - else { - c->next = cell; - cell->prev = c; - } - col->sel = cell; - col->ncells++; - - for (f = acme->frames; f && f->next; f = f->next); - if (!f) - acme->frames = new; - else { - f->next = new; - new->prev = f; - } - attach_frame_to_area(a, new); - acme->nframes++; + Acme *acme = a->aux; + Cell *c, *cell = cext_emallocz(sizeof(Cell)); + Frame *f; + + cell->frame = new; + cell->col = col; + new->aux = cell; + for(c = col->cells; c && c->next; c = c->next); + if(!c) + col->cells = cell; + else { + c->next = cell; + cell->prev = c; + } + col->sel = cell; + col->ncells++; + + for(f = acme->frames; f && f->next; f = f->next); + if(!f) + acme->frames = new; + else { + f->next = new; + new->prev = f; + } + attach_frame_to_area(a, new); + acme->nframes++; } -static void detach_frame(Area *a, Frame *old) +static void +detach_frame(Area * a, Frame * old) { - Acme *acme = a->aux; - Cell *cell = old->aux; - Column *col = cell->col; - - if (col->sel == cell) { - if (cell->prev) - col->sel = cell->prev; - else - col->sel = nil; - } - if (cell->prev) - cell->prev->next = cell->next; - else - col->cells = cell->next; - if (cell->next) - cell->next->prev = cell->prev; - if (!col->sel) - col->sel = col->cells; - free(cell); - col->ncells--; - - if (old->prev) - old->prev->next = old->next; - else - acme->frames = old->next; - if (old->next) - old->next->prev = old->prev; - old->aux = nil; - detach_frame_from_area(old); - acme->nframes--; + Acme *acme = a->aux; + Cell *cell = old->aux; + Column *col = cell->col; + + if(col->sel == cell) { + if(cell->prev) + col->sel = cell->prev; + else + col->sel = nil; + } + if(cell->prev) + cell->prev->next = cell->next; + else + col->cells = cell->next; + if(cell->next) + cell->next->prev = cell->prev; + if(!col->sel) + col->sel = col->cells; + free(cell); + col->ncells--; + + if(old->prev) + old->prev->next = old->next; + else + acme->frames = old->next; + if(old->next) + old->next->prev = old->prev; + old->aux = nil; + detach_frame_from_area(old); + acme->nframes--; } -static Client *deinit_col(Area *a) +static Client * +deinit_col(Area * a) { - Acme *acme = a->aux; - Frame *f; - Client *cl, *res = nil, *c = nil; - Column *col = acme->columns; - - while ((f = acme->frames)) { - while ((cl = f->clients)) { - detach_client_from_frame(cl, False); - cl->prev = cl->next = 0; - if (!c) - res = c = cl; - else { - c->next = cl; - cl->prev = c; - c = cl; - } - } - detach_frame(a, f); - destroy_frame(f); - } - while ((col = acme->columns)) { - acme->columns = col->next; - free(col); - } - free(acme); - a->aux = 0; - return res; + Acme *acme = a->aux; + Frame *f; + Client *cl, *res = nil, *c = nil; + Column *col = acme->columns; + + while((f = acme->frames)) { + while((cl = f->clients)) { + detach_client_from_frame(cl, False); + cl->prev = cl->next = 0; + if(!c) + res = c = cl; + else { + c->next = cl; + cl->prev = c; + c = cl; + } + } + detach_frame(a, f); + destroy_frame(f); + } + while((col = acme->columns)) { + acme->columns = col->next; + free(col); + } + free(acme); + a->aux = 0; + return res; } -static Bool attach_col(Area *a, Client *c) +static Bool +attach_col(Area * a, Client * c) { - Acme *acme = a->aux; - Column *col = acme->sel; - Frame *f = nil; - - if (col && col->sel) - f = col->sel->frame; - - if (!col) { - col = cext_emallocz(sizeof(Column)); - col->rect = area_rect; - acme->columns = acme->sel = col; - acme->ncolumns++; - } - - /* check for tabbing? */ - if (f && (((char *) f->file[F_LOCKED]->content)[0] == '1')) - f = 0; - if (!f) { - f = alloc_frame(&c->rect); - attach_frame(a, col, f); - } - attach_client_to_frame(f, c); - arrange_column(col); - if (a->page == selpage) - XMapWindow(dpy, f->win); - focus_col(f, True); - return True; + Acme *acme = a->aux; + Column *col = acme->sel; + Frame *f = nil; + + if(col && col->sel) + f = col->sel->frame; + + if(!col) { + col = cext_emallocz(sizeof(Column)); + col->rect = area_rect; + acme->columns = acme->sel = col; + acme->ncolumns++; + } + + /* check for tabbing? */ + if(f && (((char *) f->file[F_LOCKED]->content)[0] == '1')) + f = 0; + if(!f) { + f = alloc_frame(&c->rect); + attach_frame(a, col, f); + } + attach_client_to_frame(f, c); + arrange_column(col); + if(a->page == selpage) + XMapWindow(dpy, f->win); + focus_col(f, True); + return True; } -static void update_column_width(Area *a) +static void +update_column_width(Area * a) { - Acme *acme = a->aux; - unsigned int i = 0, width; - Column *col; - - if (!acme->ncolumns) - return; - - width = area_rect.width / acme->ncolumns; - for (col = acme->columns; col; col = col->next) { - col->rect = area_rect; - col->rect.x = i * width; - col->rect.width = width; - i++; - } - arrange_col(a); + Acme *acme = a->aux; + unsigned int i = 0, width; + Column *col; + + if(!acme->ncolumns) + return; + + width = area_rect.width / acme->ncolumns; + for(col = acme->columns; col; col = col->next) { + col->rect = area_rect; + col->rect.x = i * width; + col->rect.width = width; + i++; + } + arrange_col(a); } -static void detach_col(Area *a, Client *c, Bool unmap) +static void +detach_col(Area * a, Client * c, Bool unmap) { - Acme *acme = a->aux; - Frame *f = c->frame; - Cell *cell = f->aux; - Column *col = cell->col; - - detach_client_from_frame(c, unmap); - if (!f->clients) { - detach_frame(a, f); - destroy_frame(f); - } - else return; - if (col->cells) - arrange_column(col); - else { - if (acme->sel == col) { - if (col->prev) - acme->sel = col->prev; - else - acme->sel = nil; - } - if (col->prev) - col->prev->next = col->next; - else - acme->columns = col->next; - if (col->next) - col->next->prev = col->prev; - if (!acme->sel) - acme->sel = acme->columns; - acme->ncolumns--; - free(col); - update_column_width(a); - } + Acme *acme = a->aux; + Frame *f = c->frame; + Cell *cell = f->aux; + Column *col = cell->col; + + detach_client_from_frame(c, unmap); + if(!f->clients) { + detach_frame(a, f); + destroy_frame(f); + } else + return; + if(col->cells) + arrange_column(col); + else { + if(acme->sel == col) { + if(col->prev) + acme->sel = col->prev; + else + acme->sel = nil; + } + if(col->prev) + col->prev->next = col->next; + else + acme->columns = col->next; + if(col->next) + col->next->prev = col->prev; + if(!acme->sel) + acme->sel = acme->columns; + acme->ncolumns--; + free(col); + update_column_width(a); + } } -static void match_frame_horiz(Column *col, XRectangle *r) +static void +match_frame_horiz(Column * col, XRectangle * r) { - Cell *cell; - for (cell = col->cells; cell; cell = cell->next) { - Frame *f = cell->frame; - f->rect.x = r->x; - f->rect.width = r->width; - resize_frame(f, &f->rect, nil); - } + Cell *cell; + for(cell = col->cells; cell; cell = cell->next) { + Frame *f = cell->frame; + f->rect.x = r->x; + f->rect.width = r->width; + resize_frame(f, &f->rect, nil); + } } -static void drop_resize(Frame *f, XRectangle *new) +static void +drop_resize(Frame * f, XRectangle * new) { - Column *west = nil, *east = nil, *col = nil; - Cell *north = nil, *south = nil; - Cell *cell = f->aux; - - col = cell->col; - west = col->prev; - east = col->next; - north = cell->prev; - south = cell->next; - - /* horizontal resize */ - if (west && (new->x != f->rect.x)) { - west->rect.width = new->x - west->rect.x; - col->rect.width += f->rect.x - new->x; - col->rect.x = new->x; - match_frame_horiz(west, &west->rect); - match_frame_horiz(col, &col->rect); - } - if (east && (new->x + new->width != f->rect.x + f->rect.width)) { - east->rect.width -= new->x + new->width - east->rect.x; - east->rect.x = new->x + new->width; - col->rect.x = new->x; - col->rect.width = new->width; - match_frame_horiz(col, &col->rect); - match_frame_horiz(east, &east->rect); - } - - /* vertical resize */ - if (north && (new->y != f->rect.y)) { - north->frame->rect.height = new->y - north->frame->rect.y; - f->rect.height += f->rect.y - new->y; - f->rect.y = new->y; - resize_frame(north->frame, &north->frame->rect, nil); - resize_frame(f, &f->rect, nil); - } - if (south && (new->y + new->height != f->rect.y + f->rect.height)) { - south->frame->rect.height -= new->y + new->height - south->frame->rect.y; - south->frame->rect.y = new->y + new->height; - f->rect.y = new->y; - f->rect.height = new->height; - resize_frame(f, &f->rect, nil); - resize_frame(south->frame, &south->frame->rect, nil); - } + Column *west = nil, *east = nil, *col = nil; + Cell *north = nil, *south = nil; + Cell *cell = f->aux; + + col = cell->col; + west = col->prev; + east = col->next; + north = cell->prev; + south = cell->next; + + /* horizontal resize */ + if(west && (new->x != f->rect.x)) { + west->rect.width = new->x - west->rect.x; + col->rect.width += f->rect.x - new->x; + col->rect.x = new->x; + match_frame_horiz(west, &west->rect); + match_frame_horiz(col, &col->rect); + } + if(east && (new->x + new->width != f->rect.x + f->rect.width)) { + east->rect.width -= new->x + new->width - east->rect.x; + east->rect.x = new->x + new->width; + col->rect.x = new->x; + col->rect.width = new->width; + match_frame_horiz(col, &col->rect); + match_frame_horiz(east, &east->rect); + } + + /* vertical resize */ + if(north && (new->y != f->rect.y)) { + north->frame->rect.height = new->y - north->frame->rect.y; + f->rect.height += f->rect.y - new->y; + f->rect.y = new->y; + resize_frame(north->frame, &north->frame->rect, nil); + resize_frame(f, &f->rect, nil); + } + if(south && (new->y + new->height != f->rect.y + f->rect.height)) { + south->frame->rect.height -= + new->y + new->height - south->frame->rect.y; + south->frame->rect.y = new->y + new->height; + f->rect.y = new->y; + f->rect.height = new->height; + resize_frame(f, &f->rect, nil); + resize_frame(south->frame, &south->frame->rect, nil); + } } -static void drop_moving(Frame *f, XRectangle *new, XPoint *pt) +static void +drop_moving(Frame * f, XRectangle * new, XPoint * pt) { - Area *a = f->area; - Cell *cell = f->aux; - Column *src = cell->col, *tgt = 0; - Acme *acme = a->aux; - - if (!pt) - return; - - for (tgt = acme->columns; tgt && !blitz_ispointinrect(pt->x, pt->y, &tgt->rect); tgt = tgt->next); - if (tgt) { - if (tgt != src) { - if (src->ncells < 2) - return; - detach_frame(a, f); - attach_frame(a, tgt, f); - arrange_column(src); - arrange_column(tgt); - focus_col(f, True); - } - else { - Cell *c; - for (c = src->cells; c && !blitz_ispointinrect(pt->x, pt->y, &c->frame->rect); c = c->next); - if (c && c != cell) { - Frame *tmp = c->frame; - c->frame = f; - cell->frame = tmp; - arrange_column(src); - focus_col(f, True); - } - } - } + Area *a = f->area; + Cell *cell = f->aux; + Column *src = cell->col, *tgt = 0; + Acme *acme = a->aux; + + if(!pt) + return; + + for(tgt = acme->columns; + tgt && !blitz_ispointinrect(pt->x, pt->y, &tgt->rect); + tgt = tgt->next); + if(tgt) { + if(tgt != src) { + if(src->ncells < 2) + return; + detach_frame(a, f); + attach_frame(a, tgt, f); + arrange_column(src); + arrange_column(tgt); + focus_col(f, True); + } else { + Cell *c; + for(c = src->cells; + c && !blitz_ispointinrect(pt->x, pt->y, &c->frame->rect); + c = c->next); + if(c && c != cell) { + Frame *tmp = c->frame; + c->frame = f; + cell->frame = tmp; + arrange_column(src); + focus_col(f, True); + } + } + } } -static void resize_col(Frame *f, XRectangle *new, XPoint *pt) +static void +resize_col(Frame * f, XRectangle * new, XPoint * pt) { - if ((f->rect.width == new->width) - && (f->rect.height == new->height)) - drop_moving(f, new, pt); - else - drop_resize(f, new); + if((f->rect.width == new->width) + && (f->rect.height == new->height)) + drop_moving(f, new, pt); + else + drop_resize(f, new); } -static void focus_col(Frame *f, Bool raise) +static void +focus_col(Frame * f, Bool raise) { - Area *a = f->area; - Acme *acme = a->aux; - Cell *old, *cell = f->aux; - Column *col = cell->col; - - old = col->sel; - acme->sel = col; - col->sel = cell; - sel_client(f->sel); - a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content; - if (raise) - center_pointer(f); - if (old && old != cell) - draw_frame(old->frame); - draw_frame(f); + Area *a = f->area; + Acme *acme = a->aux; + Cell *old, *cell = f->aux; + Column *col = cell->col; + + old = col->sel; + acme->sel = col; + col->sel = cell; + sel_client(f->sel); + a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content; + if(raise) + center_pointer(f); + if(old && old != cell) + draw_frame(old->frame); + draw_frame(f); } -static Frame *frames_col(Area *a) +static Frame * +frames_col(Area * a) { - Acme *acme = a->aux; - return acme->frames; + Acme *acme = a->aux; + return acme->frames; } -static Frame *sel_col(Area *a) { - Acme *acme = a->aux; - Column *col = acme->sel; +static Frame * +sel_col(Area * a) +{ + Acme *acme = a->aux; + Column *col = acme->sel; - if (col && col->sel) - return col->sel->frame; - return nil; + if(col && col->sel) + return col->sel->frame; + return nil; } -static Action *actions_col(Area *a) +static Action * +actions_col(Area * a) { - return lcol_acttbl; -} + return lcol_acttbl; +} -static void select_frame(void *obj, char *arg) +static void +select_frame(void *obj, char *arg) { - Area *a = obj; - Acme *acme = a->aux; - Column *c, *col = acme->sel; - Cell *cell; - - if (!col) - return; - - cell = col->sel; - if (!cell || !arg) - return; - if (!strncmp(arg, "prev", 5)) { - if (cell->prev) - cell = cell->prev; - else - for (cell = col->cells; cell && cell->next; cell = cell->next); - } - else if (!strncmp(arg, "next", 5)) { - if (cell->next) - cell = cell->next; - else - cell = col->cells; - } - else if (!strncmp(arg, "west", 5)) { - if (col->prev) - cell = col->prev->sel; - else - for (c = acme->columns; c && c->next; c = c->next); - } - else if (!strncmp(arg, "east", 5)) { - if (col->next) - cell = col->next->sel; - else - cell = acme->columns->sel; - } - else { - unsigned int i = 0, idx = blitz_strtonum(arg, 0, col->ncells - 1); - for (cell = col->cells; cell && i != idx; cell = cell->next) i++; - } - if (cell) - focus_col(cell->frame, True); + Area *a = obj; + Acme *acme = a->aux; + Column *c, *col = acme->sel; + Cell *cell; + + if(!col) + return; + + cell = col->sel; + if(!cell || !arg) + return; + if(!strncmp(arg, "prev", 5)) { + if(cell->prev) + cell = cell->prev; + else + for(cell = col->cells; cell && cell->next; cell = cell->next); + } else if(!strncmp(arg, "next", 5)) { + if(cell->next) + cell = cell->next; + else + cell = col->cells; + } else if(!strncmp(arg, "west", 5)) { + if(col->prev) + cell = col->prev->sel; + else + for(c = acme->columns; c && c->next; c = c->next); + } else if(!strncmp(arg, "east", 5)) { + if(col->next) + cell = col->next->sel; + else + cell = acme->columns->sel; + } else { + unsigned int i = 0, idx = blitz_strtonum(arg, 0, col->ncells - 1); + for(cell = col->cells; cell && i != idx; cell = cell->next) + i++; + } + if(cell) + focus_col(cell->frame, True); } -static void swap_frame(void *obj, char *arg) +static void +swap_frame(void *obj, char *arg) { - Area *a = obj; - Acme *acme = a->aux; - Column *west = nil, *east = nil, *col = acme->sel; - Cell *north = nil, *south = nil, *cell = col->sel; - Frame *f; - XRectangle r; - - if (!col || !cell || !arg) - return; - - west = col->prev; - east = col->next; - north = cell->prev; - south = cell->next; - - if (!strncmp(arg, "north", 6) && north) { - r = north->frame->rect; - north->frame->rect = cell->frame->rect; - cell->frame->rect = r; - f = north->frame; - north->frame = cell->frame; - cell->frame = f; - cell->frame->aux = cell; - north->frame->aux = north; - resize_frame(cell->frame, &cell->frame->rect, nil); - resize_frame(north->frame, &north->frame->rect, nil); - focus_col(cell->frame, True); - } - else if (!strncmp(arg, "south", 6) && south) { - r = south->frame->rect; - south->frame->rect = cell->frame->rect; - cell->frame->rect = r; - f = south->frame; - south->frame = cell->frame; - cell->frame = f; - cell->frame->aux = cell; - south->frame->aux = south; - resize_frame(cell->frame, &cell->frame->rect, nil); - resize_frame(south->frame, &south->frame->rect, nil); - focus_col(cell->frame, True); - } - else if (!strncmp(arg, "west", 5) && west && (col->ncells > 1)) { - Cell *other = west->sel; - r = other->frame->rect; - other->frame->rect = cell->frame->rect; - cell->frame->rect = r; - f = other->frame; - other->frame = cell->frame; - cell->frame = f; - other->frame->aux = other; - cell->frame->aux = cell; - resize_frame(cell->frame, &cell->frame->rect, nil); - resize_frame(other->frame, &other->frame->rect, nil); - focus_col(cell->frame, True); - } - else if (!strncmp(arg, "east", 5) && east && (col->ncells > 1)) { - Cell *other = east->sel; - r = other->frame->rect; - other->frame->rect = cell->frame->rect; - cell->frame->rect = r; - f = other->frame; - other->frame = cell->frame; - cell->frame = f; - other->frame->aux = other; - cell->frame->aux = cell; - resize_frame(cell->frame, &cell->frame->rect, nil); - resize_frame(other->frame, &other->frame->rect, nil); - focus_col(cell->frame, True); - } + Area *a = obj; + Acme *acme = a->aux; + Column *west = nil, *east = nil, *col = acme->sel; + Cell *north = nil, *south = nil, *cell = col->sel; + Frame *f; + XRectangle r; + + if(!col || !cell || !arg) + return; + + west = col->prev; + east = col->next; + north = cell->prev; + south = cell->next; + + if(!strncmp(arg, "north", 6) && north) { + r = north->frame->rect; + north->frame->rect = cell->frame->rect; + cell->frame->rect = r; + f = north->frame; + north->frame = cell->frame; + cell->frame = f; + cell->frame->aux = cell; + north->frame->aux = north; + resize_frame(cell->frame, &cell->frame->rect, nil); + resize_frame(north->frame, &north->frame->rect, nil); + focus_col(cell->frame, True); + } else if(!strncmp(arg, "south", 6) && south) { + r = south->frame->rect; + south->frame->rect = cell->frame->rect; + cell->frame->rect = r; + f = south->frame; + south->frame = cell->frame; + cell->frame = f; + cell->frame->aux = cell; + south->frame->aux = south; + resize_frame(cell->frame, &cell->frame->rect, nil); + resize_frame(south->frame, &south->frame->rect, nil); + focus_col(cell->frame, True); + } else if(!strncmp(arg, "west", 5) && west && (col->ncells > 1)) { + Cell *other = west->sel; + r = other->frame->rect; + other->frame->rect = cell->frame->rect; + cell->frame->rect = r; + f = other->frame; + other->frame = cell->frame; + cell->frame = f; + other->frame->aux = other; + cell->frame->aux = cell; + resize_frame(cell->frame, &cell->frame->rect, nil); + resize_frame(other->frame, &other->frame->rect, nil); + focus_col(cell->frame, True); + } else if(!strncmp(arg, "east", 5) && east && (col->ncells > 1)) { + Cell *other = east->sel; + r = other->frame->rect; + other->frame->rect = cell->frame->rect; + cell->frame->rect = r; + f = other->frame; + other->frame = cell->frame; + cell->frame = f; + other->frame->aux = other; + cell->frame->aux = cell; + resize_frame(cell->frame, &cell->frame->rect, nil); + resize_frame(other->frame, &other->frame->rect, nil); + focus_col(cell->frame, True); + } } -static void new_col(void *obj, char *arg) +static void +new_col(void *obj, char *arg) { - Area *a = obj; - Acme *acme = a->aux; - Column *c, *new, *col = acme->sel; - Frame *f; + Area *a = obj; + Acme *acme = a->aux; + Column *c, *new, *col = acme->sel; + Frame *f; - if (!col || col->ncells < 2) - return; + if(!col || col->ncells < 2) + return; - f = col->sel->frame; - for (c = acme->columns; c && c->next; c = c->next); + f = col->sel->frame; + for(c = acme->columns; c && c->next; c = c->next); - new = cext_emallocz(sizeof(Column)); - new->rect = area_rect; - new->prev = c; - c->next = new; - acme->ncolumns++; - acme->sel = new; + new = cext_emallocz(sizeof(Column)); + new->rect = area_rect; + new->prev = c; + c->next = new; + acme->ncolumns++; + acme->sel = new; - detach_frame(a, f); - attach_frame(a, new, f); + detach_frame(a, f); + attach_frame(a, new, f); - update_column_width(a); - focus_col(f, True); + update_column_width(a); + focus_col(f, True); } - diff --git a/cmd/wm/layout_float.c b/cmd/wm/layout_float.c @@ -10,232 +10,246 @@ #include "wm.h" #include "layoutdef.h" -static void init_float(Area *a, Client *clients); -static Client *deinit_float(Area *a); -static void arrange_float(Area *a); -static Bool attach_float(Area *a, Client *c); -static void detach_float(Area *a, Client *c, Bool unmap); -static void resize_float(Frame *f, XRectangle *new, XPoint *pt); -static void focus_float(Frame *f, Bool raise); -static Frame *frames_float(Area *a); -static Frame *sel_float(Area *a); -static Action *actions_float(Area *a); +static void init_float(Area * a, Client * clients); +static Client *deinit_float(Area * a); +static void arrange_float(Area * a); +static Bool attach_float(Area * a, Client * c); +static void detach_float(Area * a, Client * c, Bool unmap); +static void resize_float(Frame * f, XRectangle * new, XPoint * pt); +static void focus_float(Frame * f, Bool raise); +static Frame *frames_float(Area * a); +static Frame *sel_float(Area * a); +static Action *actions_float(Area * a); static void select_frame(void *obj, char *arg); Action lfloat_acttbl[] = { - {"select", select_frame}, - {0, 0} + {"select", select_frame}, + {0, 0} }; typedef struct { - Frame *frames; - Frame *sel; - size_t nframes; + Frame *frames; + Frame *sel; + size_t nframes; } Float; -void init_layout_float() +void +init_layout_float() { - Layout *lp, *l = cext_emallocz(sizeof(Layout)); - l->name = "float"; - l->init = init_float; - l->deinit = deinit_float; - l->arrange = arrange_float; - l->attach = attach_float; - l->detach = detach_float; - l->resize = resize_float; - l->focus = focus_float; - l->frames = frames_float; - l->sel = sel_float; - l->actions = actions_float; - - for (lp = layouts; lp && lp->next; lp = lp->next); - if (lp) - lp->next = l; - else - layouts = l; + Layout *lp, *l = cext_emallocz(sizeof(Layout)); + l->name = "float"; + l->init = init_float; + l->deinit = deinit_float; + l->arrange = arrange_float; + l->attach = attach_float; + l->detach = detach_float; + l->resize = resize_float; + l->focus = focus_float; + l->frames = frames_float; + l->sel = sel_float; + l->actions = actions_float; + + for(lp = layouts; lp && lp->next; lp = lp->next); + if(lp) + lp->next = l; + else + layouts = l; } -static void arrange_float(Area *a) +static void +arrange_float(Area * a) { } -static void init_float(Area *a, Client *clients) +static void +init_float(Area * a, Client * clients) { - Client *n, *c = clients; - Float *fl = cext_emallocz(sizeof(Float)); - a->aux = fl; - - while (c) { - n = c->next; - attach_float(a, c); - c = n; - } + Client *n, *c = clients; + Float *fl = cext_emallocz(sizeof(Float)); + a->aux = fl; + + while(c) { + n = c->next; + attach_float(a, c); + c = n; + } } -static void attach_frame(Area *a, Frame *new) +static void +attach_frame(Area * a, Frame * new) { - Float *fl = a->aux; - Frame *f; - - for (f = fl->frames; f && f->next; f = f->next); - if (!f) - fl->frames = new; - else { - f->next = new; - new->prev = f; - } - attach_frame_to_area(a, new); - fl->sel = new; - fl->nframes++; + Float *fl = a->aux; + Frame *f; + + for(f = fl->frames; f && f->next; f = f->next); + if(!f) + fl->frames = new; + else { + f->next = new; + new->prev = f; + } + attach_frame_to_area(a, new); + fl->sel = new; + fl->nframes++; } -static void detach_frame(Area *a, Frame *old) +static void +detach_frame(Area * a, Frame * old) { - Float *fl = a->aux; - - if (fl->sel == old) { - if (old->prev) - fl->sel = old->prev; - else - fl->sel = nil; - } - if (old->prev) - old->prev->next = old->next; - else - fl->frames = old->next; - if (old->next) - old->next->prev = old->prev; - if (!fl->sel) - fl->sel = fl->frames; - detach_frame_from_area(old); - fl->nframes--; + Float *fl = a->aux; + + if(fl->sel == old) { + if(old->prev) + fl->sel = old->prev; + else + fl->sel = nil; + } + if(old->prev) + old->prev->next = old->next; + else + fl->frames = old->next; + if(old->next) + old->next->prev = old->prev; + if(!fl->sel) + fl->sel = fl->frames; + detach_frame_from_area(old); + fl->nframes--; } -static Client *deinit_float(Area *a) +static Client * +deinit_float(Area * a) { - Float *fl = a->aux; - Client *res = nil, *c = nil, *cl; - Frame *f; - - while ((f = fl->frames)) { - while ((cl = f->clients)) { - detach_client_from_frame(cl, False); - cl->prev = cl->next = 0; - if (!res) - res = c = cl; - else { - c->next = cl; - cl->prev = c; - c = cl; - } - } - detach_frame(a, f); - destroy_frame(f); - } - free(fl); - a->aux = nil; - return res; + Float *fl = a->aux; + Client *res = nil, *c = nil, *cl; + Frame *f; + + while((f = fl->frames)) { + while((cl = f->clients)) { + detach_client_from_frame(cl, False); + cl->prev = cl->next = 0; + if(!res) + res = c = cl; + else { + c->next = cl; + cl->prev = c; + c = cl; + } + } + detach_frame(a, f); + destroy_frame(f); + } + free(fl); + a->aux = nil; + return res; } -static Bool attach_float(Area *a, Client *c) +static Bool +attach_float(Area * a, Client * c) { - Float *fl = a->aux; - Frame *f = fl->sel; - - /* check for tabbing? */ - if (f && (((char *) f->file[F_LOCKED]->content)[0] == '1')) - f = 0; - if (!f) { - if (c->rect.y < area_rect.y) - c->rect.y = area_rect.y; - if (c->rect.x < area_rect.x) - c->rect.x = area_rect.x; - - f = alloc_frame(&c->rect); - attach_frame_to_area(a, f); - attach_frame(a, f); - } - attach_client_to_frame(f, c); - if (a->page == selpage) - XMapWindow(dpy, f->win); - focus_float(f, True); - return True; + Float *fl = a->aux; + Frame *f = fl->sel; + + /* check for tabbing? */ + if(f && (((char *) f->file[F_LOCKED]->content)[0] == '1')) + f = 0; + if(!f) { + if(c->rect.y < area_rect.y) + c->rect.y = area_rect.y; + if(c->rect.x < area_rect.x) + c->rect.x = area_rect.x; + + f = alloc_frame(&c->rect); + attach_frame_to_area(a, f); + attach_frame(a, f); + } + attach_client_to_frame(f, c); + if(a->page == selpage) + XMapWindow(dpy, f->win); + focus_float(f, True); + return True; } -static void detach_float(Area *a, Client *c, Bool unmap) +static void +detach_float(Area * a, Client * c, Bool unmap) { - Frame *f = c->frame; - detach_client_from_frame(c, unmap); - if (!f->clients) { - detach_frame(a, f); - destroy_frame(f); - } + Frame *f = c->frame; + detach_client_from_frame(c, unmap); + if(!f->clients) { + detach_frame(a, f); + destroy_frame(f); + } } -static void resize_float(Frame *f, XRectangle *new, XPoint *pt) +static void +resize_float(Frame * f, XRectangle * new, XPoint * pt) { - f->rect = *new; + f->rect = *new; } -static void focus_float(Frame *f, Bool raise) +static void +focus_float(Frame * f, Bool raise) { - Area *a = f->area; - Float *fl = a->aux; - Frame *old = fl->sel; - - sel_client(f->sel); - fl->sel = f; - a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content; - if (raise) { - XRaiseWindow(dpy, f->win); - center_pointer(f); - } - if (old && old != f) - draw_frame(old); - draw_frame(f); + Area *a = f->area; + Float *fl = a->aux; + Frame *old = fl->sel; + + sel_client(f->sel); + fl->sel = f; + a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content; + if(raise) { + XRaiseWindow(dpy, f->win); + center_pointer(f); + } + if(old && old != f) + draw_frame(old); + draw_frame(f); } -static Frame *frames_float(Area *a) +static Frame * +frames_float(Area * a) { - Float *fl = a->aux; - return fl->frames; + Float *fl = a->aux; + return fl->frames; } -static void select_frame(void *obj, char *arg) +static void +select_frame(void *obj, char *arg) { - Area *a = obj; - Float *fl = a->aux; - Frame *f = fl->sel; - - if (!f || !arg) - return; - if (!strncmp(arg, "prev", 5)) { - if (f->prev) - f = f->prev; - else - for (f = fl->frames; f && f->next; f = f->next); - } - else if (!strncmp(arg, "next", 5)) { - if (f->next) - f = f->next; - else - f = fl->frames; - } - else { - unsigned int i = 0, idx = blitz_strtonum(arg, 0, fl->nframes - 1); - for (f = fl->frames; f && i != idx; f = f->next) i++; - } - if (f) - focus_float(f, True); + Area *a = obj; + Float *fl = a->aux; + Frame *f = fl->sel; + + if(!f || !arg) + return; + if(!strncmp(arg, "prev", 5)) { + if(f->prev) + f = f->prev; + else + for(f = fl->frames; f && f->next; f = f->next); + } else if(!strncmp(arg, "next", 5)) { + if(f->next) + f = f->next; + else + f = fl->frames; + } else { + unsigned int i = 0, idx = blitz_strtonum(arg, 0, fl->nframes - 1); + for(f = fl->frames; f && i != idx; f = f->next) + i++; + } + if(f) + focus_float(f, True); } -static Frame *sel_float(Area *a) { - Float *fl = a->aux; - return fl->sel; +static Frame * +sel_float(Area * a) +{ + Float *fl = a->aux; + return fl->sel; } -static Action *actions_float(Area *a) +static Action * +actions_float(Area * a) { - return lfloat_acttbl; + return lfloat_acttbl; } diff --git a/cmd/wm/mouse.c b/cmd/wm/mouse.c @@ -7,600 +7,624 @@ #include "wm.h" -Cursor cursor_for_motion(Frame * f, int x, int y) +Cursor +cursor_for_motion(Frame * f, int x, int y) { - int n, e, w, s, tn, te, tw, ts; - int tabh, bw; - - bw = border_width(f); - tabh = tab_height(f); - - if (!f || !bw) - return normal_cursor; - - /* rectangle attributes of client are used */ - w = x < bw; - e = x >= f->rect.width - bw; - n = y < bw; - s = y >= f->rect.height - bw; - - tw = x < (tabh ? tabh : 2 * bw); - te = x > f->rect.width - (tabh ? tabh : 2 * bw); - tn = y < (tabh ? tabh : 2 * bw); - ts = s > f->rect.height - (tabh ? tabh : 2 * bw); - - if ((w && n) || (w && tn) || (n && tw)) - return nw_cursor; - else if ((e && n) || (e && tn) || (n && te)) - return ne_cursor; - else if ((w && s) || (w && ts) || (s && tw)) - return sw_cursor; - else if ((e && s) || (e && ts) || (s && te)) - return se_cursor; - else if (w) - return w_cursor; - else if (e) - return e_cursor; - else if (n) - return n_cursor; - else if (s) - return s_cursor; - - return normal_cursor; + int n, e, w, s, tn, te, tw, ts; + int tabh, bw; + + bw = border_width(f); + tabh = tab_height(f); + + if(!f || !bw) + return normal_cursor; + + /* rectangle attributes of client are used */ + w = x < bw; + e = x >= f->rect.width - bw; + n = y < bw; + s = y >= f->rect.height - bw; + + tw = x < (tabh ? tabh : 2 * bw); + te = x > f->rect.width - (tabh ? tabh : 2 * bw); + tn = y < (tabh ? tabh : 2 * bw); + ts = s > f->rect.height - (tabh ? tabh : 2 * bw); + + if((w && n) || (w && tn) || (n && tw)) + return nw_cursor; + else if((e && n) || (e && tn) || (n && te)) + return ne_cursor; + else if((w && s) || (w && ts) || (s && tw)) + return sw_cursor; + else if((e && s) || (e && ts) || (s && te)) + return se_cursor; + else if(w) + return w_cursor; + else if(e) + return e_cursor; + else if(n) + return n_cursor; + else if(s) + return s_cursor; + + return normal_cursor; } -Align xy_to_align(XRectangle * rect, int x, int y) +Align +xy_to_align(XRectangle * rect, int x, int y) { - int w = x <= rect->x + rect->width / 2; - int n = y <= rect->y + rect->height / 2; - int e = x > rect->x + rect->width / 2; - int s = y > rect->y + rect->height / 2; - int nw = w && n; - int ne = e && n; - int sw = w && s; - int se = e && s; - - if (nw) - return NWEST; - else if (ne) - return NEAST; - else if (se) - return SEAST; - else if (sw) - return SWEST; - else if (w) - return WEST; - else if (e) - return EAST; - else if (n) - return NORTH; - else if (s) - return SOUTH; - return CENTER; + int w = x <= rect->x + rect->width / 2; + int n = y <= rect->y + rect->height / 2; + int e = x > rect->x + rect->width / 2; + int s = y > rect->y + rect->height / 2; + int nw = w && n; + int ne = e && n; + int sw = w && s; + int se = e && s; + + if(nw) + return NWEST; + else if(ne) + return NEAST; + else if(se) + return SEAST; + else if(sw) + return SWEST; + else if(w) + return WEST; + else if(e) + return EAST; + else if(n) + return NORTH; + else if(s) + return SOUTH; + return CENTER; } -Align cursor_to_align(Cursor cursor) +Align +cursor_to_align(Cursor cursor) { - if (cursor == w_cursor) - return WEST; - else if (cursor == nw_cursor) - return NWEST; - else if (cursor == n_cursor) - return NORTH; - else if (cursor == ne_cursor) - return NEAST; - else if (cursor == e_cursor) - return EAST; - else if (cursor == se_cursor) - return SEAST; - else if (cursor == s_cursor) - return SOUTH; - else if (cursor == sw_cursor) - return SWEST; - return CENTER; /* should not happen */ + if(cursor == w_cursor) + return WEST; + else if(cursor == nw_cursor) + return NWEST; + else if(cursor == n_cursor) + return NORTH; + else if(cursor == ne_cursor) + return NEAST; + else if(cursor == e_cursor) + return EAST; + else if(cursor == se_cursor) + return SEAST; + else if(cursor == s_cursor) + return SOUTH; + else if(cursor == sw_cursor) + return SWEST; + return CENTER; /* should not happen */ } -static int check_vert_match(XRectangle * r, XRectangle * neighbor) +static int +check_vert_match(XRectangle * r, XRectangle * neighbor) { - /* check if neighbor matches edge */ - return (((neighbor->y <= r->y) - && (neighbor->y + neighbor->height >= r->y)) - || ((neighbor->y >= r->y) - && (r->y + r->height >= neighbor->y))); + /* check if neighbor matches edge */ + return (((neighbor->y <= r->y) + && (neighbor->y + neighbor->height >= r->y)) + || ((neighbor->y >= r->y) + && (r->y + r->height >= neighbor->y))); } -static int check_horiz_match(XRectangle * r, XRectangle * neighbor) +static int +check_horiz_match(XRectangle * r, XRectangle * neighbor) { - /* check if neighbor matches edge */ - return (((neighbor->x <= r->x) - && (neighbor->x + neighbor->width >= r->x)) - || ((neighbor->x >= r->x) - && (r->x + r->width >= neighbor->x))); + /* check if neighbor matches edge */ + return (((neighbor->x <= r->x) + && (neighbor->x + neighbor->width >= r->x)) + || ((neighbor->x >= r->x) + && (r->x + r->width >= neighbor->x))); } static void snap_move(XRectangle * r, XRectangle * rects, - unsigned int num, int snapw, int snaph) + unsigned int num, int snapw, int snaph) { - int i, j, w = 0, n = 0, e = 0, s = 0; - - /* snap to other windows */ - for (i = 0; i <= snapw && !(w && e); i++) { - - for (j = 0; j < num && !(w && e); j++) { - - /* check west neighbors leftwards */ - if (!w) { - if (r->x - i == (rects[j].x + rects[j].width)) { - /* - * west edge of neighbor found, check - * vert match - */ - w = check_vert_match(r, &rects[j]); - if (w) - r->x = rects[j].x + rects[j].width; - } - } - /* check west neighbors rightwards */ - if (!w) { - if (r->x + i == (rects[j].x + rects[j].width)) { - /* - * west edge of neighbor found, check - * vert match - */ - w = check_vert_match(r, &rects[j]); - if (w) - r->x = rects[j].x + rects[j].width; - } - } - /* check east neighbors leftwards */ - if (!e) { - if (r->x + r->width - i == rects[j].x) { - /* - * east edge of neighbor found, check - * vert match - */ - e = check_vert_match(r, &rects[j]); - if (e) - r->x = rects[j].x - r->width; - } - } - /* check east neighbors rightwards */ - if (!e) { - if (r->x + r->width + i == rects[j].x) { - /* - * east edge of neighbor found, check - * vert match - */ - e = check_vert_match(r, &rects[j]); - if (e) - r->x = rects[j].x - r->width; - } - } - } - - /* snap to west screen border */ - if (!w && (r->x - i == rect.x)) { - w = 1; - r->x = rect.x; - } - /* snap to west screen border */ - if (!w && (r->x + i == rect.x)) { - w = 1; - r->x = rect.x; - } - /* snap to east screen border */ - if (!e && (r->x + r->width - i == rect.width)) { - e = 1; - r->x = rect.x + rect.width - r->width; - } - if (!e && (r->x + r->width + i == rect.width)) { - e = 1; - r->x = rect.x + rect.width - r->width; - } - } - - for (i = 0; i <= snaph && !(n && s); i++) { - - for (j = 0; j < num && !(n && s); j++) { - /* check north neighbors upwards */ - if (!n) { - if (r->y - i == (rects[j].y + rects[j].height)) { - /* - * north edge of neighbor found, - * check horiz match - */ - n = check_horiz_match(r, &rects[j]); - if (n) - r->y = rects[j].y + rects[j].height; - } - } - /* check north neighbors downwards */ - if (!n) { - if (r->y + i == (rects[j].y + rects[j].height)) { - /* - * north edge of neighbor found, - * check horiz match - */ - n = check_horiz_match(r, &rects[j]); - if (n) - r->y = rects[j].y + rects[j].height; - } - } - /* check south neighbors upwards */ - if (!s) { - if (r->y + r->height - i == rects[j].y) { - /* - * south edge of neighbor found, - * check horiz match - */ - s = check_horiz_match(r, &rects[j]); - if (s) - r->y = rects[j].y - r->height; - } - } - /* check south neighbors downwards */ - if (!s) { - if (r->y + r->height + i == rects[j].y) { - /* - * south edge of neighbor found, - * check horiz match - */ - s = check_horiz_match(r, &rects[j]); - if (s) - r->y = rects[j].y - r->height; - } - } - } - - /* snap to north screen border */ - if (!n && (r->y - i == rect.y)) { - n = 1; - r->y = rect.y; - } - if (!n && (r->y + i == rect.y)) { - n = 1; - r->y = rect.y; - } - /* snap to south screen border */ - if (!s && (r->y + r->height - i == rect.height)) { - s = 1; - r->y = rect.y + rect.height - r->height; - } - if (!s && (r->y + r->height + i == rect.height)) { - s = 1; - r->y = rect.y + rect.height - r->height; - } - } + int i, j, w = 0, n = 0, e = 0, s = 0; + + /* snap to other windows */ + for(i = 0; i <= snapw && !(w && e); i++) { + + for(j = 0; j < num && !(w && e); j++) { + + /* check west neighbors leftwards */ + if(!w) { + if(r->x - i == (rects[j].x + rects[j].width)) { + /* + * west edge of neighbor found, check + * vert match + */ + w = check_vert_match(r, &rects[j]); + if(w) + r->x = rects[j].x + rects[j].width; + } + } + /* check west neighbors rightwards */ + if(!w) { + if(r->x + i == (rects[j].x + rects[j].width)) { + /* + * west edge of neighbor found, check + * vert match + */ + w = check_vert_match(r, &rects[j]); + if(w) + r->x = rects[j].x + rects[j].width; + } + } + /* check east neighbors leftwards */ + if(!e) { + if(r->x + r->width - i == rects[j].x) { + /* + * east edge of neighbor found, check + * vert match + */ + e = check_vert_match(r, &rects[j]); + if(e) + r->x = rects[j].x - r->width; + } + } + /* check east neighbors rightwards */ + if(!e) { + if(r->x + r->width + i == rects[j].x) { + /* + * east edge of neighbor found, check + * vert match + */ + e = check_vert_match(r, &rects[j]); + if(e) + r->x = rects[j].x - r->width; + } + } + } + + /* snap to west screen border */ + if(!w && (r->x - i == rect.x)) { + w = 1; + r->x = rect.x; + } + /* snap to west screen border */ + if(!w && (r->x + i == rect.x)) { + w = 1; + r->x = rect.x; + } + /* snap to east screen border */ + if(!e && (r->x + r->width - i == rect.width)) { + e = 1; + r->x = rect.x + rect.width - r->width; + } + if(!e && (r->x + r->width + i == rect.width)) { + e = 1; + r->x = rect.x + rect.width - r->width; + } + } + + for(i = 0; i <= snaph && !(n && s); i++) { + + for(j = 0; j < num && !(n && s); j++) { + /* check north neighbors upwards */ + if(!n) { + if(r->y - i == (rects[j].y + rects[j].height)) { + /* + * north edge of neighbor found, + * check horiz match + */ + n = check_horiz_match(r, &rects[j]); + if(n) + r->y = rects[j].y + rects[j].height; + } + } + /* check north neighbors downwards */ + if(!n) { + if(r->y + i == (rects[j].y + rects[j].height)) { + /* + * north edge of neighbor found, + * check horiz match + */ + n = check_horiz_match(r, &rects[j]); + if(n) + r->y = rects[j].y + rects[j].height; + } + } + /* check south neighbors upwards */ + if(!s) { + if(r->y + r->height - i == rects[j].y) { + /* + * south edge of neighbor found, + * check horiz match + */ + s = check_horiz_match(r, &rects[j]); + if(s) + r->y = rects[j].y - r->height; + } + } + /* check south neighbors downwards */ + if(!s) { + if(r->y + r->height + i == rects[j].y) { + /* + * south edge of neighbor found, + * check horiz match + */ + s = check_horiz_match(r, &rects[j]); + if(s) + r->y = rects[j].y - r->height; + } + } + } + + /* snap to north screen border */ + if(!n && (r->y - i == rect.y)) { + n = 1; + r->y = rect.y; + } + if(!n && (r->y + i == rect.y)) { + n = 1; + r->y = rect.y; + } + /* snap to south screen border */ + if(!s && (r->y + r->height - i == rect.height)) { + s = 1; + r->y = rect.y + rect.height - r->height; + } + if(!s && (r->y + r->height + i == rect.height)) { + s = 1; + r->y = rect.y + rect.height - r->height; + } + } } -static void draw_pseudo_border(XRectangle * r) +static void +draw_pseudo_border(XRectangle * r) { - XRectangle pseudo = *r; - - pseudo.x += 2; - pseudo.y += 2; - pseudo.width -= 4; - pseudo.height -= 4; - XDrawRectangles(dpy, root, xorgc, &pseudo, 1); - XSync(dpy, False); + XRectangle pseudo = *r; + + pseudo.x += 2; + pseudo.y += 2; + pseudo.width -= 4; + pseudo.height -= 4; + XDrawRectangles(dpy, root, xorgc, &pseudo, 1); + XSync(dpy, False); } -void mouse_move(Frame * f) +void +mouse_move(Frame * f) { - int px = 0, py = 0, wex, wey, ex, ey, first = 1, i; - Window dummy; - XEvent ev; - /* borders */ - int snapw = rect.width * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, 1000) / 1000; - int snaph = rect.height * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, 1000) / 1000; - unsigned int num; - unsigned int dmask; - XRectangle *rects = rectangles(&num); - XRectangle frect = f->rect; - XPoint pt; - - XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &wex, &wey, &dmask); - XTranslateCoordinates(dpy, f->win, root, wex, wey, &ex, &ey, &dummy); - pt.x = ex; - pt.y = ey; - XSync(dpy, False); - XGrabServer(dpy); - while (XGrabPointer(dpy, root, False, ButtonMotionMask | ButtonReleaseMask, - GrabModeAsync, GrabModeAsync, None, move_cursor, CurrentTime) != GrabSuccess) - usleep(20000); - - for (;;) { - while (!XCheckMaskEvent(dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { - usleep(20000); - continue; - } - - switch (ev.type) { - case ButtonRelease: - if (!first) { - draw_pseudo_border(&frect); - resize_frame(f, &frect, &pt); - } - free(rects); - XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); - XUngrabServer(dpy); - XSync(dpy, False); - return; - break; - case MotionNotify: - pt.x = ev.xmotion.x; - pt.y = ev.xmotion.y; - XTranslateCoordinates(dpy, f->win, root, ev.xmotion.x, - ev.xmotion.y, &px, &py, &dummy); - if (first) - first = 0; - else - draw_pseudo_border(&frect); - frect.x = px - ex; - frect.y = py - ey; - snap_move(&frect, rects, num, snapw, snaph); - draw_pseudo_border(&frect); - break; - } - } + int px = 0, py = 0, wex, wey, ex, ey, first = 1, i; + Window dummy; + XEvent ev; + /* borders */ + int snapw = + rect.width * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, + 1000) / 1000; + int snaph = + rect.height * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, + 1000) / 1000; + unsigned int num; + unsigned int dmask; + XRectangle *rects = rectangles(&num); + XRectangle frect = f->rect; + XPoint pt; + + XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &wex, &wey, &dmask); + XTranslateCoordinates(dpy, f->win, root, wex, wey, &ex, &ey, &dummy); + pt.x = ex; + pt.y = ey; + XSync(dpy, False); + XGrabServer(dpy); + while(XGrabPointer + (dpy, root, False, ButtonMotionMask | ButtonReleaseMask, + GrabModeAsync, GrabModeAsync, None, move_cursor, + CurrentTime) != GrabSuccess) + usleep(20000); + + for(;;) { + while(!XCheckMaskEvent + (dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { + usleep(20000); + continue; + } + + switch (ev.type) { + case ButtonRelease: + if(!first) { + draw_pseudo_border(&frect); + resize_frame(f, &frect, &pt); + } + free(rects); + XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); + XUngrabServer(dpy); + XSync(dpy, False); + return; + break; + case MotionNotify: + pt.x = ev.xmotion.x; + pt.y = ev.xmotion.y; + XTranslateCoordinates(dpy, f->win, root, ev.xmotion.x, + ev.xmotion.y, &px, &py, &dummy); + if(first) + first = 0; + else + draw_pseudo_border(&frect); + frect.x = px - ex; + frect.y = py - ey; + snap_move(&frect, rects, num, snapw, snaph); + draw_pseudo_border(&frect); + break; + } + } } static void snap_resize(XRectangle * r, XRectangle * o, Align align, - XRectangle * rects, unsigned int num, int px, int ox, int py, - int oy, int snapw, int snaph) + XRectangle * rects, unsigned int num, int px, int ox, int py, + int oy, int snapw, int snaph) { - int i, j, pend = 0; - int w, h; - - /* x */ - switch (align) { - case NEAST: - case EAST: - case SEAST: - w = px - r->x + (o->width - ox); - if (w < 10) - break; - r->width = w; - if (w <= snapw) - break; - /* snap to border */ - for (i = 0; !pend && (i < snapw); i++) { - if (r->x + r->width - i == rect.x + rect.width) { - r->width -= i; - break; - } - if (r->x + r->width + i == rect.x + rect.width) { - r->width += i; - break; - } - for (j = 0; j < num; j++) { - if (r->x + r->width - i == rects[j].x) { - pend = check_vert_match(r, &rects[j]); - if (pend) { - r->width -= i; - break; - } - } - if (r->x + r->width + i == rects[j].x) { - pend = check_vert_match(r, &rects[j]); - if (pend) { - r->width += i; - break; - } - } - } - } - break; - case NWEST: - case WEST: - case SWEST: - w = r->width + r->x - px + ox; - if (w < 10) - break; - r->width = w; - r->x = px - ox; - if (w <= snapw) - break; - /* snap to border */ - for (i = 0; !pend && (i < snapw); i++) { - if (r->x - i == rect.x) { - r->x -= i; - r->width += i; - break; - } - if (r->x + i == rect.x) { - r->x += i; - r->width -= i; - break; - } - for (j = 0; j < num; j++) { - if (r->x - i == rects[j].x + rects[j].width) { - pend = check_vert_match(r, &rects[j]); - if (pend) { - r->x -= i; - r->width += i; - break; - } - } - if (r->x + i == rects[j].x + rects[j].width) { - pend = check_vert_match(r, &rects[j]); - if (pend) { - r->x += i; - r->width -= i; - break; - } - } - } - } - break; - default: - break; - } - - /* y */ - pend = 0; - switch (align) { - case SWEST: - case SOUTH: - case SEAST: - h = py - r->y + (o->height - oy); - if (h < 10) - break; - r->height = h; - if (h <= snaph) - break; - /* snap to border */ - for (i = 0; !pend && (i < snaph); i++) { - if (r->y + r->height - i == rect.y + rect.height) { - r->height -= i; - break; - } - if (r->y + r->height + i == rect.y + rect.height) { - r->height += i; - break; - } - for (j = 0; j < num; j++) { - if (r->y + r->height - i == rects[j].y) { - pend = check_horiz_match(r, &rects[j]); - if (pend) { - r->height -= i; - break; - } - } - if (r->y + r->height + i == rects[j].y) { - pend = check_horiz_match(r, &rects[j]); - if (pend) { - r->height += i; - break; - } - } - } - } - break; - case NWEST: - case NORTH: - case NEAST: - h = r->height + r->y - py + oy; - if (h < 10) - break; - r->height = h; - r->y = py - oy; - if (h <= snaph) - break; - /* snap to border */ - for (i = 0; !pend && (i < snaph); i++) { - if (r->y - i == rect.y) { - r->y -= i; - r->height += i; - break; - } - if (r->y + i == rect.y) { - r->y += i; - r->height -= i; - break; - } - for (j = 0; j < num; j++) { - if (r->y - i == rects[j].y + rects[j].height) { - pend = check_horiz_match(r, &rects[j]); - if (pend) { - r->y -= i; - r->height += i; - break; - } - } - if (r->y + i == rects[j].y + rects[j].height) { - pend = check_horiz_match(r, &rects[j]); - if (pend) { - r->y += i; - r->height -= i; - break; - } - } - } - } - break; - default: - break; - } + int i, j, pend = 0; + int w, h; + + /* x */ + switch (align) { + case NEAST: + case EAST: + case SEAST: + w = px - r->x + (o->width - ox); + if(w < 10) + break; + r->width = w; + if(w <= snapw) + break; + /* snap to border */ + for(i = 0; !pend && (i < snapw); i++) { + if(r->x + r->width - i == rect.x + rect.width) { + r->width -= i; + break; + } + if(r->x + r->width + i == rect.x + rect.width) { + r->width += i; + break; + } + for(j = 0; j < num; j++) { + if(r->x + r->width - i == rects[j].x) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->width -= i; + break; + } + } + if(r->x + r->width + i == rects[j].x) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->width += i; + break; + } + } + } + } + break; + case NWEST: + case WEST: + case SWEST: + w = r->width + r->x - px + ox; + if(w < 10) + break; + r->width = w; + r->x = px - ox; + if(w <= snapw) + break; + /* snap to border */ + for(i = 0; !pend && (i < snapw); i++) { + if(r->x - i == rect.x) { + r->x -= i; + r->width += i; + break; + } + if(r->x + i == rect.x) { + r->x += i; + r->width -= i; + break; + } + for(j = 0; j < num; j++) { + if(r->x - i == rects[j].x + rects[j].width) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->x -= i; + r->width += i; + break; + } + } + if(r->x + i == rects[j].x + rects[j].width) { + pend = check_vert_match(r, &rects[j]); + if(pend) { + r->x += i; + r->width -= i; + break; + } + } + } + } + break; + default: + break; + } + + /* y */ + pend = 0; + switch (align) { + case SWEST: + case SOUTH: + case SEAST: + h = py - r->y + (o->height - oy); + if(h < 10) + break; + r->height = h; + if(h <= snaph) + break; + /* snap to border */ + for(i = 0; !pend && (i < snaph); i++) { + if(r->y + r->height - i == rect.y + rect.height) { + r->height -= i; + break; + } + if(r->y + r->height + i == rect.y + rect.height) { + r->height += i; + break; + } + for(j = 0; j < num; j++) { + if(r->y + r->height - i == rects[j].y) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->height -= i; + break; + } + } + if(r->y + r->height + i == rects[j].y) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->height += i; + break; + } + } + } + } + break; + case NWEST: + case NORTH: + case NEAST: + h = r->height + r->y - py + oy; + if(h < 10) + break; + r->height = h; + r->y = py - oy; + if(h <= snaph) + break; + /* snap to border */ + for(i = 0; !pend && (i < snaph); i++) { + if(r->y - i == rect.y) { + r->y -= i; + r->height += i; + break; + } + if(r->y + i == rect.y) { + r->y += i; + r->height -= i; + break; + } + for(j = 0; j < num; j++) { + if(r->y - i == rects[j].y + rects[j].height) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->y -= i; + r->height += i; + break; + } + } + if(r->y + i == rects[j].y + rects[j].height) { + pend = check_horiz_match(r, &rects[j]); + if(pend) { + r->y += i; + r->height -= i; + break; + } + } + } + } + break; + default: + break; + } } -void mouse_resize(Frame * f, Align align) +void +mouse_resize(Frame * f, Align align) { - int px = 0, py = 0, i, ox, oy, first = 1; - Window dummy; - XEvent ev; - /* borders */ - int snapw = rect.width * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, 1000) / 1000; - int snaph = rect.height * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, 1000) / 1000; - unsigned int dmask; - unsigned int num; - XRectangle *rects = rectangles(&num); - XRectangle frect = f->rect; - XRectangle origin = frect; - - XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &ox, &oy, &dmask); - XSync(dpy, False); - XGrabServer(dpy); - while (XGrabPointer(dpy, f->win, False, ButtonMotionMask | ButtonReleaseMask, - GrabModeAsync, GrabModeAsync, None, resize_cursor, CurrentTime) != GrabSuccess) - usleep(20000); - - for (;;) { - while (!XCheckMaskEvent(dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { - usleep(20000); - continue; - } - - switch (ev.type) { - case ButtonRelease: - if (!first) { - XPoint pt; - draw_pseudo_border(&frect); - pt.x = px; - pt.y = py; - resize_frame(f, &frect, &pt); - } - XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); - XUngrabServer(dpy); - XSync(dpy, False); - return; - break; - case MotionNotify: - XTranslateCoordinates(dpy, f->win, root, ev.xmotion.x, - ev.xmotion.y, &px, &py, &dummy); - - if (first) - first = 0; - else - draw_pseudo_border(&frect); - - snap_resize(&frect, &origin, align, rects, num, px, - ox, py, oy, snapw, snaph); - draw_pseudo_border(&frect); - break; - } - } + int px = 0, py = 0, i, ox, oy, first = 1; + Window dummy; + XEvent ev; + /* borders */ + int snapw = + rect.width * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, + 1000) / 1000; + int snaph = + rect.height * blitz_strtonum(def[WM_SNAP_VALUE]->content, 0, + 1000) / 1000; + unsigned int dmask; + unsigned int num; + XRectangle *rects = rectangles(&num); + XRectangle frect = f->rect; + XRectangle origin = frect; + + XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &ox, &oy, &dmask); + XSync(dpy, False); + XGrabServer(dpy); + while(XGrabPointer + (dpy, f->win, False, ButtonMotionMask | ButtonReleaseMask, + GrabModeAsync, GrabModeAsync, None, resize_cursor, + CurrentTime) != GrabSuccess) + usleep(20000); + + for(;;) { + while(!XCheckMaskEvent + (dpy, ButtonReleaseMask | ButtonMotionMask, &ev)) { + usleep(20000); + continue; + } + + switch (ev.type) { + case ButtonRelease: + if(!first) { + XPoint pt; + draw_pseudo_border(&frect); + pt.x = px; + pt.y = py; + resize_frame(f, &frect, &pt); + } + XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); + XUngrabServer(dpy); + XSync(dpy, False); + return; + break; + case MotionNotify: + XTranslateCoordinates(dpy, f->win, root, ev.xmotion.x, + ev.xmotion.y, &px, &py, &dummy); + + if(first) + first = 0; + else + draw_pseudo_border(&frect); + + snap_resize(&frect, &origin, align, rects, num, px, + ox, py, oy, snapw, snaph); + draw_pseudo_border(&frect); + break; + } + } } -void center_pointer(Frame * f) +void +center_pointer(Frame * f) { - Window dummy; - int wex, wey, ex, ey, i; - unsigned int dmask; - if (!f) - return; - XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &wex, &wey, &dmask); - XTranslateCoordinates(dpy, f->win, root, wex, wey, &ex, &ey, &dummy); - /* suppress EnterNotify's while mouse warping */ - XSelectInput(dpy, root, ROOT_MASK & ~EnterWindowMask); - XSync(dpy, False); - XWarpPointer(dpy, None, f->win, 0, 0, 0, 0, f->rect.width / 2, f->rect.height / 2); - XSync(dpy, False); - XSelectInput(dpy, root, ROOT_MASK); + Window dummy; + int wex, wey, ex, ey, i; + unsigned int dmask; + if(!f) + return; + XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &wex, &wey, &dmask); + XTranslateCoordinates(dpy, f->win, root, wex, wey, &ex, &ey, &dummy); + /* suppress EnterNotify's while mouse warping */ + XSelectInput(dpy, root, ROOT_MASK & ~EnterWindowMask); + XSync(dpy, False); + XWarpPointer(dpy, None, f->win, 0, 0, 0, 0, f->rect.width / 2, + f->rect.height / 2); + XSync(dpy, False); + XSelectInput(dpy, root, ROOT_MASK); } diff --git a/cmd/wm/page.c b/cmd/wm/page.c @@ -9,164 +9,172 @@ #include "wm.h" -static void handle_after_write_page(IXPServer *s, File *file); +static void handle_after_write_page(IXPServer * s, File * file); static void toggle_area(void *obj, char *arg); /* action table for /?/ namespace */ Action page_acttbl[] = { - {"toggle", toggle_area}, - {0, 0} + {"toggle", toggle_area}, + {0, 0} }; -Page *alloc_page() +Page * +alloc_page() { - Page *p, *new = cext_emallocz(sizeof(Page)); - char buf[MAX_BUF], buf2[16]; - - snprintf(buf2, sizeof(buf2), "%d", npages); - snprintf(buf, sizeof(buf), "/%d", npages); - new->file[P_PREFIX] = ixp_create(ixps, buf); - snprintf(buf, sizeof(buf), "/%d/name", npages); - new->file[P_NAME] = wmii_create_ixpfile(ixps, buf, buf2); - snprintf(buf, sizeof(buf), "/%d/layout/", npages); - new->file[P_AREA_PREFIX] = ixp_create(ixps, buf); - snprintf(buf, sizeof(buf), "/%d/layout/sel", npages); - new->file[P_SEL_AREA] = ixp_create(ixps, buf); - new->file[P_SEL_AREA]->bind = 1; /* mount point */ - snprintf(buf, sizeof(buf), "/%d/ctl", npages); - new->file[P_CTL] = ixp_create(ixps, buf); - new->file[P_CTL]->after_write = handle_after_write_page; - new->floating = alloc_area(new, "float"); - new->sel = new->managed = alloc_area(new, def[WM_LAYOUT]->content); - for (p = pages; p && p->next; p = p->next); - if (!p) - pages = new; - else { - new->prev = p; - p->next = new; - } - selpage = new; - def[WM_SEL_PAGE]->content = new->file[P_PREFIX]->content; - invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); - npages++; - return new; + Page *p, *new = cext_emallocz(sizeof(Page)); + char buf[MAX_BUF], buf2[16]; + + snprintf(buf2, sizeof(buf2), "%d", npages); + snprintf(buf, sizeof(buf), "/%d", npages); + new->file[P_PREFIX] = ixp_create(ixps, buf); + snprintf(buf, sizeof(buf), "/%d/name", npages); + new->file[P_NAME] = wmii_create_ixpfile(ixps, buf, buf2); + snprintf(buf, sizeof(buf), "/%d/layout/", npages); + new->file[P_AREA_PREFIX] = ixp_create(ixps, buf); + snprintf(buf, sizeof(buf), "/%d/layout/sel", npages); + new->file[P_SEL_AREA] = ixp_create(ixps, buf); + new->file[P_SEL_AREA]->bind = 1; /* mount point */ + snprintf(buf, sizeof(buf), "/%d/ctl", npages); + new->file[P_CTL] = ixp_create(ixps, buf); + new->file[P_CTL]->after_write = handle_after_write_page; + new->floating = alloc_area(new, "float"); + new->sel = new->managed = alloc_area(new, def[WM_LAYOUT]->content); + for(p = pages; p && p->next; p = p->next); + if(!p) + pages = new; + else { + new->prev = p; + p->next = new; + } + selpage = new; + def[WM_SEL_PAGE]->content = new->file[P_PREFIX]->content; + invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); + npages++; + return new; } -void destroy_page(Page *p) +void +destroy_page(Page * p) { - destroy_area(p->floating); - destroy_area(p->managed); - def[WM_SEL_PAGE]->content = 0; - ixp_remove_file(ixps, p->file[P_PREFIX]); - if (p == selpage) { - if (p->prev) - selpage = p->prev; - else - selpage = nil; - } - - if (p == pages) { - if (p->next) - p->next->prev = nil; - pages = p->next; - } - else { - p->prev->next = p->next; - if (p->next) - p->next->prev = p->prev; - } - - free(p); - if (!selpage) - selpage = pages; - npages--; - - if (selpage) - focus_page(selpage); + destroy_area(p->floating); + destroy_area(p->managed); + def[WM_SEL_PAGE]->content = 0; + ixp_remove_file(ixps, p->file[P_PREFIX]); + if(p == selpage) { + if(p->prev) + selpage = p->prev; + else + selpage = nil; + } + + if(p == pages) { + if(p->next) + p->next->prev = nil; + pages = p->next; + } else { + p->prev->next = p->next; + if(p->next) + p->next->prev = p->prev; + } + + free(p); + if(!selpage) + selpage = pages; + npages--; + + if(selpage) + focus_page(selpage); } -void focus_page(Page *p) +void +focus_page(Page * p) { - if (selpage != p) - hide_page(selpage); - selpage = p; - show_page(p); - def[WM_SEL_PAGE]->content = p->file[P_PREFIX]->content; - invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); - focus_area(sel_area()); + if(selpage != p) + hide_page(selpage); + selpage = p; + show_page(p); + def[WM_SEL_PAGE]->content = p->file[P_PREFIX]->content; + invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); + focus_area(sel_area()); } -XRectangle *rectangles(unsigned int *num) +XRectangle * +rectangles(unsigned int *num) { - XRectangle *result = 0; - int i, j = 0; - Window d1, d2; - Window *wins; - XWindowAttributes wa; - XRectangle r; - - if (XQueryTree(dpy, root, &d1, &d2, &wins, num)) { - result = cext_emallocz(*num * sizeof(XRectangle)); - for (i = 0; i < *num; i++) { - if (!XGetWindowAttributes(dpy, wins[i], &wa)) - continue; - if (wa.override_redirect && (wa.map_state == IsViewable)) { - r.x = wa.x; - r.y = wa.y; - r.width = wa.width; - r.height = wa.height; - result[j++] = r; - } - } - } - if (wins) { - XFree(wins); - } - *num = j; - return result; + XRectangle *result = 0; + int i, j = 0; + Window d1, d2; + Window *wins; + XWindowAttributes wa; + XRectangle r; + + if(XQueryTree(dpy, root, &d1, &d2, &wins, num)) { + result = cext_emallocz(*num * sizeof(XRectangle)); + for(i = 0; i < *num; i++) { + if(!XGetWindowAttributes(dpy, wins[i], &wa)) + continue; + if(wa.override_redirect && (wa.map_state == IsViewable)) { + r.x = wa.x; + r.y = wa.y; + r.width = wa.width; + r.height = wa.height; + result[j++] = r; + } + } + } + if(wins) { + XFree(wins); + } + *num = j; + return result; } -void hide_page(Page *p) +void +hide_page(Page * p) { - hide_area(p->managed); - hide_area(p->floating); + hide_area(p->managed); + hide_area(p->floating); } -void show_page(Page *p) +void +show_page(Page * p) { - show_area(p->managed, False); - show_area(p->floating, False); + show_area(p->managed, False); + show_area(p->floating, False); } -static void handle_after_write_page(IXPServer *s, File *file) +static void +handle_after_write_page(IXPServer * s, File * file) { - Page *p; - for (p = pages; p; p = p->next) { - if (file == p->file[P_CTL]) { - run_action(file, p, page_acttbl); - return; - } - } + Page *p; + for(p = pages; p; p = p->next) { + if(file == p->file[P_CTL]) { + run_action(file, p, page_acttbl); + return; + } + } } -static void toggle_area(void *obj, char *arg) +static void +toggle_area(void *obj, char *arg) { - Page *p = obj; + Page *p = obj; - if (p->sel == p->managed) - p->sel = p->floating; - else - p->sel = p->managed; + if(p->sel == p->managed) + p->sel = p->floating; + else + p->sel = p->managed; - focus_area(p->sel); - invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); + focus_area(p->sel); + invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]); } -Page *pageat(unsigned int idx) +Page * +pageat(unsigned int idx) { - unsigned int i = 0; - Page *p; - for (p = pages; p && i != idx; p = p->next); - return p; + unsigned int i = 0; + Page *p; + for(p = pages; p && i != idx; p = p->next); + return p; } diff --git a/cmd/wm/wm.c b/cmd/wm/wm.c @@ -10,7 +10,7 @@ #include <sys/wait.h> #include <X11/cursorfont.h> #include <X11/Xproto.h> -#include <X11/keysym.h> +#include <X11/keysym.h> #include <X11/Xatom.h> #include <X11/Xproto.h> @@ -33,656 +33,740 @@ static void reserve_area(void *obj, char *arg); /* action table for /ctl namespace */ Action wm_acttbl[] = { - {"new", new_page}, - {"destroy", _destroy_page}, - {"select", _select_page}, - {"attach", _attach_client}, - {"detach", _detach_client}, - {"close", _close_client}, - {"quit", quit}, - {"pager", pager}, - {"detached_clients", detached_clients}, - {"reserve_area", reserve_area}, - {0, 0} + {"new", new_page}, + {"destroy", _destroy_page}, + {"select", _select_page}, + {"attach", _attach_client}, + {"detach", _detach_client}, + {"close", _close_client}, + {"quit", quit}, + {"pager", pager}, + {"detached_clients", detached_clients}, + {"reserve_area", reserve_area}, + {0, 0} }; char *version[] = { - "wmiiwm - window manager improved 2 - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiiwm - window manager improved 2 - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() +{ + fprintf(stderr, "%s", + "usage: wmiiwm -s <socket file> [-c] [-v]\n" + " -s socket file\n" + " -c checks if another WM is already running\n" + " -v version info\n"); + exit(1); +} + +void +run_action(File * f, void *obj, Action * acttbl) +{ + int i; + size_t len; + + if(!f->content) + return; + for(i = 0; acttbl[i].name; i++) { + len = strlen(acttbl[i].name); + if(!strncmp(acttbl[i].name, (char *) f->content, len)) { + if(f->size > len) + acttbl[i].func(obj, &((char *) f->content)[len + 1]); + else + acttbl[i].func(obj, 0); + return; + break; + } + } + fprintf(stderr, "wmiiwm: unknown action '%s'\n" + " or invalid ctl device\n", (char *) f->content); +} + +static void +quit(void *obj, char *arg) { - fprintf(stderr, "%s", - "usage: wmiiwm -s <socket file> [-c] [-v]\n" - " -s socket file\n" - " -c checks if another WM is already running\n" - " -v version info\n"); - exit(1); + ixps->runlevel = SHUTDOWN; } -void run_action(File * f, void *obj, Action * acttbl) +void +invoke_wm_event(File * f) +{ + if(!f->content) + return; + wmii_spawn(dpy, f->content); +} + +static void +scale_rect(XRectangle * from_dim, XRectangle * to_dim, + XRectangle * src, XRectangle * tgt) { - int i; - size_t len; + double wfact = (double) to_dim->width / (double) from_dim->width; + double hfact = (double) to_dim->height / (double) from_dim->height; + + tgt->x = to_dim->x + (src->x * wfact); + tgt->y = to_dim->y + (src->y * hfact); + tgt->width = (src->width * wfact); + tgt->height = (src->height * hfact); + + if(tgt->width < 1) + tgt->width = 1; + if(tgt->height < 1) + tgt->height = 1; +} + +static void +draw_pager_area(Area * a, Draw * d) +{ + Frame *f; + Frame *sel = sel_frame(); + for(f = a->layout->frames(a); f; f = f->next) { + if(f == sel) { + 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); + } else { + d->bg = + blitz_loadcolor(dpy, screen_num, + def[WM_NORM_BG_COLOR]->content); + 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 = f->sel->file[C_NAME]->content; + scale_rect(&rect, &initial_rect, &f->rect, &d->rect); + blitz_drawlabel(dpy, d); + XSync(dpy, False); /* do not clear upwards */ + } +} + +static void +draw_pager_page(Page * p, Draw * d) +{ + unsigned int idx = 0; + char name[4]; + initial_rect = d->rect; + Page *page; + + if(p == selpage) { + 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); + } else { + d->bg = + blitz_loadcolor(dpy, screen_num, + def[WM_NORM_BG_COLOR]->content); + 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); + } + for(page = pages; page && page != p; page = page->next) + idx++; + snprintf(name, sizeof(name), "%d", idx); + d->data = name; + blitz_drawlabel(dpy, d); + XSync(dpy, False); + if(p->managed) + draw_pager_area(p->managed, d); + draw_pager_area(p->floating, d); +} - if (!f->content) - return; - for (i = 0; acttbl[i].name; i++) { - len = strlen(acttbl[i].name); - if (!strncmp(acttbl[i].name, (char *) f->content, len)) { - if (f->size > len) - acttbl[i].func(obj, &((char *) f->content)[len + 1]); - else - acttbl[i].func(obj, 0); - return; - break; - } - } - fprintf(stderr, "wmiiwm: unknown action '%s'\n" - " or invalid ctl device\n", (char *) f->content); +static void +draw_pager() +{ + Draw d = { 0 }; + unsigned int ic, ir, tw, th, rows, cols; + int dx; + Page *p; + + blitz_getbasegeometry(npages, &cols, &rows); + dx = (cols - 1) * GAP; /* GAPpx space */ + tw = (rect.width - dx) / cols; + th = ((double) tw / rect.width) * rect.height; + d.drawable = transient; + d.gc = transient_gc; + d.font = font; + p = pages; + for(ir = 0; ir < rows; ir++) { + for(ic = 0; ic < cols; ic++) { + d.rect.x = ic * tw + (ic * GAP); + d.rect.width = tw; + if(rows == 1) + d.rect.y = 0; + else + d.rect.y = ir * (rect.height - th) / (rows - 1); + d.rect.height = th; + draw_pager_page(p, &d); + if(!p->next) + return; + p = p->next; + } + } +} + +static Page * +xy_to_pager_page(int x, int y) +{ + unsigned int ic, ir, tw, th, rows, cols; + int dx; + XRectangle r; + Page *p; + + blitz_getbasegeometry(npages, &cols, &rows); + dx = (cols - 1) * GAP; /* GAPpx space */ + tw = (rect.width - dx) / cols; + th = ((double) tw / rect.width) * rect.height; + + p = pages; + for(ir = 0; ir < rows; ir++) { + for(ic = 0; ic < cols; ic++) { + r.x = ic * tw + (ic * GAP); + r.width = tw; + if(rows == 1) + r.y = 0; + else + r.y = ir * (rect.height - th) / (rows - 1); + r.height = th; + if(blitz_ispointinrect(x, y, &r)) + return p; + if(!p->next) + return nil; + p = p->next; + } + } + return nil; +} + +static int +handle_kpress(XKeyEvent * e) +{ + KeySym ksym = XKeycodeToKeysym(dpy, e->keycode, 0); + + if(ksym >= XK_0 && ksym <= XK_9) + return ksym - XK_0; + else if(ksym >= XK_a && ksym <= XK_z) + return 10 + ksym - XK_a; + + return -1; +} + +static void +pager(void *obj, char *arg) +{ + XEvent ev; + int i; + + if(!pages) + return; + + XClearWindow(dpy, transient); + XMapRaised(dpy, transient); + draw_pager(); + + while(XGrabKeyboard + (dpy, transient, True, GrabModeAsync, GrabModeAsync, + CurrentTime) != GrabSuccess) + usleep(20000); + + while(XGrabPointer + (dpy, transient, False, ButtonPressMask, GrabModeAsync, + GrabModeAsync, None, normal_cursor, CurrentTime) != GrabSuccess) + usleep(20000); + + for(;;) { + while(!XCheckWindowEvent + (dpy, transient, ButtonPressMask | KeyPressMask, &ev)) { + usleep(20000); + continue; + } + + switch (ev.type) { + case KeyPress: + XUnmapWindow(dpy, transient); + if((i = handle_kpress(&ev.xkey)) != -1) + if(i < npages) + focus_page(pageat(i)); + XUngrabKeyboard(dpy, CurrentTime); + XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); + return; + break; + case ButtonPress: + XUnmapWindow(dpy, transient); + if(ev.xbutton.button == Button1) { + Page *p = xy_to_pager_page(ev.xbutton.x, ev.xbutton.y); + if(p) + focus_page(p); + } + XUngrabKeyboard(dpy, CurrentTime); + XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); + return; + break; + } + } } -static void quit(void *obj, char *arg) +static void +draw_detached_clients() +{ + unsigned int ic, ir, tw, th, rows, cols; + int dx, dy; + Client *c = detached; + XRectangle cr; + + if(!c) + return; + blitz_getbasegeometry(ndetached, &cols, &rows); + dx = (cols - 1) * GAP; /* GAPpx space */ + dy = (rows - 1) * GAP; /* GAPpx space */ + tw = (rect.width - dx) / cols; + th = (rect.height - dy) / rows; + + XClearWindow(dpy, transient); + XMapRaised(dpy, transient); + for(ir = 0; ir < rows; ir++) { + for(ic = 0; ic < cols; ic++) { + if(!c) + return; + cr.x = ic * tw + (ic * GAP); + cr.y = ir * th + (ir * GAP); + cr.width = tw; + cr.height = th; + XMoveResizeWindow(dpy, c->win, cr.x, cr.y, cr.width, + cr.height); + configure_client(c); + show_client(c); + XRaiseWindow(dpy, c->win); + grab_client(c, AnyModifier, AnyButton); + XSync(dpy, False); + c = c->next; + } + } +} + +static void +reserve_area(void *obj, char *arg) { - ixps->runlevel = SHUTDOWN; + if(arg && strrchr(arg, ',')) { + XRectangle r; + blitz_strtorect(&rect, &r, arg); + } } -void invoke_wm_event(File * f) +static void +detached_clients(void *obj, char *arg) +{ + XEvent ev; + int n; + Client *c; + + XClearWindow(dpy, transient); + XMapRaised(dpy, transient); + draw_detached_clients(); + while(XGrabKeyboard + (dpy, transient, True, GrabModeAsync, GrabModeAsync, + CurrentTime) != GrabSuccess) + usleep(20000); + + for(;;) { + while(!XCheckMaskEvent(dpy, ButtonPressMask | KeyPressMask, &ev)) { + usleep(20000); + continue; + } + switch (ev.type) { + case KeyPress: + XUnmapWindow(dpy, transient); + for(c = detached; c; c = c->next) + hide_client(c); + if((n = handle_kpress(&ev.xkey)) != -1) { + if(n - 1 < ndetached) { + c = clientat(detached, n); + detach_detached(c); + attach_client(c); + } + } + XUngrabKeyboard(dpy, CurrentTime); + return; + break; + case ButtonPress: + XUnmapWindow(dpy, transient); + for(c = detached; c; c = c->next) + hide_client(c); + if((ev.xbutton.button == Button1) + && (c = win_to_client(ev.xbutton.window))) { + detach_detached(c); + attach_client(c); + } + XUngrabKeyboard(dpy, CurrentTime); + return; + break; + } + } +} + +static void +_close_client(void *obj, char *arg) { - if (!f->content) - return; - wmii_spawn(dpy, f->content); + Client *c = sel_client(); + if(c) + close_client(c); } static void -scale_rect(XRectangle * from_dim, XRectangle * to_dim, - XRectangle * src, XRectangle * tgt) -{ - double wfact = (double) to_dim->width / (double) from_dim->width; - double hfact = (double) to_dim->height / (double) from_dim->height; - - tgt->x = to_dim->x + (src->x * wfact); - tgt->y = to_dim->y + (src->y * hfact); - tgt->width = (src->width * wfact); - tgt->height = (src->height * hfact); - - if (tgt->width < 1) - tgt->width = 1; - if (tgt->height < 1) - tgt->height = 1; -} - -static void draw_pager_area(Area *a, Draw *d) -{ - Frame *f; - Frame *sel = sel_frame(); - for (f = a->layout->frames(a); f; f = f->next) { - if (f == sel) { - 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); - } else { - d->bg = blitz_loadcolor(dpy, screen_num, def[WM_NORM_BG_COLOR]->content); - 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 = f->sel->file[C_NAME]->content; - scale_rect(&rect, &initial_rect, &f->rect, &d->rect); - blitz_drawlabel(dpy, d); - XSync(dpy, False); /* do not clear upwards */ - } -} - -static void draw_pager_page(Page *p, Draw *d) -{ - unsigned int idx = 0; - char name[4]; - initial_rect = d->rect; - Page *page; - - if (p == selpage) { - 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); - } else { - d->bg = blitz_loadcolor(dpy, screen_num, def[WM_NORM_BG_COLOR]->content); - 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); - } - for (page = pages; page && page != p; page = page->next) idx++; - snprintf(name, sizeof(name), "%d", idx); - d->data = name; - blitz_drawlabel(dpy, d); - XSync(dpy, False); - if (p->managed) - draw_pager_area(p->managed, d); - draw_pager_area(p->floating, d); -} - -static void draw_pager() -{ - Draw d = { 0 }; - unsigned int ic, ir, tw, th, rows, cols; - int dx; - Page *p; - - blitz_getbasegeometry(npages, &cols, &rows); - dx = (cols - 1) * GAP; /* GAPpx space */ - tw = (rect.width - dx) / cols; - th = ((double) tw / rect.width) * rect.height; - d.drawable = transient; - d.gc = transient_gc; - d.font = font; - p = pages; - for (ir = 0; ir < rows; ir++) { - for (ic = 0; ic < cols; ic++) { - d.rect.x = ic * tw + (ic * GAP); - d.rect.width = tw; - if (rows == 1) - d.rect.y = 0; - else - d.rect.y = ir * (rect.height - th) / (rows - 1); - d.rect.height = th; - draw_pager_page(p, &d); - if (!p->next) - return; - p = p->next; - } - } -} - -static Page *xy_to_pager_page(int x, int y) -{ - unsigned int ic, ir, tw, th, rows, cols; - int dx; - XRectangle r; - Page *p; - - blitz_getbasegeometry(npages, &cols, &rows); - dx = (cols - 1) * GAP; /* GAPpx space */ - tw = (rect.width - dx) / cols; - th = ((double) tw / rect.width) * rect.height; - - p = pages; - for (ir = 0; ir < rows; ir++) { - for (ic = 0; ic < cols; ic++) { - r.x = ic * tw + (ic * GAP); - r.width = tw; - if (rows == 1) - r.y = 0; - else - r.y = ir * (rect.height - th) / (rows - 1); - r.height = th; - if (blitz_ispointinrect(x, y, &r)) - return p; - if (!p->next) - return nil; - p = p->next; - } - } - return nil; -} - -static int handle_kpress(XKeyEvent * e) -{ - KeySym ksym = XKeycodeToKeysym(dpy, e->keycode, 0); - - if (ksym >= XK_0 && ksym <= XK_9) - return ksym - XK_0; - else if (ksym >= XK_a && ksym <= XK_z) - return 10 + ksym - XK_a; - - return -1; -} - -static void pager(void *obj, char *arg) -{ - XEvent ev; - int i; - - if (!pages) - return; - - XClearWindow(dpy, transient); - XMapRaised(dpy, transient); - draw_pager(); - - while (XGrabKeyboard(dpy, transient, True, GrabModeAsync, GrabModeAsync, CurrentTime) != GrabSuccess) - usleep(20000); - - while (XGrabPointer(dpy, transient, False, ButtonPressMask, GrabModeAsync, - GrabModeAsync, None, normal_cursor, CurrentTime) != GrabSuccess) - usleep(20000); - - for (;;) { - while (!XCheckWindowEvent(dpy, transient, ButtonPressMask | KeyPressMask, &ev)) { - usleep(20000); - continue; - } - - switch (ev.type) { - case KeyPress: - XUnmapWindow(dpy, transient); - if ((i = handle_kpress(&ev.xkey)) != -1) - if (i < npages) - focus_page(pageat(i)); - XUngrabKeyboard(dpy, CurrentTime); - XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); - return; - break; - case ButtonPress: - XUnmapWindow(dpy, transient); - if (ev.xbutton.button == Button1) { - Page *p = xy_to_pager_page(ev.xbutton.x, ev.xbutton.y); - if (p) - focus_page(p); - } - XUngrabKeyboard(dpy, CurrentTime); - XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ ); - return; - break; - } - } -} - -static void draw_detached_clients() -{ - unsigned int ic, ir, tw, th, rows, cols; - int dx, dy; - Client *c = detached; - XRectangle cr; - - if (!c) - return; - blitz_getbasegeometry(ndetached, &cols, &rows); - dx = (cols - 1) * GAP; /* GAPpx space */ - dy = (rows - 1) * GAP; /* GAPpx space */ - tw = (rect.width - dx) / cols; - th = (rect.height - dy) / rows; - - XClearWindow(dpy, transient); - XMapRaised(dpy, transient); - for (ir = 0; ir < rows; ir++) { - for (ic = 0; ic < cols; ic++) { - if (!c) - return; - cr.x = ic * tw + (ic * GAP); - cr.y = ir * th + (ir * GAP); - cr.width = tw; - cr.height = th; - XMoveResizeWindow(dpy, c->win, cr.x, cr.y, cr.width, cr.height); - configure_client(c); - show_client(c); - XRaiseWindow(dpy, c->win); - grab_client(c, AnyModifier, AnyButton); - XSync(dpy, False); - c = c->next; - } - } -} - -static void reserve_area(void *obj, char *arg) -{ - if (arg && strrchr(arg, ',')) { - XRectangle r; - blitz_strtorect(&rect, &r, arg); - } -} - -static void detached_clients(void *obj, char *arg) -{ - XEvent ev; - int n; - Client *c; - - XClearWindow(dpy, transient); - XMapRaised(dpy, transient); - draw_detached_clients(); - while (XGrabKeyboard(dpy, transient, True, GrabModeAsync, GrabModeAsync, CurrentTime) != GrabSuccess) - usleep(20000); - - for (;;) { - while (!XCheckMaskEvent(dpy, ButtonPressMask | KeyPressMask, &ev)) { - usleep(20000); - continue; - } - switch (ev.type) { - case KeyPress: - XUnmapWindow(dpy, transient); - for (c = detached; c; c = c->next) - hide_client(c); - if ((n = handle_kpress(&ev.xkey)) != -1) { - if (n - 1 < ndetached) { - c = clientat(detached, n); - detach_detached(c); - attach_client(c); - } - } - XUngrabKeyboard(dpy, CurrentTime); - return; - break; - case ButtonPress: - XUnmapWindow(dpy, transient); - for (c = detached; c; c = c->next) - hide_client(c); - if ((ev.xbutton.button == Button1) && (c = win_to_client(ev.xbutton.window))) { - detach_detached(c); - attach_client(c); - } - XUngrabKeyboard(dpy, CurrentTime); - return; - break; - } - } -} - -static void _close_client(void *obj, char *arg) -{ - Client *c = sel_client(); - if (c) - close_client(c); -} - -static void _attach_client(void *obj, char *arg) -{ - Client *c = detached; - if (c) { - detach_detached(c); - attach_client(c); - } -} - -static void _detach_client(void *obj, char *arg) -{ - Client *c = sel_client(); - if (c) - detach_client(c, False); -} - -static void _select_page(void *obj, char *arg) -{ - Page *p = selpage; - if (!p || !arg) - return; - if (!strncmp(arg, "prev", 5)) { - if (p->prev) - p = p->prev; - else - for (p = pages; p && p->next; p = p->next); - } - else if (!strncmp(arg, "next", 5)) { - if (p->next) - p = p->next; - else - p = pages; - } - else - p = pageat(blitz_strtonum(arg, 0, npages - 1)); - if (p) - focus_page(p); -} - -static void _destroy_page(void *obj, char *arg) -{ - Page *p = selpage; - if (p) - destroy_page(p); - if (selpage) - focus_page(selpage); -} - -static void new_page(void *obj, char *arg) -{ - Page *p = selpage; - if (p) - hide_page(p); - selpage = alloc_page(); -} - -Client *win_to_client(Window w) -{ - Page *p; - Client *c; - for (c = detached; c; c = c->next) - if (c->win == w) - return c; - for (p = pages; p; p = p->next) { - Frame *f; - for (f = p->managed->layout->frames(p->managed); f; f = f->next) { - for (c = f->clients; c; c = c->next) - if (c->win == w) - return c; - } - for (f = p->floating->layout->frames(p->floating); f; f = f->next) { - for (c = f->clients; c; c = c->next) - if (c->win == w) - return c; - } - } - return nil; -} - -void scan_wins() -{ - int i; - unsigned int num; - Window *wins; - XWindowAttributes wa; - Window d1, d2; - Client *c; - - if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { - for (i = 0; i < num; i++) { - if (!XGetWindowAttributes(dpy, wins[i], &wa)) - continue; - if (wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) - continue; - if (wa.map_state == IsViewable) { - c = alloc_client(wins[i]); - init_client(c, &wa); - attach_client(c); - c->ignore_unmap++; /* was viewable already */ - } - } - } - if (wins) - XFree(wins); -} - -void *get_func(void *acttbl[][2], int rows, char *fname) -{ - int i; - for (i = 0; i < rows; i++) { - if (!strncmp((char *) acttbl[i][0], fname, MAX_BUF)) { - return acttbl[i][1]; - } - } - return 0; -} - -int win_proto(Window w) -{ - Atom *protocols; - long res; - int protos = 0; - int i; - - res = wmii_property(dpy, w, wm_protocols, XA_ATOM, 20L, ((unsigned char **) &protocols)); - if (res <= 0) { - return protos; - } - for (i = 0; i < res; i++) { - if (protocols[i] == wm_delete) { - protos |= PROTO_DEL; - } - } - free((char *) protocols); - return protos; -} - -int win_state(Window w) -{ - /* state hints */ - XWMHints *hints = XGetWMHints(dpy, w); - int res; - - long *prop = 0; - if (wmii_property(dpy, w, wm_state, wm_state, 2L, ((unsigned char **) &prop)) > 0) { - res = (int) *prop; - free((long *) prop); - } else { - res = hints ? hints->initial_state : NormalState; - } - - if (hints) { - free(hints); - } - return res; -} - -static void update_pages() -{ - Page *p; - - for (p = pages; p; p = p->next) { - p->floating->layout->arrange(p->floating); - p->managed->layout->arrange(p->managed); - } -} - -void handle_before_read(IXPServer *s, File *f) -{ - char buf[64]; - if (f == def[WM_AREA_GEOMETRY]) { - snprintf(buf, 64, "%d,%d,%d,%d", area_rect.x, area_rect.y, area_rect.width, area_rect.height); - if (f->content) - free(f->content); - f->content = cext_estrdup(buf); - f->size = strlen(buf); - } -} - -void handle_after_write(IXPServer * s, File * f) -{ - if (f == def[WM_CTL]) - run_action(f, 0, wm_acttbl); - else if (f == def[WM_TRANS_COLOR]) { - unsigned long col[1]; - col[0] = xorcolor.pixel; - XFreeColors(dpy, DefaultColormap(dpy, screen_num), col, 1, 0); - XAllocNamedColor(dpy, DefaultColormap(dpy, screen_num), - def[WM_TRANS_COLOR]->content, - &xorcolor, &xorcolor); - XSetForeground(dpy, xorgc, xorcolor.pixel); - } - else if (f == def[WM_FONT]) { - XFreeFont(dpy, font); - font = blitz_getfont(dpy, def[WM_FONT]->content); - } - else if (f == def[WM_AREA_GEOMETRY]) { - char *geom = def[WM_AREA_GEOMETRY]->content; - if (geom && strrchr(geom, ',')) { - area_rect = rect; - blitz_strtorect(&rect, &area_rect, geom); - update_pages(); - } - } - check_event(0); -} - -static void init_atoms() -{ - wm_state = XInternAtom(dpy, "WM_STATE", False); - wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False); - wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False); - wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False); - motif_wm_hints = XInternAtom(dpy, "_MOTIF_WM_HINTS", False); - net_wm_desktop = XInternAtom(dpy, "_NET_WM_DESKTOP", False); -} - -static void init_cursors() -{ - normal_cursor = XCreateFontCursor(dpy, XC_left_ptr); - resize_cursor = XCreateFontCursor(dpy, XC_sizing); - move_cursor = XCreateFontCursor(dpy, XC_fleur); - drag_cursor = XCreateFontCursor(dpy, XC_cross); - w_cursor = XCreateFontCursor(dpy, XC_left_side); - e_cursor = XCreateFontCursor(dpy, XC_right_side); - n_cursor = XCreateFontCursor(dpy, XC_top_side); - s_cursor = XCreateFontCursor(dpy, XC_bottom_side); - nw_cursor = XCreateFontCursor(dpy, XC_top_left_corner); - ne_cursor = XCreateFontCursor(dpy, XC_top_right_corner); - sw_cursor = XCreateFontCursor(dpy, XC_bottom_left_corner); - se_cursor = XCreateFontCursor(dpy, XC_bottom_right_corner); -} - -static void init_default() -{ - def[WM_DETACHED_FRAME] = ixp_create(ixps, "/detached/frame"); - def[WM_DETACHED_CLIENT] = ixp_create(ixps, "/detached/client"); - def[WM_TRANS_COLOR] = wmii_create_ixpfile(ixps, "/default/transcolor", BLITZ_SEL_FG_COLOR); - def[WM_TRANS_COLOR]->after_write = handle_after_write; - def[WM_AREA_GEOMETRY] = wmii_create_ixpfile(ixps, "/default/area/geometry", BLITZ_SEL_FG_COLOR); - def[WM_AREA_GEOMETRY]->after_write = handle_after_write; - def[WM_AREA_GEOMETRY]->before_read = handle_before_read; - def[WM_SEL_BG_COLOR] = wmii_create_ixpfile(ixps, "/default/sstyle/bgcolor", BLITZ_SEL_BG_COLOR); - def[WM_SEL_FG_COLOR] = wmii_create_ixpfile(ixps, "/default/sstyle/fgcolor", BLITZ_SEL_FG_COLOR); - def[WM_SEL_BORDER_COLOR] = wmii_create_ixpfile(ixps, "/default/sstyle/bordercolor", BLITZ_SEL_BORDER_COLOR); - def[WM_NORM_BG_COLOR] = wmii_create_ixpfile(ixps, "/default/nstyle/bgcolor", BLITZ_NORM_BG_COLOR); - def[WM_NORM_FG_COLOR] = wmii_create_ixpfile(ixps, "/default/nstyle/fgcolor", BLITZ_NORM_FG_COLOR); - def[WM_NORM_BORDER_COLOR] = wmii_create_ixpfile(ixps, "/default/nstyle/bordercolor", BLITZ_NORM_BORDER_COLOR); - def[WM_FONT] = wmii_create_ixpfile(ixps, "/default/font", BLITZ_FONT); - def[WM_FONT]->after_write = handle_after_write; - def[WM_SNAP_VALUE] = wmii_create_ixpfile(ixps, "/default/snapvalue", "20"); /* 0..1000 */ - def[WM_BORDER] = wmii_create_ixpfile(ixps, "/default/border", "1"); - def[WM_TAB] = wmii_create_ixpfile(ixps, "/default/tab", "1"); - def[WM_HANDLE_INC] = wmii_create_ixpfile(ixps, "/default/handleinc", "1"); - def[WM_LOCKED] = wmii_create_ixpfile(ixps, "/default/locked", "1"); - def[WM_LAYOUT] = wmii_create_ixpfile(ixps, "/default/layout", LAYOUT); - def[WM_SEL_PAGE] = ixp_create(ixps, "/sel"); - def[WM_EVENT_PAGE_UPDATE] = ixp_create(ixps, "/event/pageupdate"); - def[WM_EVENT_CLIENT_UPDATE] = ixp_create(ixps, "/event/clientupdate"); - def[WM_EVENT_B1PRESS] = ixp_create(ixps, "/default/event/b1press"); - def[WM_EVENT_B2PRESS] = ixp_create(ixps, "/default/event/b2press"); - def[WM_EVENT_B3PRESS] = ixp_create(ixps, "/default/event/b3press"); - def[WM_EVENT_B4PRESS] = ixp_create(ixps, "/default/event/b4press"); - def[WM_EVENT_B5PRESS] = ixp_create(ixps, "/default/event/b5press"); -} - -static void init_screen() -{ - XGCValues gcv; - XSetWindowAttributes wa; - - XAllocNamedColor(dpy, DefaultColormap(dpy, screen_num), - def[WM_TRANS_COLOR]->content, - &xorcolor, &xorcolor); - gcv.subwindow_mode = IncludeInferiors; - gcv.function = GXxor; - gcv.foreground = xorcolor.pixel; - gcv.line_width = 4; - gcv.plane_mask = AllPlanes; - gcv.graphics_exposures = False; - xorgc = XCreateGC(dpy, root, GCForeground | GCGraphicsExposures - | GCFunction | GCSubwindowMode | GCLineWidth - | GCPlaneMask, &gcv); - rect.x = rect.y = 0; - rect.width = DisplayWidth(dpy, screen_num); - rect.height = DisplayHeight(dpy, screen_num); - area_rect = rect; - - wa.override_redirect = 1; - wa.background_pixmap = ParentRelative; - wa.event_mask = ExposureMask | ButtonPressMask; - transient = XCreateWindow(dpy, root, 0, 0, rect.width, rect.height, 0, DefaultDepth(dpy, screen_num), - CopyFromParent, DefaultVisual(dpy, screen_num), - CWOverrideRedirect | CWBackPixmap | CWEventMask , &wa); - - XSync(dpy, False); - transient_gc = XCreateGC(dpy, transient, 0, 0); - XDefineCursor(dpy, transient, normal_cursor); - XDefineCursor(dpy, root, normal_cursor); +_attach_client(void *obj, char *arg) +{ + Client *c = detached; + if(c) { + detach_detached(c); + attach_client(c); + } +} + +static void +_detach_client(void *obj, char *arg) +{ + Client *c = sel_client(); + if(c) + detach_client(c, False); +} + +static void +_select_page(void *obj, char *arg) +{ + Page *p = selpage; + if(!p || !arg) + return; + if(!strncmp(arg, "prev", 5)) { + if(p->prev) + p = p->prev; + else + for(p = pages; p && p->next; p = p->next); + } else if(!strncmp(arg, "next", 5)) { + if(p->next) + p = p->next; + else + p = pages; + } else + p = pageat(blitz_strtonum(arg, 0, npages - 1)); + if(p) + focus_page(p); +} + +static void +_destroy_page(void *obj, char *arg) +{ + Page *p = selpage; + if(p) + destroy_page(p); + if(selpage) + focus_page(selpage); +} + +static void +new_page(void *obj, char *arg) +{ + Page *p = selpage; + if(p) + hide_page(p); + selpage = alloc_page(); +} + +Client * +win_to_client(Window w) +{ + Page *p; + Client *c; + for(c = detached; c; c = c->next) + if(c->win == w) + return c; + for(p = pages; p; p = p->next) { + Frame *f; + for(f = p->managed->layout->frames(p->managed); f; f = f->next) { + for(c = f->clients; c; c = c->next) + if(c->win == w) + return c; + } + for(f = p->floating->layout->frames(p->floating); f; f = f->next) { + for(c = f->clients; c; c = c->next) + if(c->win == w) + return c; + } + } + return nil; +} + +void +scan_wins() +{ + int i; + unsigned int num; + Window *wins; + XWindowAttributes wa; + Window d1, d2; + Client *c; + + if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { + for(i = 0; i < num; i++) { + if(!XGetWindowAttributes(dpy, wins[i], &wa)) + continue; + if(wa.override_redirect + || XGetTransientForHint(dpy, wins[i], &d1)) + continue; + if(wa.map_state == IsViewable) { + c = alloc_client(wins[i]); + init_client(c, &wa); + attach_client(c); + c->ignore_unmap++; /* was viewable already */ + } + } + } + if(wins) + XFree(wins); +} + +void * +get_func(void *acttbl[][2], int rows, char *fname) +{ + int i; + for(i = 0; i < rows; i++) { + if(!strncmp((char *) acttbl[i][0], fname, MAX_BUF)) { + return acttbl[i][1]; + } + } + return 0; +} + +int +win_proto(Window w) +{ + Atom *protocols; + long res; + int protos = 0; + int i; + + res = + wmii_property(dpy, w, wm_protocols, XA_ATOM, 20L, + ((unsigned char **) &protocols)); + if(res <= 0) { + return protos; + } + for(i = 0; i < res; i++) { + if(protocols[i] == wm_delete) { + protos |= PROTO_DEL; + } + } + free((char *) protocols); + return protos; +} + +int +win_state(Window w) +{ + /* state hints */ + XWMHints *hints = XGetWMHints(dpy, w); + int res; + + long *prop = 0; + if(wmii_property + (dpy, w, wm_state, wm_state, 2L, ((unsigned char **) &prop)) > 0) { + res = (int) *prop; + free((long *) prop); + } else { + res = hints ? hints->initial_state : NormalState; + } + + if(hints) { + free(hints); + } + return res; +} + +static void +update_pages() +{ + Page *p; + + for(p = pages; p; p = p->next) { + p->floating->layout->arrange(p->floating); + p->managed->layout->arrange(p->managed); + } +} + +void +handle_before_read(IXPServer * s, File * f) +{ + char buf[64]; + if(f == def[WM_AREA_GEOMETRY]) { + snprintf(buf, 64, "%d,%d,%d,%d", area_rect.x, area_rect.y, + area_rect.width, area_rect.height); + if(f->content) + free(f->content); + f->content = cext_estrdup(buf); + f->size = strlen(buf); + } +} + +void +handle_after_write(IXPServer * s, File * f) +{ + if(f == def[WM_CTL]) + run_action(f, 0, wm_acttbl); + else if(f == def[WM_TRANS_COLOR]) { + unsigned long col[1]; + col[0] = xorcolor.pixel; + XFreeColors(dpy, DefaultColormap(dpy, screen_num), col, 1, 0); + XAllocNamedColor(dpy, DefaultColormap(dpy, screen_num), + def[WM_TRANS_COLOR]->content, + &xorcolor, &xorcolor); + XSetForeground(dpy, xorgc, xorcolor.pixel); + } else if(f == def[WM_FONT]) { + XFreeFont(dpy, font); + font = blitz_getfont(dpy, def[WM_FONT]->content); + } else if(f == def[WM_AREA_GEOMETRY]) { + char *geom = def[WM_AREA_GEOMETRY]->content; + if(geom && strrchr(geom, ',')) { + area_rect = rect; + blitz_strtorect(&rect, &area_rect, geom); + update_pages(); + } + } + check_event(0); +} + +static void +init_atoms() +{ + wm_state = XInternAtom(dpy, "WM_STATE", False); + wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False); + wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False); + wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False); + motif_wm_hints = XInternAtom(dpy, "_MOTIF_WM_HINTS", False); + net_wm_desktop = XInternAtom(dpy, "_NET_WM_DESKTOP", False); +} + +static void +init_cursors() +{ + normal_cursor = XCreateFontCursor(dpy, XC_left_ptr); + resize_cursor = XCreateFontCursor(dpy, XC_sizing); + move_cursor = XCreateFontCursor(dpy, XC_fleur); + drag_cursor = XCreateFontCursor(dpy, XC_cross); + w_cursor = XCreateFontCursor(dpy, XC_left_side); + e_cursor = XCreateFontCursor(dpy, XC_right_side); + n_cursor = XCreateFontCursor(dpy, XC_top_side); + s_cursor = XCreateFontCursor(dpy, XC_bottom_side); + nw_cursor = XCreateFontCursor(dpy, XC_top_left_corner); + ne_cursor = XCreateFontCursor(dpy, XC_top_right_corner); + sw_cursor = XCreateFontCursor(dpy, XC_bottom_left_corner); + se_cursor = XCreateFontCursor(dpy, XC_bottom_right_corner); +} + +static void +init_default() +{ + def[WM_DETACHED_FRAME] = ixp_create(ixps, "/detached/frame"); + def[WM_DETACHED_CLIENT] = ixp_create(ixps, "/detached/client"); + def[WM_TRANS_COLOR] = + wmii_create_ixpfile(ixps, "/default/transcolor", + BLITZ_SEL_FG_COLOR); + def[WM_TRANS_COLOR]->after_write = handle_after_write; + def[WM_AREA_GEOMETRY] = + wmii_create_ixpfile(ixps, "/default/area/geometry", + BLITZ_SEL_FG_COLOR); + def[WM_AREA_GEOMETRY]->after_write = handle_after_write; + def[WM_AREA_GEOMETRY]->before_read = handle_before_read; + def[WM_SEL_BG_COLOR] = + wmii_create_ixpfile(ixps, "/default/sstyle/bgcolor", + BLITZ_SEL_BG_COLOR); + def[WM_SEL_FG_COLOR] = + wmii_create_ixpfile(ixps, "/default/sstyle/fgcolor", + BLITZ_SEL_FG_COLOR); + def[WM_SEL_BORDER_COLOR] = + wmii_create_ixpfile(ixps, "/default/sstyle/bordercolor", + BLITZ_SEL_BORDER_COLOR); + def[WM_NORM_BG_COLOR] = + wmii_create_ixpfile(ixps, "/default/nstyle/bgcolor", + BLITZ_NORM_BG_COLOR); + def[WM_NORM_FG_COLOR] = + wmii_create_ixpfile(ixps, "/default/nstyle/fgcolor", + BLITZ_NORM_FG_COLOR); + def[WM_NORM_BORDER_COLOR] = + wmii_create_ixpfile(ixps, "/default/nstyle/bordercolor", + BLITZ_NORM_BORDER_COLOR); + def[WM_FONT] = wmii_create_ixpfile(ixps, "/default/font", BLITZ_FONT); + def[WM_FONT]->after_write = handle_after_write; + def[WM_SNAP_VALUE] = wmii_create_ixpfile(ixps, "/default/snapvalue", "20"); /* 0..1000 */ + def[WM_BORDER] = wmii_create_ixpfile(ixps, "/default/border", "1"); + def[WM_TAB] = wmii_create_ixpfile(ixps, "/default/tab", "1"); + def[WM_HANDLE_INC] = + wmii_create_ixpfile(ixps, "/default/handleinc", "1"); + def[WM_LOCKED] = wmii_create_ixpfile(ixps, "/default/locked", "1"); + def[WM_LAYOUT] = wmii_create_ixpfile(ixps, "/default/layout", LAYOUT); + def[WM_SEL_PAGE] = ixp_create(ixps, "/sel"); + def[WM_EVENT_PAGE_UPDATE] = ixp_create(ixps, "/event/pageupdate"); + def[WM_EVENT_CLIENT_UPDATE] = ixp_create(ixps, "/event/clientupdate"); + def[WM_EVENT_B1PRESS] = ixp_create(ixps, "/default/event/b1press"); + def[WM_EVENT_B2PRESS] = ixp_create(ixps, "/default/event/b2press"); + def[WM_EVENT_B3PRESS] = ixp_create(ixps, "/default/event/b3press"); + def[WM_EVENT_B4PRESS] = ixp_create(ixps, "/default/event/b4press"); + def[WM_EVENT_B5PRESS] = ixp_create(ixps, "/default/event/b5press"); +} + +static void +init_screen() +{ + XGCValues gcv; + XSetWindowAttributes wa; + + XAllocNamedColor(dpy, DefaultColormap(dpy, screen_num), + def[WM_TRANS_COLOR]->content, &xorcolor, &xorcolor); + gcv.subwindow_mode = IncludeInferiors; + gcv.function = GXxor; + gcv.foreground = xorcolor.pixel; + gcv.line_width = 4; + gcv.plane_mask = AllPlanes; + gcv.graphics_exposures = False; + xorgc = XCreateGC(dpy, root, GCForeground | GCGraphicsExposures + | GCFunction | GCSubwindowMode | GCLineWidth + | GCPlaneMask, &gcv); + rect.x = rect.y = 0; + rect.width = DisplayWidth(dpy, screen_num); + rect.height = DisplayHeight(dpy, screen_num); + area_rect = rect; + + wa.override_redirect = 1; + wa.background_pixmap = ParentRelative; + wa.event_mask = ExposureMask | ButtonPressMask; + transient = + XCreateWindow(dpy, root, 0, 0, rect.width, rect.height, 0, + DefaultDepth(dpy, screen_num), CopyFromParent, + DefaultVisual(dpy, screen_num), + CWOverrideRedirect | CWBackPixmap | CWEventMask, + &wa); + + XSync(dpy, False); + transient_gc = XCreateGC(dpy, transient, 0, 0); + XDefineCursor(dpy, transient, normal_cursor); + XDefineCursor(dpy, root, normal_cursor); } /* @@ -691,132 +775,142 @@ static void init_screen() * Other types of errors call Xlib's default error handler, which * calls exit(). */ -static int wmii_error_handler(Display * dpy, XErrorEvent * error) -{ - if (error->error_code == BadWindow - || (error->request_code == X_SetInputFocus && error->error_code == BadMatch) - || (error->request_code == X_PolyText8 && error->error_code == BadDrawable) - || (error->request_code == X_PolyFillRectangle && error->error_code == BadDrawable) - || (error->request_code == X_PolySegment && error->error_code == BadDrawable) - || (error->request_code == X_ConfigureWindow && error->error_code == BadMatch)) - return 0; - fprintf(stderr, "%s", "wmiiwm: fatal error"); - return x_error_handler(dpy, error); /* calls exit() */ +static int +wmii_error_handler(Display * dpy, XErrorEvent * error) +{ + if(error->error_code == BadWindow + || (error->request_code == X_SetInputFocus + && error->error_code == BadMatch) + || (error->request_code == X_PolyText8 + && error->error_code == BadDrawable) + || (error->request_code == X_PolyFillRectangle + && error->error_code == BadDrawable) + || (error->request_code == X_PolySegment + && error->error_code == BadDrawable) + || (error->request_code == X_ConfigureWindow + && error->error_code == BadMatch)) + return 0; + fprintf(stderr, "%s", "wmiiwm: fatal error"); + return x_error_handler(dpy, error); /* calls exit() */ } /* * Startup Error handler to check if another window manager * is already running. */ -static int startup_error_handler(Display * dpy, XErrorEvent * error) -{ - other_wm_running = 1; - return -1; -} - -static void cleanup() -{ - Page *p; - Client *c; - for (p = pages; p; p = p->next) { - Frame *f; - for (f = p->managed->layout->frames(p->managed); f; f = f->next) { - while ((c = f->clients)) - detach_client_from_frame(c, False); - } - for (f = p->floating->layout->frames(p->floating); f; f = f->next) { - while ((c = f->clients)) - detach_client_from_frame(c, False); - } - } - XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); -} - -int main(int argc, char *argv[]) -{ - int i; - int checkwm = 0; - char *sockfile = 0; - - /* command line args */ - if (argc > 1) { - for (i = 1; (i < argc) && (argv[i][0] == '-'); i++) { - switch (argv[i][1]) { - case 'v': - fprintf(stdout, "%s", version[0]); - exit(0); - break; - case 'c': - checkwm = 1; - break; - case 's': - if (i + 1 < argc) - sockfile = argv[++i]; - else - usage(); - break; - default: - usage(); - break; - } - } - } - dpy = XOpenDisplay(0); - if (!dpy) { - fprintf(stderr, "%s", "wmiiwm: cannot open display\n"); - exit(1); - } - screen_num = DefaultScreen(dpy); - root = RootWindow(dpy, screen_num); - - /* check if another WM is already running */ - other_wm_running = 0; - XSetErrorHandler(startup_error_handler); - /* this causes an error if some other WM is running */ - XSelectInput(dpy, root, ROOT_MASK); - XSync(dpy, False); - - if (other_wm_running) { - fprintf(stderr, - "wmiiwm: another window manager is already running\n"); - exit(1); - } - if (checkwm) { - XCloseDisplay(dpy); - exit(0); - } - XSetErrorHandler(0); - x_error_handler = XSetErrorHandler(wmii_error_handler); - - ixps = wmii_setup_server(sockfile); - - init_event_hander(); - - if (!(def[WM_CTL] = ixp_create(ixps, "/ctl"))) { - perror("wmiiwm: cannot connect IXP server"); - exit(1); - } - def[WM_CTL]->after_write = handle_after_write; - - pages = selpage = nil; - ndetached = npages = 0; - detached = nil; - layouts = nil; - - init_atoms(); - init_cursors(); - init_default(); - font = blitz_getfont(dpy, def[WM_FONT]->content); - wmii_init_lock_modifiers(dpy, &valid_mask, &num_lock_mask); - init_screen(); - init_layouts(); - scan_wins(); - - /* main event loop */ - run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, 0); - cleanup(); +static int +startup_error_handler(Display * dpy, XErrorEvent * error) +{ + other_wm_running = 1; + return -1; +} + +static void +cleanup() +{ + Page *p; + Client *c; + for(p = pages; p; p = p->next) { + Frame *f; + for(f = p->managed->layout->frames(p->managed); f; f = f->next) { + while((c = f->clients)) + detach_client_from_frame(c, False); + } + for(f = p->floating->layout->frames(p->floating); f; f = f->next) { + while((c = f->clients)) + detach_client_from_frame(c, False); + } + } + XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); +} + +int +main(int argc, char *argv[]) +{ + int i; + int checkwm = 0; + char *sockfile = 0; + + /* command line args */ + if(argc > 1) { + for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { + switch (argv[i][1]) { + case 'v': + fprintf(stdout, "%s", version[0]); + exit(0); + break; + case 'c': + checkwm = 1; + break; + case 's': + if(i + 1 < argc) + sockfile = argv[++i]; + else + usage(); + break; + default: + usage(); + break; + } + } + } + dpy = XOpenDisplay(0); + if(!dpy) { + fprintf(stderr, "%s", "wmiiwm: cannot open display\n"); + exit(1); + } + screen_num = DefaultScreen(dpy); + root = RootWindow(dpy, screen_num); + + /* check if another WM is already running */ + other_wm_running = 0; + XSetErrorHandler(startup_error_handler); + /* this causes an error if some other WM is running */ + XSelectInput(dpy, root, ROOT_MASK); + XSync(dpy, False); + + if(other_wm_running) { + fprintf(stderr, + "wmiiwm: another window manager is already running\n"); + exit(1); + } + if(checkwm) { + XCloseDisplay(dpy); + exit(0); + } + XSetErrorHandler(0); + x_error_handler = XSetErrorHandler(wmii_error_handler); + + ixps = wmii_setup_server(sockfile); + + init_event_hander(); + + if(!(def[WM_CTL] = ixp_create(ixps, "/ctl"))) { + perror("wmiiwm: cannot connect IXP server"); + exit(1); + } + def[WM_CTL]->after_write = handle_after_write; + + pages = selpage = nil; + ndetached = npages = 0; + detached = nil; + layouts = nil; + + init_atoms(); + init_cursors(); + init_default(); + font = blitz_getfont(dpy, def[WM_FONT]->content); + wmii_init_lock_modifiers(dpy, &valid_mask, &num_lock_mask); + init_screen(); + init_layouts(); + scan_wins(); + + /* main event loop */ + run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, + 0); + cleanup(); /* deinit_server(ixps);*/ - XCloseDisplay(dpy); + XCloseDisplay(dpy); - return 0; + return 0; } diff --git a/cmd/wm/wm.h b/cmd/wm/wm.h @@ -10,84 +10,84 @@ /* array indexes of page file pointers */ enum { - P_PREFIX, - P_NAME, - P_AREA_PREFIX, - P_SEL_AREA, - P_CTL, - P_LAST + P_PREFIX, + P_NAME, + P_AREA_PREFIX, + P_SEL_AREA, + P_CTL, + P_LAST }; /* array indexes of area file pointers */ enum { - A_PREFIX, - A_FRAME_PREFIX, - A_SEL_FRAME, - A_CTL, - A_LAYOUT, - A_LAST + A_PREFIX, + A_FRAME_PREFIX, + A_SEL_FRAME, + A_CTL, + A_LAYOUT, + A_LAST }; /* array indexes of frame file pointers */ enum { - F_PREFIX, - F_CLIENT_PREFIX, - F_SEL_CLIENT, - F_CTL, - F_GEOMETRY, - F_BORDER, - F_TAB, - F_HANDLE_INC, - F_LOCKED, - F_SEL_BG_COLOR, - F_SEL_FG_COLOR, - F_SEL_BORDER_COLOR, - F_NORM_BG_COLOR, - F_NORM_FG_COLOR, - F_NORM_BORDER_COLOR, - F_EVENT_B2PRESS, - F_EVENT_B3PRESS, - F_EVENT_B4PRESS, - F_EVENT_B5PRESS, - F_LAST + F_PREFIX, + F_CLIENT_PREFIX, + F_SEL_CLIENT, + F_CTL, + F_GEOMETRY, + F_BORDER, + F_TAB, + F_HANDLE_INC, + F_LOCKED, + F_SEL_BG_COLOR, + F_SEL_FG_COLOR, + F_SEL_BORDER_COLOR, + F_NORM_BG_COLOR, + F_NORM_FG_COLOR, + F_NORM_BORDER_COLOR, + F_EVENT_B2PRESS, + F_EVENT_B3PRESS, + F_EVENT_B4PRESS, + F_EVENT_B5PRESS, + F_LAST }; /* array indexes of client file pointers */ enum { - C_PREFIX, - C_NAME, - C_LAST + C_PREFIX, + C_NAME, + C_LAST }; /* array indexes of wm file pointers */ enum { - WM_CTL, - WM_DETACHED_FRAME, - WM_DETACHED_CLIENT, - WM_TRANS_COLOR, - WM_AREA_GEOMETRY, - WM_SEL_BG_COLOR, - WM_SEL_BORDER_COLOR, - WM_SEL_FG_COLOR, - WM_NORM_BG_COLOR, - WM_NORM_BORDER_COLOR, - WM_NORM_FG_COLOR, - WM_FONT, - WM_BORDER, - WM_TAB, - WM_HANDLE_INC, - WM_LOCKED, - WM_SNAP_VALUE, - WM_SEL_PAGE, - WM_LAYOUT, - WM_EVENT_PAGE_UPDATE, - WM_EVENT_CLIENT_UPDATE, - WM_EVENT_B1PRESS, - WM_EVENT_B2PRESS, - WM_EVENT_B3PRESS, - WM_EVENT_B4PRESS, - WM_EVENT_B5PRESS, - WM_LAST + WM_CTL, + WM_DETACHED_FRAME, + WM_DETACHED_CLIENT, + WM_TRANS_COLOR, + WM_AREA_GEOMETRY, + WM_SEL_BG_COLOR, + WM_SEL_BORDER_COLOR, + WM_SEL_FG_COLOR, + WM_NORM_BG_COLOR, + WM_NORM_BORDER_COLOR, + WM_NORM_FG_COLOR, + WM_FONT, + WM_BORDER, + WM_TAB, + WM_HANDLE_INC, + WM_LOCKED, + WM_SNAP_VALUE, + WM_SEL_PAGE, + WM_LAYOUT, + WM_EVENT_PAGE_UPDATE, + WM_EVENT_CLIENT_UPDATE, + WM_EVENT_B1PRESS, + WM_EVENT_B2PRESS, + WM_EVENT_B3PRESS, + WM_EVENT_B4PRESS, + WM_EVENT_B5PRESS, + WM_LAST }; #define PROTO_DEL 1 @@ -105,64 +105,64 @@ typedef struct Frame Frame; typedef struct Client Client; struct Page { - Area *managed; - Area *floating; - Area *sel; - File *file[P_LAST]; - Page *next; - Page *prev; + Area *managed; + Area *floating; + Area *sel; + File *file[P_LAST]; + Page *next; + Page *prev; }; struct Layout { - char *name; - void (*init) (Area *, Client *); /* called when layout is initialized */ - Client *(*deinit) (Area *); /* called when layout is uninitialized */ - void (*arrange) (Area *); /* called when area is resized */ - Bool (*attach) (Area *, Client *); /* called on attach */ - void (*detach) (Area *, Client *, Bool unmap); /* called on detach */ - void (*resize) (Frame *, XRectangle *, XPoint *); /* called after resize */ - void (*focus) (Frame *, Bool raise); /* focussing a frame */ - Frame *(*frames) (Area *); /* called for drawing */ - Frame *(*sel) (Area *); /* returns selected frame */ - Action *(*actions) (Area *); /* local action table */ - Layout *next; + char *name; + void (*init) (Area *, Client *); /* called when layout is initialized */ + Client *(*deinit) (Area *); /* called when layout is uninitialized */ + void (*arrange) (Area *); /* called when area is resized */ + Bool(*attach) (Area *, Client *); /* called on attach */ + void (*detach) (Area *, Client *, Bool unmap); /* called on detach */ + void (*resize) (Frame *, XRectangle *, XPoint *); /* called after resize */ + void (*focus) (Frame *, Bool raise); /* focussing a frame */ + Frame *(*frames) (Area *); /* called for drawing */ + Frame *(*sel) (Area *); /* returns selected frame */ + Action *(*actions) (Area *); /* local action table */ + Layout *next; }; struct Area { - Page *page; - Layout *layout; - void *aux; /* free pointer */ - File *file[A_LAST]; + Page *page; + Layout *layout; + void *aux; /* free pointer */ + File *file[A_LAST]; }; struct Frame { - Area *area; - Window win; - Client *sel; - Client *clients; - size_t nclients; - GC gc; - XRectangle rect; - Cursor cursor; - void *aux; /* free pointer */ - File *file[F_LAST]; - Frame *next; - Frame *prev; + Area *area; + Window win; + Client *sel; + Client *clients; + size_t nclients; + GC gc; + XRectangle rect; + Cursor cursor; + void *aux; /* free pointer */ + File *file[F_LAST]; + Frame *next; + Frame *prev; }; struct Client { - int proto; - unsigned int border; - unsigned int ignore_unmap; - Bool destroyed; - Window win; - Window trans; - XRectangle rect; - XSizeHints size; - Frame *frame; - File *file[C_LAST]; - Client *next; - Client *prev; + int proto; + unsigned int border; + unsigned int ignore_unmap; + Bool destroyed; + Window win; + Window trans; + XRectangle rect; + XSizeHints size; + Frame *frame; + File *file[C_LAST]; + Client *next; + Client *prev; }; @@ -212,14 +212,14 @@ File *def[WM_LAST]; unsigned int valid_mask, num_lock_mask; /* area.c */ -Area *alloc_area(Page *p, char *layout); -void destroy_area(Area *a); -void focus_area(Area *a); -void hide_area(Area *a); -void show_area(Area *a, Bool raise); +Area *alloc_area(Page * p, char *layout); +void destroy_area(Area * a); +void focus_area(Area * a); +void hide_area(Area * a); +void show_area(Area * a, Bool raise); Area *sel_area(); -void attach_frame_to_area(Area *a, Frame *f); -void detach_frame_from_area(Frame *f); +void attach_frame_to_area(Area * a, Frame * f); +void detach_frame_from_area(Frame * f); /* client.c */ Client *alloc_client(Window w); @@ -228,7 +228,7 @@ void destroy_client(Client * c); void configure_client(Client * c); void handle_client_property(Client * c, XPropertyEvent * e); void close_client(Client * c); -void draw_client(Client *client); +void draw_client(Client * client); void draw_clients(Frame * f); void gravitate(Client * c, unsigned int tabh, unsigned int bw, int invert); void grab_client(Client * c, unsigned long mod, unsigned int button); @@ -236,23 +236,23 @@ void ungrab_client(Client * c, unsigned long mod, unsigned int button); void hide_client(Client * c); void show_client(Client * c); void reparent_client(Client * c, Window w, int x, int y); -void focus_client(Client *c); -void attach_client(Client *c); -void detach_client(Client *c, Bool unmap); +void focus_client(Client * c); +void attach_client(Client * c); +void detach_client(Client * c, Bool unmap); Client *sel_client(); -Client *clientat(Client *clients, size_t idx); -void detach_detached(Client *c); -void attach_detached(Client *c); +Client *clientat(Client * clients, size_t idx); +void detach_detached(Client * c); +void attach_detached(Client * c); /* frame.c */ Frame *win_to_frame(Window w); Frame *alloc_frame(XRectangle * r); void destroy_frame(Frame * f); -void resize_frame(Frame *f, XRectangle *r, XPoint *pt); -void draw_frame(Frame *f); -void handle_frame_buttonpress(XButtonEvent *e, Frame *f); -void attach_client_to_frame(Frame *f, Client *client); -void detach_client_from_frame(Client *client, Bool unmap); +void resize_frame(Frame * f, XRectangle * r, XPoint * pt); +void draw_frame(Frame * f); +void handle_frame_buttonpress(XButtonEvent * e, Frame * f); +void attach_client_to_frame(Frame * f, Client * client); +void detach_client_from_frame(Client * client, Bool unmap); unsigned int tab_height(Frame * f); unsigned int border_width(Frame * f); Frame *sel_frame(); @@ -273,11 +273,11 @@ void center_pointer(Frame * f); /* page.c */ Page *pageat(unsigned int idx); Page *alloc_page(); -void destroy_page(Page *p); -void focus_page(Page *p); +void destroy_page(Page * p); +void focus_page(Page * p); XRectangle *rectangles(unsigned int *num); -void hide_page(Page *p); -void show_page(Page *p); +void hide_page(Page * p); +void show_page(Page * p); /* layout.c */ Layout *match_layout(char *name); diff --git a/cmd/wmiibar.c b/cmd/wmiibar.c @@ -18,23 +18,23 @@ /* 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, - B_LAST + B_CTL, + B_NEW, + B_EXPANDABLE, + B_GEOMETRY, + B_FONT, + B_FG_COLOR, + B_BORDER_COLOR, + B_BG_COLOR, + B_EVENT_RESIZE, + B_LAST } BarIndexes; typedef struct Item Item; struct Item { - File *root; - Draw d; - Item *next; + File *root; + Draw d; + Item *next; }; static unsigned int id = 0; @@ -64,25 +64,26 @@ static void _destroy(void *obj, char *arg); static void handle_after_write(IXPServer * s, File * f); static Action acttbl[] = { - {"quit", quit}, - {"display", display}, - {"update", draw_bar}, - {"reset", reset}, - {"destroy", _destroy}, - {0, 0} + {"quit", quit}, + {"display", display}, + {"update", draw_bar}, + {"reset", reset}, + {"destroy", _destroy}, + {0, 0} }; static char *version[] = { - "wmiibar - window manager improved bar - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiibar - window manager improved bar - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, "%s", - "usage: wmiibar -s <socket file> [-v]\n" - " -s socket file\n" " -v version info\n"); - exit(1); + fprintf(stderr, "%s", + "usage: wmiibar -s <socket file> [-v]\n" + " -s socket file\n" " -v version info\n"); + exit(1); } /** @@ -96,439 +97,474 @@ static void usage() * <path>/b4press "<command>" * <path>/b5press "<command>" */ -static void create_label(char *path) +static void +create_label(char *path) { - File *f; - char buf[MAX_BUF]; - int i; - - snprintf(buf, MAX_BUF, "%s/data", path); - f = ixp_create(ixps, buf); - 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); - for (i = 1; i < 6; i++) { /* 5 buttons events */ - snprintf(buf, MAX_BUF, "%s/b%dpress", path, i); - ixp_create(ixps, buf); - } + File *f; + char buf[MAX_BUF]; + int i; + + snprintf(buf, MAX_BUF, "%s/data", path); + f = ixp_create(ixps, buf); + 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); + for(i = 1; i < 6; i++) { /* 5 buttons events */ + snprintf(buf, MAX_BUF, "%s/b%dpress", path, i); + ixp_create(ixps, buf); + } } -static void _destroy(void *obj, char *arg) +static void +_destroy(void *obj, char *arg) { - char buf[512]; - if (!arg) - return; - snprintf(buf, sizeof(buf), "/%s", arg); - ixps->remove(ixps, buf); - draw_bar(0, 0); + char buf[512]; + if(!arg) + return; + snprintf(buf, sizeof(buf), "/%s", arg); + ixps->remove(ixps, buf); + draw_bar(0, 0); } -static void reset(void *obj, char *arg) +static void +reset(void *obj, char *arg) { - int i; - char buf[512]; - for (i = 0; i < id; i++) { - snprintf(buf, sizeof(buf), "/%d", i); - ixps->remove(ixps, buf); - } - id = 0; - draw_bar(0, 0); + int i; + char buf[512]; + for(i = 0; i < id; i++) { + snprintf(buf, sizeof(buf), "/%d", i); + ixps->remove(ixps, buf); + } + id = 0; + draw_bar(0, 0); } -static void quit(void *obj, char *arg) +static void +quit(void *obj, char *arg) { - ixps->runlevel = SHUTDOWN; + ixps->runlevel = SHUTDOWN; } -static void display(void *obj, char *arg) +static void +display(void *obj, char *arg) { - if (!arg) - return; - displayed = blitz_strtonum(arg, 0, 1); - if (displayed) { - XMapRaised(dpy, win); - draw_bar(0, 0); - } else { - XUnmapWindow(dpy, win); - XSync(dpy, False); - } + if(!arg) + return; + displayed = blitz_strtonum(arg, 0, 1); + 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) +static void +init_draw_label(char *path, Draw * d) { - char buf[MAX_BUF]; - File *f; - - /* 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); + char buf[MAX_BUF]; + File *f; + + /* 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) +static void +init_item(char *path, Item * i) { - 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); + 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); } -static int comp_str(const void *s1, const void *s2) +static int +comp_str(const void *s1, const void *s2) { - return strcmp(*(char **) s1, *(char **) s2); + return strcmp(*(char **) s1, *(char **) s2); } -static void draw() +static void +draw() { - Item *i; - unsigned int idx = 0, w = 0, xoff = 0; - unsigned expandable = 0; - char buf[32]; - - if (!nitems) - return; - - expandable = blitz_strtonum(file[B_EXPANDABLE]->content, 0, nitems); - snprintf(buf, sizeof(buf), "/%d", expandable); - if (!ixp_walk(ixps, buf)) - expandable = 0; - - /* precalc */ - for (i = items; i; i = i->next) { - if (idx != expandable) { - i->d.rect.width = brect.height; - if (i->d.data) { - if (!strncmp(i->d.data, "%m:", 3)) - /* meter */ - i->d.rect.width = brect.height / 2; - else - i->d.rect.width += XTextWidth(font, i->d.data, strlen(i->d.data)); - } - w += i->d.rect.width; - } - idx++; - } - if (w > brect.width) { - /* failsafe mode, give all labels same width */ - w = brect.width / nitems; - for (i = items; i && i->next; i = i->next) - i->d.rect.width = w; - i->d.rect.width = w; - i->d.rect.width = brect.width - i->d.rect.x; - } - else { - idx = 0; - for (i = items; i && idx != expandable; i = i->next) idx++; - i->d.rect.width = brect.width - w; - } - for (i = items; i; i = i->next) { - i->d.font = font; - i->d.rect.x = xoff; - xoff += i->d.rect.width; - if (i->d.data && !strncmp(i->d.data, "%m:", 3)) - blitz_drawmeter(dpy, &i->d); - else - blitz_drawlabel(dpy, &i->d); - } - XCopyArea(dpy, pmap, win, gc, 0, 0, brect.width, brect.height, 0, 0); - XSync(dpy, False); + Item *i; + unsigned int idx = 0, w = 0, xoff = 0; + unsigned expandable = 0; + char buf[32]; + + if(!nitems) + return; + + expandable = blitz_strtonum(file[B_EXPANDABLE]->content, 0, nitems); + snprintf(buf, sizeof(buf), "/%d", expandable); + if(!ixp_walk(ixps, buf)) + expandable = 0; + + /* precalc */ + for(i = items; i; i = i->next) { + if(idx != expandable) { + i->d.rect.width = brect.height; + if(i->d.data) { + if(!strncmp(i->d.data, "%m:", 3)) + /* meter */ + i->d.rect.width = brect.height / 2; + else + i->d.rect.width += + XTextWidth(font, i->d.data, strlen(i->d.data)); + } + w += i->d.rect.width; + } + idx++; + } + if(w > brect.width) { + /* failsafe mode, give all labels same width */ + w = brect.width / nitems; + for(i = items; i && i->next; i = i->next) + i->d.rect.width = w; + i->d.rect.width = w; + i->d.rect.width = brect.width - i->d.rect.x; + } else { + idx = 0; + for(i = items; i && idx != expandable; i = i->next) + idx++; + i->d.rect.width = brect.width - w; + } + for(i = items; i; i = i->next) { + i->d.font = font; + i->d.rect.x = xoff; + xoff += i->d.rect.width; + if(i->d.data && !strncmp(i->d.data, "%m:", 3)) + blitz_drawmeter(dpy, &i->d); + else + blitz_drawlabel(dpy, &i->d); + } + XCopyArea(dpy, pmap, win, gc, 0, 0, brect.width, brect.height, 0, 0); + XSync(dpy, False); } -static void draw_bar(void *obj, char *arg) +static void +draw_bar(void *obj, char *arg) { - File *label = 0; - unsigned int j = 0; - Item *i, *it; - char buf[512]; - - if (!displayed) - return; - while ((i = items)) { - items = items->next; - free(i); - } - it = items = nil; - snprintf(buf, sizeof(buf), "%s", "/0"); - label = ixp_walk(ixps, buf); - if (!label) { - Draw d = { 0 }; - /* 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); - blitz_drawlabelnoborder(dpy, &d); - } else { - File *f; - char **paths = 0; - /* - * take order into account, directory names are used in - * alphabetical order - */ - nitems = 0; - for (f = label; f; f = f->next) - nitems++; - paths = cext_emallocz(sizeof(char *) * nitems); - j = 0; - for (f = label; f; f = f->next) - paths[j++] = f->name; - qsort(paths, nitems, sizeof(char *), comp_str); - for (j = 0; j < nitems; j++) { - snprintf(buf, sizeof(buf), "/%s", paths[j]); - i = cext_emallocz(sizeof(Item)); - init_item(buf, i); - if (!it) - it = items = i; - else { - it->next = i; - it = i; - } - } - free(paths); - draw(); - } + File *label = 0; + unsigned int j = 0; + Item *i, *it; + char buf[512]; + + if(!displayed) + return; + while((i = items)) { + items = items->next; + free(i); + } + it = items = nil; + snprintf(buf, sizeof(buf), "%s", "/0"); + label = ixp_walk(ixps, buf); + if(!label) { + Draw d = { 0 }; + /* 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); + blitz_drawlabelnoborder(dpy, &d); + } else { + File *f; + char **paths = 0; + /* + * take order into account, directory names are used in + * alphabetical order + */ + nitems = 0; + for(f = label; f; f = f->next) + nitems++; + paths = cext_emallocz(sizeof(char *) * nitems); + j = 0; + for(f = label; f; f = f->next) + paths[j++] = f->name; + qsort(paths, nitems, sizeof(char *), comp_str); + for(j = 0; j < nitems; j++) { + snprintf(buf, sizeof(buf), "/%s", paths[j]); + i = cext_emallocz(sizeof(Item)); + init_item(buf, i); + if(!it) + it = items = i; + else { + it->next = i; + it = i; + } + } + free(paths); + draw(); + } } -static Item *item_for_file(File *f) +static Item * +item_for_file(File * f) { - Item *i; - for (i = items; i; i = i->next) { - if (f == i->root) - return i; - } - return nil; + Item *i; + for(i = items; i; i = i->next) { + if(f == i->root) + return i; + } + return nil; } -static void handle_buttonpress(XButtonPressedEvent *e) +static void +handle_buttonpress(XButtonPressedEvent * e) { - Item *i; - File *p; - char buf[MAX_BUF]; - char path[512]; - for (i = items; i; i = i->next) - 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; - } + Item *i; + File *p; + char buf[MAX_BUF]; + char path[512]; + for(i = items; i; i = i->next) + 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; + } } -static void check_event(Connection * e) +static void +check_event(Connection * e) { - XEvent ev; - - 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; - } - } + XEvent ev; + + 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() +static void +update_geometry() { - 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); + 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); } -static void handle_after_write(IXPServer * s, File * f) +static void +handle_after_write(IXPServer * s, File * f) { - int i; - size_t len; - Item *item; - char buf[512]; - - buf[0] = 0; - if (!strncmp(f->name, "data", 5)) { - if ((item = 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(); - draw_bar(0, 0); - } - } 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)) { - 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); - } - check_event(0); + int i; + size_t len; + Item *item; + char buf[512]; + + buf[0] = 0; + if(!strncmp(f->name, "data", 5)) { + if((item = 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(); + draw_bar(0, 0); + } + } 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)) { + 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); + } + check_event(0); } -static void handle_before_read(IXPServer * s, File * f) +static void +handle_before_read(IXPServer * s, File * f) { - char buf[64]; - if (f == file[B_GEOMETRY]) { - snprintf(buf, 64, "%d,%d,%d,%d", brect.x, brect.y, brect.width, brect.height); - 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++); - if (f->content) - free(f->content); - f->content = strdup(buf); - f->size = strlen(buf); - create_label(buf); - draw_bar(0, 0); - } + char buf[64]; + if(f == file[B_GEOMETRY]) { + snprintf(buf, 64, "%d,%d,%d,%d", brect.x, brect.y, brect.width, + brect.height); + 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++); + 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) +static int +dummy_error_handler(Display * dpy, XErrorEvent * err) { - return 0; + return 0; } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i; - XSetWindowAttributes wa; - XGCValues gcv; - - /* 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); - - - /* init */ - if (!(file[B_CTL] = ixp_create(ixps, "/ctl"))) { - perror("wmiibar: cannot connect IXP server"); - 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"); - - wa.override_redirect = 1; - wa.background_pixmap = ParentRelative; - wa.event_mask = ExposureMask | ButtonPressMask | SubstructureRedirectMask | SubstructureNotifyMask; - - 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; - - win = XCreateWindow(dpy, RootWindow(dpy, screen_num), brect.x, brect.y, - brect.width, brect.height, 0, DefaultDepth(dpy, screen_num), - CopyFromParent, DefaultVisual(dpy, screen_num), - CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - 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)); - - /* main event loop */ - run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, 0); - deinit_server(ixps); - XFreePixmap(dpy, pmap); - XFreeGC(dpy, gc); - XCloseDisplay(dpy); - - return 0; + int i; + XSetWindowAttributes wa; + XGCValues gcv; + + /* 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); + + + /* init */ + if(!(file[B_CTL] = ixp_create(ixps, "/ctl"))) { + perror("wmiibar: cannot connect IXP server"); + 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"); + + wa.override_redirect = 1; + wa.background_pixmap = ParentRelative; + wa.event_mask = + ExposureMask | ButtonPressMask | SubstructureRedirectMask | + SubstructureNotifyMask; + + 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; + + win = XCreateWindow(dpy, RootWindow(dpy, screen_num), brect.x, brect.y, + brect.width, brect.height, 0, DefaultDepth(dpy, + screen_num), + CopyFromParent, DefaultVisual(dpy, screen_num), + CWOverrideRedirect | CWBackPixmap | CWEventMask, + &wa); + 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)); + + /* main event loop */ + run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, + 0); + deinit_server(ixps); + XFreePixmap(dpy, pmap); + XFreeGC(dpy, gc); + XCloseDisplay(dpy); + + return 0; } diff --git a/cmd/wmiibar2.c b/cmd/wmiibar2.c @@ -38,56 +38,56 @@ * /1/fgcolor Fcolor <#RRGGBB, #RGB> * /1/bordercolor Fcolor <#RRGGBB, #RGB> ... */ -enum { /* 8-bit qid.path.type */ - Droot, - Ditem, - Fdisplay, - Fnew, - Fdata, /* data to display */ - Fevent, - Fcolor, - Ffont +enum { /* 8-bit qid.path.type */ + Droot, + Ditem, + Fdisplay, + Fnew, + Fdata, /* data to display */ + Fevent, + Fcolor, + Ffont }; -#define NONE (u16)0xffff +#define NONE (unsigned short)0xffff typedef struct { - char *name; - u8 type; + char *name; + unsigned char type; } QFile; static QFile qfilelist[] = { - {"display", Fdisplay}, - {"font", Ffont}, - {"new", Fnew}, - {"data", Fdata}, - {"bgcolor", Fcolor}, - {"fgcolor", Fcolor}, - {"bordercolor", Fcolor}, - {"b1press", Fevent}, - {"b2press", Fevent}, - {"b3press", Fevent}, - {"b4press", Fevent}, - {"b5press", Fevent}, - {0, 0}, + {"display", Fdisplay}, + {"font", Ffont}, + {"new", Fnew}, + {"data", Fdata}, + {"bgcolor", Fcolor}, + {"fgcolor", Fcolor}, + {"bordercolor", Fcolor}, + {"b1press", Fevent}, + {"b2press", Fevent}, + {"b3press", Fevent}, + {"b4press", Fevent}, + {"b5press", Fevent}, + {0, 0}, }; typedef struct Map Map; struct Map { - u32 fid; - Qid qid; - Map *next; + unsigned int fid; + Qid qid; + Map *next; }; typedef struct { - int id; - char text[256]; - int value; - unsigned long bg; - unsigned long fg; - unsigned long border[4]; - XFontStruct *font; - char event[5][256]; + int id; + char text[256]; + int value; + unsigned long bg; + unsigned long fg; + unsigned long border[4]; + XFontStruct *font; + char event[5][256]; } Item; static size_t nitems = 0; @@ -110,361 +110,381 @@ static Draw zero_draw = { 0 }; */ static char *version[] = { - "wmiibar - window manager improved bar - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiibar - window manager improved bar - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, "%s %d", - "usage: wmiibar -a <server address> [-v]\n" - " -a server address \n" - " -v version info\n", - NONE); - exit(1); + fprintf(stderr, "%s %d", + "usage: wmiibar -a <server address> [-v]\n" + " -a server address \n" + " -v version info\n", NONE); + exit(1); } -static int dummy_error_handler(Display * dpy, XErrorEvent * err) +static int +dummy_error_handler(Display * dpy, XErrorEvent * err) { - return 0; + return 0; } -static void exit_cleanup() +static void +exit_cleanup() { - if (mypid == getpid()) - unlink(sockfile); + if(mypid == getpid()) + unlink(sockfile); } -static u64 make_qpath(u8 type, u16 item, u16 file) +static unsigned long long +make_qpath(unsigned char type, unsigned short item, unsigned short file) { - return ((u64) file << 24) | ((u64) item << 8) | (u64) type; + return ((unsigned long long) file << 24) | ((unsigned long long) item << 8) | (unsigned long long) type; } -static u8 qpath_type(u64 path) +static unsigned char +qpath_type(unsigned long long path) { - return path & 0xff; + return path & 0xff; } -static u16 qpath_item(u64 path) +static unsigned short +qpath_item(unsigned long long path) { - return (path >> 8) & 0xffff; + return (path >> 8) & 0xffff; } /* -static u16 -qpath_file(u64 path) +static unsigned short +qpath_file(unsigned long long path) { return (path >> 24) & 0xffff; } */ -static Map *fid_to_map(Map *maps, u32 fid) +static Map * +fid_to_map(Map * maps, unsigned int fid) { - Map *m; - for (m = maps; m; m = m->next) - if (m->fid == fid) - return m; - return nil; + Map *m; + for(m = maps; m; m = m->next) + if(m->fid == fid) + return m; + return nil; } -static Bool qfile_index(char *name, u16 * index) +static Bool +qfile_index(char *name, unsigned short *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 False; + int i; + for(i = 0; qfilelist[i].name; i++) + if(!strncmp(name, qfilelist[i].name, strlen(qfilelist[i].name))) { + *index = i; + return True; + } + return False; } -static Bool 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; - new->version = nil; - if (!qfile_index(wname, &idx)) { - new->type = IXP_QTDIR; - if (!strncmp(wname, "..", 3)) { - *new = root_qid; - return True; - } else if (!strncmp(wname, "default", 8)) { - new->path = make_qpath(Ditem, 0, NONE); - return True; - } - /* check if wname is a number, otherwise file not found */ - idx = (u16) cext_strtonum(wname, 1, 0xffff, &errstr); - if (errstr || nitems < 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); - } - return True; + unsigned short idx; + const char *errstr; + if(dir->type != IXP_QTDIR) + return False; + new->version = nil; + if(!qfile_index(wname, &idx)) { + new->type = IXP_QTDIR; + if(!strncmp(wname, "..", 3)) { + *new = root_qid; + return True; + } else if(!strncmp(wname, "default", 8)) { + new->path = make_qpath(Ditem, 0, NONE); + return True; + } + /* check if wname is a number, otherwise file not found */ + idx = (unsigned short) cext_strtonum(wname, 1, 0xffff, &errstr); + if(errstr || nitems < 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); + } + return True; } -static int attach(IXPServer *s, IXPConn *c) +static int +attach(IXPServer * s, IXPConn * c) { - Map *maps = c->aux; - Map *m, *new = cext_emallocz(sizeof(Map)); - - if (!maps) - c->aux = new; - else { - for (m = maps; m && m->next; m = m->next); - m->next = new; - } - fprintf(stderr, "attaching %u %u %s %s\n", s->fcall.fid, s->fcall.afid, s->fcall.uname, s->fcall.aname); - new->qid = root_qid; - new->fid = s->fcall.fid; - s->fcall.id = RATTACH; - s->fcall.qid = root_qid; - return TRUE; + Map *maps = c->aux; + Map *m, *new = cext_emallocz(sizeof(Map)); + + if(!maps) + c->aux = new; + else { + for(m = maps; m && m->next; m = m->next); + m->next = new; + } + fprintf(stderr, "attaching %u %u %s %s\n", s->fcall.fid, s->fcall.afid, + s->fcall.uname, s->fcall.aname); + new->qid = root_qid; + new->fid = s->fcall.fid; + s->fcall.id = RATTACH; + s->fcall.qid = root_qid; + return TRUE; } -static int walk(IXPServer * s, IXPConn * c) +static int +walk(IXPServer * s, IXPConn * c) { - u16 nwqid = 0; - Qid qid; - Map *map; - - fprintf(stderr, "%s", "walking\n"); - if (!(map = fid_to_map(c->aux, s->fcall.fid))) { - s->errstr = "no directory associated with fid"; - return FALSE; - } - if (s->fcall.fid != s->fcall.newfid - && (fid_to_map(c->aux, s->fcall.newfid))) { - s->errstr = "fid alreay in use"; - return FALSE; - } - 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++) - qid = s->fcall.wqid[nwqid]; - if (!nwqid) { - s->errstr = "file not found"; - return FALSE; - } - } - /* - * following condition is required by 9P, a fid will only be valid if - * the walk was complete - */ - if (nwqid == s->fcall.nwname) { - Map *m, *maps = c->aux; - if (s->fcall.fid == s->fcall.newfid) { - if (maps == map) - maps = maps->next; - else { - for (m = maps; m && m->next != map; m = m->next); - m->next = map->next; - } - free(map); - } - map = cext_emallocz(sizeof(Map)); - map->qid = qid; - map->fid = s->fcall.newfid; - for (m = maps; m && m->next; m = m->next); - if (!m) - maps = map; - else - m->next = map; - } - s->fcall.id = RWALK; - s->fcall.nwqid = nwqid; - return TRUE; + unsigned short nwqid = 0; + Qid qid; + Map *map; + + fprintf(stderr, "%s", "walking\n"); + if(!(map = fid_to_map(c->aux, s->fcall.fid))) { + s->errstr = "no directory associated with fid"; + return FALSE; + } + if(s->fcall.fid != s->fcall.newfid + && (fid_to_map(c->aux, s->fcall.newfid))) { + s->errstr = "fid alreay in use"; + return FALSE; + } + 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++) + qid = s->fcall.wqid[nwqid]; + if(!nwqid) { + s->errstr = "file not found"; + return FALSE; + } + } + /* + * following condition is required by 9P, a fid will only be valid if + * the walk was complete + */ + if(nwqid == s->fcall.nwname) { + Map *m, *maps = c->aux; + if(s->fcall.fid == s->fcall.newfid) { + if(maps == map) + maps = maps->next; + else { + for(m = maps; m && m->next != map; m = m->next); + m->next = map->next; + } + free(map); + } + map = cext_emallocz(sizeof(Map)); + map->qid = qid; + map->fid = s->fcall.newfid; + for(m = maps; m && m->next; m = m->next); + if(!m) + maps = map; + else + m->next = map; + } + s->fcall.id = RWALK; + s->fcall.nwqid = nwqid; + return TRUE; } -static int _open(IXPServer * s, IXPConn * c) +static int +_open(IXPServer * s, IXPConn * c) { - Map *map = fid_to_map(c->aux, s->fcall.fid); - - fprintf(stderr, "%s", "opening\n"); - if (!map) { - s->errstr = "invalid fid"; - return FALSE; - } - if ((s->fcall.mode != IXP_OREAD) && (s->fcall.mode != IXP_OWRITE)) { - s->errstr = "mode not supported"; - return FALSE; - } - s->fcall.id = ROPEN; - s->fcall.qid = map->qid; - s->fcall.iounit = - s->fcall.maxmsg - (sizeof(u8) + sizeof(u16) + 2 * sizeof(u32)); - return TRUE; + Map *map = fid_to_map(c->aux, s->fcall.fid); + + fprintf(stderr, "%s", "opening\n"); + if(!map) { + s->errstr = "invalid fid"; + return FALSE; + } + if((s->fcall.mode != IXP_OREAD) && (s->fcall.mode != IXP_OWRITE)) { + s->errstr = "mode not supported"; + return FALSE; + } + s->fcall.id = ROPEN; + s->fcall.qid = map->qid; + s->fcall.iounit = + s->fcall.maxmsg - (sizeof(unsigned char) + sizeof(unsigned short) + 2 * sizeof(unsigned int)); + return TRUE; } -static int _read(IXPServer * s, IXPConn * c) +static int +_read(IXPServer * s, IXPConn * c) { - Map *map = fid_to_map(c->aux, s->fcall.fid); - Stat stat = { 0 }; - u8 *p; - - fprintf(stderr, "%s", "reading\n"); - if (!map) { - s->errstr = "invalid fid"; - return FALSE; - } - stat.mode = 0xff; - stat.atime = stat.mtime = time(0); - cext_strlcpy(stat.uid, getenv("USER"), sizeof(stat.uid)); - cext_strlcpy(stat.gid, getenv("USER"), sizeof(stat.gid)); - cext_strlcpy(stat.muid, getenv("USER"), sizeof(stat.muid)); - - fprintf(stderr, "%d\n", qpath_item(map->qid.path)); - switch (qpath_type(map->qid.path)) { - default: - case Droot: - p = s->fcall.data; - cext_strlcpy(stat.name, "display", sizeof(stat.name)); - stat.length = strlen(align); - make_qid(&root_qid, "display", &stat.qid); - stat.size = ixp_sizeof_stat(&stat); - s->fcall.count = stat.size; - p = ixp_enc_stat(p, &stat); - cext_strlcpy(stat.name, "font", sizeof(stat.name)); - stat.length = strlen(font); - make_qid(&root_qid, "font", &stat.qid); - stat.size = ixp_sizeof_stat(&stat);; - s->fcall.count += stat.size; - p = ixp_enc_stat(p, &stat); - cext_strlcpy(stat.name, "new", sizeof(stat.name)); - stat.length = 0; - make_qid(&root_qid, "new", &stat.qid); - stat.size = ixp_sizeof_stat(&stat);; - s->fcall.count += stat.size; - p = ixp_enc_stat(p, &stat); - s->fcall.id = RREAD; - fprintf(stderr, "%d msize\n", s->fcall.count); - break; - case Ditem: - break; - case Fdisplay: - break; - case Fnew: - break; - case Fdata: - break; - case Fevent: - break; - case Fcolor: - break; - case Ffont: - break; - } - - return TRUE; + Map *map = fid_to_map(c->aux, s->fcall.fid); + Stat stat = { 0 }; + unsigned char *p; + + fprintf(stderr, "%s", "reading\n"); + if(!map) { + s->errstr = "invalid fid"; + return FALSE; + } + stat.mode = 0xff; + stat.atime = stat.mtime = time(0); + cext_strlcpy(stat.uid, getenv("USER"), sizeof(stat.uid)); + cext_strlcpy(stat.gid, getenv("USER"), sizeof(stat.gid)); + cext_strlcpy(stat.muid, getenv("USER"), sizeof(stat.muid)); + + fprintf(stderr, "%d\n", qpath_item(map->qid.path)); + switch (qpath_type(map->qid.path)) { + default: + case Droot: + p = s->fcall.data; + cext_strlcpy(stat.name, "display", sizeof(stat.name)); + stat.length = strlen(align); + make_qid(&root_qid, "display", &stat.qid); + stat.size = ixp_sizeof_stat(&stat); + s->fcall.count = stat.size; + p = ixp_enc_stat(p, &stat); + cext_strlcpy(stat.name, "font", sizeof(stat.name)); + stat.length = strlen(font); + make_qid(&root_qid, "font", &stat.qid); + stat.size = ixp_sizeof_stat(&stat);; + s->fcall.count += stat.size; + p = ixp_enc_stat(p, &stat); + cext_strlcpy(stat.name, "new", sizeof(stat.name)); + stat.length = 0; + make_qid(&root_qid, "new", &stat.qid); + stat.size = ixp_sizeof_stat(&stat);; + s->fcall.count += stat.size; + p = ixp_enc_stat(p, &stat); + s->fcall.id = RREAD; + fprintf(stderr, "%d msize\n", s->fcall.count); + break; + case Ditem: + break; + case Fdisplay: + break; + case Fnew: + break; + case Fdata: + break; + case Fevent: + break; + case Fcolor: + break; + case Ffont: + break; + } + + return TRUE; } -static int _write(IXPServer *s, IXPConn *c) +static int +_write(IXPServer * s, IXPConn * c) { - return FALSE; + return FALSE; } -static int clunk(IXPServer *s, IXPConn *c) +static int +clunk(IXPServer * s, IXPConn * c) { - Map *maps = c->aux; - Map *m, *map = fid_to_map(maps, s->fcall.fid); - - if (!map) { - s->errstr = "invalid fid"; - return FALSE; - } - if (maps == map) - maps = maps->next; - else { - for (m = maps; m && m->next != map; m = m->next); - m->next = map->next; - } - free(map); - s->fcall.id = RCLUNK; - return TRUE; + Map *maps = c->aux; + Map *m, *map = fid_to_map(maps, s->fcall.fid); + + if(!map) { + s->errstr = "invalid fid"; + return FALSE; + } + if(maps == map) + maps = maps->next; + else { + for(m = maps; m && m->next != map; m = m->next); + m->next = map->next; + } + free(map); + s->fcall.id = RCLUNK; + return TRUE; } -static void freeconn(IXPServer * s, IXPConn * c) +static void +freeconn(IXPServer * s, IXPConn * c) { - Map *m, *maps = c->aux; + Map *m, *maps = c->aux; - while ((m = maps)) { - maps = maps->next; - free(m); - } - c->aux = nil; + while((m = maps)) { + maps = maps->next; + free(m); + } + c->aux = nil; } static IXPTFunc funcs[] = { - {TVERSION, ixp_server_tversion}, - {TATTACH, attach}, - {TWALK, walk}, - {TOPEN, _open}, - {TREAD, _read}, - {TWRITE, _write}, - {TCLUNK, clunk}, - {0, 0} + {TVERSION, ixp_server_tversion}, + {TATTACH, attach}, + {TWALK, walk}, + {TOPEN, _open}, + {TREAD, _read}, + {TWRITE, _write}, + {TCLUNK, clunk}, + {0, 0} }; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i; - Item *item; - - /* 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 'a': - 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); - - if (!ixp_server_init(&srv, sockfile, funcs, freeconn)) { - fprintf(stderr, "wmiibar: fatal: %s\n", srv.errstr); - exit(1); - } - root_qid.type = IXP_QTDIR; - root_qid.version = 0; - root_qid.path = make_qpath(Droot, NONE, NONE); - - mypid = getpid(); - atexit(exit_cleanup); - - /* default item settings */ - item = cext_emallocz(sizeof(Item)); - - align = "bottom"; - font = "fixed"; - - ixp_server_loop(&srv); - if (srv.errstr) { - fprintf(stderr, "wmiibar: fatal: %s\n", srv.errstr); - ixp_server_deinit(&srv); - exit(1); - } - ixp_server_deinit(&srv); - return 0; + int i; + Item *item; + + /* 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 'a': + 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); + + if(!ixp_server_init(&srv, sockfile, funcs, freeconn)) { + fprintf(stderr, "wmiibar: fatal: %s\n", srv.errstr); + exit(1); + } + root_qid.type = IXP_QTDIR; + root_qid.version = 0; + root_qid.path = make_qpath(Droot, NONE, NONE); + + mypid = getpid(); + atexit(exit_cleanup); + + /* default item settings */ + item = cext_emallocz(sizeof(Item)); + + align = "bottom"; + font = "fixed"; + + ixp_server_loop(&srv); + if(srv.errstr) { + fprintf(stderr, "wmiibar: fatal: %s\n", srv.errstr); + ixp_server_deinit(&srv); + exit(1); + } + ixp_server_deinit(&srv); + return 0; } diff --git a/cmd/wmiifs.c b/cmd/wmiifs.c @@ -11,23 +11,23 @@ /* array indexes for file pointers */ typedef enum { - F_CTL, - F_LAST + F_CTL, + F_LAST } FsIndexes; typedef struct Route Route; struct Route { - File dest; - int src; + File dest; + int src; }; typedef struct Bind Bind; struct Bind { - IXPClient *client; - Route route[MAX_CONN * MAX_OPEN_FILES]; - File *mount; - char *prefix; - Bind *next; + IXPClient *client; + Route route[MAX_CONN * MAX_OPEN_FILES]; + File *mount; + char *prefix; + Bind *next; }; static Display *dpy; @@ -41,344 +41,373 @@ static void bind(void *obj, char *arg); static void unbind(void *obj, char *arg); static Action acttbl[] = { - {"quit", quit}, - {"unbind", unbind}, - {"bind", bind}, - {0, 0} + {"quit", quit}, + {"unbind", unbind}, + {"bind", bind}, + {0, 0} }; static char *version[] = { - "wmiifs - window manager improved filesystem - " VERSION "\n" - " (C)opyright MMV Anselm R. Garbe\n", 0 + "wmiifs - window manager improved filesystem - " VERSION "\n" + " (C)opyright MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, - "usage: wmiifs -s <socket file> [-v]\n" - " -s socket file\n" " -v version info\n"); - exit(1); + fprintf(stderr, + "usage: wmiifs -s <socket file> [-v]\n" + " -s socket file\n" " -v version info\n"); + exit(1); } -static void quit(void *obj, char *arg) +static void +quit(void *obj, char *arg) { - Bind *b; - while ((b = bindings)) { - bindings = bindings->next; - if (b->mount) { - b->mount->content = nil; - ixp_remove(ixps, b->prefix); - if (ixps->errstr) - fprintf(stderr, "wmiifs: error on unbind %s: %s\n", b->prefix, ixps->errstr); - } - free(b->prefix); - free(b); - } - bindings = nil; - ixps->runlevel = SHUTDOWN; + Bind *b; + while((b = bindings)) { + bindings = bindings->next; + if(b->mount) { + b->mount->content = nil; + ixp_remove(ixps, b->prefix); + if(ixps->errstr) + fprintf(stderr, "wmiifs: error on unbind %s: %s\n", + b->prefix, ixps->errstr); + } + free(b->prefix); + free(b); + } + bindings = nil; + ixps->runlevel = SHUTDOWN; } -static void do_unbind(Bind *bind) +static void +do_unbind(Bind * bind) { - Bind *b; - if (bind == bindings) - bindings = bind->next; - else { - for (b = bindings; b && b->next != bind; b = b->next); - b->next = bind->next; - } - /* free stuff */ - deinit_client(bind->client); - free(bind->prefix); - free(bind); + Bind *b; + if(bind == bindings) + bindings = bind->next; + else { + for(b = bindings; b && b->next != bind; b = b->next); + b->next = bind->next; + } + /* free stuff */ + deinit_client(bind->client); + free(bind->prefix); + free(bind); } -static void unbind(void *obj, char *arg) +static void +unbind(void *obj, char *arg) { - Bind *b; - - for (b = bindings; b && strncmp(b->prefix, arg, strlen(b->prefix)); b = b->next); - - if (!b) { - fprintf(stderr, "wmiifs: unbind: '%s' no such path\n", arg); - return; - } - do_unbind(b); + Bind *b; + + for(b = bindings; b && strncmp(b->prefix, arg, strlen(b->prefix)); + b = b->next); + + if(!b) { + fprintf(stderr, "wmiifs: unbind: '%s' no such path\n", arg); + return; + } + do_unbind(b); } -static void bind(void *obj, char *arg) +static void +bind(void *obj, char *arg) { - Bind *b, *new = nil; - char cmd[1024]; - char *sfile; - - if (!arg) - return; - cext_strlcpy(cmd, arg, sizeof(cmd)); - sfile = strchr(cmd, ' '); - if (!sfile) { - fprintf(stderr, "wmiifs: bind: '%s' without socket argument, ignoring\n", arg); - return; /* shortcut with empty argument */ - } - *sfile = 0; - sfile++; - if (*sfile == 0) { - fprintf(stderr, "wmiifs: bind: '%s' without socket argument, ignoring\n", arg); - return; /* shortcut with empty argument */ - } - new = cext_emallocz(sizeof(Bind)); - new->client = init_ixp_client(sfile); - - if (!new->client) { - fprintf(stderr, "wmiifs: bind: cannot connect to server '%s', ignoring\n", sfile); - free(new); - return; - } - new->prefix = strdup(cmd); - new->mount = ixp_create(ixps, new->prefix); - new->mount->content = new->mount; /* shall be a directory */ - - if (!bindings) - bindings = new; - else { - for (b = bindings; b && b->next; b = b->next); - b->next = new; - } + Bind *b, *new = nil; + char cmd[1024]; + char *sfile; + + if(!arg) + return; + cext_strlcpy(cmd, arg, sizeof(cmd)); + sfile = strchr(cmd, ' '); + if(!sfile) { + fprintf(stderr, + "wmiifs: bind: '%s' without socket argument, ignoring\n", + arg); + return; /* shortcut with empty argument */ + } + *sfile = 0; + sfile++; + if(*sfile == 0) { + fprintf(stderr, + "wmiifs: bind: '%s' without socket argument, ignoring\n", + arg); + return; /* shortcut with empty argument */ + } + new = cext_emallocz(sizeof(Bind)); + new->client = init_ixp_client(sfile); + + if(!new->client) { + fprintf(stderr, + "wmiifs: bind: cannot connect to server '%s', ignoring\n", + sfile); + free(new); + return; + } + new->prefix = strdup(cmd); + new->mount = ixp_create(ixps, new->prefix); + new->mount->content = new->mount; /* shall be a directory */ + + if(!bindings) + bindings = new; + else { + for(b = bindings; b && b->next; b = b->next); + b->next = new; + } } -static void handle_after_write(IXPServer * s, File * f) +static void +handle_after_write(IXPServer * s, File * f) { - int i; - size_t len; - - 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; - } - } + int i; + size_t len; + + 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; + } + } } -static Bind *fd_to_bind(int fd, int *client_fd) +static Bind * +fd_to_bind(int fd, int *client_fd) { - File *f = fd_to_file(ixps, fd); - unsigned int i; - Bind *b; - - if (!f) - return nil; - for (b = bindings; b; b = b->next) { - for (i = 0; i < MAX_CONN * MAX_OPEN_FILES; i++) { - if (&b->route[i].dest == f) { - *client_fd = b->route[i].src; - return b; - } - } - } - return nil; + File *f = fd_to_file(ixps, fd); + unsigned int i; + Bind *b; + + if(!f) + return nil; + for(b = bindings; b; b = b->next) { + for(i = 0; i < MAX_CONN * MAX_OPEN_FILES; i++) { + if(&b->route[i].dest == f) { + *client_fd = b->route[i].src; + return b; + } + } + } + return nil; } -static File *fixp_create(IXPServer * s, char *path) +static File * +fixp_create(IXPServer * s, char *path) { - Bind *b; - size_t len; - - for (b = bindings; b && strncmp(b->prefix, path, strlen(b->prefix)); b = b->next); - - if (!b) { - File *f = ixp_create(s, path); - return f; - } - /* route to b */ - len = strlen(b->prefix); - b->client->create(b->client, path[len] == 0 ? "/" : &path[len]); - if (b->client->errstr) { - if (!strcmp(b->client->errstr, DEAD_SERVER)) - do_unbind(b); - } - return nil; + Bind *b; + size_t len; + + for(b = bindings; b && strncmp(b->prefix, path, strlen(b->prefix)); + b = b->next); + + if(!b) { + File *f = ixp_create(s, path); + return f; + } + /* route to b */ + len = strlen(b->prefix); + b->client->create(b->client, path[len] == 0 ? "/" : &path[len]); + if(b->client->errstr) { + if(!strcmp(b->client->errstr, DEAD_SERVER)) + do_unbind(b); + } + return nil; } -static File *fixp_open(IXPServer * s, char *path) +static File * +fixp_open(IXPServer * s, char *path) { - Bind *b; - int fd; - size_t len; - - for (b = bindings; b && strncmp(b->prefix, path, strlen(b->prefix)); b = b->next); - - if (!b) { - File *f = ixp_open(s, path); - return f; - } - /* route to b */ - len = strlen(b->prefix); - fd = b->client->open(b->client, path[len] == 0 ? "/" : &path[len]); - if (b->client->errstr) { - set_error(s, b->client->errstr); - if (!strcmp(b->client->errstr, DEAD_SERVER)) - do_unbind(b); - return nil; - } - b->route[fd].src = fd; - return &b->route[fd].dest; + Bind *b; + int fd; + size_t len; + + for(b = bindings; b && strncmp(b->prefix, path, strlen(b->prefix)); + b = b->next); + + if(!b) { + File *f = ixp_open(s, path); + return f; + } + /* route to b */ + len = strlen(b->prefix); + fd = b->client->open(b->client, path[len] == 0 ? "/" : &path[len]); + if(b->client->errstr) { + set_error(s, b->client->errstr); + if(!strcmp(b->client->errstr, DEAD_SERVER)) + do_unbind(b); + return nil; + } + b->route[fd].src = fd; + return &b->route[fd].dest; } -static size_t fixp_read(IXPServer * s, int fd, size_t offset, void *out_buf, size_t out_buf_len) +static size_t +fixp_read(IXPServer * s, int fd, size_t offset, void *out_buf, + size_t out_buf_len) { - int cfd; - Bind *b = fd_to_bind(fd, &cfd); - size_t result; - - if (!b) { - result = ixp_read(s, fd, offset, out_buf, out_buf_len); - return result; - } - /* route to b */ - result = seek_read(b->client, cfd, offset, out_buf, out_buf_len); - if (b->client->errstr) { - set_error(s, b->client->errstr); - if (!strcmp(b->client->errstr, DEAD_SERVER)) - do_unbind(b); - } - return result; + int cfd; + Bind *b = fd_to_bind(fd, &cfd); + size_t result; + + if(!b) { + result = ixp_read(s, fd, offset, out_buf, out_buf_len); + return result; + } + /* route to b */ + result = seek_read(b->client, cfd, offset, out_buf, out_buf_len); + if(b->client->errstr) { + set_error(s, b->client->errstr); + if(!strcmp(b->client->errstr, DEAD_SERVER)) + do_unbind(b); + } + return result; } -static void fixp_write(IXPServer * s, int fd, size_t offset, void *content, size_t in_len) +static void +fixp_write(IXPServer * s, int fd, size_t offset, void *content, + size_t in_len) { - int cfd; - Bind *b = fd_to_bind(fd, &cfd); - - if (!b) { - ixp_write(s, fd, offset, content, in_len); - return; - } - /* route to b */ - seek_write(b->client, cfd, offset, content, in_len); - if (b->client->errstr) { - set_error(s, b->client->errstr); - if (!strcmp(b->client->errstr, DEAD_SERVER)) - do_unbind(b); - } + int cfd; + Bind *b = fd_to_bind(fd, &cfd); + + if(!b) { + ixp_write(s, fd, offset, content, in_len); + return; + } + /* route to b */ + seek_write(b->client, cfd, offset, content, in_len); + if(b->client->errstr) { + set_error(s, b->client->errstr); + if(!strcmp(b->client->errstr, DEAD_SERVER)) + do_unbind(b); + } } -static void fixp_close(IXPServer * s, int fd) +static void +fixp_close(IXPServer * s, int fd) { - int cfd; - Bind *b = fd_to_bind(fd, &cfd); - - if (!b) { - ixp_close(s, fd); - return; - } - /* route to b */ - b->client->close(b->client, cfd); - if (b->client->errstr) { - set_error(s, b->client->errstr); - if (!strcmp(b->client->errstr, DEAD_SERVER)) - do_unbind(b); - } + int cfd; + Bind *b = fd_to_bind(fd, &cfd); + + if(!b) { + ixp_close(s, fd); + return; + } + /* route to b */ + b->client->close(b->client, cfd); + if(b->client->errstr) { + set_error(s, b->client->errstr); + if(!strcmp(b->client->errstr, DEAD_SERVER)) + do_unbind(b); + } } -static void fixp_remove(IXPServer * s, char *path) +static void +fixp_remove(IXPServer * s, char *path) { - Bind *b; - size_t len; - - for (b = bindings; b && strncmp(b->prefix, path, strlen(b->prefix)); b = b->next); - - if (!b) { - ixp_remove(s, path); - return; - } - /* route to b */ - len = strlen(b->prefix); - b->client->remove(b->client, path[len] == 0 ? "/" : &path[len]); - if (b->client->errstr) { - set_error(s, b->client->errstr); - if (!strcmp(b->client->errstr, DEAD_SERVER)) - do_unbind(b); - } + Bind *b; + size_t len; + + for(b = bindings; b && strncmp(b->prefix, path, strlen(b->prefix)); + b = b->next); + + if(!b) { + ixp_remove(s, path); + return; + } + /* route to b */ + len = strlen(b->prefix); + b->client->remove(b->client, path[len] == 0 ? "/" : &path[len]); + if(b->client->errstr) { + set_error(s, b->client->errstr); + if(!strcmp(b->client->errstr, DEAD_SERVER)) + do_unbind(b); + } } -static void check_event(Connection * e) +static void +check_event(Connection * e) { - XEvent ev; - while (XPending(dpy)) { - /* - * wmiifs isn't interested in any X events, so just drop them - * all - */ - XNextEvent(dpy, &ev); - } - /* why check them? because X won't kill wmiifs when X dies */ + XEvent ev; + while(XPending(dpy)) { + /* + * wmiifs isn't interested in any X events, so just drop them + * all + */ + XNextEvent(dpy, &ev); + } + /* why check them? because X won't kill wmiifs when X dies */ } -static void run() +static void +run() { - if (!(files[F_CTL] = ixp_create(ixps, "/ctl"))) { - perror("wmiifs: cannot connect IXP server"); - exit(1); - } - files[F_CTL]->after_write = handle_after_write; - - /* routing functions */ - ixps->create = fixp_create; - ixps->remove = fixp_remove; - ixps->open = fixp_open; - ixps->close = fixp_close; - ixps->read = fixp_read; - ixps->write = fixp_write; - - /* main event loop */ - run_server_with_fd_support(ixps, ConnectionNumber(dpy), - check_event, 0); - - deinit_server(ixps); + if(!(files[F_CTL] = ixp_create(ixps, "/ctl"))) { + perror("wmiifs: cannot connect IXP server"); + exit(1); + } + files[F_CTL]->after_write = handle_after_write; + + /* routing functions */ + ixps->create = fixp_create; + ixps->remove = fixp_remove; + ixps->open = fixp_open; + ixps->close = fixp_close; + ixps->read = fixp_read; + ixps->write = fixp_write; + + /* main event loop */ + run_server_with_fd_support(ixps, ConnectionNumber(dpy), + check_event, 0); + + deinit_server(ixps); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i; - - /* 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; - } - } - - if (!getenv("HOME")) { - fprintf(stderr, "%s", - "wmiifs: $HOME environment variable is not set\n"); - usage(); - } - /* just for the case X crashes/gets quit */ - dpy = XOpenDisplay(0); - if (!dpy) { - fprintf(stderr, "%s", "wmiifs: cannot open display\n"); - exit(1); - } - ixps = wmii_setup_server(sockfile); - - run(); - - return 0; + int i; + + /* 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; + } + } + + if(!getenv("HOME")) { + fprintf(stderr, "%s", + "wmiifs: $HOME environment variable is not set\n"); + usage(); + } + /* just for the case X crashes/gets quit */ + dpy = XOpenDisplay(0); + if(!dpy) { + fprintf(stderr, "%s", "wmiifs: cannot open display\n"); + exit(1); + } + ixps = wmii_setup_server(sockfile); + + run(); + + return 0; } diff --git a/cmd/wmiikeys.c b/cmd/wmiikeys.c @@ -18,25 +18,25 @@ /* array indexes for file pointers */ typedef enum { - K_CTL, - K_LOOKUP, - K_GRAB_KB, - K_FONT, - K_FG_COLOR, - K_BG_COLOR, - K_BORDER_COLOR, - K_LAST + K_CTL, + K_LOOKUP, + K_GRAB_KB, + K_FONT, + K_FG_COLOR, + K_BG_COLOR, + K_BORDER_COLOR, + K_LAST } KeyIndexes; typedef struct Shortcut Shortcut; struct Shortcut { - char name[MAX_BUF]; - unsigned long mod; - KeyCode key; - File *cmdfile; - Shortcut *snext; - Shortcut *next; + char name[MAX_BUF]; + unsigned long mod; + KeyCode key; + File *cmdfile; + Shortcut *snext; + Shortcut *next; }; static IXPServer *ixps = nil; @@ -61,229 +61,247 @@ static void draw_shortcut_box(char *text); static void quit(void *obj, char *arg); static Action acttbl[] = { - {"quit", quit}, - {0, 0} + {"quit", quit}, + {0, 0} }; static char *version[] = { - "wmiikeys - window manager improved keys - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiikeys - window manager improved keys - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, "%s", - "usage: wmiikeys [-s <socket file>] [-v]\n" - " -s socket file (default: /tmp/.ixp-$USER/wmiikeys-$WMII_IDENT)\n" - " -v version info\n"); - exit(1); + fprintf(stderr, "%s", + "usage: wmiikeys [-s <socket file>] [-v]\n" + " -s socket file (default: /tmp/.ixp-$USER/wmiikeys-$WMII_IDENT)\n" + " -v version info\n"); + exit(1); } -static void center() +static void +center() { - krect.x = rect.width / 2 - krect.width / 2; - krect.y = rect.height / 2 - krect.height / 2; + krect.x = rect.width / 2 - krect.width / 2; + krect.y = rect.height / 2 - krect.height / 2; } /* grabs shortcut on all screens */ -static void grab_shortcut(Shortcut * s) +static void +grab_shortcut(Shortcut * s) { - XGrabKey(dpy, s->key, s->mod, root, - True, GrabModeAsync, GrabModeAsync); - if (num_lock_mask) { - XGrabKey(dpy, s->key, s->mod | num_lock_mask, root, - True, GrabModeAsync, GrabModeAsync); - XGrabKey(dpy, s->key, s->mod | num_lock_mask | LockMask, root, - True, GrabModeAsync, GrabModeAsync); - } - XSync(dpy, False); + XGrabKey(dpy, s->key, s->mod, root, + True, GrabModeAsync, GrabModeAsync); + if(num_lock_mask) { + XGrabKey(dpy, s->key, s->mod | num_lock_mask, root, + True, GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, s->key, s->mod | num_lock_mask | LockMask, root, + True, GrabModeAsync, GrabModeAsync); + } + XSync(dpy, False); } /* * don't handle evil keys anymore, just define more shortcuts if you cannot * live without evil key handling */ -static void ungrab_shortcut(Shortcut * s) +static void +ungrab_shortcut(Shortcut * s) { - XUngrabKey(dpy, s->key, s->mod, root); - if (num_lock_mask) { - XUngrabKey(dpy, s->key, s->mod | num_lock_mask, root); - XUngrabKey(dpy, s->key, s->mod | num_lock_mask | LockMask, root); - } - XSync(dpy, False); + XUngrabKey(dpy, s->key, s->mod, root); + if(num_lock_mask) { + XUngrabKey(dpy, s->key, s->mod | num_lock_mask, root); + XUngrabKey(dpy, s->key, s->mod | num_lock_mask | LockMask, root); + } + XSync(dpy, False); } -static void create_shortcut(File * f) +static void +create_shortcut(File * f) { - static char *chain[8]; - char *k; - size_t i, toks; - Shortcut *s = 0, *r = 0; - - cext_strlcpy(buf, f->name, sizeof(buf)); - toks = cext_tokenize(chain, 8, buf, ','); - - for (i = 0; i < toks; i++) { - if (!s) - r = s = cext_emallocz(sizeof(Shortcut)); - else { - s->snext = cext_emallocz(sizeof(Shortcut)); - s = s->snext; - } - cext_strlcpy(s->name, chain[i], MAX_BUF); - k = strrchr(chain[i], '-'); - if (k) - k++; - else - k = chain[i]; - s->key = XKeysymToKeycode(dpy, XStringToKeysym(k)); - s->mod = blitz_strtomod(chain[i]); - } - if (r) { - s->cmdfile = f; - if (!shortcuts) - shortcuts = r; - else { - for (s = shortcuts; s && s->next; s = s->next); - s->next = r; - } - grab_shortcut(r); - } + static char *chain[8]; + char *k; + size_t i, toks; + Shortcut *s = 0, *r = 0; + + cext_strlcpy(buf, f->name, sizeof(buf)); + toks = cext_tokenize(chain, 8, buf, ','); + + for(i = 0; i < toks; i++) { + if(!s) + r = s = cext_emallocz(sizeof(Shortcut)); + else { + s->snext = cext_emallocz(sizeof(Shortcut)); + s = s->snext; + } + cext_strlcpy(s->name, chain[i], MAX_BUF); + k = strrchr(chain[i], '-'); + if(k) + k++; + else + k = chain[i]; + s->key = XKeysymToKeycode(dpy, XStringToKeysym(k)); + s->mod = blitz_strtomod(chain[i]); + } + if(r) { + s->cmdfile = f; + if(!shortcuts) + shortcuts = r; + else { + for(s = shortcuts; s && s->next; s = s->next); + s->next = r; + } + grab_shortcut(r); + } } -static void destroy_shortcut(Shortcut * s, int ungrab) +static void +destroy_shortcut(Shortcut * s, int ungrab) { - if (s->snext) - destroy_shortcut(s->snext, 0); - if (ungrab) - ungrab_shortcut(s); - free(s); + if(s->snext) + destroy_shortcut(s->snext, 0); + if(ungrab) + ungrab_shortcut(s); + free(s); } -static void next_keystroke(unsigned long *mod, KeyCode * key) +static void +next_keystroke(unsigned long *mod, KeyCode * key) { - XEvent e; - KeySym sym; - *mod = 0; - do { - XMaskEvent(dpy, KeyPressMask, &e); - *mod |= e.xkey.state & valid_mask; - *key = (KeyCode) e.xkey.keycode; - sym = XKeycodeToKeysym(dpy, e.xkey.keycode, 0); - } while (IsModifierKey(sym)); + XEvent e; + KeySym sym; + *mod = 0; + do { + XMaskEvent(dpy, KeyPressMask, &e); + *mod |= e.xkey.state & valid_mask; + *key = (KeyCode) e.xkey.keycode; + sym = XKeycodeToKeysym(dpy, e.xkey.keycode, 0); + } while(IsModifierKey(sym)); } -static void emulate_key_press(unsigned long mod, KeyCode key) +static void +emulate_key_press(unsigned long mod, KeyCode key) { - XEvent e; - Window client_win; - int revert; - - XGetInputFocus(dpy, &client_win, &revert); - - e.xkey.type = KeyPress; - e.xkey.time = CurrentTime; - e.xkey.window = client_win; - e.xkey.display = dpy; - e.xkey.state = mod; - e.xkey.keycode = key; - XSendEvent(dpy, client_win, True, KeyPressMask, &e); - e.xkey.type = KeyRelease; - XSendEvent(dpy, client_win, True, KeyReleaseMask, &e); - XSync(dpy, False); + XEvent e; + Window client_win; + int revert; + + XGetInputFocus(dpy, &client_win, &revert); + + e.xkey.type = KeyPress; + e.xkey.time = CurrentTime; + e.xkey.window = client_win; + e.xkey.display = dpy; + e.xkey.state = mod; + e.xkey.keycode = key; + XSendEvent(dpy, client_win, True, KeyPressMask, &e); + e.xkey.type = KeyRelease; + XSendEvent(dpy, client_win, True, KeyReleaseMask, &e); + XSync(dpy, False); } -static void handle_shortcut_chain(Window w, Shortcut * processed, char *prefix, int grab) +static void +handle_shortcut_chain(Window w, Shortcut * processed, char *prefix, + int grab) { - unsigned long mod; - KeyCode key; - Shortcut *s = processed->snext; - - if (grab) { - XGrabKeyboard(dpy, w, True, GrabModeAsync, GrabModeAsync, CurrentTime); - XMapRaised(dpy, win); - } - draw_shortcut_box(prefix); - next_keystroke(&mod, &key); - - if ((processed->mod == mod) && (processed->key == key)) { - /* double shortcut */ - emulate_key_press(mod, key); - } else if ((s->mod == mod) && (s->key == key)) { - if (s->cmdfile && s->cmdfile->content) - wmii_spawn(dpy, s->cmdfile->content); - else if (s->snext) { - snprintf(buf, sizeof(buf), "%s/%s", prefix, s->name); - handle_shortcut_chain(w, s, buf, 0); - } - } - if (grab) { - XUngrabKeyboard(dpy, CurrentTime); - XUnmapWindow(dpy, win); - XSync(dpy, False); - } + unsigned long mod; + KeyCode key; + Shortcut *s = processed->snext; + + if(grab) { + XGrabKeyboard(dpy, w, True, GrabModeAsync, GrabModeAsync, + CurrentTime); + XMapRaised(dpy, win); + } + draw_shortcut_box(prefix); + next_keystroke(&mod, &key); + + if((processed->mod == mod) && (processed->key == key)) { + /* double shortcut */ + emulate_key_press(mod, key); + } else if((s->mod == mod) && (s->key == key)) { + if(s->cmdfile && s->cmdfile->content) + wmii_spawn(dpy, s->cmdfile->content); + else if(s->snext) { + snprintf(buf, sizeof(buf), "%s/%s", prefix, s->name); + handle_shortcut_chain(w, s, buf, 0); + } + } + if(grab) { + XUngrabKeyboard(dpy, CurrentTime); + XUnmapWindow(dpy, win); + XSync(dpy, False); + } } -static void handle_shortcut_gkb(Window w, unsigned long mod, KeyCode key) +static void +handle_shortcut_gkb(Window w, unsigned long mod, KeyCode key) { - Shortcut *s; - if (!files[K_LOOKUP]->content) - return; - - for (s = shortcuts; s && ((s->mod != mod) || (s->key != key)); s = s->next); - if (s && s->cmdfile && s->cmdfile->content) { - wmii_spawn(dpy, s->cmdfile->content); - return; - } - XBell(dpy, 0); + Shortcut *s; + if(!files[K_LOOKUP]->content) + return; + + for(s = shortcuts; s && ((s->mod != mod) || (s->key != key)); + s = s->next); + if(s && s->cmdfile && s->cmdfile->content) { + wmii_spawn(dpy, s->cmdfile->content); + return; + } + XBell(dpy, 0); } -static void handle_shortcut(Window w, unsigned long mod, KeyCode key) +static void +handle_shortcut(Window w, unsigned long mod, KeyCode key) { - Shortcut *s; - if (!files[K_LOOKUP]->content) - return; - - for (s = shortcuts; s && ((s->mod != mod) || (s->key != key)); s = s->next); - if (s && s->cmdfile && s->cmdfile->content) { - wmii_spawn(dpy, s->cmdfile->content); - return; - } - if (s && s->snext) - handle_shortcut_chain(w, s, s->name, 1); + Shortcut *s; + if(!files[K_LOOKUP]->content) + return; + + for(s = shortcuts; s && ((s->mod != mod) || (s->key != key)); + s = s->next); + if(s && s->cmdfile && s->cmdfile->content) { + wmii_spawn(dpy, s->cmdfile->content); + return; + } + if(s && s->snext) + handle_shortcut_chain(w, s, s->name, 1); } -static void quit(void *obj, char *arg) +static void +quit(void *obj, char *arg) { - ixps->runlevel = SHUTDOWN; + ixps->runlevel = SHUTDOWN; } -static void update() +static void +update() { - Shortcut *s; - File *f, *p; - if (!files[K_LOOKUP]->content) - return; - - f = ixp_walk(ixps, files[K_LOOKUP]->content); - - if (!f || !is_directory(f)) - return; /* cannot update */ - - /* destroy existing shortcuts if any */ - while ((s = shortcuts)) { - shortcuts = shortcuts->next; - destroy_shortcut(s, 1); - } - shortcuts = nil; - - if (grabkb) { - XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime); - return; - } - /* create new shortcuts */ - for (p = f->content; p; p = p->next) - create_shortcut(p); + Shortcut *s; + File *f, *p; + if(!files[K_LOOKUP]->content) + return; + + f = ixp_walk(ixps, files[K_LOOKUP]->content); + + if(!f || !is_directory(f)) + return; /* cannot update */ + + /* destroy existing shortcuts if any */ + while((s = shortcuts)) { + shortcuts = shortcuts->next; + destroy_shortcut(s, 1); + } + shortcuts = nil; + + if(grabkb) { + XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, + CurrentTime); + return; + } + /* create new shortcuts */ + for(p = f->content; p; p = p->next) + create_shortcut(p); } /* @@ -294,181 +312,195 @@ static void update() * /box/style/bgcolor "#RRGGBBAA" * /box/style/bordercolor "#RRGGBBAA" */ -static void draw_shortcut_box(char *text) +static void +draw_shortcut_box(char *text) { - Draw d = { 0 }; - - d.font = font; - krect.width = XTextWidth(d.font, text, strlen(text)) + krect.height; - krect.height = font->ascent + font->descent + 4; - center(); - XMoveResizeWindow(dpy, win, krect.x, krect.y, krect.width, krect.height); - - /* default stuff */ - d.gc = gc; - d.drawable = win; - d.data = text; - d.rect.y = 0; - d.rect.width = krect.width; - d.rect.height = krect.height; - d.bg = blitz_loadcolor(dpy, screen_num, files[K_BG_COLOR]->content); - d.fg = blitz_loadcolor(dpy, screen_num, files[K_FG_COLOR]->content); - d.border = blitz_loadcolor(dpy, screen_num, files[K_BORDER_COLOR]->content); - blitz_drawlabel(dpy, &d); + Draw d = { 0 }; + + d.font = font; + krect.width = XTextWidth(d.font, text, strlen(text)) + krect.height; + krect.height = font->ascent + font->descent + 4; + center(); + XMoveResizeWindow(dpy, win, krect.x, krect.y, krect.width, + krect.height); + + /* default stuff */ + d.gc = gc; + d.drawable = win; + d.data = text; + d.rect.y = 0; + d.rect.width = krect.width; + d.rect.height = krect.height; + d.bg = blitz_loadcolor(dpy, screen_num, files[K_BG_COLOR]->content); + d.fg = blitz_loadcolor(dpy, screen_num, files[K_FG_COLOR]->content); + d.border = + blitz_loadcolor(dpy, screen_num, files[K_BORDER_COLOR]->content); + blitz_drawlabel(dpy, &d); } -static void check_event(Connection * c) +static void +check_event(Connection * c) { - XEvent ev; - - while (XPending(dpy)) { - XNextEvent(dpy, &ev); - switch (ev.type) { - case KeyPress: - ev.xkey.state &= valid_mask; - if (grabkb) - handle_shortcut_gkb(root, ev.xkey.state, - (KeyCode) ev.xkey.keycode); - else - handle_shortcut(root, ev.xkey.state, - (KeyCode) ev.xkey.keycode); - break; - case KeymapNotify: - update(); - break; - default: - break; - } - } + XEvent ev; + + while(XPending(dpy)) { + XNextEvent(dpy, &ev); + switch (ev.type) { + case KeyPress: + ev.xkey.state &= valid_mask; + if(grabkb) + handle_shortcut_gkb(root, ev.xkey.state, + (KeyCode) ev.xkey.keycode); + else + handle_shortcut(root, ev.xkey.state, + (KeyCode) ev.xkey.keycode); + break; + case KeymapNotify: + update(); + break; + default: + break; + } + } } -static void handle_after_write(IXPServer * s, File * f) +static void +handle_after_write(IXPServer * s, File * f) { - int i; - size_t len; - - if (f == files[K_CTL]) { - 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 (f == files[K_GRAB_KB]) { - grabkb = blitz_strtonum(files[K_GRAB_KB]->content, 0, 1); - if (!grabkb) { - XUngrabKeyboard(dpy, CurrentTime); - XUnmapWindow(dpy, win); - XSync(dpy, False); - } else - update(); - } else if (f == files[K_FONT]) { - XFreeFont(dpy, font); - font = blitz_getfont(dpy, files[K_FONT]->content); - krect = rect; - krect.height = font->ascent + font->descent + 4; - center(); - XMoveResizeWindow(dpy, win, krect.x, krect.y, krect.width, krect.height); - } else if (f == files[K_LOOKUP]) { - update(); - } - check_event(0); + int i; + size_t len; + + if(f == files[K_CTL]) { + 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(f == files[K_GRAB_KB]) { + grabkb = blitz_strtonum(files[K_GRAB_KB]->content, 0, 1); + if(!grabkb) { + XUngrabKeyboard(dpy, CurrentTime); + XUnmapWindow(dpy, win); + XSync(dpy, False); + } else + update(); + } else if(f == files[K_FONT]) { + XFreeFont(dpy, font); + font = blitz_getfont(dpy, files[K_FONT]->content); + krect = rect; + krect.height = font->ascent + font->descent + 4; + center(); + XMoveResizeWindow(dpy, win, krect.x, krect.y, krect.width, + krect.height); + } else if(f == files[K_LOOKUP]) { + update(); + } + check_event(0); } -static int dummy_error_handler(Display * dpy, XErrorEvent * err) +static int +dummy_error_handler(Display * dpy, XErrorEvent * err) { - return 0; + return 0; } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i; - XSetWindowAttributes wa; - XGCValues gcv; - - /* 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", "wmiikeys: cannot open display\n"); - exit(1); - } - XSetErrorHandler(dummy_error_handler); - screen_num = DefaultScreen(dpy); - - /* init */ - ixps = wmii_setup_server(sockfile); - - if (!(files[K_CTL] = ixp_create(ixps, "/ctl"))) { - perror("wmiikeys: cannot connect IXP server"); - exit(1); - } - files[K_CTL]->after_write = handle_after_write; - files[K_LOOKUP] = ixp_create(ixps, "/lookup"); - files[K_LOOKUP]->after_write = handle_after_write; - files[K_GRAB_KB] = wmii_create_ixpfile(ixps, "/grabkeyb", "0"); - files[K_GRAB_KB]->after_write = handle_after_write; - files[K_FONT] = wmii_create_ixpfile(ixps, "/box/font", BLITZ_FONT); - files[K_FONT]->after_write = handle_after_write; - font = blitz_getfont(dpy, files[K_FONT]->content); - files[K_FG_COLOR] = wmii_create_ixpfile(ixps, "/box/fgcolor", BLITZ_SEL_FG_COLOR); - files[K_BG_COLOR] = wmii_create_ixpfile(ixps, "/box/bgcolor", BLITZ_SEL_BG_COLOR); - files[K_BORDER_COLOR] = wmii_create_ixpfile(ixps, "/box/bordercolor", BLITZ_SEL_BORDER_COLOR); - - wa.override_redirect = 1; - wa.background_pixmap = ParentRelative; - wa.event_mask = - ExposureMask | SubstructureRedirectMask | SubstructureNotifyMask; - - root = RootWindow(dpy, screen_num); - rect.x = rect.y = 0; - rect.width = DisplayWidth(dpy, screen_num); - rect.height = DisplayHeight(dpy, screen_num); - krect.x = krect.y = 0; - krect.width = krect.height = 1; - - wmii_init_lock_modifiers(dpy, &valid_mask, &num_lock_mask); - - win = XCreateWindow(dpy, RootWindow(dpy, screen_num), krect.x, krect.y, - krect.width, krect.height, 0, DefaultDepth(dpy, screen_num), - CopyFromParent, DefaultVisual(dpy, screen_num), - CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_left_ptr)); - XSync(dpy, False); - - gcv.function = GXcopy; - gcv.graphics_exposures = False; - - gc = XCreateGC(dpy, win, 0, 0); - - /* main event loop */ - run_server_with_fd_support(ixps, ConnectionNumber(dpy), - check_event, 0); - deinit_server(ixps); - XFreeGC(dpy, gc); - XCloseDisplay(dpy); - - return 0; + int i; + XSetWindowAttributes wa; + XGCValues gcv; + + /* 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", "wmiikeys: cannot open display\n"); + exit(1); + } + XSetErrorHandler(dummy_error_handler); + screen_num = DefaultScreen(dpy); + + /* init */ + ixps = wmii_setup_server(sockfile); + + if(!(files[K_CTL] = ixp_create(ixps, "/ctl"))) { + perror("wmiikeys: cannot connect IXP server"); + exit(1); + } + files[K_CTL]->after_write = handle_after_write; + files[K_LOOKUP] = ixp_create(ixps, "/lookup"); + files[K_LOOKUP]->after_write = handle_after_write; + files[K_GRAB_KB] = wmii_create_ixpfile(ixps, "/grabkeyb", "0"); + files[K_GRAB_KB]->after_write = handle_after_write; + files[K_FONT] = wmii_create_ixpfile(ixps, "/box/font", BLITZ_FONT); + files[K_FONT]->after_write = handle_after_write; + font = blitz_getfont(dpy, files[K_FONT]->content); + files[K_FG_COLOR] = + wmii_create_ixpfile(ixps, "/box/fgcolor", BLITZ_SEL_FG_COLOR); + files[K_BG_COLOR] = + wmii_create_ixpfile(ixps, "/box/bgcolor", BLITZ_SEL_BG_COLOR); + files[K_BORDER_COLOR] = + wmii_create_ixpfile(ixps, "/box/bordercolor", + BLITZ_SEL_BORDER_COLOR); + + wa.override_redirect = 1; + wa.background_pixmap = ParentRelative; + wa.event_mask = + ExposureMask | SubstructureRedirectMask | SubstructureNotifyMask; + + root = RootWindow(dpy, screen_num); + rect.x = rect.y = 0; + rect.width = DisplayWidth(dpy, screen_num); + rect.height = DisplayHeight(dpy, screen_num); + krect.x = krect.y = 0; + krect.width = krect.height = 1; + + wmii_init_lock_modifiers(dpy, &valid_mask, &num_lock_mask); + + win = XCreateWindow(dpy, RootWindow(dpy, screen_num), krect.x, krect.y, + krect.width, krect.height, 0, DefaultDepth(dpy, + screen_num), + CopyFromParent, DefaultVisual(dpy, screen_num), + CWOverrideRedirect | CWBackPixmap | CWEventMask, + &wa); + XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_left_ptr)); + XSync(dpy, False); + + gcv.function = GXcopy; + gcv.graphics_exposures = False; + + gc = XCreateGC(dpy, win, 0, 0); + + /* main event loop */ + run_server_with_fd_support(ixps, ConnectionNumber(dpy), + check_event, 0); + deinit_server(ixps); + XFreeGC(dpy, gc); + XCloseDisplay(dpy); + + return 0; } diff --git a/cmd/wmiimenu.c b/cmd/wmiimenu.c @@ -20,31 +20,31 @@ /* array indexes for file pointers */ typedef enum { - M_CTL, - M_GEOMETRY, - M_PRE_COMMAND, - M_COMMAND, - M_HISTORY, - M_LOOKUP, - M_FONT, - M_SEL_BG_COLOR, - M_SEL_TEXT_COLOR, - M_SEL_BORDER_COLOR, - M_NORM_BG_COLOR, - M_NORM_TEXT_COLOR, - M_NORM_BORDER_COLOR, - M_LAST + M_CTL, + M_GEOMETRY, + M_PRE_COMMAND, + M_COMMAND, + M_HISTORY, + M_LOOKUP, + M_FONT, + M_SEL_BG_COLOR, + M_SEL_TEXT_COLOR, + M_SEL_BORDER_COLOR, + M_NORM_BG_COLOR, + M_NORM_TEXT_COLOR, + M_NORM_BORDER_COLOR, + M_LAST } InputIndexes; enum { - OFF_NEXT, OFF_PREV, OFF_CURR, OFF_LAST + OFF_NEXT, OFF_PREV, OFF_CURR, OFF_LAST }; typedef struct Item Item; struct Item { - File *file; - Item *next; - Item *prev; + File *file; + Item *next; + Item *prev; }; static IXPServer *ixps = 0; @@ -64,7 +64,7 @@ static Item *history = nil; static Item *offset[OFF_LAST]; static unsigned int cmdw = 0; static Pixmap pmap; -static const int seek = 30; /* 30px */ +static const int seek = 30; /* 30px */ static XFontStruct *font; static Align align = SOUTH; @@ -77,584 +77,646 @@ static void display(void *obj, char *arg); static size_t update_items(char *prefix); static Action acttbl[2] = { - {"quit", quit}, - {"display", display} + {"quit", quit}, + {"display", display} }; static char *version[] = { - "wmiimenu - window manager improved menu - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiimenu - window manager improved menu - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, "%s", - "usage: wmiimenu [-s <socket file>] [-r] [-v]\n" - " -s socket file (default: /tmp/.ixp-$USER/wmiimenu-%s-%s)\n" - " -v version info\n"); - exit(1); + fprintf(stderr, "%s", + "usage: wmiimenu [-s <socket file>] [-r] [-v]\n" + " -s socket file (default: /tmp/.ixp-$USER/wmiimenu-%s-%s)\n" + " -v version info\n"); + exit(1); } -static void add_history(char *cmd) +static void +add_history(char *cmd) { - Item *h, *new; - char buf[MAX_BUF]; - snprintf(buf, MAX_BUF, "/history/%ld", (long) time(0)); - - new = cext_emallocz(sizeof(Item)); - new->file = wmii_create_ixpfile(ixps, buf, cmd); - for (h = history; h && h->next; h = h->next); - if (!h) - history = new; - else { - h->next = new; - new->prev = h; - } - selhist = new; + Item *h, *new; + char buf[MAX_BUF]; + snprintf(buf, MAX_BUF, "/history/%ld", (long) time(0)); + + new = cext_emallocz(sizeof(Item)); + new->file = wmii_create_ixpfile(ixps, buf, cmd); + for(h = history; h && h->next; h = h->next); + if(!h) + history = new; + else { + h->next = new; + new->prev = h; + } + selhist = new; } -static void exec_item(char *cmd) +static void +exec_item(char *cmd) { - char *rc = cmd; - - if (!cmd || cmd[0] == 0) - return; - - if (sel && sel->file->size) - rc = cmd = sel->file->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); - snprintf(rc, len, "%s %s", (char *) files[M_PRE_COMMAND]->content, cmd); - } - /* fallback */ - wmii_spawn(dpy, rc); - /* cleanup */ - if (files[M_PRE_COMMAND]->content) - free(rc); + char *rc = cmd; + + if(!cmd || cmd[0] == 0) + return; + + if(sel && sel->file->size) + rc = cmd = sel->file->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); + snprintf(rc, len, "%s %s", (char *) files[M_PRE_COMMAND]->content, + cmd); + } + /* fallback */ + wmii_spawn(dpy, rc); + /* cleanup */ + if(files[M_PRE_COMMAND]->content) + free(rc); } -static void quit(void *obj, char *arg) +static void +quit(void *obj, char *arg) { - ixps->runlevel = SHUTDOWN; + ixps->runlevel = SHUTDOWN; } -static void show() +static void +show() { - set_text(0); - XMapRaised(dpy, win); - XSync(dpy, False); - update_items(files[M_COMMAND]->content); - draw_menu(); - while (XGrabKeyboard - (dpy, RootWindow(dpy, screen_num), True, GrabModeAsync, - GrabModeAsync, CurrentTime) != GrabSuccess) - usleep(1000); + set_text(0); + XMapRaised(dpy, win); + XSync(dpy, False); + update_items(files[M_COMMAND]->content); + draw_menu(); + while(XGrabKeyboard + (dpy, RootWindow(dpy, screen_num), True, GrabModeAsync, + GrabModeAsync, CurrentTime) != GrabSuccess) + usleep(1000); } -static void hide() +static void +hide() { - XUngrabKeyboard(dpy, CurrentTime); - XUnmapWindow(dpy, win); - XSync(dpy, False); + XUngrabKeyboard(dpy, CurrentTime); + XUnmapWindow(dpy, win); + XSync(dpy, False); } -static void display(void *obj, char *arg) +static void +display(void *obj, char *arg) { - if (!arg) - return; - if (blitz_strtonum(arg, 0, 1)) - show(); - else - hide(); - check_event(0); + if(!arg) + return; + if(blitz_strtonum(arg, 0, 1)) + show(); + else + hide(); + check_event(0); } -void set_text(char *text) +void +set_text(char *text) { - if (files[M_COMMAND]->content) { - free(files[M_COMMAND]->content); - files[M_COMMAND]->content = 0; - files[M_COMMAND]->size = 0; - } - if (text && strlen(text)) { - files[M_COMMAND]->content = strdup(text); - files[M_COMMAND]->size = strlen(text); - } + if(files[M_COMMAND]->content) { + free(files[M_COMMAND]->content); + files[M_COMMAND]->content = 0; + files[M_COMMAND]->size = 0; + } + if(text && strlen(text)) { + files[M_COMMAND]->content = strdup(text); + files[M_COMMAND]->size = strlen(text); + } } -static void update_offsets() +static void +update_offsets() { - Item *i; - unsigned int w = cmdw + 2 * seek; - - if (!i) - return; - - /* calc next offset */ - for (i = offset[OFF_CURR]; i; i = i->next) { - w += XTextWidth(font, i->file->content, strlen(i->file->content)) + mrect.height; - if (w > mrect.width) - break; - } - offset[OFF_NEXT] = i; - - w = cmdw + 2 * seek; - for (i = offset[OFF_CURR]->prev; i && i->prev; i = i->prev) { - w += XTextWidth(font, i->file->content, strlen(i->file->content)) + mrect.height; - if (w > mrect.width) - break; - } - offset[OFF_PREV] = i; + Item *i; + unsigned int w = cmdw + 2 * seek; + + if(!i) + return; + + /* calc next offset */ + for(i = offset[OFF_CURR]; i; i = i->next) { + w += XTextWidth(font, i->file->content, + strlen(i->file->content)) + mrect.height; + if(w > mrect.width) + break; + } + offset[OFF_NEXT] = i; + + w = cmdw + 2 * seek; + for(i = offset[OFF_CURR]->prev; i && i->prev; i = i->prev) { + w += XTextWidth(font, i->file->content, + strlen(i->file->content)) + mrect.height; + if(w > mrect.width) + break; + } + offset[OFF_PREV] = i; } -static size_t update_items(char *pattern) +static size_t +update_items(char *pattern) { - size_t plen = pattern ? strlen(pattern) : 0, len, max = 0; - int matched = pattern ? plen == 0 : 1; - File *f, *p, *maxitem; - Item *i, *new; - - if (!files[M_LOOKUP]->content) - return 0; - f = ixp_walk(ixps, files[M_LOOKUP]->content); - if (!f || !is_directory(f)) - return 0; - - cmdw = 0; - offset[OFF_CURR] = offset[OFF_PREV] = offset[OFF_NEXT] = nil; - - while ((i = items)) { - items = items->next; - free(i); - } - sel = items = nil; - nitems = 0; - - /* build new items */ - for (p = f->content; p; p = p->next) { - len = strlen(p->name); - if (max < len) { - maxitem = p; - max = len; - } - } - - if (maxitem) - cmdw = XTextWidth(font, maxitem->name, max) + mrect.height; - - for (p = f->content; p; p = p->next) { - if (matched || !strncmp(pattern, p->name, plen)) { - new = cext_emallocz(sizeof(Item)); - new->file = p; - nitems++; - if (!items) - offset[OFF_CURR] = sel = items = i = new; - else { - i->next = new; - new->prev = i; - i = new; - } - p->parent = 0; /* HACK to prevent doubled items */ - } - } - - for (p = f->content; p; p = p->next) { - if (p->parent && strstr(p->name, pattern)) { - new = cext_emallocz(sizeof(Item)); - new->file = p; - nitems++; - if (!items) - offset[OFF_CURR] = sel = items = i = new; - else { - i->next = new; - new->prev = i; - i = new; - } - } - else - p->parent = f; /* restore HACK */ - } - - update_offsets(); - return nitems; + size_t plen = pattern ? strlen(pattern) : 0, len, max = 0; + int matched = pattern ? plen == 0 : 1; + File *f, *p, *maxitem; + Item *i, *new; + + if(!files[M_LOOKUP]->content) + return 0; + f = ixp_walk(ixps, files[M_LOOKUP]->content); + if(!f || !is_directory(f)) + return 0; + + cmdw = 0; + offset[OFF_CURR] = offset[OFF_PREV] = offset[OFF_NEXT] = nil; + + while((i = items)) { + items = items->next; + free(i); + } + sel = items = nil; + nitems = 0; + + /* build new items */ + for(p = f->content; p; p = p->next) { + len = strlen(p->name); + if(max < len) { + maxitem = p; + max = len; + } + } + + if(maxitem) + cmdw = XTextWidth(font, maxitem->name, max) + mrect.height; + + for(p = f->content; p; p = p->next) { + if(matched || !strncmp(pattern, p->name, plen)) { + new = cext_emallocz(sizeof(Item)); + new->file = p; + nitems++; + if(!items) + offset[OFF_CURR] = sel = items = i = new; + else { + i->next = new; + new->prev = i; + i = new; + } + p->parent = 0; /* HACK to prevent doubled items */ + } + } + + for(p = f->content; p; p = p->next) { + if(p->parent && strstr(p->name, pattern)) { + new = cext_emallocz(sizeof(Item)); + new->file = p; + nitems++; + if(!items) + offset[OFF_CURR] = sel = items = i = new; + else { + i->next = new; + new->prev = i; + i = new; + } + } else + p->parent = f; /* restore HACK */ + } + + update_offsets(); + return nitems; } /* creates draw structs for menu mode drawing */ -static void draw_menu() +static void +draw_menu() { - Draw d = { 0 }; - unsigned int offx = 0; - Item *i = sel; - - d.gc = gc; - d.font = font; - d.drawable = pmap; - d.rect = mrect; - d.rect.x = 0; - d.rect.y = 0; - d.bg = blitz_loadcolor(dpy, screen_num, files[M_NORM_BG_COLOR]->content); - d.border = blitz_loadcolor(dpy, screen_num, files[M_NORM_BORDER_COLOR]->content); - blitz_drawlabelnoborder(dpy, &d); - - /* print command */ - d.align = WEST; - d.font = font; - d.fg = blitz_loadcolor(dpy, screen_num, files[M_NORM_TEXT_COLOR]->content); - d.data = files[M_COMMAND]->content; - if (cmdw && sel) - d.rect.width = cmdw; - offx += d.rect.width; - blitz_drawlabelnoborder(dpy, &d); - - d.align = CENTER; - if (sel) { - 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_PREV] ? "<" : nil; - d.rect.x = offx; - d.rect.width = seek; - offx += d.rect.width; - blitz_drawlabelnoborder(dpy, &d); - - /* determine maximum items */ - for (i = offset[OFF_CURR]; i && i != offset[OFF_NEXT]; i = i->next) { - d.data = i->file->name; - d.rect.x = offx; - d.rect.width = XTextWidth(d.font, d.data, strlen(d.data)) + mrect.height; - offx += d.rect.width; - /*fprintf(stderr, "%s (%d, %d, %d, %d)\n", item->name, d.rect.x, d.rect.y, d.rect.width, d.rect.height);*/ - if (sel == i) { - 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 { - 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.border = blitz_loadcolor(dpy, screen_num, files[M_NORM_BORDER_COLOR]->content); - blitz_drawlabelnoborder(dpy, &d); - } - } - - 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_NEXT] ? ">" : nil; - d.rect.x = mrect.width - seek; - d.rect.width = seek; - blitz_drawlabelnoborder(dpy, &d); - } - XCopyArea(dpy, pmap, win, gc, 0, 0, mrect.width, mrect.height, 0, 0); - XSync(dpy, False); + Draw d = { 0 }; + unsigned int offx = 0; + Item *i = sel; + + d.gc = gc; + d.font = font; + d.drawable = pmap; + d.rect = mrect; + d.rect.x = 0; + d.rect.y = 0; + d.bg = + blitz_loadcolor(dpy, screen_num, files[M_NORM_BG_COLOR]->content); + d.border = + blitz_loadcolor(dpy, screen_num, + files[M_NORM_BORDER_COLOR]->content); + blitz_drawlabelnoborder(dpy, &d); + + /* print command */ + d.align = WEST; + d.font = font; + d.fg = + blitz_loadcolor(dpy, screen_num, + files[M_NORM_TEXT_COLOR]->content); + d.data = files[M_COMMAND]->content; + if(cmdw && sel) + d.rect.width = cmdw; + offx += d.rect.width; + blitz_drawlabelnoborder(dpy, &d); + + d.align = CENTER; + if(sel) { + 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_PREV] ? "<" : nil; + d.rect.x = offx; + d.rect.width = seek; + offx += d.rect.width; + blitz_drawlabelnoborder(dpy, &d); + + /* determine maximum items */ + for(i = offset[OFF_CURR]; i && i != offset[OFF_NEXT]; i = i->next) { + d.data = i->file->name; + d.rect.x = offx; + d.rect.width = + XTextWidth(d.font, d.data, strlen(d.data)) + mrect.height; + offx += d.rect.width; + /*fprintf(stderr, "%s (%d, %d, %d, %d)\n", item->name, d.rect.x, d.rect.y, d.rect.width, d.rect.height); */ + if(sel == i) { + 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 { + 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.border = + blitz_loadcolor(dpy, screen_num, + files[M_NORM_BORDER_COLOR]->content); + blitz_drawlabelnoborder(dpy, &d); + } + } + + 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_NEXT] ? ">" : nil; + d.rect.x = mrect.width - seek; + d.rect.width = seek; + blitz_drawlabelnoborder(dpy, &d); + } + XCopyArea(dpy, pmap, win, gc, 0, 0, mrect.width, mrect.height, 0, 0); + XSync(dpy, False); } -static void handle_kpress(XKeyEvent * e) +static void +handle_kpress(XKeyEvent * e) { - KeySym ksym; - char buf[32]; - int num; - static char text[4096]; - size_t len = 0; - - text[0] = 0; - if (files[M_COMMAND]->content) { - cext_strlcpy(text, files[M_COMMAND]->content, sizeof(text)); - len = strlen(text); - } - buf[0] = 0; - num = XLookupString(e, buf, sizeof(buf), &ksym, 0); - - if (IsFunctionKey(ksym) || IsKeypadKey(ksym) - || IsMiscFunctionKey(ksym) || IsPFKey(ksym) - || IsPrivateKeypadKey(ksym)) - return; - - /* first check if a control mask is omitted */ - if (e->state & ShiftMask) { - if (ksym == XK_ISO_Left_Tab) - ksym = XK_Left; - } else if (e->state & ControlMask) { - switch (ksym) { - case XK_E: - case XK_e: - ksym = XK_End; - break; - case XK_H: - case XK_h: - ksym = XK_BackSpace; - break; - case XK_J: - case XK_j: - ksym = XK_Return; - break; - case XK_U: - case XK_u: - set_text(0); - update_items(0); - draw_menu(); - return; - break; - default: /* ignore other control sequences */ - return; - break; - } - } - switch (ksym) { - case XK_Left: - if (!sel) - return; - if (sel->prev) { - sel = sel->prev; - set_text(sel->file->name); - } else - return; - break; - case XK_Right: - case XK_Tab: - if (!sel) - return; - if (sel->next) { - sel = sel->next; - set_text(sel->file->name); - } else - return; - break; - case XK_Down: - if (selhist) { - set_text(selhist->file->content); - if (selhist->next) - selhist = selhist->next; - } - update_items(files[M_COMMAND]->content); - break; - case XK_Up: - if (selhist) { - set_text(selhist->file->content); - if (selhist->prev) - selhist = selhist->prev; - } - update_items(files[M_COMMAND]->content); - break; - case XK_Return: - if (sel) - exec_item(sel->file->name); - else if (text) - exec_item(text); - case XK_Escape: - hide(); - break; - case XK_BackSpace: - if (len) { - size_t i = len; - if (i) { - do - text[--i] = 0; - while (nitems && i && nitems == update_items(text)); - } - set_text(text); - update_items(files[M_COMMAND]->content); - } - break; - default: - if ((num == 1) && !iscntrl((int) buf[0])) { - buf[num] = 0; - if (len > 0) - cext_strlcat(text, buf, sizeof(text)); - else - cext_strlcpy(text, buf, sizeof(text)); - set_text(text); - update_items(files[M_COMMAND]->content); - } - } - if (sel) { - if (sel == offset[OFF_CURR]->prev) { - offset[OFF_CURR] = offset[OFF_PREV]; - update_offsets(); - } else if (sel == offset[OFF_NEXT]) { - offset[OFF_CURR] = offset[OFF_NEXT]; - update_offsets(); - } - } - draw_menu(); + KeySym ksym; + char buf[32]; + int num; + static char text[4096]; + size_t len = 0; + + text[0] = 0; + if(files[M_COMMAND]->content) { + cext_strlcpy(text, files[M_COMMAND]->content, sizeof(text)); + len = strlen(text); + } + buf[0] = 0; + num = XLookupString(e, buf, sizeof(buf), &ksym, 0); + + if(IsFunctionKey(ksym) || IsKeypadKey(ksym) + || IsMiscFunctionKey(ksym) || IsPFKey(ksym) + || IsPrivateKeypadKey(ksym)) + return; + + /* first check if a control mask is omitted */ + if(e->state & ShiftMask) { + if(ksym == XK_ISO_Left_Tab) + ksym = XK_Left; + } else if(e->state & ControlMask) { + switch (ksym) { + case XK_E: + case XK_e: + ksym = XK_End; + break; + case XK_H: + case XK_h: + ksym = XK_BackSpace; + break; + case XK_J: + case XK_j: + ksym = XK_Return; + break; + case XK_U: + case XK_u: + set_text(0); + update_items(0); + draw_menu(); + return; + break; + default: /* ignore other control sequences */ + return; + break; + } + } + switch (ksym) { + case XK_Left: + if(!sel) + return; + if(sel->prev) { + sel = sel->prev; + set_text(sel->file->name); + } else + return; + break; + case XK_Right: + case XK_Tab: + if(!sel) + return; + if(sel->next) { + sel = sel->next; + set_text(sel->file->name); + } else + return; + break; + case XK_Down: + if(selhist) { + set_text(selhist->file->content); + if(selhist->next) + selhist = selhist->next; + } + update_items(files[M_COMMAND]->content); + break; + case XK_Up: + if(selhist) { + set_text(selhist->file->content); + if(selhist->prev) + selhist = selhist->prev; + } + update_items(files[M_COMMAND]->content); + break; + case XK_Return: + if(sel) + exec_item(sel->file->name); + else if(text) + exec_item(text); + case XK_Escape: + hide(); + break; + case XK_BackSpace: + if(len) { + size_t i = len; + if(i) { + do + text[--i] = 0; + while(nitems && i && nitems == update_items(text)); + } + set_text(text); + update_items(files[M_COMMAND]->content); + } + break; + default: + if((num == 1) && !iscntrl((int) buf[0])) { + buf[num] = 0; + if(len > 0) + cext_strlcat(text, buf, sizeof(text)); + else + cext_strlcpy(text, buf, sizeof(text)); + set_text(text); + update_items(files[M_COMMAND]->content); + } + } + if(sel) { + if(sel == offset[OFF_CURR]->prev) { + offset[OFF_CURR] = offset[OFF_PREV]; + update_offsets(); + } else if(sel == offset[OFF_NEXT]) { + offset[OFF_CURR] = offset[OFF_NEXT]; + update_offsets(); + } + } + draw_menu(); } -static void check_event(Connection * c) +static void +check_event(Connection * c) { - XEvent ev; - - while (XPending(dpy)) { - XNextEvent(dpy, &ev); - switch (ev.type) { - case KeyPress: - handle_kpress(&ev.xkey); - break; - case Expose: - if (ev.xexpose.count == 0) { - draw_menu(); - } - break; - default: - break; - } - } + XEvent ev; + + while(XPending(dpy)) { + XNextEvent(dpy, &ev); + switch (ev.type) { + case KeyPress: + handle_kpress(&ev.xkey); + break; + case Expose: + if(ev.xexpose.count == 0) { + draw_menu(); + } + break; + default: + break; + } + } } -static void update_geometry() +static void +update_geometry() { - mrect = rect; - mrect.height = font->ascent + font->descent + 4; - if (align == SOUTH) - mrect.y = rect.height - mrect.height; - XMoveResizeWindow(dpy, win, mrect.x, mrect.y, mrect.width, mrect.height); - XSync(dpy, False); - XFreePixmap(dpy, pmap); - pmap = XCreatePixmap(dpy, win, mrect.width, mrect.height, DefaultDepth(dpy, screen_num)); - XSync(dpy, False); + mrect = rect; + mrect.height = font->ascent + font->descent + 4; + if(align == SOUTH) + mrect.y = rect.height - mrect.height; + XMoveResizeWindow(dpy, win, mrect.x, mrect.y, mrect.width, + mrect.height); + XSync(dpy, False); + XFreePixmap(dpy, pmap); + pmap = + XCreatePixmap(dpy, win, mrect.width, mrect.height, + DefaultDepth(dpy, screen_num)); + XSync(dpy, False); } -static void handle_after_write(IXPServer * s, File * f) +static void +handle_after_write(IXPServer * s, File * f) { - int i; - size_t len; - - if (files[M_CTL] == f) { - for (i = 0; i < 2; 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 (files[M_GEOMETRY] == f) { - if (f->content) { - if (!strncmp(f->content, "south", 6)) - align = SOUTH; - else if(!strncmp(f->content, "north", 6)) - align = NORTH; - update_geometry(); - draw_menu(); - } - } else if (files[M_FONT] == f) { - XFreeFont(dpy, font); - font = blitz_getfont(dpy, files[M_FONT]->content); - update_geometry(); - draw_menu(); - } else if (files[M_COMMAND] == f) { - update_items(files[M_COMMAND]->content); - draw_menu(); - } - check_event(0); + int i; + size_t len; + + if(files[M_CTL] == f) { + for(i = 0; i < 2; 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(files[M_GEOMETRY] == f) { + if(f->content) { + if(!strncmp(f->content, "south", 6)) + align = SOUTH; + else if(!strncmp(f->content, "north", 6)) + align = NORTH; + update_geometry(); + draw_menu(); + } + } else if(files[M_FONT] == f) { + XFreeFont(dpy, font); + font = blitz_getfont(dpy, files[M_FONT]->content); + update_geometry(); + draw_menu(); + } else if(files[M_COMMAND] == f) { + update_items(files[M_COMMAND]->content); + draw_menu(); + } + check_event(0); } -static void handle_before_read(IXPServer * s, File * f) +static void +handle_before_read(IXPServer * s, File * f) { - char buf[64]; - if (f != files[M_GEOMETRY]) - return; - snprintf(buf, sizeof(buf), "%d,%d,%d,%d", mrect.x, mrect.y, mrect.width, mrect.height); - if (f->content) - free(f->content); - f->content = strdup(buf); - f->size = strlen(buf); + char buf[64]; + if(f != files[M_GEOMETRY]) + return; + snprintf(buf, sizeof(buf), "%d,%d,%d,%d", mrect.x, mrect.y, + mrect.width, mrect.height); + if(f->content) + free(f->content); + f->content = strdup(buf); + f->size = strlen(buf); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i; - XSetWindowAttributes wa; - XGCValues gcv; - - /* 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", "wmiimenu: cannot open display\n"); - exit(1); - } - screen_num = DefaultScreen(dpy); - - ixps = wmii_setup_server(sockfile); - - /* init */ - if (!(files[M_CTL] = ixp_create(ixps, "/ctl"))) { - perror("wmiimenu: cannot connect IXP server"); - exit(1); - } - files[M_CTL]->after_write = handle_after_write; - files[M_GEOMETRY] = ixp_create(ixps, "/geometry"); - files[M_GEOMETRY]->before_read = handle_before_read; - files[M_GEOMETRY]->after_write = handle_after_write; - files[M_PRE_COMMAND] = ixp_create(ixps, "/precmd"); - files[M_COMMAND] = ixp_create(ixps, "/cmd"); - files[M_COMMAND]->after_write = handle_after_write; - files[M_HISTORY] = ixp_create(ixps, "/history"); - add_history(""); - files[M_LOOKUP] = ixp_create(ixps, "/lookup"); - files[M_FONT] = wmii_create_ixpfile(ixps, "/font", BLITZ_FONT); - files[M_FONT]->after_write = handle_after_write; - font = blitz_getfont(dpy, files[M_FONT]->content); - files[M_SEL_BG_COLOR] = wmii_create_ixpfile(ixps, "/sstyle/bgcolor", BLITZ_SEL_BG_COLOR); - files[M_SEL_TEXT_COLOR] = wmii_create_ixpfile(ixps, "/sstyle/fgcolor", BLITZ_SEL_FG_COLOR); - files[M_SEL_BORDER_COLOR] = wmii_create_ixpfile(ixps, "/sstyle/bordercolor", BLITZ_SEL_BORDER_COLOR); - 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); - - wa.override_redirect = 1; - wa.background_pixmap = ParentRelative; - wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask - | SubstructureRedirectMask | SubstructureNotifyMask; - - rect.x = rect.y = 0; - rect.width = DisplayWidth(dpy, screen_num); - rect.height = DisplayHeight(dpy, screen_num); - mrect = rect; - mrect.height = font->ascent + font->descent + 4; - mrect.y = rect.height - mrect.height; - - win = XCreateWindow(dpy, RootWindow(dpy, screen_num), mrect.x, mrect.y, - mrect.width, mrect.height, 0, DefaultDepth(dpy, screen_num), - CopyFromParent, DefaultVisual(dpy, screen_num), - CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm)); - XSync(dpy, False); - - /* window pixmap */ - gcv.function = GXcopy; - gcv.graphics_exposures = False; - - gc = XCreateGC(dpy, win, 0, 0); - pmap = XCreatePixmap(dpy, win, mrect.width, mrect.height, DefaultDepth(dpy, screen_num)); - - /* main event loop */ - run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, 0); - deinit_server(ixps); - XFreePixmap(dpy, pmap); - XFreeGC(dpy, gc); - XCloseDisplay(dpy); - - return 0; + int i; + XSetWindowAttributes wa; + XGCValues gcv; + + /* 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", "wmiimenu: cannot open display\n"); + exit(1); + } + screen_num = DefaultScreen(dpy); + + ixps = wmii_setup_server(sockfile); + + /* init */ + if(!(files[M_CTL] = ixp_create(ixps, "/ctl"))) { + perror("wmiimenu: cannot connect IXP server"); + exit(1); + } + files[M_CTL]->after_write = handle_after_write; + files[M_GEOMETRY] = ixp_create(ixps, "/geometry"); + files[M_GEOMETRY]->before_read = handle_before_read; + files[M_GEOMETRY]->after_write = handle_after_write; + files[M_PRE_COMMAND] = ixp_create(ixps, "/precmd"); + files[M_COMMAND] = ixp_create(ixps, "/cmd"); + files[M_COMMAND]->after_write = handle_after_write; + files[M_HISTORY] = ixp_create(ixps, "/history"); + add_history(""); + files[M_LOOKUP] = ixp_create(ixps, "/lookup"); + files[M_FONT] = wmii_create_ixpfile(ixps, "/font", BLITZ_FONT); + files[M_FONT]->after_write = handle_after_write; + font = blitz_getfont(dpy, files[M_FONT]->content); + files[M_SEL_BG_COLOR] = + wmii_create_ixpfile(ixps, "/sstyle/bgcolor", BLITZ_SEL_BG_COLOR); + files[M_SEL_TEXT_COLOR] = + wmii_create_ixpfile(ixps, "/sstyle/fgcolor", BLITZ_SEL_FG_COLOR); + files[M_SEL_BORDER_COLOR] = + wmii_create_ixpfile(ixps, "/sstyle/bordercolor", + BLITZ_SEL_BORDER_COLOR); + 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); + + wa.override_redirect = 1; + wa.background_pixmap = ParentRelative; + wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask + | SubstructureRedirectMask | SubstructureNotifyMask; + + rect.x = rect.y = 0; + rect.width = DisplayWidth(dpy, screen_num); + rect.height = DisplayHeight(dpy, screen_num); + mrect = rect; + mrect.height = font->ascent + font->descent + 4; + mrect.y = rect.height - mrect.height; + + win = XCreateWindow(dpy, RootWindow(dpy, screen_num), mrect.x, mrect.y, + mrect.width, mrect.height, 0, DefaultDepth(dpy, + screen_num), + CopyFromParent, DefaultVisual(dpy, screen_num), + CWOverrideRedirect | CWBackPixmap | CWEventMask, + &wa); + XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm)); + XSync(dpy, False); + + /* window pixmap */ + gcv.function = GXcopy; + gcv.graphics_exposures = False; + + gc = XCreateGC(dpy, win, 0, 0); + pmap = + XCreatePixmap(dpy, win, mrect.width, mrect.height, + DefaultDepth(dpy, screen_num)); + + /* main event loop */ + run_server_with_fd_support(ixps, ConnectionNumber(dpy), check_event, + 0); + deinit_server(ixps); + XFreePixmap(dpy, pmap); + XFreeGC(dpy, gc); + XCloseDisplay(dpy); + + return 0; } diff --git a/cmd/wmiiplumb.c b/cmd/wmiiplumb.c @@ -11,82 +11,85 @@ #include <X11/Xatom.h> static char *version[] = { - "wmiiplumb - window manager improved plumb - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiiplumb - window manager improved plumb - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, "%s\n", - "usage: wmiiplumb [-v]\n" " -v version info\n"); - exit(1); + fprintf(stderr, "%s\n", + "usage: wmiiplumb [-v]\n" " -v version info\n"); + exit(1); } -static void print_sel(Display * dpy, Window w, XSelectionEvent * e) +static void +print_sel(Display * dpy, Window w, XSelectionEvent * e) { - Atom typeret; - int format; - unsigned long nitems, bytesleft; - unsigned char *data; + Atom typeret; + int format; + unsigned long nitems, bytesleft; + unsigned char *data; - XGetWindowProperty(dpy, w, e->property, 0L, 4096L, False, - AnyPropertyType, &typeret, &format, - &nitems, &bytesleft, &data); - if (format == 8) { - int i; - for (i = 0; i < nitems; i++) - putchar(data[i]); - putchar('\n'); - } - XDeleteProperty(dpy, w, e->property); + XGetWindowProperty(dpy, w, e->property, 0L, 4096L, False, + AnyPropertyType, &typeret, &format, + &nitems, &bytesleft, &data); + if(format == 8) { + int i; + for(i = 0; i < nitems; i++) + putchar(data[i]); + putchar('\n'); + } + XDeleteProperty(dpy, w, e->property); } -int main(int argc, char **argv) +int +main(int argc, char **argv) { - Display *dpy; - Atom xa_clip_string; - Window w; - XEvent ev; - int pdone = 0; + Display *dpy; + Atom xa_clip_string; + Window w; + XEvent ev; + int pdone = 0; - /* command line args */ - if (argc > 1) { - if (!strncmp(argv[1], "-v", 3)) { - fprintf(stdout, "%s", version[0]); - exit(0); - } else - usage(); - } - dpy = XOpenDisplay(0); - if (!dpy) { - fprintf(stderr, "%s", "wmiiplumb: cannot open display\n"); - exit(1); - } - xa_clip_string = XInternAtom(dpy, "PLUMB_STRING", False); - w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200, - 1, CopyFromParent, CopyFromParent); - while (1 && !pdone) { - XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string, - w, CurrentTime); - XFlush(dpy); - XNextEvent(dpy, &ev); - switch (ev.type) { - case SelectionNotify: - if (ev.xselection.property != None) - print_sel(dpy, w, &ev.xselection); - else - putchar('\n'); - XDestroyWindow(dpy, w); - XCloseDisplay(dpy); - pdone = 1; - break; - default: - break; - } - } - return 0; - /* - * XDestroyWindow(dpy, w); XCloseDisplay(dpy); return 1; - */ + /* command line args */ + if(argc > 1) { + if(!strncmp(argv[1], "-v", 3)) { + fprintf(stdout, "%s", version[0]); + exit(0); + } else + usage(); + } + dpy = XOpenDisplay(0); + if(!dpy) { + fprintf(stderr, "%s", "wmiiplumb: cannot open display\n"); + exit(1); + } + xa_clip_string = XInternAtom(dpy, "PLUMB_STRING", False); + w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200, + 1, CopyFromParent, CopyFromParent); + while(1 && !pdone) { + XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string, + w, CurrentTime); + XFlush(dpy); + XNextEvent(dpy, &ev); + switch (ev.type) { + case SelectionNotify: + if(ev.xselection.property != None) + print_sel(dpy, w, &ev.xselection); + else + putchar('\n'); + XDestroyWindow(dpy, w); + XCloseDisplay(dpy); + pdone = 1; + break; + default: + break; + } + } + return 0; + /* + * XDestroyWindow(dpy, w); XCloseDisplay(dpy); return 1; + */ } diff --git a/cmd/wmiir.c b/cmd/wmiir.c @@ -13,213 +13,219 @@ static IXPClient *c; static int exit_code = 0; static char *version[] = { - "wmiir - window manager improved remote - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiir - window manager improved remote - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, "%s", - "usage: wmiir [-s <socket file>] [-v] <action> <action_arg> [...]\n" - " -s socket file (default: $WMIIR_SOCKET)\n" - " -f read actions from stdin\n" - " -v version info\n" - "actions:\n" - " create <file> [<content>] -- creates and optionally writes content to a file\n" - " write <file> <content> -- writes content to a file\n" - " read <directory/file> -- reads file or directory contents\n" - " remove <directory/file> -- removes file or directory, use with care!\n"); - exit(1); + fprintf(stderr, "%s", + "usage: wmiir [-s <socket file>] [-v] <action> <action_arg> [...]\n" + " -s socket file (default: $WMIIR_SOCKET)\n" + " -f read actions from stdin\n" + " -v version info\n" + "actions:\n" + " create <file> [<content>] -- creates and optionally writes content to a file\n" + " write <file> <content> -- writes content to a file\n" + " read <directory/file> -- reads file or directory contents\n" + " remove <directory/file> -- removes file or directory, use with care!\n"); + exit(1); } -static void perform(char *action, char *file, char *content) +static void +perform(char *action, char *file, char *content) { - size_t out_len = 0; - char output[2050]; - int crt, fd = -1; + size_t out_len = 0; + char output[2050]; + int crt, fd = -1; - if (!action) - return; + if(!action) + return; - crt = !strncmp(action, "create", 7); - if (!strncmp(action, "write", 6) || crt) { - if (!file) - return; - /* create file first */ - if (crt) { - c->create(c, file); - if (c->errstr) { - fprintf(stderr, "wmiir: error: create %s: %s\n", file, - c->errstr); - exit_code = 1; - return; - } - } - if (!content) - return; - fd = c->open(c, file); - if (c->errstr) { - fprintf(stderr, "wmiir: error: open %s: %s\n", file, c->errstr); - exit_code = 1; - return; - } - c->write(c, fd, content, strlen(content)); - if (c->errstr) { - fprintf(stderr, "wmiir: error: write %s: %s\n", file, - c->errstr); - exit_code = 1; - if (!strncmp(c->errstr, DEAD_SERVER, strlen(DEAD_SERVER) + 1)) - return; - } - } else if (!strncmp(action, "read", 5)) { - if (!file) - return; - fd = c->open(c, file); - if (c->errstr) { - fprintf(stderr, "wmiir: error: open %s: %s\n", file, c->errstr); - exit_code = 1; - return; - } - do { - out_len = c->read(c, fd, output, 2048); - if (c->errstr) { - fprintf(stderr, "wmiir: error: read %s: %s\n", file, - c->errstr); - exit_code = 1; - if (!strncmp - (c->errstr, DEAD_SERVER, strlen(DEAD_SERVER) + 1)) - return; - break; - } - output[out_len] = 0; - fprintf(stdout, "%s", output); - } - while (out_len == 2048); - fprintf(stdout, "%s", "\n"); - } else if (!strncmp(action, "remove", 7)) { - if (!file) - return; - c->remove(c, file); - if (c->errstr) { - fprintf(stderr, "wmiir: error: remove %s: %s\n", file, - c->errstr); - exit_code = 1; - return; - } - } - if (fd != -1) { - c->close(c, fd); - if (c->errstr) { - fprintf(stderr, "wmiir: error: close %s: %s\n", file, - c->errstr); - exit_code = 1; - return; - } - } + crt = !strncmp(action, "create", 7); + if(!strncmp(action, "write", 6) || crt) { + if(!file) + return; + /* create file first */ + if(crt) { + c->create(c, file); + if(c->errstr) { + fprintf(stderr, "wmiir: error: create %s: %s\n", file, + c->errstr); + exit_code = 1; + return; + } + } + if(!content) + return; + fd = c->open(c, file); + if(c->errstr) { + fprintf(stderr, "wmiir: error: open %s: %s\n", file, + c->errstr); + exit_code = 1; + return; + } + c->write(c, fd, content, strlen(content)); + if(c->errstr) { + fprintf(stderr, "wmiir: error: write %s: %s\n", file, + c->errstr); + exit_code = 1; + if(!strncmp(c->errstr, DEAD_SERVER, strlen(DEAD_SERVER) + 1)) + return; + } + } else if(!strncmp(action, "read", 5)) { + if(!file) + return; + fd = c->open(c, file); + if(c->errstr) { + fprintf(stderr, "wmiir: error: open %s: %s\n", file, + c->errstr); + exit_code = 1; + return; + } + do { + out_len = c->read(c, fd, output, 2048); + if(c->errstr) { + fprintf(stderr, "wmiir: error: read %s: %s\n", file, + c->errstr); + exit_code = 1; + if(!strncmp + (c->errstr, DEAD_SERVER, strlen(DEAD_SERVER) + 1)) + return; + break; + } + output[out_len] = 0; + fprintf(stdout, "%s", output); + } + while(out_len == 2048); + fprintf(stdout, "%s", "\n"); + } else if(!strncmp(action, "remove", 7)) { + if(!file) + return; + c->remove(c, file); + if(c->errstr) { + fprintf(stderr, "wmiir: error: remove %s: %s\n", file, + c->errstr); + exit_code = 1; + return; + } + } + if(fd != -1) { + c->close(c, fd); + if(c->errstr) { + fprintf(stderr, "wmiir: error: close %s: %s\n", file, + c->errstr); + exit_code = 1; + return; + } + } } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i = 0, read_stdin = 0; - char line[4096], *p; - char *sockfile = getenv("WMIIR_SOCKET"); + int i = 0, read_stdin = 0; + char line[4096], *p; + char *sockfile = getenv("WMIIR_SOCKET"); - /* command line args */ - if (argc > 1) { - for (i = 1; (i < argc) && (argv[i][0] == '-'); i++) { - switch (argv[i][1]) { - case 'v': - fprintf(stderr, "%s", version[0]); - exit(0); - break; - case 's': - if (i + 1 < argc) { - sockfile = argv[++i]; - } else { - usage(); - } - break; - case 'f': - read_stdin = 1; - break; - default: - usage(); - break; - } - } - } - if ((argc <= 1) || (!read_stdin && (i + 1) >= argc)) { - fprintf(stderr, "%s", "wmiir: arguments: "); - for (i = 1; i < argc; i++) - fprintf(stderr, "%s, ", argv[i]); - fprintf(stderr, "%s", "\n"); - usage(); - } - if (!sockfile) { - fprintf(stderr, "%s", - "wmiir: error: WMIIR_SOCKET environment not set\n"); - usage(); - } - /* open socket */ - if (!(c = init_ixp_client(sockfile))) { - fprintf(stderr, "wmiir: cannot connect to server '%s'\n", sockfile); - exit(1); - } - if (read_stdin) { - /* simple shell */ - char *action, *file, *content; - while (fgets(line, 4096, stdin)) { - p = line; - while (*p != 0 && (*p == ' ' || *p == '\t')) - p++; - if (*p == 0) - continue; /* empty line */ - if (strncmp(p, "create ", 7) && - strncmp(p, "write ", 6) && - strncmp(p, "read ", 5) && strncmp(p, "remove ", 7)) - continue; + /* command line args */ + if(argc > 1) { + for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { + switch (argv[i][1]) { + case 'v': + fprintf(stderr, "%s", version[0]); + exit(0); + break; + case 's': + if(i + 1 < argc) { + sockfile = argv[++i]; + } else { + usage(); + } + break; + case 'f': + read_stdin = 1; + break; + default: + usage(); + break; + } + } + } + if((argc <= 1) || (!read_stdin && (i + 1) >= argc)) { + fprintf(stderr, "%s", "wmiir: arguments: "); + for(i = 1; i < argc; i++) + fprintf(stderr, "%s, ", argv[i]); + fprintf(stderr, "%s", "\n"); + usage(); + } + if(!sockfile) { + fprintf(stderr, "%s", + "wmiir: error: WMIIR_SOCKET environment not set\n"); + usage(); + } + /* open socket */ + if(!(c = init_ixp_client(sockfile))) { + fprintf(stderr, "wmiir: cannot connect to server '%s'\n", + sockfile); + exit(1); + } + if(read_stdin) { + /* simple shell */ + char *action, *file, *content; + while(fgets(line, 4096, stdin)) { + p = line; + while(*p != 0 && (*p == ' ' || *p == '\t')) + p++; + if(*p == 0) + continue; /* empty line */ + if(strncmp(p, "create ", 7) && + strncmp(p, "write ", 6) && + strncmp(p, "read ", 5) && strncmp(p, "remove ", 7)) + continue; - action = p; - while (*p != 0 && *p != ' ' && *p != '\t' && *p != '\n') - p++; - if (*p == 0 || *p == '\n') - continue; /* ignore bogus command */ - *p = 0; - p++; - while (*p != 0 && (*p == ' ' || *p == '\t')) - p++; - if (*p == 0) - continue; /* ignore bogus command */ - file = p; - while (*p != 0 && *p != ' ' && *p != '\t' && *p != '\n') - p++; - if (*p == 0 || *p == '\n') { - content = 0; - *p = 0; - } else { - *p = 0; - p++; - content = p; - } - if (file[0] == 0) - continue; - if (content) { - static size_t len; - if ((len = strlen(content))) - content[len - 1] = 0; - } - perform(action, file, content); - if (c->errstr) - fprintf(stderr, "wmiir: error: read %s: %s\n", file, - c->errstr); - } - } else { - perform(argv[i], argv[i + 1], (i + 2) < argc ? argv[i + 2] : 0); - } + action = p; + while(*p != 0 && *p != ' ' && *p != '\t' && *p != '\n') + p++; + if(*p == 0 || *p == '\n') + continue; /* ignore bogus command */ + *p = 0; + p++; + while(*p != 0 && (*p == ' ' || *p == '\t')) + p++; + if(*p == 0) + continue; /* ignore bogus command */ + file = p; + while(*p != 0 && *p != ' ' && *p != '\t' && *p != '\n') + p++; + if(*p == 0 || *p == '\n') { + content = 0; + *p = 0; + } else { + *p = 0; + p++; + content = p; + } + if(file[0] == 0) + continue; + if(content) { + static size_t len; + if((len = strlen(content))) + content[len - 1] = 0; + } + perform(action, file, content); + if(c->errstr) + fprintf(stderr, "wmiir: error: read %s: %s\n", file, + c->errstr); + } + } else { + perform(argv[i], argv[i + 1], (i + 2) < argc ? argv[i + 2] : 0); + } - if (c->errstr) { - deinit_client(c); - exit_code = 1; - } - return exit_code; + if(c->errstr) { + deinit_client(c); + exit_code = 1; + } + return exit_code; } diff --git a/cmd/wmiir2.c b/cmd/wmiir2.c @@ -12,9 +12,9 @@ #include <cext.h> typedef struct { - char *name; - int (*cmd) (char **argv); - int min_argc; + char *name; + int (*cmd) (char **argv); + int min_argc; } Command; static int xcreate(char **argv); @@ -22,208 +22,217 @@ static int xread(char **argv); static int xwrite(char **argv); static int xremove(char **argv); static Command cmds[] = { - {"create", xcreate, 2}, - {"read", xread, 1}, - {"write", xwrite, 2}, - {"remove", xremove, 1}, - {0, 0} + {"create", xcreate, 2}, + {"read", xread, 1}, + {"write", xwrite, 2}, + {"remove", xremove, 1}, + {0, 0} }; static IXPClient c = { 0 }; static char *version[] = { - "wmiir - window manager improved remote - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiir - window manager improved remote - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, "%s", - "usage: wmiir [-a <server address>] [-v]\n" - " -a server address (default: $WMIIR_SOCKET)\n" - " -v version info\n" - "commands read from stdin:\n" - " create <file> <content> -- creates file\n" - " read <file/directory> -- reads file/directory contents\n" - " write <file> <content> -- writes content to a file\n" - " remove <file/directory> -- removes file\n"); - exit(1); + fprintf(stderr, "%s", + "usage: wmiir [-a <server address>] [-v]\n" + " -a server address (default: $WMIIR_SOCKET)\n" + " -v version info\n" + "commands read from stdin:\n" + " create <file> <content> -- creates file\n" + " read <file/directory> -- reads file/directory contents\n" + " write <file> <content> -- writes content to a file\n" + " remove <file/directory> -- removes file\n"); + exit(1); } -static u32 write_data(u32 fid, u8 * data, u32 count) +static unsigned int +write_data(unsigned int fid, unsigned char *data, unsigned int count) { - u32 len, i, runs = count / c.fcall.iounit; - - for (i = 0; i <= runs; i++) { - /* write */ - len = count - (i * c.fcall.iounit); - if (len > c.fcall.iounit) - len = c.fcall.iounit; - if (ixp_client_write - (&c, fid, i * c.fcall.iounit, len, - &data[i * c.fcall.iounit]) != count) { - fprintf(stderr, "wmiir: cannot write file: %s\n", c.errstr); - return 0; - } - } - return count; + unsigned int len, i, runs = count / c.fcall.iounit; + + for(i = 0; i <= runs; i++) { + /* write */ + len = count - (i * c.fcall.iounit); + if(len > c.fcall.iounit) + len = c.fcall.iounit; + if(ixp_client_write + (&c, fid, i * c.fcall.iounit, len, + &data[i * c.fcall.iounit]) != count) { + fprintf(stderr, "wmiir: cannot write file: %s\n", c.errstr); + return 0; + } + } + return count; } -static int xcreate(char **argv) +static int +xcreate(char **argv) { - u32 fid; - char *p = strrchr(argv[0], '/'); - - fid = c.root_fid << 2; - /* walk to bottom-most directory */ - *p = 0; - if (!ixp_client_walk(&c, fid, argv[0])) { - fprintf(stderr, "wmiir: cannot walk to %s: %s\n", argv[0], - c.errstr); - return 1; - } - /* create */ - p++; - if (!ixp_client_create(&c, fid, p, (u32) 0xff, IXP_OWRITE)) { - fprintf(stderr, "wmiir: cannot create file: %s\n", c.errstr); - return 1; - } - write_data(fid, (u8 *) argv[1], strlen(argv[1])); - return !ixp_client_close(&c, fid); + unsigned int fid; + char *p = strrchr(argv[0], '/'); + + fid = c.root_fid << 2; + /* walk to bottom-most directory */ + *p = 0; + if(!ixp_client_walk(&c, fid, argv[0])) { + fprintf(stderr, "wmiir: cannot walk to %s: %s\n", argv[0], + c.errstr); + return 1; + } + /* create */ + p++; + if(!ixp_client_create(&c, fid, p, (unsigned int) 0xff, IXP_OWRITE)) { + fprintf(stderr, "wmiir: cannot create file: %s\n", c.errstr); + return 1; + } + write_data(fid, (unsigned char *) argv[1], strlen(argv[1])); + return !ixp_client_close(&c, fid); } -static int xwrite(char **argv) +static int +xwrite(char **argv) { - /* open */ - u32 fid = c.root_fid << 2; - if (!ixp_client_open(&c, fid, argv[0], IXP_OWRITE)) { - fprintf(stderr, "wmiir: cannot open file: %s\n", c.errstr); - return 1; - } - write_data(fid, (u8 *) argv[1], strlen(argv[1])); - return !ixp_client_close(&c, fid); + /* open */ + unsigned int fid = c.root_fid << 2; + if(!ixp_client_open(&c, fid, argv[0], IXP_OWRITE)) { + fprintf(stderr, "wmiir: cannot open file: %s\n", c.errstr); + return 1; + } + write_data(fid, (unsigned char *) argv[1], strlen(argv[1])); + return !ixp_client_close(&c, fid); } -static void print_directory(void *result, u32 msize) +static void +print_directory(void *result, unsigned int msize) { - void *p = result; - static Stat stat, zerostat = { 0 }; - u32 len = 0; - do { - p = ixp_dec_stat(p, &stat); - len += stat.size + sizeof(u16); - if (stat.qid.type == IXP_QTDIR) - fprintf(stdout, "%s/\n", stat.name); - else - fprintf(stdout, "%s\n", stat.name); - stat = zerostat; - } - while (len < msize); + void *p = result; + static Stat stat, zerostat = { 0 }; + unsigned int len = 0; + do { + p = ixp_dec_stat(p, &stat); + len += stat.size + sizeof(unsigned short); + if(stat.qid.type == IXP_QTDIR) + fprintf(stdout, "%s/\n", stat.name); + else + fprintf(stdout, "%s\n", stat.name); + stat = zerostat; + } + while(len < msize); } -static int xread(char **argv) +static int +xread(char **argv) { - u32 count, fid = c.root_fid << 2; - int is_directory = FALSE; - static u8 result[IXP_MAX_MSG]; - - /* open */ - if (!ixp_client_open(&c, fid, argv[0], IXP_OREAD)) { - fprintf(stderr, "wmiir: cannot open file: %s\n", c.errstr); - return 1; - } - is_directory = !c.fcall.nwqid || (c.fcall.qid.type == IXP_QTDIR); - /* read */ - if (!(count = ixp_client_read(&c, fid, 0, result, IXP_MAX_MSG)) - && c.errstr) { - fprintf(stderr, "wmiir: cannot read file: %s\n", c.errstr); - return 1; - } - if (count) { - if (is_directory) - print_directory(result, count); - else { - u32 i; - for (i = 0; i < count; i++) - putchar(result[i]); - } - } - return !ixp_client_close(&c, fid); + unsigned int count, fid = c.root_fid << 2; + int is_directory = FALSE; + static unsigned char result[IXP_MAX_MSG]; + + /* open */ + if(!ixp_client_open(&c, fid, argv[0], IXP_OREAD)) { + fprintf(stderr, "wmiir: cannot open file: %s\n", c.errstr); + return 1; + } + is_directory = !c.fcall.nwqid || (c.fcall.qid.type == IXP_QTDIR); + /* read */ + if(!(count = ixp_client_read(&c, fid, 0, result, IXP_MAX_MSG)) + && c.errstr) { + fprintf(stderr, "wmiir: cannot read file: %s\n", c.errstr); + return 1; + } + if(count) { + if(is_directory) + print_directory(result, count); + else { + unsigned int i; + for(i = 0; i < count; i++) + putchar(result[i]); + } + } + return !ixp_client_close(&c, fid); } -static int xremove(char **argv) +static int +xremove(char **argv) { - u32 fid; - - /* remove */ - fid = c.root_fid << 2; - if (!ixp_client_remove(&c, fid, argv[0])) { - fprintf(stderr, "wmiir: cannot remove file: %s\n", c.errstr); - return 1; - } - return 0; + unsigned int fid; + + /* remove */ + fid = c.root_fid << 2; + if(!ixp_client_remove(&c, fid, argv[0])) { + fprintf(stderr, "wmiir: cannot remove file: %s\n", c.errstr); + return 1; + } + return 0; } -static int perform_cmd(int argc, char **argv) +static int +perform_cmd(int argc, char **argv) { - int i; - for (i = 0; cmds[i].name; i++) - if (!strncmp(cmds[i].name, argv[0], strlen(cmds[i].name))) { - if (cmds[i].min_argc <= argc) - return cmds[i].cmd(&argv[1]); - else - usage(); - } - /* bogus command */ - return 1; + int i; + for(i = 0; cmds[i].name; i++) + if(!strncmp(cmds[i].name, argv[0], strlen(cmds[i].name))) { + if(cmds[i].min_argc <= argc) + return cmds[i].cmd(&argv[1]); + else + usage(); + } + /* bogus command */ + return 1; } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - int i = 0, ret; - char line[4096]; - char *sockfile = getenv("WMIIR_SOCKET"); - char *stdin_argv[3]; - int stdin_argc; - - /* command line args */ - if (argc > 1) { - for (i = 1; (i < argc) && (argv[i][0] == '-'); i++) { - switch (argv[i][1]) { - case 'v': - fprintf(stderr, "%s", version[0]); - exit(0); - break; - case 'a': - if (i + 1 < argc) - sockfile = argv[++i]; - else - usage(); - break; - default: - usage(); - break; - } - } - } - if (!sockfile) { - fprintf(stderr, "%s", - "wmiir: error: WMIIR_SOCKET environment not set\n"); - usage(); - } - /* open socket */ - if (!ixp_client_init(&c, sockfile)) { - fprintf(stderr, "wmiir: %s\n", c.errstr); - exit(1); - } - /* wether perform directly or read from stdin */ - while (fgets(line, 4096, stdin)) - if ((stdin_argc = cext_tokenize(stdin_argv, 3, line, ' '))) { - if ((ret = perform_cmd(stdin_argc, stdin_argv))) - break; - } - - /* close socket */ - ixp_client_deinit(&c); - - return ret; + int i = 0, ret; + char line[4096]; + char *sockfile = getenv("WMIIR_SOCKET"); + char *stdin_argv[3]; + int stdin_argc; + + /* command line args */ + if(argc > 1) { + for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { + switch (argv[i][1]) { + case 'v': + fprintf(stderr, "%s", version[0]); + exit(0); + break; + case 'a': + if(i + 1 < argc) + sockfile = argv[++i]; + else + usage(); + break; + default: + usage(); + break; + } + } + } + if(!sockfile) { + fprintf(stderr, "%s", + "wmiir: error: WMIIR_SOCKET environment not set\n"); + usage(); + } + /* open socket */ + if(!ixp_client_init(&c, sockfile)) { + fprintf(stderr, "wmiir: %s\n", c.errstr); + exit(1); + } + /* wether perform directly or read from stdin */ + while(fgets(line, 4096, stdin)) + if((stdin_argc = cext_tokenize(stdin_argv, 3, line, ' '))) { + if((ret = perform_cmd(stdin_argc, stdin_argv))) + break; + } + + /* close socket */ + ixp_client_deinit(&c); + + return ret; } diff --git a/cmd/wmiiwarp.c b/cmd/wmiiwarp.c @@ -11,44 +11,47 @@ #include <X11/Xatom.h> static char *version[] = { - "wmiiwarp - window manager improved warp - " VERSION "\n" - " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 + "wmiiwarp - window manager improved warp - " VERSION "\n" + " (C)opyright MMIV-MMV Anselm R. Garbe\n", 0 }; -static void usage() +static void +usage() { - fprintf(stderr, - "usage: wmiiwarp [-v] <x>,<y>\n" " -v version info\n"); - exit(1); + fprintf(stderr, + "usage: wmiiwarp [-v] <x>,<y>\n" + " -v version info\n"); + exit(1); } -int main(int argc, char **argv) +int +main(int argc, char **argv) { - Display *dpy; - int x, y; + Display *dpy; + int x, y; - /* command line args */ - if (argc != 2) - usage(); - if (!strncmp(argv[1], "-v", 2)) { - fprintf(stdout, "%s", version[0]); - exit(0); - } - dpy = XOpenDisplay(0); - if (!dpy) { - fprintf(stderr, "%s", "wmiiwarp: cannot open display\n"); - exit(1); - } - if (!strncmp(argv[1], "center", 7)) { - x = DisplayWidth(dpy, DefaultScreen(dpy)) / 2; - y = DisplayHeight(dpy, DefaultScreen(dpy)) / 2; - } else if (sscanf(argv[1], "%d,%d", &x, &y) != 2) { - XCloseDisplay(dpy); - usage(); - } - XWarpPointer(dpy, None, RootWindow(dpy, DefaultScreen(dpy)), - 0, 0, 0, 0, x, y); - XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); - XCloseDisplay(dpy); - return 0; + /* command line args */ + if(argc != 2) + usage(); + if(!strncmp(argv[1], "-v", 2)) { + fprintf(stdout, "%s", version[0]); + exit(0); + } + dpy = XOpenDisplay(0); + if(!dpy) { + fprintf(stderr, "%s", "wmiiwarp: cannot open display\n"); + exit(1); + } + if(!strncmp(argv[1], "center", 7)) { + x = DisplayWidth(dpy, DefaultScreen(dpy)) / 2; + y = DisplayHeight(dpy, DefaultScreen(dpy)) / 2; + } else if(sscanf(argv[1], "%d,%d", &x, &y) != 2) { + XCloseDisplay(dpy); + usage(); + } + XWarpPointer(dpy, None, RootWindow(dpy, DefaultScreen(dpy)), + 0, 0, 0, 0, x, y); + XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); + XCloseDisplay(dpy); + return 0; } diff --git a/libcext/cext.h b/libcext/cext.h @@ -28,7 +28,8 @@ size_t cext_strlcat(char *dst, const char *src, size_t siz); size_t cext_strlcpy(char *dst, const char *src, size_t siz); /* strtonum.c */ -long long cext_strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp); +long long cext_strtonum(const char *numstr, long long minval, + long long maxval, const char **errstrp); /* tokenize.c */ size_t cext_tokenize(char **result, size_t reslen, char *str, char delim); diff --git a/libcext/emallocz.c b/libcext/emallocz.c @@ -8,14 +8,15 @@ #include "cext.h" -void *cext_emallocz(size_t size) +void * +cext_emallocz(size_t size) { - void *res = calloc(1, size); + void *res = calloc(1, size); - if (!res) { - fprintf(stderr, "fatal: could not malloc() %d bytes\n", - (int) size); - exit(1); - } - return res; + if(!res) { + fprintf(stderr, "fatal: could not malloc() %d bytes\n", + (int) size); + exit(1); + } + return res; } diff --git a/libcext/estrdup.c b/libcext/estrdup.c @@ -9,12 +9,13 @@ #include "cext.h" -char *cext_estrdup(const char *s) +char * +cext_estrdup(const char *s) { - char *tmp; + char *tmp; - tmp = (char *) cext_emallocz(strlen(s) + 1); - strcpy(tmp, (char *) s); + tmp = (char *) cext_emallocz(strlen(s) + 1); + strcpy(tmp, (char *) s); - return tmp; + return tmp; } diff --git a/libcext/strlcat.c b/libcext/strlcat.c @@ -26,29 +26,30 @@ * Returns strlen(src) + MIN(siz, strlen(initial dst)). * If retval >= siz, truncation occurred. */ -size_t cext_strlcat(char *dst, const char *src, size_t siz) +size_t +cext_strlcat(char *dst, const char *src, size_t siz) { - register char *d = dst; - register const char *s = src; - register size_t n = siz; - size_t dlen; + register char *d = dst; + register const char *s = src; + register size_t n = siz; + size_t dlen; - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != 0) - d++; - dlen = d - dst; - n = siz - dlen; + /* Find the end of dst and adjust bytes left but don't go past end */ + while(n-- != 0 && *d != 0) + d++; + dlen = d - dst; + n = siz - dlen; - if (n == 0) - return (dlen + strlen(s)); - while (*s != 0) { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = 0; + if(n == 0) + return (dlen + strlen(s)); + while(*s != 0) { + if(n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = 0; - return (dlen + (s - src)); /* count does not include NUL */ + return (dlen + (s - src)); /* count does not include NUL */ } diff --git a/libcext/strlcpy.c b/libcext/strlcpy.c @@ -23,24 +23,25 @@ * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ -size_t cext_strlcpy(char *dst, const char *src, size_t siz) +size_t +cext_strlcpy(char *dst, const char *src, size_t siz) { - register char *d = dst; - register const char *s = src; - register size_t n = siz; + register char *d = dst; + register const char *s = src; + register size_t n = siz; - /* Copy as many bytes as will fit */ - if (n != 0 && --n != 0) { - do { - if ((*d++ = *s++) == 0) - break; - } while (--n != 0); - } - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++); - } - return (s - src - 1); /* count does not include NUL */ + /* Copy as many bytes as will fit */ + if(n != 0 && --n != 0) { + do { + if((*d++ = *s++) == 0) + break; + } while(--n != 0); + } + /* Not enough room in dst, add NUL and traverse rest of src */ + if(n == 0) { + if(siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while(*s++); + } + return (s - src - 1); /* count does not include NUL */ } diff --git a/libcext/strtonum.c b/libcext/strtonum.c @@ -23,39 +23,41 @@ #define TOOSMALL 2 #define TOOLARGE 3 -long long cext_strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp) +long long +cext_strtonum(const char *numstr, long long minval, long long maxval, + const char **errstrp) { - long long ll = 0; - char *ep; - int error = 0; - struct errval { - const char *errstr; - int err; - } ev[4] = { - { NULL, 0}, - { "invalid", EINVAL}, - { "too small", ERANGE}, - { "too large", ERANGE}, - }; + long long ll = 0; + char *ep; + int error = 0; + struct errval { + const char *errstr; + int err; + } ev[4] = { + { + NULL, 0}, { + "invalid", EINVAL}, { + "too small", ERANGE}, { + "too large", ERANGE},}; - ev[0].err = errno; - errno = 0; - if (minval > maxval) - error = INVALID; - else { - ll = strtoll(numstr, &ep, 10); - if (numstr == ep || *ep != 0) - error = INVALID; - else if (errno == ERANGE || ll < minval) - error = TOOSMALL; - else if (errno == ERANGE || ll > maxval) - error = TOOLARGE; - } - if (errstrp != NULL) - *errstrp = ev[error].errstr; - errno = ev[error].err; - if (error) - ll = 0; + ev[0].err = errno; + errno = 0; + if(minval > maxval) + error = INVALID; + else { + ll = strtoll(numstr, &ep, 10); + if(numstr == ep || *ep != 0) + error = INVALID; + else if(errno == ERANGE || ll < minval) + error = TOOSMALL; + else if(errno == ERANGE || ll > maxval) + error = TOOLARGE; + } + if(errstrp != NULL) + *errstrp = ev[error].errstr; + errno = ev[error].err; + if(error) + ll = 0; - return (ll); + return (ll); } diff --git a/libcext/tokenize.c b/libcext/tokenize.c @@ -7,27 +7,28 @@ #include "cext.h" -size_t cext_tokenize(char **result, size_t reslen, char *str, char delim) +size_t +cext_tokenize(char **result, size_t reslen, char *str, char delim) { - char *p, *n; - size_t i = 0; + char *p, *n; + size_t i = 0; - if (!str) - return 0; - for (n = str; *n == ' '; n++); - p = n; - for (i = 0; *n != 0;) { - if (i == reslen) - return i; - if (*n == delim) { - *n = 0; - if (strlen(p)) - result[i++] = p; - p = ++n; - } else - n++; - } - if (strlen(p)) - result[i++] = p; - return i; /* number of tokens */ + if(!str) + return 0; + for(n = str; *n == ' '; n++); + p = n; + for(i = 0; *n != 0;) { + if(i == reslen) + return i; + if(*n == delim) { + *n = 0; + if(strlen(p)) + result[i++] = p; + p = ++n; + } else + n++; + } + if(strlen(p)) + result[i++] = p; + return i; /* number of tokens */ } diff --git a/libixp2/client.c b/libixp2/client.c @@ -13,168 +13,183 @@ #include "cext.h" #include "ixp.h" -static u8 msg[IXP_MAX_MSG]; +static unsigned char msg[IXP_MAX_MSG]; -static int do_fcall(IXPClient * c) +static int +do_fcall(IXPClient * c) { - u32 msize = ixp_fcall_to_msg(&c->fcall, msg, IXP_MAX_MSG); - c->errstr = 0; - if (ixp_send_message(c->fd, msg, msize, &c->errstr) != msize) - return FALSE; - if (!ixp_recv_message(c->fd, msg, IXP_MAX_MSG, &c->errstr)) - return FALSE; - if (!(msize = ixp_msg_to_fcall(msg, IXP_MAX_MSG, &c->fcall))) { - c->errstr = "received bad message"; - return FALSE; - } - if (c->fcall.id == RERROR) { - c->errstr = c->fcall.errstr; - return FALSE; - } - return TRUE; + unsigned int msize = ixp_fcall_to_msg(&c->fcall, msg, IXP_MAX_MSG); + c->errstr = 0; + if(ixp_send_message(c->fd, msg, msize, &c->errstr) != msize) + return FALSE; + if(!ixp_recv_message(c->fd, msg, IXP_MAX_MSG, &c->errstr)) + return FALSE; + if(!(msize = ixp_msg_to_fcall(msg, IXP_MAX_MSG, &c->fcall))) { + c->errstr = "received bad message"; + return FALSE; + } + if(c->fcall.id == RERROR) { + c->errstr = c->fcall.errstr; + return FALSE; + } + return TRUE; } -int ixp_client_init(IXPClient * c, char *sockfile) +int +ixp_client_init(IXPClient * c, char *sockfile) { - if ((c->fd = ixp_connect_sock(sockfile)) < 0) { - c->errstr = "cannot connect server"; - return FALSE; - } - /* version */ - c->fcall.id = TVERSION; - c->fcall.tag = IXP_NOTAG; - c->fcall.maxmsg = IXP_MAX_MSG; - cext_strlcpy(c->fcall.version, IXP_VERSION, sizeof(c->fcall.version)); - if (!do_fcall(c)) { - ixp_client_deinit(c); - return FALSE; - } - if (strncmp(c->fcall.version, IXP_VERSION, strlen(IXP_VERSION))) { - c->errstr = "9P versions differ"; - ixp_client_deinit(c); - return FALSE; /* we cannot handle this version */ - } - c->root_fid = getpid(); + if((c->fd = ixp_connect_sock(sockfile)) < 0) { + c->errstr = "cannot connect server"; + return FALSE; + } + /* version */ + c->fcall.id = TVERSION; + c->fcall.tag = IXP_NOTAG; + c->fcall.maxmsg = IXP_MAX_MSG; + cext_strlcpy(c->fcall.version, IXP_VERSION, sizeof(c->fcall.version)); + if(!do_fcall(c)) { + ixp_client_deinit(c); + return FALSE; + } + if(strncmp(c->fcall.version, IXP_VERSION, strlen(IXP_VERSION))) { + c->errstr = "9P versions differ"; + ixp_client_deinit(c); + return FALSE; /* we cannot handle this version */ + } + c->root_fid = getpid(); - /* attach */ - c->fcall.id = TATTACH; - c->fcall.tag = IXP_NOTAG; - c->fcall.fid = c->root_fid; - c->fcall.afid = IXP_NOFID; - cext_strlcpy(c->fcall.uname, getenv("USER"), sizeof(c->fcall.uname)); - c->fcall.aname[0] = 0; - if (!do_fcall(c)) { - ixp_client_deinit(c); - return FALSE; - } - c->root_qid = c->fcall.qid; - return TRUE; + /* attach */ + c->fcall.id = TATTACH; + c->fcall.tag = IXP_NOTAG; + c->fcall.fid = c->root_fid; + c->fcall.afid = IXP_NOFID; + cext_strlcpy(c->fcall.uname, getenv("USER"), sizeof(c->fcall.uname)); + c->fcall.aname[0] = 0; + if(!do_fcall(c)) { + ixp_client_deinit(c); + return FALSE; + } + c->root_qid = c->fcall.qid; + return TRUE; } -int ixp_client_remove(IXPClient * c, u32 newfid, char *filepath) +int +ixp_client_remove(IXPClient * c, unsigned int newfid, char *filepath) { - if (!ixp_client_walk(c, newfid, filepath)) - return FALSE; - /* remove */ - c->fcall.id = TREMOVE; - c->fcall.tag = IXP_NOTAG; - c->fcall.fid = newfid; - return do_fcall(c); + if(!ixp_client_walk(c, newfid, filepath)) + return FALSE; + /* remove */ + c->fcall.id = TREMOVE; + c->fcall.tag = IXP_NOTAG; + c->fcall.fid = newfid; + return do_fcall(c); } int -ixp_client_create(IXPClient * c, u32 dirfid, char *name, u32 perm, u8 mode) +ixp_client_create(IXPClient * c, unsigned int dirfid, char *name, + unsigned int perm, unsigned char mode) { - /* create */ - c->fcall.id = TCREATE; - c->fcall.tag = IXP_NOTAG; - c->fcall.fid = dirfid; - cext_strlcpy(c->fcall.name, name, sizeof(c->fcall.name)); - c->fcall.perm = perm; - c->fcall.mode = mode; - return do_fcall(c); + /* create */ + c->fcall.id = TCREATE; + c->fcall.tag = IXP_NOTAG; + c->fcall.fid = dirfid; + cext_strlcpy(c->fcall.name, name, sizeof(c->fcall.name)); + c->fcall.perm = perm; + c->fcall.mode = mode; + return do_fcall(c); } -int ixp_client_walk(IXPClient * c, u32 newfid, char *filepath) +int +ixp_client_walk(IXPClient * c, unsigned int newfid, char *filepath) { - /* walk */ - c->fcall.id = TWALK; - c->fcall.fid = c->root_fid; - c->fcall.newfid = newfid; - if (filepath) { - cext_strlcpy(c->fcall.name, filepath, sizeof(c->fcall.name)); - c->fcall.nwname = cext_tokenize((char **) c->fcall.wname, IXP_MAX_WELEM, c->fcall.name, '/'); - } - return do_fcall(c); + /* walk */ + c->fcall.id = TWALK; + c->fcall.fid = c->root_fid; + c->fcall.newfid = newfid; + if(filepath) { + cext_strlcpy(c->fcall.name, filepath, sizeof(c->fcall.name)); + c->fcall.nwname = + cext_tokenize((char **) c->fcall.wname, IXP_MAX_WELEM, + c->fcall.name, '/'); + } + return do_fcall(c); } -int ixp_client_open(IXPClient * c, u32 newfid, char *filepath, u8 mode) +int +ixp_client_open(IXPClient * c, unsigned int newfid, char *filepath, + unsigned char mode) { - if (!ixp_client_walk(c, newfid, filepath)) - return FALSE; + if(!ixp_client_walk(c, newfid, filepath)) + return FALSE; - /* open */ - c->fcall.id = TOPEN; - c->fcall.tag = IXP_NOTAG; - c->fcall.fid = newfid; - c->fcall.mode = mode; - return do_fcall(c); + /* open */ + c->fcall.id = TOPEN; + c->fcall.tag = IXP_NOTAG; + c->fcall.fid = newfid; + c->fcall.mode = mode; + return do_fcall(c); } -u32 -ixp_client_read(IXPClient * c, u32 fid, u64 offset, void *result, - u32 res_len) +unsigned int +ixp_client_read(IXPClient * c, unsigned int fid, unsigned long long offset, + void *result, unsigned int res_len) { - u32 bytes = - c->fcall.maxmsg - (sizeof(u8) + sizeof(u16) + 2 * sizeof(u32) + - sizeof(u64)); - /* read */ - c->fcall.id = TREAD; - c->fcall.tag = IXP_NOTAG; - c->fcall.fid = fid; - c->fcall.offset = offset; - c->fcall.count = res_len < bytes ? res_len : bytes; - if (!do_fcall(c)) - return 0; - memcpy(result, c->fcall.data, c->fcall.count); - return c->fcall.count; + unsigned int bytes = + c->fcall.maxmsg - (sizeof(unsigned char) + sizeof(unsigned short) + + 2 * sizeof(unsigned int) + + sizeof(unsigned long long)); + /* read */ + c->fcall.id = TREAD; + c->fcall.tag = IXP_NOTAG; + c->fcall.fid = fid; + c->fcall.offset = offset; + c->fcall.count = res_len < bytes ? res_len : bytes; + if(!do_fcall(c)) + return 0; + memcpy(result, c->fcall.data, c->fcall.count); + return c->fcall.count; } -u32 -ixp_client_write(IXPClient * c, u32 fid, u64 offset, u32 count, u8 * data) +unsigned int +ixp_client_write(IXPClient * c, unsigned int fid, + unsigned long long offset, unsigned int count, + unsigned char *data) { - if (count > - c->fcall.maxmsg - (sizeof(u8) + sizeof(u16) + 2 * sizeof(u32) + - sizeof(u64))) { - c->errstr = "message size exceeds buffer size"; - return 0; - } - /* write */ - c->fcall.id = TWRITE; - c->fcall.tag = IXP_NOTAG; - c->fcall.fid = fid; - c->fcall.offset = offset; - c->fcall.count = count; - memcpy(c->fcall.data, data, count); - if (!do_fcall(c)) - return 0; - return c->fcall.count; + if(count > + c->fcall.maxmsg - (sizeof(unsigned char) + sizeof(unsigned short) + + 2 * sizeof(unsigned int) + + sizeof(unsigned long long))) { + c->errstr = "message size exceeds buffer size"; + return 0; + } + /* write */ + c->fcall.id = TWRITE; + c->fcall.tag = IXP_NOTAG; + c->fcall.fid = fid; + c->fcall.offset = offset; + c->fcall.count = count; + memcpy(c->fcall.data, data, count); + if(!do_fcall(c)) + return 0; + return c->fcall.count; } -int ixp_client_close(IXPClient * c, u32 fid) +int +ixp_client_close(IXPClient * c, unsigned int fid) { - /* clunk */ - c->fcall.id = TCLUNK; - c->fcall.tag = IXP_NOTAG; - c->fcall.fid = fid; - return do_fcall(c); + /* clunk */ + c->fcall.id = TCLUNK; + c->fcall.tag = IXP_NOTAG; + c->fcall.fid = fid; + return do_fcall(c); } -void ixp_client_deinit(IXPClient * c) +void +ixp_client_deinit(IXPClient * c) { - /* session finished, now shutdown */ - if (c->fd) { - shutdown(c->fd, SHUT_RDWR); - close(c->fd); - } + /* session finished, now shutdown */ + if(c->fd) { + shutdown(c->fd, SHUT_RDWR); + close(c->fd); + } } diff --git a/libixp2/convert.c b/libixp2/convert.c @@ -9,161 +9,189 @@ /* encode/decode stuff */ -void *ixp_enc_u8(u8 * msg, u8 val) +void * +ixp_enc_u8(unsigned char *msg, unsigned char val) { - msg[0] = val; - return &msg[1]; + msg[0] = val; + return &msg[1]; } -void *ixp_dec_u8(u8 * msg, u8 * val) +void * +ixp_dec_u8(unsigned char *msg, unsigned char *val) { - *val = msg[0]; - return &msg[1]; + *val = msg[0]; + return &msg[1]; } -void *ixp_enc_u16(u8 * msg, u16 val) +void * +ixp_enc_u16(unsigned char *msg, unsigned short val) { - msg[0] = val; - msg[1] = val >> 8; - return &msg[2]; + msg[0] = val; + msg[1] = val >> 8; + return &msg[2]; } -void *ixp_dec_u16(u8 * msg, u16 * val) +void * +ixp_dec_u16(unsigned char *msg, unsigned short *val) { - *val = msg[0] | (msg[1] << 8); - return &msg[2]; + *val = msg[0] | (msg[1] << 8); + return &msg[2]; } -void *ixp_enc_u32(u8 * msg, u32 val) +void * +ixp_enc_u32(unsigned char *msg, unsigned int val) { - msg[0] = val; - msg[1] = val >> 8; - msg[2] = val >> 16; - msg[3] = val >> 24; - return &msg[4]; + msg[0] = val; + msg[1] = val >> 8; + msg[2] = val >> 16; + msg[3] = val >> 24; + return &msg[4]; } -void *ixp_dec_u32(u8 * msg, u32 * val) +void * +ixp_dec_u32(unsigned char *msg, unsigned int *val) { - *val = msg[0] | (msg[1] << 8) | (msg[2] << 16) | (msg[3] << 24); - return &msg[4]; + *val = msg[0] | (msg[1] << 8) | (msg[2] << 16) | (msg[3] << 24); + return &msg[4]; } -void *ixp_enc_u64(u8 * msg, u64 val) +void * +ixp_enc_u64(unsigned char *msg, unsigned long long val) { - msg[0] = val; - msg[1] = val >> 8; - msg[2] = val >> 16; - msg[3] = val >> 24; - msg[4] = val >> 32; - msg[5] = val >> 40; - msg[6] = val >> 48; - msg[7] = val >> 56; - return &msg[8]; + msg[0] = val; + msg[1] = val >> 8; + msg[2] = val >> 16; + msg[3] = val >> 24; + msg[4] = val >> 32; + msg[5] = val >> 40; + msg[6] = val >> 48; + msg[7] = val >> 56; + return &msg[8]; } -void *ixp_dec_u64(u8 * msg, u64 * val) +void * +ixp_dec_u64(unsigned char *msg, unsigned long long *val) { - *val = (u64) msg[0] | ((u64) msg[1] << 8) | - ((u64) msg[2] << 16) | ((u64) msg[3] << 24) | - ((u64) msg[4] << 32) | ((u64) msg[5] << 40) | - ((u64) msg[6] << 48) | ((u64) msg[7] << 56); - return &msg[8]; + *val = + (unsigned long long) msg[0] | ((unsigned long long) msg[1] << 8) | + ((unsigned long long) msg[2] << 16) | ((unsigned long long) msg[3] + << 24) | ((unsigned long + long) msg[4] << + 32) | ((unsigned + long long) + msg[5] << + 40) | + ((unsigned long long) msg[6] << 48) | ((unsigned long long) msg[7] + << 56); + return &msg[8]; } -void *ixp_enc_string(u8 * msg, const char *s) +void * +ixp_enc_string(unsigned char *msg, const char *s) { - u16 len = s ? strlen(s) : 0; - msg = ixp_enc_u16(msg, len); - if (s) - memcpy(msg, s, len); - return &msg[len]; + unsigned short len = s ? strlen(s) : 0; + msg = ixp_enc_u16(msg, len); + if(s) + memcpy(msg, s, len); + return &msg[len]; } -void *ixp_dec_string(u8 * msg, char *string, u16 stringlen, u16 * len) +void * +ixp_dec_string(unsigned char *msg, char *string, unsigned short stringlen, + unsigned short *len) { - msg = ixp_dec_u16(msg, len); - if (!(*len)) - return msg; - if (*len > stringlen - 1) - /* might never happen if stringlen == IXP_MAX_MSG */ - string[0] = 0; - else { - memcpy(string, msg, *len); - string[*len] = 0; - } - return &msg[*len]; + msg = ixp_dec_u16(msg, len); + if(!(*len)) + return msg; + if(*len > stringlen - 1) + /* might never happen if stringlen == IXP_MAX_MSG */ + string[0] = 0; + else { + memcpy(string, msg, *len); + string[*len] = 0; + } + return &msg[*len]; } -void *ixp_enc_data(u8 * msg, u8 * data, u32 datalen) +void * +ixp_enc_data(unsigned char *msg, unsigned char *data, unsigned int datalen) { - memcpy(msg, data, datalen); - return &msg[datalen]; + memcpy(msg, data, datalen); + return &msg[datalen]; } -void *ixp_dec_data(u8 * msg, u8 * data, u32 datalen) +void * +ixp_dec_data(unsigned char *msg, unsigned char *data, unsigned int datalen) { - memcpy(data, msg, datalen); - return &msg[datalen]; + memcpy(data, msg, datalen); + return &msg[datalen]; } -void *ixp_enc_prefix(u8 * msg, u32 size, u8 id, u16 tag) +void * +ixp_enc_prefix(unsigned char *msg, unsigned int size, unsigned char id, + unsigned short tag) { - msg = ixp_enc_u32(msg, size); - msg = ixp_enc_u8(msg, id); - return ixp_enc_u16(msg, tag); + msg = ixp_enc_u32(msg, size); + msg = ixp_enc_u8(msg, id); + return ixp_enc_u16(msg, tag); } -void *ixp_dec_prefix(u8 * msg, u32 * size, u8 * id, u16 * tag) +void * +ixp_dec_prefix(unsigned char *msg, unsigned int *size, unsigned char *id, + unsigned short *tag) { - msg = ixp_dec_u32(msg, size); - msg = ixp_dec_u8(msg, id); - return ixp_dec_u16(msg, tag); + msg = ixp_dec_u32(msg, size); + msg = ixp_dec_u8(msg, id); + return ixp_dec_u16(msg, tag); } -void *ixp_enc_qid(u8 * msg, Qid * qid) +void * +ixp_enc_qid(unsigned char *msg, Qid * qid) { - msg = ixp_enc_u8(msg, qid->type); - msg = ixp_enc_u32(msg, qid->version); - return ixp_enc_u64(msg, qid->path); + msg = ixp_enc_u8(msg, qid->type); + msg = ixp_enc_u32(msg, qid->version); + return ixp_enc_u64(msg, qid->path); } -void *ixp_dec_qid(u8 * msg, Qid * qid) +void * +ixp_dec_qid(unsigned char *msg, Qid * qid) { - msg = ixp_dec_u8(msg, &qid->type); - msg = ixp_dec_u32(msg, &qid->version); - return ixp_dec_u64(msg, &qid->path); + msg = ixp_dec_u8(msg, &qid->type); + msg = ixp_dec_u32(msg, &qid->version); + return ixp_dec_u64(msg, &qid->path); } -void *ixp_enc_stat(u8 * msg, Stat * stat) +void * +ixp_enc_stat(unsigned char *msg, Stat * stat) { - msg = ixp_enc_u16(msg, stat->size); - msg = ixp_enc_u16(msg, stat->type); - msg = ixp_enc_u32(msg, stat->dev); - msg = ixp_enc_qid(msg, &stat->qid); - msg = ixp_enc_u32(msg, stat->mode); - msg = ixp_enc_u32(msg, stat->atime); - msg = ixp_enc_u32(msg, stat->mtime); - msg = ixp_enc_u64(msg, stat->length); - msg = ixp_enc_string(msg, stat->name); - msg = ixp_enc_string(msg, stat->uid); - msg = ixp_enc_string(msg, stat->gid); - return ixp_enc_string(msg, stat->muid); + msg = ixp_enc_u16(msg, stat->size); + msg = ixp_enc_u16(msg, stat->type); + msg = ixp_enc_u32(msg, stat->dev); + msg = ixp_enc_qid(msg, &stat->qid); + msg = ixp_enc_u32(msg, stat->mode); + msg = ixp_enc_u32(msg, stat->atime); + msg = ixp_enc_u32(msg, stat->mtime); + msg = ixp_enc_u64(msg, stat->length); + msg = ixp_enc_string(msg, stat->name); + msg = ixp_enc_string(msg, stat->uid); + msg = ixp_enc_string(msg, stat->gid); + return ixp_enc_string(msg, stat->muid); } -void *ixp_dec_stat(u8 * msg, Stat * stat) +void * +ixp_dec_stat(unsigned char *msg, Stat * stat) { - u16 len; - msg = ixp_dec_u16(msg, &stat->size); - msg = ixp_dec_u16(msg, &stat->type); - msg = ixp_dec_u32(msg, &stat->dev); - msg = ixp_dec_qid(msg, &stat->qid); - msg = ixp_dec_u32(msg, &stat->mode); - msg = ixp_dec_u32(msg, &stat->atime); - msg = ixp_dec_u32(msg, &stat->mtime); - msg = ixp_dec_u64(msg, &stat->length); - msg = ixp_dec_string(msg, stat->name, sizeof(stat->name), &len); - msg = ixp_dec_string(msg, stat->uid, sizeof(stat->uid), &len); - msg = ixp_dec_string(msg, stat->gid, sizeof(stat->gid), &len); - return ixp_dec_string(msg, stat->muid, sizeof(stat->muid), &len); + unsigned short len; + msg = ixp_dec_u16(msg, &stat->size); + msg = ixp_dec_u16(msg, &stat->type); + msg = ixp_dec_u32(msg, &stat->dev); + msg = ixp_dec_qid(msg, &stat->qid); + msg = ixp_dec_u32(msg, &stat->mode); + msg = ixp_dec_u32(msg, &stat->atime); + msg = ixp_dec_u32(msg, &stat->mtime); + msg = ixp_dec_u64(msg, &stat->length); + msg = ixp_dec_string(msg, stat->name, sizeof(stat->name), &len); + msg = ixp_dec_string(msg, stat->uid, sizeof(stat->uid), &len); + msg = ixp_dec_string(msg, stat->gid, sizeof(stat->gid), &len); + return ixp_dec_string(msg, stat->muid, sizeof(stat->muid), &len); } diff --git a/libixp2/ixp.h b/libixp2/ixp.h @@ -23,7 +23,7 @@ #define IXP_MAX_FLEN 128 #define IXP_MAX_ULEN 32 #define IXP_MAX_STAT 64 -#define IXP_MAX_WELEM 16 /* MAXWELEM */ +#define IXP_MAX_WELEM 16 /* MAXWELEM */ #define IXP_MAX_TFUNCS 14 @@ -76,198 +76,207 @@ /* 9P message types */ enum { - TVERSION = 100, - RVERSION, - TAUTH = 102, - RAUTH, - TATTACH = 104, - RATTACH, - TERROR = 106, - RERROR, - TFLUSH = 108, - RFLUSH, - TWALK = 110, - RWALK, - TOPEN = 112, - ROPEN, - TCREATE = 114, - RCREATE, - TREAD = 116, - RREAD, - TWRITE = 118, - RWRITE, - TCLUNK = 120, - RCLUNK, - TREMOVE = 122, - RREMOVE, - TSTAT = 124, - RSTAT, - TWSTAT = 126, - RWSTAT, + TVERSION = 100, + RVERSION, + TAUTH = 102, + RAUTH, + TATTACH = 104, + RATTACH, + TERROR = 106, + RERROR, + TFLUSH = 108, + RFLUSH, + TWALK = 110, + RWALK, + TOPEN = 112, + ROPEN, + TCREATE = 114, + RCREATE, + TREAD = 116, + RREAD, + TWRITE = 118, + RWRITE, + TCLUNK = 120, + RCLUNK, + TREMOVE = 122, + RREMOVE, + TSTAT = 124, + RSTAT, + TWSTAT = 126, + RWSTAT, }; /* modes */ enum { - IXP_OREAD = 0x00, - IXP_OWRITE = 0x01, - IXP_ORDWR = 0x02, - IXP_OEXEC = 0x03, - IXP_OEXCL = 0x04, - IXP_OTRUNC = 0x10, - IXP_OREXEC = 0x20, - IXP_ORCLOSE = 0x40, - IXP_OAPPEND = 0x80, + IXP_OREAD = 0x00, + IXP_OWRITE = 0x01, + IXP_ORDWR = 0x02, + IXP_OEXEC = 0x03, + IXP_OEXCL = 0x04, + IXP_OTRUNC = 0x10, + IXP_OREXEC = 0x20, + IXP_ORCLOSE = 0x40, + IXP_OAPPEND = 0x80, }; /* qid.types */ enum { - IXP_QTDIR = 0x80, - IXP_QTAPPEND = 0x40, - IXP_QTEXCL = 0x20, - IXP_QTMOUNT = 0x10, - IXP_QTAUTH = 0x08, - IXP_QTTMP = 0x04, - IXP_QTSYMLINK = 0x02, - IXP_QTLINK = 0x01, - IXP_QTFILE = 0x00, + IXP_QTDIR = 0x80, + IXP_QTAPPEND = 0x40, + IXP_QTEXCL = 0x20, + IXP_QTMOUNT = 0x10, + IXP_QTAUTH = 0x08, + IXP_QTTMP = 0x04, + IXP_QTSYMLINK = 0x02, + IXP_QTLINK = 0x01, + IXP_QTFILE = 0x00, }; -/* this should work on all architectures */ -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; -typedef unsigned long long u64; - -#define IXP_NOTAG (u16)~0U /* Dummy tag */ -#define IXP_NOFID (u32)~0 /* No auth */ +#define IXP_NOTAG (unsigned short)~0U /* Dummy tag */ +#define IXP_NOFID (unsigned int)~0 /* No auth */ typedef struct { - u8 type; - u32 version; - u64 path; + unsigned char type; + unsigned int version; + unsigned long long path; } Qid; /* stat structure */ typedef struct { - u16 size; - u16 type; - u32 dev; - Qid qid; - u32 mode; - u32 atime; - u32 mtime; - u64 length; - char name[IXP_MAX_FLEN]; - char uid[IXP_MAX_ULEN]; - char gid[IXP_MAX_ULEN]; - char muid[IXP_MAX_ULEN]; + unsigned short size; + unsigned short type; + unsigned int dev; + Qid qid; + unsigned int mode; + unsigned int atime; + unsigned int mtime; + unsigned long long length; + char name[IXP_MAX_FLEN]; + char uid[IXP_MAX_ULEN]; + char gid[IXP_MAX_ULEN]; + char muid[IXP_MAX_ULEN]; } Stat; typedef struct { - u8 id; - u16 tag; - u32 fid; - u32 maxmsg; /* Tversion, Rversion */ - char version[IXP_MAX_VERSION]; /* Tversion, Rversion */ - u16 oldtag; /* Tflush */ - char errstr[IXP_MAX_ERROR]; /* Rerror */ - Qid qid; /* Rattach, Ropen, Rcreate */ - u32 iounit; /* Ropen, Rcreate */ - Qid aqid; /* Rauth */ - u32 afid; /* Tauth, Tattach */ - char uname[IXP_MAX_ULEN]; /* Tauth, Tattach */ - char aname[IXP_MAX_FLEN]; /* Tauth, Tattach */ - u32 perm; /* Tcreate */ - char name[IXP_MAX_FLEN]; /* Tcreate */ - u8 mode; /* Tcreate, Topen */ - u32 newfid; /* Twalk */ - u16 nwname; /* Twalk */ - char *wname[IXP_MAX_WELEM]; /* Twalk */ - u16 nwqid; /* Rwalk */ - Qid wqid[IXP_MAX_WELEM]; /* Rwalk */ - u64 offset; /* Tread, Twrite */ - u32 count; /* Tread, Twrite, Rread */ - Stat stat; /* Rstat */ - u16 nstat; /* Twstat, Rstat */ - u8 data[IXP_MAX_MSG]; /* Twrite, Rread, Twstat, - * Rstat */ + unsigned char id; + unsigned short tag; + unsigned int fid; + unsigned int maxmsg; /* Tversion, Rversion */ + char version[IXP_MAX_VERSION]; /* Tversion, Rversion */ + unsigned short oldtag; /* Tflush */ + char errstr[IXP_MAX_ERROR]; /* Rerror */ + Qid qid; /* Rattach, Ropen, Rcreate */ + unsigned int iounit; /* Ropen, Rcreate */ + Qid aqid; /* Rauth */ + unsigned int afid; /* Tauth, Tattach */ + char uname[IXP_MAX_ULEN]; /* Tauth, Tattach */ + char aname[IXP_MAX_FLEN]; /* Tauth, Tattach */ + unsigned int perm; /* Tcreate */ + char name[IXP_MAX_FLEN]; /* Tcreate */ + unsigned char mode; /* Tcreate, Topen */ + unsigned int newfid; /* Twalk */ + unsigned short nwname; /* Twalk */ + char *wname[IXP_MAX_WELEM]; /* Twalk */ + unsigned short nwqid; /* Rwalk */ + Qid wqid[IXP_MAX_WELEM]; /* Rwalk */ + unsigned long long offset; /* Tread, Twrite */ + unsigned int count; /* Tread, Twrite, Rread */ + Stat stat; /* Rstat */ + unsigned short nstat; /* Twstat, Rstat */ + unsigned char data[IXP_MAX_MSG]; /* Twrite, Rread, Twstat, + * Rstat */ } Fcall; typedef struct IXPServer IXPServer; typedef struct IXPConn IXPConn; typedef struct { - u8 id; - int (*tfunc) (IXPServer *, IXPConn *); + unsigned char id; + int (*tfunc) (IXPServer *, IXPConn *); } IXPTFunc; struct IXPConn { - int fd; - int dont_close; - void (*read) (IXPServer *, IXPConn *); - void *aux; + int fd; + int dont_close; + void (*read) (IXPServer *, IXPConn *); + void *aux; }; struct IXPServer { - int running; - IXPConn conn[IXP_MAX_CONN]; - void (*freeconn) (IXPServer *, IXPConn *); - int maxfd; - fd_set rd; - IXPTFunc *funcs; - Fcall fcall; - char *errstr; + int running; + IXPConn conn[IXP_MAX_CONN]; + void (*freeconn) (IXPServer *, IXPConn *); + int maxfd; + fd_set rd; + IXPTFunc *funcs; + Fcall fcall; + char *errstr; }; typedef struct { - int fd; - u32 root_fid; - Qid root_qid; - Fcall fcall; - char *errstr; + int fd; + unsigned int root_fid; + Qid root_qid; + Fcall fcall; + char *errstr; } IXPClient; /* client.c */ int ixp_client_init(IXPClient * c, char *sockfile); void ixp_client_deinit(IXPClient * c); -int ixp_client_remove(IXPClient * c, u32 newfid, char *filepath); -int ixp_client_create(IXPClient * c, u32 dirfid, char *name, u32 perm, u8 mode); -int ixp_client_walk(IXPClient * c, u32 newfid, char *filepath); -int ixp_client_open(IXPClient * c, u32 newfid, char *filepath, u8 mode); -u32 ixp_client_read(IXPClient * c, u32 fid, u64 offset, void *result, u32 res_len); -u32 ixp_client_write(IXPClient * c, u32 fid, u64 offset, u32 count, u8 * data); -int ixp_client_close(IXPClient * c, u32 fid); +int ixp_client_remove(IXPClient * c, unsigned int newfid, char *filepath); +int ixp_client_create(IXPClient * c, unsigned int dirfid, char *name, + unsigned int perm, unsigned char mode); +int ixp_client_walk(IXPClient * c, unsigned int newfid, char *filepath); +int ixp_client_open(IXPClient * c, unsigned int newfid, char *filepath, + unsigned char mode); +unsigned int ixp_client_read(IXPClient * c, unsigned int fid, + unsigned long long offset, void *result, + unsigned int res_len); +unsigned int ixp_client_write(IXPClient * c, unsigned int fid, + unsigned long long offset, + unsigned int count, unsigned char *data); +int ixp_client_close(IXPClient * c, unsigned int fid); /* convert.c */ -void *ixp_enc_u8(u8 * msg, u8 val); -void *ixp_dec_u8(u8 * msg, u8 * val); -void *ixp_enc_u16(u8 * msg, u16 val); -void *ixp_dec_u16(u8 * msg, u16 * val); -void *ixp_enc_u32(u8 * msg, u32 val); -void *ixp_dec_u32(u8 * msg, u32 * val); -void *ixp_enc_u64(u8 * msg, u64 val); -void *ixp_dec_u64(u8 * msg, u64 * val); -void *ixp_enc_string(u8 * msg, const char *s); -void *ixp_dec_string(u8 * msg, char *string, u16 stringlen, u16 * len); -void *ixp_enc_data(u8 * msg, u8 * data, u32 datalen); -void *ixp_dec_data(u8 * msg, u8 * data, u32 datalen); -void *ixp_enc_prefix(u8 * msg, u32 size, u8 id, u16 tag); -void *ixp_dec_prefix(u8 * msg, u32 * size, u8 * id, u16 * tag); -void *ixp_enc_qid(u8 * msg, Qid * qid); -void *ixp_dec_qid(u8 * msg, Qid * qid); -void *ixp_enc_stat(u8 * msg, Stat * stat); -void *ixp_dec_stat(u8 * msg, Stat * stat); +void *ixp_enc_u8(unsigned char *msg, unsigned char val); +void *ixp_dec_u8(unsigned char *msg, unsigned char *val); +void *ixp_enc_u16(unsigned char *msg, unsigned short val); +void *ixp_dec_u16(unsigned char *msg, unsigned short *val); +void *ixp_enc_u32(unsigned char *msg, unsigned int val); +void *ixp_dec_u32(unsigned char *msg, unsigned int *val); +void *ixp_enc_u64(unsigned char *msg, unsigned long long val); +void *ixp_dec_u64(unsigned char *msg, unsigned long long *val); +void *ixp_enc_string(unsigned char *msg, const char *s); +void *ixp_dec_string(unsigned char *msg, char *string, + unsigned short stringlen, unsigned short *len); +void *ixp_enc_data(unsigned char *msg, unsigned char *data, + unsigned int datalen); +void *ixp_dec_data(unsigned char *msg, unsigned char *data, + unsigned int datalen); +void *ixp_enc_prefix(unsigned char *msg, unsigned int size, + unsigned char id, unsigned short tag); +void *ixp_dec_prefix(unsigned char *msg, unsigned int *size, + unsigned char *id, unsigned short *tag); +void *ixp_enc_qid(unsigned char *msg, Qid * qid); +void *ixp_dec_qid(unsigned char *msg, Qid * qid); +void *ixp_enc_stat(unsigned char *msg, Stat * stat); +void *ixp_dec_stat(unsigned char *msg, Stat * stat); /* message.c */ -u16 ixp_sizeof_stat(Stat * stat); -u32 ixp_fcall_to_msg(Fcall * fcall, void *msg, u32 msglen); -u32 ixp_msg_to_fcall(void *msg, u32 msglen, Fcall * fcall); +unsigned short ixp_sizeof_stat(Stat * stat); +unsigned int ixp_fcall_to_msg(Fcall * fcall, void *msg, + unsigned int msglen); +unsigned int ixp_msg_to_fcall(void *msg, unsigned int msglen, + Fcall * fcall); /* server.c */ -IXPConn *ixp_server_add_conn(IXPServer * s, int fd, int dont_close, void (*read) (IXPServer *, IXPConn *)); +IXPConn *ixp_server_add_conn(IXPServer * s, int fd, int dont_close, + void (*read) (IXPServer *, IXPConn *)); int ixp_server_tversion(IXPServer *, IXPConn * c); void ixp_server_rm_conn(IXPServer * s, IXPConn * c); void ixp_server_loop(IXPServer * s); -int ixp_server_init(IXPServer * s, char *sockfile, IXPTFunc * funcs, void (*freeconn) (IXPServer *, IXPConn *)); +int ixp_server_init(IXPServer * s, char *sockfile, IXPTFunc * funcs, + void (*freeconn) (IXPServer *, IXPConn *)); void ixp_server_deinit(IXPServer * s); /* socket.c */ @@ -276,5 +285,7 @@ int ixp_accept_sock(int fd); int ixp_create_sock(char *sockfile, char **errstr); /* transport.c */ -u32 ixp_send_message(int fd, void *msg, u32 msize, char **errstr); -u32 ixp_recv_message(int fd, void *msg, u32 msglen, char **errstr); +unsigned int ixp_send_message(int fd, void *msg, unsigned int msize, + char **errstr); +unsigned int ixp_recv_message(int fd, void *msg, unsigned int msglen, + char **errstr); diff --git a/libixp2/message.c b/libixp2/message.c @@ -7,286 +7,297 @@ #include <string.h> #include "ixp.h" -static u16 sizeof_string(const char *s) +static unsigned short +sizeof_string(const char *s) { - return sizeof(u16) + strlen(s); + return sizeof(unsigned short) + strlen(s); } -u16 ixp_sizeof_stat(Stat * stat) +unsigned short +ixp_sizeof_stat(Stat * stat) { - return sizeof(Qid) - + 2 * sizeof(u16) - + 4 * sizeof(u32) - + sizeof(u64) - + sizeof_string(stat->name) - + sizeof_string(stat->uid) - + sizeof_string(stat->gid) - + sizeof_string(stat->muid); + return sizeof(Qid) + + 2 * sizeof(unsigned short) + + 4 * sizeof(unsigned int) + + sizeof(unsigned long long) + + sizeof_string(stat->name) + + sizeof_string(stat->uid) + + sizeof_string(stat->gid) + + sizeof_string(stat->muid); } -u32 ixp_fcall_to_msg(Fcall * fcall, void *msg, u32 msglen) +unsigned int +ixp_fcall_to_msg(Fcall * fcall, void *msg, unsigned int msglen) { - u32 i, msize = sizeof(u8) + sizeof(u16) + sizeof(u32); - void *p = msg; + unsigned int i, msize = + sizeof(unsigned char) + sizeof(unsigned short) + + sizeof(unsigned int); + void *p = msg; - switch (fcall->id) { - case TVERSION: - case RVERSION: - msize += sizeof(u32) + sizeof_string(fcall->version); - break; - case TAUTH: - msize += - sizeof(u32) + sizeof_string(fcall->uname) + - sizeof_string(fcall->aname); - break; - case RAUTH: - case RATTACH: - msize += sizeof(Qid); - break; - case TATTACH: - msize += - 2 * sizeof(u32) + sizeof_string(fcall->uname) + - sizeof_string(fcall->aname); - break; - case RERROR: - msize += sizeof_string(fcall->errstr); - break; - case RWRITE: - case TCLUNK: - case TREMOVE: - case TSTAT: - msize += sizeof(u32); - break; - case TWALK: - msize += sizeof(u16) + 2 * sizeof(u32); - for (i = 0; i < fcall->nwname; i++) - msize += sizeof_string(fcall->wname[i]); - break; - case TFLUSH: - msize += sizeof(u16); - break; - case RWALK: - msize += sizeof(u16) + fcall->nwqid * sizeof(Qid); - break; - case TOPEN: - msize += sizeof(u32) + sizeof(u8); - break; - case ROPEN: - case RCREATE: - msize += sizeof(Qid) + sizeof(u32); - break; - case TCREATE: - msize += sizeof(u8) + 2 * sizeof(u32) + sizeof_string(fcall->name); - break; - case TREAD: - msize += 2 * sizeof(u32) + sizeof(u64); - break; - case RREAD: - msize += sizeof(u32) + fcall->count; - break; - case TWRITE: - msize += 2 * sizeof(u32) + sizeof(u64) + fcall->count; - break; - case RSTAT: - msize += ixp_sizeof_stat(&fcall->stat); - break; - case TWSTAT: - msize += sizeof(u32) + ixp_sizeof_stat(&fcall->stat); - break; - default: - break; - } + switch (fcall->id) { + case TVERSION: + case RVERSION: + msize += sizeof(unsigned int) + sizeof_string(fcall->version); + break; + case TAUTH: + msize += + sizeof(unsigned int) + sizeof_string(fcall->uname) + + sizeof_string(fcall->aname); + break; + case RAUTH: + case RATTACH: + msize += sizeof(Qid); + break; + case TATTACH: + msize += + 2 * sizeof(unsigned int) + sizeof_string(fcall->uname) + + sizeof_string(fcall->aname); + break; + case RERROR: + msize += sizeof_string(fcall->errstr); + break; + case RWRITE: + case TCLUNK: + case TREMOVE: + case TSTAT: + msize += sizeof(unsigned int); + break; + case TWALK: + msize += sizeof(unsigned short) + 2 * sizeof(unsigned int); + for(i = 0; i < fcall->nwname; i++) + msize += sizeof_string(fcall->wname[i]); + break; + case TFLUSH: + msize += sizeof(unsigned short); + break; + case RWALK: + msize += sizeof(unsigned short) + fcall->nwqid * sizeof(Qid); + break; + case TOPEN: + msize += sizeof(unsigned int) + sizeof(unsigned char); + break; + case ROPEN: + case RCREATE: + msize += sizeof(Qid) + sizeof(unsigned int); + break; + case TCREATE: + msize += + sizeof(unsigned char) + 2 * sizeof(unsigned int) + + sizeof_string(fcall->name); + break; + case TREAD: + msize += 2 * sizeof(unsigned int) + sizeof(unsigned long long); + break; + case RREAD: + msize += sizeof(unsigned int) + fcall->count; + break; + case TWRITE: + msize += + 2 * sizeof(unsigned int) + sizeof(unsigned long long) + + fcall->count; + break; + case RSTAT: + msize += ixp_sizeof_stat(&fcall->stat); + break; + case TWSTAT: + msize += sizeof(unsigned int) + ixp_sizeof_stat(&fcall->stat); + break; + default: + break; + } - if (msize > msglen) - return 0; - p = ixp_enc_prefix(p, msize, fcall->id, fcall->tag); + if(msize > msglen) + return 0; + p = ixp_enc_prefix(p, msize, fcall->id, fcall->tag); - switch (fcall->id) { - case TVERSION: - case RVERSION: - p = ixp_enc_u32(p, fcall->maxmsg); - p = ixp_enc_string(p, fcall->version); - break; - case TAUTH: - p = ixp_enc_u32(p, fcall->afid); - p = ixp_enc_string(p, fcall->uname); - p = ixp_enc_string(p, fcall->aname); - break; - case RAUTH: - p = ixp_enc_qid(p, &fcall->aqid); - break; - case RATTACH: - p = ixp_enc_qid(p, &fcall->qid); - break; - case TATTACH: - p = ixp_enc_u32(p, fcall->fid); - p = ixp_enc_u32(p, fcall->afid); - p = ixp_enc_string(p, fcall->uname); - p = ixp_enc_string(p, fcall->aname); - break; - case RERROR: - p = ixp_enc_string(p, fcall->errstr); - break; - case TFLUSH: - p = ixp_enc_u16(p, fcall->oldtag); - break; - case TWALK: - p = ixp_enc_u32(p, fcall->fid); - p = ixp_enc_u32(p, fcall->newfid); - p = ixp_enc_u16(p, fcall->nwname); - for (i = 0; i < fcall->nwname; i++) - p = ixp_enc_string(p, fcall->wname[i]); - break; - case RWALK: - p = ixp_enc_u16(p, fcall->nwqid); - for (i = 0; i < fcall->nwqid; i++) - p = ixp_enc_qid(p, &fcall->wqid[i]); - break; - case TOPEN: - p = ixp_enc_u32(p, fcall->fid); - p = ixp_enc_u8(p, fcall->mode); - break; - case ROPEN: - case RCREATE: - p = ixp_enc_qid(p, &fcall->qid); - p = ixp_enc_u32(p, fcall->iounit); - break; - case TCREATE: - p = ixp_enc_u32(p, fcall->fid); - p = ixp_enc_string(p, fcall->name); - p = ixp_enc_u32(p, fcall->perm); - p = ixp_enc_u8(p, fcall->mode); - break; - case TREAD: - p = ixp_enc_u32(p, fcall->fid); - p = ixp_enc_u64(p, fcall->offset); - p = ixp_enc_u32(p, fcall->count); - break; - case RREAD: - p = ixp_enc_u32(p, fcall->count); - p = ixp_enc_data(p, fcall->data, fcall->count); - break; - case TWRITE: - p = ixp_enc_u32(p, fcall->fid); - p = ixp_enc_u64(p, fcall->offset); - p = ixp_enc_u32(p, fcall->count); - p = ixp_enc_data(p, fcall->data, fcall->count); - break; - case RWRITE: - p = ixp_enc_u32(p, fcall->count); - break; - case TCLUNK: - case TREMOVE: - case TSTAT: - p = ixp_enc_u32(p, fcall->fid); - break; - case RSTAT: - p = ixp_enc_stat(p, &fcall->stat); - break; - case TWSTAT: - p = ixp_enc_u32(p, fcall->fid); - p = ixp_enc_stat(p, &fcall->stat); - break; - } - return msize; + switch (fcall->id) { + case TVERSION: + case RVERSION: + p = ixp_enc_u32(p, fcall->maxmsg); + p = ixp_enc_string(p, fcall->version); + break; + case TAUTH: + p = ixp_enc_u32(p, fcall->afid); + p = ixp_enc_string(p, fcall->uname); + p = ixp_enc_string(p, fcall->aname); + break; + case RAUTH: + p = ixp_enc_qid(p, &fcall->aqid); + break; + case RATTACH: + p = ixp_enc_qid(p, &fcall->qid); + break; + case TATTACH: + p = ixp_enc_u32(p, fcall->fid); + p = ixp_enc_u32(p, fcall->afid); + p = ixp_enc_string(p, fcall->uname); + p = ixp_enc_string(p, fcall->aname); + break; + case RERROR: + p = ixp_enc_string(p, fcall->errstr); + break; + case TFLUSH: + p = ixp_enc_u16(p, fcall->oldtag); + break; + case TWALK: + p = ixp_enc_u32(p, fcall->fid); + p = ixp_enc_u32(p, fcall->newfid); + p = ixp_enc_u16(p, fcall->nwname); + for(i = 0; i < fcall->nwname; i++) + p = ixp_enc_string(p, fcall->wname[i]); + break; + case RWALK: + p = ixp_enc_u16(p, fcall->nwqid); + for(i = 0; i < fcall->nwqid; i++) + p = ixp_enc_qid(p, &fcall->wqid[i]); + break; + case TOPEN: + p = ixp_enc_u32(p, fcall->fid); + p = ixp_enc_u8(p, fcall->mode); + break; + case ROPEN: + case RCREATE: + p = ixp_enc_qid(p, &fcall->qid); + p = ixp_enc_u32(p, fcall->iounit); + break; + case TCREATE: + p = ixp_enc_u32(p, fcall->fid); + p = ixp_enc_string(p, fcall->name); + p = ixp_enc_u32(p, fcall->perm); + p = ixp_enc_u8(p, fcall->mode); + break; + case TREAD: + p = ixp_enc_u32(p, fcall->fid); + p = ixp_enc_u64(p, fcall->offset); + p = ixp_enc_u32(p, fcall->count); + break; + case RREAD: + p = ixp_enc_u32(p, fcall->count); + p = ixp_enc_data(p, fcall->data, fcall->count); + break; + case TWRITE: + p = ixp_enc_u32(p, fcall->fid); + p = ixp_enc_u64(p, fcall->offset); + p = ixp_enc_u32(p, fcall->count); + p = ixp_enc_data(p, fcall->data, fcall->count); + break; + case RWRITE: + p = ixp_enc_u32(p, fcall->count); + break; + case TCLUNK: + case TREMOVE: + case TSTAT: + p = ixp_enc_u32(p, fcall->fid); + break; + case RSTAT: + p = ixp_enc_stat(p, &fcall->stat); + break; + case TWSTAT: + p = ixp_enc_u32(p, fcall->fid); + p = ixp_enc_stat(p, &fcall->stat); + break; + } + return msize; } -u32 ixp_msg_to_fcall(void *msg, u32 msglen, Fcall * fcall) +unsigned int +ixp_msg_to_fcall(void *msg, unsigned int msglen, Fcall * fcall) { - u32 i, msize; - u16 len; - void *p = ixp_dec_prefix(msg, &msize, &fcall->id, &fcall->tag); + unsigned int i, msize; + unsigned short len; + void *p = ixp_dec_prefix(msg, &msize, &fcall->id, &fcall->tag); - if (msize > msglen) /* bad message */ - return 0; + if(msize > msglen) /* bad message */ + return 0; - switch (fcall->id) { - case TVERSION: - case RVERSION: - p = ixp_dec_u32(p, &fcall->maxmsg); - p = ixp_dec_string(p, fcall->version, sizeof(fcall->version), &len); - break; - case TAUTH: - p = ixp_dec_u32(p, &fcall->afid); - p = ixp_dec_string(p, fcall->uname, sizeof(fcall->uname), &len); - p = ixp_dec_string(p, fcall->aname, sizeof(fcall->aname), &len); - break; - case RAUTH: - p = ixp_dec_qid(p, &fcall->aqid); - break; - case RATTACH: - p = ixp_dec_qid(p, &fcall->qid); - break; - case TATTACH: - p = ixp_dec_u32(p, &fcall->fid); - p = ixp_dec_u32(p, &fcall->afid); - p = ixp_dec_string(p, fcall->uname, sizeof(fcall->uname), &len); - p = ixp_dec_string(p, fcall->aname, sizeof(fcall->aname), &len); - break; - case RERROR: - p = ixp_dec_string(p, fcall->errstr, sizeof(fcall->errstr), &len); - break; - case TFLUSH: - p = ixp_dec_u16(p, &fcall->oldtag); - break; - case TWALK: - p = ixp_dec_u32(p, &fcall->fid); - p = ixp_dec_u32(p, &fcall->newfid); - p = ixp_dec_u16(p, &fcall->nwname); - for (i = 0; i < fcall->nwname; i++) - p = ixp_dec_string(p, fcall->wname[i], IXP_MAX_FLEN, &len); - break; - case RWALK: - p = ixp_dec_u16(p, &fcall->nwqid); - for (i = 0; i < fcall->nwqid; i++) - p = ixp_dec_qid(p, &fcall->wqid[i]); - break; - case TOPEN: - p = ixp_dec_u32(p, &fcall->fid); - p = ixp_dec_u8(p, &fcall->mode); - break; - case ROPEN: - case RCREATE: - p = ixp_dec_qid(p, &fcall->qid); - p = ixp_dec_u32(p, &fcall->iounit); - break; - case TCREATE: - p = ixp_dec_u32(p, &fcall->fid); - p = ixp_dec_string(p, fcall->name, sizeof(fcall->name), &len); - p = ixp_dec_u32(p, &fcall->perm); - p = ixp_dec_u8(p, &fcall->mode); - break; - case TREAD: - p = ixp_dec_u32(p, &fcall->fid); - p = ixp_dec_u64(p, &fcall->offset); - p = ixp_dec_u32(p, &fcall->count); - break; - case RREAD: - p = ixp_dec_u32(p, &fcall->count); - p = ixp_dec_data(p, fcall->data, fcall->count); - break; - case TWRITE: - p = ixp_dec_u32(p, &fcall->fid); - p = ixp_dec_u64(p, &fcall->offset); - p = ixp_dec_u32(p, &fcall->count); - p = ixp_dec_data(p, fcall->data, fcall->count); - break; - case RWRITE: - p = ixp_dec_u32(p, &fcall->count); - break; - case TCLUNK: - case TREMOVE: - case TSTAT: - p = ixp_dec_u32(p, &fcall->fid); - break; - case RSTAT: - p = ixp_dec_stat(p, &fcall->stat); - break; - case TWSTAT: - p = ixp_dec_u32(p, &fcall->fid); - p = ixp_dec_stat(p, &fcall->stat); - break; - } + switch (fcall->id) { + case TVERSION: + case RVERSION: + p = ixp_dec_u32(p, &fcall->maxmsg); + p = ixp_dec_string(p, fcall->version, sizeof(fcall->version), + &len); + break; + case TAUTH: + p = ixp_dec_u32(p, &fcall->afid); + p = ixp_dec_string(p, fcall->uname, sizeof(fcall->uname), &len); + p = ixp_dec_string(p, fcall->aname, sizeof(fcall->aname), &len); + break; + case RAUTH: + p = ixp_dec_qid(p, &fcall->aqid); + break; + case RATTACH: + p = ixp_dec_qid(p, &fcall->qid); + break; + case TATTACH: + p = ixp_dec_u32(p, &fcall->fid); + p = ixp_dec_u32(p, &fcall->afid); + p = ixp_dec_string(p, fcall->uname, sizeof(fcall->uname), &len); + p = ixp_dec_string(p, fcall->aname, sizeof(fcall->aname), &len); + break; + case RERROR: + p = ixp_dec_string(p, fcall->errstr, sizeof(fcall->errstr), &len); + break; + case TFLUSH: + p = ixp_dec_u16(p, &fcall->oldtag); + break; + case TWALK: + p = ixp_dec_u32(p, &fcall->fid); + p = ixp_dec_u32(p, &fcall->newfid); + p = ixp_dec_u16(p, &fcall->nwname); + for(i = 0; i < fcall->nwname; i++) + p = ixp_dec_string(p, fcall->wname[i], IXP_MAX_FLEN, &len); + break; + case RWALK: + p = ixp_dec_u16(p, &fcall->nwqid); + for(i = 0; i < fcall->nwqid; i++) + p = ixp_dec_qid(p, &fcall->wqid[i]); + break; + case TOPEN: + p = ixp_dec_u32(p, &fcall->fid); + p = ixp_dec_u8(p, &fcall->mode); + break; + case ROPEN: + case RCREATE: + p = ixp_dec_qid(p, &fcall->qid); + p = ixp_dec_u32(p, &fcall->iounit); + break; + case TCREATE: + p = ixp_dec_u32(p, &fcall->fid); + p = ixp_dec_string(p, fcall->name, sizeof(fcall->name), &len); + p = ixp_dec_u32(p, &fcall->perm); + p = ixp_dec_u8(p, &fcall->mode); + break; + case TREAD: + p = ixp_dec_u32(p, &fcall->fid); + p = ixp_dec_u64(p, &fcall->offset); + p = ixp_dec_u32(p, &fcall->count); + break; + case RREAD: + p = ixp_dec_u32(p, &fcall->count); + p = ixp_dec_data(p, fcall->data, fcall->count); + break; + case TWRITE: + p = ixp_dec_u32(p, &fcall->fid); + p = ixp_dec_u64(p, &fcall->offset); + p = ixp_dec_u32(p, &fcall->count); + p = ixp_dec_data(p, fcall->data, fcall->count); + break; + case RWRITE: + p = ixp_dec_u32(p, &fcall->count); + break; + case TCLUNK: + case TREMOVE: + case TSTAT: + p = ixp_dec_u32(p, &fcall->fid); + break; + case RSTAT: + p = ixp_dec_stat(p, &fcall->stat); + break; + case TWSTAT: + p = ixp_dec_u32(p, &fcall->fid); + p = ixp_dec_stat(p, &fcall->stat); + break; + } - return msize; + return msize; } diff --git a/libixp2/server.c b/libixp2/server.c @@ -18,173 +18,184 @@ #include "cext.h" static IXPConn zero_conn = { -1, 0, 0, 0 }; -static u8 msg[IXP_MAX_MSG]; +static unsigned char msg[IXP_MAX_MSG]; -static IXPConn *next_free_conn(IXPServer * s) +static IXPConn * +next_free_conn(IXPServer * s) { - int i; - for (i = 0; i < IXP_MAX_CONN; i++) - if (s->conn[i].fd < 0) - return &s->conn[i]; - return nil; + int i; + for(i = 0; i < IXP_MAX_CONN; i++) + if(s->conn[i].fd < 0) + return &s->conn[i]; + return nil; } -static void prepare_select(IXPServer * s) +static void +prepare_select(IXPServer * s) { - int i; - FD_ZERO(&s->rd); - for (i = 0; i < IXP_MAX_CONN; i++) { - if (s->conn[i].fd >= 0) { - if (s->maxfd < s->conn[i].fd) - s->maxfd = s->conn[i].fd; - if (s->conn[i].read) - FD_SET(s->conn[i].fd, &s->rd); - } - } + int i; + FD_ZERO(&s->rd); + for(i = 0; i < IXP_MAX_CONN; i++) { + if(s->conn[i].fd >= 0) { + if(s->maxfd < s->conn[i].fd) + s->maxfd = s->conn[i].fd; + if(s->conn[i].read) + FD_SET(s->conn[i].fd, &s->rd); + } + } } -void ixp_server_rm_conn(IXPServer * s, IXPConn * c) +void +ixp_server_rm_conn(IXPServer * s, IXPConn * c) { - if (!c->dont_close) { - shutdown(c->fd, SHUT_RDWR); - close(c->fd); - } - if (s->freeconn) - s->freeconn(s, c); - *c = zero_conn; + if(!c->dont_close) { + shutdown(c->fd, SHUT_RDWR); + close(c->fd); + } + if(s->freeconn) + s->freeconn(s, c); + *c = zero_conn; } -static IXPConn *init_conn(IXPConn * c, int fd, int dont_close, - void (*read) (IXPServer *, IXPConn *)) +static IXPConn * +init_conn(IXPConn * c, int fd, int dont_close, + void (*read) (IXPServer *, IXPConn *)) { - *c = zero_conn; - c->fd = fd; - c->dont_close = dont_close; - c->read = read; - return c; + *c = zero_conn; + c->fd = fd; + c->dont_close = dont_close; + c->read = read; + return c; } -IXPConn *ixp_server_add_conn(IXPServer * s, int fd, int dont_close, - void (*read) (IXPServer *, IXPConn *)) +IXPConn * +ixp_server_add_conn(IXPServer * s, int fd, int dont_close, + void (*read) (IXPServer *, IXPConn *)) { - IXPConn *c = next_free_conn(s); - if (!c) - return nil; - return init_conn(c, fd, dont_close, read); + IXPConn *c = next_free_conn(s); + if(!c) + return nil; + return init_conn(c, fd, dont_close, read); } -static void handle_conns(IXPServer * s) +static void +handle_conns(IXPServer * s) { - int i; - for (i = 0; i < IXP_MAX_CONN; i++) { - if (s->conn[i].fd >= 0) { - if (FD_ISSET(s->conn[i].fd, &s->rd) && s->conn[i].read) - /* call back read handler */ - s->conn[i].read(s, &s->conn[i]); - } - } + int i; + for(i = 0; i < IXP_MAX_CONN; i++) { + if(s->conn[i].fd >= 0) { + if(FD_ISSET(s->conn[i].fd, &s->rd) && s->conn[i].read) + /* call back read handler */ + s->conn[i].read(s, &s->conn[i]); + } + } } -static void server_client_read(IXPServer * s, IXPConn * c) +static void +server_client_read(IXPServer * s, IXPConn * c) { - u32 i, msize; - s->errstr = 0; - if (!(msize = ixp_recv_message(c->fd, msg, IXP_MAX_MSG, &s->errstr))) { - ixp_server_rm_conn(s, c); - return; - } - fprintf(stderr, "msize=%d\n", msize); - if ((msize = ixp_msg_to_fcall(msg, IXP_MAX_MSG, &s->fcall))) { - for (i = 0; s->funcs && s->funcs[i].id; i++) { - if (s->funcs[i].id == s->fcall.id) { - if (!s->funcs[i].tfunc(s, c)) - break; - msize = ixp_fcall_to_msg(&s->fcall, msg, s->fcall.maxmsg); - fprintf(stderr, "msize=%d\n", msize); - if (ixp_send_message(c->fd, msg, msize, &s->errstr) != - msize) - break; - return; - } - } - } - if (!s->errstr) - s->errstr = "function not supported"; - s->fcall.id = RERROR; - cext_strlcpy(s->fcall.errstr, s->errstr, sizeof(s->fcall.errstr)); - msize = ixp_fcall_to_msg(&s->fcall, msg, IXP_MAX_MSG); - if (ixp_send_message(c->fd, msg, msize, &s->errstr) != msize) - ixp_server_rm_conn(s, c); + unsigned int i, msize; + s->errstr = 0; + if(!(msize = ixp_recv_message(c->fd, msg, IXP_MAX_MSG, &s->errstr))) { + ixp_server_rm_conn(s, c); + return; + } + fprintf(stderr, "msize=%d\n", msize); + if((msize = ixp_msg_to_fcall(msg, IXP_MAX_MSG, &s->fcall))) { + for(i = 0; s->funcs && s->funcs[i].id; i++) { + if(s->funcs[i].id == s->fcall.id) { + if(!s->funcs[i].tfunc(s, c)) + break; + msize = ixp_fcall_to_msg(&s->fcall, msg, s->fcall.maxmsg); + fprintf(stderr, "msize=%d\n", msize); + if(ixp_send_message(c->fd, msg, msize, &s->errstr) != + msize) + break; + return; + } + } + } + if(!s->errstr) + s->errstr = "function not supported"; + s->fcall.id = RERROR; + cext_strlcpy(s->fcall.errstr, s->errstr, sizeof(s->fcall.errstr)); + msize = ixp_fcall_to_msg(&s->fcall, msg, IXP_MAX_MSG); + if(ixp_send_message(c->fd, msg, msize, &s->errstr) != msize) + ixp_server_rm_conn(s, c); } -static void server_read(IXPServer * s, IXPConn * c) +static void +server_read(IXPServer * s, IXPConn * c) { - int fd; - IXPConn *new = next_free_conn(s); - if (new && ((fd = ixp_accept_sock(c->fd)) >= 0)) - init_conn(new, fd, 0, server_client_read); + int fd; + IXPConn *new = next_free_conn(s); + if(new && ((fd = ixp_accept_sock(c->fd)) >= 0)) + init_conn(new, fd, 0, server_client_read); } -void ixp_server_loop(IXPServer * s) +void +ixp_server_loop(IXPServer * s) { - int r; - s->running = TRUE; - s->errstr = 0; - - /* main loop */ - while (s->running) { - - prepare_select(s); - - r = select(s->maxfd + 1, &s->rd, 0, 0, 0); - if (r == -1 && errno == EINTR) - continue; - if (r < 0) { - s->errstr = "fatal select error"; - break; /* allow cleanups in IXP using app */ - } else if (r > 0) - handle_conns(s); - } + int r; + s->running = TRUE; + s->errstr = 0; + + /* main loop */ + while(s->running) { + + prepare_select(s); + + r = select(s->maxfd + 1, &s->rd, 0, 0, 0); + if(r == -1 && errno == EINTR) + continue; + if(r < 0) { + s->errstr = "fatal select error"; + break; /* allow cleanups in IXP using app */ + } else if(r > 0) + handle_conns(s); + } } -int ixp_server_tversion(IXPServer * s, IXPConn * c) +int +ixp_server_tversion(IXPServer * s, IXPConn * c) { - fprintf(stderr, "got version %s (%s) %d (%d)\n", s->fcall.version, - IXP_VERSION, s->fcall.maxmsg, IXP_MAX_MSG); - if (strncmp(s->fcall.version, IXP_VERSION, strlen(IXP_VERSION))) { - s->errstr = "9P versions differ"; - return FALSE; - } else if (s->fcall.maxmsg > IXP_MAX_MSG) - s->fcall.maxmsg = IXP_MAX_MSG; - s->fcall.id = RVERSION; - return TRUE; + fprintf(stderr, "got version %s (%s) %d (%d)\n", s->fcall.version, + IXP_VERSION, s->fcall.maxmsg, IXP_MAX_MSG); + if(strncmp(s->fcall.version, IXP_VERSION, strlen(IXP_VERSION))) { + s->errstr = "9P versions differ"; + return FALSE; + } else if(s->fcall.maxmsg > IXP_MAX_MSG) + s->fcall.maxmsg = IXP_MAX_MSG; + s->fcall.id = RVERSION; + return TRUE; } int ixp_server_init(IXPServer * s, char *sockfile, IXPTFunc * funcs, - void (*freeconn) (IXPServer *, IXPConn *)) + void (*freeconn) (IXPServer *, IXPConn *)) { - int fd, i; - s->funcs = funcs; - s->freeconn = freeconn; - s->errstr = 0; - if (!sockfile) { - s->errstr = "no socket file provided or invalid directory"; - return FALSE; - } - if ((fd = ixp_create_sock(sockfile, &s->errstr)) < 0) - return FALSE; - for (i = 0; i < IXP_MAX_CONN; i++) - s->conn[i] = zero_conn; - ixp_server_add_conn(s, fd, 0, server_read); - return TRUE; + int fd, i; + s->funcs = funcs; + s->freeconn = freeconn; + s->errstr = 0; + if(!sockfile) { + s->errstr = "no socket file provided or invalid directory"; + return FALSE; + } + if((fd = ixp_create_sock(sockfile, &s->errstr)) < 0) + return FALSE; + for(i = 0; i < IXP_MAX_CONN; i++) + s->conn[i] = zero_conn; + ixp_server_add_conn(s, fd, 0, server_read); + return TRUE; } -void ixp_server_deinit(IXPServer * s) +void +ixp_server_deinit(IXPServer * s) { - int i; - /* shut down server */ - for (i = 0; i < IXP_MAX_CONN; i++) - if (s->conn[i].fd >= 0) - ixp_server_rm_conn(s, &s->conn[i]); + int i; + /* shut down server */ + for(i = 0; i < IXP_MAX_CONN; i++) + if(s->conn[i].fd >= 0) + ixp_server_rm_conn(s, &s->conn[i]); } diff --git a/libixp2/socket.c b/libixp2/socket.c @@ -16,68 +16,71 @@ #include "cext.h" #include "ixp.h" -int ixp_connect_sock(char *sockfile) +int +ixp_connect_sock(char *sockfile) { - int fd = 0; - struct sockaddr_un addr = { 0 }; - socklen_t su_len; + int fd = 0; + struct sockaddr_un addr = { 0 }; + socklen_t su_len; - /* init */ - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, sockfile, sizeof(addr.sun_path)); - su_len = sizeof(struct sockaddr) + strlen(addr.sun_path); + /* init */ + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, sockfile, sizeof(addr.sun_path)); + su_len = sizeof(struct sockaddr) + strlen(addr.sun_path); - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) - return -1; - if (connect(fd, (struct sockaddr *) &addr, su_len)) { - close(fd); - return -1; - } - return fd; + if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) + return -1; + if(connect(fd, (struct sockaddr *) &addr, su_len)) { + close(fd); + return -1; + } + return fd; } -int ixp_accept_sock(int fd) +int +ixp_accept_sock(int fd) { - socklen_t su_len; - struct sockaddr_un addr = { 0 }; + socklen_t su_len; + struct sockaddr_un addr = { 0 }; - su_len = sizeof(struct sockaddr); - return accept(fd, (struct sockaddr *) &addr, &su_len); + su_len = sizeof(struct sockaddr); + return accept(fd, (struct sockaddr *) &addr, &su_len); } -int ixp_create_sock(char *sockfile, char **errstr) +int +ixp_create_sock(char *sockfile, char **errstr) { - int fd; - int yes = 1; - struct sockaddr_un addr = { 0 }; - socklen_t su_len; + int fd; + int yes = 1; + struct sockaddr_un addr = { 0 }; + socklen_t su_len; - signal(SIGPIPE, SIG_IGN); - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - *errstr = "cannot open socket"; - return -1; - } - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, - (char *) &yes, sizeof(yes)) < 0) { - *errstr = "cannot set socket options"; - close(fd); - return -1; - } - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, sockfile, sizeof(addr.sun_path)); - su_len = sizeof(struct sockaddr) + strlen(addr.sun_path); + signal(SIGPIPE, SIG_IGN); + if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + *errstr = "cannot open socket"; + return -1; + } + if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, + (char *) &yes, sizeof(yes)) < 0) { + *errstr = "cannot set socket options"; + close(fd); + return -1; + } + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, sockfile, sizeof(addr.sun_path)); + su_len = sizeof(struct sockaddr) + strlen(addr.sun_path); - if (bind(fd, (struct sockaddr *) &addr, su_len) < 0) { - *errstr = "cannot bind socket"; - close(fd); - return -1; - } - chmod(sockfile, S_IRWXU); + if(bind(fd, (struct sockaddr *) &addr, su_len) < 0) { + *errstr = "cannot bind socket"; + close(fd); + return -1; + } + chmod(sockfile, S_IRWXU); - if (listen(fd, IXP_MAX_CONN) < 0) { - *errstr = "cannot listen on socket"; - close(fd); - return -1; - } - return fd; + if(listen(fd, IXP_MAX_CONN) < 0) { + *errstr = "cannot listen on socket"; + close(fd); + return -1; + } + return fd; } diff --git a/libixp2/transport.c b/libixp2/transport.c @@ -17,59 +17,65 @@ #include <cext.h> -u32 ixp_send_message(int fd, void *msg, u32 msize, char **errstr) +unsigned int +ixp_send_message(int fd, void *msg, unsigned int msize, char **errstr) { - u32 num = 0; - int r; + unsigned int num = 0; + int r; - /* send message */ - while (num < msize) { - r = write(fd, msg + num, msize - num); - if (r == -1 && errno == EINTR) - continue; - if (r < 1) { - *errstr = "cannot send message"; - return 0; - } - num += r; - } - return num; + /* send message */ + while(num < msize) { + r = write(fd, msg + num, msize - num); + if(r == -1 && errno == EINTR) + continue; + if(r < 1) { + *errstr = "cannot send message"; + return 0; + } + num += r; + } + return num; } -static u32 ixp_recv_data(int fd, void *msg, u32 msize, char **errstr) +static unsigned int +ixp_recv_data(int fd, void *msg, unsigned int msize, char **errstr) { - u32 num = 0; - int r = 0; + unsigned int num = 0; + int r = 0; - /* receive data */ - while (num < msize) { - r = read(fd, msg + num, msize - num); - if (r == -1 && errno == EINTR) - continue; - if (r < 1) { - *errstr = "cannot receive data"; - return 0; - } - num += r; - } - return num; + /* receive data */ + while(num < msize) { + r = read(fd, msg + num, msize - num); + if(r == -1 && errno == EINTR) + continue; + if(r < 1) { + *errstr = "cannot receive data"; + return 0; + } + num += r; + } + return num; } -u32 ixp_recv_message(int fd, void *msg, u32 msglen, char **errstr) +unsigned int +ixp_recv_message(int fd, void *msg, unsigned int msglen, char **errstr) { - u32 msize; + unsigned int msize; - /* receive header */ - if (ixp_recv_data(fd, msg, sizeof(u32), errstr) != sizeof(u32)) - return 0; - ixp_dec_u32(msg, &msize); - if (msize > msglen) { - *errstr = "message size exceeds buffer size"; - return 0; - } - /* receive message */ - if (ixp_recv_data(fd, msg + sizeof(u32), msize - sizeof(u32), errstr) - != msize - sizeof(u32)) - return 0; - return msize; + /* receive header */ + if(ixp_recv_data(fd, msg, sizeof(unsigned int), errstr) != + sizeof(unsigned int)) + return 0; + ixp_dec_u32(msg, &msize); + if(msize > msglen) { + *errstr = "message size exceeds buffer size"; + return 0; + } + /* receive message */ + if(ixp_recv_data + (fd, msg + sizeof(unsigned int), msize - sizeof(unsigned int), + errstr) + != msize - sizeof(unsigned int)) + return 0; + return msize; } diff --git a/libwmii/ixputil.c b/libwmii/ixputil.c @@ -15,79 +15,84 @@ static pid_t mypid; static char *mysockfile; -File *wmii_create_ixpfile(IXPServer * s, char *key, char *val) +File * +wmii_create_ixpfile(IXPServer * s, char *key, char *val) { - File *f = ixp_create(s, key); - if (f && !is_directory(f)) { - size_t l = val ? strlen(val) : 0; - f->content = l ? strdup(val) : 0; - f->size = l; - return f; - } - /* forbidden, file is directory */ - return 0; + File *f = ixp_create(s, key); + if(f && !is_directory(f)) { + size_t l = val ? strlen(val) : 0; + f->content = l ? strdup(val) : 0; + f->size = l; + return f; + } + /* forbidden, file is directory */ + return 0; } -void wmii_get_ixppath(File * f, char *path, size_t size) +void +wmii_get_ixppath(File * f, char *path, size_t size) { - char buf[512]; + char buf[512]; - buf[0] = 0; - if (path) - cext_strlcpy(buf, path, sizeof(buf)); - snprintf(path, size, "%s/", f->name); - if (buf[0] != 0) - cext_strlcat(path, buf, size); - if (f->parent) - wmii_get_ixppath(f->parent, path, size); + buf[0] = 0; + if(path) + cext_strlcpy(buf, path, sizeof(buf)); + snprintf(path, size, "%s/", f->name); + if(buf[0] != 0) + cext_strlcat(path, buf, size); + if(f->parent) + wmii_get_ixppath(f->parent, path, size); } -void wmii_move_ixpfile(File * f, File * to_parent) +void +wmii_move_ixpfile(File * f, File * to_parent) { - File *p = f->parent; - File *fil = p->content; + File *p = f->parent; + File *fil = p->content; - /* detach */ - if (p->content == f) - p->content = fil->next; - else { - while (fil->next != f) - fil = fil->next; - fil->next = f->next; - } - f->next = 0; + /* detach */ + if(p->content == f) + p->content = fil->next; + else { + while(fil->next != f) + fil = fil->next; + fil->next = f->next; + } + f->next = 0; - /* attach */ - if (!to_parent->content) - to_parent->content = f; - else { - for (fil = to_parent->content; fil->next; fil = fil->next); - fil->next = f; - } - f->parent = to_parent; + /* attach */ + if(!to_parent->content) + to_parent->content = f; + else { + for(fil = to_parent->content; fil->next; fil = fil->next); + fil->next = f; + } + f->parent = to_parent; } -static void exit_cleanup() +static void +exit_cleanup() { - if (mypid == getpid()) - unlink(mysockfile); + if(mypid == getpid()) + unlink(mysockfile); } -IXPServer *wmii_setup_server(char *sockfile) +IXPServer * +wmii_setup_server(char *sockfile) { - IXPServer *s; + IXPServer *s; - if (!sockfile) { - fprintf(stderr, "%s\n", "libwmii: no socket file provided"); - exit(1); - } - mysockfile = sockfile; - mypid = getpid(); - s = init_server(sockfile, exit_cleanup); - if (!s) { - perror("libwmii: cannot initialize IXP server"); - exit(1); - } - return s; + if(!sockfile) { + fprintf(stderr, "%s\n", "libwmii: no socket file provided"); + exit(1); + } + mysockfile = sockfile; + mypid = getpid(); + s = init_server(sockfile, exit_cleanup); + if(!s) { + perror("libwmii: cannot initialize IXP server"); + exit(1); + } + return s; } diff --git a/libwmii/spawn.c b/libwmii/spawn.c @@ -11,18 +11,19 @@ #include "wmii.h" -void wmii_spawn(void *dpy, char *cmd) +void +wmii_spawn(void *dpy, char *cmd) { - /* the questionable double-fork is done to catch all zombies */ - if (fork() == 0) { - if (fork() == 0) { - setsid(); - close(ConnectionNumber(dpy)); - execlp("rc", "rc", "-c", cmd, (char *) 0); - perror("failed"); - exit(1); - } - exit(0); - } - wait(0); + /* the questionable double-fork is done to catch all zombies */ + if(fork() == 0) { + if(fork() == 0) { + setsid(); + close(ConnectionNumber(dpy)); + execlp("rc", "rc", "-c", cmd, (char *) 0); + perror("failed"); + exit(1); + } + exit(0); + } + wait(0); } diff --git a/libwmii/wm.c b/libwmii/wm.c @@ -13,62 +13,69 @@ #include "blitz.h" -int wmii_property(Display * dpy, Window w, Atom a, Atom t, long l, unsigned char **prop) +int +wmii_property(Display * dpy, Window w, Atom a, Atom t, long l, + unsigned char **prop) { - Atom real; - int format; - unsigned long res, extra; - int status; + Atom real; + int format; + unsigned long res, extra; + int status; - status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format, &res, &extra, prop); + status = + XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format, + &res, &extra, prop); - if (status != Success || *prop == 0) { - return 0; - } - if (res == 0) { - free((void *) *prop); - } - return res; + if(status != Success || *prop == 0) { + return 0; + } + if(res == 0) { + free((void *) *prop); + } + return res; } -void wmii_send_message(Display *dpy, Window w, Atom a, long value) +void +wmii_send_message(Display * dpy, Window w, Atom a, long value) { - XEvent e; - e.type = ClientMessage; - e.xclient.window = w; - e.xclient.message_type = a; - e.xclient.format = 32; - e.xclient.data.l[0] = value; - e.xclient.data.l[1] = CurrentTime; + XEvent e; + e.type = ClientMessage; + e.xclient.window = w; + e.xclient.message_type = a; + e.xclient.format = 32; + e.xclient.data.l[0] = value; + e.xclient.data.l[1] = CurrentTime; - XSendEvent(dpy, w, False, NoEventMask, &e); - XSync(dpy, False); + XSendEvent(dpy, w, False, NoEventMask, &e); + XSync(dpy, False); } #define NUM_MASKS 8 -void wmii_init_lock_modifiers(Display * dpy, unsigned int *valid_mask, unsigned int *num_lock_mask) +void +wmii_init_lock_modifiers(Display * dpy, unsigned int *valid_mask, + unsigned int *num_lock_mask) { - XModifierKeymap *modmap; - KeyCode num_lock; - static int masks[NUM_MASKS] = { - ShiftMask, LockMask, ControlMask, Mod1Mask, - Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask - }; - int i; + XModifierKeymap *modmap; + KeyCode num_lock; + static int masks[NUM_MASKS] = { + ShiftMask, LockMask, ControlMask, Mod1Mask, + Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask + }; + int i; - *num_lock_mask = 0; - modmap = XGetModifierMapping(dpy); - num_lock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock")); + *num_lock_mask = 0; + modmap = XGetModifierMapping(dpy); + num_lock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock")); - if (modmap && modmap->max_keypermod > 0) { - int max = NUM_MASKS * modmap->max_keypermod; - for (i = 0; i < max; i++) { - if (num_lock && (modmap->modifiermap[i] == num_lock)) { - *num_lock_mask = masks[i / modmap->max_keypermod]; - } - } - } - XFreeModifiermap(modmap); + if(modmap && modmap->max_keypermod > 0) { + int max = NUM_MASKS * modmap->max_keypermod; + for(i = 0; i < max; i++) { + if(num_lock && (modmap->modifiermap[i] == num_lock)) { + *num_lock_mask = masks[i / modmap->max_keypermod]; + } + } + } + XFreeModifiermap(modmap); - *valid_mask = 255 & ~(*num_lock_mask | LockMask); + *valid_mask = 255 & ~(*num_lock_mask | LockMask); } diff --git a/libwmii/wmii.h b/libwmii/wmii.h @@ -12,8 +12,8 @@ typedef struct Action Action; struct Action { - char *name; - void (*func) (void *obj, char *); + char *name; + void (*func) (void *obj, char *); }; /* ixputil.c */ @@ -26,6 +26,8 @@ IXPServer *wmii_setup_server(char *sockfile); void wmii_spawn(void *dpy, char *cmd); /* wm.c */ -int wmii_property(Display *dpy, Window w, Atom a, Atom t, long l, unsigned char **prop); -void wmii_send_message(Display *dpy, Window w, Atom a, long value); -void wmii_init_lock_modifiers(Display *dpy, unsigned int *valid_mask, unsigned int *num_lock_mask); +int wmii_property(Display * dpy, Window w, Atom a, Atom t, long l, + unsigned char **prop); +void wmii_send_message(Display * dpy, Window w, Atom a, long value); +void wmii_init_lock_modifiers(Display * dpy, unsigned int *valid_mask, + unsigned int *num_lock_mask);