#include #include #include #include "getscr.h" /* * allocates temporary storage for and reads * /dev/screen... modified from of lens.c */ uchar * getscr(void) { char buf[5*12]; ulong chan; uchar *screenbuf; int d, screenfd; screenfd = open("/dev/screen", OREAD); if(screenfd < 0) sysfatal("can't open /dev/screen: %r"); if(read(screenfd, buf, sizeof buf) != sizeof buf) sysfatal("can't read /dev/screen: %r"); chan = strtochan(buf); d = chantodepth(chan); if(d < 8) sysfatal("can't handle screen format %11.11s", buf); bypp = d/8; screenr.min.x = atoi(buf+1*12); screenr.min.y = atoi(buf+2*12); screenr.max.x = atoi(buf+3*12); screenr.max.y = atoi(buf+4*12); screenbuf = malloc(bypp*Dx(screenr)*Dy(screenr)); if(screenbuf == nil) sysfatal("screen buffer malloc failed: %r"); if(readn(screenfd, screenbuf, bypp*Dx(screenr)*Dy(screenr)) != bypp*Dx(screenr)*Dy(screenr)) sysfatal("can't read screen: %r"); return screenbuf; } /* * squeeze the screenshot to fit in the current * window */ Image * squeeze(uchar *buf) { int x, y, xx, yy, i; float dx, dy; Image *tmp; uchar *out; if(buf == nil) return nil; tmp = allocimage(display, screen->r, screen->chan, 0, DBlack); if(tmp == nil) sysfatal("can't allocate temp image"); dx = (float)Dx(screenr)/Dx(tmp->r); dy = (float)Dy(screenr)/Dy(tmp->r); out = (uchar *)malloc(bypp*Dx(tmp->r)*Dy(tmp->r)); if(out == nil) sysfatal("can't allocate temp memory"); for(y=0; yr); y++){ for(x=0; xr); x++){ for(i=0; ir)+x) + i] = buf[bypp*(yy*Dx(screenr)+xx)+i]; } } } if(loadimage(tmp, tmp->r, out, bypp*Dx(tmp->r)*Dy(tmp->r)) != bypp*Dx(tmp->r)*Dy(tmp->r)){ sysfatal("loadimage"); } return(tmp); }