diff options
-rw-r--r-- | Makefile | 16 | ||||
-rw-r--r-- | config.mk | 4 | ||||
-rw-r--r-- | dinput.c | 1 | ||||
-rw-r--r-- | draw.c | 136 | ||||
-rw-r--r-- | draw/Makefile | 26 | ||||
-rw-r--r-- | draw/cleanupdraw.c | 13 | ||||
-rw-r--r-- | draw/draw.h (renamed from draw.h) | 3 | ||||
-rw-r--r-- | draw/drawtext.c | 34 | ||||
-rw-r--r-- | draw/eprint.c | 18 | ||||
-rw-r--r-- | draw/getcolor.c | 13 | ||||
-rw-r--r-- | draw/initfont.c | 36 | ||||
-rw-r--r-- | draw/setupdraw.c | 16 | ||||
-rw-r--r-- | draw/textnw.c | 14 | ||||
-rw-r--r-- | draw/textw.c | 9 |
14 files changed, 191 insertions, 148 deletions
@@ -3,7 +3,7 @@ | |||
3 | 3 | ||
4 | include config.mk | 4 | include config.mk |
5 | 5 | ||
6 | SRC = dinput.c dmenu.c draw.c | 6 | SRC = dinput.c dmenu.c |
7 | OBJ = ${SRC:.c=.o} | 7 | OBJ = ${SRC:.c=.o} |
8 | 8 | ||
9 | all: options dinput dmenu | 9 | all: options dinput dmenu |
@@ -18,28 +18,28 @@ options: | |||
18 | @echo CC $< | 18 | @echo CC $< |
19 | @${CC} -c ${CFLAGS} $< | 19 | @${CC} -c ${CFLAGS} $< |
20 | 20 | ||
21 | ${OBJ}: config.h config.mk draw.h | 21 | ${OBJ}: config.h config.mk draw/libdraw.a |
22 | 22 | ||
23 | config.h: | 23 | config.h: |
24 | @echo creating $@ from config.def.h | 24 | @echo creating $@ from config.def.h |
25 | @cp config.def.h $@ | 25 | @cp config.def.h $@ |
26 | 26 | ||
27 | dinput: dinput.o draw.o | 27 | .o: |
28 | @echo CC -o $@ | 28 | @echo CC -o $@ |
29 | @${CC} -o $@ $+ ${LDFLAGS} | 29 | @${CC} -o $@ $< ${LDFLAGS} |
30 | 30 | ||
31 | dmenu: dmenu.o draw.o | 31 | draw/libdraw.a: |
32 | @echo CC -o $@ | 32 | @cd draw && make |
33 | @${CC} -o $@ $+ ${LDFLAGS} | ||
34 | 33 | ||
35 | clean: | 34 | clean: |
36 | @echo cleaning | 35 | @echo cleaning |
37 | @rm -f dinput dmenu ${OBJ} dmenu-${VERSION}.tar.gz | 36 | @rm -f dinput dmenu ${OBJ} dmenu-${VERSION}.tar.gz |
37 | @cd draw && make clean | ||
38 | 38 | ||
39 | dist: clean | 39 | dist: clean |
40 | @echo creating dist tarball | 40 | @echo creating dist tarball |
41 | @mkdir -p dmenu-${VERSION} | 41 | @mkdir -p dmenu-${VERSION} |
42 | @cp -R LICENSE Makefile README config.mk dmenu.1 config.def.h dmenu_path dmenu_run ${SRC} dmenu-${VERSION} | 42 | @cp -R LICENSE Makefile README config.mk dmenu.1 config.def.h dmenu_path dmenu_run draw ${SRC} dmenu-${VERSION} |
43 | @tar -cf dmenu-${VERSION}.tar dmenu-${VERSION} | 43 | @tar -cf dmenu-${VERSION}.tar dmenu-${VERSION} |
44 | @gzip dmenu-${VERSION}.tar | 44 | @gzip dmenu-${VERSION}.tar |
45 | @rm -rf dmenu-${VERSION} | 45 | @rm -rf dmenu-${VERSION} |
@@ -15,8 +15,8 @@ XINERAMALIBS = -L${X11LIB} -lXinerama | |||
15 | XINERAMAFLAGS = -DXINERAMA | 15 | XINERAMAFLAGS = -DXINERAMA |
16 | 16 | ||
17 | # includes and libs | 17 | # includes and libs |
18 | INCS = -I. -I/usr/include -I${X11INC} | 18 | INCS = -I. -Idraw -I/usr/include -I${X11INC} |
19 | LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${XINERAMALIBS} | 19 | LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -Ldraw -ldraw ${XINERAMALIBS} |
20 | 20 | ||
21 | # flags | 21 | # flags |
22 | CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} | 22 | CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} |
@@ -5,7 +5,6 @@ | |||
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
7 | #include <string.h> | 7 | #include <string.h> |
8 | #include <strings.h> | ||
9 | #include <unistd.h> | 8 | #include <unistd.h> |
10 | #include <X11/keysym.h> | 9 | #include <X11/keysym.h> |
11 | #include <X11/Xlib.h> | 10 | #include <X11/Xlib.h> |
@@ -1,136 +0,0 @@ | |||
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" | ||
11 | |||
12 | /* macros */ | ||
13 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | ||
14 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||
15 | |||
16 | /* variables */ | ||
17 | const char *progname; | ||
18 | |||
19 | void | ||
20 | cleanupdraw(DC *dc) { | ||
21 | if(dc->font.set) | ||
22 | XFreeFontSet(dc->dpy, dc->font.set); | ||
23 | else | ||
24 | XFreeFont(dc->dpy, dc->font.xfont); | ||
25 | XFreePixmap(dc->dpy, dc->drawable); | ||
26 | XFreeGC(dc->dpy, dc->gc); | ||
27 | } | ||
28 | |||
29 | void | ||
30 | setupdraw(DC *dc, Window w) { | ||
31 | XWindowAttributes wa; | ||
32 | |||
33 | XGetWindowAttributes(dc->dpy, w, &wa); | ||
34 | dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height, | ||
35 | DefaultDepth(dc->dpy, DefaultScreen(dc->dpy))); | ||
36 | dc->gc = XCreateGC(dc->dpy, w, 0, NULL); | ||
37 | XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); | ||
38 | if(!dc->font.set) | ||
39 | XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); | ||
40 | } | ||
41 | |||
42 | void | ||
43 | drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { | ||
44 | char buf[256]; | ||
45 | int i, x, y, h, len, olen; | ||
46 | XRectangle r = { dc->x, dc->y, dc->w, dc->h }; | ||
47 | |||
48 | XSetForeground(dc->dpy, dc->gc, col[ColBG]); | ||
49 | XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1); | ||
50 | if(!text) | ||
51 | return; | ||
52 | olen = strlen(text); | ||
53 | h = dc->font.height; | ||
54 | y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent; | ||
55 | x = dc->x + (h / 2); | ||
56 | /* shorten text if necessary */ | ||
57 | for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--); | ||
58 | if(!len) | ||
59 | return; | ||
60 | memcpy(buf, text, len); | ||
61 | if(len < olen) | ||
62 | for(i = len; i && i > len - 3; buf[--i] = '.'); | ||
63 | XSetForeground(dc->dpy, dc->gc, col[ColFG]); | ||
64 | if(dc->font.set) | ||
65 | XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len); | ||
66 | else | ||
67 | XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len); | ||
68 | } | ||
69 | |||
70 | void | ||
71 | eprint(const char *fmt, ...) { | ||
72 | va_list ap; | ||
73 | |||
74 | fprintf(stderr, "%s: ", progname); | ||
75 | va_start(ap, fmt); | ||
76 | vfprintf(stderr, fmt, ap); | ||
77 | va_end(ap); | ||
78 | exit(EXIT_FAILURE); | ||
79 | } | ||
80 | |||
81 | unsigned long | ||
82 | getcolor(DC *dc, const char *colstr) { | ||
83 | Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); | ||
84 | XColor color; | ||
85 | |||
86 | if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) | ||
87 | eprint("cannot allocate color '%s'\n", colstr); | ||
88 | return color.pixel; | ||
89 | } | ||
90 | |||
91 | void | ||
92 | initfont(DC *dc, const char *fontstr) { | ||
93 | char *def, **missing = NULL; | ||
94 | int i, n; | ||
95 | |||
96 | if(!fontstr || !*fontstr) | ||
97 | eprint("cannot load null font\n"); | ||
98 | dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def); | ||
99 | if(missing) | ||
100 | XFreeStringList(missing); | ||
101 | if(dc->font.set) { | ||
102 | XFontStruct **xfonts; | ||
103 | char **font_names; | ||
104 | dc->font.ascent = dc->font.descent = 0; | ||
105 | n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names); | ||
106 | for(i = 0; i < n; i++) { | ||
107 | dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent); | ||
108 | dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent); | ||
109 | xfonts++; | ||
110 | } | ||
111 | } | ||
112 | else { | ||
113 | if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr)) | ||
114 | && !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed"))) | ||
115 | eprint("cannot load font '%s'\n", fontstr); | ||
116 | dc->font.ascent = dc->font.xfont->ascent; | ||
117 | dc->font.descent = dc->font.xfont->descent; | ||
118 | } | ||
119 | dc->font.height = dc->font.ascent + dc->font.descent; | ||
120 | } | ||
121 | |||
122 | int | ||
123 | textnw(DC *dc, const char *text, unsigned int len) { | ||
124 | XRectangle r; | ||
125 | |||
126 | if(dc->font.set) { | ||
127 | XmbTextExtents(dc->font.set, text, len, NULL, &r); | ||
128 | return r.width; | ||
129 | } | ||
130 | return XTextWidth(dc->font.xfont, text, len); | ||
131 | } | ||
132 | |||
133 | int | ||
134 | textw(DC *dc, const char *text) { | ||
135 | return textnw(dc, text, strlen(text)) + dc->font.height; | ||
136 | } | ||
diff --git a/draw/Makefile b/draw/Makefile new file mode 100644 index 0000000..1f72b61 --- /dev/null +++ b/draw/Makefile | |||
@@ -0,0 +1,26 @@ | |||
1 | # libdraw - dynamic drawing library | ||
2 | # See LICENSE file for copyright and license details. | ||
3 | |||
4 | include ../config.mk | ||
5 | |||
6 | SRC = cleanupdraw.c setupdraw.c drawtext.c eprint.c getcolor.c initfont.c \ | ||
7 | textnw.c textw.c | ||
8 | OBJ = ${SRC:.c=.o} | ||
9 | |||
10 | all: libdraw.a | ||
11 | |||
12 | .c.o: | ||
13 | @echo CC $< | ||
14 | @${CC} -c ${CFLAGS} $< | ||
15 | |||
16 | ${OBJ}: ../config.mk draw.h | ||
17 | |||
18 | libdraw.a: ${OBJ} | ||
19 | @echo AR $@ | ||
20 | @ar cr $@ $+ | ||
21 | |||
22 | clean: | ||
23 | @echo cleaning libdraw | ||
24 | @rm -f libdraw.a ${OBJ} | ||
25 | |||
26 | .PHONY: all options clean | ||
diff --git a/draw/cleanupdraw.c b/draw/cleanupdraw.c new file mode 100644 index 0000000..28bce13 --- /dev/null +++ b/draw/cleanupdraw.c | |||
@@ -0,0 +1,13 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | void | ||
6 | cleanupdraw(DC *dc) { | ||
7 | if(dc->font.set) | ||
8 | XFreeFontSet(dc->dpy, dc->font.set); | ||
9 | else | ||
10 | XFreeFont(dc->dpy, dc->font.xfont); | ||
11 | XFreePixmap(dc->dpy, dc->drawable); | ||
12 | XFreeGC(dc->dpy, dc->gc); | ||
13 | } | ||
@@ -1,4 +1,5 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | 1 | /* See LICENSE file for copyright and license details. */ |
2 | #include <X11/Xlib.h> | ||
2 | 3 | ||
3 | /* enums */ | 4 | /* enums */ |
4 | enum { ColFG, ColBG, ColLast }; | 5 | enum { ColFG, ColBG, ColLast }; |
@@ -20,11 +21,11 @@ typedef struct { | |||
20 | 21 | ||
21 | /* forward declarations */ | 22 | /* forward declarations */ |
22 | void cleanupdraw(DC *dc); | 23 | void cleanupdraw(DC *dc); |
23 | void setupdraw(DC *dc, Window w); | ||
24 | void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); | 24 | void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); |
25 | void eprint(const char *fmt, ...); | 25 | void eprint(const char *fmt, ...); |
26 | unsigned long getcolor(DC *dc, const char *colstr); | 26 | unsigned long getcolor(DC *dc, const char *colstr); |
27 | void initfont(DC *dc, const char *fontstr); | 27 | void initfont(DC *dc, const char *fontstr); |
28 | void setupdraw(DC *dc, Window w); | ||
28 | int textnw(DC *dc, const char *text, unsigned int len); | 29 | int textnw(DC *dc, const char *text, unsigned int len); |
29 | int textw(DC *dc, const char *text); | 30 | int textw(DC *dc, const char *text); |
30 | 31 | ||
diff --git a/draw/drawtext.c b/draw/drawtext.c new file mode 100644 index 0000000..cf7b015 --- /dev/null +++ b/draw/drawtext.c | |||
@@ -0,0 +1,34 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <string.h> | ||
3 | #include <X11/Xlib.h> | ||
4 | #include "draw.h" | ||
5 | |||
6 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | ||
7 | |||
8 | void | ||
9 | drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { | ||
10 | char buf[256]; | ||
11 | int i, x, y, h, len, olen; | ||
12 | XRectangle r = { dc->x, dc->y, dc->w, dc->h }; | ||
13 | |||
14 | XSetForeground(dc->dpy, dc->gc, col[ColBG]); | ||
15 | XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1); | ||
16 | if(!text) | ||
17 | return; | ||
18 | olen = strlen(text); | ||
19 | h = dc->font.height; | ||
20 | y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent; | ||
21 | x = dc->x + (h / 2); | ||
22 | /* shorten text if necessary */ | ||
23 | for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--); | ||
24 | if(!len) | ||
25 | return; | ||
26 | memcpy(buf, text, len); | ||
27 | if(len < olen) | ||
28 | for(i = len; i && i > len - 3; buf[--i] = '.'); | ||
29 | XSetForeground(dc->dpy, dc->gc, col[ColFG]); | ||
30 | if(dc->font.set) | ||
31 | XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len); | ||
32 | else | ||
33 | XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len); | ||
34 | } | ||
diff --git a/draw/eprint.c b/draw/eprint.c new file mode 100644 index 0000000..3094b61 --- /dev/null +++ b/draw/eprint.c | |||
@@ -0,0 +1,18 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <stdarg.h> | ||
3 | #include <stdio.h> | ||
4 | #include <stdlib.h> | ||
5 | #include "draw.h" | ||
6 | |||
7 | const char *progname; | ||
8 | |||
9 | void | ||
10 | eprint(const char *fmt, ...) { | ||
11 | va_list ap; | ||
12 | |||
13 | fprintf(stderr, "%s: ", progname); | ||
14 | va_start(ap, fmt); | ||
15 | vfprintf(stderr, fmt, ap); | ||
16 | va_end(ap); | ||
17 | exit(EXIT_FAILURE); | ||
18 | } | ||
diff --git a/draw/getcolor.c b/draw/getcolor.c new file mode 100644 index 0000000..c0e5d21 --- /dev/null +++ b/draw/getcolor.c | |||
@@ -0,0 +1,13 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | unsigned long | ||
6 | getcolor(DC *dc, const char *colstr) { | ||
7 | Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); | ||
8 | XColor color; | ||
9 | |||
10 | if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) | ||
11 | eprint("cannot allocate color '%s'\n", colstr); | ||
12 | return color.pixel; | ||
13 | } | ||
diff --git a/draw/initfont.c b/draw/initfont.c new file mode 100644 index 0000000..77d3182 --- /dev/null +++ b/draw/initfont.c | |||
@@ -0,0 +1,36 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||
6 | |||
7 | void | ||
8 | initfont(DC *dc, const char *fontstr) { | ||
9 | char *def, **missing = NULL; | ||
10 | int i, n; | ||
11 | |||
12 | if(!fontstr || !*fontstr) | ||
13 | eprint("cannot load null font\n"); | ||
14 | dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def); | ||
15 | if(missing) | ||
16 | XFreeStringList(missing); | ||
17 | if(dc->font.set) { | ||
18 | XFontStruct **xfonts; | ||
19 | char **font_names; | ||
20 | dc->font.ascent = dc->font.descent = 0; | ||
21 | n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names); | ||
22 | for(i = 0; i < n; i++) { | ||
23 | dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent); | ||
24 | dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent); | ||
25 | xfonts++; | ||
26 | } | ||
27 | } | ||
28 | else { | ||
29 | if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr)) | ||
30 | && !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed"))) | ||
31 | eprint("cannot load font '%s'\n", fontstr); | ||
32 | dc->font.ascent = dc->font.xfont->ascent; | ||
33 | dc->font.descent = dc->font.xfont->descent; | ||
34 | } | ||
35 | dc->font.height = dc->font.ascent + dc->font.descent; | ||
36 | } | ||
diff --git a/draw/setupdraw.c b/draw/setupdraw.c new file mode 100644 index 0000000..7dd5012 --- /dev/null +++ b/draw/setupdraw.c | |||
@@ -0,0 +1,16 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | void | ||
6 | setupdraw(DC *dc, Window w) { | ||
7 | XWindowAttributes wa; | ||
8 | |||
9 | XGetWindowAttributes(dc->dpy, w, &wa); | ||
10 | dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height, | ||
11 | DefaultDepth(dc->dpy, DefaultScreen(dc->dpy))); | ||
12 | dc->gc = XCreateGC(dc->dpy, w, 0, NULL); | ||
13 | XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); | ||
14 | if(!dc->font.set) | ||
15 | XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); | ||
16 | } | ||
diff --git a/draw/textnw.c b/draw/textnw.c new file mode 100644 index 0000000..9c0c122 --- /dev/null +++ b/draw/textnw.c | |||
@@ -0,0 +1,14 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | int | ||
6 | textnw(DC *dc, const char *text, unsigned int len) { | ||
7 | XRectangle r; | ||
8 | |||
9 | if(dc->font.set) { | ||
10 | XmbTextExtents(dc->font.set, text, len, NULL, &r); | ||
11 | return r.width; | ||
12 | } | ||
13 | return XTextWidth(dc->font.xfont, text, len); | ||
14 | } | ||
diff --git a/draw/textw.c b/draw/textw.c new file mode 100644 index 0000000..a8407f6 --- /dev/null +++ b/draw/textw.c | |||
@@ -0,0 +1,9 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <string.h> | ||
3 | #include <X11/Xlib.h> | ||
4 | #include "draw.h" | ||
5 | |||
6 | int | ||
7 | textw(DC *dc, const char *text) { | ||
8 | return textnw(dc, text, strlen(text)) + dc->font.height; | ||
9 | } | ||