diff options
Diffstat (limited to '')
| -rw-r--r-- | Makefile | 14 | ||||
| -rw-r--r-- | config.h | 8 | ||||
| -rw-r--r-- | draw.c | 41 | ||||
| -rw-r--r-- | draw.h | 31 | ||||
| -rw-r--r-- | gridmenu.c | 239 | ||||
| -rw-r--r-- | util.c | 59 | ||||
| -rw-r--r-- | util.h | 47 | ||||
| -rw-r--r-- | wm.c | 15 | ||||
| -rw-r--r-- | wm.h | 8 |
9 files changed, 256 insertions, 206 deletions
| @@ -5,10 +5,12 @@ include config.mk | |||
| 5 | 5 | ||
| 6 | WMSRC = wm.c draw.c util.c | 6 | WMSRC = wm.c draw.c util.c |
| 7 | WMOBJ = ${WMSRC:.c=.o} | 7 | WMOBJ = ${WMSRC:.c=.o} |
| 8 | MENSRC = gridmenu.c draw.c util.c | ||
| 9 | MENOBJ = ${MENSRC:.c=.o} | ||
| 8 | MAN = gridwm.1 | 10 | MAN = gridwm.1 |
| 9 | BIN = gridwm gridmenu | 11 | BIN = gridwm gridmenu |
| 10 | 12 | ||
| 11 | all: config gridwm | 13 | all: config gridwm gridmenu |
| 12 | @echo finished | 14 | @echo finished |
| 13 | 15 | ||
| 14 | config: | 16 | config: |
| @@ -22,18 +24,22 @@ config: | |||
| 22 | @echo CC $< | 24 | @echo CC $< |
| 23 | @${CC} -c ${CFLAGS} $< | 25 | @${CC} -c ${CFLAGS} $< |
| 24 | 26 | ||
| 25 | ${WMOBJ}: wm.h draw.h config.h | 27 | ${WMOBJ}: wm.h draw.h config.h util.h |
| 28 | |||
| 29 | gridmenu: ${MENOBJ} | ||
| 30 | @echo LD $@ | ||
| 31 | @${CC} -o $@ ${MENOBJ} ${LDFLAGS} | ||
| 26 | 32 | ||
| 27 | gridwm: ${WMOBJ} | 33 | gridwm: ${WMOBJ} |
| 28 | @echo LD $@ | 34 | @echo LD $@ |
| 29 | @${CC} -o $@ ${WMOBJ} ${LDFLAGS} | 35 | @${CC} -o $@ ${WMOBJ} ${LDFLAGS} |
| 30 | 36 | ||
| 31 | clean: | 37 | clean: |
| 32 | rm -f gridwm *.o | 38 | rm -f gridwm gridmenu *.o core |
| 33 | 39 | ||
| 34 | dist: clean | 40 | dist: clean |
| 35 | mkdir -p gridwm-${VERSION} | 41 | mkdir -p gridwm-${VERSION} |
| 36 | cp -R Makefile README LICENSE config.mk ${WMSRC} ${MAN} gridwm-${VERSION} | 42 | cp -R Makefile README LICENSE config.mk *.h *.c ${MAN} gridwm-${VERSION} |
| 37 | tar -cf gridwm-${VERSION}.tar gridwm-${VERSION} | 43 | tar -cf gridwm-${VERSION}.tar gridwm-${VERSION} |
| 38 | gzip gridwm-${VERSION}.tar | 44 | gzip gridwm-${VERSION}.tar |
| 39 | rm -rf gridwm-${VERSION} | 45 | rm -rf gridwm-${VERSION} |
| @@ -3,7 +3,7 @@ | |||
| 3 | * See LICENSE file for license details. | 3 | * See LICENSE file for license details. |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | #define FONT "fixed" | 6 | #define FONT "-*-terminus-medium-*-*-*-14-*-*-*-*-*-iso10646-*" |
| 7 | #define FGCOLOR "#000000" | 7 | #define FGCOLOR "#000000" |
| 8 | #define BGCOLOR "#ffaa00" | 8 | #define BGCOLOR "#ffaa00" |
| 9 | #define BOCOLOR "#000000" | 9 | #define BORDERCOLOR "#000000" |
| @@ -14,7 +14,7 @@ drawborder(Display *dpy, Brush *b) | |||
| 14 | { | 14 | { |
| 15 | XPoint points[5]; | 15 | XPoint points[5]; |
| 16 | XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter); | 16 | XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter); |
| 17 | XSetForeground(dpy, b->gc, b->color.border); | 17 | XSetForeground(dpy, b->gc, b->border); |
| 18 | points[0].x = b->rect.x; | 18 | points[0].x = b->rect.x; |
| 19 | points[0].y = b->rect.y; | 19 | points[0].y = b->rect.y; |
| 20 | points[1].x = b->rect.width - 1; | 20 | points[1].x = b->rect.width - 1; |
| @@ -29,54 +29,54 @@ drawborder(Display *dpy, Brush *b) | |||
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | void | 31 | void |
| 32 | draw(Display *dpy, Brush *b) | 32 | draw(Display *dpy, Brush *b, Bool border, const char *text) |
| 33 | { | 33 | { |
| 34 | unsigned int x, y, w, h, len; | 34 | unsigned int x, y, w, h, len; |
| 35 | static char buf[256]; | 35 | static char buf[256]; |
| 36 | XGCValues gcv; | 36 | XGCValues gcv; |
| 37 | 37 | ||
| 38 | XSetForeground(dpy, b->gc, b->color.bg); | 38 | XSetForeground(dpy, b->gc, b->bg); |
| 39 | XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1); | 39 | XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1); |
| 40 | 40 | ||
| 41 | if(b->border) | 41 | if(border) |
| 42 | drawborder(dpy, b); | 42 | drawborder(dpy, b); |
| 43 | 43 | ||
| 44 | if(!b->text) | 44 | if(!text) |
| 45 | return; | 45 | return; |
| 46 | 46 | ||
| 47 | len = strlen(b->text); | 47 | len = strlen(text); |
| 48 | if(len >= sizeof(buf)) | 48 | if(len >= sizeof(buf)) |
| 49 | len = sizeof(buf) - 1; | 49 | len = sizeof(buf) - 1; |
| 50 | memcpy(buf, b->text, len); | 50 | memcpy(buf, text, len); |
| 51 | buf[len] = 0; | 51 | buf[len] = 0; |
| 52 | 52 | ||
| 53 | h = b->font->ascent + b->font->descent; | 53 | h = b->font.ascent + b->font.descent; |
| 54 | y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font->ascent; | 54 | y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent; |
| 55 | x = b->rect.x + (h / 2); | 55 | x = b->rect.x + (h / 2); |
| 56 | 56 | ||
| 57 | /* shorten text if necessary */ | 57 | /* shorten text if necessary */ |
| 58 | while(len && (w = textwidth_l(b->font, buf, len)) > b->rect.width - h) | 58 | while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h) |
| 59 | buf[--len] = 0; | 59 | buf[--len] = 0; |
| 60 | 60 | ||
| 61 | if(w > b->rect.width) | 61 | if(w > b->rect.width) |
| 62 | return; /* too long */ | 62 | return; /* too long */ |
| 63 | 63 | ||
| 64 | gcv.foreground = b->color.fg; | 64 | gcv.foreground = b->fg; |
| 65 | gcv.background = b->color.bg; | 65 | gcv.background = b->bg; |
| 66 | if(b->font->set) { | 66 | if(b->font.set) { |
| 67 | XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv); | 67 | XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv); |
| 68 | XmbDrawImageString(dpy, b->drawable, b->font->set, b->gc, | 68 | XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc, |
| 69 | x, y, buf, len); | 69 | x, y, buf, len); |
| 70 | } | 70 | } |
| 71 | else { | 71 | else { |
| 72 | gcv.font = b->font->xfont->fid; | 72 | gcv.font = b->font.xfont->fid; |
| 73 | XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv); | 73 | XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv); |
| 74 | XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len); | 74 | XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len); |
| 75 | } | 75 | } |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | static unsigned long | 78 | static unsigned long |
| 79 | xloadcolor(Display *dpy, Colormap cmap, const char *colstr) | 79 | xloadcolors(Display *dpy, Colormap cmap, const char *colstr) |
| 80 | { | 80 | { |
| 81 | XColor color; | 81 | XColor color; |
| 82 | XAllocNamedColor(dpy, cmap, colstr, &color, &color); | 82 | XAllocNamedColor(dpy, cmap, colstr, &color, &color); |
| @@ -84,13 +84,13 @@ xloadcolor(Display *dpy, Colormap cmap, const char *colstr) | |||
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | void | 86 | void |
| 87 | loadcolor(Display *dpy, int screen, Color *c, | 87 | loadcolors(Display *dpy, int screen, Brush *b, |
| 88 | const char *bg, const char *fg, const char *border) | 88 | const char *bg, const char *fg, const char *border) |
| 89 | { | 89 | { |
| 90 | Colormap cmap = DefaultColormap(dpy, screen); | 90 | Colormap cmap = DefaultColormap(dpy, screen); |
| 91 | c->bg = xloadcolor(dpy, cmap, bg); | 91 | b->bg = xloadcolors(dpy, cmap, bg); |
| 92 | c->fg = xloadcolor(dpy, cmap, fg); | 92 | b->fg = xloadcolors(dpy, cmap, fg); |
| 93 | c->border = xloadcolor(dpy, cmap, border); | 93 | b->border = xloadcolors(dpy, cmap, border); |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | unsigned int | 96 | unsigned int |
| @@ -160,4 +160,5 @@ loadfont(Display *dpy, Fnt *font, const char *fontstr) | |||
| 160 | font->ascent = font->xfont->ascent; | 160 | font->ascent = font->xfont->ascent; |
| 161 | font->descent = font->xfont->descent; | 161 | font->descent = font->xfont->descent; |
| 162 | } | 162 | } |
| 163 | font->height = font->ascent + font->descent; | ||
| 163 | } | 164 | } |
| @@ -3,4 +3,33 @@ | |||
| 3 | * See LICENSE file for license details. | 3 | * See LICENSE file for license details. |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | extern void error(char *errstr, ...); | 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 | XRectangle rect; | ||
| 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 textwidth_l(Fnt *font, char *text, unsigned int len); | ||
| 35 | extern unsigned int textwidth(Fnt *font, char *text); | ||
| @@ -4,6 +4,10 @@ | |||
| 4 | * See LICENSE file for license details. | 4 | * See LICENSE file for license details. |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | #include "config.h" | ||
| 8 | #include "draw.h" | ||
| 9 | #include "util.h" | ||
| 10 | |||
| 7 | #include <ctype.h> | 11 | #include <ctype.h> |
| 8 | #include <stdlib.h> | 12 | #include <stdlib.h> |
| 9 | #include <stdio.h> | 13 | #include <stdio.h> |
| @@ -12,14 +16,10 @@ | |||
| 12 | #include <sys/wait.h> | 16 | #include <sys/wait.h> |
| 13 | #include <time.h> | 17 | #include <time.h> |
| 14 | #include <unistd.h> | 18 | #include <unistd.h> |
| 15 | #include <X11/Xlib.h> | ||
| 16 | #include <X11/cursorfont.h> | 19 | #include <X11/cursorfont.h> |
| 17 | #include <X11/Xutil.h> | 20 | #include <X11/Xutil.h> |
| 18 | #include <X11/keysym.h> | 21 | #include <X11/keysym.h> |
| 19 | 22 | ||
| 20 | #include <blitz.h> | ||
| 21 | #include <cext.h> | ||
| 22 | |||
| 23 | typedef struct Item Item; | 23 | typedef struct Item Item; |
| 24 | 24 | ||
| 25 | struct Item { | 25 | struct Item { |
| @@ -28,37 +28,40 @@ struct Item { | |||
| 28 | char *text; | 28 | char *text; |
| 29 | }; | 29 | }; |
| 30 | 30 | ||
| 31 | static char *title = nil; | 31 | static Display *dpy; |
| 32 | static Window root; | ||
| 33 | static Window win; | ||
| 34 | static XRectangle rect; | ||
| 32 | static Bool done = False; | 35 | static Bool done = False; |
| 33 | static int ret = 0; | 36 | |
| 37 | static Item *allitem = 0; /* first of all items */ | ||
| 38 | static Item *item = 0; /* first of pattern matching items */ | ||
| 39 | static Item *sel = 0; | ||
| 40 | static Item *nextoff = 0; | ||
| 41 | static Item *prevoff = 0; | ||
| 42 | static Item *curroff = 0; | ||
| 43 | |||
| 44 | static int screen; | ||
| 45 | static char *title = 0; | ||
| 34 | static char text[4096]; | 46 | static char text[4096]; |
| 35 | static BlitzColor selcolor; | 47 | static int ret = 0; |
| 36 | static BlitzColor normcolor; | ||
| 37 | static Window win; | ||
| 38 | static XRectangle mrect; | ||
| 39 | static Item *allitem = nil; /* first of all items */ | ||
| 40 | static Item *item = nil; /* first of pattern matching items */ | ||
| 41 | static Item *sel = nil; | ||
| 42 | static Item *nextoff = nil; | ||
| 43 | static Item *prevoff = nil; | ||
| 44 | static Item *curroff = nil; | ||
| 45 | static int nitem = 0; | 48 | static int nitem = 0; |
| 46 | static unsigned int cmdw = 0; | 49 | static unsigned int cmdw = 0; |
| 47 | static unsigned int twidth = 0; | 50 | static unsigned int twidth = 0; |
| 48 | static unsigned int cwidth = 0; | 51 | static unsigned int cwidth = 0; |
| 49 | static Blitz blz = {0}; | ||
| 50 | static BlitzBrush brush = {0}; | ||
| 51 | static const int seek = 30; /* 30px */ | 52 | static const int seek = 30; /* 30px */ |
| 52 | 53 | ||
| 54 | static Brush brush = {0}; | ||
| 55 | |||
| 53 | static void draw_menu(void); | 56 | static void draw_menu(void); |
| 54 | static void handle_kpress(XKeyEvent * e); | 57 | static void kpress(XKeyEvent * e); |
| 55 | 58 | ||
| 56 | static char version[] = "wmiimenu - " VERSION ", (C)opyright MMIV-MMVI Anselm R. Garbe\n"; | 59 | static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; |
| 57 | 60 | ||
| 58 | static void | 61 | static void |
| 59 | usage() | 62 | usage() |
| 60 | { | 63 | { |
| 61 | fprintf(stderr, "%s", "usage: wmiimenu [-v] [-t <title>]\n"); | 64 | fprintf(stderr, "%s", "usage: gridmenu [-v] [-t <title>]\n"); |
| 62 | exit(1); | 65 | exit(1); |
| 63 | } | 66 | } |
| 64 | 67 | ||
| @@ -71,21 +74,21 @@ update_offsets() | |||
| 71 | return; | 74 | return; |
| 72 | 75 | ||
| 73 | for(nextoff = curroff; nextoff; nextoff=nextoff->right) { | 76 | for(nextoff = curroff; nextoff; nextoff=nextoff->right) { |
| 74 | tw = blitz_textwidth(brush.font, nextoff->text); | 77 | tw = textwidth(&brush.font, nextoff->text); |
| 75 | if(tw > mrect.width / 3) | 78 | if(tw > rect.width / 3) |
| 76 | tw = mrect.width / 3; | 79 | tw = rect.width / 3; |
| 77 | w += tw + mrect.height; | 80 | w += tw + brush.font.height; |
| 78 | if(w > mrect.width) | 81 | if(w > rect.width) |
| 79 | break; | 82 | break; |
| 80 | } | 83 | } |
| 81 | 84 | ||
| 82 | w = cmdw + 2 * seek; | 85 | w = cmdw + 2 * seek; |
| 83 | for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) { | 86 | for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) { |
| 84 | tw = blitz_textwidth(brush.font, prevoff->left->text); | 87 | tw = textwidth(&brush.font, prevoff->left->text); |
| 85 | if(tw > mrect.width / 3) | 88 | if(tw > rect.width / 3) |
| 86 | tw = mrect.width / 3; | 89 | tw = rect.width / 3; |
| 87 | w += tw + mrect.height; | 90 | w += tw + brush.font.height; |
| 88 | if(w > mrect.width) | 91 | if(w > rect.width) |
| 89 | break; | 92 | break; |
| 90 | } | 93 | } |
| 91 | } | 94 | } |
| @@ -104,7 +107,7 @@ update_items(char *pattern) | |||
| 104 | else | 107 | else |
| 105 | cmdw = twidth; | 108 | cmdw = twidth; |
| 106 | 109 | ||
| 107 | item = j = nil; | 110 | item = j = 0; |
| 108 | nitem = 0; | 111 | nitem = 0; |
| 109 | 112 | ||
| 110 | for(i = allitem; i; i=i->next) | 113 | for(i = allitem; i; i=i->next) |
| @@ -114,7 +117,7 @@ update_items(char *pattern) | |||
| 114 | else | 117 | else |
| 115 | j->right = i; | 118 | j->right = i; |
| 116 | i->left = j; | 119 | i->left = j; |
| 117 | i->right = nil; | 120 | i->right = 0; |
| 118 | j = i; | 121 | j = i; |
| 119 | nitem++; | 122 | nitem++; |
| 120 | } | 123 | } |
| @@ -126,7 +129,7 @@ update_items(char *pattern) | |||
| 126 | else | 129 | else |
| 127 | j->right = i; | 130 | j->right = i; |
| 128 | i->left = j; | 131 | i->left = j; |
| 129 | i->right = nil; | 132 | i->right = 0; |
| 130 | j = i; | 133 | j = i; |
| 131 | nitem++; | 134 | nitem++; |
| 132 | } | 135 | } |
| @@ -141,72 +144,62 @@ static void | |||
| 141 | draw_menu() | 144 | draw_menu() |
| 142 | { | 145 | { |
| 143 | unsigned int offx = 0; | 146 | unsigned int offx = 0; |
| 144 | |||
| 145 | Item *i; | 147 | Item *i; |
| 146 | 148 | ||
| 147 | brush.align = WEST; | 149 | brush.rect = rect; |
| 148 | |||
| 149 | brush.rect = mrect; | ||
| 150 | brush.rect.x = 0; | 150 | brush.rect.x = 0; |
| 151 | brush.rect.y = 0; | 151 | brush.rect.y = 0; |
| 152 | brush.color = normcolor; | 152 | draw(dpy, &brush, False, 0); |
| 153 | brush.border = False; | ||
| 154 | blitz_draw_tile(&brush); | ||
| 155 | 153 | ||
| 156 | /* print command */ | 154 | /* print command */ |
| 157 | if(!title || text[0]) { | 155 | if(!title || text[0]) { |
| 158 | brush.color = normcolor; | ||
| 159 | cmdw = cwidth; | 156 | cmdw = cwidth; |
| 160 | if(cmdw && item) | 157 | if(cmdw && item) |
| 161 | brush.rect.width = cmdw; | 158 | brush.rect.width = cmdw; |
| 162 | blitz_draw_label(&brush, text); | 159 | draw(dpy, &brush, False, text); |
| 163 | } | 160 | } |
| 164 | else { | 161 | else { |
| 165 | cmdw = twidth; | 162 | cmdw = twidth; |
| 166 | brush.color = selcolor; | ||
| 167 | brush.rect.width = cmdw; | 163 | brush.rect.width = cmdw; |
| 168 | blitz_draw_label(&brush, title); | 164 | draw(dpy, &brush, False, title); |
| 169 | } | 165 | } |
| 170 | offx += brush.rect.width; | 166 | offx += brush.rect.width; |
| 171 | 167 | ||
| 172 | brush.align = CENTER; | ||
| 173 | if(curroff) { | 168 | if(curroff) { |
| 174 | brush.color = normcolor; | ||
| 175 | brush.rect.x = offx; | 169 | brush.rect.x = offx; |
| 176 | brush.rect.width = seek; | 170 | brush.rect.width = seek; |
| 177 | offx += brush.rect.width; | 171 | offx += brush.rect.width; |
| 178 | blitz_draw_label(&brush, (curroff && curroff->left) ? "<" : nil); | 172 | draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0); |
| 179 | 173 | ||
| 180 | /* determine maximum items */ | 174 | /* determine maximum items */ |
| 181 | for(i = curroff; i != nextoff; i=i->right) { | 175 | for(i = curroff; i != nextoff; i=i->right) { |
| 182 | brush.color = normcolor; | ||
| 183 | brush.border = False; | 176 | brush.border = False; |
| 184 | brush.rect.x = offx; | 177 | brush.rect.x = offx; |
| 185 | brush.rect.width = blitz_textwidth(brush.font, i->text); | 178 | brush.rect.width = textwidth(&brush.font, i->text); |
| 186 | if(brush.rect.width > mrect.width / 3) | 179 | if(brush.rect.width > rect.width / 3) |
| 187 | brush.rect.width = mrect.width / 3; | 180 | brush.rect.width = rect.width / 3; |
| 188 | brush.rect.width += mrect.height; | 181 | brush.rect.width += brush.font.height; |
| 189 | if(sel == i) { | 182 | if(sel == i) { |
| 190 | brush.color = selcolor; | 183 | swap((void **)&brush.fg, (void **)&brush.bg); |
| 191 | brush.border = True; | 184 | draw(dpy, &brush, True, i->text); |
| 185 | swap((void **)&brush.fg, (void **)&brush.bg); | ||
| 192 | } | 186 | } |
| 193 | blitz_draw_label(&brush, i->text); | 187 | else |
| 188 | draw(dpy, &brush, False, i->text); | ||
| 194 | offx += brush.rect.width; | 189 | offx += brush.rect.width; |
| 195 | } | 190 | } |
| 196 | 191 | ||
| 197 | brush.color = normcolor; | 192 | brush.rect.x = rect.width - seek; |
| 198 | brush.border = False; | ||
| 199 | brush.rect.x = mrect.width - seek; | ||
| 200 | brush.rect.width = seek; | 193 | brush.rect.width = seek; |
| 201 | blitz_draw_label(&brush, nextoff ? ">" : nil); | 194 | draw(dpy, &brush, False, nextoff ? ">" : 0); |
| 202 | } | 195 | } |
| 203 | XCopyArea(blz.dpy, brush.drawable, win, brush.gc, 0, 0, mrect.width, | 196 | XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, rect.width, |
| 204 | mrect.height, 0, 0); | 197 | rect.height, 0, 0); |
| 205 | XSync(blz.dpy, False); | 198 | XFlush(dpy); |
| 206 | } | 199 | } |
| 207 | 200 | ||
| 208 | static void | 201 | static void |
| 209 | handle_kpress(XKeyEvent * e) | 202 | kpress(XKeyEvent * e) |
| 210 | { | 203 | { |
| 211 | KeySym ksym; | 204 | KeySym ksym; |
| 212 | char buf[32]; | 205 | char buf[32]; |
| @@ -272,7 +265,7 @@ handle_kpress(XKeyEvent * e) | |||
| 272 | case XK_Tab: | 265 | case XK_Tab: |
| 273 | if(!sel) | 266 | if(!sel) |
| 274 | return; | 267 | return; |
| 275 | cext_strlcpy(text, sel->text, sizeof(text)); | 268 | strncpy(text, sel->text, sizeof(text)); |
| 276 | update_items(text); | 269 | update_items(text); |
| 277 | break; | 270 | break; |
| 278 | case XK_Right: | 271 | case XK_Right: |
| @@ -314,9 +307,9 @@ handle_kpress(XKeyEvent * e) | |||
| 314 | if((num == 1) && !iscntrl((int) buf[0])) { | 307 | if((num == 1) && !iscntrl((int) buf[0])) { |
| 315 | buf[num] = 0; | 308 | buf[num] = 0; |
| 316 | if(len > 0) | 309 | if(len > 0) |
| 317 | cext_strlcat(text, buf, sizeof(text)); | 310 | strncat(text, buf, sizeof(text)); |
| 318 | else | 311 | else |
| 319 | cext_strlcpy(text, buf, sizeof(text)); | 312 | strncpy(text, buf, sizeof(text)); |
| 320 | update_items(text); | 313 | update_items(text); |
| 321 | } | 314 | } |
| 322 | } | 315 | } |
| @@ -326,24 +319,24 @@ handle_kpress(XKeyEvent * e) | |||
| 326 | static char * | 319 | static char * |
| 327 | read_allitems() | 320 | read_allitems() |
| 328 | { | 321 | { |
| 329 | static char *maxname = nil; | 322 | static char *maxname = 0; |
| 330 | char *p, buf[1024]; | 323 | char *p, buf[1024]; |
| 331 | unsigned int len = 0, max = 0; | 324 | unsigned int len = 0, max = 0; |
| 332 | Item *i, *new; | 325 | Item *i, *new; |
| 333 | 326 | ||
| 334 | i = nil; | 327 | i = 0; |
| 335 | while(fgets(buf, sizeof(buf), stdin)) { | 328 | while(fgets(buf, sizeof(buf), stdin)) { |
| 336 | len = strlen(buf); | 329 | len = strlen(buf); |
| 337 | if (buf[len - 1] == '\n') | 330 | if (buf[len - 1] == '\n') |
| 338 | buf[len - 1] = 0; | 331 | buf[len - 1] = 0; |
| 339 | p = cext_estrdup(buf); | 332 | p = estrdup(buf); |
| 340 | if(max < len) { | 333 | if(max < len) { |
| 341 | maxname = p; | 334 | maxname = p; |
| 342 | max = len; | 335 | max = len; |
| 343 | } | 336 | } |
| 344 | 337 | ||
| 345 | new = cext_emalloc(sizeof(Item)); | 338 | new = emalloc(sizeof(Item)); |
| 346 | new->next = new->left = new->right = nil; | 339 | new->next = new->left = new->right = 0; |
| 347 | new->text = p; | 340 | new->text = p; |
| 348 | if(!i) | 341 | if(!i) |
| 349 | allitem = new; | 342 | allitem = new; |
| @@ -360,10 +353,7 @@ main(int argc, char *argv[]) | |||
| 360 | { | 353 | { |
| 361 | int i; | 354 | int i; |
| 362 | XSetWindowAttributes wa; | 355 | XSetWindowAttributes wa; |
| 363 | char *maxname, *p; | 356 | char *maxname; |
| 364 | BlitzFont font = {0}; | ||
| 365 | GC gc; | ||
| 366 | Drawable pmap; | ||
| 367 | XEvent ev; | 357 | XEvent ev; |
| 368 | 358 | ||
| 369 | /* command line args */ | 359 | /* command line args */ |
| @@ -388,93 +378,70 @@ main(int argc, char *argv[]) | |||
| 388 | usage(); | 378 | usage(); |
| 389 | } | 379 | } |
| 390 | 380 | ||
| 391 | blz.dpy = XOpenDisplay(0); | 381 | dpy = XOpenDisplay(0); |
| 392 | if(!blz.dpy) { | 382 | if(!dpy) |
| 393 | fprintf(stderr, "%s", "wmiimenu: cannot open dpy\n"); | 383 | error("gridmenu: cannot open dpy\n"); |
| 394 | exit(1); | 384 | screen = DefaultScreen(dpy); |
| 395 | } | 385 | root = RootWindow(dpy, screen); |
| 396 | blz.screen = DefaultScreen(blz.dpy); | ||
| 397 | blz.root = RootWindow(blz.dpy, blz.screen); | ||
| 398 | 386 | ||
| 399 | maxname = read_allitems(); | 387 | maxname = read_allitems(); |
| 400 | 388 | ||
| 401 | /* grab as early as possible, but after reading all items!!! */ | 389 | /* grab as early as possible, but after reading all items!!! */ |
| 402 | while(XGrabKeyboard | 390 | while(XGrabKeyboard(dpy, root, True, GrabModeAsync, |
| 403 | (blz.dpy, blz.root, True, GrabModeAsync, | ||
| 404 | GrabModeAsync, CurrentTime) != GrabSuccess) | 391 | GrabModeAsync, CurrentTime) != GrabSuccess) |
| 405 | usleep(1000); | 392 | usleep(1000); |
| 406 | 393 | ||
| 407 | font.fontstr = getenv("WMII_FONT"); | 394 | /* style */ |
| 408 | if (!font.fontstr) | 395 | loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); |
| 409 | font.fontstr = cext_estrdup(BLITZ_FONT); | 396 | loadfont(dpy, &brush.font, FONT); |
| 410 | blitz_loadfont(&blz, &font); | ||
| 411 | |||
| 412 | if((p = getenv("WMII_NORMCOLORS"))) | ||
| 413 | cext_strlcpy(normcolor.colstr, p, sizeof(normcolor.colstr)); | ||
| 414 | if(strlen(normcolor.colstr) != 23) | ||
| 415 | cext_strlcpy(normcolor.colstr, BLITZ_NORMCOLORS, sizeof(normcolor.colstr)); | ||
| 416 | blitz_loadcolor(&blz, &normcolor); | ||
| 417 | |||
| 418 | if((p = getenv("WMII_SELCOLORS"))) | ||
| 419 | cext_strlcpy(selcolor.colstr, p, sizeof(selcolor.colstr)); | ||
| 420 | if(strlen(selcolor.colstr) != 23) | ||
| 421 | cext_strlcpy(selcolor.colstr, BLITZ_SELCOLORS, sizeof(selcolor.colstr)); | ||
| 422 | blitz_loadcolor(&blz, &selcolor); | ||
| 423 | 397 | ||
| 424 | wa.override_redirect = 1; | 398 | wa.override_redirect = 1; |
| 425 | wa.background_pixmap = ParentRelative; | 399 | wa.background_pixmap = ParentRelative; |
| 426 | wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | 400 | wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask |
| 427 | | SubstructureRedirectMask | SubstructureNotifyMask; | 401 | | SubstructureRedirectMask | SubstructureNotifyMask; |
| 428 | 402 | ||
| 429 | mrect.width = DisplayWidth(blz.dpy, blz.screen); | 403 | rect.width = DisplayWidth(dpy, screen); |
| 430 | mrect.height = font.ascent + font.descent + 4; | 404 | rect.height = brush.font.height + 4; |
| 431 | mrect.y = DisplayHeight(blz.dpy, blz.screen) - mrect.height; | 405 | rect.y = DisplayHeight(dpy, screen) - rect.height; |
| 432 | mrect.x = 0; | 406 | rect.x = 0; |
| 433 | 407 | ||
| 434 | win = XCreateWindow(blz.dpy, blz.root, mrect.x, mrect.y, | 408 | win = XCreateWindow(dpy, root, rect.x, rect.y, |
| 435 | mrect.width, mrect.height, 0, DefaultDepth(blz.dpy, blz.screen), | 409 | rect.width, rect.height, 0, DefaultDepth(dpy, screen), |
| 436 | CopyFromParent, DefaultVisual(blz.dpy, blz.screen), | 410 | CopyFromParent, DefaultVisual(dpy, screen), |
| 437 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); | 411 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); |
| 438 | XDefineCursor(blz.dpy, win, XCreateFontCursor(blz.dpy, XC_xterm)); | 412 | XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm)); |
| 439 | XSync(blz.dpy, False); | 413 | XFlush(dpy); |
| 440 | 414 | ||
| 441 | /* pixmap */ | 415 | /* pixmap */ |
| 442 | gc = XCreateGC(blz.dpy, win, 0, 0); | 416 | brush.gc = XCreateGC(dpy, win, 0, 0); |
| 443 | pmap = XCreatePixmap(blz.dpy, win, mrect.width, mrect.height, | 417 | brush.drawable = XCreatePixmap(dpy, win, rect.width, rect.height, |
| 444 | DefaultDepth(blz.dpy, blz.screen)); | 418 | DefaultDepth(dpy, screen)); |
| 445 | 419 | XFlush(dpy); | |
| 446 | XSync(blz.dpy, False); | ||
| 447 | |||
| 448 | brush.blitz = &blz; | ||
| 449 | brush.color = normcolor; | ||
| 450 | brush.drawable = pmap; | ||
| 451 | brush.gc = gc; | ||
| 452 | brush.font = &font; | ||
| 453 | 420 | ||
| 454 | if(maxname) | 421 | if(maxname) |
| 455 | cwidth = blitz_textwidth(brush.font, maxname) + mrect.height; | 422 | cwidth = textwidth(&brush.font, maxname) + brush.font.height; |
| 456 | if(cwidth > mrect.width / 3) | 423 | if(cwidth > rect.width / 3) |
| 457 | cwidth = mrect.width / 3; | 424 | cwidth = rect.width / 3; |
| 458 | 425 | ||
| 459 | if(title) { | 426 | if(title) { |
| 460 | twidth = blitz_textwidth(brush.font, title) + mrect.height; | 427 | twidth = textwidth(&brush.font, title) + brush.font.height; |
| 461 | if(twidth > mrect.width / 3) | 428 | if(twidth > rect.width / 3) |
| 462 | twidth = mrect.width / 3; | 429 | twidth = rect.width / 3; |
| 463 | } | 430 | } |
| 464 | 431 | ||
| 465 | cmdw = title ? twidth : cwidth; | 432 | cmdw = title ? twidth : cwidth; |
| 466 | 433 | ||
| 467 | text[0] = 0; | 434 | text[0] = 0; |
| 468 | update_items(text); | 435 | update_items(text); |
| 469 | XMapRaised(blz.dpy, win); | 436 | XMapRaised(dpy, win); |
| 470 | draw_menu(); | 437 | draw_menu(); |
| 471 | XSync(blz.dpy, False); | 438 | XFlush(dpy); |
| 472 | 439 | ||
| 473 | /* main event loop */ | 440 | /* main event loop */ |
| 474 | while(!XNextEvent(blz.dpy, &ev)) { | 441 | while(!XNextEvent(dpy, &ev)) { |
| 475 | switch (ev.type) { | 442 | switch (ev.type) { |
| 476 | case KeyPress: | 443 | case KeyPress: |
| 477 | handle_kpress(&ev.xkey); | 444 | kpress(&ev.xkey); |
| 478 | break; | 445 | break; |
| 479 | case Expose: | 446 | case Expose: |
| 480 | if(ev.xexpose.count == 0) { | 447 | if(ev.xexpose.count == 0) { |
| @@ -488,11 +455,11 @@ main(int argc, char *argv[]) | |||
| 488 | break; | 455 | break; |
| 489 | } | 456 | } |
| 490 | 457 | ||
| 491 | XUngrabKeyboard(blz.dpy, CurrentTime); | 458 | XUngrabKeyboard(dpy, CurrentTime); |
| 492 | XFreePixmap(blz.dpy, pmap); | 459 | XFreePixmap(dpy, brush.drawable); |
| 493 | XFreeGC(blz.dpy, gc); | 460 | XFreeGC(dpy, brush.gc); |
| 494 | XDestroyWindow(blz.dpy, win); | 461 | XDestroyWindow(dpy, win); |
| 495 | XCloseDisplay(blz.dpy); | 462 | XCloseDisplay(dpy); |
| 496 | 463 | ||
| 497 | return ret; | 464 | return ret; |
| 498 | } | 465 | } |
| @@ -6,6 +6,7 @@ | |||
| 6 | #include <stdarg.h> | 6 | #include <stdarg.h> |
| 7 | #include <stdio.h> | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
| 9 | #include <string.h> | ||
| 9 | 10 | ||
| 10 | void | 11 | void |
| 11 | error(char *errstr, ...) { | 12 | error(char *errstr, ...) { |
| @@ -16,3 +17,61 @@ error(char *errstr, ...) { | |||
| 16 | exit(1); | 17 | exit(1); |
| 17 | } | 18 | } |
| 18 | 19 | ||
| 20 | static void | ||
| 21 | bad_malloc(unsigned int size) | ||
| 22 | { | ||
| 23 | fprintf(stderr, "fatal: could not malloc() %d bytes\n", | ||
| 24 | (int) size); | ||
| 25 | exit(1); | ||
| 26 | } | ||
| 27 | |||
| 28 | void * | ||
| 29 | emallocz(unsigned int size) | ||
| 30 | { | ||
| 31 | void *res = calloc(1, size); | ||
| 32 | if(!res) | ||
| 33 | bad_malloc(size); | ||
| 34 | return res; | ||
| 35 | } | ||
| 36 | |||
| 37 | void * | ||
| 38 | emalloc(unsigned int size) | ||
| 39 | { | ||
| 40 | void *res = malloc(size); | ||
| 41 | if(!res) | ||
| 42 | bad_malloc(size); | ||
| 43 | return res; | ||
| 44 | } | ||
| 45 | |||
| 46 | void * | ||
| 47 | erealloc(void *ptr, unsigned int size) | ||
| 48 | { | ||
| 49 | void *res = realloc(ptr, size); | ||
| 50 | if(!res) | ||
| 51 | bad_malloc(size); | ||
| 52 | return res; | ||
| 53 | } | ||
| 54 | |||
| 55 | char * | ||
| 56 | estrdup(const char *str) | ||
| 57 | { | ||
| 58 | void *res = strdup(str); | ||
| 59 | if(!res) | ||
| 60 | bad_malloc(strlen(str)); | ||
| 61 | return res; | ||
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | failed_assert(char *a, char *file, int line) | ||
| 66 | { | ||
| 67 | fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line); | ||
| 68 | abort(); | ||
| 69 | } | ||
| 70 | |||
| 71 | void | ||
| 72 | swap(void **p1, void **p2) | ||
| 73 | { | ||
| 74 | void *tmp = *p1; | ||
| 75 | *p1 = *p2; | ||
| 76 | *p2 = tmp; | ||
| 77 | } | ||
| @@ -3,39 +3,14 @@ | |||
| 3 | * See LICENSE file for license details. | 3 | * See LICENSE file for license details. |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | #include <X11/Xlib.h> | 6 | extern void error(char *errstr, ...); |
| 7 | #include <X11/Xlocale.h> | 7 | extern void *emallocz(unsigned int size); |
| 8 | 8 | extern void *emalloc(unsigned int size); | |
| 9 | typedef struct Brush Brush; | 9 | extern void *erealloc(void *ptr, unsigned int size); |
| 10 | typedef struct Color Color; | 10 | extern char *estrdup(const char *str); |
| 11 | typedef struct Fnt Fnt; | 11 | #define eassert(a) do { \ |
| 12 | 12 | if(!(a)) \ | |
| 13 | struct Color { | 13 | failed_assert(#a, __FILE__, __LINE__); \ |
| 14 | unsigned long bg; | 14 | } while (0) |
| 15 | unsigned long fg; | 15 | void failed_assert(char *a, char *file, int line); |
| 16 | unsigned long border; | 16 | void swap(void **p1, void **p2); |
| 17 | }; | ||
| 18 | |||
| 19 | struct Fnt { | ||
| 20 | XFontStruct *xfont; | ||
| 21 | XFontSet set; | ||
| 22 | int ascent; | ||
| 23 | int descent; | ||
| 24 | }; | ||
| 25 | |||
| 26 | struct Brush { | ||
| 27 | GC gc; | ||
| 28 | Drawable drawable; | ||
| 29 | XRectangle rect; | ||
| 30 | Bool border; | ||
| 31 | Fnt *font; | ||
| 32 | Color color; | ||
| 33 | const char *text; | ||
| 34 | }; | ||
| 35 | |||
| 36 | extern void draw(Display *dpy, Brush *b); | ||
| 37 | extern void loadcolor(Display *dpy, int screen, Color *c, | ||
| 38 | const char *bg, const char *fg, const char *bo); | ||
| 39 | extern unsigned int textwidth_l(Fnt *font, char *text, unsigned int len); | ||
| 40 | extern unsigned int textwidth(Fnt *font, char *text); | ||
| 41 | extern void loadfont(Display *dpy, Fnt *font, const char *fontstr); | ||
| @@ -13,15 +13,20 @@ | |||
| 13 | 13 | ||
| 14 | #include "wm.h" | 14 | #include "wm.h" |
| 15 | 15 | ||
| 16 | /* X structs */ | ||
| 16 | Display *dpy; | 17 | Display *dpy; |
| 17 | Window root; | 18 | Window root; |
| 18 | XRectangle rect; | 19 | XRectangle rect; |
| 19 | int screen, sel_screen; | 20 | Pixmap pmap; |
| 20 | Atom wm_atom[WMLast]; | 21 | Atom wm_atom[WMLast]; |
| 21 | Atom net_atom[NetLast]; | 22 | Atom net_atom[NetLast]; |
| 22 | Cursor cursor[CurLast]; | 23 | Cursor cursor[CurLast]; |
| 24 | |||
| 25 | int screen, sel_screen; | ||
| 23 | unsigned int kmask, numlock_mask; | 26 | unsigned int kmask, numlock_mask; |
| 24 | Pixmap pmap; | 27 | |
| 28 | /* draw structs */ | ||
| 29 | Brush brush = {0}; | ||
| 25 | 30 | ||
| 26 | enum { WM_PROTOCOL_DELWIN = 1 }; | 31 | enum { WM_PROTOCOL_DELWIN = 1 }; |
| 27 | 32 | ||
| @@ -208,7 +213,7 @@ main(int argc, char *argv[]) | |||
| 208 | XSetErrorHandler(startup_error_handler); | 213 | XSetErrorHandler(startup_error_handler); |
| 209 | /* this causes an error if some other WM is running */ | 214 | /* this causes an error if some other WM is running */ |
| 210 | XSelectInput(dpy, root, SubstructureRedirectMask); | 215 | XSelectInput(dpy, root, SubstructureRedirectMask); |
| 211 | XSync(dpy, False); | 216 | XFlush(dpy); |
| 212 | 217 | ||
| 213 | if(other_wm_running) | 218 | if(other_wm_running) |
| 214 | error("gridwm: another window manager is already running\n"); | 219 | error("gridwm: another window manager is already running\n"); |
| @@ -246,6 +251,10 @@ main(int argc, char *argv[]) | |||
| 246 | wa.cursor = cursor[CurNormal]; | 251 | wa.cursor = cursor[CurNormal]; |
| 247 | XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); | 252 | XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); |
| 248 | 253 | ||
| 254 | /* style */ | ||
| 255 | loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); | ||
| 256 | loadfont(dpy, &brush.font, FONT); | ||
| 257 | |||
| 249 | scan_wins(); | 258 | scan_wins(); |
| 250 | 259 | ||
| 251 | cleanup(); | 260 | cleanup(); |
| @@ -3,6 +3,7 @@ | |||
| 3 | * See LICENSE file for license details. | 3 | * See LICENSE file for license details. |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | #include "config.h" | ||
| 6 | #include "draw.h" | 7 | #include "draw.h" |
| 7 | #include "util.h" | 8 | #include "util.h" |
| 8 | 9 | ||
| @@ -48,11 +49,14 @@ struct Tag { | |||
| 48 | extern Display *dpy; | 49 | extern Display *dpy; |
| 49 | extern Window root; | 50 | extern Window root; |
| 50 | extern XRectangle rect; | 51 | extern XRectangle rect; |
| 51 | extern int screen, sel_screen; | ||
| 52 | extern unsigned int kmask, numlock_mask; | ||
| 53 | extern Atom wm_atom[WMLast]; | 52 | extern Atom wm_atom[WMLast]; |
| 54 | extern Atom net_atom[NetLast]; | 53 | extern Atom net_atom[NetLast]; |
| 55 | extern Cursor cursor[CurLast]; | 54 | extern Cursor cursor[CurLast]; |
| 56 | extern Pixmap pmap; | 55 | extern Pixmap pmap; |
| 57 | 56 | ||
| 57 | extern int screen, sel_screen; | ||
| 58 | extern unsigned int kmask, numlock_mask; | ||
| 59 | |||
| 60 | extern Brush brush; | ||
| 61 | |||
| 58 | /* wm.c */ | 62 | /* wm.c */ |
