~amavect/makeu

ref: 4a0025afeb6d1738f64ec587b7862cfb66e97224 makeu/makeu.c -rw-r--r-- 8.2 KiB
4a0025afamavect Rename dogetwindow -> egetwindow, remove unnecessary call. 1 year, 10 months ago
                                                                                
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* Amavect! */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <cursor.h>
#include <thread.h>
#include <keyboard.h>
#include <mouse.h>
#include "elementile.h"
#include "com.h"
#include "components.h"

extern Channel* init9pfs(char*, char*, int);

void mapevent(HSVd);
void pickevent(ulong);
void redevent(double);
void greenevent(double);
void blueevent(double);
void hueevent(double);
void satevent(double);
void valevent(double);
void alphaevent(double);

int pflag = 0;

/* 
 * Alternative color models are needed because they do not map
 * injectively to each other.
 */
ulong color = 0xFFFFFFFF; /* premultiplied color */
RGB rgb = {0xFF, 0xFF, 0xFF}; /* non-premultiplied color */
ulong alpha = 0xFF;
HSVd hsvd = {1.0, 0.0, 1.0};

HSVmap hsva = {{1.0, 0.0, 1.0}, mapevent, nil};
Screenpick spa = {0xFFFFFFFF, nil, pickevent};
Slider ra = {0xFF, 0, 0xFF, 1, 0, "%2φ", redevent};
Slider ga = {0xFF, 0, 0xFF, 1, 0, "%2φ", greenevent};
Slider ba = {0xFF, 0, 0xFF, 1, 0, "%2φ", blueevent};
Slider ha = {0, 0, 6, 1.0/255.0, 0, "%1.2f", hueevent};
Slider sa = {0, 0, 1, 1.0/255.0, 0, "%1.2f", satevent};
Slider va = {0, 0, 1, 1.0/255.0, 0, "%1.2f", valevent};
Slider aa = {0xFF, 0, 0xFF, 1, 1, "%2φ", alphaevent};

Elementile hsvmp = {&hsva, hsvmapinit, hsvmapresize, hsvmapupdate, hsvmapmouse, hsvmapkeyboard, hsvmapfree};
Elementile scrpk = {&spa, screenpickinit, screenpickresize, screenpickupdate, screenpickmouse, screenpickkeyboard};
Elementile rsldr = {&ra, sliderinit, sliderresize, sliderupdate, slidermouse, sliderkeyboard};
Elementile gsldr = {&ga, sliderinit, sliderresize, sliderupdate, slidermouse, sliderkeyboard};
Elementile bsldr = {&ba, sliderinit, sliderresize, sliderupdate, slidermouse, sliderkeyboard};
Elementile hsldr = {&ha, sliderinit, sliderresize, sliderupdate, slidermouse, sliderkeyboard};
Elementile ssldr = {&sa, sliderinit, sliderresize, sliderupdate, slidermouse, sliderkeyboard};
Elementile vsldr = {&va, sliderinit, sliderresize, sliderupdate, slidermouse, sliderkeyboard};
Elementile asldr = {&aa, sliderinit, sliderresize, sliderupdate, slidermouse, sliderkeyboard};

Guipart parts[8];
Elementile tree[8];

Guipart parts[8] = {
	{Vdiv, 1, 1.0, &hsvmp, &tree[1]},
	{Hdiv, 1, 0.3, &scrpk, &tree[2]},
	{Hdiv, 1, 0.95, &tree[3], &asldr},
	{Hdiv, 1, 0.5, &tree[4], &tree[6]},
	{Vdiv, 1, 1.0/3.0, &rsldr, &tree[5]},
	{Vdiv, 1, 0.5, &gsldr, &bsldr},
	{Vdiv, 1, 1.0/3.0, &hsldr, &tree[7]},
	{Vdiv, 1, 0.5, &ssldr, &vsldr}
};

