aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConnor Lane Smith <cls@lubutu.com>2010-06-24 17:44:35 +0100
committerConnor Lane Smith <cls@lubutu.com>2010-06-24 17:44:35 +0100
commit4508fd2c4ee3171bdc1bffb7e53ecda8290292ef (patch)
treee86bbdfc4e1cc93fe5906ef204bf970d389c3df2
parent723361fa124aa666d637e3fc9f5df1210a9e02b4 (diff)
moved draw.c to libdraw.a
-rw-r--r--Makefile16
-rw-r--r--config.mk4
-rw-r--r--dinput.c1
-rw-r--r--draw.c136
-rw-r--r--draw/Makefile26
-rw-r--r--draw/cleanupdraw.c13
-rw-r--r--draw/draw.h (renamed from draw.h)3
-rw-r--r--draw/drawtext.c34
-rw-r--r--draw/eprint.c18
-rw-r--r--draw/getcolor.c13
-rw-r--r--draw/initfont.c36
-rw-r--r--draw/setupdraw.c16
-rw-r--r--draw/textnw.c14
-rw-r--r--draw/textw.c9
14 files changed, 191 insertions, 148 deletions
diff --git a/Makefile b/Makefile
index da5344b..90f178b 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
3 3
4include config.mk 4include config.mk
5 5
6SRC = dinput.c dmenu.c draw.c 6SRC = dinput.c dmenu.c
7OBJ = ${SRC:.c=.o} 7OBJ = ${SRC:.c=.o}
8 8
9all: options dinput dmenu 9all: 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
23config.h: 23config.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
27dinput: dinput.o draw.o 27.o:
28 @echo CC -o $@ 28 @echo CC -o $@
29 @${CC} -o $@ $+ ${LDFLAGS} 29 @${CC} -o $@ $< ${LDFLAGS}
30 30
31dmenu: dmenu.o draw.o 31draw/libdraw.a:
32 @echo CC -o $@ 32 @cd draw && make
33 @${CC} -o $@ $+ ${LDFLAGS}
34 33
35clean: 34clean:
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
39dist: clean 39dist: 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}
diff --git a/config.mk b/config.mk
index eb410dc..431754e 100644
--- a/config.mk
+++ b/config.mk
@@ -15,8 +15,8 @@ XINERAMALIBS = -L${X11LIB} -lXinerama
15XINERAMAFLAGS = -DXINERAMA 15XINERAMAFLAGS = -DXINERAMA
16 16
17# includes and libs 17# includes and libs
18INCS = -I. -I/usr/include -I${X11INC} 18INCS = -I. -Idraw -I/usr/include -I${X11INC}
19LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${XINERAMALIBS} 19LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -Ldraw -ldraw ${XINERAMALIBS}
20 20
21# flags 21# flags
22CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} 22CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
diff --git a/dinput.c b/dinput.c
index cd13b1d..283b018 100644
--- a/dinput.c
+++ b/dinput.c
@@ -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>
diff --git a/draw.c b/draw.c
deleted file mode 100644
index 9142905..0000000
--- a/draw.c
+++ /dev/null
@@ -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 */
17const char *progname;
18
19void
20cleanupdraw(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
29void
30setupdraw(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
42void
43drawtext(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
70void
71eprint(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
81unsigned long
82getcolor(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
91void
92initfont(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
122int
123textnw(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
133int
134textw(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
4include ../config.mk
5
6SRC = cleanupdraw.c setupdraw.c drawtext.c eprint.c getcolor.c initfont.c \
7textnw.c textw.c
8OBJ = ${SRC:.c=.o}
9
10all: libdraw.a
11
12.c.o:
13 @echo CC $<
14 @${CC} -c ${CFLAGS} $<
15
16${OBJ}: ../config.mk draw.h
17
18libdraw.a: ${OBJ}
19 @echo AR $@
20 @ar cr $@ $+
21
22clean:
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
5void
6cleanupdraw(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}
diff --git a/draw.h b/draw/draw.h
index 1705909..4646a18 100644
--- a/draw.h
+++ b/draw/draw.h
@@ -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 */
4enum { ColFG, ColBG, ColLast }; 5enum { ColFG, ColBG, ColLast };
@@ -20,11 +21,11 @@ typedef struct {
20 21
21/* forward declarations */ 22/* forward declarations */
22void cleanupdraw(DC *dc); 23void cleanupdraw(DC *dc);
23void setupdraw(DC *dc, Window w);
24void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); 24void drawtext(DC *dc, const char *text, unsigned long col[ColLast]);
25void eprint(const char *fmt, ...); 25void eprint(const char *fmt, ...);
26unsigned long getcolor(DC *dc, const char *colstr); 26unsigned long getcolor(DC *dc, const char *colstr);
27void initfont(DC *dc, const char *fontstr); 27void initfont(DC *dc, const char *fontstr);
28void setupdraw(DC *dc, Window w);
28int textnw(DC *dc, const char *text, unsigned int len); 29int textnw(DC *dc, const char *text, unsigned int len);
29int textw(DC *dc, const char *text); 30int 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
8void
9drawtext(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
7const char *progname;
8
9void
10eprint(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
5unsigned long
6getcolor(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
7void
8initfont(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
5void
6setupdraw(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
5int
6textnw(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
6int
7textw(DC *dc, const char *text) {
8 return textnw(dc, text, strlen(text)) + dc->font.height;
9}