summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--draw.c60
-rw-r--r--draw.h10
-rw-r--r--dwm.c14
-rw-r--r--util.c17
-rw-r--r--util.h6
6 files changed, 80 insertions, 29 deletions
diff --git a/Makefile b/Makefile
index ce6e508..b04b79c 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
3 3
4include config.mk 4include config.mk
5 5
6SRC = draw.c dwm.c 6SRC = util.c draw.c dwm.c
7OBJ = ${SRC:.c=.o} 7OBJ = ${SRC:.c=.o}
8 8
9all: options dwm 9all: options dwm
diff --git a/draw.c b/draw.c
index 01436ea..e88a70c 100644
--- a/draw.c
+++ b/draw.c
@@ -1,8 +1,10 @@
1/* See LICENSE file for copyright and license details. */ 1/* See LICENSE file for copyright and license details. */
2#include <stdio.h>
2#include <stdlib.h> 3#include <stdlib.h>
3#include <X11/Xlib.h> 4#include <X11/Xlib.h>
4 5
5#include "draw.h" 6#include "draw.h"
7#include "util.h"
6 8
7Draw * 9Draw *
8draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) { 10draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
@@ -36,32 +38,70 @@ draw_free(Draw *draw) {
36} 38}
37 39
38Fnt * 40Fnt *
39font_create(const char *fontname) { 41draw_font_create(Draw *draw, const char *fontname) {
40 Fnt *font = (Fnt *)calloc(1, sizeof(Fnt)); 42 Fnt *font;
41 /* TODO: allocate actual font */ 43 char *def, **missing;
44 int n;
45
46 if(!draw)
47 return NULL;
48 font = (Fnt *)calloc(1, sizeof(Fnt));
49 font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
50 if(missing) {
51 while(n--)
52 fprintf(stderr, "draw: missing fontset: %s\n", missing[n]);
53 XFreeStringList(missing);
54 }
55 if(font->set) {
56 XFontStruct **xfonts;
57 char **font_names;
58
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;
42 return font; 75 return font;
43} 76}
44 77
45void 78void
46font_free(Fnt *font) { 79draw_font_free(Draw *draw, Fnt *font) {
47 if(!font) 80 if(!draw || !font)
48 return; 81 return;
49 /* TODO: deallocate any font resources */ 82 if(font->set)
83 XFreeFontSet(draw->dpy, font->set);
84 else
85 XFreeFont(draw->dpy, font->xfont);
50 free(font); 86 free(font);
51} 87}
52 88
53Col * 89Col *
54col_create(const char *colname) { 90draw_col_create(Draw *draw, const char *colname) {
55 Col *col = (Col *)calloc(1, sizeof(Col)); 91 Col *col = (Col *)calloc(1, sizeof(Col));
56 /* TODO: allocate color */ 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;
57 return col; 98 return col;
58} 99}
59 100
60void 101void
61col_free(Col *col) { 102draw_col_free(Draw *draw, Col *col) {
62 if(!col) 103 if(!col)
63 return; 104 return;
64 /* TODO: deallocate any color resource */
65 free(col); 105 free(col);
66} 106}
67 107
diff --git a/draw.h b/draw.h
index d1a2f37..0c811ab 100644
--- a/draw.h
+++ b/draw.h
@@ -8,7 +8,7 @@ typedef struct _XCol Col;
8struct _XFont { 8struct _XFont {
9 int ascent; 9 int ascent;
10 int descent; 10 int descent;
11 unsigned int h, w; 11 unsigned int h;
12 XFontSet set; 12 XFontSet set;
13 XFontStruct *xfont; 13 XFontStruct *xfont;
14}; 14};
@@ -45,12 +45,12 @@ void draw_resize(Draw *draw, unsigned int w, unsigned int h);
45void draw_free(Draw *draw); 45void draw_free(Draw *draw);
46 46
47/* Fnt abstraction */ 47/* Fnt abstraction */
48Fnt *font_create(const char *fontname); 48Fnt *draw_font_create(Draw *draw, const char *fontname);
49void font_free(Fnt *font); 49void draw_font_free(Draw *draw, Fnt *font);
50 50
51/* Colour abstraction */ 51/* Colour abstraction */
52Col *col_create(const char *colname); 52Col *draw_col_create(Draw *draw, const char *colname);
53void col_free(Col *col); 53void draw_col_free(Draw *draw, Col *col);
54 54
55/* Drawing context manipulation */ 55/* Drawing context manipulation */
56void draw_setfont(Draw *draw, Fnt *font); 56void draw_setfont(Draw *draw, Fnt *font);
diff --git a/dwm.c b/dwm.c
index 8b67cea..d9443da 100644
--- a/dwm.c
+++ b/dwm.c
@@ -41,6 +41,7 @@
41#endif /* XINERAMA */ 41#endif /* XINERAMA */
42 42
43#include "draw.h" 43#include "draw.h"
44#include "util.h"
44 45
45/* macros */ 46/* macros */
46#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) 47#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
@@ -49,8 +50,6 @@
49 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) 50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
50#define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags])) 51#define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
51#define LENGTH(X) (sizeof X / sizeof X[0]) 52#define LENGTH(X) (sizeof X / sizeof X[0])
52#define MAX(A, B) ((A) > (B) ? (A) : (B))
53#define MIN(A, B) ((A) < (B) ? (A) : (B))
54#define MOUSEMASK (BUTTONMASK|PointerMotionMask) 53#define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55#define WIDTH(X) ((X)->w + 2 * (X)->bw) 54#define WIDTH(X) ((X)->w + 2 * (X)->bw)
56#define HEIGHT(X) ((X)->h + 2 * (X)->bw) 55#define HEIGHT(X) ((X)->h + 2 * (X)->bw)
@@ -176,7 +175,6 @@ static Monitor *createmon(void);
176static void destroynotify(XEvent *e); 175static void destroynotify(XEvent *e);
177static void detach(Client *c); 176static void detach(Client *c);
178static void detachstack(Client *c); 177static void detachstack(Client *c);
179static void die(const char *errstr, ...);
180static Monitor *dirtomon(int dir); 178static Monitor *dirtomon(int dir);
181static void drawbar(Monitor *m); 179static void drawbar(Monitor *m);
182static void drawbars(void); 180static void drawbars(void);
@@ -695,16 +693,6 @@ detachstack(Client *c) {
695 } 693 }
696} 694}
697 695
698void
699die(const char *errstr, ...) {
700 va_list ap;
701
702 va_start(ap, errstr);
703 vfprintf(stderr, errstr, ap);
704 va_end(ap);
705 exit(EXIT_FAILURE);
706}
707
708Monitor * 696Monitor *
709dirtomon(int dir) { 697dirtomon(int dir) {
710 Monitor *m = NULL; 698 Monitor *m = NULL;
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..51acd1a
--- /dev/null
+++ b/util.c
@@ -0,0 +1,17 @@
1/* See LICENSE file for copyright and license details. */
2#include <stdarg.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6#include "util.h"
7
8void
9die(const char *errstr, ...) {
10 va_list ap;
11
12 va_start(ap, errstr);
13 vfprintf(stderr, errstr, ap);
14 va_end(ap);
15 exit(EXIT_FAILURE);
16}
17
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..033700c
--- /dev/null
+++ b/util.h
@@ -0,0 +1,6 @@
1/* See LICENSE file for copyright and license details. */
2
3#define MAX(A, B) ((A) > (B) ? (A) : (B))
4#define MIN(A, B) ((A) < (B) ? (A) : (B))
5
6void die(const char *errstr, ...);