Elementile tree[8] = {
	{&parts[0], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard},
	{&parts[1], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard},
	{&parts[2], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard},
	{&parts[3], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard},
	{&parts[4], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard},
	{&parts[5], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard},
	{&parts[6], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard},
	{&parts[7], guipartinit, guipartresize, guipartupdate, guipartmouse, guipartkeyboard}
};

Elementile *root = &tree[0];

char *buttons3[] = {"Send", "Exit", nil};
Menu menu3 = {buttons3};

#pragma varargck type "φ" double
/* 
 * lazy man's double to hex fmt
 * φ for φloat
 */
int
φfmt(Fmt *f)
{
	double d;
	char fmt[64];
	d = va_arg(f->args, double);
	snprint(fmt, 64, "%%%dlX", f->width); //right justification
	return fmtprint(f, fmt, (long)d);
}

/* resolve functions consolidate differences of global color models */
void
resolvecolor(void)
{
	rgb = colortorgb(color);
	alpha = color & 0xFF;
	hsvd = rgbtohsvd(rgb);
}

void
resolvergba(void)
{
	color = setalpha(rgbtocolor(rgb), alpha);
	hsvd = rgbtohsvd(rgb);
}

void
resolvehsvd(void)
{
	rgb = hsvdtorgb(hsvd);
	color = setalpha(rgbtocolor(rgb), alpha);
}

/* common event actions */
void
setmap(void)
{
	hsva.hsv = hsvd;
	hsvmp.update(&hsvmp);
}

void
setpick(void)
{
	spa.color = color;
	scrpk.update(&scrpk);
}

void
sethsvsliders(void)
{
	ha.val = hsvd.hue;
	hsldr.update(&hsldr);
	sa.val = hsvd.sat;
	ssldr.update(&ssldr);
	va.val = hsvd.val;
	vsldr.update(&vsldr);
}

void
setrgbsliders(void)
{
	ra.val = rgb.red;
	rsldr.update(&rsldr);
	ga.val = rgb.green;
	gsldr.update(&gsldr);
	ba.val = rgb.blue;
	bsldr.update(&bsldr);
}

void
setsliders(void)
{
	setrgbsliders();
	sethsvsliders();
}

/* events */
void
mapevent(HSVd h)
{
	hsvd = h;
	resolvehsvd();
	setpick();
	setsliders();
}

void
pickevent(ulong c)
{
	color = c;
	resolvecolor();
	setmap();
	setsliders();
	aa.val = alpha;
	asldr.update(&asldr);
}

void
allevent(ulong c)
{
	color = c;
	pickevent(c);
	setpick();
}

void
commonrgb(void)
{
	resolvergba();
	setmap();
	setpick();
	sethsvsliders();
}

void
redevent(double val)
{
	rgb.red = (ulong)(val + 0.5);
	commonrgb();
}

void
greenevent(double val)
{
	rgb.green = (ulong)(val + 0.5);
	commonrgb();
}

void
blueevent(double val)
{
	rgb.blue = (ulong)(val + 0.5);
	commonrgb();
}

void
commonhsv(void)
{
	resolvehsvd();
	setmap();
	setpick();
	setrgbsliders();
}

void
hueevent(double val)
{
	hsvd.hue = val;
	commonhsv();
}

void
satevent(double val)
{
	hsvd.sat = val;
	commonhsv();
}

void
valevent(double val)
{
	hsvd.val = val;
	commonhsv();
}

void
alphaevent(double val)
{
	alpha = (ulong)(val + 0.5);
	resolvergba();
	setpick();
}


vlong startt, stopt;
#define STARTTIME() startt = nsec()
#define STOPTIME() stopt = nsec(); \
	fprint(2, "%lld\n", stopt-startt)

void
egetwindow(void)
{
	if(getwindow(display, Refnone) < 0)
		sysfatal("Cannot reconnect to display: %r");
	draw(screen, screen->r, display->black, nil, ZP);
}

