9 typedef struct Page Page;
40 char pagespool[] = "/tmp/pagespool.";
49 char *pagemenugen(int i);
84 {0xff, 0x80, 0xff, 0x80, 0xff, 0x00, 0xfe, 0x00,
85 0xff, 0x00, 0xff, 0x80, 0xff, 0xc0, 0xef, 0xe0,
86 0xc7, 0xf0, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0xc0,
87 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, },
88 {0x00, 0x00, 0x7f, 0x00, 0x7e, 0x00, 0x7c, 0x00,
89 0x7e, 0x00, 0x7f, 0x00, 0x6f, 0x80, 0x47, 0xc0,
90 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xe0, 0x00, 0x40,
91 0x00, 0x00, 0x01, 0xb6, 0x01, 0xb6, 0x00, 0x00, }
94 void showpage(Page *);
95 void drawpage(Page *);
96 Point pagesize(Page *);
99 addpage(Page *up, char *label, int (*popen)(Page *), void *pdata, int fd)
103 p = mallocz(sizeof(*p), 1);
104 p->label = strdup(label);
119 up->down = up->tail = p;
127 if(up && current == up)
133 resizewin(Point size)
137 if((wctl = open("/dev/wctl", OWRITE)) < 0)
140 size = addpt(size, Pt(Borderwidth*2, Borderwidth*2));
141 fprint(wctl, "resize -dx %d -dy %d\n", size.x, size.y);
146 createtmp(ulong id, char *pfx)
150 snprint(nam, sizeof nam, "%s%s%.12d%.8lux", pagespool, pfx, getpid(), id ^ 0xcafebabe);
151 return create(nam, OEXCL|ORCLOSE|ORDWR, 0600);
155 catchnote(void *, char *msg)
157 if(strstr(msg, "sys: write on closed pipe"))
159 if(strstr(msg, "hangup"))
161 if(strstr(msg, "alarm"))
167 pipeline(int fd, char *fmt, ...)
169 char buf[128], *argv[4];
178 switch(rfork(RFPROC|RFFDG|RFREND|RFNOWAIT)){
192 vsnprint(buf, sizeof buf, fmt, arg);
199 exec("/bin/rc", argv);
200 sysfatal("exec: %r");
216 if((fd = dup(p->fd, -1)) < 0){
224 pipeline(fd, "%s", (char*)p->data);
227 * dont keep the file descriptor arround if it can simply
230 fd2path(p->fd, nam, sizeof(nam));
231 if(strncmp(nam, pagespool, strlen(pagespool))){
234 p->data = strdup(nam);
244 char mnt[32], cmd[NPATH], *argv[4];
246 snprint(mnt, sizeof(mnt), "/n/tapefs.%.12d%.8lux", getpid(), (ulong)p);
247 switch(rfork(RFREND|RFPROC|RFFDG)){
256 snprint(cmd, sizeof(cmd), "%s -m %s /fd/0", p->data, mnt);
259 exec("/bin/rc", argv);
260 sysfatal("exec: %r");
266 p->data = strdup(mnt);
271 typedef struct Ghost Ghost;
290 switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
299 fprint(gs->pin, "%s DoPDFPage\n"
302 "dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring "
303 "flushfile\n", p->label);
304 while((n = read(gs->pdat, buf, sizeof buf)) > 0){
305 if(memcmp(buf, "THIS IS NOT AN INFERNO BITMAP\n", 30) == 0)
309 if(write(pfd[1], buf, n) != n){
322 infernobithdr(char *buf, int n)
325 if(memcmp(buf, "compressed\n", 11) == 0)
327 if(strtochan((char*)buf))
329 if(memcmp(buf, " ", 10) == 0 &&
330 '0' <= buf[10] && buf[10] <= '9' &&
340 int n, i, pdf, ifd, ofd, pin[2], pout[2], pdat[2];
341 char buf[NBUF], nam[32], *argv[16];
347 if(read(ifd, buf, 5) != 5)
350 if(memcmp(buf, "%PDF-", 5) == 0)
352 p->text = strdup(p->label);
371 switch(rfork(RFREND|RFPROC|RFFDG|RFNOWAIT)){
378 if(dup(pout[1], 1)<0)
388 if(dup(pdat[1], 3)<0)
402 pipeline(4, "%s", (char*)p->data);
406 argv[2] = "-sDEVICE=plan9";
407 argv[3] = "-sOutputFile=/fd/3";
409 argv[5] = pdf ? "-dDELAYSAFER" : "-dSAFER";
411 argv[7] = "-dTextAlphaBits=4";
412 argv[8] = "-dGraphicsAlphaBits=4";
413 snprint(buf, sizeof buf, "-r%d", ppi);
415 argv[10] = "-dDOINTERPOLATE";
416 argv[11] = pdf ? "-" : "/fd/4";
418 exec("/bin/gs", argv);
419 sysfatal("exec: %r");
430 "/PAGEOUT (/fd/1) (w) file def\n"
431 "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n"
435 "/PDFSave null def\n"
436 "/DSCPageCount 0 def\n"
437 "/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n"
439 "GS_PDF_ProcSet begin\n"
441 "(/fd/4) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n"
443 "pdfpagecount PAGE==\n";
446 if(write(pin[0], prolog, n) != n)
448 if((n = read(pout[0], buf, sizeof(buf)-1)) < 0)
456 gs = mallocz(sizeof(*gs), 1);
461 snprint(nam, sizeof nam, "%d", i);
462 addpage(p, nam, popenpdf, gs, -1);
465 /* keep ghostscript arround */
470 while((n = read(pdat[0], buf, sizeof(buf))) >= 0){
471 if(ofd >= 0 && (n <= 0 || infernobithdr(buf, n))){
472 snprint(nam, sizeof nam, "%d", i);
473 addpage(p, nam, popenconv, nil, ofd);
479 snprint(nam, sizeof nam, "%.4d", ++i);
480 if((ofd = createtmp((ulong)p, nam)) < 0)
481 ofd = dup(nullfd, -1);
483 if(write(ofd, buf, n) != n)
499 char buf[NBUF], *file;
507 if((fd = open(file, OREAD)) < 0){
515 if((d = dirfstat(fd)) == nil){
523 if((n = dirreadall(fd, &d)) < 0)
526 addpage(p, d[i].name, popenfile, smprint("%s/%s", file, d[i].name), -1);
528 p->text = strdup(p->label);
533 memset(buf, 0, 32+1);
534 if((n = read(fd, buf, 32)) <= 0)
540 if(memcmp(buf, "%PDF-", 5) == 0 || strstr(buf, "%!"))
542 else if(memcmp(buf, "x T ", 4) == 0){
543 p->data = "lp -dstdout";
546 else if(memcmp(buf, "\xF7\x02\x01\x83\x92\xC0\x1C;", 8) == 0){
547 p->data = "dvips -Pps -r0 -q1 -f1";
550 else if(memcmp(buf, "\x1F\x8B", 2) == 0){
554 else if(memcmp(buf, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", 8) == 0){
558 else if(memcmp(buf, "PK\x03\x04", 4) == 0){
559 p->data = "fs/zipfs";
562 else if(memcmp(buf, "GIF", 3) == 0)
564 else if(memcmp(buf, "\111\111\052\000", 4) == 0)
565 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
566 else if(memcmp(buf, "\115\115\000\052", 4) == 0)
567 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
568 else if(memcmp(buf, "\377\330\377", 3) == 0)
570 else if(memcmp(buf, "\211PNG\r\n\032\n", 3) == 0)
572 else if(memcmp(buf, "\0PC Research, Inc", 17) == 0)
573 p->data = "aux/g3p9bit -g";
574 else if(memcmp(buf, "TYPE=ccitt-g31", 14) == 0)
575 p->data = "aux/g3p9bit -g";
576 else if(memcmp(buf, "II*", 3) == 0)
577 p->data = "aux/g3p9bit -g";
578 else if(memcmp(buf, "TYPE=", 5) == 0)
579 p->data = "fb/3to1 rgbv |fb/pcp -tplan9";
580 else if(buf[0] == 'P' && '0' <= buf[1] && buf[1] <= '9')
582 else if(memcmp(buf, "BM", 2) == 0)
584 else if(infernobithdr(buf, n))
587 werrstr("unknown image format");
591 if(seek(fd, 0, 0) < 0)
593 if((i = read(fd, buf+n, n)) < 0)
595 if(i != n || memcmp(buf, buf+n, i)){
598 if((tfd = createtmp((ulong)p, "file")) < 0)
601 if(write(tfd, buf, n) != n)
603 if((n = read(fd, buf, sizeof(buf))) < 0)
606 if(dup(tfd, fd) < 0){
637 for(p = root->down; p; p = t)
638 if((t = nextpage(p)) == x)
650 if(p->open == nil || (fd = p->open(p)) < 0)
654 pipeline(fd, "rotate -r %d", rotate);
656 pipeline(fd, "resize -x %d", resize.x);
658 pipeline(fd, "resize -y %d", resize.y);
668 if(p->open && p->image == nil && p->text == nil){
669 if((fd = openpage(p)) >= 0){
671 p->image = readimage(display, fd, 1);
674 if(p->image == nil && p->text == nil)
675 p->text = smprint("%s: %r", p->label);
688 lockdisplay(display);
690 unlockdisplay(display);
701 for(p = root->down; p; p = nextpage(p)){
702 if(age == 0) /* synchronous flush */
704 else if(!canqlock(p))
706 if((pagegen - p->gen) >= age)
713 loadpages(Page *p, int ahead, int oviewgen)
717 ahead++; /* load at least one */
718 unloadpages(ahead*2);
719 for(i = 0; i < ahead && p; p = nextpage(p), i++){
720 if(viewgen != oviewgen)
724 if(viewgen != oviewgen){
734 if(size.x && size.y && newwin){
738 lockdisplay(display);
740 unlockdisplay(display);
748 * A draw operation that touches only the area contained in bot but not in top.
749 * mp and sp get aligned with bot.min.
752 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
753 Image *src, Point sp, Image *mask, Point mp, int op)
761 if(Dx(bot)*Dy(bot) == 0)
764 /* no points in bot - top */
765 if(rectinrect(bot, top))
768 /* bot - top ≡ bot */
769 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
770 gendrawop(dst, bot, src, sp, mask, mp, op);
775 /* split bot into rectangles that don't intersect top */
777 if(bot.min.x < top.min.x){
778 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
779 delta = subpt(r.min, origin);
780 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
781 bot.min.x = top.min.x;
785 if(bot.max.x > top.max.x){
786 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
787 delta = subpt(r.min, origin);
788 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
789 bot.max.x = top.max.x;
793 if(bot.min.y < top.min.y){
794 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
795 delta = subpt(r.min, origin);
796 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
797 bot.min.y = top.min.y;
801 if(bot.max.y > top.max.y){
802 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
803 delta = subpt(r.min, origin);
804 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
805 bot.max.y = top.max.y;
810 zoomdraw(Image *d, Rectangle r, Rectangle top, Image *s, Point sp, int f)
817 gendrawdiff(d, r, top, s, sp, nil, ZP, S);
821 if(r.min.x < d->r.min.x){
822 sp.x += (d->r.min.x - r.min.x)/f;
823 a.x = (d->r.min.x - r.min.x)%f;
824 r.min.x = d->r.min.x;
826 if(r.min.y < d->r.min.y){
827 sp.y += (d->r.min.y - r.min.y)/f;
828 a.y = (d->r.min.y - r.min.y)%f;
829 r.min.y = d->r.min.y;
832 w = s->r.max.x - sp.x;
835 t = allocimage(display, Rect(r.min.x, r.min.y, r.min.x+w, r.max.y), s->chan, 0, DNofill);
838 for(y=r.min.y; y<r.max.y; y++){
839 draw(t, Rect(r.min.x, y, r.min.x+w, y+1), s, nil, sp);
846 for(x=r.min.x; x<r.max.x; x++){
847 gendrawdiff(d, Rect(x, r.min.y, x+1, r.max.y), top, t, sp, nil, ZP, S);
859 return p->image ? mulpt(subpt(p->image->r.max, p->image->r.min), zoom) : ZP;
868 if((i = p->image) == nil){
871 if((s = p->text) == nil)
874 r.max = stringsize(font, p->text);
875 r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2), divpt(r.max, 2)),
877 draw(screen, r, display->white, nil, ZP);
878 string(screen, r.min, display->black, ZP, font, s);
880 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
881 zoomdraw(screen, r, ZR, i, i->r.min, zoom);
883 gendrawdiff(screen, screen->r, r, display->white, ZP, nil, ZP, S);
884 border(screen, r, -Borderwidth, display->black, ZP);
885 flushimage(display, 1);
889 translate(Page *p, Point d)
895 if((i==0) || (d.x==0 && d.y==0))
897 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
899 nr = rectaddpt(r, d);
901 rectclip(&or, screen->r);
902 draw(screen, rectaddpt(or, d), screen, nil, or.min);
903 zoomdraw(screen, nr, rectaddpt(or, d), i, i->r.min, zoom);
904 gendrawdiff(screen, screen->r, nr, display->white, ZP, nil, ZP, S);
905 border(screen, nr, -Borderwidth, display->black, ZP);
906 flushimage(display, 1);
916 /* look in current document first */
917 if(current && current->up){
918 for(p = current->up->down; p; p = p->next)
919 if(cistrncmp(p->label, name, n) == 0)
922 /* look everywhere */
923 for(p = root->down; p; p = nextpage(p))
924 if(cistrncmp(p->label, name, n) == 0)
934 for(p = root->down; i > 0 && p; p = nextpage(p))
945 for(i = 0, p = root->down; p && p != x; p = nextpage(p))
947 return (p == x) ? i : -1;
967 esetcursor(&reading);
973 switch(rfork(RFPROC|RFMEM)){
975 sysfatal("rfork: %r");
977 loadpages(p, NAHEAD, oviewgen);
985 char nam[64], *argv[4];
990 esetcursor(&reading);
992 if((fd = openpage(p)) < 0)
994 if(rfork(RFREND|RFFDG|RFPROC|RFENVG|RFNOTEG|RFNOWAIT) == 0){
998 snprint(nam, sizeof nam, "/bin/%s", argv0);
1003 sysfatal("exec: %r");
1016 lockdisplay(display);
1017 if(new && getwindow(display, Refnone) == -1)
1018 sysfatal("getwindow: %r");
1025 unlockdisplay(display);
1028 void killcohort(void)
1031 for(i=0;i!=3;i++){ /* It's a long way to the kitchen */
1032 postnote(PNGROUP, getpid(), "kill");
1037 void drawerr(Display *, char *msg)
1039 sysfatal("draw: %s", msg);
1046 if(x = strrchr(s, '/'))
1055 fprint(2, "usage: %s [ -iRw ] [ -p ppi ] [ file ... ]\n", argv0);
1060 main(int argc, char *argv[])
1062 enum { Eplumb = 4 };
1087 ppi = atoi(EARGF(usage()));
1094 * so that we can stop all subprocesses with a note,
1095 * and to isolate rendezvous from other processes
1097 rfork(RFNOTEG|RFNAMEG|RFREND);
1099 atnotify(catchnote, 1);
1101 s = smprint("-pid %d", getpid());
1102 if(newwindow(s) < 0)
1103 sysfatal("newwindow: %r");
1106 initdraw(drawerr, nil, argv0);
1107 display->locking = 1;
1108 unlockdisplay(display);
1109 einit(Ekeyboard|Emouse);
1110 eplumb(Eplumb, "image");
1111 nullfd = open("/dev/null", ORDWR);
1112 current = root = addpage(nil, "root", nil, nil, -1);
1114 if(*argv == nil && !imode)
1115 addpage(root, "stdin", popenfile, strdup("/fd/0"), -1);
1116 for(; *argv; argv++)
1117 addpage(root, shortname(*argv), popenfile, strdup(*argv), -1);
1124 lockdisplay(display);
1127 if(current == nil || !canqlock(current))
1132 if((m.buttons & 1) == 0)
1134 translate(current, subpt(m.xy, o));
1140 i = emenuhit(2, &m, &menu);
1141 if(i < 0 || i >= nelem(menuitems) || menuitems[i]==nil)
1144 if(strcmp(s, "orig size")==0){
1151 unlockdisplay(display);
1152 esetcursor(&reading);
1157 if(strncmp(s, "rotate ", 7)==0){
1158 rotate += atoi(s+7);
1162 if(strcmp(s, "upside down")==0){
1166 if(strcmp(s, "fit width")==0){
1169 resize = subpt(screen->r.max, screen->r.min);
1173 if(strcmp(s, "fit height")==0){
1176 resize = subpt(screen->r.max, screen->r.min);
1180 if(strncmp(s, "zoom", 4)==0){
1181 if(current && canqlock(current)){
1182 o = subpt(m.xy, screen->r.min);
1183 if(strstr(s, "in")){
1184 if(zoom < 0x40000000){
1186 pos = addpt(mulpt(subpt(pos, o), 2), o);
1191 pos = addpt(divpt(subpt(pos, o), 2), o);
1198 unlockdisplay(display);
1199 if(strcmp(s, "next")==0)
1200 showpage(nextpage(current));
1201 if(strcmp(s, "prev")==0)
1202 showpage(prevpage(current));
1203 if(strcmp(s, "zerox")==0)
1205 if(strcmp(s, "quit")==0)
1210 if(root->down == nil)
1212 pagemenu.lasthit = pageindex(current);
1213 i = emenuhit(3, &m, &pagemenu);
1214 unlockdisplay(display);
1216 showpage(pageat(i));
1220 unlockdisplay(display);
1229 if(current == nil || !canqlock(current))
1231 lockdisplay(display);
1233 translate(current, Pt(0, Dy(screen->r)/2));
1234 unlockdisplay(display);
1238 unlockdisplay(display);
1240 if(prevpage(current))
1245 showpage(prevpage(current));
1248 if(current == nil || !canqlock(current))
1250 o = addpt(pos, pagesize(current));
1251 lockdisplay(display);
1252 if(o.y > Dy(screen->r)){
1253 translate(current, Pt(0, -Dy(screen->r)/2));
1254 unlockdisplay(display);
1258 unlockdisplay(display);
1260 if(nextpage(current))
1264 showpage(findpage(jump));
1270 showpage(nextpage(current));
1274 if(i+1 < sizeof(jump)){
1282 if(pm && pm->ndata > 0){
1286 s = plumblookup(pm->attr, "action");
1287 if(s && strcmp(s, "quit")==0)
1289 if(s && strcmp(s, "showdata")==0){
1290 static ulong plumbid;
1292 if((fd = createtmp(plumbid++, "plumb")) < 0){
1293 fprint(2, "plumb: createtmp: %r\n");
1297 if(fd2path(fd, s, NPATH) < 0){
1301 write(fd, pm->data, pm->ndata);
1302 }else if(pm->data[0] == '/'){
1303 s = strdup(pm->data);
1305 s = malloc(strlen(pm->wdir)+1+pm->ndata+1);
1306 sprint(s, "%s/%s", pm->wdir, pm->data);
1309 showpage(addpage(root, shortname(s), popenfile, s, fd));