9 typedef struct Page Page;
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);
120 up->down = up->tail = p;
128 if(up && current == up)
134 resizewin(Point size)
138 if((wctl = open("/dev/wctl", OWRITE)) < 0)
141 size = addpt(size, Pt(Borderwidth*2, Borderwidth*2));
142 fprint(wctl, "resize -dx %d -dy %d\n", size.x, size.y);
147 createtmp(ulong id, char *pfx)
151 snprint(nam, sizeof nam, "%s%s%.12d%.8lux", pagespool, pfx, getpid(), id ^ 0xcafebabe);
152 return create(nam, OEXCL|ORCLOSE|ORDWR, 0600);
156 catchnote(void *, char *msg)
158 if(strstr(msg, "sys: write on closed pipe"))
160 if(strstr(msg, "hangup"))
162 if(strstr(msg, "alarm"))
168 pipeline(int fd, char *fmt, ...)
170 char buf[128], *argv[4];
179 switch(rfork(RFPROC|RFFDG|RFREND|RFNOWAIT)){
193 vsnprint(buf, sizeof buf, fmt, arg);
200 exec("/bin/rc", argv);
201 sysfatal("exec: %r");
217 if((fd = dup(p->fd, -1)) < 0){
225 pipeline(fd, "%s", (char*)p->data);
228 * dont keep the file descriptor arround if it can simply
231 fd2path(p->fd, nam, sizeof(nam));
232 if(strncmp(nam, pagespool, strlen(pagespool))){
235 p->data = strdup(nam);
245 char mnt[32], cmd[64], *argv[4];
248 snprint(mnt, sizeof(mnt), "/n/tapefs.%.12d%.8lux", getpid(), (ulong)p);
249 switch(rfork(RFREND|RFPROC|RFFDG)){
259 snprint(cmd, sizeof(cmd), "%s -m %s /fd/0", p->data, mnt);
262 exec("/bin/rc", argv);
263 sysfatal("exec: %r");
268 p->data = strdup(mnt);
274 typedef struct Ghost Ghost;
293 switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
302 fprint(gs->pin, "%s DoPDFPage\n"
305 "dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring "
306 "flushfile\n", p->label);
307 while((n = read(gs->pdat, buf, sizeof buf)) > 0){
308 if(memcmp(buf, "THIS IS NOT AN INFERNO BITMAP\n", 30) == 0)
312 if(write(pfd[1], buf, n) != n){
325 infernobithdr(char *buf, int n)
328 if(memcmp(buf, "compressed\n", 11) == 0)
330 if(strtochan((char*)buf))
332 if(memcmp(buf, " ", 10) == 0 &&
333 '0' <= buf[10] && buf[10] <= '9' &&
343 int n, i, pdf, ifd, ofd, pin[2], pout[2], pdat[2];
344 char buf[NBUF], nam[32], *argv[16];
350 if(read(ifd, buf, 5) != 5)
353 if(memcmp(buf, "%PDF-", 5) == 0)
355 p->text = strdup(p->label);
374 switch(rfork(RFREND|RFPROC|RFFDG|RFNOWAIT)){
381 if(dup(pout[1], 1)<0)
391 if(dup(pdat[1], 3)<0)
405 pipeline(4, "%s", (char*)p->data);
409 argv[2] = "-sDEVICE=plan9";
410 argv[3] = "-sOutputFile=/fd/3";
412 argv[5] = pdf ? "-dDELAYSAFER" : "-dSAFER";
414 argv[7] = "-dTextAlphaBits=4";
415 argv[8] = "-dGraphicsAlphaBits=4";
416 snprint(buf, sizeof buf, "-r%d", ppi);
418 argv[10] = "-dDOINTERPOLATE";
419 argv[11] = pdf ? "-" : "/fd/4";
421 exec("/bin/gs", argv);
422 sysfatal("exec: %r");
433 "/PAGEOUT (/fd/1) (w) file def\n"
434 "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n"
438 "/PDFSave null def\n"
439 "/DSCPageCount 0 def\n"
440 "/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n"
442 "GS_PDF_ProcSet begin\n"
444 "(/fd/4) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n"
446 "pdfpagecount PAGE==\n";
449 if(write(pin[0], prolog, n) != n)
451 if((n = read(pout[0], buf, sizeof(buf)-1)) < 0)
459 gs = mallocz(sizeof(*gs), 1);
464 snprint(nam, sizeof nam, "%d", i);
465 addpage(p, nam, popenpdf, gs, -1);
468 /* keep ghostscript arround */
473 while((n = read(pdat[0], buf, sizeof(buf))) >= 0){
474 if(ofd >= 0 && (n <= 0 || infernobithdr(buf, n))){
475 snprint(nam, sizeof nam, "%d", i);
476 addpage(p, nam, popenconv, nil, ofd);
482 snprint(nam, sizeof nam, "%.4d", ++i);
483 if((ofd = createtmp((ulong)p, nam)) < 0)
484 ofd = dup(nullfd, -1);
486 if(write(ofd, buf, n) != n)
500 dircmp(void *p1, void *p2)
507 return strcmp(d1->name, d2->name);
513 char buf[NBUF], *file;
521 if((fd = open(file, OREAD)) < 0){
529 if((d = dirfstat(fd)) == nil){
537 if((n = dirreadall(fd, &d)) < 0)
539 qsort(d, n, sizeof d[0], dircmp);
541 addpage(p, d[i].name, popenfile, smprint("%s/%s", file, d[i].name), -1);
543 p->text = strdup(p->label);
548 memset(buf, 0, 32+1);
549 if((n = read(fd, buf, 32)) <= 0)
555 if(memcmp(buf, "%PDF-", 5) == 0 || strstr(buf, "%!"))
557 else if(memcmp(buf, "x T ", 4) == 0){
558 p->data = "lp -dstdout";
561 else if(memcmp(buf, "\xF7\x02\x01\x83\x92\xC0\x1C;", 8) == 0){
562 p->data = "dvips -Pps -r0 -q1 -f1";
565 else if(memcmp(buf, "\x1F\x8B", 2) == 0){
569 else if(memcmp(buf, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", 8) == 0){
573 else if(memcmp(buf, "PK\x03\x04", 4) == 0){
574 p->data = "fs/zipfs";
577 else if(memcmp(buf, "GIF", 3) == 0)
579 else if(memcmp(buf, "\111\111\052\000", 4) == 0)
580 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
581 else if(memcmp(buf, "\115\115\000\052", 4) == 0)
582 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
583 else if(memcmp(buf, "\377\330\377", 3) == 0)
585 else if(memcmp(buf, "\211PNG\r\n\032\n", 3) == 0)
587 else if(memcmp(buf, "\0PC Research, Inc", 17) == 0)
588 p->data = "aux/g3p9bit -g";
589 else if(memcmp(buf, "TYPE=ccitt-g31", 14) == 0)
590 p->data = "aux/g3p9bit -g";
591 else if(memcmp(buf, "II*", 3) == 0)
592 p->data = "aux/g3p9bit -g";
593 else if(memcmp(buf, "TYPE=", 5) == 0)
594 p->data = "fb/3to1 rgbv |fb/pcp -tplan9";
595 else if(buf[0] == 'P' && '0' <= buf[1] && buf[1] <= '9')
597 else if(memcmp(buf, "BM", 2) == 0)
599 else if(infernobithdr(buf, n))
602 werrstr("unknown image format");
606 if(seek(fd, 0, 0) < 0)
608 if((i = read(fd, buf+n, n)) < 0)
610 if(i != n || memcmp(buf, buf+n, i)){
613 if((tfd = createtmp((ulong)p, "file")) < 0)
616 if(write(tfd, buf, n) != n)
618 if((n = read(fd, buf, sizeof(buf))) < 0)
621 if(dup(tfd, fd) < 0){
652 for(p = root->down; p; p = t)
653 if((t = nextpage(p)) == x)
665 if(p->open == nil || (fd = p->open(p)) < 0)
669 pipeline(fd, "rotate -r %d", rotate);
671 pipeline(fd, "resize -x %d", resize.x);
673 pipeline(fd, "resize -y %d", resize.y);
683 if(p->open && p->image == nil && p->text == nil){
684 if((fd = openpage(p)) >= 0){
686 p->image = readimage(display, fd, 1);
689 if(p->image == nil && p->text == nil)
690 p->text = smprint("%s: %r", p->label);
703 lockdisplay(display);
705 unlockdisplay(display);
716 for(p = root->down; p; p = nextpage(p)){
717 if(age == 0) /* synchronous flush */
719 else if(!canqlock(p))
721 if((pagegen - p->gen) >= age)
728 loadpages(Page *p, int ahead, int oviewgen)
732 ahead++; /* load at least one */
733 unloadpages(ahead*2);
734 for(i = 0; i < ahead && p; p = nextpage(p), i++){
735 if(viewgen != oviewgen)
739 if(viewgen != oviewgen){
749 if(size.x && size.y && newwin){
753 lockdisplay(display);
755 unlockdisplay(display);
763 * A draw operation that touches only the area contained in bot but not in top.
764 * mp and sp get aligned with bot.min.
767 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
768 Image *src, Point sp, Image *mask, Point mp, int op)
776 if(Dx(bot)*Dy(bot) == 0)
779 /* no points in bot - top */
780 if(rectinrect(bot, top))
783 /* bot - top ≡ bot */
784 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
785 gendrawop(dst, bot, src, sp, mask, mp, op);
790 /* split bot into rectangles that don't intersect top */
792 if(bot.min.x < top.min.x){
793 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
794 delta = subpt(r.min, origin);
795 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
796 bot.min.x = top.min.x;
800 if(bot.max.x > top.max.x){
801 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
802 delta = subpt(r.min, origin);
803 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
804 bot.max.x = top.max.x;
808 if(bot.min.y < top.min.y){
809 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
810 delta = subpt(r.min, origin);
811 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
812 bot.min.y = top.min.y;
816 if(bot.max.y > top.max.y){
817 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
818 delta = subpt(r.min, origin);
819 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
820 bot.max.y = top.max.y;
825 zoomdraw(Image *d, Rectangle r, Rectangle top, Image *s, Point sp, int f)
832 gendrawdiff(d, r, top, s, sp, nil, ZP, S);
836 if(r.min.x < d->r.min.x){
837 sp.x += (d->r.min.x - r.min.x)/f;
838 a.x = (d->r.min.x - r.min.x)%f;
839 r.min.x = d->r.min.x;
841 if(r.min.y < d->r.min.y){
842 sp.y += (d->r.min.y - r.min.y)/f;
843 a.y = (d->r.min.y - r.min.y)%f;
844 r.min.y = d->r.min.y;
847 w = s->r.max.x - sp.x;
850 t = allocimage(display, Rect(r.min.x, r.min.y, r.min.x+w, r.max.y), s->chan, 0, DNofill);
853 for(y=r.min.y; y<r.max.y; y++){
854 draw(t, Rect(r.min.x, y, r.min.x+w, y+1), s, nil, sp);
861 for(x=r.min.x; x<r.max.x; x++){
862 gendrawdiff(d, Rect(x, r.min.y, x+1, r.max.y), top, t, sp, nil, ZP, S);
874 return p->image ? mulpt(subpt(p->image->r.max, p->image->r.min), zoom) : ZP;
883 if((i = p->image) == nil){
886 if((s = p->text) == nil)
889 r.max = stringsize(font, p->text);
890 r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2), divpt(r.max, 2)),
892 draw(screen, r, display->white, nil, ZP);
893 string(screen, r.min, display->black, ZP, font, s);
895 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
896 zoomdraw(screen, r, ZR, i, i->r.min, zoom);
898 gendrawdiff(screen, screen->r, r, background, ZP, nil, ZP, S);
899 border(screen, r, -Borderwidth, display->black, ZP);
900 flushimage(display, 1);
904 translate(Page *p, Point d)
910 if((i==0) || (d.x==0 && d.y==0))
912 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
914 nr = rectaddpt(r, d);
916 rectclip(&or, screen->r);
917 draw(screen, rectaddpt(or, d), screen, nil, or.min);
918 zoomdraw(screen, nr, rectaddpt(or, d), i, i->r.min, zoom);
919 gendrawdiff(screen, screen->r, nr, background, ZP, nil, ZP, S);
920 border(screen, nr, -Borderwidth, display->black, ZP);
921 flushimage(display, 1);
931 /* look in current document first */
932 if(current && current->up){
933 for(p = current->up->down; p; p = p->next)
934 if(cistrncmp(p->label, name, n) == 0)
937 /* look everywhere */
938 for(p = root->down; p; p = nextpage(p))
939 if(cistrncmp(p->label, name, n) == 0)
949 for(p = root->down; i > 0 && p; p = nextpage(p))
960 for(i = 0, p = root->down; p && p != x; p = nextpage(p))
962 return (p == x) ? i : -1;
982 esetcursor(&reading);
988 switch(rfork(RFPROC|RFMEM)){
990 sysfatal("rfork: %r");
992 loadpages(p, NAHEAD, oviewgen);
1000 char nam[64], *argv[4];
1005 esetcursor(&reading);
1007 if((fd = openpage(p)) < 0)
1009 if(rfork(RFREND|RFFDG|RFPROC|RFENVG|RFNOTEG|RFNOWAIT) == 0){
1013 snprint(nam, sizeof nam, "/bin/%s", argv0);
1018 sysfatal("exec: %r");
1031 lockdisplay(display);
1032 if(new && getwindow(display, Refnone) == -1)
1033 sysfatal("getwindow: %r");
1040 unlockdisplay(display);
1043 void killcohort(void)
1046 for(i=0;i!=3;i++){ /* It's a long way to the kitchen */
1047 postnote(PNGROUP, getpid(), "kill");
1052 void drawerr(Display *, char *msg)
1054 sysfatal("draw: %s", msg);
1061 if(x = strrchr(s, '/'))
1070 fprint(2, "usage: %s [ -iRw ] [ -p ppi ] [ file ... ]\n", argv0);
1075 main(int argc, char *argv[])
1077 enum { Eplumb = 4 };
1102 ppi = atoi(EARGF(usage()));
1109 * so that we can stop all subprocesses with a note,
1110 * and to isolate rendezvous from other processes
1112 rfork(RFNOTEG|RFNAMEG|RFREND);
1114 atnotify(catchnote, 1);
1116 s = smprint("-pid %d", getpid());
1117 if(newwindow(s) < 0)
1118 sysfatal("newwindow: %r");
1121 initdraw(drawerr, nil, argv0);
1122 background = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x777777FF);
1123 draw(screen, screen->r, background, nil, ZP);
1124 flushimage(display, 1);
1125 display->locking = 1;
1126 unlockdisplay(display);
1127 einit(Ekeyboard|Emouse);
1128 eplumb(Eplumb, "image");
1129 nullfd = open("/dev/null", ORDWR);
1130 current = root = addpage(nil, "root", nil, nil, -1);
1132 if(*argv == nil && !imode)
1133 addpage(root, "stdin", popenfile, strdup("/fd/0"), -1);
1134 for(; *argv; argv++)
1135 addpage(root, shortname(*argv), popenfile, strdup(*argv), -1);
1142 lockdisplay(display);
1145 if(current == nil || !canqlock(current))
1150 if((m.buttons & 1) == 0)
1152 translate(current, subpt(m.xy, o));
1158 i = emenuhit(2, &m, &menu);
1159 if(i < 0 || i >= nelem(menuitems) || menuitems[i]==nil)
1162 if(strcmp(s, "orig size")==0){
1169 unlockdisplay(display);
1170 esetcursor(&reading);
1175 if(strncmp(s, "rotate ", 7)==0){
1176 rotate += atoi(s+7);
1180 if(strcmp(s, "upside down")==0){
1184 if(strcmp(s, "fit width")==0){
1187 resize = subpt(screen->r.max, screen->r.min);
1191 if(strcmp(s, "fit height")==0){
1194 resize = subpt(screen->r.max, screen->r.min);
1198 if(strncmp(s, "zoom", 4)==0){
1199 if(current && canqlock(current)){
1200 o = subpt(m.xy, screen->r.min);
1201 if(strstr(s, "in")){
1202 if(zoom < 0x40000000){
1204 pos = addpt(mulpt(subpt(pos, o), 2), o);
1209 pos = addpt(divpt(subpt(pos, o), 2), o);
1216 unlockdisplay(display);
1217 if(strcmp(s, "next")==0)
1218 showpage(nextpage(current));
1219 if(strcmp(s, "prev")==0)
1220 showpage(prevpage(current));
1221 if(strcmp(s, "zerox")==0)
1223 if(strcmp(s, "quit")==0)
1228 if(root->down == nil)
1230 pagemenu.lasthit = pageindex(current);
1231 i = emenuhit(3, &m, &pagemenu);
1232 unlockdisplay(display);
1234 showpage(pageat(i));
1238 unlockdisplay(display);
1247 if(current == nil || !canqlock(current))
1249 lockdisplay(display);
1251 translate(current, Pt(0, Dy(screen->r)/2));
1252 unlockdisplay(display);
1256 unlockdisplay(display);
1258 if(prevpage(current))
1263 showpage(prevpage(current));
1266 if(current == nil || !canqlock(current))
1268 o = addpt(pos, pagesize(current));
1269 lockdisplay(display);
1270 if(o.y > Dy(screen->r)){
1271 translate(current, Pt(0, -Dy(screen->r)/2));
1272 unlockdisplay(display);
1276 unlockdisplay(display);
1278 if(nextpage(current))
1282 showpage(findpage(jump));
1288 showpage(nextpage(current));
1292 if(i+1 < sizeof(jump)){
1300 if(pm && pm->ndata > 0){
1304 s = plumblookup(pm->attr, "action");
1305 if(s && strcmp(s, "quit")==0)
1307 if(s && strcmp(s, "showdata")==0){
1308 static ulong plumbid;
1310 if((fd = createtmp(plumbid++, "plumb")) < 0){
1311 fprint(2, "plumb: createtmp: %r\n");
1315 if(fd2path(fd, s, NPATH) < 0){
1319 write(fd, pm->data, pm->ndata);
1320 }else if(pm->data[0] == '/'){
1321 s = strdup(pm->data);
1323 s = malloc(strlen(pm->wdir)+1+pm->ndata+1);
1324 sprint(s, "%s/%s", pm->wdir, pm->data);
1327 showpage(addpage(root, shortname(s), popenfile, s, fd));