added draw.h
595e7976
5 file(s) · +80 −71
| 6 | 6 | SRC = dinput.c dmenu.c draw.c |
|
| 7 | 7 | OBJ = ${SRC:.c=.o} |
|
| 8 | 8 | ||
| 9 | - | all: options dinput dmenu |
|
| 9 | + | all: options draw.o dinput dmenu |
|
| 10 | 10 | ||
| 11 | 11 | options: |
|
| 12 | 12 | @echo dmenu build options: |
|
| 18 | 18 | @echo CC $< |
|
| 19 | 19 | @${CC} -c ${CFLAGS} $< |
|
| 20 | 20 | ||
| 21 | - | ${OBJ}: config.h config.mk draw.c |
|
| 21 | + | ${OBJ}: config.h config.mk draw.h |
|
| 22 | 22 | ||
| 23 | 23 | config.h: |
|
| 24 | 24 | @echo creating $@ from config.def.h |
|
| 26 | 26 | ||
| 27 | 27 | .o: |
|
| 28 | 28 | @echo CC -o $@ |
|
| 29 | - | @${CC} -o $@ $< ${LDFLAGS} |
|
| 29 | + | @${CC} -o $@ $< draw.o ${LDFLAGS} |
|
| 30 | 30 | ||
| 31 | 31 | clean: |
|
| 32 | 32 | @echo cleaning |
|
| 25 | 25 | static void cleanup(void); |
|
| 26 | 26 | static void drawcursor(void); |
|
| 27 | 27 | static void drawinput(void); |
|
| 28 | - | static void eprint(const char *errstr, ...); |
|
| 29 | 28 | static Bool grabkeyboard(void); |
|
| 30 | 29 | static void kpress(XKeyEvent * e); |
|
| 31 | 30 | static void run(void); |
|
| 32 | 31 | static void setup(Bool topbar); |
|
| 33 | 32 | ||
| 34 | 33 | #include "config.h" |
|
| 34 | + | #include "draw.h" |
|
| 35 | 35 | ||
| 36 | 36 | /* variables */ |
|
| 37 | 37 | static char *prompt = NULL; |
|
| 38 | 38 | static char text[4096]; |
|
| 39 | 39 | static int promptw = 0; |
|
| 40 | 40 | static int ret = 0; |
|
| 41 | - | static int screen; |
|
| 42 | - | static unsigned int mw, mh; |
|
| 43 | 41 | static unsigned int cursor = 0; |
|
| 44 | 42 | static unsigned int numlockmask = 0; |
|
| 45 | 43 | static Bool running = True; |
|
| 46 | - | static Display *dpy; |
|
| 47 | - | static Window parent, win; |
|
| 48 | - | ||
| 49 | - | #include "draw.c" |
|
| 44 | + | static Window win; |
|
| 50 | 45 | ||
| 51 | 46 | void |
|
| 52 | 47 | cleanup(void) { |
|
| 53 | - | dccleanup(); |
|
| 48 | + | drawcleanup(); |
|
| 54 | 49 | XDestroyWindow(dpy, win); |
|
| 55 | 50 | XUngrabKeyboard(dpy, CurrentTime); |
|
| 56 | 51 | } |
|
| 84 | 79 | drawcursor(); |
|
| 85 | 80 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); |
|
| 86 | 81 | XFlush(dpy); |
|
| 87 | - | } |
|
| 88 | - | ||
| 89 | - | void |
|
| 90 | - | eprint(const char *errstr, ...) { |
|
| 91 | - | va_list ap; |
|
| 92 | - | ||
| 93 | - | va_start(ap, errstr); |
|
| 94 | - | vfprintf(stderr, errstr, ap); |
|
| 95 | - | va_end(ap); |
|
| 96 | - | exit(EXIT_FAILURE); |
|
| 97 | 82 | } |
|
| 98 | 83 | ||
| 99 | 84 | Bool |
|
| 318 | 303 | DefaultVisual(dpy, screen), |
|
| 319 | 304 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); |
|
| 320 | 305 | ||
| 321 | - | dcsetup(); |
|
| 306 | + | drawsetup(); |
|
| 322 | 307 | if(prompt) |
|
| 323 | 308 | promptw = MIN(textw(prompt), mw / 5); |
|
| 324 | 309 | cursor = strlen(text); |
|
| 46 | 46 | static void setup(Bool topbar); |
|
| 47 | 47 | ||
| 48 | 48 | #include "config.h" |
|
| 49 | + | #include "draw.h" |
|
| 49 | 50 | ||
| 50 | 51 | /* variables */ |
|
| 51 | 52 | static char *maxname = NULL; |
|
| 54 | 55 | static int cmdw = 0; |
|
| 55 | 56 | static int promptw = 0; |
|
| 56 | 57 | static int ret = 0; |
|
| 57 | - | static int screen; |
|
| 58 | - | static unsigned int mw, mh; |
|
| 59 | 58 | static unsigned int numlockmask = 0; |
|
| 60 | 59 | static Bool running = True; |
|
| 61 | - | static Display *dpy; |
|
| 62 | 60 | static Item *allitems = NULL; /* first of all items */ |
|
| 63 | 61 | static Item *item = NULL; /* first of pattern matching items */ |
|
| 64 | 62 | static Item *sel = NULL; |
|
| 65 | 63 | static Item *next = NULL; |
|
| 66 | 64 | static Item *prev = NULL; |
|
| 67 | 65 | static Item *curr = NULL; |
|
| 68 | - | static Window parent, win; |
|
| 66 | + | static Window win; |
|
| 69 | 67 | static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; |
|
| 70 | 68 | static char *(*fstrstr)(const char *, const char *) = strstr; |
|
| 71 | 69 | static unsigned int lines = 0; |
|
| 72 | 70 | static void (*calcoffsets)(void) = calcoffsetsh; |
|
| 73 | - | ||
| 74 | - | #include "draw.c" |
|
| 75 | 71 | ||
| 76 | 72 | void |
|
| 77 | 73 | appenditem(Item *i, Item **list, Item **last) { |
|
| 136 | 132 | ||
| 137 | 133 | void |
|
| 138 | 134 | cleanup(void) { |
|
| 139 | - | dccleanup(); |
|
| 135 | + | drawcleanup(); |
|
| 140 | 136 | XDestroyWindow(dpy, win); |
|
| 141 | 137 | XUngrabKeyboard(dpy, CurrentTime); |
|
| 142 | 138 | } |
|
| 200 | 196 | } |
|
| 201 | 197 | dc.h = mh - dc.y; |
|
| 202 | 198 | drawtext(NULL, dc.norm); |
|
| 203 | - | } |
|
| 204 | - | ||
| 205 | - | void |
|
| 206 | - | eprint(const char *errstr, ...) { |
|
| 207 | - | va_list ap; |
|
| 208 | - | ||
| 209 | - | va_start(ap, errstr); |
|
| 210 | - | vfprintf(stderr, errstr, ap); |
|
| 211 | - | va_end(ap); |
|
| 212 | - | exit(EXIT_FAILURE); |
|
| 213 | 199 | } |
|
| 214 | 200 | ||
| 215 | 201 | Bool |
|
| 529 | 515 | DefaultVisual(dpy, screen), |
|
| 530 | 516 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); |
|
| 531 | 517 | ||
| 532 | - | dcsetup(); |
|
| 518 | + | drawsetup(); |
|
| 533 | 519 | if(maxname) |
|
| 534 | 520 | cmdw = MIN(textw(maxname), mw / 3); |
|
| 535 | 521 | if(prompt) |
|
| 1 | 1 | /* See LICENSE file for copyright and license details. */ |
|
| 2 | + | #include <ctype.h> |
|
| 3 | + | #include <locale.h> |
|
| 4 | + | #include <stdarg.h> |
|
| 5 | + | #include <stdio.h> |
|
| 6 | + | #include <stdlib.h> |
|
| 7 | + | #include <string.h> |
|
| 8 | + | #include <strings.h> |
|
| 9 | + | #include <X11/Xlib.h> |
|
| 10 | + | #include "draw.h" |
|
| 2 | 11 | ||
| 3 | - | /* enums */ |
|
| 4 | - | enum { ColFG, ColBG, ColLast }; |
|
| 5 | - | ||
| 6 | - | /* typedefs */ |
|
| 7 | - | typedef struct { |
|
| 8 | - | int x, y, w, h; |
|
| 9 | - | unsigned long norm[ColLast]; |
|
| 10 | - | unsigned long sel[ColLast]; |
|
| 11 | - | Drawable drawable; |
|
| 12 | - | GC gc; |
|
| 13 | - | struct { |
|
| 14 | - | XFontStruct *xfont; |
|
| 15 | - | XFontSet set; |
|
| 16 | - | int ascent; |
|
| 17 | - | int descent; |
|
| 18 | - | int height; |
|
| 19 | - | } font; |
|
| 20 | - | } DC; /* draw context */ |
|
| 21 | - | ||
| 22 | - | /* forward declarations */ |
|
| 23 | - | static void dccleanup(void); |
|
| 24 | - | static void dcsetup(void); |
|
| 25 | - | static void drawtext(const char *text, unsigned long col[ColLast]); |
|
| 26 | - | static unsigned long getcolor(const char *colstr); |
|
| 27 | - | static void initfont(const char *fontstr); |
|
| 28 | - | static int textnw(const char *text, unsigned int len); |
|
| 29 | - | static int textw(const char *text); |
|
| 30 | - | ||
| 31 | - | static DC dc; |
|
| 12 | + | /* macros */ |
|
| 13 | + | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
|
| 14 | + | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
|
| 32 | 15 | ||
| 33 | 16 | void |
|
| 34 | - | dccleanup(void) { |
|
| 17 | + | drawcleanup(void) { |
|
| 35 | 18 | if(dc.font.set) |
|
| 36 | 19 | XFreeFontSet(dpy, dc.font.set); |
|
| 37 | 20 | else |
|
| 41 | 24 | } |
|
| 42 | 25 | ||
| 43 | 26 | void |
|
| 44 | - | dcsetup(void) { |
|
| 27 | + | drawsetup(void) { |
|
| 45 | 28 | /* style */ |
|
| 46 | 29 | dc.norm[ColBG] = getcolor(normbgcolor); |
|
| 47 | 30 | dc.norm[ColFG] = getcolor(normfgcolor); |
|
| 82 | 65 | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |
|
| 83 | 66 | else |
|
| 84 | 67 | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | void |
|
| 71 | + | eprint(const char *errstr, ...) { |
|
| 72 | + | va_list ap; |
|
| 73 | + | ||
| 74 | + | va_start(ap, errstr); |
|
| 75 | + | vfprintf(stderr, errstr, ap); |
|
| 76 | + | va_end(ap); |
|
| 77 | + | exit(EXIT_FAILURE); |
|
| 85 | 78 | } |
|
| 86 | 79 | ||
| 87 | 80 | unsigned long |
|
| 1 | + | /* See LICENSE file for copyright and license details. */ |
|
| 2 | + | ||
| 3 | + | /* enums */ |
|
| 4 | + | enum { ColFG, ColBG, ColLast }; |
|
| 5 | + | ||
| 6 | + | /* typedefs */ |
|
| 7 | + | typedef struct { |
|
| 8 | + | int x, y, w, h; |
|
| 9 | + | unsigned long norm[ColLast]; |
|
| 10 | + | unsigned long sel[ColLast]; |
|
| 11 | + | Drawable drawable; |
|
| 12 | + | GC gc; |
|
| 13 | + | struct { |
|
| 14 | + | XFontStruct *xfont; |
|
| 15 | + | XFontSet set; |
|
| 16 | + | int ascent; |
|
| 17 | + | int descent; |
|
| 18 | + | int height; |
|
| 19 | + | } font; |
|
| 20 | + | } DC; /* draw context */ |
|
| 21 | + | ||
| 22 | + | /* forward declarations */ |
|
| 23 | + | void drawcleanup(void); |
|
| 24 | + | void drawsetup(void); |
|
| 25 | + | void drawtext(const char *text, unsigned long col[ColLast]); |
|
| 26 | + | void eprint(const char *errstr, ...); |
|
| 27 | + | unsigned long getcolor(const char *colstr); |
|
| 28 | + | void initfont(const char *fontstr); |
|
| 29 | + | int textnw(const char *text, unsigned int len); |
|
| 30 | + | int textw(const char *text); |
|
| 31 | + | ||
| 32 | + | /* variables */ |
|
| 33 | + | Display *dpy; |
|
| 34 | + | DC dc; |
|
| 35 | + | int screen; |
|
| 36 | + | unsigned int mw, mh; |
|
| 37 | + | unsigned int spaceitem; |
|
| 38 | + | Window parent; |
|
| 39 | + | ||
| 40 | + | /* style */ |
|
| 41 | + | const char *font; |
|
| 42 | + | const char *normbgcolor; |
|
| 43 | + | const char *normfgcolor; |
|
| 44 | + | const char *selbgcolor; |
|
| 45 | + | const char *selfgcolor; |