2 * the actual viewer that handles screen stuff
23 int showbottom = 0; /* on the next showpage, move the image so the bottom is visible. */
25 Rectangle ulrange; /* the upper left corner of the image must be in this rectangle */
26 Point ul; /* the upper left corner of the image is at this point on the screen */
28 Point pclip(Point, Rectangle);
29 Rectangle mkrange(Rectangle screenr, Rectangle imr);
31 void plumbproc(void*);
35 {0xff, 0x80, 0xff, 0x80, 0xff, 0x00, 0xfe, 0x00,
36 0xff, 0x00, 0xff, 0x80, 0xff, 0xc0, 0xef, 0xe0,
37 0xc7, 0xf0, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0xc0,
38 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, },
39 {0x00, 0x00, 0x7f, 0x00, 0x7e, 0x00, 0x7c, 0x00,
40 0x7e, 0x00, 0x7f, 0x00, 0x6f, 0x80, 0x47, 0xc0,
41 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xe0, 0x00, 0x40,
42 0x00, 0x00, 0x01, 0xb6, 0x01, 0xb6, 0x00, 0x00, }
47 {0x0f, 0xf0, 0x1f, 0xf8, 0x3f, 0xfc, 0x7f, 0xfe,
48 0x7c, 0x7e, 0x78, 0x7e, 0x00, 0xfc, 0x01, 0xf8,
49 0x03, 0xf0, 0x07, 0xe0, 0x07, 0xc0, 0x07, 0xc0,
50 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, },
51 {0x00, 0x00, 0x0f, 0xf0, 0x1f, 0xf8, 0x3c, 0x3c,
52 0x38, 0x1c, 0x00, 0x3c, 0x00, 0x78, 0x00, 0xf0,
53 0x01, 0xe0, 0x03, 0xc0, 0x03, 0x80, 0x03, 0x80,
54 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x00, 0x00, }
87 static char menustr[32];
99 p = doc->pagename(doc, n);
100 len = (sizeof menustr)-2;
102 if(strlen(p) > len && strrchr(p, '/'))
103 p = strrchr(p, '/')+1;
107 strcpy(menustr+1, p);
116 showpage(int page, Menu *m)
121 m->lasthit = 0; /* this page */
123 m->lasthit = reverse ? doc->npage-1-page : page;
125 setcursor(mc, &reading);
127 if((page < 0 || page >= doc->npage) && !doc->fwdonly){
131 im = doc->drawpage(doc, page);
133 if(doc->fwdonly) /* this is how we know we're out of pages */
136 im = xallocimage(display, Rect(0,0,50,50), GREY1, 1, DBlack);
138 fprint(2, "out of memory: %r\n");
141 string(im, ZP, display->white, ZP, display->defaultfont, "?");
143 resize(Dx(im->r), Dy(im->r));
145 if(im->r.min.x > 0 || im->r.min.y > 0) {
146 tmp = xallocimage(display, Rect(0, 0, Dx(im->r), Dy(im->r)), im->chan, 0, DNofill);
148 fprint(2, "out of memory during showpage: %r\n");
151 drawop(tmp, tmp->r, im, nil, im->r.min, S);
170 ul.y = screen->r.max.y - Dy(im->r);
175 flushimage(display, 1);
183 static char result[200];
190 memset(basename, 0, sizeof basename);
192 strncpy(basename, doc->docname, sizeof(basename)-1);
193 else if((p = menugen(page)) && p[0] != '\0')
194 strncpy(basename, p+1, sizeof(basename)-1);
197 if(q = strrchr(basename, '/'))
201 if(p = strchr(q, '.'))
204 memset(name, 0, sizeof name);
205 snprint(name, sizeof(name)-1, "%s.%d.bit", q, page+1);
206 if(access(name, 0) >= 0) {
207 strcat(name, "XXXX");
211 return "couldn't think of a name for bitmap";
213 strcpy(name, "bitXXXX");
216 return "couldn't think of a name for bitmap";
220 snprint(result, sizeof result, "cannot create %s: %r", name);
224 if(writeimage(fd, im, 0) < 0) {
225 snprint(result, sizeof result, "cannot writeimage: %r");
231 snprint(result, sizeof result, "wrote %s", name);
235 static void translate(Point);
238 showdata(Plumbmsg *msg)
242 s = plumblookup(msg->attr, "action");
243 return s && strcmp(s, "showdata")==0;
246 /* correspond to entries in miditems[] below,
247 * changing one means you need to change
270 int i, fd, n, oldpage;
281 static char *fwditems[] = { "this page", "next page", "exit", 0 };
282 static char *miditems[] = {
311 cp = chancreate(sizeof pm, 0);
314 doc = dd; /* save global for menuhit */
316 mc = initmouse(nil, screen);
317 kc = initkeyboard(nil);
318 alts[CMouse].c = mc->c;
320 alts[CMouse].op = CHANRCV;
321 alts[CResize].c = mc->resizec;
322 alts[CResize].v = &size;
323 alts[CResize].op = CHANRCV;
324 alts[CKeyboard].c = kc->c;
325 alts[CKeyboard].v = &run;
326 alts[CKeyboard].op = CHANRCV;
328 alts[CPlumb].v = ±
329 alts[CPlumb].op = CHANNOP;
330 alts[CN].op = CHANEND;
333 if(doc->addpage != nil) {
334 alts[CPlumb].op = CHANRCV;
335 proccreate(plumbproc, cp, 16384);
338 setcursor(mc, &reading);
342 * im is a global pointer to the current image.
343 * eventually, i think we will have a layer between
344 * the display routines and the ps/pdf/whatever routines
345 * to perhaps cache and handle images of different
349 page = reverse ? doc->npage-1 : 0;
352 menu.item = fwditems;
361 midmenu.item = miditems;
363 midmenu.lasthit = Next;
365 showpage(page, &menu);
371 * throughout, if doc->fwdonly is set, we restrict the functionality
372 * a fair amount. we don't care about doc->npage anymore, and
373 * all that can be done is select the next page.
377 if(run <= 0xFF && isdigit(run)) {
378 nxt = nxt*10+run-'0';
380 } else if(run != '\n')
383 case 'r': /* reverse page order */
387 menu.lasthit = doc->npage-1-menu.lasthit;
390 * the theory is that if we are reversing the
391 * document order and are on the first or last
392 * page then we're just starting and really want
393 * to view the other end. maybe the if
394 * should be dropped and this should happen always.
396 if(page == 0 || page == doc->npage-1) {
397 page = doc->npage-1-page;
398 showpage(page, &menu);
401 case 'w': /* write bitmap of current screen */
402 setcursor(mc, &reading);
405 string(screen, addpt(screen->r.min, Pt(5,5)), display->black, ZP,
406 display->defaultfont, s);
408 flushimage(display, 1);
410 case 'd': /* remove image from working set */
411 if(doc->rmpage && page < doc->npage) {
412 if(doc->rmpage(doc, page) >= 0) {
415 if(page >= doc->npage)
417 showpage(page, &menu);
422 case 0x04: /* ctrl-d */
427 setcursor(mc, &reading);
430 angle = (angle+180) % 360;
432 flushimage(display, 1);
437 if(page > 0 && !doc->fwdonly) {
439 showpage(page, &menu);
445 if(nxt >= 0 && nxt < doc->npage && !doc->fwdonly)
446 showpage(page=nxt, &menu);
454 if(doc->npage && ++page >= doc->npage && !doc->fwdonly)
456 showpage(page, &menu);
460 * The upper y coordinate of the image is at ul.y in screen->r.
461 * Panning up means moving the upper left corner down. If the
462 * upper left corner is currently visible, we need to go back a page.
465 if(screen->r.min.y <= ul.y && ul.y < screen->r.max.y){
466 if(page > 0 && !doc->fwdonly){
469 showpage(page, &menu);
475 if(i+ul.y > screen->r.min.y)
476 i = screen->r.min.y - ul.y;
482 * If the lower y coordinate is on the screen, we go to the next page.
483 * The lower y coordinate is at ul.y + Dy(im->r).
486 i = ul.y + Dy(im->r);
487 if(screen->r.min.y <= i && i <= screen->r.max.y){
488 ul.y = screen->r.min.y;
491 i = -Dy(screen->r)/2;
494 if(i+ul.y+Dy(im->r) <= screen->r.max.y)
495 i = screen->r.max.y - Dy(im->r) - ul.y - 1;
500 setcursor(mc, &query);
513 dxy = subpt(m.xy, oxy);
517 } while(m.buttons == Left);
519 dxy = subpt(xy0, oxy);
528 n = menuhit(Middle, mc, &midmenu, nil);
532 case Next: /* next */
542 if((page >= doc->npage) && !doc->fwdonly)
545 showpage(page, &menu);
548 case Prev: /* prev */
558 if((page >= doc->npage) && !doc->fwdonly && !reverse)
561 showpage(page, &menu);
564 case Zerox: /* prev */
567 case Zin: /* zoom in */
572 r = getrect(Middle, mc);
573 if((rectclip(&r, rectaddpt(im->r, ul)) == 0) ||
574 Dx(r) == 0 || Dy(r) == 0)
576 /* use the smaller side to expand */
578 delta = (double)Dx(im->r)/(double)Dx(r);
580 delta = (double)Dy(im->r)/(double)Dy(r);
582 setcursor(mc, &reading);
583 tmp = xallocimage(display,
584 Rect(0, 0, (int)((double)Dx(im->r)*delta), (int)((double)Dy(im->r)*delta)),
585 im->chan, 0, DBlack);
587 fprint(2, "out of memory during zoom: %r\n");
596 flushimage(display, 1);
604 delta = (double)Dx(screen->r)/(double)Dx(im->r);
605 if((double)Dy(im->r)*delta > Dy(screen->r))
606 delta = (double)Dy(screen->r)/(double)Dy(im->r);
608 r = Rect(0, 0, (int)((double)Dx(im->r)*delta), (int)((double)Dy(im->r)*delta));
609 setcursor(mc, &reading);
610 tmp = xallocimage(display, r, im->chan, 0, DBlack);
612 fprint(2, "out of memory during fit: %r\n");
621 flushimage(display, 1);
624 case Rot: /* rotate 90 */
625 setcursor(mc, &reading);
628 angle = (angle+90) % 360;
630 flushimage(display, 1);
632 case Upside: /* upside-down */
635 setcursor(mc, &reading);
638 angle = (angle+180) % 360;
640 flushimage(display, 1);
642 case Restore: /* restore */
643 showpage(page, &menu);
645 case Reverse: /* reverse */
649 menu.lasthit = doc->npage-1-menu.lasthit;
651 if(page == 0 || page == doc->npage-1) {
652 page = doc->npage-1-page;
653 showpage(page, &menu);
656 case Write: /* write */
657 setcursor(mc, &reading);
660 string(screen, addpt(screen->r.min, Pt(5,5)), display->black, ZP,
661 display->defaultfont, s);
663 flushimage(display, 1);
665 case Del: /* delete */
666 if(doc->rmpage && page < doc->npage) {
667 if(doc->rmpage(doc, page) >= 0) {
670 if(page >= doc->npage)
672 showpage(page, &menu);
676 case Exit: /* exit */
692 n = menuhit(RMenu, mc, &menu, nil);
698 case 0: /* this page */
700 case 1: /* next page */
701 showpage(++page, &menu);
712 page = reverse ? doc->npage-1-n : n;
715 showpage(page, &menu);
722 if(getwindow(display, Refnone) < 0)
723 fprint(2,"can't reattach to window");
724 ul = addpt(ul, subpt(screen->r.min, r.min));
726 flushimage(display, 1);
734 s = estrdup("/tmp/pageplumbXXXXXXX");
736 write(fd, pm->data, pm->ndata);
737 /* lose fd reference on purpose; the file is open ORCLOSE */
738 } else if(pm->data[0] == '/') {
739 s = estrdup(pm->data);
741 s = emalloc(strlen(pm->wdir)+1+pm->ndata+1);
742 sprint(s, "%s/%s", pm->wdir, pm->data);
745 if((i = doc->addpage(doc, s)) >= 0) {
748 showpage(page, &menu);
760 * A draw operation that touches only the area contained in bot but not in top.
761 * mp and sp get aligned with bot.min.
764 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
765 Image *src, Point sp, Image *mask, Point mp, int op)
773 if(Dx(bot)*Dy(bot) == 0)
776 /* no points in bot - top */
777 if(rectinrect(bot, top))
780 /* bot - top ≡ bot */
781 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
782 gendrawop(dst, bot, src, sp, mask, mp, op);
787 /* split bot into rectangles that don't intersect top */
789 if(bot.min.x < top.min.x){
790 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
791 delta = subpt(r.min, origin);
792 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
793 bot.min.x = top.min.x;
797 if(bot.max.x > top.max.x){
798 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
799 delta = subpt(r.min, origin);
800 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
801 bot.max.x = top.max.x;
805 if(bot.min.y < top.min.y){
806 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
807 delta = subpt(r.min, origin);
808 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
809 bot.min.y = top.min.y;
813 if(bot.max.y > top.max.y){
814 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
815 delta = subpt(r.min, origin);
816 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
817 bot.max.y = top.max.y;
822 drawdiff(Image *dst, Rectangle bot, Rectangle top, Image *src, Image *mask, Point p, int op)
824 gendrawdiff(dst, bot, top, src, p, mask, p, op);
828 * Translate the image in the window by delta.
831 translate(Point delta)
839 u = pclip(addpt(ul, delta), ulrange);
840 delta = subpt(u, ul);
841 if(delta.x == 0 && delta.y == 0)
845 * The upper left corner of the image is currently at ul.
846 * We want to move it to u.
848 or = rectaddpt(Rpt(ZP, Pt(Dx(im->r), Dy(im->r))), ul);
849 r = rectaddpt(or, delta);
851 drawop(screen, r, screen, nil, ul, S);
854 /* fill in gray where image used to be but isn't. */
855 drawdiff(screen, insetrect(or, -2), insetrect(r, -2), gray, nil, ZP, S);
857 /* fill in black border */
858 drawdiff(screen, insetrect(r, -2), r, display->black, nil, ZP, S);
860 /* fill in image where it used to be off the screen. */
861 if(rectclip(&or, screen->r))
862 drawdiff(screen, r, rectaddpt(or, delta), im, nil, im->r.min, S);
864 drawop(screen, r, im, nil, im->r.min, S);
865 flushimage(display, 1);
869 redraw(Image *screen)
876 ulrange.max = screen->r.max;
877 ulrange.min = subpt(screen->r.min, Pt(Dx(im->r), Dy(im->r)));
879 ul = pclip(ul, ulrange);
880 drawop(screen, screen->r, im, nil, subpt(im->r.min, subpt(ul, screen->r.min)), S);
885 /* fill in any outer edges */
887 r = rectaddpt(im->r, subpt(ul, im->r.min));
888 border(screen, r, -2, display->black, ZP);
889 r.min = subpt(r.min, Pt(2,2));
890 r.max = addpt(r.max, Pt(2,2));
892 /* gray for the rest */
894 gray = xallocimage(display, Rect(0,0,1,1), RGB24, 1, 0x888888FF);
896 fprint(2, "g out of memory: %r\n");
900 border(screen, r, -4000, gray, ZP);
901 // flushimage(display, 0);
904 /* clip p to be in r */
906 pclip(Point p, Rectangle r)
910 else if(p.x >= r.max.x)
915 else if(p.y >= r.max.y)
922 * resize is perhaps a misnomer.
923 * this really just grows the window to be at least dx across
924 * and dy high. if the window hits the bottom or right edge,
925 * it is backed up until it hits the top or left edge.
928 resize(int dx, int dy)
934 if(Dx(sr)*Dy(sr) == 0) {
936 /* Start with the size of the first image */
941 if(Dx(r) >= dx && Dy(r) >= dy)
946 r.max.x = max(r.min.x+dx, r.max.x);
947 r.max.y = max(r.min.y+dy, r.max.y);
948 if(r.max.x > sr.max.x){
953 r = rectaddpt(r, Pt(sr.max.x-r.max.x, 0));
955 if(r.max.y > sr.max.y){
960 r = rectaddpt(r, Pt(0, sr.max.y-r.max.y));
964 * Sometimes we can't actually grow the window big enough,
965 * and resizing it to the same shape makes it flash.
967 if(Dx(r) == Dx(or) && Dy(r) == Dy(or))
974 * If we allocimage after a resize but before flushing the draw buffer,
975 * we won't have seen the reshape event, and we won't have called
976 * getwindow, and allocimage will fail. So we flushimage before every alloc.
979 xallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong val)
981 flushimage(display, 0);
982 return allocimage(d, r, chan, repl, val);
992 fd = plumbopenfid("image", OREAD|OCEXEC);
994 fprint(2, "Cannot connect to the plumber");
995 threadexits("plumber");
998 send(cp, plumbrecvfid(fd));
1002 /* XXX: This function is ugly and hacky. There may be a better way... or not */
1012 wexits("pipe failed");
1014 fd[0] = open("/dev/null", OREAD);
1017 if(threadspawnl(fd, "rc", "rc", "-c", "xdpyinfo | grep 'dimensions:'", nil) == -1)
1018 wexits("threadspawnl failed");
1020 if((n = read(pfd[0], buf, 63)) <= 0)
1021 wexits("read xdpyinfo failed");
1025 for(p = buf; *p; p++)
1026 if(*p >= '0' && *p <= '9') break;
1028 wexits("xdpyinfo parse failed");
1030 w = strtoul(p, &pr, 10);
1031 if(p == pr || *pr == '\0' || *(++pr) == '\0')
1032 wexits("xdpyinfo parse failed");
1033 h = strtoul(pr, &p, 10);
1035 wexits("xdpyinfo parse failed");
1037 return Rect(0, 0, w, h);
1050 threadspawnl(fd, "page", "page", "-R", nil);
1052 writeimage(pfd[1], im, 0);