basic draw.c structure d456617f
anselm@garbe.us · 2012-11-18 12:04 3 file(s) · +149 −6
Makefile +1 −1
3 3
4 4
include config.mk
5 5
6 -
SRC = dwm.c
6 +
SRC = draw.c dwm.c
7 7
OBJ = ${SRC:.c=.o}
8 8
9 9
all: options dwm
draw.c (added) +139 −0
1 +
/* See LICENSE file for copyright and license details. */
2 +
#include <stdlib.h>
3 +
#include <X11/Xlib.h>
4 +
5 +
#include "draw.h"
6 +
7 +
Draw *
8 +
draw_create(Display *dpy, Window win, unsigned int w, unsigned int h) {
9 +
	Draw *draw = (Draw *)calloc(1, sizeof(Draw));
10 +
	draw->w = w;
11 +
	draw->h = h;
12 +
	/* TODO: drawable creation */
13 +
	/* TODO: gc allocation */
14 +
	return draw;
15 +
}
16 +
17 +
void
18 +
draw_resize(Draw *draw, unsigned int w, unsigned int h) {
19 +
	if(!draw)
20 +
		return;
21 +
	draw->w = w;
22 +
	draw->h = h;
23 +
	/* TODO: resize drawable */
24 +
}
25 +
26 +
void
27 +
draw_free(Draw *draw) {
28 +
	/* TODO: deallocate DDCs */
29 +
	/* TODO: deallocate drawable */
30 +
	free(draw);
31 +
}
32 +
33 +
DDC *
34 +
dc_create(Draw *draw) {
35 +
	DDC *dc = (DDC *)calloc(1, sizeof(DDC));
36 +
	dc->draw = draw;
37 +
	dc->next = draw->dc;
38 +
	draw->dc = dc;
39 +
	return dc;
40 +
}
41 +
42 +
void
43 +
dc_free(DDC *dc) {
44 +
	DDC **tdc;
45 +
46 +
	if(!dc)
47 +
		return;
48 +
	/* remove from dc list */
49 +
	for(tdc = &dc->draw->dc; *tdc && *tdc != dc; tdc = &(*tdc)->next);
50 +
	*tdc = dc->next;
51 +
	/* TODO: deallocate any resources of this dc, if needed */
52 +
	free(dc);
53 +
}
54 +
55 +
Fnt *
56 +
font_create(const char *fontname) {
57 +
	Fnt *font = (Fnt *)calloc(1, sizeof(Fnt));
58 +
	/* TODO: allocate actual font */
59 +
	return font;
60 +
}
61 +
62 +
void
63 +
font_free(Fnt *font) {
64 +
	if(!font)
65 +
		return;
66 +
	/* TODO: deallocate any font resources */
67 +
	free(font);
68 +
}
69 +
70 +
Col *
71 +
col_create(const char *colname) {
72 +
	Col *col = (Col *)calloc(1, sizeof(Col));
73 +
	/* TODO: allocate color */
74 +
	return col;
75 +
}
76 +
77 +
void
78 +
col_free(Col *col) {
79 +
	if(!col)
80 +
		return;
81 +
	/* TODO: deallocate any color resource */
82 +
	free(col);
83 +
}
84 +
85 +
void
86 +
dc_setfont(DDC *dc, Fnt *font) {
87 +
	if(!dc || !font)
88 +
		return;
89 +
	dc->font = font;
90 +
}
91 +
92 +
void
93 +
dc_setfg(DDC *dc, Col *col) {
94 +
	if(!dc || !col) 
95 +
		return;
96 +
	dc->fg = col;
97 +
}
98 +
99 +
void
100 +
dc_setbg(DDC *dc, Col *col) {
101 +
	if(!dc || !col)
102 +
		return;
103 +
	dc->bg = col;
104 +
}
105 +
106 +
void
107 +
dc_setfill(DDC *dc, Bool fill) {
108 +
	if(!dc)
109 +
		return;
110 +
	dc->fill = fill;
111 +
}
112 +
113 +
void
114 +
dc_drawrect(DDC *dc, int x, int y, unsigned int w, unsigned int h) {
115 +
	if(!dc)
116 +
		return;
117 +
	/* TODO: draw the rectangle */
118 +
}
119 +
120 +
void
121 +
dc_drawtext(DDC *dc, int x, int y, const char *text) {
122 +
	if(!dc)
123 +
		return;
124 +
	/* TODO: draw the text */
125 +
}
126 +
127 +
void
128 +
dc_map(DDC *dc, int x, int y, unsigned int w, unsigned int h) {
129 +
	if(!dc)
130 +
		return;
131 +
	/* TODO: map the dc contents in the region */
132 +
}
133 +
134 +
void
135 +
dc_getextents(DDC *dc, const char *text, TextExtents *extents) {
136 +
	if(!dc || !extents)
137 +
		return;
138 +
	/* TODO: get extents */
139 +
}
draw.h +9 −5
1 1
/* See LICENSE file for copyright and license details. */
2 2
3 +
typedef struct _DDC DDC;
4 +
3 5
/* X11 types - begin */
6 +
typedef struct _XDraw Draw;
4 7
struct _XDraw {
5 8
	unsigned int w, h;
6 9
	Display *dpy;
7 10
	Drawable drawable;
8 11
	GC gc;
12 +
	DDC *dc;
9 13
};
10 -
typedef struct _XDraw Draw;
11 14
12 15
struct _XCol {
13 16
	unsigned long rgb;
24 27
typedef struct _XFont Fnt;
25 28
/* X11 types - end */
26 29
27 -
typedef struct {
30 +
struct _DDC {
28 31
	Draw *draw;
29 32
	Col *fg;
30 33
	Col *bg;
31 34
	Fnt *font;
32 35
	Bool fill;
33 -
} DDC;
36 +
	DDC *next;
37 +
};
34 38
35 39
typedef struct {
36 40
	unsigned int w;
60 64
61 65
/* Drawing context manipulation */
62 66
void dc_setfont(DDC *dc, Fnt *font);
63 -
void dc_setfg(DDC *dc, Col col);
64 -
void dc_setbg(DDC *dc, Col col);
67 +
void dc_setfg(DDC *dc, Col *col);
68 +
void dc_setbg(DDC *dc, Col *col);
65 69
void dc_setfill(DDC *dc, Bool fill);
66 70
67 71
/* Drawing functions */