moved draw.c to libdraw.a
4508fd2c
14 file(s) · +191 −148
| 3 | 3 | ||
| 4 | 4 | include config.mk |
|
| 5 | 5 | ||
| 6 | - | SRC = dinput.c dmenu.c draw.c |
|
| 6 | + | SRC = dinput.c dmenu.c |
|
| 7 | 7 | OBJ = ${SRC:.c=.o} |
|
| 8 | 8 | ||
| 9 | 9 | all: options dinput dmenu |
|
| 18 | 18 | @echo CC $< |
|
| 19 | 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 | 23 | config.h: |
|
| 24 | 24 | @echo creating $@ from config.def.h |
|
| 25 | 25 | @cp config.def.h $@ |
|
| 26 | 26 | ||
| 27 | - | dinput: dinput.o draw.o |
|
| 27 | + | .o: |
|
| 28 | 28 | @echo CC -o $@ |
|
| 29 | - | @${CC} -o $@ $+ ${LDFLAGS} |
|
| 29 | + | @${CC} -o $@ $< ${LDFLAGS} |
|
| 30 | 30 | ||
| 31 | - | dmenu: dmenu.o draw.o |
|
| 32 | - | @echo CC -o $@ |
|
| 33 | - | @${CC} -o $@ $+ ${LDFLAGS} |
|
| 31 | + | draw/libdraw.a: |
|
| 32 | + | @cd draw && make |
|
| 34 | 33 | ||
| 35 | 34 | clean: |
|
| 36 | 35 | @echo cleaning |
|
| 37 | 36 | @rm -f dinput dmenu ${OBJ} dmenu-${VERSION}.tar.gz |
|
| 37 | + | @cd draw && make clean |
|
| 38 | 38 | ||
| 39 | 39 | dist: clean |
|
| 40 | 40 | @echo creating dist tarball |
|
| 41 | 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 | 43 | @tar -cf dmenu-${VERSION}.tar dmenu-${VERSION} |
|
| 44 | 44 | @gzip dmenu-${VERSION}.tar |
|
| 45 | 45 | @rm -rf dmenu-${VERSION} |
|
| 15 | 15 | XINERAMAFLAGS = -DXINERAMA |
|
| 16 | 16 | ||
| 17 | 17 | # includes and libs |
|
| 18 | - | INCS = -I. -I/usr/include -I${X11INC} |
|
| 19 | - | LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${XINERAMALIBS} |
|
| 18 | + | INCS = -I. -Idraw -I/usr/include -I${X11INC} |
|
| 19 | + | LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -Ldraw -ldraw ${XINERAMALIBS} |
|
| 20 | 20 | ||
| 21 | 21 | # flags |
|
| 22 | 22 | CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} |
| 5 | 5 | #include <stdio.h> |
|
| 6 | 6 | #include <stdlib.h> |
|
| 7 | 7 | #include <string.h> |
|
| 8 | - | #include <strings.h> |
|
| 9 | 8 | #include <unistd.h> |
|
| 10 | 9 | #include <X11/keysym.h> |
|
| 11 | 10 | #include <X11/Xlib.h> |
| 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 | - | } |
| 1 | 1 | /* See LICENSE file for copyright and license details. */ |
|
| 2 | + | #include <X11/Xlib.h> |
|
| 2 | 3 | ||
| 3 | 4 | /* enums */ |
|
| 4 | 5 | enum { ColFG, ColBG, ColLast }; |
|
| 20 | 21 | ||
| 21 | 22 | /* forward declarations */ |
|
| 22 | 23 | void cleanupdraw(DC *dc); |
|
| 23 | - | void setupdraw(DC *dc, Window w); |
|
| 24 | 24 | void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); |
|
| 25 | 25 | void eprint(const char *fmt, ...); |
|
| 26 | 26 | unsigned long getcolor(DC *dc, const char *colstr); |
|
| 27 | 27 | void initfont(DC *dc, const char *fontstr); |
|
| 28 | + | void setupdraw(DC *dc, Window w); |
|
| 28 | 29 | int textnw(DC *dc, const char *text, unsigned int len); |
|
| 29 | 30 | int textw(DC *dc, const char *text); |
|
| 30 | 31 | ||
| 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 |
| 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 | + | /* 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 | + | } |
| 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 | + | } |
| 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 | + | } |
| 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 | + | } |
| 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 | + | } |
| 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 | + | } |
| 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 | + | } |