renamed draw into drw f2544a33
Anselm R Garbe · 2013-04-17 21:21 6 file(s) · +265 −268
Makefile +1 −1
3 3
4 4
include config.mk
5 5
6 -
SRC = util.c draw.c dwm.c
6 +
SRC = drw.c dwm.c util.c
7 7
OBJ = ${SRC:.c=.o}
8 8
9 9
all: options dwm
draw.c (deleted) +0 −202
1 -
/* See LICENSE file for copyright and license details. */
2 -
#include <stdio.h>
3 -
#include <stdlib.h>
4 -
#include <string.h>
5 -
#include <X11/Xlib.h>
6 -
7 -
#include "draw.h"
8 -
#include "util.h"
9 -
10 -
Draw *
11 -
draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
12 -
	Draw *draw = (Draw *)calloc(1, sizeof(Draw));
13 -
	draw->dpy = dpy;
14 -
	draw->screen = screen;
15 -
	draw->win = win;
16 -
	draw->w = w;
17 -
	draw->h = h;
18 -
	draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
19 -
	draw->gc = XCreateGC(dpy, win, 0, NULL);
20 -
	XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter);
21 -
	return draw;
22 -
}
23 -
24 -
void
25 -
draw_resize(Draw *draw, unsigned int w, unsigned int h) {
26 -
	if(!draw)
27 -
		return;
28 -
	draw->w = w;
29 -
	draw->h = h;
30 -
	XFreePixmap(draw->dpy, draw->drawable);
31 -
	draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
32 -
}
33 -
34 -
void
35 -
draw_free(Draw *draw) {
36 -
	XFreePixmap(draw->dpy, draw->drawable);
37 -
	XFreeGC(draw->dpy, draw->gc);
38 -
	free(draw);
39 -
}
40 -
41 -
Fnt *
42 -
draw_font_create(Draw *draw, const char *fontname) {
43 -
	Fnt *font;
44 -
	char *def, **missing;
45 -
	int n;
46 -
47 -
	if(!draw)
48 -
		return NULL;
49 -
	font = (Fnt *)calloc(1, sizeof(Fnt));
50 -
	font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
51 -
	if(missing) {
52 -
		while(n--)
53 -
			fprintf(stderr, "draw: missing fontset: %s\n", missing[n]);
54 -
		XFreeStringList(missing);
55 -
	}
56 -
	if(font->set) {
57 -
		XFontStruct **xfonts;
58 -
		char **font_names;
59 -
		XExtentsOfFontSet(font->set);
60 -
		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
61 -
		while(n--) {
62 -
			font->ascent = MAX(font->ascent, (*xfonts)->ascent);
63 -
			font->descent = MAX(font->descent,(*xfonts)->descent);
64 -
			xfonts++;
65 -
		}
66 -
	}
67 -
	else {
68 -
		if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname))
69 -
		&& !(font->xfont = XLoadQueryFont(draw->dpy, "fixed")))
70 -
			die("error, cannot load font: '%s'\n", fontname);
71 -
		font->ascent = font->xfont->ascent;
72 -
		font->descent = font->xfont->descent;
73 -
	}
74 -
	font->h = font->ascent + font->descent;
75 -
	return font;
76 -
}
77 -
78 -
void
79 -
draw_font_free(Draw *draw, Fnt *font) {
80 -
	if(!draw || !font)
81 -
		return;
82 -
	if(font->set)
83 -
		XFreeFontSet(draw->dpy, font->set);
84 -
	else
85 -
		XFreeFont(draw->dpy, font->xfont);
86 -
	free(font);
87 -
}
88 -
89 -
Col *
90 -
draw_col_create(Draw *draw, const char *colname) {
91 -
	Col *col = (Col *)calloc(1, sizeof(Col));
92 -
	Colormap cmap = DefaultColormap(draw->dpy, draw->screen);
93 -
	XColor color;
94 -
95 -
	if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color))
96 -
		die("error, cannot allocate color '%s'\n", colname);
97 -
	col->rgb = color.pixel;
98 -
	return col;
99 -
}
100 -
101 -
void
102 -
draw_col_free(Draw *draw, Col *col) {
103 -
	if(!col)
104 -
		return;
105 -
	free(col);
106 -
}
107 -
108 -
void
109 -
draw_setfont(Draw *draw, Fnt *font) {
110 -
	if(!draw)
111 -
		return;
112 -
	draw->font = font;
113 -
}
114 -
115 -
void
116 -
draw_setfg(Draw *draw, Col *col) {
117 -
	if(!draw) 
118 -
		return;
119 -
	draw->fg = col;
120 -
}
121 -
122 -
void
123 -
draw_setbg(Draw *draw, Col *col) {
124 -
	if(!draw)
125 -
		return;
126 -
	draw->bg = col;
127 -
}
128 -
129 -
void
130 -
draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
131 -
	int dx;
