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