void
usage(void)
{
	fprint(2, "usage: %s [-p] [-c 0xRRGGBBAA]\n", argv0);
	threadexitsall("usage");
}


void
threadmain(int argc, char **argv)
{
	Keyboardctl *kctl;
	Rune r;
	Mousectl *mctl;
	Mouse m;
	ulong incolor;
	Channel *fsc;
	char *mtpt, *srv;
	int c9p;
	
	mtpt = nil;
	srv = nil;
	c9p = 0;
	incolor = 0xFFFFFFFF;
	ARGBEGIN{
	case 'p':
		pflag++;
		break;
	case 'c':
		incolor = strtocolor(EARGF(usage()));
		if(incolor == DNotacolor)
			incolor = 0xFFFFFFFF;
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 's':
		srv = EARGF(usage());
		break;
	case 'D':
		c9p++;
		break;
	default:
		usage();
	}ARGEND;

	fmtinstall(L'φ', φfmt);
	
	if(initdraw(nil, nil, argv0) < 0)
		sysfatal("%r");
	if((mctl = initmouse(nil, screen)) == nil)
		sysfatal("%r");
	if((kctl = initkeyboard(nil)) == nil)
		sysfatal("%r");
	fsc = init9pfs(mtpt, srv, c9p);
	spa.mctl = mctl;
	hsva.mctl = mctl;
	
	color = incolor;
	alpha = color & 0xFF;
	rgb = colortorgb(unsetalpha(incolor));
	hsvd = rgbtohsvd(rgb);
	
	hsva.hsv = hsvd;
	spa.color = color;
	ra.val = rgb.red;
	ga.val = rgb.green;
	ba.val = rgb.blue;
	ha.val = hsvd.hue;
	sa.val = hsvd.sat;
	va.val = hsvd.val;
	aa.val = alpha;
	
	root->init(root);
	root->resize(root, screen->r);
	flushimage(display, 1);
	
	enum { MOUSE, RESIZE, KEYS, FS, NONE };
	Alt alts[] = {
		[MOUSE] =  {mctl->c, &m, CHANRCV},
		[RESIZE] =  {mctl->resizec, nil, CHANRCV},
		[KEYS] = {kctl->c, &r, CHANRCV},
		[FS] = {fsc, &incolor, CHANRCV},
		[NONE] =  {nil, nil, CHANEND}
	};
	
	/* DEBUG frame counter */
	/*
	Rectangle rekt;
	ulong frames = 0;
	char ftext[10];
	*/
	STARTTIME();
	for(;;){
		/* DEBUG frame counter */
		/*
		snprint(ftext, sizeof(ftext), "%uld", (ulong)frames);
		rekt = Rpt(screen->r.min, addpt(screen->r.min, stringsize(display->defaultfont, ftext)));
		draw(screen, rekt, display->white, nil, ZP);
		string(screen, screen->r.min, display->black, ZP, display->defaultfont, ftext);
		frames++;
		flushimage(display, 1);
		*/
		
		switch(alt(alts)){
		case MOUSE:
			if(m.buttons == 4){
				switch(menuhit(3, mctl, &menu3, nil)){
				case 0:
					print("0x%08ulX\n", color);
					if(pflag == 0)
						threadexitsall(nil);
					break;
				case 1:
					threadexitsall(nil);
					break;
				}
				break;
			}
			if(root->mouse(root, m) == 1)
				flushimage(display, 1);
			break;
		case RESIZE:
			egetwindow();
			root->resize(root, screen->r);
			flushimage(display, 1);
			break;
		case KEYS:
			if(r == Kdel)
				threadexitsall(nil);
			break;
		case FS:
			if(incolor == color)
				break;
			allevent(incolor);
			flushimage(display, 1);
			break;
		case NONE:
			print("I'm a woodchuck, not a woodchucker! (thanks for playing)\n");
			break;
		}
	}
}