132 -
133 -
	if(!draw || !draw->font || !draw->fg || !draw->bg)
134 -
		return;
135 -
	XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
136 -
	dx = (draw->font->ascent + draw->font->descent + 2) / 4;
137 -
	if(filled)
138 -
		XFillRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx+1, dx+1);
139 -
	else if(empty)
140 -
		XDrawRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx, dx);
141 -
}
142 -
143 -
void
144 -
draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
145 -
	char buf[256];
146 -
	int i, tx, ty, len, olen;
147 -
	TextExtents tex;
148 -
149 -
	if(!draw || !draw->fg || !draw->bg)
150 -
		return;
151 -
	XSetForeground(draw->dpy, draw->gc, invert ? draw->fg->rgb : draw->bg->rgb);
152 -
	XFillRectangle(draw->dpy, draw->drawable, draw->gc, x, y, w, h);
153 -
	if(!text || !draw->font)
154 -
		return;
155 -
	olen = strlen(text);
156 -
	draw_getextents(draw, text, olen, &tex);
157 -
	ty = y + (h / 2) - tex.yOff;
158 -
	tx = x + tex.xOff;
159 -
	/* shorten text if necessary */
160 -
	for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
161 -
		draw_getextents(draw, text, len, &tex);
162 -
	if(!len)
163 -
		return;
164 -
	memcpy(buf, text, len);
165 -
	if(len < olen)
166 -
		for(i = len; i && i > len - 3; buf[--i] = '.');
167 -
	XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
168 -
	if(draw->font->set)
169 -
		XmbDrawString(draw->dpy, draw->drawable, draw->font->set, draw->gc, tx, ty, buf, len);
170 -
	else
171 -
		XDrawString(draw->dpy, draw->drawable, draw->gc, tx, ty, buf, len);
172 -
}
173 -
174 -
void
175 -
draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
176 -
	if(!draw)
177 -
		return;
178 -
	XCopyArea(draw->dpy, draw->drawable, draw->win, draw->gc, x, y, w, h, x, y);
179 -
	XSync(draw->dpy, False);
180 -
}
181 -
182 -
183 -
void
184 -
draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents) {
185 -
	XRectangle r;
186 -
187 -
	if(!draw || !draw->font || !text)
188 -
		return;
189 -
	if(draw->font->set) {
190 -
		XmbTextExtents(draw->font->set, text, len, NULL, &r);
191 -
		extents->xOff = r.x;
192 -
		extents->yOff = r.y;
193 -
		extents->w = r.width;
194 -
		extents->h = r.height;
195 -
	}
196 -
	else {
197 -
		extents->h = draw->font->ascent + draw->font->descent;
198 -
		extents->w = XTextWidth(draw->font->xfont, text, len);
199 -
		extents->xOff = extents->h / 2;
200 -
		extents->yOff = (extents->h / 2) + draw->font->ascent;
201 -
	}
202 -
}
draw.h (deleted) +0 −64
1 -
/* See LICENSE file for copyright and license details. */
2 -
3 -
struct _XCol {
4 -
	unsigned long rgb;
5 -
};
6 -
typedef struct _XCol Col;
7 -
8 -
struct _XFont {
9 -
	int ascent;
10 -
	int descent;
11 -
	unsigned int h;
12 -
	XFontSet set;
13 -
	XFontStruct *xfont;
14 -
};
15 -
typedef struct _XFont Fnt;
16 -
17 -
typedef struct _XDraw Draw;
18 -
struct _XDraw {
19 -
	unsigned int w, h;
20 -
	Display *dpy;
21 -
	int screen;
22 -
	Window win;
23 -
	Drawable drawable;
24 -
	GC gc;
25 -
	Col *fg;
26 -
	Col *bg;
27 -
	Fnt *font;
28 -
};
29 -
30 -
typedef struct {
31 -
	unsigned int w;
32 -
	unsigned int h;
33 -
	int xOff;
34 -
	int yOff;
35 -
} TextExtents;
36 -
37 -
/* Drawable abstraction */
38 -
Draw *draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
39 -
void draw_resize(Draw *draw, unsigned int w, unsigned int h);
40 -
void draw_free(Draw *draw);
41 -
42 -
/* Fnt abstraction */
43 -
Fnt *draw_font_create(Draw *draw, const char *fontname);
44 -
void draw_font_free(Draw *draw, Fnt *font);
45 -
46 -
/* Colour abstraction */
47 -
Col *draw_col_create(Draw *draw, const char *colname);
48 -
void draw_col_free(Draw *draw, Col *col);
49 -
50 -
/* Drawing context manipulation */
51 -
void draw_setfont(Draw *draw, Fnt *font);
52 -
void draw_setfg(Draw *draw, Col *col);
53 -
void draw_setbg(Draw *draw, Col *col);
54 -
55 -
/* Drawing functions */
56 -
void draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert);
57 -
void draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert);
58 -
59 -
/* Map functions */
60 -
void draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h);
61 -
62 -
/* Text functions */
63 -
void draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents);
64 -
drw.c (added) +202 −0
1 +
/* See LICENSE file for copyright and license details. */
2 +
#include <stdio.h>
3 +
#include <stdlib.h>
4 +
#include <string.h>
5 +
#include <X11/Xlib.h>
6 +
7 +
#include "drw.h"
8 +
#include "util.h"
9 +
10 +
Drw *
11 +
drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
12 +
	Drw *drw = (Drw *)calloc(1, sizeof(Drw));
