52 border(screen, screen->r, Edge, red, ZP);
54 r = insetrect(screen->r, Edge);
55 draw(screen, r, tmp, nil, tmp->r.min);
56 flushimage(display, 1);
62 main(int argc, char *argv[])
70 if(initdraw(nil, nil, "lens") < 0){
71 fprint(2, "lens: initdraw failed: %r\n");
74 einit(Emouse|Ekeyboard);
75 red = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DRed);
76 chequer = allocimage(display, Rect(0, 0, 2, 2), GREY1, 1, DBlack);
77 draw(chequer, Rect(0, 0, 1, 1), display->white, nil, ZP);
78 draw(chequer, Rect(1, 1, 2, 2), display->white, nil, ZP);
79 lastp = divpt(addpt(screen->r.min, screen->r.max), 2);
80 screenfd = open("/dev/screen", OREAD);
82 fprint(2, "lens: can't open /dev/screen: %r\n");
85 if(read(screenfd, buf, sizeof buf) != sizeof buf){
86 fprint(2, "lens: can't read /dev/screen: %r\n");
89 chan = strtochan(buf);
90 d = chantodepth(chan);
92 fprint(2, "lens: can't handle screen format %11.11s\n", buf);
96 screenr.min.x = atoi(buf+1*12);
97 screenr.min.y = atoi(buf+2*12);
98 screenr.max.x = atoi(buf+3*12);
99 screenr.max.y = atoi(buf+4*12);
100 screenbuf = malloc(bypp*Dx(screenr)*Dy(screenr));
101 if(screenbuf == nil){
102 fprint(2, "lens: buffer malloc failed: %r\n");
127 showgrid = !showgrid;
145 case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case'0':
155 if(e.mouse.buttons & 1){
159 if(e.mouse.buttons & 4)
160 switch(emenuhit(3, &e.mouse, &menu)){
188 grid = allocimage(display, Rect(0, 0, m, m),
189 CHAN2(CGrey, 8, CAlpha, 8), 1, DTransparent);
191 draw(grid, Rect(0, 0, m, 1), chequer, nil, ZP);
192 draw(grid, Rect(0, 1, 1, m), chequer, nil, ZP);
200 if(new && getwindow(display, Refnone) < 0){
201 fprint(2, "lens: can't reattach to window: %r\n");
205 tmp = allocimage(display, Rect(0, 0, Dx(screen->r)-Edge, Dy(screen->r)-Edge+Maxmag), screen->chan, 0, DNofill);
207 fprint(2, "lens: allocimage failed: %r\n");
216 int x, y, xx, yy, dd, i;
222 dx = (Dx(tmp->r)+mag-1)/mag;
223 dy = (Dy(tmp->r)+mag-1)/mag;
224 xoff = lastp.x-Dx(tmp->r)/(mag*2);
225 yoff = lastp.y-Dy(tmp->r)/(mag*2);
233 if(yy+dd > Dy(screenr))
235 seek(screenfd, 5*12+bypp*yy*Dx(screenr), 0);
236 if(readn(screenfd, screenbuf+bypp*yy*Dx(screenr), bypp*Dx(screenr)*dd) != bypp*Dx(screenr)*dd){
237 fprint(2, "lens: can't read screen: %r\n");
243 if(yy>=0 && yy<Dy(screenr))
246 if(xx>=0 && xx<Dx(screenr)) /* snarf pixel at xx, yy */
247 for(i=0; i<bypp; i++)
248 sp[i] = screenbuf[bypp*(yy*Dx(screenr)+xx)+i];
250 sp[0] = sp[1] = sp[2] = sp[3] = 0;
252 for(xx=0; xx<mag; xx++)
253 if(x*mag+xx < tmp->r.max.x)
254 for(i=0; i<bypp; i++)
255 out[(x*mag+xx)*bypp+i] = sp[i];
258 memset(out, 0, bypp*Dx(tmp->r));
259 for(yy=0; yy<mag && y*mag+yy<Dy(tmp->r); yy++){
261 if(loadimage(tmp, Rect(0, y*mag+yy, Dx(tmp->r), y*mag+yy+1), out, bypp*Dx(tmp->r)) != bypp*Dx(tmp->r)){
266 if (showgrid && mag && grid)
267 draw(tmp, tmp->r, grid, nil, mulpt(Pt(xoff, yoff), mag));