~amavect/makeu

ref: b6f6785f804b8f37745fdeff16f2e707c3a25664 makeu/screenpick.c -rw-r--r-- 5.4 KiB
b6f6785famavect rename com.h to more standard fns.h 1 year, 3 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
/* 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 "fns.h"
#include "components.h"

static
Cursor icursethee = {
	{-7, -7},
	{0x07, 0xC0, 0x1F, 0xF0, 0x3F, 0xF8, 0x7F, 0xFC,
	 0x7B, 0xBC, 0xF0, 0x1E, 0xF8, 0x3E, 0xF8, 0x3E,
	 0xF8, 0x3E, 0xF0, 0x1E, 0x7B, 0xBC, 0x7F, 0xFC,
	 0x3F, 0xF8, 0x1F, 0xF0, 0x07, 0xC0, 0x00, 0x00, },
	{0x00, 0x00, 0x07, 0xC0, 0x1F, 0xF0, 0x39, 0x38,
	 0x30, 0x18, 0x60, 0x0C, 0x60, 0x0C, 0x70, 0x1C,
	 0x60, 0x0C, 0x60, 0x0C, 0x30, 0x18, 0x39, 0x38,
	 0x1F, 0xF0, 0x07, 0xC0, 0x00, 0x00, 0x00, 0x00, }
};

/* Returns the big endian color of the 1x1 pixel image */
static
ulong
getpixelcolor(Screenpick *sp)
{
	uchar data[4];
	Image *pixel;
	pixel = sp->pixel;
	unloadimage(pixel, pixel->r, data, sizeof(data));
	return (data[3]<<24) | (data[2]<<16) | (data[1]<<8) | 0xFF;
}

/* Regenerate image */
static
void
redraw(Screenpick *sp)
{
	char hex[9];
	Point strsize, pfont, pfont2;
	Rectangle r, r2, r3;
	int i, dx, dy;
	
	r = sp->r;
	dx = Dx(r), dy = Dy(r);
	snprint(hex, 9, "%08ulX", sp->color);
	strsize = stringsize(display->defaultfont, hex);
	pfont = addpt(divpt(subpt(subpt(r.max, r.min), strsize), 2), r.min);
	pfont2 = pfont;
	pfont.y -= 3*strsize.y/2;
	pfont2.y -= strsize.y/2;
	
	draw(screenbuf, r, display->black, nil, ZP);
	for(i = 0; i < 2; i++){
		r2.min.x = dx*i/2 + r.min.x;
		r2.min.y = dy/2 + r.min.y;
		r2.max.x = r2.min.x + dx/4;
		r2.max.y = r2.min.y + dy/4;
		r3.min = r2.max;
		r3.max.x = dx*(i+1)/2 + r.min.x;
		r3.max.y = r.max.y;
		draw(screenbuf, r2, display->white, nil, ZP);
		draw(screenbuf, r3, display->white, nil, ZP);
	}
	draw(screenbuf, r, sp->pixel, nil, ZP);
	string(screenbuf, pfont, display->black, ZP, display->defaultfont, hex);
	string(screenbuf, pfont2, display->white, ZP, display->defaultfont, hex);
}

/*
 * Open /dev/screen for reading. This is done for optimization by reading one line at a time.
 * Then, calculate minimum size using the default font.
 */
Point
screenpickinit(Elementile *e)
{
	Screenpick *sp;
	Point strsize;
	char hex[9];
	sp = e->aux;
	sp->state = 0;
	sp->screenfd = open("/dev/screen", OREAD);
	if(sp->screenfd < 0)
		sysfatal("%r");
	sp->screenline = nil;
	sp->linesize = 0;
	sp->linebuf = nil;
	sp->pixel = allocimage(display, Rect(0,0,1,1), RGBA32, 1, sp->color);
	if(sp->pixel == nil)
		sysfatal("download more ram plz: %r");
	snprint(hex, sizeof(hex), "%08ulX", setalpha(sp->color, sp->color & 0xFF));
	strsize = stringsize(display->defaultfont, hex);
	strsize.y *= 4;
	return strsize;
}