13 +
	drw->dpy = dpy;
14 +
	drw->screen = screen;
15 +
	drw->win = win;
16 +
	drw->w = w;
17 +
	drw->h = h;
18 +
	drw->drwable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
19 +
	drw->gc = XCreateGC(dpy, win, 0, NULL);
20 +
	XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
21 +
	return drw;
22 +
}
23 +
24 +
void
25 +
drw_resize(Drw *drw, unsigned int w, unsigned int h) {
26 +
	if(!drw)
27 +
		return;
28 +
	drw->w = w;
29 +
	drw->h = h;
30 +
	XFreePixmap(drw->dpy, drw->drwable);
31 +
	drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen));
32 +
}
33 +
34 +
void
35 +
drw_free(Drw *drw) {
36 +
	XFreePixmap(drw->dpy, drw->drwable);
37 +
	XFreeGC(drw->dpy, drw->gc);
38 +
	free(drw);
39 +
}
40 +
41 +
Fnt *
42 +
drw_font_create(Drw *drw, const char *fontname) {
43 +
	Fnt *font;
44 +
	char *def, **missing;
45 +
	int n;
46 +
47 +
	if(!drw)
48 +
		return NULL;
49 +
	font = (Fnt *)calloc(1, sizeof(Fnt));
50 +
	font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def);
51 +
	if(missing) {
52 +
		while(n--)
53 +
			fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
54 +
		XFreeStringList(missing);
55 +
	}
56 +
	if(font->set) {
57 +
		XFontStruct **xfonts;
58 +
		char **font_names;
59 +
		XExtentsOfFontSet(font->set);
60 +
		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
61 +
		while(n--) {
62 +
			font->ascent = MAX(font->ascent, (*xfonts)->ascent);
63 +
			font->descent = MAX(font->descent,(*xfonts)->descent);
64 +
			xfonts++;
65 +
		}
66 +
	}
67 +
	else {
68 +
		if(!(font->xfont = XLoadQueryFont(drw->dpy, fontname))
69 +
		&& !(font->xfont = XLoadQueryFont(drw->dpy, "fixed")))
70 +
			die("error, cannot load font: '%s'\n", fontname);
71 +
		font->ascent = font->xfont->ascent;
72 +
		font->descent = font->xfont->descent;
73 +
	}
74 +
	font->h = font->ascent + font->descent;
75 +
	return font;
76 +
}
77 +
78 +
void
79 +
drw_font_free(Drw *drw, Fnt *font) {
80 +
	if(!drw || !font)
81 +
		return;
82 +
	if(font->set)
83 +
		XFreeFontSet(drw->dpy, font->set);
84 +
	else
85 +
		XFreeFont(drw->dpy, font->xfont);
86 +
	free(font);
87 +
}
88 +
89 +
Clr *
90 +
drw_clr_create(Drw *drw, const char *clrname) {
91 +
	Clr *clr = (Clr *)calloc(1, sizeof(Clr));
92 +
	Colormap cmap = DefaultColormap(drw->dpy, drw->screen);
93 +
	XColor color;
94 +
95 +
	if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
96 +
		die("error, cannot allocate color '%s'\n", clrname);
97 +
	clr->rgb = color.pixel;
98 +
	return clr;
99 +
}
100 +
101 +
void
102 +
drw_clr_free(Drw *drw, Clr *clr) {
103 +
	if(!clr)
104 +
		return;
105 +
	free(clr);
106 +
}
107 +
108 +
void
109 +
drw_setfont(Drw *drw, Fnt *font) {
110 +
	if(!drw)
111 +
		return;
112 +
	drw->font = font;
113 +
}
114 +
115 +
void
116 +
drw_setfg(Drw *drw, Clr *clr) {
117 +
	if(!drw) 
118 +
		return;
119 +
	drw->fg = clr;
120 +
}
121 +
122 +
void
123 +
drw_setbg(Drw *drw, Clr *clr) {
124 +
	if(!drw)
125 +
		return;
126 +
	drw->bg = clr;
127 +
}
128 +
129 +
void
130 +
drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
131 +
	int dx;
