--- dmenu.c | 416 +++++++++++++++++++++++++++++++++---------------------------= ---- draw.c | 88 ++++++++------ draw.h | 40 +++---- stest.c | 32 ++--- 4 files changed, 303 insertions(+), 273 deletions(-) diff --git a/dmenu.c b/dmenu.c index 077c4ca..936a7f6 100644 --- a/dmenu.c +++ b/dmenu.c _AT_@ -6,12 +6,13 @@ #include <string.h> #include <strings.h> #include <unistd.h> -#include <X11/Xlib.h> #include <X11/Xatom.h> +#include <X11/Xlib.h> #include <X11/Xutil.h> #ifdef XINERAMA #include <X11/extensions/Xinerama.h> #endif + #include "draw.h" =20 #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - = MAX((x),(r).x_org)) \ _AT_@ -23,24 +24,24 @@ typedef struct Item Item; struct Item { char *text; Item *left, *right; - int out; + int out; }; =20 -static long estrtol(const char *s, int base); -static void appenditem(Item *item, Item **list, Item **last); -static void calcoffsets(void); -static char *cistrstr(const char *s, const char *sub); -static void drawmenu(void); -static void grabkeyboard(void); -static void insert(const char *str, ssize_t n); -static void keypress(XKeyEvent *ev); -static void match(void); -static size_t nextrune(int inc); -static void paste(void); -static void readstdin(void); -static void run(void); -static void setup(void); -static void usage(void); +static void appenditem(Item *, Item **, Item **); +static void calcoffsets(void); +static char *cistrstr(const char *, const char *); +static void drawmenu(void); +static void grabkeyboard(void); +static void insert(const char *, ssize_t); +static void keypress(XKeyEvent *); +static void match(void); +static size_t nextrune(int); +static void paste(void); +static void readstdin(void); +static void run(void); +static void setup(void); +static void usage(void); +static long estrtol(const char *, int); =20 static char text[BUFSIZ] =3D ""; static int bh, mw, mh; _AT_@ -62,89 +63,10 @@ static XIC xic; static int (*fstrncmp)(const char *, const char *, size_t) =3D strncmp; static char *(*fstrstr)(const char *, const char *) =3D strstr; =20 -static long -estrtol(const char *s, int base) -{ - char *end; - long n; - errno =3D 0; - n =3D strtol(s, &end, base); - if (*end !=3D '\0') { - if (base =3D=3D 0) { - printf("%s: not an integer\n", s); - exit(1); - } else { - printf("%s: not a base %d integer\n", s, base); - exit(1); - } - } - if (errno !=3D 0) { - printf("%s:", s); - exit(1); - } - return n; -} - -int -main(int argc, char *argv[]) { - int fast =3D 0, i; - - for(i =3D 1; i < argc; i++) - /* these options take no arguments */ - if(!strcmp(argv[i], "-v")) { /* prints version information */ - puts("dmenu-"VERSION", =C2=A9 2006-2014 dmenu engineers, see LICENSE fo= r details"); - exit(EXIT_SUCCESS); - } - else if(!strcmp(argv[i], "-b")) /* appears at the bottom of the screen= */ - topbar =3D 0; - else if(!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin= */ - fast =3D 1; - else if(!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ - fstrncmp =3D strncasecmp; - fstrstr =3D cistrstr; - } - else if(i+1 =3D=3D argc) - usage(); - /* these options take one argument */ - else if(!strcmp(argv[i], "-l")) /* number of lines in vertical list */ - lines =3D estrtol(argv[++i], 0); - else if(!strcmp(argv[i], "-m")) - mon =3D estrtol(argv[++i], 0); - else if(!strcmp(argv[i], "-p")) /* adds prompt to left of input field = */ - prompt =3D argv[++i]; - else if(!strcmp(argv[i], "-fn")) /* font or font set */ - font =3D argv[++i]; - else if(!strcmp(argv[i], "-nb")) /* normal background color */ - normbgcolor =3D argv[++i]; - else if(!strcmp(argv[i], "-nf")) /* normal foreground color */ - normfgcolor =3D argv[++i]; - else if(!strcmp(argv[i], "-sb")) /* selected background color */ - selbgcolor =3D argv[++i]; - else if(!strcmp(argv[i], "-sf")) /* selected foreground color */ - selfgcolor =3D argv[++i]; - else - usage(); - - dc =3D initdc(); - initfont(dc, font); - - if(fast) { - grabkeyboard(); - readstdin(); - } - else { - readstdin(); - grabkeyboard(); - } - setup(); - run(); - - return 1; /* unreachable */ -} - void -appenditem(Item *item, Item **list, Item **last) { - if(*last) +appenditem(Item *item, Item **list, Item **last) +{ + if (*last) (*last)->right =3D item; else *list =3D item; _AT_@ -155,34 +77,38 @@ appenditem(Item *item, Item **list, Item **last) { } =20 void -calcoffsets(void) { +calcoffsets(void) +{ int i, n; =20 - if(lines > 0) + if (lines > 0) n =3D lines * bh; else n =3D mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">")); + /* calculate which items will begin the next page and previous page */ - for(i =3D 0, next =3D curr; next; next =3D next->right) - if((i +=3D (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n) + for (i =3D 0, next =3D curr; next; next =3D next->right) + if ((i +=3D (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n) break; - for(i =3D 0, prev =3D curr; prev && prev->left; prev =3D prev->left) - if((i +=3D (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n) + for (i =3D 0, prev =3D curr; prev && prev->left; prev =3D prev->left) + if ((i +=3D (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n) break; } =20 char * -cistrstr(const char *s, const char *sub) { +cistrstr(const char *s, const char *sub) +{ size_t len; =20 - for(len =3D strlen(sub); *s; s++) - if(!strncasecmp(s, sub, len)) + for (len =3D strlen(sub); *s; s++) + if (!strncasecmp(s, sub, len)) return (char *)s; return NULL; } =20 void -drawmenu(void) { +drawmenu(void) +{ int curpos; Item *item; =20 _AT_@ -191,7 +117,7 @@ drawmenu(void) { dc->h =3D bh; drawrect(dc, 0, 0, mw, mh, 1, BG(dc, normcol)); =20 - if(prompt && *prompt) { + if (prompt && *prompt) { dc->w =3D promptw; drawtext(dc, prompt, selcol); dc->x =3D dc->w; _AT_@ -199,25 +125,25 @@ drawmenu(void) { /* draw input field */ dc->w =3D (lines > 0 || !matches) ? mw - dc->x : inputw; drawtext(dc, text, normcol); - if((curpos =3D textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w) + if ((curpos =3D textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w) drawrect(dc, curpos, 2, 1, dc->h - 4, 1, FG(dc, normcol)); =20 - if(lines > 0) { + if (lines > 0) { /* draw vertical list */ dc->w =3D mw - dc->x; - for(item =3D curr; item !=3D next; item =3D item->right) { + for (item =3D curr; item !=3D next; item =3D item->right) { dc->y +=3D dc->h; drawtext(dc, item->text, (item =3D=3D sel) ? selcol : (item->out) ? outcol : normcol); } } - else if(matches) { + else if (matches) { /* draw horizontal list */ dc->x +=3D inputw; dc->w =3D textw(dc, "<"); - if(curr->left) + if (curr->left) drawtext(dc, "<", normcol); - for(item =3D curr; item !=3D next; item =3D item->right) { + for (item =3D curr; item !=3D next; item =3D item->right) { dc->x +=3D dc->w; dc->w =3D MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">")); drawtext(dc, item->text, (item =3D=3D sel) ? selcol : _AT_@ -225,19 +151,20 @@ drawmenu(void) { } dc->w =3D textw(dc, ">"); dc->x =3D mw - dc->w; - if(next) + if (next) drawtext(dc, ">", normcol); } mapdc(dc, win, mw, mh); } =20 void -grabkeyboard(void) { +grabkeyboard(void) +{ int i; =20 /* try to grab keyboard, we may have to wait for another process to ungra= b */ - for(i =3D 0; i < 1000; i++) { - if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), 1, + for (i =3D 0; i < 1000; i++) { + if (XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), 1, GrabModeAsync, GrabModeAsync, CurrentTime) =3D=3D GrabS= uccess) return; usleep(1000); _AT_@ -246,29 +173,31 @@ grabkeyboard(void) { } =20 void -insert(const char *str, ssize_t n) { - if(strlen(text) + n > sizeof text - 1) +insert(const char *str, ssize_t n) +{ + if (strlen(text) + n > sizeof text - 1) return; /* move existing text out of the way, insert new text, and update cursor = */ memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0= )); - if(n > 0) + if (n > 0) memcpy(&text[cursor], str, n); cursor +=3D n; match(); } =20 void -keypress(XKeyEvent *ev) { +keypress(XKeyEvent *ev) +{ char buf[32]; int len; KeySym ksym =3D NoSymbol; Status status; =20 len =3D XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status); - if(status =3D=3D XBufferOverflow) + if (status =3D=3D XBufferOverflow) return; - if(ev->state & ControlMask) - switch(ksym) { + if (ev->state & ControlMask) + switch (ksym) { case XK_a: ksym =3D XK_Home; break; case XK_b: ksym =3D XK_Left; break; case XK_c: ksym =3D XK_Escape; break; _AT_@ -293,9 +222,9 @@ keypress(XKeyEvent *ev) { insert(NULL, 0 - cursor); break; case XK_w: /* delete word */ - while(cursor > 0 && text[nextrune(-1)] =3D=3D ' ') + while (cursor > 0 && text[nextrune(-1)] =3D=3D ' ') insert(NULL, nextrune(-1) - cursor); - while(cursor > 0 && text[nextrune(-1)] !=3D ' ') + while (cursor > 0 && text[nextrune(-1)] !=3D ' ') insert(NULL, nextrune(-1) - cursor); break; case XK_y: /* paste selection */ _AT_@ -310,8 +239,8 @@ keypress(XKeyEvent *ev) { default: return; } - else if(ev->state & Mod1Mask) - switch(ksym) { + else if (ev->state & Mod1Mask) + switch (ksym) { case XK_g: ksym =3D XK_Home; break; case XK_G: ksym =3D XK_End; break; case XK_h: ksym =3D XK_Up; break; _AT_@ -321,33 +250,33 @@ keypress(XKeyEvent *ev) { default: return; } - switch(ksym) { + switch (ksym) { default: - if(!iscntrl(*buf)) + if (!iscntrl(*buf)) insert(buf, len); break; case XK_Delete: - if(text[cursor] =3D=3D '\0') + if (text[cursor] =3D=3D '\0') return; cursor =3D nextrune(+1); /* fallthrough */ case XK_BackSpace: - if(cursor =3D=3D 0) + if (cursor =3D=3D 0) return; insert(NULL, nextrune(-1) - cursor); break; case XK_End: - if(text[cursor] !=3D '\0') { + if (text[cursor] !=3D '\0') { cursor =3D strlen(text); break; } - if(next) { + if (next) { /* jump to end of list and position items in reverse */ curr =3D matchend; calcoffsets(); curr =3D prev; calcoffsets(); - while(next && (curr =3D curr->right)) + while (next && (curr =3D curr->right)) calcoffsets(); } sel =3D matchend; _AT_@ -355,7 +284,7 @@ keypress(XKeyEvent *ev) { case XK_Escape: exit(EXIT_FAILURE); case XK_Home: - if(sel =3D=3D matches) { + if (sel =3D=3D matches) { cursor =3D 0; break; } _AT_@ -363,27 +292,27 @@ keypress(XKeyEvent *ev) { calcoffsets(); break; case XK_Left: - if(cursor > 0 && (!sel || !sel->left || lines > 0)) { + if (cursor > 0 && (!sel || !sel->left || lines > 0)) { cursor =3D nextrune(-1); break; } - if(lines > 0) + if (lines > 0) return; /* fallthrough */ case XK_Up: - if(sel && sel->left && (sel =3D sel->left)->right =3D=3D curr) { + if (sel && sel->left && (sel =3D sel->left)->right =3D=3D curr) { curr =3D prev; calcoffsets(); } break; case XK_Next: - if(!next) + if (!next) return; sel =3D curr =3D next; calcoffsets(); break; case XK_Prior: - if(!prev) + if (!prev) return; sel =3D curr =3D prev; calcoffsets(); _AT_@ -391,27 +320,27 @@ keypress(XKeyEvent *ev) { case XK_Return: case XK_KP_Enter: puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); - if(!(ev->state & ControlMask)) + if (!(ev->state & ControlMask)) exit(EXIT_SUCCESS); - if(sel) + if (sel) sel->out =3D 1; break; case XK_Right: - if(text[cursor] !=3D '\0') { + if (text[cursor] !=3D '\0') { cursor =3D nextrune(+1); break; } - if(lines > 0) + if (lines > 0) return; /* fallthrough */ case XK_Down: - if(sel && sel->right && (sel =3D sel->right) =3D=3D next) { + if (sel && sel->right && (sel =3D sel->right) =3D=3D next) { curr =3D next; calcoffsets(); } break; case XK_Tab: - if(!sel) + if (!sel) return; strncpy(text, sel->text, sizeof text - 1); text[sizeof text - 1] =3D '\0'; _AT_@ -423,7 +352,8 @@ keypress(XKeyEvent *ev) { } =20 void -match(void) { +match(void) +{ static char **tokv =3D NULL; static int tokn =3D 0; =20 _AT_@ -434,28 +364,28 @@ match(void) { =20 strcpy(buf, text); /* separate input text into tokens to be matched individually */ - for(s =3D strtok(buf, " "); s; tokv[tokc-1] =3D s, s =3D strtok(NULL, " "= )) - if(++tokc > tokn && !(tokv =3D realloc(tokv, ++tokn * sizeof *tokv))) + for (s =3D strtok(buf, " "); s; tokv[tokc-1] =3D s, s =3D strtok(NULL, " = ")) + if (++tokc > tokn && !(tokv =3D realloc(tokv, ++tokn * sizeof *tokv))) eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv); len =3D tokc ? strlen(tokv[0]) : 0; =20 matches =3D lprefix =3D lsubstr =3D matchend =3D prefixend =3D substrend = =3D NULL; - for(item =3D items; item && item->text; item++) { - for(i =3D 0; i < tokc; i++) - if(!fstrstr(item->text, tokv[i])) + for (item =3D items; item && item->text; item++) { + for (i =3D 0; i < tokc; i++) + if (!fstrstr(item->text, tokv[i])) break; - if(i !=3D tokc) /* not all tokens match */ + if (i !=3D tokc) /* not all tokens match */ continue; /* exact matches go first, then prefixes, then substrings */ - if(!tokc || !fstrncmp(tokv[0], item->text, len+1)) + if (!tokc || !fstrncmp(tokv[0], item->text, len+1)) appenditem(item, &matches, &matchend); - else if(!fstrncmp(tokv[0], item->text, len)) + else if (!fstrncmp(tokv[0], item->text, len)) appenditem(item, &lprefix, &prefixend); else appenditem(item, &lsubstr, &substrend); } - if(lprefix) { - if(matches) { + if (lprefix) { + if (matches) { matchend->right =3D lprefix; lprefix->left =3D matchend; } _AT_@ -463,8 +393,8 @@ match(void) { matches =3D lprefix; matchend =3D prefixend; } - if(lsubstr) { - if(matches) { + if (lsubstr) { + if (matches) { matchend->right =3D lsubstr; lsubstr->left =3D matchend; } _AT_@ -477,16 +407,18 @@ match(void) { } =20 size_t -nextrune(int inc) { +nextrune(int inc) +{ ssize_t n; =20 /* return location of next utf8 rune in the given direction (+1 or -1) */ - for(n =3D cursor + inc; n + inc >=3D 0 && (text[n] & 0xc0) =3D=3D 0x80; n= +=3D inc); + for (n =3D cursor + inc; n + inc >=3D 0 && (text[n] & 0xc0) =3D=3D 0x80; = n +=3D inc); return n; } =20 void -paste(void) { +paste(void) +{ char *p, *q; int di; unsigned long dl; _AT_@ -501,50 +433,52 @@ paste(void) { } =20 void -readstdin(void) { +readstdin(void) +{ char buf[sizeof text], *p, *maxstr =3D NULL; size_t i, max =3D 0, size =3D 0; =20 /* read each line from stdin and add it to the item list */ - for(i =3D 0; fgets(buf, sizeof buf, stdin); i++) { - if(i+1 >=3D size / sizeof *items) - if(!(items =3D realloc(items, (size +=3D BUFSIZ)))) + for (i =3D 0; fgets(buf, sizeof buf, stdin); i++) { + if (i + 1 >=3D size / sizeof *items) + if (!(items =3D realloc(items, (size +=3D BUFSIZ)))) eprintf("cannot realloc %u bytes:", size); - if((p =3D strchr(buf, '\n'))) + if ((p =3D strchr(buf, '\n'))) *p =3D '\0'; - if(!(items[i].text =3D strdup(buf))) + if (!(items[i].text =3D strdup(buf))) eprintf("cannot strdup %u bytes:", strlen(buf)+1); items[i].out =3D 0; - if(strlen(items[i].text) > max) + if (strlen(items[i].text) > max) max =3D strlen(maxstr =3D items[i].text); } - if(items) + if (items) items[i].text =3D NULL; inputw =3D maxstr ? textw(dc, maxstr) : 0; lines =3D MIN(lines, i); } =20 void -run(void) { +run(void) +{ XEvent ev; =20 - while(!XNextEvent(dc->dpy, &ev)) { - if(XFilterEvent(&ev, win)) + while (!XNextEvent(dc->dpy, &ev)) { + if (XFilterEvent(&ev, win)) continue; - switch(ev.type) { + switch (ev.type) { case Expose: - if(ev.xexpose.count =3D=3D 0) + if (ev.xexpose.count =3D=3D 0) mapdc(dc, win, mw, mh); break; case KeyPress: keypress(&ev.xkey); break; case SelectionNotify: - if(ev.xselection.property =3D=3D utf8) + if (ev.xselection.property =3D=3D utf8) paste(); break; case VisibilityNotify: - if(ev.xvisibility.state !=3D VisibilityUnobscured) + if (ev.xvisibility.state !=3D VisibilityUnobscured) XRaiseWindow(dc->dpy, win); break; } _AT_@ -552,9 +486,10 @@ run(void) { } =20 void -setup(void) { - int x, y, screen =3D DefaultScreen(dc->dpy); - Window root =3D RootWindow(dc->dpy, screen); +setup(void) +{ + int x, y, screen; + Window root; XSetWindowAttributes swa; XIM xim; #ifdef XINERAMA _AT_@ -562,6 +497,9 @@ setup(void) { XineramaScreenInfo *info; #endif =20 + screen =3D DefaultScreen(dc->dpy); + root =3D RootWindow(dc->dpy, screen); + normcol[ColBG] =3D getcolor(dc, normbgcolor); normcol[ColFG] =3D getcolor(dc, normfgcolor); selcol[ColBG] =3D getcolor(dc, selbgcolor); _AT_@ -577,41 +515,40 @@ setup(void) { lines =3D MAX(lines, 0); mh =3D (lines + 1) * bh; #ifdef XINERAMA - if((info =3D XineramaQueryScreens(dc->dpy, &n))) { + if ((info =3D XineramaQueryScreens(dc->dpy, &n))) { int a, j, di, i =3D 0, area =3D 0; unsigned int du; Window w, pw, dw, *dws; XWindowAttributes wa; =20 XGetInputFocus(dc->dpy, &w, &di); - if(mon !=3D -1 && mon < n) + if (mon !=3D -1 && mon < n) i =3D mon; - if(!i && w !=3D root && w !=3D PointerRoot && w !=3D None) { + if (!i && w !=3D root && w !=3D PointerRoot && w !=3D None) { /* find top-level window containing current input focus */ do { - if(XQueryTree(dc->dpy, (pw =3D w), &dw, &w, &dws, &du) && dws) + if (XQueryTree(dc->dpy, (pw =3D w), &dw, &w, &dws, &du) && dws) XFree(dws); - } while(w !=3D root && w !=3D pw); + } while (w !=3D root && w !=3D pw); /* find xinerama screen with which the window intersects most */ - if(XGetWindowAttributes(dc->dpy, pw, &wa)) - for(j =3D 0; j < n; j++) - if((a =3D INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area= ) { + if (XGetWindowAttributes(dc->dpy, pw, &wa)) + for (j =3D 0; j < n; j++) + if ((a =3D INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > are= a) { area =3D a; i =3D j; } } /* no focused window is on screen, so use pointer location instead */ - if(mon =3D=3D -1 && !area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, = &y, &di, &di, &du)) - for(i =3D 0; i < n; i++) - if(INTERSECT(x, y, 1, 1, info[i])) + if (mon =3D=3D -1 && !area && XQueryPointer(dc->dpy, root, &dw, &dw, &x,= &y, &di, &di, &du)) + for (i =3D 0; i < n; i++) + if (INTERSECT(x, y, 1, 1, info[i])) break; =20 x =3D info[i].x_org; y =3D info[i].y_org + (topbar ? 0 : info[i].height - mh); mw =3D info[i].width; XFree(info); - } - else + } else #endif { x =3D 0; _AT_@ -641,9 +578,86 @@ setup(void) { drawmenu(); } =20 +static long +estrtol(const char *s, int base) +{ + char *end; + long n; + + errno =3D 0; + n =3D strtol(s, &end, base); + if (*end !=3D '\0') { + if (base =3D=3D 0) + eprintf("%s: not an integer\n", s); + else + eprintf("%s: not a base %d integer\n", s, base); + } + if (errno !=3D 0) { + eprintf("%s:", s); + } + return n; +} + void -usage(void) { - fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font] [-m = monitor]\n" - " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\= n", stderr); - exit(EXIT_FAILURE); +usage(void) +{ + eprintf("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font] [-= m monitor]\n" + " [-nb color] [-nf color] [-sb color] [-sf color] [-v= ]\n"); +} + +int +main(int argc, char *argv[]) +{ + int fast =3D 0, i; + + for (i =3D 1; i < argc; i++) + /* Flags without argument */ + if (!strcmp(argv[i], "-v")) { + puts("dmenu-"VERSION); + exit(0); + } else if (!strcmp(argv[i], "-b")) { + topbar =3D 0; + } else if (!strcmp(argv[i], "-f")) { + fast =3D 1; + } else if (!strcmp(argv[i], "-i")) { + fstrncmp =3D strncasecmp; + fstrstr =3D cistrstr; + } else if (i + 1 =3D=3D argc) { + usage(); + + /* Flags with argument */ + } else if (!strcmp(argv[i], "-l")) { + lines =3D estrtol(argv[++i], 0); + } else if (!strcmp(argv[i], "-m")) { + mon =3D estrtol(argv[++i], 0); + } else if (!strcmp(argv[i], "-p")) { + prompt =3D argv[++i]; + } else if (!strcmp(argv[i], "-fn")) { + font =3D argv[++i]; + } else if (!strcmp(argv[i], "-nb")) { + normbgcolor =3D argv[++i]; + } else if (!strcmp(argv[i], "-nf")) { + normfgcolor =3D argv[++i]; + } else if (!strcmp(argv[i], "-sb")) { + selbgcolor =3D argv[++i]; + } else if (!strcmp(argv[i], "-sf")) { + selfgcolor =3D argv[++i]; + } else { + usage(); + } + + dc =3D initdc(); + initfont(dc, font); + + if (fast) { + grabkeyboard(); + readstdin(); + } else { + readstdin(); + grabkeyboard(); + } + setup(); + run(); + + return 1; /* unreachable */ } diff --git a/draw.c b/draw.c index ad770f6..b593dee 100644 --- a/draw.c +++ b/draw.c _AT_@ -5,6 +5,7 @@ #include <stdlib.h> #include <string.h> #include <X11/Xlib.h> + #include "draw.h" =20 #define MAX(a, b) ((a) > (b) ? (a) : (b)) _AT_@ -14,38 +15,41 @@ static int loadfont(DC *dc, const char *fontstr); =20 void -drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fill, u= nsigned long color) { +drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fill, u= nsigned long color) +{ XSetForeground(dc->dpy, dc->gc, color); - if(fill) + if (fill) XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h); else XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h= -1); } =20 void -drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { +drawtext(DC *dc, const char *text, unsigned long col[ColLast]) +{ char buf[BUFSIZ]; size_t mn, n =3D strlen(text); =20 /* shorten text if necessary */ - for(mn =3D MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 >= dc->w; mn--) - if(mn =3D=3D 0) + for (mn =3D MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 = > dc->w; mn--) + if (mn =3D=3D 0) return; memcpy(buf, text, mn); - if(mn < n) - for(n =3D MAX(mn-3, 0); n < mn; buf[n++] =3D '.'); + if (mn < n) + for (n =3D MAX(mn-3, 0); n < mn; buf[n++] =3D '.'); =20 drawrect(dc, 0, 0, dc->w, dc->h, 1, BG(dc, col)); drawtextn(dc, buf, mn, col); } =20 void -drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) { +drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) +{ int x =3D dc->x + dc->font.height/2; int y =3D dc->y + dc->font.ascent+1; =20 XSetForeground(dc->dpy, dc->gc, FG(dc, col)); - if(dc->font.set) + if (dc->font.set) XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n); else { XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); _AT_@ -54,14 +58,15 @@ drawtextn(DC *dc, const char *text, size_t n, unsigned = long col[ColLast]) { } =20 void -eprintf(const char *fmt, ...) { +eprintf(const char *fmt, ...) +{ va_list ap; =20 va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); =20 - if(fmt[0] !=3D '\0' && fmt[strlen(fmt)-1] =3D=3D ':') { + if (fmt[0] !=3D '\0' && fmt[strlen(fmt)-1] =3D=3D ':') { fputc(' ', stderr); perror(NULL); } _AT_@ -69,12 +74,13 @@ eprintf(const char *fmt, ...) { } =20 void -freedc(DC *dc) { - if(dc->font.set) +freedc(DC *dc) +{ + if (dc->font.set) XFreeFontSet(dc->dpy, dc->font.set); - if(dc->font.xfont) + if (dc->font.xfont) XFreeFont(dc->dpy, dc->font.xfont); - if(dc->canvas) + if (dc->canvas) XFreePixmap(dc->dpy, dc->canvas); XFreeGC(dc->dpy, dc->gc); XCloseDisplay(dc->dpy); _AT_@ -82,24 +88,26 @@ freedc(DC *dc) { } =20 unsigned long -getcolor(DC *dc, const char *colstr) { +getcolor(DC *dc, const char *colstr) +{ Colormap cmap =3D DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); XColor color; =20 - if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) + if (!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) eprintf("cannot allocate color '%s'\n", colstr); return color.pixel; } =20 DC * -initdc(void) { +initdc(void) +{ DC *dc; =20 - if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) + if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) fputs("no locale support\n", stderr); - if(!(dc =3D calloc(1, sizeof *dc))) + if (!(dc =3D calloc(1, sizeof *dc))) eprintf("cannot malloc %u bytes:", sizeof *dc); - if(!(dc->dpy =3D XOpenDisplay(NULL))) + if (!(dc->dpy =3D XOpenDisplay(NULL))) eprintf("cannot open display\n"); =20 dc->gc =3D XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); _AT_@ -108,50 +116,54 @@ initdc(void) { } =20 void -initfont(DC *dc, const char *fontstr) { - if(!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) { - if(fontstr !=3D NULL) +initfont(DC *dc, const char *fontstr) +{ + if (!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) { + if (fontstr !=3D NULL) fprintf(stderr, "cannot load font '%s'\n", fontstr); - if(fontstr =3D=3D NULL || !loadfont(dc, DEFAULTFN)) + if (fontstr =3D=3D NULL || !loadfont(dc, DEFAULTFN)) eprintf("cannot load font '%s'\n", DEFAULTFN); } dc->font.height =3D dc->font.ascent + dc->font.descent; } =20 int -loadfont(DC *dc, const char *fontstr) { +loadfont(DC *dc, const char *fontstr) +{ char *def, **missing, **names; int i, n; XFontStruct **xfonts; =20 - if(!*fontstr) + if (!*fontstr) return 0; - if((dc->font.set =3D XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def)= )) { + if ((dc->font.set =3D XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def= ))) { n =3D XFontsOfFontSet(dc->font.set, &xfonts, &names); - for(i =3D 0; i < n; i++) { + for (i =3D 0; i < n; i++) { dc->font.ascent =3D MAX(dc->font.ascent, xfonts[i]->ascent); dc->font.descent =3D MAX(dc->font.descent, xfonts[i]->descent); dc->font.width =3D MAX(dc->font.width, xfonts[i]->max_bounds.width); } } - else if((dc->font.xfont =3D XLoadQueryFont(dc->dpy, fontstr))) { + else if ((dc->font.xfont =3D XLoadQueryFont(dc->dpy, fontstr))) { dc->font.ascent =3D dc->font.xfont->ascent; dc->font.descent =3D dc->font.xfont->descent; dc->font.width =3D dc->font.xfont->max_bounds.width; } - if(missing) + if (missing) XFreeStringList(missing); return dc->font.set || dc->font.xfont; } =20 void -mapdc(DC *dc, Window win, unsigned int w, unsigned int h) { +mapdc(DC *dc, Window win, unsigned int w, unsigned int h) +{ XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0); } =20 void -resizedc(DC *dc, unsigned int w, unsigned int h) { - if(dc->canvas) +resizedc(DC *dc, unsigned int w, unsigned int h) +{ + if (dc->canvas) XFreePixmap(dc->dpy, dc->canvas); =20 dc->w =3D w; _AT_@ -161,8 +173,9 @@ resizedc(DC *dc, unsigned int w, unsigned int h) { } =20 int -textnw(DC *dc, const char *text, size_t len) { - if(dc->font.set) { +textnw(DC *dc, const char *text, size_t len) +{ + if (dc->font.set) { XRectangle r; =20 XmbTextExtents(dc->font.set, text, len, NULL, &r); _AT_@ -172,6 +185,7 @@ textnw(DC *dc, const char *text, size_t len) { } =20 int -textw(DC *dc, const char *text) { +textw(DC *dc, const char *text) +{ return textnw(dc, text, strlen(text)) + dc->font.height; } diff --git a/draw.h b/draw.h index a6ef9de..aeba1b2 100644 --- a/draw.h +++ b/draw.h _AT_@ -6,30 +6,30 @@ enum { ColBG, ColFG, ColBorder, ColLast }; =20 typedef struct { - int x, y, w, h; - int invert; + int x, y, w, h; + int invert; Display *dpy; - GC gc; - Pixmap canvas; + GC gc; + Pixmap canvas; struct { - int ascent; - int descent; - int height; - int width; - XFontSet set; + int ascent; + int descent; + int height; + int width; + XFontSet set; XFontStruct *xfont; } font; } DC; /* draw context */ =20 -void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fi= ll, unsigned long color); -void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); -void drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLa= st]); -void eprintf(const char *fmt, ...); -void freedc(DC *dc); -unsigned long getcolor(DC *dc, const char *colstr); +void drawrect(DC *, int, int, unsigned int, unsigned int, int, unsigned lo= ng); +void drawtext(DC *, const char *, unsigned long[ColLast]); +void drawtextn(DC *, const char *, size_t, unsigned long[ColLast]); +void eprintf(const char *, ...); +void freedc(DC *); +unsigned long getcolor(DC *, const char *); DC *initdc(void); -void initfont(DC *dc, const char *fontstr); -void mapdc(DC *dc, Window win, unsigned int w, unsigned int h); -void resizedc(DC *dc, unsigned int w, unsigned int h); -int textnw(DC *dc, const char *text, size_t len); -int textw(DC *dc, const char *text); +void initfont(DC *, const char *); +void mapdc(DC *, Window, unsigned int, unsigned int); +void resizedc(DC *, unsigned int, unsigned int); +int textnw(DC *, const char *, size_t); +int textw(DC *, const char *); diff --git a/stest.c b/stest.c index 886d418..ca90ca5 100644 --- a/stest.c +++ b/stest.c _AT_@ -3,8 +3,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <sys/stat.h> +#include <unistd.h> =20 #define FLAG(x) (flag[(x)-'a']) =20 _AT_@ -15,17 +15,18 @@ static int flag[26]; static struct stat old, new; =20 int -main(int argc, char *argv[]) { +main(int argc, char *argv[]) +{ struct dirent *d; char buf[BUFSIZ], *p; DIR *dir; int opt; =20 - while((opt =3D getopt(argc, argv, "abcdefghln:o:pqrsuvwx")) !=3D -1) - switch(opt) { + while ((opt =3D getopt(argc, argv, "abcdefghln:o:pqrsuvwx")) !=3D -1) + switch (opt) { case 'n': /* newer than file */ case 'o': /* older than file */ - if(!(FLAG(opt) =3D !stat(optarg, (opt =3D=3D 'n' ? &new : &old)))) + if (!(FLAG(opt) =3D !stat(optarg, (opt =3D=3D 'n' ? &new : &old)))) perror(optarg); break; default: /* miscellaneous operators */ _AT_@ -35,17 +36,17 @@ main(int argc, char *argv[]) { fprintf(stderr, "usage: %s [-abcdefghlpqrsuvwx] [-n file] [-o file] [fi= le...]\n", argv[0]); exit(2); } - if(optind =3D=3D argc) - while(fgets(buf, sizeof buf, stdin)) { - if((p =3D strchr(buf, '\n'))) + if (optind =3D=3D argc) + while (fgets(buf, sizeof buf, stdin)) { + if ((p =3D strchr(buf, '\n'))) *p =3D '\0'; test(buf, buf); } - for(; optind < argc; optind++) - if(FLAG('l') && (dir =3D opendir(argv[optind]))) { + for (; optind < argc; optind++) + if (FLAG('l') && (dir =3D opendir(argv[optind]))) { /* test directory contents */ - while((d =3D readdir(dir))) - if(snprintf(buf, sizeof buf, "%s/%s", argv[optind], d->d_name) < sizeo= f buf) + while ((d =3D readdir(dir))) + if (snprintf(buf, sizeof buf, "%s/%s", argv[optind], d->d_name) < size= of buf) test(buf, d->d_name); closedir(dir); } _AT_@ -56,10 +57,11 @@ main(int argc, char *argv[]) { } =20 void -test(const char *path, const char *name) { +test(const char *path, const char *name) +{ struct stat st, ln; =20 - if((!stat(path, &st) && (FLAG('a') || name[0] !=3D '.') /* hidden= files */ + if ((!stat(path, &st) && (FLAG('a') || name[0] !=3D '.') /* hidden= files */ && (!FLAG('b') || S_ISBLK(st.st_mode)) /* block sp= ecial */ && (!FLAG('c') || S_ISCHR(st.st_mode)) /* characte= r special */ && (!FLAG('d') || S_ISDIR(st.st_mode)) /* director= y */ _AT_@ -75,7 +77,7 @@ test(const char *path, const char *name) { && (!FLAG('u') || st.st_mode & S_ISUID) /* set-user= -id flag */ && (!FLAG('w') || access(path, W_OK) =3D=3D 0) /* writ= able */ && (!FLAG('x') || access(path, X_OK) =3D=3D 0)) !=3D FLAG('v')) { /* ex= ecutable */ - if(FLAG('q')) + if (FLAG('q')) exit(0); match =3D 1; puts(name); --=20 1.8.5.5 --Multipart=_Mon__22_Dec_2014_18_40_59_+0100_ph7wbDS_mXmI_UXb Content-Type: text/x-diff; name="0005-Add-myself-to-License.patch" Content-Disposition: attachment; filename="0005-Add-myself-to-License.patch" Content-Transfer-Encoding: quoted-printableReceived on Mon Sep 17 2001 - 00:00:00 CEST
This archive was generated by hypermail 2.3.0 : Mon Dec 22 2014 - 19:12:04 CET