summaryrefslogtreecommitdiff
path: root/graphics.c
blob: cb1127fb41b7e27477b80e0979e2baaa47bf9d1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>

#include "guifs.h"

Mousectl *mouse;
Keyboardctl *keyboard;
Channel *updatechan;
Channel *mkcolourchan;
Channel *newcolourchan;

void
drawgui(GuiElement *g)
{
	rlock(&g->lock);
	GuiSpec spec = guispecs[g->type];

	if(memcmp(&g->rect, &g->border, sizeof(Rectangle)) != 0 && Dx(g->border) > 0 && Dy(g->border) > 0){
		/* draw the border first */
		Image *bc = getprop(g, Pbordercolour).colour->image;
		Rectangle r;

		/* top part */
		r.min.x = g->border.min.x;
		r.min.y = g->border.min.y;
		r.max.x = g->border.max.x;
		r.max.y = g->rect.min.y;
		draw(screen, r, bc, nil, ZP);

		/* right part */
		r.min.x = g->rect.max.x;
		r.min.y = g->rect.min.y;
		r.max.x = g->border.max.x;
		r.max.y = g->border.max.y;
		draw(screen, r, bc, nil, ZP);

		/* bottom part */
		r.min.x = g->border.min.x;
		r.min.y = g->rect.max.y;
		r.max.x = g->border.max.x;
		r.max.y = g->border.max.y;
		draw(screen, r, bc, nil, ZP);

		/* left part */
		r.min.x = g->border.min.x;
		r.min.y = g->border.min.y;
		r.max.x = g->rect.min.x;
		r.max.y = g->border.max.y;
		draw(screen, r, bc, nil, ZP);
	}

	if(Dx(g->rect) > 0 && Dy(g->rect) > 0){
		spec.draw(g);

		for(int i = 0; i < g->nchildren; i++)
			drawgui(g->children[i]);
	}
	runlock(&g->lock);
}

void
drawcontainer(GuiElement *g)
{
	Image *bg = getprop(g, Pbackground).colour->image;
	draw(screen, g->rect, bg, nil, ZP);
}

Colour *
mkcolour(ulong c)
{
	Colour *res = nil;
	send(mkcolourchan, &c);
	recv(newcolourchan, &res);
	return res;
}

void
updategui(int new)
{
	/* Trigger a call to resized by sending a message */
	send(updatechan, &new);
}

void
resized(int new)
{
	if(new && getwindow(display, Refnone) < 0)
		sysfatal("can't reattach to window: %r");

	if(root != nil){
		layout(root, screen->r);
		drawgui(root);
		flushimage(display, 1);
	}
}

void
guiproc(void *)
{
	int i;
	ulong c;
	if(initdraw(nil, nil, "guifs") < 0)
		sysfatal("initdraw failed");

	if((mouse = initmouse(nil, screen)) == nil)
		sysfatal("initmouse failed");
	if((keyboard = initkeyboard(nil)) == nil)
		sysfatal("initkeyboard failed");

	enum {
		Aupdategui,
		Aresize,
		Amouse,
		Amkcolour,
		Aaltend,
	};
	Alt a[] = {
		[Aupdategui] =
			{updatechan, &i, CHANRCV},
		[Aresize] =
			{mouse->resizec, nil, CHANRCV},
		[Amouse] =
			{mouse->c, &mouse->Mouse, CHANRCV},
		[Amkcolour] =
			{mkcolourchan, &c, CHANRCV},
		[Aaltend] =
			{nil, nil, CHANEND},
	};

	while(1){
		int which = alt(a);

		switch(which){
		case Aupdategui:
			resized(i);
			break;
		case Aresize:
			resized(1);
			break;
		case Amouse:
			break;
		case Amkcolour:
			{
	
				Colour *col = emalloc(sizeof(Colour));
				col->image = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c);
				col->code = c;
				send(newcolourchan, &col);
			}
		}
	}
}

void
initgraphics(void)
{
	updatechan = chancreate(sizeof(int), 0);
	mkcolourchan = chancreate(sizeof(ulong), 0);
	newcolourchan = chancreate(sizeof(Colour *), 0);
	proccreate(guiproc, nil, mainstacksize);
	updategui(1);
}