132 +
133 +
	if(!drw || !drw->font || !drw->fg || !drw->bg)
134 +
		return;
135 +
	XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
136 +
	dx = (drw->font->ascent + drw->font->descent + 2) / 4;
137 +
	if(filled)
138 +
		XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1);
139 +
	else if(empty)
140 +
		XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx);
141 +
}
142 +
143 +
void
144 +
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
145 +
	char buf[256];
146 +
	int i, tx, ty, len, olen;
147 +
	Extnts tex;
148 +
149 +
	if(!drw || !drw->fg || !drw->bg)
150 +
		return;
151 +
	XSetForeground(drw->dpy, drw->gc, invert ? drw->fg->rgb : drw->bg->rgb);
152 +
	XFillRectangle(drw->dpy, drw->drwable, drw->gc, x, y, w, h);
153 +
	if(!text || !drw->font)
154 +
		return;
155 +
	olen = strlen(text);
156 +
	drw_getexts(drw, text, olen, &tex);
157 +
	ty = y + (h / 2) - tex.yOff;
158 +
	tx = x + tex.xOff;
159 +
	/* shorten text if necessary */
160 +
	for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
161 +
		drw_getexts(drw, text, len, &tex);
162 +
	if(!len)
163 +
		return;
164 +
	memcpy(buf, text, len);
165 +
	if(len < olen)
166 +
		for(i = len; i && i > len - 3; buf[--i] = '.');
167 +
	XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
168 +
	if(drw->font->set)
169 +
		XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len);
170 +
	else
171 +
		XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len);
172 +
}
173 +
174 +
void
175 +
drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) {
176 +
	if(!drw)
177 +
		return;
178 +
	XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y);
179 +
	XSync(drw->dpy, False);
180 +
}
181 +
182 +
183 +
void
184 +
drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) {
185 +
	XRectangle r;
186 +
187 +
	if(!drw || !drw->font || !text)
188 +
		return;
189 +
	if(drw->font->set) {
190 +
		XmbTextExtents(drw->font->set, text, len, NULL, &r);
191 +
		tex->xOff = r.x;
192 +
		tex->yOff = r.y;
193 +
		tex->w = r.width;
194 +
		tex->h = r.height;
195 +
	}
196 +
	else {
197 +
		tex->h = drw->font->ascent + drw->font->descent;
198 +
		tex->w = XTextWidth(drw->font->xfont, text, len);
199 +
		tex->xOff = tex->h / 2;
200 +
		tex->yOff = (tex->h / 2) + drw->font->ascent;
201 +
	}
202 +
}
drw.h (added) +61 −0
1 +
/* See LICENSE file for copyright and license details. */
2 +
3 +
typedef struct {
4 +
	unsigned long rgb;
5 +
} Clr;
6 +
7 +
typedef struct {
8 +
	int ascent;
9 +
	int descent;
10 +
	unsigned int h;
11 +
	XFontSet set;
12 +
	XFontStruct *xfont;
13 +
} Fnt;
14 +
15 +
typedef struct {
16 +
	unsigned int w, h;
17 +
	Display *dpy;
18 +
	int screen;
19 +
	Window win;
20 +
	Drawable drwable;
21 +
	GC gc;
22 +
	Clr *fg;
23 +
	Clr *bg;
24 +
	Fnt *font;
25 +
} Drw;
26 +
27 +
typedef struct {
28 +
	unsigned int w;
29 +
	unsigned int h;
30 +
	int xOff;
31 +
	int yOff;
32 +
} Extnts;
33 +
34 +
/* Drawable abstraction */
35 +
Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
36 +
void drw_resize(Drw *drw, unsigned int w, unsigned int h);
37 +
void drw_free(Drw *drw);
38 +
39 +
/* Fnt abstraction */
40 +
Fnt *drw_font_create(Drw *drw, const char *fontname);
41 +
void drw_font_free(Drw *drw, Fnt *font);
42 +
43 +
/* Clrour abstraction */
44 +
Clr *drw_clr_create(Drw *drw, const char *clrname);
45 +
void drw_clr_free(Drw *drw, Clr *clr);
46 +
47 +
/* Drawing context manipulation */
48 +
void drw_setfont(Drw *drw, Fnt *font);
49 +
void drw_setfg(Drw *drw, Clr *clr);
50 +
void drw_setbg(Drw *drw, Clr *clr);
51 +
52 +
/* Drawing functions */
53 +
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert);
54 +
void drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert);
55 +
56 +
/* Map functions */
57 +
void drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h);
58 +
59 +
/* Text functions */
60 +
void drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *extnts);
61 +
dwm.c +1 −1
40 40
#include <X11/extensions/Xinerama.h>
41 41
#endif /* XINERAMA */
42 42
43 -
#include "draw.h"
43 +
#include "drw.h"
44 44
#include "util.h"
45 45
46 46
/* macros */