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[64], *argv[4];
247 snprint(mnt, sizeof(mnt), "/n/tapefs.%.12d%.8lux", getpid(), (ulong)p);
248 switch(rfork(RFREND|RFPROC|RFFDG)){
258 snprint(cmd, sizeof(cmd), "%s -m %s /fd/0", p->data, mnt);
261 exec("/bin/rc", argv);
262 sysfatal("exec: %r");
267 p->data = strdup(mnt);
273 typedef struct Ghost Ghost;
292 switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
301 fprint(gs->pin, "%s DoPDFPage\n"
304 "dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring "
305 "flushfile\n", p->label);
306 while((n = read(gs->pdat, buf, sizeof buf)) > 0){
307 if(memcmp(buf, "THIS IS NOT AN INFERNO BITMAP\n", 30) == 0)
311 if(write(pfd[1], buf, n) != n){
324 infernobithdr(char *buf, int n)
327 if(memcmp(buf, "compressed\n", 11) == 0)
329 if(strtochan((char*)buf))
331 if(memcmp(buf, " ", 10) == 0 &&
332 '0' <= buf[10] && buf[10] <= '9' &&
342 int n, i, pdf, ifd, ofd, pin[2], pout[2], pdat[2];
343 char buf[NBUF], nam[32], *argv[16];
349 if(read(ifd, buf, 5) != 5)
352 if(memcmp(buf, "%PDF-", 5) == 0)
354 p->text = strdup(p->label);
373 switch(rfork(RFREND|RFPROC|RFFDG|RFNOWAIT)){
380 if(dup(pout[1], 1)<0)
390 if(dup(pdat[1], 3)<0)
404 pipeline(4, "%s", (char*)p->data);
408 argv[2] = "-sDEVICE=plan9";
409 argv[3] = "-sOutputFile=/fd/3";
411 argv[5] = pdf ? "-dDELAYSAFER" : "-dSAFER";
413 argv[7] = "-dTextAlphaBits=4";
414 argv[8] = "-dGraphicsAlphaBits=4";
415 snprint(buf, sizeof buf, "-r%d", ppi);
417 argv[10] = "-dDOINTERPOLATE";
418 argv[11] = pdf ? "-" : "/fd/4";
420 exec("/bin/gs", argv);
421 sysfatal("exec: %r");
432 "/PAGEOUT (/fd/1) (w) file def\n"
433 "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n"
437 "/PDFSave null def\n"
438 "/DSCPageCount 0 def\n"
439 "/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n"
441 "GS_PDF_ProcSet begin\n"
443 "(/fd/4) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n"
445 "pdfpagecount PAGE==\n";
448 if(write(pin[0], prolog, n) != n)
450 if((n = read(pout[0], buf, sizeof(buf)-1)) < 0)
458 gs = mallocz(sizeof(*gs), 1);
463 snprint(nam, sizeof nam, "%d", i);
464 addpage(p, nam, popenpdf, gs, -1);
467 /* keep ghostscript arround */
472 while((n = read(pdat[0], buf, sizeof(buf))) >= 0){
473 if(ofd >= 0 && (n <= 0 || infernobithdr(buf, n))){
474 snprint(nam, sizeof nam, "%d", i);
475 addpage(p, nam, popenconv, nil, ofd);
481 snprint(nam, sizeof nam, "%.4d", ++i);
482 if((ofd = createtmp((ulong)p, nam)) < 0)
483 ofd = dup(nullfd, -1);
485 if(write(ofd, buf, n) != n)
499 dircmp(void *p1, void *p2)
506 return strcmp(d1->name, d2->name);
512 char buf[NBUF], *file;
520 if((fd = open(file, OREAD)) < 0){
528 if((d = dirfstat(fd)) == nil){
536 if((n = dirreadall(fd, &d)) < 0)
538 qsort(d, n, sizeof d[0], dircmp);
540 addpage(p, d[i].name, popenfile, smprint("%s/%s", file, d[i].name), -1);
542 p->text = strdup(p->label);
547 memset(buf, 0, 32+1);
548 if((n = read(fd, buf, 32)) <= 0)
554 if(memcmp(buf, "%PDF-", 5) == 0 || strstr(buf, "%!"))
556 else if(memcmp(buf, "x T ", 4) == 0){
557 p->data = "lp -dstdout";
560 else if(memcmp(buf, "\xF7\x02\x01\x83\x92\xC0\x1C;", 8) == 0){
561 p->data = "dvips -Pps -r0 -q1 -f1";
564 else if(memcmp(buf, "\x1F\x8B", 2) == 0){
568 else if(memcmp(buf, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", 8) == 0){
572 else if(memcmp(buf, "PK\x03\x04", 4) == 0){
573 p->data = "fs/zipfs";
576 else if(memcmp(buf, "GIF", 3) == 0)
578 else if(memcmp(buf, "\111\111\052\000", 4) == 0)
579 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
580 else if(memcmp(buf, "\115\115\000\052", 4) == 0)
581 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
582 else if(memcmp(buf, "\377\330\377", 3) == 0)
584 else if(memcmp(buf, "\211PNG\r\n\032\n", 3) == 0)
586 else if(memcmp(buf, "\0PC Research, Inc", 17) == 0)
587 p->data = "aux/g3p9bit -g";
588 else if(memcmp(buf, "TYPE=ccitt-g31", 14) == 0)
589 p->data = "aux/g3p9bit -g";
590 else if(memcmp(buf, "II*", 3) == 0)
591 p->data = "aux/g3p9bit -g";
592 else if(memcmp(buf, "TYPE=", 5) == 0)
593 p->data = "fb/3to1 rgbv |fb/pcp -tplan9";
594 else if(buf[0] == 'P' && '0' <= buf[1] && buf[1] <= '9')
596 else if(memcmp(buf, "BM", 2) == 0)
598 else if(infernobithdr(buf, n))
601 werrstr("unknown image format");
605 if(seek(fd, 0, 0) < 0)
607 if((i = read(fd, buf+n, n)) < 0)
609 if(i != n || memcmp(buf, buf+n, i)){
612 if((tfd = createtmp((ulong)p, "file")) < 0)
615 if(write(tfd, buf, n) != n)
617 if((n = read(fd, buf, sizeof(buf))) < 0)
620 if(dup(tfd, fd) < 0){
651 for(p = root->down; p; p = t)
652 if((t = nextpage(p)) == x)
664 if(p->open == nil || (fd = p->open(p)) < 0)
668 pipeline(fd, "rotate -r %d", rotate);
670 pipeline(fd, "resize -x %d", resize.x);
672 pipeline(fd, "resize -y %d", resize.y);
682 if(p->open && p->image == nil && p->text == nil){
683 if((fd = openpage(p)) >= 0){
685 p->image = readimage(display, fd, 1);
688 if(p->image == nil && p->text == nil)
689 p->text = smprint("%s: %r", p->label);
702 lockdisplay(display);
704 unlockdisplay(display);
715 for(p = root->down; p; p = nextpage(p)){
716 if(age == 0) /* synchronous flush */
718 else if(!canqlock(p))
720 if((pagegen - p->gen) >= age)
727 loadpages(Page *p, int ahead, int oviewgen)
731 ahead++; /* load at least one */
732 unloadpages(ahead*2);
733 for(i = 0; i < ahead && p; p = nextpage(p), i++){
734 if(viewgen != oviewgen)
738 if(viewgen != oviewgen){
748 if(size.x && size.y && newwin){
752 lockdisplay(display);
754 unlockdisplay(display);
762 * A draw operation that touches only the area contained in bot but not in top.
763 * mp and sp get aligned with bot.min.
766 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
767 Image *src, Point sp, Image *mask, Point mp, int op)
775 if(Dx(bot)*Dy(bot) == 0)
778 /* no points in bot - top */
779 if(rectinrect(bot, top))
782 /* bot - top ≡ bot */
783 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
784 gendrawop(dst, bot, src, sp, mask, mp, op);
789 /* split bot into rectangles that don't intersect top */
791 if(bot.min.x < top.min.x){
792 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
793 delta = subpt(r.min, origin);
794 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
795 bot.min.x = top.min.x;
799 if(bot.max.x > top.max.x){
800 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
801 delta = subpt(r.min, origin);
802 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
803 bot.max.x = top.max.x;
807 if(bot.min.y < top.min.y){
808 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
809 delta = subpt(r.min, origin);
810 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
811 bot.min.y = top.min.y;
815 if(bot.max.y > top.max.y){
816 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
817 delta = subpt(r.min, origin);
818 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
819 bot.max.y = top.max.y;
824 zoomdraw(Image *d, Rectangle r, Rectangle top, Image *s, Point sp, int f)
831 gendrawdiff(d, r, top, s, sp, nil, ZP, S);
835 if(r.min.x < d->r.min.x){
836 sp.x += (d->r.min.x - r.min.x)/f;
837 a.x = (d->r.min.x - r.min.x)%f;
838 r.min.x = d->r.min.x;
840 if(r.min.y < d->r.min.y){
841 sp.y += (d->r.min.y - r.min.y)/f;
842 a.y = (d->r.min.y - r.min.y)%f;
843 r.min.y = d->r.min.y;
846 w = s->r.max.x - sp.x;
849 t = allocimage(display, Rect(r.min.x, r.min.y, r.min.x+w, r.max.y), s->chan, 0, DNofill);
852 for(y=r.min.y; y<r.max.y; y++){
853 draw(t, Rect(r.min.x, y, r.min.x+w, y+1), s, nil, sp);
860 for(x=r.min.x; x<r.max.x; x++){
861 gendrawdiff(d, Rect(x, r.min.y, x+1, r.max.y), top, t, sp, nil, ZP, S);
873 return p->image ? mulpt(subpt(p->image->r.max, p->image->r.min), zoom) : ZP;
882 if((i = p->image) == nil){
885 if((s = p->text) == nil)
888 r.max = stringsize(font, p->text);
889 r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2), divpt(r.max, 2)),
891 draw(screen, r, display->white, nil, ZP);
892 string(screen, r.min, display->black, ZP, font, s);
894 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
895 zoomdraw(screen, r, ZR, i, i->r.min, zoom);
897 gendrawdiff(screen, screen->r, r, display->white, ZP, nil, ZP, S);
898 border(screen, r, -Borderwidth, display->black, ZP);
899 flushimage(display, 1);
903 translate(Page *p, Point d)
909 if((i==0) || (d.x==0 && d.y==0))
911 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
913 nr = rectaddpt(r, d);
915 rectclip(&or, screen->r);
916 draw(screen, rectaddpt(or, d), screen, nil, or.min);
917 zoomdraw(screen, nr, rectaddpt(or, d), i, i->r.min, zoom);
918 gendrawdiff(screen, screen->r, nr, display->white, ZP, nil, ZP, S);
919 border(screen, nr, -Borderwidth, display->black, ZP);
920 flushimage(display, 1);
930 /* look in current document first */
931 if(current && current->up){
932 for(p = current->up->down; p; p = p->next)
933 if(cistrncmp(p->label, name, n) == 0)
936 /* look everywhere */
937 for(p = root->down; p; p = nextpage(p))
938 if(cistrncmp(p->label, name, n) == 0)
948 for(p = root->down; i > 0 && p; p = nextpage(p))
959 for(i = 0, p = root->down; p && p != x; p = nextpage(p))
961 return (p == x) ? i : -1;
981 esetcursor(&reading);
987 switch(rfork(RFPROC|RFMEM)){
989 sysfatal("rfork: %r");
991 loadpages(p, NAHEAD, oviewgen);
999 char nam[64], *argv[4];
1004 esetcursor(&reading);
1006 if((fd = openpage(p)) < 0)
1008 if(rfork(RFREND|RFFDG|RFPROC|RFENVG|RFNOTEG|RFNOWAIT) == 0){
1012 snprint(nam, sizeof nam, "/bin/%s", argv0);
1017 sysfatal("exec: %r");
1030 lockdisplay(display);
1031 if(new && getwindow(display, Refnone) == -1)
1032 sysfatal("getwindow: %r");
1039 unlockdisplay(display);
1042 void killcohort(void)
1045 for(i=0;i!=3;i++){ /* It's a long way to the kitchen */
1046 postnote(PNGROUP, getpid(), "kill");
1051 void drawerr(Display *, char *msg)
1053 sysfatal("draw: %s", msg);
1060 if(x = strrchr(s, '/'))
1069 fprint(2, "usage: %s [ -iRw ] [ -p ppi ] [ file ... ]\n", argv0);
1074 main(int argc, char *argv[])
1076 enum { Eplumb = 4 };
1101 ppi = atoi(EARGF(usage()));
1108 * so that we can stop all subprocesses with a note,
1109 * and to isolate rendezvous from other processes
1111 rfork(RFNOTEG|RFNAMEG|RFREND);
1113 atnotify(catchnote, 1);
1115 s = smprint("-pid %d", getpid());
1116 if(newwindow(s) < 0)
1117 sysfatal("newwindow: %r");
1120 initdraw(drawerr, nil, argv0);
1121 display->locking = 1;
1122 unlockdisplay(display);
1123 einit(Ekeyboard|Emouse);
1124 eplumb(Eplumb, "image");
1125 nullfd = open("/dev/null", ORDWR);
1126 current = root = addpage(nil, "root", nil, nil, -1);
1128 if(*argv == nil && !imode)
1129 addpage(root, "stdin", popenfile, strdup("/fd/0"), -1);
1130 for(; *argv; argv++)
1131 addpage(root, shortname(*argv), popenfile, strdup(*argv), -1);
1138 lockdisplay(display);
1141 if(current == nil || !canqlock(current))
1146 if((m.buttons & 1) == 0)
1148 translate(current, subpt(m.xy, o));
1154 i = emenuhit(2, &m, &menu);
1155 if(i < 0 || i >= nelem(menuitems) || menuitems[i]==nil)
1158 if(strcmp(s, "orig size")==0){
1165 unlockdisplay(display);
1166 esetcursor(&reading);
1171 if(strncmp(s, "rotate ", 7)==0){
1172 rotate += atoi(s+7);
1176 if(strcmp(s, "upside down")==0){
1180 if(strcmp(s, "fit width")==0){
1183 resize = subpt(screen->r.max, screen->r.min);
1187 if(strcmp(s, "fit height")==0){
1190 resize = subpt(screen->r.max, screen->r.min);
1194 if(strncmp(s, "zoom", 4)==0){
1195 if(current && canqlock(current)){
1196 o = subpt(m.xy, screen->r.min);
1197 if(strstr(s, "in")){
1198 if(zoom < 0x40000000){
1200 pos = addpt(mulpt(subpt(pos, o), 2), o);
1205 pos = addpt(divpt(subpt(pos, o), 2), o);
1212 unlockdisplay(display);
1213 if(strcmp(s, "next")==0)
1214 showpage(nextpage(current));
1215 if(strcmp(s, "prev")==0)
1216 showpage(prevpage(current));
1217 if(strcmp(s, "zerox")==0)
1219 if(strcmp(s, "quit")==0)
1224 if(root->down == nil)
1226 pagemenu.lasthit = pageindex(current);
1227 i = emenuhit(3, &m, &pagemenu);
1228 unlockdisplay(display);
1230 showpage(pageat(i));
1234 unlockdisplay(display);
1243 if(current == nil || !canqlock(current))
1245 lockdisplay(display);
1247 translate(current, Pt(0, Dy(screen->r)/2));
1248 unlockdisplay(display);
1252 unlockdisplay(display);
1254 if(prevpage(current))
1259 showpage(prevpage(current));
1262 if(current == nil || !canqlock(current))
1264 o = addpt(pos, pagesize(current));
1265 lockdisplay(display);
1266 if(o.y > Dy(screen->r)){
1267 translate(current, Pt(0, -Dy(screen->r)/2));
1268 unlockdisplay(display);
1272 unlockdisplay(display);
1274 if(nextpage(current))
1278 showpage(findpage(jump));
1284 showpage(nextpage(current));
1288 if(i+1 < sizeof(jump)){
1296 if(pm && pm->ndata > 0){
1300 s = plumblookup(pm->attr, "action");
1301 if(s && strcmp(s, "quit")==0)
1303 if(s && strcmp(s, "showdata")==0){
1304 static ulong plumbid;
1306 if((fd = createtmp(plumbid++, "plumb")) < 0){
1307 fprint(2, "plumb: createtmp: %r\n");
1311 if(fd2path(fd, s, NPATH) < 0){
1315 write(fd, pm->data, pm->ndata);
1316 }else if(pm->data[0] == '/'){
1317 s = strdup(pm->data);
1319 s = malloc(strlen(pm->wdir)+1+pm->ndata+1);
1320 sprint(s, "%s/%s", pm->wdir, pm->data);
1323 showpage(addpage(root, shortname(s), popenfile, s, fd));