53 border(screen, screen->r, Edge, red, ZP);
55 r = insetrect(screen->r, Edge);
56 draw(screen, r, tmp, nil, tmp->r.min);
57 flushimage(display, 1);
63 main(int argc, char *argv[])
71 if(initdraw(nil, nil, "lens") < 0){
72 fprint(2, "lens: initdraw failed: %r\n");
75 einit(Emouse|Ekeyboard);
76 red = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DRed);
77 chequer = allocimage(display, Rect(0, 0, 2, 2), GREY1, 1, DBlack);
78 draw(chequer, Rect(0, 0, 1, 1), display->white, nil, ZP);
79 draw(chequer, Rect(1, 1, 2, 2), display->white, nil, ZP);
80 lastp = divpt(addpt(screen->r.min, screen->r.max), 2);
81 screenfd = open("/dev/screen", OREAD);
83 fprint(2, "lens: can't open /dev/screen: %r\n");
86 if(read(screenfd, buf, sizeof buf) != sizeof buf){
87 fprint(2, "lens: can't read /dev/screen: %r\n");
90 chan = strtochan(buf);
91 d = chantodepth(chan);
93 fprint(2, "lens: can't handle screen format %11.11s\n", buf);
97 screenr.min.x = atoi(buf+1*12);
98 screenr.min.y = atoi(buf+2*12);
99 screenr.max.x = atoi(buf+3*12);
100 screenr.max.y = atoi(buf+4*12);
101 screenbuf = malloc(bypp*Dx(screenr)*Dy(screenr));
102 if(screenbuf == nil){
103 fprint(2, "lens: buffer malloc failed: %r\n");
128 showgrid = !showgrid;
146 case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case'0':
156 if(e.mouse.buttons & 1){
160 if(e.mouse.buttons & 4)
161 switch(emenuhit(3, &e.mouse, &menu)){
189 grid = allocimage(display, Rect(0, 0, m, m),
190 CHAN2(CGrey, 8, CAlpha, 8), 1, DTransparent);
192 draw(grid, Rect(0, 0, m, 1), chequer, nil, ZP);
193 draw(grid, Rect(0, 1, 1, m), chequer, nil, ZP);
201 if(new && getwindow(display, Refnone) < 0){
202 fprint(2, "lens: can't reattach to window: %r\n");
206 tmp = allocimage(display, Rect(0, 0, Dx(screen->r)-Edge, Dy(screen->r)-Edge+Maxmag), screen->chan, 0, DNofill);
208 fprint(2, "lens: allocimage failed: %r\n");
217 int x, y, xx, yy, dd, i;
223 dx = (Dx(tmp->r)+mag-1)/mag;
224 dy = (Dy(tmp->r)+mag-1)/mag;
225 xoff = lastp.x-Dx(tmp->r)/(mag*2);
226 yoff = lastp.y-Dy(tmp->r)/(mag*2);
234 if(yy+dd > Dy(screenr))
236 seek(screenfd, 5*12+bypp*yy*Dx(screenr), 0);
237 if(readn(screenfd, screenbuf+bypp*yy*Dx(screenr), bypp*Dx(screenr)*dd) != bypp*Dx(screenr)*dd){
238 fprint(2, "lens: can't read screen: %r\n");
244 if(yy>=0 && yy<Dy(screenr))
247 if(xx>=0 && xx<Dx(screenr)) /* snarf pixel at xx, yy */
248 for(i=0; i<bypp; i++)
249 sp[i] = screenbuf[bypp*(yy*Dx(screenr)+xx)+i];
251 sp[0] = sp[1] = sp[2] = sp[3] = 0;
253 for(xx=0; xx<mag; xx++)
254 if(x*mag+xx < tmp->r.max.x)
255 for(i=0; i<bypp; i++)
256 out[(x*mag+xx)*bypp+i] = sp[i];
259 memset(out, 0, bypp*Dx(tmp->r));
260 for(yy=0; yy<mag && y*mag+yy<Dy(tmp->r); yy++){
262 if(loadimage(tmp, Rect(0, y*mag+yy, Dx(tmp->r), y*mag+yy+1), out, bypp*Dx(tmp->r)) != bypp*Dx(tmp->r)){
267 if (showgrid && mag && grid)
268 draw(tmp, tmp->r, grid, nil, mulpt(Pt(xoff, yoff), mag));