9 typedef struct Page Page;
39 Image *frame, *paper, *ground;
41 char pagespool[] = "/tmp/pagespool.";
50 char *pagemenugen(int i);
85 {0xff, 0x80, 0xff, 0x80, 0xff, 0x00, 0xfe, 0x00,
86 0xff, 0x00, 0xff, 0x80, 0xff, 0xc0, 0xef, 0xe0,
87 0xc7, 0xf0, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0xc0,
88 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, },
89 {0x00, 0x00, 0x7f, 0x00, 0x7e, 0x00, 0x7c, 0x00,
90 0x7e, 0x00, 0x7f, 0x00, 0x6f, 0x80, 0x47, 0xc0,
91 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xe0, 0x00, 0x40,
92 0x00, 0x00, 0x01, 0xb6, 0x01, 0xb6, 0x00, 0x00, }
95 void showpage(Page *);
96 void drawpage(Page *);
97 Point pagesize(Page *);
100 addpage(Page *up, char *label, int (*popen)(Page *), void *pdata, int fd)
104 p = mallocz(sizeof(*p), 1);
105 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);
150 snprint(nam, sizeof nam, "%s%s%.12d%.8lux", pagespool, pfx, getpid(), id++);
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"))
174 for(mfd = 0; fd >= 0; fd = va_arg(arg, int), mfd++)
179 if((fd = open("/fd", OREAD)) < 0)
180 sysfatal("open: %r");
181 n = dirreadall(fd, &dir);
183 if(strstr(dir[i].name, "ctl"))
185 fd = atoi(dir[i].name);
193 pipeline(int fd, char *fmt, ...)
195 char buf[NPATH], *argv[4];
205 vsnprint(buf, sizeof buf, fmt, arg);
207 switch(rfork(RFPROC|RFMEM|RFFDG|RFREND|RFNOWAIT)){
213 dupfds(fd, pfd[1], 2, -1);
218 exec("/bin/rc", argv);
219 sysfatal("exec: %r");
230 if(x = strrchr(s, '/'))
245 if((fd = dup(p->fd, -1)) < 0){
253 pipeline(fd, "%s", (char*)p->data);
256 * dont keep the file descriptor arround if it can simply
259 fd2path(p->fd, nam, sizeof(nam));
260 if(strncmp(nam, pagespool, strlen(pagespool))){
263 p->data = strdup(nam);
275 pipeline(p->fd, "%s", (char*)p->data);
285 char mnt[32], cmd[64], *argv[4];
288 snprint(mnt, sizeof(mnt), "/n/tapefs.%.12d%.8lux", getpid(), (ulong)p);
289 snprint(cmd, sizeof(cmd), "%s -m %s /fd/0", p->data, mnt);
290 switch(rfork(RFPROC|RFMEM|RFFDG|RFREND)){
296 dupfds(p->fd, 1, 2, -1);
301 exec("/bin/rc", argv);
302 sysfatal("exec: %r");
307 p->data = strdup(mnt);
315 char buf[NPATH], *s, *e;
321 e = buf+sizeof(buf)-1;
322 s += snprint(s, e-s, "%s/", (char*)p->data);
325 pipeline(fd, "awk '/\\<rootfile/{"
326 "if(match($0, /full\\-path\\=\\\"([^\\\"]+)\\\"/)){"
327 "print substr($0, RSTART+11,RLENGTH-12);exit}}'");
328 n = read(fd, s, e - s);
332 while(n > 0 && s[n-1] == '\n')
336 if((fd = open(buf, OREAD)) < 0)
338 pipeline(fd, "awk '/\\<item/{"
339 "if(match($0, /id\\=\\\"([^\\\"]+)\\\"/)){"
340 "id=substr($0, RSTART+4, RLENGTH-5);"
341 "if(match($0, /href\\=\\\"([^\\\"]+)\\\"/)){"
342 "item[id]=substr($0, RSTART+6, RLENGTH-7)}}};"
344 "if(match($0, /idref\\=\\\"([^\\\"]+)\\\"/)){"
345 "ref=substr($0, RSTART+7, RLENGTH-8);"
346 "print item[ref]; fflush}}'");
347 s = strrchr(buf, '/')+1;
348 while((n = read(fd, s, e-s)) > 0){
349 while(n > 0 && s[n-1] == '\n')
352 addpage(p, shortname(buf), popenfile, strdup(buf), -1);
358 typedef struct Ghost Ghost;
377 switch(rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT)){
385 dupfds(gs->pdat, gs->pin, pfd[1], -1);
386 fprint(1, "%s DoPDFPage\n"
389 "dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring "
390 "flushfile\n", p->label);
391 while((n = read(0, buf, sizeof buf)) > 0){
392 if(memcmp(buf, "THIS IS NOT AN INFERNO BITMAP\n", 30) == 0)
404 infernobithdr(char *buf, int n)
407 if(memcmp(buf, "compressed\n", 11) == 0)
409 if(strtochan((char*)buf))
411 if(memcmp(buf, " ", 10) == 0 &&
412 '0' <= buf[10] && buf[10] <= '9' &&
422 int n, i, pdf, ifd, ofd, pin[2], pout[2], pdat[2];
423 char buf[NBUF], nam[32], *argv[16];
429 if(read(ifd, buf, 5) != 5)
432 if(memcmp(buf, "%PDF-", 5) == 0)
453 switch(rfork(RFPROC|RFMEM|RFFDG|RFREND|RFNOWAIT)){
458 dupfds(pin[1], pout[1], nullfd, pdat[1], ifd, -1);
460 dupfds(nullfd, nullfd, nullfd, pdat[1], ifd, -1);
462 pipeline(4, "%s", argv[0]);
465 argv[2] = "-sDEVICE=plan9";
466 argv[3] = "-sOutputFile=/fd/3";
468 argv[5] = pdf ? "-dDELAYSAFER" : "-dSAFER";
470 argv[7] = "-dTextAlphaBits=4";
471 argv[8] = "-dGraphicsAlphaBits=4";
472 snprint(buf, sizeof buf, "-r%d", ppi);
474 argv[10] = "-dDOINTERPOLATE";
475 argv[11] = pdf ? "-" : "/fd/4";
477 exec("/bin/gs", argv);
478 sysfatal("exec: %r");
489 "/PAGEOUT (/fd/1) (w) file def\n"
490 "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n"
494 "/PDFSave null def\n"
495 "/DSCPageCount 0 def\n"
496 "/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n"
498 "GS_PDF_ProcSet begin\n"
500 "(/fd/4) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n"
502 "pdfpagecount PAGE==\n";
505 if(write(pin[0], prolog, n) != n)
507 if((n = read(pout[0], buf, sizeof(buf)-1)) < 0)
515 gs = mallocz(sizeof(*gs), 1);
520 snprint(nam, sizeof nam, "%d", i);
521 addpage(p, nam, popenpdf, gs, -1);
524 /* keep ghostscript arround */
529 while((n = read(pdat[0], buf, sizeof(buf))) >= 0){
530 if(ofd >= 0 && (n <= 0 || infernobithdr(buf, n))){
531 snprint(nam, sizeof nam, "%d", i);
532 addpage(p, nam, popenimg, nil, ofd);
538 snprint(nam, sizeof nam, "%.4d", ++i);
539 if((ofd = createtmp(nam)) < 0)
540 ofd = dup(nullfd, -1);
542 if(write(ofd, buf, n) != n)
556 filetype(char *buf, int nbuf, char *typ, int ntyp)
558 int n, ifd[2], ofd[2];
561 if(infernobithdr(buf, nbuf)){
562 strncpy(typ, "image/p9bit", ntyp);
574 if(rfork(RFPROC|RFMEM|RFFDG|RFREND|RFNOWAIT) == 0){
575 dupfds(ifd[1], ofd[1], 2, -1);
579 exec("/bin/file", argv);
583 if(rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT) == 0){
589 if((n = readn(ofd[0], typ, ntyp-1)) < 0)
592 while(n > 0 && typ[n-1] == '\n')
599 dircmp(void *p1, void *p2)
606 return strcmp(d1->name, d2->name);
617 "application/pdf", popengs, nil,
618 "application/postscript", popengs, nil,
619 "application/troff", popengs, "lp -dstdout",
620 "text/plain", popengs, "lp -dstdout",
621 "text/html", popengs, "uhtml | html2ms | tbl | troff -ms | lp -dstdout",
622 "application/dvi", popengs, "dvips -Pps -r0 -q1 -f1",
623 "application/doc", popengs, "doc2ps",
624 "application/zip", popentape, "fs/zipfs",
625 "application/x-tar", popentape, "fs/tarfs",
626 "application/x-ustar", popentape, "fs/tarfs",
627 "application/x-compress", popenfilter, "uncompress",
628 "application/x-gzip", popenfilter, "gunzip",
629 "application/x-bzip2", popenfilter, "bunzip2",
630 "image/gif", popenimg, "gif -t9",
631 "image/jpeg", popenimg, "jpg -t9",
632 "image/png", popenimg, "png -t9",
633 "image/ppm", popenimg, "ppm -t9",
634 "image/bmp", popenimg, "bmp -t9",
635 "image/p9bit", popenimg, nil,
638 char buf[NBUF], typ[128], *file;
647 if((fd = open(file, OREAD)) < 0){
654 if((d = dirfstat(fd)) == nil){
663 snprint(buf, sizeof(buf), "%s/META-INF/container.xml", file);
664 if((tfd = open(buf, OREAD)) >= 0){
672 if((n = dirreadall(fd, &d)) < 0)
674 qsort(d, n, sizeof d[0], dircmp);
676 addpage(p, d[i].name, popenfile, smprint("%s/%s", file, d[i].name), -1);
682 memset(buf, 0, NBUF/2);
683 if((n = readn(fd, buf, NBUF/2)) <= 0)
685 filetype(buf, n, typ, sizeof(typ));
686 for(i=0; i<nelem(tab); i++)
687 if(strncmp(typ, tab[i].typ, strlen(tab[i].typ)) == 0)
690 werrstr("unknown image format: %s", typ);
694 p->data = tab[i].data;
695 p->open = tab[i].open;
696 if(seek(fd, 0, 0) < 0)
698 if((i = read(fd, buf+n, n)) < 0)
700 if(i != n || memcmp(buf, buf+n, i)){
703 if((tfd = createtmp("file")) < 0)
706 if(write(tfd, buf, n) != n)
708 if((n = read(fd, buf, sizeof(buf))) < 0)
711 if(dup(tfd, fd) < 0){
741 for(p = root->down; p; p = t)
742 if((t = nextpage(p)) == x)
754 if(p->open == nil || (fd = p->open(p)) < 0)
758 pipeline(fd, "rotate -r %d", rotate);
760 pipeline(fd, "resize -x %d", resize.x);
762 pipeline(fd, "resize -y %d", resize.y);
772 if(p->open && p->image == nil){
773 if((fd = openpage(p)) >= 0){
775 if((p->image = readimage(display, fd, 1)) == nil)
776 fprint(2, "readimage: %r\n");
788 if(p->open == nil || p->image == nil)
790 lockdisplay(display);
792 unlockdisplay(display);
801 for(p = root->down; p; p = nextpage(p)){
802 if(age == 0) /* synchronous flush */
804 else if(!canqlock(p))
806 if((pagegen - p->gen) >= age)
813 loadpages(Page *p, int ahead, int oviewgen)
817 ahead++; /* load at least one */
818 unloadpages(ahead*2);
819 for(i = 0; i < ahead && p; p = nextpage(p), i++){
820 if(viewgen != oviewgen)
824 if(viewgen != oviewgen){
834 if(size.x && size.y && newwin){
838 lockdisplay(display);
840 unlockdisplay(display);
848 * A draw operation that touches only the area contained in bot but not in top.
849 * mp and sp get aligned with bot.min.
852 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
853 Image *src, Point sp, Image *mask, Point mp, int op)
859 if(Dx(bot)*Dy(bot) == 0)
862 /* no points in bot - top */
863 if(rectinrect(bot, top))
866 /* bot - top ≡ bot */
867 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
868 gendrawop(dst, bot, src, sp, mask, mp, op);
873 /* split bot into rectangles that don't intersect top */
875 if(bot.min.x < top.min.x){
876 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
877 delta = subpt(r.min, origin);
878 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
879 bot.min.x = top.min.x;
883 if(bot.max.x > top.max.x){
884 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
885 delta = subpt(r.min, origin);
886 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
887 bot.max.x = top.max.x;
891 if(bot.min.y < top.min.y){
892 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
893 delta = subpt(r.min, origin);
894 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
895 bot.min.y = top.min.y;
899 if(bot.max.y > top.max.y){
900 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
901 delta = subpt(r.min, origin);
902 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
903 bot.max.y = top.max.y;
908 alphachan(ulong chan)
910 for(; chan; chan >>= 8)
911 if(TYPE(chan) == CAlpha)
917 zoomdraw(Image *d, Rectangle r, Rectangle top, Image *b, Image *s, Point sp, int f)
925 if(r.min.x < d->r.min.x){
926 sp.x += (d->r.min.x - r.min.x)/f;
927 a.x = (d->r.min.x - r.min.x)%f;
928 r.min.x = d->r.min.x;
930 if(r.min.y < d->r.min.y){
931 sp.y += (d->r.min.y - r.min.y)/f;
932 a.y = (d->r.min.y - r.min.y)%f;
933 r.min.y = d->r.min.y;
936 w = s->r.max.x - sp.x;
940 dr.max.x = dr.min.x+w;
941 if(!alphachan(s->chan))
944 if(b) gendrawdiff(d, dr, top, b, sp, nil, ZP, SoverD);
945 gendrawdiff(d, dr, top, s, sp, nil, ZP, SoverD);
948 if((t = allocimage(display, dr, s->chan, 0, 0)) == nil)
950 for(; dr.min.y < r.max.y; dr.min.y++){
951 dr.max.y = dr.min.y+1;
952 draw(t, dr, s, nil, sp);
959 for(sp=dr.min; dr.min.x < r.max.x; sp.x++){
960 dr.max.x = dr.min.x+1;
961 if(b) gendrawdiff(d, dr, top, b, sp, nil, ZP, SoverD);
962 gendrawdiff(d, dr, top, t, sp, nil, ZP, SoverD);
963 for(dr.min.x++; ++a.x < f && dr.min.x < r.max.x; dr.min.x++){
964 dr.max.x = dr.min.x+1;
965 gendrawdiff(d, dr, top, d, Pt(dr.min.x-1, dr.min.y), nil, ZP, SoverD);
975 return p->image ? mulpt(subpt(p->image->r.max, p->image->r.min), zoom) : ZP;
979 drawframe(Rectangle r)
981 border(screen, r, -Borderwidth, frame, ZP);
982 gendrawdiff(screen, screen->r, insetrect(r, -Borderwidth), ground, ZP, nil, ZP, SoverD);
983 flushimage(display, 1);
993 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
994 zoomdraw(screen, r, ZR, paper, i, i->r.min, zoom);
996 r = Rpt(ZP, stringsize(font, p->label));
997 r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2),
998 divpt(r.max, 2)), screen->r.min));
999 draw(screen, r, paper, nil, ZP);
1000 string(screen, r.min, display->black, ZP, font, p->label);
1006 translate(Page *p, Point d)
1012 if(i==0 || d.x==0 && d.y==0)
1014 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
1015 pos = addpt(pos, d);
1016 nr = rectaddpt(r, d);
1017 rectclip(&r, screen->r);
1018 draw(screen, rectaddpt(r, d), screen, nil, r.min);
1019 zoomdraw(screen, nr, rectaddpt(r, d), paper, i, i->r.min, zoom);
1024 findpage(char *name)
1030 /* look in current document first */
1031 if(current && current->up){
1032 for(p = current->up->down; p; p = p->next)
1033 if(cistrncmp(p->label, name, n) == 0)
1036 /* look everywhere */
1037 for(p = root->down; p; p = nextpage(p))
1038 if(cistrncmp(p->label, name, n) == 0)
1048 for(p = root->down; i > 0 && p; p = nextpage(p))
1059 for(i = 0, p = root->down; p && p != x; p = nextpage(p))
1061 return (p == x) ? i : -1;
1081 esetcursor(&reading);
1087 switch(rfork(RFPROC|RFMEM)){
1089 sysfatal("rfork: %r");
1091 loadpages(p, NAHEAD, oviewgen);
1101 for(p = nextpage(current); p; p = nextpage(p))
1102 if(p->image || p->open)
1112 for(p = prevpage(current); p; p = prevpage(p))
1113 if(p->image || p->open)
1121 char nam[64], *argv[4];
1126 esetcursor(&reading);
1128 if((fd = openpage(p)) < 0)
1130 if(rfork(RFPROC|RFMEM|RFFDG|RFENVG|RFNOTEG|RFNOWAIT) == 0){
1131 dupfds(fd, 1, 2, -1);
1132 snprint(nam, sizeof nam, "/bin/%s", argv0);
1137 sysfatal("exec: %r");
1150 lockdisplay(display);
1151 if(new && getwindow(display, Refnone) == -1)
1152 sysfatal("getwindow: %r");
1159 unlockdisplay(display);
1162 void killcohort(void)
1165 for(i=0;i!=3;i++){ /* It's a long way to the kitchen */
1166 postnote(PNGROUP, getpid(), "kill");
1171 void drawerr(Display *, char *msg)
1173 sysfatal("draw: %s", msg);
1179 fprint(2, "usage: %s [ -iRw ] [ -p ppi ] [ file ... ]\n", argv0);
1184 main(int argc, char *argv[])
1186 enum { Eplumb = 4 };
1211 ppi = atoi(EARGF(usage()));
1218 * so that we can stop all subprocesses with a note,
1219 * and to isolate rendezvous from other processes
1221 rfork(RFNOTEG|RFNAMEG|RFREND);
1223 atnotify(catchnote, 1);
1225 s = smprint("-pid %d", getpid());
1226 if(newwindow(s) < 0)
1227 sysfatal("newwindow: %r");
1230 initdraw(drawerr, nil, argv0);
1231 paper = display->white;
1232 frame = display->black;
1233 ground = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x777777FF);
1234 display->locking = 1;
1235 unlockdisplay(display);
1236 einit(Ekeyboard|Emouse);
1237 eplumb(Eplumb, "image");
1238 if((nullfd = open("/dev/null", ORDWR)) < 0)
1239 sysfatal("open: %r");
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)
1341 if(root->down == nil)
1344 pagemenu.lasthit = pageindex(current);
1345 x = pageat(emenuhit(3, &m, &pagemenu));
1347 unlockdisplay(display);
1352 unlockdisplay(display);
1361 if(current == nil || !canqlock(current))
1363 lockdisplay(display);
1365 translate(current, Pt(0, Dy(screen->r)/2));
1366 unlockdisplay(display);
1370 unlockdisplay(display);
1372 if(prevpage(current))
1380 if(current == nil || !canqlock(current))
1382 o = addpt(pos, pagesize(current));
1383 lockdisplay(display);
1384 if(o.y > Dy(screen->r)){
1385 translate(current, Pt(0, -Dy(screen->r)/2));
1386 unlockdisplay(display);
1390 unlockdisplay(display);
1392 if(nextpage(current))
1396 showpage(findpage(jump));
1406 if(i+1 < sizeof(jump)){
1414 if(pm && pm->ndata > 0){
1418 s = plumblookup(pm->attr, "action");
1419 if(s && strcmp(s, "quit")==0)
1421 if(s && strcmp(s, "showdata")==0){
1422 if((fd = createtmp("plumb")) < 0){
1423 fprint(2, "plumb: createtmp: %r\n");
1427 if(fd2path(fd, s, NPATH) < 0){
1431 write(fd, pm->data, pm->ndata);
1432 }else if(pm->data[0] == '/'){
1433 s = strdup(pm->data);
1435 s = malloc(strlen(pm->wdir)+1+pm->ndata+1);
1436 sprint(s, "%s/%s", pm->wdir, pm->data);
1439 showpage(addpage(root, shortname(s), popenfile, s, fd));