/*
 * Update the size, refresh screenline in case of screen resolution resize, and redraw.
 */
void
screenpickresize(Elementile *e, Rectangle r)
{
	Screenpick *sp;
	ulong chantype;
	char chanstr[12], minxstr[12], minystr[12], maxxstr[12], maxystr[12];
	Rectangle rect;
	
	sp = e->aux;
	sp->r = r;
	
	freeimage(sp->screenline);
	free(sp->linebuf);
	
	/* 
	 * Read screenfd directly so a single scanline can be read.
	 * Potential for a crash if /dev/screen is bind-ed with a malicious file.
	 * Potential for usability on named images (draw(3)) or other /dev/wsys/window.
	 * Need a second opinion.
	 */
	if(pread(sp->screenfd, chanstr, 12, 0) < 12)
		sysfatal("%r");
	if(pread(sp->screenfd, minxstr, 12, 12) < 12)
		sysfatal("%r");
	if(pread(sp->screenfd, minystr, 12, 2*12) < 12)
		sysfatal("%r");
	if(pread(sp->screenfd, maxxstr, 12, 3*12) < 12)
		sysfatal("%r");
	if(pread(sp->screenfd, maxystr, 12, 4*12) < 12)
		sysfatal("%r");
	chanstr[11] = 0;
	minxstr[11] = 0;
	minystr[11] = 0;
	maxxstr[11] = 0;
	maxystr[11] = 0;
	chantype = strtochan(chanstr);
	rect.min.x = (int)strtol(minxstr, nil, 0);
	rect.min.y = (int)strtol(minystr, nil, 0);
	rect.max.x = (int)strtol(maxxstr, nil, 0);
	rect.max.y = (int)strtol(maxystr, nil, 0);
	
	sp->screenline = allocimage(display, Rect(0,0,rect.max.x,1), chantype, 0, DWhite);
	sp->linesize = bytesperline(rect, chantodepth(chantype));
	sp->linebuf = malloc(sp->linesize);
	if(sp->screenline == nil || sp->linebuf == nil)
		sysfatal("download more ram plz: %r");
	
	redraw(sp);
}

/*
 * Load and redraw the current color.
 */
int
screenpickupdate(Elementile *e)
{
	Screenpick *sp;
	uchar col[4];
	sp = e->aux;
	colortoarray(col, sp->color);
	loadimage(sp->pixel, sp->pixel->r, col, 4);
	redraw(sp);
	return 1;
}

/*
 * Read a single scanline (for speed) and load it into the new pixel. 
 * However, do not send an update until the mouse is released.
 * When the mouse is released, get the pixel at that x location.
 */
int
screenpickmouse(Elementile *e, Mouse m)
{
	Screenpick *sp;
	Rectangle newpixr;
	Image *pixel, *screenline;
	sp = e->aux;
again:	
	switch(sp->state){
	case 0:
		if(m.buttons & 1){
			setcursor(sp->mctl, &icursethee);
			sp->state = 1;
			goto again;
		}
		break;
	case 1:
		pread(sp->screenfd, sp->linebuf, sp->linesize, m.xy.y*sp->linesize+12*5);
		pixel = sp->pixel;
		screenline = sp->screenline;
		newpixr = sp->r;
		newpixr.min = addpt(newpixr.min, Pt(Dx(newpixr)/2,Dy(newpixr)/2));
		loadimage(screenline, screenline->r, sp->linebuf, sp->linesize);
		draw(pixel, pixel->r, screenline, nil, Pt(m.xy.x, 0));
		draw(screenbuf, newpixr, pixel, nil, ZP);
		if(m.buttons == 0){
			setcursor(sp->mctl, nil);
			sp->color = getpixelcolor(sp);
			redraw(sp);
			sp->onchange(sp->color);
			sp->state = 0;
		}
		return 1;
		break;
	}
	return 0;
}

/*
 * insert functionality here
 */
int
screenpickkeyboard(Elementile*, Rune)
{
	return 0;
}