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);
118 up->down = up->tail = p;
126 if(up && current == up)
132 resizewin(Point size)
136 if((wctl = open("/dev/wctl", OWRITE)) < 0)
139 size = addpt(size, Pt(Borderwidth*2, Borderwidth*2));
140 fprint(wctl, "resize -dx %d -dy %d\n", size.x, size.y);
149 snprint(nam, sizeof nam, "%s%s%.12d%.8lux", pagespool, pfx, getpid(), id++);
150 return create(nam, OEXCL|ORCLOSE|ORDWR, 0600);
154 catchnote(void *, char *msg)
156 if(strstr(msg, "sys: write on closed pipe"))
158 if(strstr(msg, "hangup"))
160 if(strstr(msg, "alarm"))
166 pipeline(int fd, char *fmt, ...)
168 char buf[NPATH], *argv[4];
177 switch(rfork(RFPROC|RFFDG|RFREND|RFNOWAIT)){
191 vsnprint(buf, sizeof buf, fmt, arg);
198 exec("/bin/rc", argv);
199 sysfatal("exec: %r");
210 if(x = strrchr(s, '/'))
225 if((fd = dup(p->fd, -1)) < 0){
233 pipeline(fd, "%s", (char*)p->data);
236 * dont keep the file descriptor arround if it can simply
239 fd2path(p->fd, nam, sizeof(nam));
240 if(strncmp(nam, pagespool, strlen(pagespool))){
243 p->data = strdup(nam);
255 pipeline(p->fd, "%s", (char*)p->data);
265 char mnt[32], cmd[64], *argv[4];
268 snprint(mnt, sizeof(mnt), "/n/tapefs.%.12d%.8lux", getpid(), (ulong)p);
269 switch(rfork(RFREND|RFPROC|RFFDG)){
279 snprint(cmd, sizeof(cmd), "%s -m %s /fd/0", p->data, mnt);
282 exec("/bin/rc", argv);
283 sysfatal("exec: %r");
288 p->data = strdup(mnt);
296 char buf[NPATH], *s, *e;
302 e = buf+sizeof(buf)-1;
303 s += snprint(s, e-s, "%s/", (char*)p->data);
306 pipeline(fd, "awk '/\\<rootfile/{"
307 "if(match($0, /full\\-path\\=\\\"([^\\\"]+)\\\"/)){"
308 "print substr($0, RSTART+11,RLENGTH-12);exit}}'");
309 n = read(fd, s, e - s);
313 while(n > 0 && s[n-1] == '\n')
317 if((fd = open(buf, OREAD)) < 0)
319 pipeline(fd, "awk '/\\<item/{"
320 "if(match($0, /id\\=\\\"([^\\\"]+)\\\"/)){"
321 "id=substr($0, RSTART+4, RLENGTH-5);"
322 "if(match($0, /href\\=\\\"([^\\\"]+)\\\"/)){"
323 "item[id]=substr($0, RSTART+6, RLENGTH-7)}}};"
325 "if(match($0, /idref\\=\\\"([^\\\"]+)\\\"/)){"
326 "ref=substr($0, RSTART+7, RLENGTH-8);"
327 "print item[ref]; fflush}}'");
328 s = strrchr(buf, '/')+1;
329 while((n = read(fd, s, e-s)) > 0){
330 while(n > 0 && s[n-1] == '\n')
333 addpage(p, shortname(buf), popenfile, strdup(buf), -1);
339 typedef struct Ghost Ghost;
358 switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
367 fprint(gs->pin, "%s DoPDFPage\n"
370 "dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring "
371 "flushfile\n", p->label);
372 while((n = read(gs->pdat, buf, sizeof buf)) > 0){
373 if(memcmp(buf, "THIS IS NOT AN INFERNO BITMAP\n", 30) == 0)
377 if(write(pfd[1], buf, n) != n){
390 infernobithdr(char *buf, int n)
393 if(memcmp(buf, "compressed\n", 11) == 0)
395 if(strtochan((char*)buf))
397 if(memcmp(buf, " ", 10) == 0 &&
398 '0' <= buf[10] && buf[10] <= '9' &&
408 int n, i, pdf, ifd, ofd, pin[2], pout[2], pdat[2];
409 char buf[NBUF], nam[32], *argv[16];
415 if(read(ifd, buf, 5) != 5)
418 if(memcmp(buf, "%PDF-", 5) == 0)
438 switch(rfork(RFREND|RFPROC|RFFDG|RFNOWAIT)){
445 if(dup(pout[1], 1)<0)
455 if(dup(pdat[1], 3)<0)
469 pipeline(4, "%s", (char*)p->data);
473 argv[2] = "-sDEVICE=plan9";
474 argv[3] = "-sOutputFile=/fd/3";
476 argv[5] = pdf ? "-dDELAYSAFER" : "-dSAFER";
478 argv[7] = "-dTextAlphaBits=4";
479 argv[8] = "-dGraphicsAlphaBits=4";
480 snprint(buf, sizeof buf, "-r%d", ppi);
482 argv[10] = "-dDOINTERPOLATE";
483 argv[11] = pdf ? "-" : "/fd/4";
485 exec("/bin/gs", argv);
486 sysfatal("exec: %r");
497 "/PAGEOUT (/fd/1) (w) file def\n"
498 "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n"
502 "/PDFSave null def\n"
503 "/DSCPageCount 0 def\n"
504 "/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n"
506 "GS_PDF_ProcSet begin\n"
508 "(/fd/4) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n"
510 "pdfpagecount PAGE==\n";
513 if(write(pin[0], prolog, n) != n)
515 if((n = read(pout[0], buf, sizeof(buf)-1)) < 0)
523 gs = mallocz(sizeof(*gs), 1);
528 snprint(nam, sizeof nam, "%d", i);
529 addpage(p, nam, popenpdf, gs, -1);
532 /* keep ghostscript arround */
537 while((n = read(pdat[0], buf, sizeof(buf))) >= 0){
538 if(ofd >= 0 && (n <= 0 || infernobithdr(buf, n))){
539 snprint(nam, sizeof nam, "%d", i);
540 addpage(p, nam, popenimg, nil, ofd);
546 snprint(nam, sizeof nam, "%.4d", ++i);
547 if((ofd = createtmp(nam)) < 0)
548 ofd = dup(nullfd, -1);
550 if(write(ofd, buf, n) != n)
564 filetype(char *buf, int nbuf, char *typ, int ntyp)
566 int n, ifd[2], ofd[2];
569 if(infernobithdr(buf, nbuf)){
570 strncpy(typ, "image/p9bit", ntyp);
582 if(rfork(RFFDG|RFPROC|RFNOWAIT) == 0){
594 exec("/bin/file", argv);
598 if(rfork(RFFDG|RFPROC|RFNOWAIT) == 0){
600 write(ifd[0], buf, nbuf);
604 if((n = readn(ofd[0], typ, ntyp-1)) < 0)
607 while(n > 0 && typ[n-1] == '\n')
614 dircmp(void *p1, void *p2)
621 return strcmp(d1->name, d2->name);
632 "application/pdf", popengs, nil,
633 "application/postscript", popengs, nil,
634 "application/troff", popengs, "lp -dstdout",
635 "text/plain", popengs, "lp -dstdout",
636 "text/html", popengs, "uhtml | html2ms | tbl | troff -ms | lp -dstdout",
637 "application/dvi", popengs, "dvips -Pps -r0 -q1 -f1",
638 "application/doc", popengs, "doc2ps",
639 "application/zip", popentape, "fs/zipfs",
640 "application/x-tar", popentape, "fs/tarfs",
641 "application/x-ustar", popentape, "fs/tarfs",
642 "application/x-compress", popenfilter, "uncompress",
643 "application/x-gzip", popenfilter, "gunzip",
644 "application/x-bzip2", popenfilter, "bunzip2",
645 "image/gif", popenimg, "gif -t9",
646 "image/jpeg", popenimg, "jpg -t9",
647 "image/png", popenimg, "png -t9",
648 "image/ppm", popenimg, "ppm -t9",
649 "image/bmp", popenimg, "bmp -t9",
650 "image/p9bit", popenimg, nil,
653 char buf[NBUF], typ[128], *file;
662 if((fd = open(file, OREAD)) < 0){
669 if((d = dirfstat(fd)) == nil){
678 snprint(buf, sizeof(buf), "%s/META-INF/container.xml", file);
679 if((tfd = open(buf, OREAD)) >= 0){
687 if((n = dirreadall(fd, &d)) < 0)
689 qsort(d, n, sizeof d[0], dircmp);
691 addpage(p, d[i].name, popenfile, smprint("%s/%s", file, d[i].name), -1);
697 memset(buf, 0, NBUF/2);
698 if((n = readn(fd, buf, NBUF/2)) <= 0)
700 filetype(buf, n, typ, sizeof(typ));
701 for(i=0; i<nelem(tab); i++)
702 if(strncmp(typ, tab[i].typ, strlen(tab[i].typ)) == 0)
705 werrstr("unknown image format: %s", typ);
709 p->data = tab[i].data;
710 p->open = tab[i].open;
711 if(seek(fd, 0, 0) < 0)
713 if((i = read(fd, buf+n, n)) < 0)
715 if(i != n || memcmp(buf, buf+n, i)){
718 if((tfd = createtmp("file")) < 0)
721 if(write(tfd, buf, n) != n)
723 if((n = read(fd, buf, sizeof(buf))) < 0)
726 if(dup(tfd, fd) < 0){
757 for(p = root->down; p; p = t)
758 if((t = nextpage(p)) == x)
770 if(p->open == nil || (fd = p->open(p)) < 0)
774 pipeline(fd, "rotate -r %d", rotate);
776 pipeline(fd, "resize -x %d", resize.x);
778 pipeline(fd, "resize -y %d", resize.y);
788 if(p->open && p->image == nil){
789 if((fd = openpage(p)) >= 0){
791 p->image = readimage(display, fd, 1);
803 if(p->open == nil || p->image == nil)
805 lockdisplay(display);
807 unlockdisplay(display);
816 for(p = root->down; p; p = nextpage(p)){
817 if(age == 0) /* synchronous flush */
819 else if(!canqlock(p))
821 if((pagegen - p->gen) >= age)
828 loadpages(Page *p, int ahead, int oviewgen)
832 ahead++; /* load at least one */
833 unloadpages(ahead*2);
834 for(i = 0; i < ahead && p; p = nextpage(p), i++){
835 if(viewgen != oviewgen)
839 if(viewgen != oviewgen){
849 if(size.x && size.y && newwin){
853 lockdisplay(display);
855 unlockdisplay(display);
863 * A draw operation that touches only the area contained in bot but not in top.
864 * mp and sp get aligned with bot.min.
867 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
868 Image *src, Point sp, Image *mask, Point mp, int op)
876 if(Dx(bot)*Dy(bot) == 0)
879 /* no points in bot - top */
880 if(rectinrect(bot, top))
883 /* bot - top ≡ bot */
884 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
885 gendrawop(dst, bot, src, sp, mask, mp, op);
890 /* split bot into rectangles that don't intersect top */
892 if(bot.min.x < top.min.x){
893 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
894 delta = subpt(r.min, origin);
895 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
896 bot.min.x = top.min.x;
900 if(bot.max.x > top.max.x){
901 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
902 delta = subpt(r.min, origin);
903 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
904 bot.max.x = top.max.x;
908 if(bot.min.y < top.min.y){
909 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
910 delta = subpt(r.min, origin);
911 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
912 bot.min.y = top.min.y;
916 if(bot.max.y > top.max.y){
917 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
918 delta = subpt(r.min, origin);
919 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
920 bot.max.y = top.max.y;
925 zoomdraw(Image *d, Rectangle r, Rectangle top, Image *s, Point sp, int f)
932 gendrawdiff(d, r, top, s, sp, nil, ZP, SoverD);
936 if(r.min.x < d->r.min.x){
937 sp.x += (d->r.min.x - r.min.x)/f;
938 a.x = (d->r.min.x - r.min.x)%f;
939 r.min.x = d->r.min.x;
941 if(r.min.y < d->r.min.y){
942 sp.y += (d->r.min.y - r.min.y)/f;
943 a.y = (d->r.min.y - r.min.y)%f;
944 r.min.y = d->r.min.y;
947 w = s->r.max.x - sp.x;
950 t = allocimage(display, Rect(r.min.x, r.min.y, r.min.x+w, r.max.y), s->chan, 0, DNofill);
953 for(y=r.min.y; y<r.max.y; y++){
954 draw(t, Rect(r.min.x, y, r.min.x+w, y+1), s, nil, sp);
962 for(sp=r.min; x<r.max.x; sp.x++){
963 gendrawdiff(d, Rect(x, r.min.y, x+1, r.max.y), top, t, sp, nil, ZP, SoverD);
964 for(x++; ++a.x<f && x<r.max.x; x++)
965 gendrawdiff(d, Rect(x, r.min.y, x+1, r.max.y), top, d,
966 Pt(x-1, r.min.y), nil, ZP, SoverD);
976 return p->image ? mulpt(subpt(p->image->r.max, p->image->r.min), zoom) : ZP;
985 if((i = p->image) == nil){
987 r.max = stringsize(font, p->label);
988 r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2), divpt(r.max, 2)),
990 draw(screen, r, display->white, nil, ZP);
991 string(screen, r.min, display->black, ZP, font, p->label);
993 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
994 zoomdraw(screen, r, ZR, i, i->r.min, zoom);
996 gendrawdiff(screen, screen->r, r, background, ZP, nil, ZP, SoverD);
997 border(screen, r, -Borderwidth, display->black, ZP);
998 flushimage(display, 1);
1002 translate(Page *p, Point d)
1004 Rectangle r, or, nr;
1008 if((i==0) || (d.x==0 && d.y==0))
1010 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
1011 pos = addpt(pos, d);
1012 nr = rectaddpt(r, d);
1014 rectclip(&or, screen->r);
1015 draw(screen, rectaddpt(or, d), screen, nil, or.min);
1016 zoomdraw(screen, nr, rectaddpt(or, d), i, i->r.min, zoom);
1017 gendrawdiff(screen, screen->r, nr, background, ZP, nil, ZP, SoverD);
1018 border(screen, nr, -Borderwidth, display->black, ZP);
1019 flushimage(display, 1);
1023 findpage(char *name)
1029 /* look in current document first */
1030 if(current && current->up){
1031 for(p = current->up->down; p; p = p->next)
1032 if(cistrncmp(p->label, name, n) == 0)
1035 /* look everywhere */
1036 for(p = root->down; p; p = nextpage(p))
1037 if(cistrncmp(p->label, name, n) == 0)
1047 for(p = root->down; i > 0 && p; p = nextpage(p))
1058 for(i = 0, p = root->down; p && p != x; p = nextpage(p))
1060 return (p == x) ? i : -1;
1080 esetcursor(&reading);
1086 switch(rfork(RFPROC|RFMEM)){
1088 sysfatal("rfork: %r");
1090 loadpages(p, NAHEAD, oviewgen);
1100 for(p = nextpage(current); p; p = nextpage(p))
1101 if(p->image || p->open)
1111 for(p = prevpage(current); p; p = prevpage(p))
1112 if(p->image || p->open)
1120 char nam[64], *argv[4];
1125 esetcursor(&reading);
1127 if((fd = openpage(p)) < 0)
1129 if(rfork(RFREND|RFFDG|RFPROC|RFENVG|RFNOTEG|RFNOWAIT) == 0){
1133 snprint(nam, sizeof nam, "/bin/%s", argv0);
1138 sysfatal("exec: %r");
1151 lockdisplay(display);
1152 if(new && getwindow(display, Refnone) == -1)
1153 sysfatal("getwindow: %r");
1160 unlockdisplay(display);
1163 void killcohort(void)
1166 for(i=0;i!=3;i++){ /* It's a long way to the kitchen */
1167 postnote(PNGROUP, getpid(), "kill");
1172 void drawerr(Display *, char *msg)
1174 sysfatal("draw: %s", msg);
1180 fprint(2, "usage: %s [ -iRw ] [ -p ppi ] [ file ... ]\n", argv0);
1185 main(int argc, char *argv[])
1187 enum { Eplumb = 4 };
1212 ppi = atoi(EARGF(usage()));
1219 * so that we can stop all subprocesses with a note,
1220 * and to isolate rendezvous from other processes
1222 rfork(RFNOTEG|RFNAMEG|RFREND);
1224 atnotify(catchnote, 1);
1226 s = smprint("-pid %d", getpid());
1227 if(newwindow(s) < 0)
1228 sysfatal("newwindow: %r");
1231 initdraw(drawerr, nil, argv0);
1232 background = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x777777FF);
1233 draw(screen, screen->r, background, nil, ZP);
1234 flushimage(display, 1);
1235 display->locking = 1;
1236 unlockdisplay(display);
1237 einit(Ekeyboard|Emouse);
1238 eplumb(Eplumb, "image");
1239 nullfd = open("/dev/null", ORDWR);
1240 current = root = addpage(nil, "root", nil, nil, -1);
1242 if(*argv == nil && !imode)
1243 addpage(root, "stdin", popenfile, strdup("/fd/0"), -1);
1244 for(; *argv; argv++)
1245 addpage(root, shortname(*argv), popenfile, strdup(*argv), -1);
1252 lockdisplay(display);
1255 if(current == nil || !canqlock(current))
1260 if((m.buttons & 1) == 0)
1262 translate(current, subpt(m.xy, o));
1269 i = emenuhit(2, &m, &menu);
1270 if(i < 0 || i >= nelem(menuitems) || menuitems[i]==nil)
1273 if(strcmp(s, "orig size")==0){
1280 unlockdisplay(display);
1281 esetcursor(&reading);
1286 if(strncmp(s, "rotate ", 7)==0){
1287 rotate += atoi(s+7);
1291 if(strcmp(s, "upside down")==0){
1295 if(strcmp(s, "fit width")==0){
1298 resize = subpt(screen->r.max, screen->r.min);
1302 if(strcmp(s, "fit height")==0){
1305 resize = subpt(screen->r.max, screen->r.min);
1309 if(strncmp(s, "zoom", 4)==0){
1310 if(current && canqlock(current)){
1311 o = subpt(o, screen->r.min);
1312 if(strstr(s, "in")){
1313 if(zoom < 0x40000000){
1315 pos = addpt(mulpt(subpt(pos, o), 2), o);
1320 pos = addpt(divpt(subpt(pos, o), 2), o);
1327 unlockdisplay(display);
1328 if(strcmp(s, "next")==0)
1330 if(strcmp(s, "prev")==0)
1332 if(strcmp(s, "zerox")==0)
1334 if(strcmp(s, "quit")==0)
1339 if(root->down == nil)
1341 pagemenu.lasthit = pageindex(current);
1342 i = emenuhit(3, &m, &pagemenu);
1343 unlockdisplay(display);
1345 showpage(pageat(i));
1349 unlockdisplay(display);
1358 if(current == nil || !canqlock(current))
1360 lockdisplay(display);
1362 translate(current, Pt(0, Dy(screen->r)/2));
1363 unlockdisplay(display);
1367 unlockdisplay(display);
1369 if(prevpage(current))
1377 if(current == nil || !canqlock(current))
1379 o = addpt(pos, pagesize(current));
1380 lockdisplay(display);
1381 if(o.y > Dy(screen->r)){
1382 translate(current, Pt(0, -Dy(screen->r)/2));
1383 unlockdisplay(display);
1387 unlockdisplay(display);
1389 if(nextpage(current))
1393 showpage(findpage(jump));
1403 if(i+1 < sizeof(jump)){
1411 if(pm && pm->ndata > 0){
1415 s = plumblookup(pm->attr, "action");
1416 if(s && strcmp(s, "quit")==0)
1418 if(s && strcmp(s, "showdata")==0){
1419 if((fd = createtmp("plumb")) < 0){
1420 fprint(2, "plumb: createtmp: %r\n");
1424 if(fd2path(fd, s, NPATH) < 0){
1428 write(fd, pm->data, pm->ndata);
1429 }else if(pm->data[0] == '/'){
1430 s = strdup(pm->data);
1432 s = malloc(strlen(pm->wdir)+1+pm->ndata+1);
1433 sprint(s, "%s/%s", pm->wdir, pm->data);
1436 showpage(addpage(root, shortname(s), popenfile, s, fd));