diff options
| -rw-r--r-- | Makefile | 26 | ||||
| -rw-r--r-- | README | 6 | ||||
| -rw-r--r-- | bar.c | 41 | ||||
| -rw-r--r-- | client.c | 34 | ||||
| -rw-r--r-- | config.h | 15 | ||||
| -rw-r--r-- | draw.c | 5 | ||||
| -rw-r--r-- | draw.h | 36 | ||||
| -rw-r--r-- | event.c | 9 | ||||
| -rw-r--r-- | gridmenu.1 | 68 | ||||
| -rw-r--r-- | kb.c | 12 | ||||
| -rw-r--r-- | menu.c | 432 | ||||
| -rw-r--r-- | util.c | 51 | ||||
| -rw-r--r-- | util.h | 22 | ||||
| -rw-r--r-- | wm.c | 60 | ||||
| -rw-r--r-- | wm.h | 63 |
15 files changed, 95 insertions, 785 deletions
| @@ -3,14 +3,12 @@ | |||
| 3 | 3 | ||
| 4 | include config.mk | 4 | include config.mk |
| 5 | 5 | ||
| 6 | WMSRC = bar.c client.c draw.c event.c kb.c mouse.c util.c wm.c | 6 | SRC = client.c draw.c event.c kb.c mouse.c util.c wm.c |
| 7 | WMOBJ = ${WMSRC:.c=.o} | 7 | OBJ = ${SRC:.c=.o} |
| 8 | MENSRC = menu.c draw.c util.c | 8 | MAN1 = gridwm.1 |
| 9 | MENOBJ = ${MENSRC:.c=.o} | 9 | BIN = gridwm |
| 10 | MAN1 = gridwm.1 gridmenu.1 | 10 | |
| 11 | BIN = gridwm gridmenu | 11 | all: config gridwm |
| 12 | |||
| 13 | all: config gridwm gridmenu | ||
| 14 | @echo finished | 12 | @echo finished |
| 15 | 13 | ||
| 16 | config: | 14 | config: |
| @@ -24,18 +22,14 @@ config: | |||
| 24 | @echo CC $< | 22 | @echo CC $< |
| 25 | @${CC} -c ${CFLAGS} $< | 23 | @${CC} -c ${CFLAGS} $< |
| 26 | 24 | ||
| 27 | ${WMOBJ}: wm.h draw.h config.h util.h | 25 | ${OBJ}: wm.h |
| 28 | |||
| 29 | gridmenu: ${MENOBJ} | ||
| 30 | @echo LD $@ | ||
| 31 | @${CC} -o $@ ${MENOBJ} ${LDFLAGS} | ||
| 32 | 26 | ||
| 33 | gridwm: ${WMOBJ} | 27 | gridwm: ${OBJ} |
| 34 | @echo LD $@ | 28 | @echo LD $@ |
| 35 | @${CC} -o $@ ${WMOBJ} ${LDFLAGS} | 29 | @${CC} -o $@ ${OBJ} ${LDFLAGS} |
| 36 | 30 | ||
| 37 | clean: | 31 | clean: |
| 38 | rm -f gridwm gridmenu *.o core | 32 | rm -f gridwm *.o core |
| 39 | 33 | ||
| 40 | dist: clean | 34 | dist: clean |
| 41 | mkdir -p gridwm-${VERSION} | 35 | mkdir -p gridwm-${VERSION} |
| @@ -37,7 +37,5 @@ This will start gridwm on display :1 of the host foo.bar. | |||
| 37 | 37 | ||
| 38 | Configuration | 38 | Configuration |
| 39 | ------------- | 39 | ------------- |
| 40 | The configuration of gridwm is done by customizing the config.h source file. To | 40 | The configuration of gridwm is done by customizing the wm.h source file. To |
| 41 | customize the key bindings edit kb.c. To change the status output, edit the | 41 | customize the key bindings edit kb.c. |
| 42 | status command definition in wm.c. | ||
| 43 | |||
| @@ -1,41 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> | ||
| 3 | * See LICENSE file for license details. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #include "wm.h" | ||
| 7 | |||
| 8 | void | ||
| 9 | draw_bar() | ||
| 10 | { | ||
| 11 | int i; | ||
| 12 | brush.x = brush.y = 0; | ||
| 13 | brush.w = bw; | ||
| 14 | brush.h = bh; | ||
| 15 | draw(dpy, &brush, False, NULL); | ||
| 16 | |||
| 17 | brush.w = 0; | ||
| 18 | for(i = 0; i < TLast; i++) { | ||
| 19 | brush.x += brush.w; | ||
| 20 | brush.w = textw(&brush.font, tags[i]) + bh; | ||
| 21 | if(i == tsel) { | ||
| 22 | swap((void **)&brush.fg, (void **)&brush.bg); | ||
| 23 | draw(dpy, &brush, True, tags[i]); | ||
| 24 | swap((void **)&brush.fg, (void **)&brush.bg); | ||
| 25 | } | ||
| 26 | else | ||
| 27 | draw(dpy, &brush, True, tags[i]); | ||
| 28 | } | ||
| 29 | if(stack) { | ||
| 30 | swap((void **)&brush.fg, (void **)&brush.bg); | ||
| 31 | brush.x += brush.w; | ||
| 32 | brush.w = textw(&brush.font, stack->name) + bh; | ||
| 33 | draw(dpy, &brush, True, stack->name); | ||
| 34 | swap((void **)&brush.fg, (void **)&brush.bg); | ||
| 35 | } | ||
| 36 | brush.w = textw(&brush.font, stext) + bh; | ||
| 37 | brush.x = bx + bw - brush.w; | ||
| 38 | draw(dpy, &brush, False, stext); | ||
| 39 | XCopyArea(dpy, brush.drawable, barwin, brush.gc, 0, 0, bw, bh, 0, 0); | ||
| 40 | XFlush(dpy); | ||
| 41 | } | ||
| @@ -7,8 +7,8 @@ | |||
| 7 | #include <stdlib.h> | 7 | #include <stdlib.h> |
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <X11/Xatom.h> | 9 | #include <X11/Xatom.h> |
| 10 | #include <X11/Xutil.h> | ||
| 10 | 11 | ||
| 11 | #include "util.h" | ||
| 12 | #include "wm.h" | 12 | #include "wm.h" |
| 13 | 13 | ||
| 14 | void (*arrange)(void *aux); | 14 | void (*arrange)(void *aux); |
| @@ -19,9 +19,9 @@ max(void *aux) | |||
| 19 | if(!stack) | 19 | if(!stack) |
| 20 | return; | 20 | return; |
| 21 | stack->x = sx; | 21 | stack->x = sx; |
| 22 | stack->y = bh; | 22 | stack->y = sy; |
| 23 | stack->w = sw - 2 * stack->border; | 23 | stack->w = sw - 2 * stack->border; |
| 24 | stack->h = sh - bh - 2 * stack->border; | 24 | stack->h = sh - 2 * stack->border; |
| 25 | resize(stack); | 25 | resize(stack); |
| 26 | discard_events(EnterWindowMask); | 26 | discard_events(EnterWindowMask); |
| 27 | } | 27 | } |
| @@ -59,11 +59,11 @@ grid(void *aux) | |||
| 59 | cols = rows; | 59 | cols = rows; |
| 60 | 60 | ||
| 61 | gw = (sw - 2) / cols; | 61 | gw = (sw - 2) / cols; |
| 62 | gh = (sh - bh - 2) / rows; | 62 | gh = (sh - 2) / rows; |
| 63 | 63 | ||
| 64 | for(i = j = 0, c = clients; c; c = c->next) { | 64 | for(i = j = 0, c = clients; c; c = c->next) { |
| 65 | c->x = i * gw; | 65 | c->x = i * gw; |
| 66 | c->y = j * gh + bh; | 66 | c->y = j * gh; |
| 67 | c->w = gw; | 67 | c->w = gw; |
| 68 | c->h = gh; | 68 | c->h = gh; |
| 69 | resize(c); | 69 | resize(c); |
| @@ -89,12 +89,12 @@ sel(void *aux) | |||
| 89 | for(c = stack; c && c->snext; c = c->snext); | 89 | for(c = stack; c && c->snext; c = c->snext); |
| 90 | if(!c) | 90 | if(!c) |
| 91 | c = stack; | 91 | c = stack; |
| 92 | raise(c); | 92 | craise(c); |
| 93 | focus(c); | 93 | focus(c); |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | void | 96 | void |
| 97 | kill(void *aux) | 97 | ckill(void *aux) |
| 98 | { | 98 | { |
| 99 | Client *c = stack; | 99 | Client *c = stack; |
| 100 | 100 | ||
| @@ -114,8 +114,8 @@ resize_title(Client *c) | |||
| 114 | c->tw = 0; | 114 | c->tw = 0; |
| 115 | for(i = 0; i < TLast; i++) | 115 | for(i = 0; i < TLast; i++) |
| 116 | if(c->tags[i]) | 116 | if(c->tags[i]) |
| 117 | c->tw += textw(&brush.font, c->tags[i]) + bh; | 117 | c->tw += textw(&brush.font, c->tags[i]) + brush.font.height; |
| 118 | c->tw += textw(&brush.font, c->name) + bh; | 118 | c->tw += textw(&brush.font, c->name) + brush.font.height; |
| 119 | if(c->tw > c->w) | 119 | if(c->tw > c->w) |
| 120 | c->tw = c->w + 2; | 120 | c->tw = c->w + 2; |
| 121 | c->tx = c->x + c->w - c->tw + 2; | 121 | c->tx = c->x + c->w - c->tw + 2; |
| @@ -190,7 +190,7 @@ update_size(Client *c) | |||
| 190 | } | 190 | } |
| 191 | 191 | ||
| 192 | void | 192 | void |
| 193 | raise(Client *c) | 193 | craise(Client *c) |
| 194 | { | 194 | { |
| 195 | XRaiseWindow(dpy, c->win); | 195 | XRaiseWindow(dpy, c->win); |
| 196 | XRaiseWindow(dpy, c->title); | 196 | XRaiseWindow(dpy, c->title); |
| @@ -234,11 +234,9 @@ manage(Window w, XWindowAttributes *wa) | |||
| 234 | c->win = w; | 234 | c->win = w; |
| 235 | c->tx = c->x = wa->x; | 235 | c->tx = c->x = wa->x; |
| 236 | c->ty = c->y = wa->y; | 236 | c->ty = c->y = wa->y; |
| 237 | if(c->y < bh) | ||
| 238 | c->ty = c->y += bh; | ||
| 239 | c->tw = c->w = wa->width; | 237 | c->tw = c->w = wa->width; |
| 240 | c->h = wa->height; | 238 | c->h = wa->height; |
| 241 | c->th = bh; | 239 | c->th = th; |
| 242 | c->border = 1; | 240 | c->border = 1; |
| 243 | update_size(c); | 241 | update_size(c); |
| 244 | XSetWindowBorderWidth(dpy, c->win, 1); | 242 | XSetWindowBorderWidth(dpy, c->win, 1); |
| @@ -379,10 +377,8 @@ unmanage(Client *c) | |||
| 379 | XDestroyWindow(dpy, c->title); | 377 | XDestroyWindow(dpy, c->title); |
| 380 | 378 | ||
| 381 | for(l=&clients; *l && *l != c; l=&(*l)->next); | 379 | for(l=&clients; *l && *l != c; l=&(*l)->next); |
| 382 | eassert(*l == c); | ||
| 383 | *l = c->next; | 380 | *l = c->next; |
| 384 | for(l=&stack; *l && *l != c; l=&(*l)->snext); | 381 | for(l=&stack; *l && *l != c; l=&(*l)->snext); |
| 385 | eassert(*l == c); | ||
| 386 | *l = c->snext; | 382 | *l = c->snext; |
| 387 | free(c); | 383 | free(c); |
| 388 | 384 | ||
| @@ -418,10 +414,8 @@ void | |||
| 418 | draw_client(Client *c) | 414 | draw_client(Client *c) |
| 419 | { | 415 | { |
| 420 | int i; | 416 | int i; |
| 421 | if(c == stack) { | 417 | if(c == stack) |
| 422 | draw_bar(); | ||
| 423 | return; | 418 | return; |
| 424 | } | ||
| 425 | 419 | ||
| 426 | brush.x = brush.y = 0; | 420 | brush.x = brush.y = 0; |
| 427 | brush.h = c->th; | 421 | brush.h = c->th; |
| @@ -430,12 +424,12 @@ draw_client(Client *c) | |||
| 430 | for(i = 0; i < TLast; i++) { | 424 | for(i = 0; i < TLast; i++) { |
| 431 | if(c->tags[i]) { | 425 | if(c->tags[i]) { |
| 432 | brush.x += brush.w; | 426 | brush.x += brush.w; |
| 433 | brush.w = textw(&brush.font, c->tags[i]) + bh; | 427 | brush.w = textw(&brush.font, c->tags[i]) + brush.font.height; |
| 434 | draw(dpy, &brush, True, c->tags[i]); | 428 | draw(dpy, &brush, True, c->tags[i]); |
| 435 | } | 429 | } |
| 436 | } | 430 | } |
| 437 | brush.x += brush.w; | 431 | brush.x += brush.w; |
| 438 | brush.w = textw(&brush.font, c->name) + bh; | 432 | brush.w = textw(&brush.font, c->name) + brush.font.height; |
| 439 | draw(dpy, &brush, True, c->name); | 433 | draw(dpy, &brush, True, c->name); |
| 440 | XCopyArea(dpy, brush.drawable, c->title, brush.gc, | 434 | XCopyArea(dpy, brush.drawable, c->title, brush.gc, |
| 441 | 0, 0, c->tw, c->th, 0, 0); | 435 | 0, 0, c->tw, c->th, 0, 0); |
diff --git a/config.h b/config.h deleted file mode 100644 index dba11da..0000000 --- a/config.h +++ /dev/null | |||
| @@ -1,15 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> | ||
| 3 | * See LICENSE file for license details. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #define FONT "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*" | ||
| 7 | #define BGCOLOR "#666699" | ||
| 8 | #define FGCOLOR "#ffffff" | ||
| 9 | #define BORDERCOLOR "#9999CC" | ||
| 10 | #define STATUSDELAY 10 /* seconds */ | ||
| 11 | |||
| 12 | /* tags, see wm.c for further config */ | ||
| 13 | enum { Tscratch, Tdev, Tirc, Twww, Twork, /* never remove: */ TLast }; | ||
| 14 | |||
| 15 | /* see kb.c for shortcut customization */ | ||
| @@ -6,8 +6,9 @@ | |||
| 6 | #include <stdio.h> | 6 | #include <stdio.h> |
| 7 | #include <string.h> | 7 | #include <string.h> |
| 8 | 8 | ||
| 9 | #include "draw.h" | 9 | #include <X11/Xlocale.h> |
| 10 | #include "util.h" | 10 | |
| 11 | #include "wm.h" | ||
| 11 | 12 | ||
| 12 | static void | 13 | static void |
| 13 | drawborder(Display *dpy, Brush *b) | 14 | drawborder(Display *dpy, Brush *b) |
| @@ -1,36 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> | ||
| 3 | * See LICENSE file for license details. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #include <X11/Xlib.h> | ||
| 7 | #include <X11/Xlocale.h> | ||
| 8 | |||
| 9 | typedef struct Brush Brush; | ||
| 10 | typedef struct Fnt Fnt; | ||
| 11 | |||
| 12 | struct Fnt { | ||
| 13 | XFontStruct *xfont; | ||
| 14 | XFontSet set; | ||
| 15 | int ascent; | ||
| 16 | int descent; | ||
| 17 | int height; | ||
| 18 | }; | ||
| 19 | |||
| 20 | struct Brush { | ||
| 21 | GC gc; | ||
| 22 | Drawable drawable; | ||
| 23 | int x, y, w, h; | ||
| 24 | Fnt font; | ||
| 25 | unsigned long bg; | ||
| 26 | unsigned long fg; | ||
| 27 | unsigned long border; | ||
| 28 | }; | ||
| 29 | |||
| 30 | extern void draw(Display *dpy, Brush *b, Bool border, const char *text); | ||
| 31 | extern void loadcolors(Display *dpy, int screen, Brush *b, | ||
| 32 | const char *bg, const char *fg, const char *bo); | ||
| 33 | extern void loadfont(Display *dpy, Fnt *font, const char *fontstr); | ||
| 34 | extern unsigned int textnw(Fnt *font, char *text, unsigned int len); | ||
| 35 | extern unsigned int textw(Fnt *font, char *text); | ||
| 36 | extern unsigned int texth(Fnt *font); | ||
| @@ -51,7 +51,7 @@ buttonpress(XEvent *e) | |||
| 51 | Client *c; | 51 | Client *c; |
| 52 | 52 | ||
| 53 | if((c = getclient(ev->window))) { | 53 | if((c = getclient(ev->window))) { |
| 54 | raise(c); | 54 | craise(c); |
| 55 | switch(ev->button) { | 55 | switch(ev->button) { |
| 56 | default: | 56 | default: |
| 57 | break; | 57 | break; |
| @@ -147,8 +147,6 @@ expose(XEvent *e) | |||
| 147 | if(ev->count == 0) { | 147 | if(ev->count == 0) { |
| 148 | if((c = gettitle(ev->window))) | 148 | if((c = gettitle(ev->window))) |
| 149 | draw_client(c); | 149 | draw_client(c); |
| 150 | else if(ev->window == barwin) | ||
| 151 | draw_bar(); | ||
| 152 | } | 150 | } |
| 153 | } | 151 | } |
| 154 | 152 | ||
| @@ -203,10 +201,7 @@ propertynotify(XEvent *e) | |||
| 203 | } | 201 | } |
| 204 | if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) { | 202 | if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) { |
| 205 | update_name(c); | 203 | update_name(c); |
| 206 | if(c == stack) | 204 | draw_client(c); |
| 207 | draw_bar(); | ||
| 208 | else | ||
| 209 | draw_client(c); | ||
| 210 | } | 205 | } |
| 211 | } | 206 | } |
| 212 | } | 207 | } |
diff --git a/gridmenu.1 b/gridmenu.1 deleted file mode 100644 index 527f186..0000000 --- a/gridmenu.1 +++ /dev/null | |||
| @@ -1,68 +0,0 @@ | |||
| 1 | .TH GRIDMENU 1 grid-0.0 | ||
| 2 | .SH NAME | ||
| 3 | gridmenu \- grid window manager menu | ||
| 4 | .SH SYNOPSIS | ||
| 5 | .B gridmenu | ||
| 6 | .RB [ \-v ] | ||
| 7 | .RB [ \-t | ||
| 8 | .IR title ] | ||
| 9 | .SH DESCRIPTION | ||
| 10 | .SS Overview | ||
| 11 | .B gridmenu | ||
| 12 | is a generic, highly customizable, and efficient menu for the X Window System, | ||
| 13 | originally designed for | ||
| 14 | .BR grid (1). | ||
| 15 | It supports arbitrary, user defined menu contents. | ||
| 16 | .SS Options | ||
| 17 | .TP | ||
| 18 | .B \-v | ||
| 19 | prints version information to stdout, then exits. | ||
| 20 | .TP | ||
| 21 | .BI \-t " title" | ||
| 22 | displays | ||
| 23 | .I title | ||
| 24 | above the menu. | ||
| 25 | .SS Usage | ||
| 26 | .B gridmenu | ||
| 27 | reads a list of newline-separated items from stdin and creates a menu. | ||
| 28 | When the user selects an item or enters any text and presses Enter, his choice | ||
| 29 | is printed to stdout and | ||
| 30 | .B gridmenu | ||
| 31 | terminates. | ||
| 32 | .SS Keyboard Control | ||
| 33 | .B gridmenu | ||
| 34 | is completely controlled by the keyboard. The following keys are recognized: | ||
| 35 | .TP 2 | ||
| 36 | Any printable character | ||
| 37 | appends the character to the text in the input field. This works as a filter: | ||
| 38 | only items containing this text will be displayed. | ||
| 39 | .TP 2 | ||
| 40 | Left/Right (Control-p/Control-n) | ||
| 41 | select the previous/next item. | ||
| 42 | .TP 2 | ||
| 43 | Tab (Control-i) | ||
| 44 | copy the selected item to the input field. | ||
| 45 | .TP 2 | ||
| 46 | Enter (Control-j) | ||
| 47 | confirm selection and quit (print the selected item to stdout). | ||
| 48 | .TP 2 | ||
| 49 | Shift-Enter (Shift-Control-j) | ||
| 50 | confirm selection and quit (print the text in the input field to stdout). | ||
| 51 | .TP 2 | ||
| 52 | Escape (Control-[) | ||
| 53 | quit without selecting an item. | ||
| 54 | .TP 2 | ||
| 55 | Backspace (Control-h) | ||
| 56 | remove enough characters from the input field to change its filtering effect. | ||
| 57 | .TP 2 | ||
| 58 | Control-u | ||
| 59 | remove all characters from the input field. | ||
| 60 | .SS Exit codes | ||
| 61 | .B gridmenu | ||
| 62 | returns | ||
| 63 | .B 0 | ||
| 64 | if Enter is pressed on termination, | ||
| 65 | .B 1 | ||
| 66 | if Escape is pressed. | ||
| 67 | .SH SEE ALSO | ||
| 68 | .BR gridwm (1) | ||
| @@ -9,25 +9,19 @@ | |||
| 9 | 9 | ||
| 10 | /********** CUSTOMIZE **********/ | 10 | /********** CUSTOMIZE **********/ |
| 11 | 11 | ||
| 12 | char *cmdterm[] = { | 12 | char *term[] = { |
| 13 | "aterm", "-tr", "+sb", "-bg", "black", "-fg", "white", "-fn", | 13 | "aterm", "-tr", "+sb", "-bg", "black", "-fg", "white", "-fn", |
| 14 | "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*",NULL | 14 | "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*",NULL |
| 15 | }; | 15 | }; |
| 16 | 16 | ||
| 17 | char *cmdproglist[] = { | ||
| 18 | "sh", "-c", "exec `ls -lL /bin /sbin /usr/bin /usr/local/bin 2>/dev/null " | ||
| 19 | "| awk 'NF>2 && $1 ~ /^[^d].*x/ {print $NF}' | sort | uniq | gridmenu`", 0 | ||
| 20 | }; | ||
| 21 | |||
| 22 | static Key key[] = { | 17 | static Key key[] = { |
| 23 | { Mod1Mask, XK_Return, run, cmdterm }, | 18 | { Mod1Mask, XK_Return, run, term }, |
| 24 | { Mod1Mask, XK_p, run, cmdproglist}, | ||
| 25 | { Mod1Mask, XK_k, sel, "prev" }, | 19 | { Mod1Mask, XK_k, sel, "prev" }, |
| 26 | { Mod1Mask, XK_j, sel, "next" }, | 20 | { Mod1Mask, XK_j, sel, "next" }, |
| 27 | { Mod1Mask, XK_g, grid, NULL }, | 21 | { Mod1Mask, XK_g, grid, NULL }, |
| 28 | { Mod1Mask, XK_f, floating, NULL }, | 22 | { Mod1Mask, XK_f, floating, NULL }, |
| 29 | { Mod1Mask, XK_m, max, NULL }, | 23 | { Mod1Mask, XK_m, max, NULL }, |
| 30 | { Mod1Mask | ShiftMask, XK_c, kill, NULL }, | 24 | { Mod1Mask | ShiftMask, XK_c, ckill, NULL }, |
| 31 | { Mod1Mask | ShiftMask, XK_q, quit, NULL }, | 25 | { Mod1Mask | ShiftMask, XK_q, quit, NULL }, |
| 32 | }; | 26 | }; |
| 33 | 27 | ||
| @@ -1,432 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> | ||
| 3 | * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com> | ||
| 4 | * See LICENSE file for license details. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include "config.h" | ||
| 8 | #include "draw.h" | ||
| 9 | #include "util.h" | ||
| 10 | |||
| 11 | #include <ctype.h> | ||
| 12 | #include <stdlib.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include <string.h> | ||
| 15 | #include <unistd.h> | ||
| 16 | #include <X11/cursorfont.h> | ||
| 17 | #include <X11/Xutil.h> | ||
| 18 | #include <X11/keysym.h> | ||
| 19 | |||
| 20 | typedef struct Item Item; | ||
| 21 | |||
| 22 | struct Item { | ||
| 23 | Item *next; /* traverses all items */ | ||
| 24 | Item *left, *right; /* traverses items matching current search pattern */ | ||
| 25 | char *text; | ||
| 26 | }; | ||
| 27 | |||
| 28 | static Display *dpy; | ||
| 29 | static Window root; | ||
| 30 | static Window win; | ||
| 31 | static Bool done = False; | ||
| 32 | |||
| 33 | static Item *allitem = NULL; /* first of all items */ | ||
| 34 | static Item *item = NULL; /* first of pattern matching items */ | ||
| 35 | static Item *sel = NULL; | ||
| 36 | static Item *nextoff = NULL; | ||
| 37 | static Item *prevoff = NULL; | ||
| 38 | static Item *curroff = NULL; | ||
| 39 | |||
| 40 | static int screen, mx, my, mw, mh; | ||
| 41 | static char *title = NULL; | ||
| 42 | static char text[4096]; | ||
| 43 | static int ret = 0; | ||
| 44 | static int nitem = 0; | ||
| 45 | static unsigned int cmdw = 0; | ||
| 46 | static unsigned int tw = 0; | ||
| 47 | static unsigned int cw = 0; | ||
| 48 | static const int seek = 30; /* 30px */ | ||
| 49 | |||
| 50 | static Brush brush = {0}; | ||
| 51 | |||
| 52 | static void draw_menu(); | ||
| 53 | static void kpress(XKeyEvent * e); | ||
| 54 | |||
| 55 | static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; | ||
| 56 | |||
| 57 | static void | ||
| 58 | usage() { error("usage: gridmenu [-v] [-t <title>]\n"); } | ||
| 59 | |||
| 60 | static void | ||
| 61 | update_offsets() | ||
| 62 | { | ||
| 63 | unsigned int tw, w = cmdw + 2 * seek; | ||
| 64 | |||
| 65 | if(!curroff) | ||
| 66 | return; | ||
| 67 | |||
| 68 | for(nextoff = curroff; nextoff; nextoff=nextoff->right) { | ||
| 69 | tw = textw(&brush.font, nextoff->text); | ||
| 70 | if(tw > mw / 3) | ||
| 71 | tw = mw / 3; | ||
| 72 | w += tw + brush.font.height; | ||
| 73 | if(w > mw) | ||
| 74 | break; | ||
| 75 | } | ||
| 76 | |||
| 77 | w = cmdw + 2 * seek; | ||
| 78 | for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) { | ||
| 79 | tw = textw(&brush.font, prevoff->left->text); | ||
| 80 | if(tw > mw / 3) | ||
| 81 | tw = mw / 3; | ||
| 82 | w += tw + brush.font.height; | ||
| 83 | if(w > mw) | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | static void | ||
| 89 | update_items(char *pattern) | ||
| 90 | { | ||
| 91 | unsigned int plen = strlen(pattern); | ||
| 92 | Item *i, *j; | ||
| 93 | |||
| 94 | if(!pattern) | ||
| 95 | return; | ||
| 96 | |||
| 97 | if(!title || *pattern) | ||
| 98 | cmdw = cw; | ||
| 99 | else | ||
| 100 | cmdw = tw; | ||
| 101 | |||
| 102 | item = j = NULL; | ||
| 103 | nitem = 0; | ||
| 104 | |||
| 105 | for(i = allitem; i; i=i->next) | ||
| 106 | if(!plen || !strncmp(pattern, i->text, plen)) { | ||
| 107 | if(!j) | ||
| 108 | item = i; | ||
| 109 | else | ||
| 110 | j->right = i; | ||
| 111 | i->left = j; | ||
| 112 | i->right = NULL; | ||
| 113 | j = i; | ||
| 114 | nitem++; | ||
| 115 | } | ||
| 116 | for(i = allitem; i; i=i->next) | ||
| 117 | if(plen && strncmp(pattern, i->text, plen) | ||
| 118 | && strstr(i->text, pattern)) { | ||
| 119 | if(!j) | ||
| 120 | item = i; | ||
| 121 | else | ||
| 122 | j->right = i; | ||
| 123 | i->left = j; | ||
| 124 | i->right = NULL; | ||
| 125 | j = i; | ||
| 126 | nitem++; | ||
| 127 | } | ||
| 128 | |||
| 129 | curroff = prevoff = nextoff = sel = item; | ||
| 130 | |||
| 131 | update_offsets(); | ||
| 132 | } | ||
| 133 | |||
| 134 | /* creates brush structs for brush mode drawing */ | ||
| 135 | static void | ||
| 136 | draw_menu() | ||
| 137 | { | ||
| 138 | Item *i; | ||
| 139 | |||
| 140 | brush.x = 0; | ||
| 141 | brush.y = 0; | ||
| 142 | brush.w = mw; | ||
| 143 | brush.h = mh; | ||
| 144 | draw(dpy, &brush, False, 0); | ||
| 145 | |||
| 146 | /* print command */ | ||
| 147 | if(!title || text[0]) { | ||
| 148 | cmdw = cw; | ||
| 149 | if(cmdw && item) | ||
| 150 | brush.w = cmdw; | ||
| 151 | draw(dpy, &brush, False, text); | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | cmdw = tw; | ||
| 155 | brush.w = cmdw; | ||
| 156 | draw(dpy, &brush, False, title); | ||
| 157 | } | ||
| 158 | brush.x += brush.w; | ||
| 159 | |||
| 160 | if(curroff) { | ||
| 161 | brush.w = seek; | ||
| 162 | draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0); | ||
| 163 | brush.x += brush.w; | ||
| 164 | |||
| 165 | /* determine maximum items */ | ||
| 166 | for(i = curroff; i != nextoff; i=i->right) { | ||
| 167 | brush.border = False; | ||
| 168 | brush.w = textw(&brush.font, i->text); | ||
| 169 | if(brush.w > mw / 3) | ||
| 170 | brush.w = mw / 3; | ||
| 171 | brush.w += brush.font.height; | ||
| 172 | if(sel == i) { | ||
| 173 | swap((void **)&brush.fg, (void **)&brush.bg); | ||
| 174 | draw(dpy, &brush, True, i->text); | ||
| 175 | swap((void **)&brush.fg, (void **)&brush.bg); | ||
| 176 | } | ||
| 177 | else | ||
| 178 | draw(dpy, &brush, False, i->text); | ||
| 179 | brush.x += brush.w; | ||
| 180 | } | ||
| 181 | |||
| 182 | brush.x = mw - seek; | ||
| 183 | brush.w = seek; | ||
| 184 | draw(dpy, &brush, False, nextoff ? ">" : 0); | ||
| 185 | } | ||
| 186 | XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0); | ||
| 187 | XFlush(dpy); | ||
| 188 | } | ||
| 189 | |||
| 190 | static void | ||
| 191 | kpress(XKeyEvent * e) | ||
| 192 | { | ||
| 193 | KeySym ksym; | ||
| 194 | char buf[32]; | ||
| 195 | int num, prev_nitem; | ||
| 196 | unsigned int i, len = strlen(text); | ||
| 197 | |||
| 198 | buf[0] = 0; | ||
| 199 | num = XLookupString(e, buf, sizeof(buf), &ksym, 0); | ||
| 200 | |||
| 201 | if(IsFunctionKey(ksym) || IsKeypadKey(ksym) | ||
| 202 | || IsMiscFunctionKey(ksym) || IsPFKey(ksym) | ||
| 203 | || IsPrivateKeypadKey(ksym)) | ||
| 204 | return; | ||
| 205 | |||
| 206 | /* first check if a control mask is omitted */ | ||
| 207 | if(e->state & ControlMask) { | ||
| 208 | switch (ksym) { | ||
| 209 | default: /* ignore other control sequences */ | ||
| 210 | return; | ||
| 211 | break; | ||
| 212 | case XK_h: | ||
| 213 | ksym = XK_BackSpace; | ||
| 214 | break; | ||
| 215 | case XK_U: | ||
| 216 | case XK_u: | ||
| 217 | text[0] = 0; | ||
| 218 | update_items(text); | ||
| 219 | draw_menu(); | ||
| 220 | return; | ||
| 221 | break; | ||
| 222 | case XK_bracketleft: | ||
| 223 | ksym = XK_Escape; | ||
| 224 | break; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | switch(ksym) { | ||
| 228 | case XK_Left: | ||
| 229 | if(!(sel && sel->left)) | ||
| 230 | return; | ||
| 231 | sel=sel->left; | ||
| 232 | if(sel->right == curroff) { | ||
| 233 | curroff = prevoff; | ||
| 234 | update_offsets(); | ||
| 235 | } | ||
| 236 | break; | ||
| 237 | case XK_Tab: | ||
| 238 | if(!sel) | ||
| 239 | return; | ||
| 240 | strncpy(text, sel->text, sizeof(text)); | ||
| 241 | update_items(text); | ||
| 242 | break; | ||
| 243 | case XK_Right: | ||
| 244 | if(!(sel && sel->right)) | ||
| 245 | return; | ||
| 246 | sel=sel->right; | ||
| 247 | if(sel == nextoff) { | ||
| 248 | curroff = nextoff; | ||
| 249 | update_offsets(); | ||
| 250 | } | ||
| 251 | break; | ||
| 252 | case XK_Return: | ||
| 253 | if(e->state & ShiftMask) { | ||
| 254 | if(text) | ||
| 255 | fprintf(stdout, "%s", text); | ||
| 256 | } | ||
| 257 | else if(sel) | ||
| 258 | fprintf(stdout, "%s", sel->text); | ||
| 259 | else if(text) | ||
| 260 | fprintf(stdout, "%s", text); | ||
| 261 | fflush(stdout); | ||
| 262 | done = True; | ||
| 263 | break; | ||
| 264 | case XK_Escape: | ||
| 265 | ret = 1; | ||
| 266 | done = True; | ||
| 267 | break; | ||
| 268 | case XK_BackSpace: | ||
| 269 | if((i = len)) { | ||
| 270 | prev_nitem = nitem; | ||
| 271 | do { | ||
| 272 | text[--i] = 0; | ||
| 273 | update_items(text); | ||
| 274 | } while(i && nitem && prev_nitem == nitem); | ||
| 275 | update_items(text); | ||
| 276 | } | ||
| 277 | break; | ||
| 278 | default: | ||
| 279 | if(num && !iscntrl((int) buf[0])) { | ||
| 280 | buf[num] = 0; | ||
| 281 | if(len > 0) | ||
| 282 | strncat(text, buf, sizeof(text)); | ||
| 283 | else | ||
| 284 | strncpy(text, buf, sizeof(text)); | ||
| 285 | update_items(text); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | draw_menu(); | ||
| 289 | } | ||
| 290 | |||
| 291 | static char * | ||
| 292 | read_allitems() | ||
| 293 | { | ||
| 294 | static char *maxname = NULL; | ||
| 295 | char *p, buf[1024]; | ||
| 296 | unsigned int len = 0, max = 0; | ||
| 297 | Item *i, *new; | ||
| 298 | |||
| 299 | i = 0; | ||
| 300 | while(fgets(buf, sizeof(buf), stdin)) { | ||
| 301 | len = strlen(buf); | ||
| 302 | if (buf[len - 1] == '\n') | ||
| 303 | buf[len - 1] = 0; | ||
| 304 | p = estrdup(buf); | ||
| 305 | if(max < len) { | ||
| 306 | maxname = p; | ||
| 307 | max = len; | ||
| 308 | } | ||
| 309 | |||
| 310 | new = emalloc(sizeof(Item)); | ||
| 311 | new->next = new->left = new->right = NULL; | ||
| 312 | new->text = p; | ||
| 313 | if(!i) | ||
| 314 | allitem = new; | ||
| 315 | else | ||
| 316 | i->next = new; | ||
| 317 | i = new; | ||
| 318 | } | ||
| 319 | |||
| 320 | return maxname; | ||
| 321 | } | ||
| 322 | |||
| 323 | int | ||
| 324 | main(int argc, char *argv[]) | ||
| 325 | { | ||
| 326 | int i; | ||
| 327 | XSetWindowAttributes wa; | ||
| 328 | char *maxname; | ||
| 329 | XEvent ev; | ||
| 330 | |||
| 331 | /* command line args */ | ||
| 332 | for(i = 1; i < argc; i++) { | ||
| 333 | if (argv[i][0] == '-') | ||
| 334 | switch (argv[i][1]) { | ||
| 335 | case 'v': | ||
| 336 | fprintf(stdout, "%s", version); | ||
| 337 | exit(0); | ||
| 338 | break; | ||
| 339 | case 't': | ||
| 340 | if(++i < argc) | ||
| 341 | title = argv[i]; | ||
| 342 | else | ||
| 343 | usage(); | ||
| 344 | break; | ||
| 345 | default: | ||
| 346 | usage(); | ||
| 347 | break; | ||
| 348 | } | ||
| 349 | else | ||
| 350 | usage(); | ||
| 351 | } | ||
| 352 | |||
| 353 | dpy = XOpenDisplay(0); | ||
| 354 | if(!dpy) | ||
| 355 | error("gridmenu: cannot open dpy\n"); | ||
| 356 | screen = DefaultScreen(dpy); | ||
| 357 | root = RootWindow(dpy, screen); | ||
| 358 | |||
| 359 | maxname = read_allitems(); | ||
| 360 | |||
| 361 | /* grab as early as possible, but after reading all items!!! */ | ||
| 362 | while(XGrabKeyboard(dpy, root, True, GrabModeAsync, | ||
| 363 | GrabModeAsync, CurrentTime) != GrabSuccess) | ||
| 364 | usleep(1000); | ||
| 365 | |||
| 366 | /* style */ | ||
| 367 | loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); | ||
| 368 | loadfont(dpy, &brush.font, FONT); | ||
| 369 | |||
| 370 | wa.override_redirect = 1; | ||
| 371 | wa.background_pixmap = ParentRelative; | ||
| 372 | wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask; | ||
| 373 | |||
| 374 | mx = my = 0; | ||
| 375 | mw = DisplayWidth(dpy, screen); | ||
| 376 | mh = texth(&brush.font); | ||
| 377 | |||
| 378 | win = XCreateWindow(dpy, root, mx, my, mw, mh, 0, | ||
| 379 | DefaultDepth(dpy, screen), CopyFromParent, | ||
| 380 | DefaultVisual(dpy, screen), | ||
| 381 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); | ||
| 382 | XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm)); | ||
| 383 | XFlush(dpy); | ||
| 384 | |||
| 385 | /* pixmap */ | ||
| 386 | brush.gc = XCreateGC(dpy, root, 0, 0); | ||
| 387 | brush.drawable = XCreatePixmap(dpy, win, mw, mh, | ||
| 388 | DefaultDepth(dpy, screen)); | ||
| 389 | XFlush(dpy); | ||
| 390 | |||
| 391 | if(maxname) | ||
| 392 | cw = textw(&brush.font, maxname) + brush.font.height; | ||
| 393 | if(cw > mw / 3) | ||
| 394 | cw = mw / 3; | ||
| 395 | |||
| 396 | if(title) { | ||
| 397 | tw = textw(&brush.font, title) + brush.font.height; | ||
| 398 | if(tw > mw / 3) | ||
| 399 | tw = mw / 3; | ||
| 400 | } | ||
| 401 | |||
| 402 | cmdw = title ? tw : cw; | ||
| 403 | |||
| 404 | text[0] = 0; | ||
| 405 | update_items(text); | ||
| 406 | XMapRaised(dpy, win); | ||
| 407 | draw_menu(); | ||
| 408 | XFlush(dpy); | ||
| 409 | |||
| 410 | /* main event loop */ | ||
| 411 | while(!done && !XNextEvent(dpy, &ev)) { | ||
| 412 | switch (ev.type) { | ||
| 413 | case KeyPress: | ||
| 414 | kpress(&ev.xkey); | ||
| 415 | break; | ||
| 416 | case Expose: | ||
| 417 | if(ev.xexpose.count == 0) | ||
| 418 | draw_menu(); | ||
| 419 | break; | ||
| 420 | default: | ||
| 421 | break; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | XUngrabKeyboard(dpy, CurrentTime); | ||
| 426 | XFreePixmap(dpy, brush.drawable); | ||
| 427 | XFreeGC(dpy, brush.gc); | ||
| 428 | XDestroyWindow(dpy, win); | ||
| 429 | XCloseDisplay(dpy); | ||
| 430 | |||
| 431 | return ret; | ||
| 432 | } | ||
| @@ -11,7 +11,7 @@ | |||
| 11 | #include <sys/wait.h> | 11 | #include <sys/wait.h> |
| 12 | #include <unistd.h> | 12 | #include <unistd.h> |
| 13 | 13 | ||
| 14 | #include "util.h" | 14 | #include "wm.h" |
| 15 | 15 | ||
| 16 | void | 16 | void |
| 17 | error(char *errstr, ...) { | 17 | error(char *errstr, ...) { |
| @@ -60,20 +60,13 @@ erealloc(void *ptr, unsigned int size) | |||
| 60 | char * | 60 | char * |
| 61 | estrdup(const char *str) | 61 | estrdup(const char *str) |
| 62 | { | 62 | { |
| 63 | void *res = strdup(str); | 63 | char *res = strdup(str); |
| 64 | if(!res) | 64 | if(!res) |
| 65 | bad_malloc(strlen(str)); | 65 | bad_malloc(strlen(str)); |
| 66 | return res; | 66 | return res; |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | void | 69 | void |
| 70 | failed_assert(char *a, char *file, int line) | ||
| 71 | { | ||
| 72 | fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line); | ||
| 73 | abort(); | ||
| 74 | } | ||
| 75 | |||
| 76 | void | ||
| 77 | swap(void **p1, void **p2) | 70 | swap(void **p1, void **p2) |
| 78 | { | 71 | { |
| 79 | void *tmp = *p1; | 72 | void *tmp = *p1; |
| @@ -99,43 +92,3 @@ spawn(Display *dpy, char *argv[]) | |||
| 99 | } | 92 | } |
| 100 | wait(0); | 93 | wait(0); |
| 101 | } | 94 | } |
| 102 | |||
| 103 | void | ||
| 104 | pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[]) | ||
| 105 | { | ||
| 106 | unsigned int l, n; | ||
| 107 | int pfd[2]; | ||
| 108 | |||
| 109 | if(!argv || !argv[0]) | ||
| 110 | return; | ||
| 111 | |||
| 112 | if(pipe(pfd) == -1) { | ||
| 113 | perror("pipe"); | ||
| 114 | exit(1); | ||
| 115 | } | ||
| 116 | |||
| 117 | if(fork() == 0) { | ||
| 118 | if(dpy) | ||
| 119 | close(ConnectionNumber(dpy)); | ||
| 120 | setsid(); | ||
| 121 | dup2(pfd[1], STDOUT_FILENO); | ||
| 122 | close(pfd[0]); | ||
| 123 | close(pfd[1]); | ||
| 124 | execvp(argv[0], argv); | ||
| 125 | fprintf(stderr, "gridwm: execvp %s", argv[0]); | ||
| 126 | perror(" failed"); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | l = n = 0; | ||
| 130 | close(pfd[1]); | ||
| 131 | while(n < len) { | ||
| 132 | if((l = read(pfd[0], buf + n, len - n)) < 1) | ||
| 133 | break; | ||
| 134 | n += l; | ||
| 135 | } | ||
| 136 | while(l > n); | ||
| 137 | close(pfd[0]); | ||
| 138 | buf[n < len ? n : len - 1] = 0; | ||
| 139 | } | ||
| 140 | wait(0); | ||
| 141 | } | ||
| @@ -1,22 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> | ||
| 3 | * See LICENSE file for license details. | ||
| 4 | */ | ||
| 5 | #include <X11/Xlib.h> | ||
| 6 | |||
| 7 | extern void error(char *errstr, ...); | ||
| 8 | extern void *emallocz(unsigned int size); | ||
| 9 | extern void *emalloc(unsigned int size); | ||
| 10 | extern void *erealloc(void *ptr, unsigned int size); | ||
| 11 | extern char *estrdup(const char *str); | ||
| 12 | #define eassert(a) \ | ||
| 13 | do { \ | ||
| 14 | if(!(a)) \ | ||
| 15 | failed_assert(#a, __FILE__, __LINE__); \ | ||
| 16 | } while (0) | ||
| 17 | extern void failed_assert(char *a, char *file, int line); | ||
| 18 | extern void pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[]); | ||
| 19 | extern void spawn(Display *dpy, char *argv[]); | ||
| 20 | extern void swap(void **p1, void **p2); | ||
| 21 | extern unsigned int tokenize(char **result, unsigned int reslen, | ||
| 22 | char *str, char delim); | ||
| @@ -3,15 +3,10 @@ | |||
| 3 | * See LICENSE file for license details. | 3 | * See LICENSE file for license details. |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | #include <errno.h> | ||
| 7 | |||
| 8 | #include <stdarg.h> | 6 | #include <stdarg.h> |
| 9 | #include <stdio.h> | 7 | #include <stdio.h> |
| 10 | #include <stdlib.h> | 8 | #include <stdlib.h> |
| 11 | 9 | ||
| 12 | #include <sys/types.h> | ||
| 13 | #include <sys/time.h> | ||
| 14 | |||
| 15 | #include <X11/cursorfont.h> | 10 | #include <X11/cursorfont.h> |
| 16 | #include <X11/Xatom.h> | 11 | #include <X11/Xatom.h> |
| 17 | #include <X11/Xproto.h> | 12 | #include <X11/Xproto.h> |
| @@ -28,17 +23,6 @@ char *tags[TLast] = { | |||
| 28 | [Twork] = "work", | 23 | [Twork] = "work", |
| 29 | }; | 24 | }; |
| 30 | 25 | ||
| 31 | /* commands */ | ||
| 32 | static char *cmdwallpaper[] = { | ||
| 33 | "feh", "--bg-scale", "/home/garbeam/wallpaper/bg.jpg", NULL | ||
| 34 | }; | ||
| 35 | |||
| 36 | static char *cmdstatus[] = { | ||
| 37 | "sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`" | ||
| 38 | " `uptime | sed 's/.*://; s/,//g'`" | ||
| 39 | " `acpi | awk '{print $4}' | sed 's/,//'`", NULL | ||
| 40 | }; | ||
| 41 | |||
| 42 | /********** CUSTOMIZE **********/ | 26 | /********** CUSTOMIZE **********/ |
| 43 | 27 | ||
| 44 | /* X structs */ | 28 | /* X structs */ |
| @@ -51,7 +35,7 @@ Bool issel; | |||
| 51 | 35 | ||
| 52 | char stext[1024]; | 36 | char stext[1024]; |
| 53 | int tsel = Tdev; /* default tag */ | 37 | int tsel = Tdev; /* default tag */ |
| 54 | int screen, sx, sy, sw, sh, bx, by, bw, bh; | 38 | int screen, sx, sy, sw, sh, th; |
| 55 | 39 | ||
| 56 | Brush brush = {0}; | 40 | Brush brush = {0}; |
| 57 | Client *clients = NULL; | 41 | Client *clients = NULL; |
| @@ -209,11 +193,6 @@ main(int argc, char *argv[]) | |||
| 209 | unsigned int mask; | 193 | unsigned int mask; |
| 210 | Window w; | 194 | Window w; |
| 211 | XEvent ev; | 195 | XEvent ev; |
| 212 | fd_set fds; | ||
| 213 | struct timeval t, timeout = { | ||
| 214 | .tv_usec = 0, | ||
| 215 | .tv_sec = STATUSDELAY, | ||
| 216 | }; | ||
| 217 | 196 | ||
| 218 | /* command line args */ | 197 | /* command line args */ |
| 219 | for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { | 198 | for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { |
| @@ -245,7 +224,6 @@ main(int argc, char *argv[]) | |||
| 245 | if(other_wm_running) | 224 | if(other_wm_running) |
| 246 | error("gridwm: another window manager is already running\n"); | 225 | error("gridwm: another window manager is already running\n"); |
| 247 | 226 | ||
| 248 | spawn(dpy, cmdwallpaper); | ||
| 249 | sx = sy = 0; | 227 | sx = sy = 0; |
| 250 | sw = DisplayWidth(dpy, screen); | 228 | sw = DisplayWidth(dpy, screen); |
| 251 | sh = DisplayHeight(dpy, screen); | 229 | sh = DisplayHeight(dpy, screen); |
| @@ -275,25 +253,11 @@ main(int argc, char *argv[]) | |||
| 275 | loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); | 253 | loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); |
| 276 | loadfont(dpy, &brush.font, FONT); | 254 | loadfont(dpy, &brush.font, FONT); |
| 277 | 255 | ||
| 278 | wa.override_redirect = 1; | 256 | th = texth(&brush.font); |
| 279 | wa.background_pixmap = ParentRelative; | ||
| 280 | wa.event_mask = ExposureMask; | ||
| 281 | |||
| 282 | bx = by = 0; | ||
| 283 | bw = sw; | ||
| 284 | bh = texth(&brush.font); | ||
| 285 | barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), | ||
| 286 | CopyFromParent, DefaultVisual(dpy, screen), | ||
| 287 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); | ||
| 288 | XDefineCursor(dpy, barwin, cursor[CurNormal]); | ||
| 289 | XMapRaised(dpy, barwin); | ||
| 290 | 257 | ||
| 291 | brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); | 258 | brush.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen)); |
| 292 | brush.gc = XCreateGC(dpy, root, 0, 0); | 259 | brush.gc = XCreateGC(dpy, root, 0, 0); |
| 293 | 260 | ||
| 294 | pipe_spawn(stext, sizeof(stext), dpy, cmdstatus); | ||
| 295 | draw_bar(); | ||
| 296 | |||
| 297 | wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ | 261 | wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ |
| 298 | | LeaveWindowMask; | 262 | | LeaveWindowMask; |
| 299 | wa.cursor = cursor[CurNormal]; | 263 | wa.cursor = cursor[CurNormal]; |
| @@ -303,21 +267,9 @@ main(int argc, char *argv[]) | |||
| 303 | scan_wins(); | 267 | scan_wins(); |
| 304 | 268 | ||
| 305 | while(running) { | 269 | while(running) { |
| 306 | if(XPending(dpy) > 0) { | 270 | XNextEvent(dpy, &ev); |
| 307 | XNextEvent(dpy, &ev); | 271 | if(handler[ev.type]) |
| 308 | if(handler[ev.type]) | 272 | (handler[ev.type])(&ev); /* call handler */ |
| 309 | (handler[ev.type]) (&ev); /* call handler */ | ||
| 310 | continue; | ||
| 311 | } | ||
| 312 | FD_ZERO(&fds); | ||
| 313 | FD_SET(ConnectionNumber(dpy), &fds); | ||
| 314 | t = timeout; | ||
| 315 | if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0) | ||
| 316 | continue; | ||
| 317 | else if(errno != EINTR) { | ||
| 318 | pipe_spawn(stext, sizeof(stext), dpy, cmdstatus); | ||
| 319 | draw_bar(); | ||
| 320 | } | ||
| 321 | } | 273 | } |
| 322 | 274 | ||
| 323 | cleanup(); | 275 | cleanup(); |
| @@ -3,15 +3,25 @@ | |||
| 3 | * See LICENSE file for license details. | 3 | * See LICENSE file for license details. |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | #include "config.h" | 6 | #include <X11/Xlib.h> |
| 7 | #include "draw.h" | ||
| 8 | #include "util.h" | ||
| 9 | 7 | ||
| 10 | #include <X11/Xutil.h> | 8 | /********** CUSTOMIZE **********/ |
| 11 | 9 | ||
| 10 | #define FONT "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*" | ||
| 11 | #define BGCOLOR "#666699" | ||
| 12 | #define FGCOLOR "#ffffff" | ||
| 13 | #define BORDERCOLOR "#9999CC" | ||
| 14 | #define STATUSDELAY 10 /* seconds */ | ||
| 12 | #define WM_PROTOCOL_DELWIN 1 | 15 | #define WM_PROTOCOL_DELWIN 1 |
| 13 | 16 | ||
| 17 | /* tags */ | ||
| 18 | enum { Tscratch, Tdev, Tirc, Twww, Twork, TLast }; | ||
| 19 | |||
| 20 | /********** CUSTOMIZE **********/ | ||
| 21 | |||
| 22 | typedef struct Brush Brush; | ||
| 14 | typedef struct Client Client; | 23 | typedef struct Client Client; |
| 24 | typedef struct Fnt Fnt; | ||
| 15 | typedef struct Key Key; | 25 | typedef struct Key Key; |
| 16 | 26 | ||
| 17 | /* atoms */ | 27 | /* atoms */ |
| @@ -21,6 +31,24 @@ enum { NetSupported, NetWMName, NetLast }; | |||
| 21 | /* cursor */ | 31 | /* cursor */ |
| 22 | enum { CurNormal, CurResize, CurMove, CurInput, CurLast }; | 32 | enum { CurNormal, CurResize, CurMove, CurInput, CurLast }; |
| 23 | 33 | ||
| 34 | struct Fnt { | ||
| 35 | XFontStruct *xfont; | ||
| 36 | XFontSet set; | ||
| 37 | int ascent; | ||
| 38 | int descent; | ||
| 39 | int height; | ||
| 40 | }; | ||
| 41 | |||
| 42 | struct Brush { | ||
| 43 | GC gc; | ||
| 44 | Drawable drawable; | ||
| 45 | int x, y, w, h; | ||
| 46 | Fnt font; | ||
| 47 | unsigned long bg; | ||
| 48 | unsigned long fg; | ||
| 49 | unsigned long border; | ||
| 50 | }; | ||
| 51 | |||
| 24 | struct Client { | 52 | struct Client { |
| 25 | char name[256]; | 53 | char name[256]; |
| 26 | char *tags[TLast]; | 54 | char *tags[TLast]; |
| @@ -46,21 +74,27 @@ struct Key { | |||
| 46 | }; | 74 | }; |
| 47 | 75 | ||
| 48 | extern Display *dpy; | 76 | extern Display *dpy; |
| 49 | extern Window root, barwin; | 77 | extern Window root; |
| 50 | extern Atom wm_atom[WMLast], net_atom[NetLast]; | 78 | extern Atom wm_atom[WMLast], net_atom[NetLast]; |
| 51 | extern Cursor cursor[CurLast]; | 79 | extern Cursor cursor[CurLast]; |
| 52 | extern Bool running, issel; | 80 | extern Bool running, issel; |
| 53 | extern void (*handler[LASTEvent]) (XEvent *); | 81 | extern void (*handler[LASTEvent]) (XEvent *); |
| 54 | extern void (*arrange)(void *aux); | 82 | extern void (*arrange)(void *aux); |
| 55 | 83 | ||
| 56 | extern int tsel, screen, sx, sy, sw, sh, bx, by, bw, bh; | 84 | extern int tsel, screen, sx, sy, sw, sh, th; |
| 57 | extern char stext[1024], *tags[TLast]; | 85 | extern char stext[1024], *tags[TLast]; |
| 58 | 86 | ||
| 59 | extern Brush brush; | 87 | extern Brush brush; |
| 60 | extern Client *clients, *stack; | 88 | extern Client *clients, *stack; |
| 61 | 89 | ||
| 62 | /* bar.c */ | 90 | /* draw.c */ |
| 63 | extern void draw_bar(); | 91 | extern void draw(Display *dpy, Brush *b, Bool border, const char *text); |
| 92 | extern void loadcolors(Display *dpy, int screen, Brush *b, | ||
| 93 | const char *bg, const char *fg, const char *bo); | ||
| 94 | extern void loadfont(Display *dpy, Fnt *font, const char *fontstr); | ||
| 95 | extern unsigned int textnw(Fnt *font, char *text, unsigned int len); | ||
| 96 | extern unsigned int textw(Fnt *font, char *text); | ||
| 97 | extern unsigned int texth(Fnt *font); | ||
| 64 | 98 | ||
| 65 | /* client.c */ | 99 | /* client.c */ |
| 66 | extern void manage(Window w, XWindowAttributes *wa); | 100 | extern void manage(Window w, XWindowAttributes *wa); |
| @@ -72,9 +106,9 @@ extern void draw_client(Client *c); | |||
| 72 | extern void resize(Client *c); | 106 | extern void resize(Client *c); |
| 73 | extern void update_size(Client *c); | 107 | extern void update_size(Client *c); |
| 74 | extern Client *gettitle(Window w); | 108 | extern Client *gettitle(Window w); |
| 75 | extern void raise(Client *c); | 109 | extern void craise(Client *c); |
| 76 | extern void lower(Client *c); | 110 | extern void lower(Client *c); |
| 77 | extern void kill(void *aux); | 111 | extern void ckill(void *aux); |
| 78 | extern void sel(void *aux); | 112 | extern void sel(void *aux); |
| 79 | extern void max(void *aux); | 113 | extern void max(void *aux); |
| 80 | extern void floating(void *aux); | 114 | extern void floating(void *aux); |
| @@ -92,6 +126,15 @@ extern void keypress(XEvent *e); | |||
| 92 | extern void mresize(Client *c); | 126 | extern void mresize(Client *c); |
| 93 | extern void mmove(Client *c); | 127 | extern void mmove(Client *c); |
| 94 | 128 | ||
| 129 | /* util.c */ | ||
| 130 | extern void error(char *errstr, ...); | ||
| 131 | extern void *emallocz(unsigned int size); | ||
| 132 | extern void *emalloc(unsigned int size); | ||
| 133 | extern void *erealloc(void *ptr, unsigned int size); | ||
| 134 | extern char *estrdup(const char *str); | ||
| 135 | extern void spawn(Display *dpy, char *argv[]); | ||
| 136 | extern void swap(void **p1, void **p2); | ||
| 137 | |||
| 95 | /* wm.c */ | 138 | /* wm.c */ |
| 96 | extern int error_handler(Display *dpy, XErrorEvent *error); | 139 | extern int error_handler(Display *dpy, XErrorEvent *error); |
| 97 | extern void send_message(Window w, Atom a, long value); | 140 | extern void send_message(Window w, Atom a, long value); |
