2 * the actual viewer that handles screen stuff
20 int showbottom = 0; /* on the next showpage, move the image so the bottom is visible. */
22 Rectangle ulrange; /* the upper left corner of the image must be in this rectangle */
23 Point ul; /* the upper left corner of the image is at this point on the screen */
25 Point pclip(Point, Rectangle);
26 Rectangle mkrange(Rectangle screenr, Rectangle imr);
31 {0xff, 0x80, 0xff, 0x80, 0xff, 0x00, 0xfe, 0x00,
32 0xff, 0x00, 0xff, 0x80, 0xff, 0xc0, 0xef, 0xe0,
33 0xc7, 0xf0, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0xc0,
34 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, },
35 {0x00, 0x00, 0x7f, 0x00, 0x7e, 0x00, 0x7c, 0x00,
36 0x7e, 0x00, 0x7f, 0x00, 0x6f, 0x80, 0x47, 0xc0,
37 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xe0, 0x00, 0x40,
38 0x00, 0x00, 0x01, 0xb6, 0x01, 0xb6, 0x00, 0x00, }
43 {0x0f, 0xf0, 0x1f, 0xf8, 0x3f, 0xfc, 0x7f, 0xfe,
44 0x7c, 0x7e, 0x78, 0x7e, 0x00, 0xfc, 0x01, 0xf8,
45 0x03, 0xf0, 0x07, 0xe0, 0x07, 0xc0, 0x07, 0xc0,
46 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, },
47 {0x00, 0x00, 0x0f, 0xf0, 0x1f, 0xf8, 0x3c, 0x3c,
48 0x38, 0x1c, 0x00, 0x3c, 0x00, 0x78, 0x00, 0xf0,
49 0x01, 0xe0, 0x03, 0xc0, 0x03, 0x80, 0x03, 0x80,
50 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x00, 0x00, }
67 wctl = open("/dev/wctl", OWRITE);
71 write(wctl, "unhide", 6);
90 static char menustr[32];
102 p = doc->pagename(doc, n);
103 len = (sizeof menustr)-2;
105 if(strlen(p) > len && strrchr(p, '/'))
106 p = strrchr(p, '/')+1;
110 strcpy(menustr+1, p);
119 showpage(int page, Menu *m)
124 m->lasthit = 0; /* this page */
126 m->lasthit = reverse ? doc->npage-1-page : page;
128 esetcursor(&reading);
130 if((page < 0 || page >= doc->npage) && !doc->fwdonly){
134 im = doc->drawpage(doc, page);
136 if(doc->fwdonly) /* this is how we know we're out of pages */
139 im = xallocimage(display, Rect(0,0,50,50), GREY1, 1, DBlack);
141 fprint(2, "out of memory: %r\n");
144 string(im, ZP, display->white, ZP, display->defaultfont, "?");
146 resize(Dx(im->r), Dy(im->r));
148 if(im->r.min.x > 0 || im->r.min.y > 0) {
149 tmp = xallocimage(display, Rect(0, 0, Dx(im->r), Dy(im->r)), im->chan, 0, DNofill);
151 fprint(2, "out of memory during showpage: %r\n");
154 drawop(tmp, tmp->r, im, nil, im->r.min, S);
164 ul.y = screen->r.max.y - Dy(im->r);
169 flushimage(display, 1);
177 static char result[200];
184 memset(basename, 0, sizeof basename);
186 strncpy(basename, doc->docname, sizeof(basename)-1);
187 else if((p = menugen(page)) && p[0] != '\0')
188 strncpy(basename, p+1, sizeof(basename)-1);
191 if(q = strrchr(basename, '/'))
195 if(p = strchr(q, '.'))
198 memset(name, 0, sizeof name);
199 snprint(name, sizeof(name)-1, "%s.%d.bit", q, page+1);
200 if(access(name, 0) >= 0) {
201 strcat(name, "XXXX");
204 if(access(name, 0) >= 0)
205 return "couldn't think of a name for bitmap";
207 strcpy(name, "bitXXXX");
209 if(access(name, 0) >= 0)
210 return "couldn't think of a name for bitmap";
213 if((fd = create(name, OWRITE, 0666)) < 0) {
214 snprint(result, sizeof result, "cannot create %s: %r", name);
218 if(writeimage(fd, im, 0) < 0) {
219 snprint(result, sizeof result, "cannot writeimage: %r");
225 snprint(result, sizeof result, "wrote %s", name);
229 static void translate(Point);
232 showdata(Plumbmsg *msg)
236 s = plumblookup(msg->attr, "action");
237 return s && strcmp(s, "showdata")==0;
240 /* correspond to entries in miditems[] below,
241 * changing one means you need to change
264 int i, fd, n, oldpage;
272 static char *fwditems[] = { "this page", "next page", "exit", 0 };
273 static char *miditems[] = {
295 doc = dd; /* save global for menuhit */
297 einit(Emouse|Ekeyboard);
298 if(doc->addpage != nil)
299 eplumb(Eplumb, "image");
301 esetcursor(&reading);
305 * im is a global pointer to the current image.
306 * eventually, i think we will have a layer between
307 * the display routines and the ps/pdf/whatever routines
308 * to perhaps cache and handle images of different
312 page = reverse ? doc->npage-1 : 0;
315 menu.item = fwditems;
324 midmenu.item = miditems;
326 midmenu.lasthit = Next;
328 showpage(page, &menu);
334 * throughout, if doc->fwdonly is set, we restrict the functionality
335 * a fair amount. we don't care about doc->npage anymore, and
336 * all that can be done is select the next page.
338 switch(eread(Emouse|Ekeyboard|Eplumb, &e)){
340 if(e.kbdc <= 0xFF && isdigit(e.kbdc)) {
341 nxt = nxt*10+e.kbdc-'0';
343 } else if(e.kbdc != '\n')
346 case 'r': /* reverse page order */
350 menu.lasthit = doc->npage-1-menu.lasthit;
353 * the theory is that if we are reversing the
354 * document order and are on the first or last
355 * page then we're just starting and really want
356 * to view the other end. maybe the if
357 * should be dropped and this should happen always.
359 if(page == 0 || page == doc->npage-1) {
360 page = doc->npage-1-page;
361 showpage(page, &menu);
364 case 'w': /* write bitmap of current screen */
365 esetcursor(&reading);
368 string(screen, addpt(screen->r.min, Pt(5,5)), display->black, ZP,
369 display->defaultfont, s);
371 flushimage(display, 1);
373 case 'd': /* remove image from working set */
374 if(doc->rmpage && page < doc->npage) {
375 if(doc->rmpage(doc, page) >= 0) {
378 if(page >= doc->npage)
380 showpage(page, &menu);
385 case 0x04: /* ctrl-d */
390 esetcursor(&reading);
395 flushimage(display, 1);
400 if(page > 0 && !doc->fwdonly) {
402 showpage(page, &menu);
408 if(nxt >= 0 && nxt < doc->npage && !doc->fwdonly)
409 showpage(page=nxt, &menu);
417 if(doc->npage && ++page >= doc->npage && !doc->fwdonly)
419 showpage(page, &menu);
423 * The upper y coordinate of the image is at ul.y in screen->r.
424 * Panning up means moving the upper left corner down. If the
425 * upper left corner is currently visible, we need to go back a page.
428 if(screen->r.min.y <= ul.y && ul.y < screen->r.max.y){
429 if(page > 0 && !doc->fwdonly){
432 showpage(page, &menu);
438 if(i+ul.y > screen->r.min.y)
439 i = screen->r.min.y - ul.y;
445 * If the lower y coordinate is on the screen, we go to the next page.
446 * The lower y coordinate is at ul.y + Dy(im->r).
449 i = ul.y + Dy(im->r);
450 if(screen->r.min.y <= i && i <= screen->r.max.y){
451 ul.y = screen->r.min.y;
454 i = -Dy(screen->r)/2;
457 if(i+ul.y+Dy(im->r) <= screen->r.max.y)
458 i = screen->r.max.y - Dy(im->r) - ul.y - 1;
477 dxy = subpt(m.xy, oxy);
481 } while(m.buttons == Left);
483 dxy = subpt(xy0, oxy);
492 n = emenuhit(Middle, &m, &midmenu);
496 case Next: /* next */
506 if((page >= doc->npage) && !doc->fwdonly)
509 showpage(page, &menu);
512 case Prev: /* prev */
522 if((page >= doc->npage) && !doc->fwdonly && !reverse)
525 showpage(page, &menu);
528 case Zerox: /* prev */
531 case Zin: /* zoom in */
536 r = egetrect(Middle, &m);
537 if((rectclip(&r, rectaddpt(im->r, ul)) == 0) ||
538 Dx(r) == 0 || Dy(r) == 0)
540 /* use the smaller side to expand */
542 delta = (double)Dx(im->r)/(double)Dx(r);
544 delta = (double)Dy(im->r)/(double)Dy(r);
546 esetcursor(&reading);
547 tmp = xallocimage(display,
548 Rect(0, 0, (int)((double)Dx(im->r)*delta), (int)((double)Dy(im->r)*delta)),
549 im->chan, 0, DBlack);
551 fprint(2, "out of memory during zoom: %r\n");
560 flushimage(display, 1);
568 delta = (double)Dx(screen->r)/(double)Dx(im->r);
569 if((double)Dy(im->r)*delta > Dy(screen->r))
570 delta = (double)Dy(screen->r)/(double)Dy(im->r);
572 r = Rect(0, 0, (int)((double)Dx(im->r)*delta), (int)((double)Dy(im->r)*delta));
573 esetcursor(&reading);
574 tmp = xallocimage(display, r, im->chan, 0, DBlack);
576 fprint(2, "out of memory during fit: %r\n");
585 flushimage(display, 1);
588 case Rot: /* rotate 90 */
589 esetcursor(&reading);
593 flushimage(display, 1);
595 case Upside: /* upside-down */
598 esetcursor(&reading);
603 flushimage(display, 1);
605 case Restore: /* restore */
606 showpage(page, &menu);
608 case Reverse: /* reverse */
612 menu.lasthit = doc->npage-1-menu.lasthit;
614 if(page == 0 || page == doc->npage-1) {
615 page = doc->npage-1-page;
616 showpage(page, &menu);
619 case Write: /* write */
620 esetcursor(&reading);
623 string(screen, addpt(screen->r.min, Pt(5,5)), display->black, ZP,
624 display->defaultfont, s);
626 flushimage(display, 1);
628 case Del: /* delete */
629 if(doc->rmpage && page < doc->npage) {
630 if(doc->rmpage(doc, page) >= 0) {
633 if(page >= doc->npage)
635 showpage(page, &menu);
639 case Exit: /* exit */
655 n = emenuhit(RMenu, &m, &menu);
661 case 0: /* this page */
663 case 1: /* next page */
664 showpage(++page, &menu);
675 page = reverse ? doc->npage-1-n : n;
678 showpage(page, &menu);
691 s = estrdup("/tmp/pageplumbXXXXXXX");
693 write(fd, pm->data, pm->ndata);
694 /* lose fd reference on purpose; the file is open ORCLOSE */
695 } else if(pm->data[0] == '/') {
696 s = estrdup(pm->data);
698 s = emalloc(strlen(pm->wdir)+1+pm->ndata+1);
699 sprint(s, "%s/%s", pm->wdir, pm->data);
702 if((i = doc->addpage(doc, s)) >= 0) {
705 showpage(page, &menu);
717 * A draw operation that touches only the area contained in bot but not in top.
718 * mp and sp get aligned with bot.min.
721 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
722 Image *src, Point sp, Image *mask, Point mp, int op)
730 if(Dx(bot)*Dy(bot) == 0)
733 /* no points in bot - top */
734 if(rectinrect(bot, top))
737 /* bot - top ≡ bot */
738 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
739 gendrawop(dst, bot, src, sp, mask, mp, op);
744 /* split bot into rectangles that don't intersect top */
746 if(bot.min.x < top.min.x){
747 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
748 delta = subpt(r.min, origin);
749 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
750 bot.min.x = top.min.x;
754 if(bot.max.x > top.max.x){
755 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
756 delta = subpt(r.min, origin);
757 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
758 bot.max.x = top.max.x;
762 if(bot.min.y < top.min.y){
763 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
764 delta = subpt(r.min, origin);
765 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
766 bot.min.y = top.min.y;
770 if(bot.max.y > top.max.y){
771 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
772 delta = subpt(r.min, origin);
773 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
774 bot.max.y = top.max.y;
779 drawdiff(Image *dst, Rectangle bot, Rectangle top, Image *src, Image *mask, Point p, int op)
781 gendrawdiff(dst, bot, top, src, p, mask, p, op);
785 * Translate the image in the window by delta.
788 translate(Point delta)
796 u = pclip(addpt(ul, delta), ulrange);
797 delta = subpt(u, ul);
798 if(delta.x == 0 && delta.y == 0)
802 * The upper left corner of the image is currently at ul.
803 * We want to move it to u.
805 or = rectaddpt(Rpt(ZP, Pt(Dx(im->r), Dy(im->r))), ul);
806 r = rectaddpt(or, delta);
808 drawop(screen, r, screen, nil, ul, S);
811 /* fill in gray where image used to be but isn't. */
812 drawdiff(screen, insetrect(or, -2), insetrect(r, -2), gray, nil, ZP, S);
814 /* fill in black border */
815 drawdiff(screen, insetrect(r, -2), r, display->black, nil, ZP, S);
817 /* fill in image where it used to be off the screen. */
818 if(rectclip(&or, screen->r))
819 drawdiff(screen, r, rectaddpt(or, delta), im, nil, im->r.min, S);
821 drawop(screen, r, im, nil, im->r.min, S);
822 flushimage(display, 1);
826 redraw(Image *screen)
833 ulrange.max = screen->r.max;
834 ulrange.min = subpt(screen->r.min, Pt(Dx(im->r), Dy(im->r)));
836 ul = pclip(ul, ulrange);
837 drawop(screen, screen->r, im, nil, subpt(im->r.min, subpt(ul, screen->r.min)), S);
842 /* fill in any outer edges */
844 r = rectaddpt(im->r, subpt(ul, im->r.min));
845 border(screen, r, -2, display->black, ZP);
846 r.min = subpt(r.min, Pt(2,2));
847 r.max = addpt(r.max, Pt(2,2));
849 /* gray for the rest */
851 gray = xallocimage(display, Rect(0,0,1,1), RGB24, 1, 0x888888FF);
853 fprint(2, "g out of memory: %r\n");
857 border(screen, r, -4000, gray, ZP);
858 // flushimage(display, 0);
866 if(new && getwindow(display, Refnone) < 0)
867 fprint(2,"can't reattach to window");
868 ul = addpt(ul, subpt(screen->r.min, r.min));
872 /* clip p to be in r */
874 pclip(Point p, Rectangle r)
878 else if(p.x >= r.max.x)
883 else if(p.y >= r.max.y)
890 * resize is perhaps a misnomer.
891 * this really just grows the window to be at least dx across
892 * and dy high. if the window hits the bottom or right edge,
893 * it is backed up until it hits the top or left edge.
896 resize(int dx, int dy)
904 wctlfd = open("/dev/wctl", OWRITE);
909 r = insetrect(screen->r, -Borderwidth);
910 if(Dx(r) >= dx && Dy(r) >= dy)
913 if(Dx(sr)*Dy(sr) == 0)
918 r.max.x = max(r.min.x+dx, r.max.x);
919 r.max.y = max(r.min.y+dy, r.max.y);
920 if(r.max.x > sr.max.x){
925 r = rectaddpt(r, Pt(sr.max.x-r.max.x, 0));
927 if(r.max.y > sr.max.y){
932 r = rectaddpt(r, Pt(0, sr.max.y-r.max.y));
936 * Sometimes we can't actually grow the window big enough,
937 * and resizing it to the same shape makes it flash.
939 if(Dx(r) == Dx(or) && Dy(r) == Dy(or))
942 fprint(wctlfd, "resize -minx %d -miny %d -maxx %d -maxy %d\n",
943 r.min.x, r.min.y, r.max.x, r.max.y);
947 * If we allocimage after a resize but before flushing the draw buffer,
948 * we won't have seen the reshape event, and we won't have called
949 * getwindow, and allocimage will fail. So we flushimage before every alloc.
952 xallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong val)
954 flushimage(display, 0);
955 return allocimage(d, r, chan, repl, val);
958 /* all code below this line should be in the library, but is stolen from colors instead */
965 fd = open(name, OREAD);
968 size = seek(fd, 0, 2);
971 fprint(2, "page: can't malloc: %r\n");
987 fd = open("/dev/screen", OREAD);
989 fd=open("/mnt/term/dev/screen", OREAD);
991 fprint(2, "page: can't open /dev/screen: %r\n");
992 wexits("window read");
994 if(read(fd, buf, sizeof buf) != sizeof buf){
995 fprint(2, "page: can't read /dev/screen: %r\n");
996 wexits("screen read");
999 return Rect(atoi(buf+12), atoi(buf+24), atoi(buf+36), atoi(buf+48));
1008 switch(rfork(RFFDG|RFPROC)) {
1010 wexits("cannot fork in zerox: %r");
1014 execl("/bin/page", "page", "-w", 0);
1015 wexits("cannot exec in zerox: %r\n");
1018 writeimage(pfd[0], im, 0);