home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / logo / patch01 / sun.i < prev   
Encoding:
Text File  |  1987-07-06  |  11.3 KB  |  477 lines

  1.  
  2. /* Include file for turtle.c for Sun Microsystems workstation */
  3.  
  4. /* modified for use with 2.0 SunWindows by Philippe Lacroute */
  5.  
  6. /* must be loaded -lsuntool -lsunwindow -lpixrect */
  7.  
  8. #include <suntool/tool_hs.h>
  9. #include <fcntl.h>
  10. #include <signal.h>
  11.  
  12. /* Window system variables and functions */
  13.  
  14. #define CLR    PIX_CLR
  15. #define SET    PIX_SET
  16. #define INVERT    PIX_SRC ^ PIX_DST
  17. #define SPLIT_TEXT_SIZE    5    /* default # lines of text in split mode */
  18. #define BORDER    5        /* width of the window borders */
  19. #define SCREEN_DEPTH    1
  20. #define FIX_PICTURE    1
  21. #define FIX_SIZE    2
  22.  
  23. #define ltos_xcor(c)    c + scalex    /* conversion: logo to window */
  24. #define ltos_ycor(c)    scaley - c    /*         system coordinates */
  25.  
  26. static short logocur_data[16] = {
  27.     0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0000, 0x0000, 0xF83E,
  28.     0x0000, 0x0000, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0000
  29. };
  30.  
  31. mpr_static(logocur_pr, 16, 16, 1, logocur_data);
  32.  
  33. struct cursor logocur = { 7, 7, PIX_SRC ^ PIX_DST, &logocur_pr };
  34.  
  35. static setwinchflag();
  36. extern char pr_reversesrc[];
  37. int screen_height, screen_width;/* size for retained pixrect */
  38. int gfx_windowfd = -1;        /* window device (blanket) */
  39. int real_gfx_fd;        /* gfx window parent */
  40. struct pixwin *gfx_pixwin;    /* actual window */
  41. int gfx_flags = 0;        /* used for SIGWINCH */
  42. int parentfd;            /* toll window */
  43. int textfd;            /* text window */
  44. int scalex, scaley;
  45. int font_height;
  46. int tlines = SPLIT_TEXT_SIZE;
  47. struct rect tool_rect;
  48. int sigwinch_ok = 0;
  49. int mouse_x = -1, mouse_y = -1;    /* current location of mouse (screen coor.) */
  50. int mousein = 0;
  51.  
  52. int sunpens[] = {SET, CLR, INVERT};
  53. char state;        /* What state we are in (text, split, full) */
  54. NUMBER sunoldx,sunoldy;
  55.  
  56. int sunturt(),sunfrom(),sunto(),suninit(),sunstate();
  57.  
  58. struct display sun = {
  59.     0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1,    /* dims initialized later */
  60.     "", "", "", "", sunturt, sunfrom, sunto, nullfn, suninit, nullfn,
  61.     nullfn, nullfn, nullfn, sunstate
  62. };
  63.  
  64. #define transline(op,fx,fy,tx,ty)    pw_vector(gfx_pixwin, ltos_xcor(fx), ltos_ycor(fy), ltos_xcor(tx), ltos_ycor(ty), op, 1)
  65.  
  66. sunturt(hide)
  67. int hide;    /* 1 to erase */
  68. {
  69.     NUMBER newx, newy, oldx, oldy, angle;
  70.     static double scrunch = 1.0;
  71.  
  72.     if (!hide)
  73.         scrunch = yscrunch;
  74.     angle = (mydpy->turth-90.0)*3.141592654/180.0;
  75.     oldx = mydpy->turtx + 15.0*sin(angle);
  76.     oldy = mydpy->turty + 15.0*cos(angle);
  77.     angle = mydpy->turth*3.141592654/180.0;
  78.     newx = mydpy->turtx + 15.0*sin(angle);
  79.     newy = mydpy->turty + 15.0*cos(angle);
  80.     transline(INVERT,(int)oldx,(int)(scrunch*oldy),
  81.         (int)newx,(int)(scrunch*newy));
  82.     oldx = newx;
  83.     oldy = newy;
  84.     angle = (mydpy->turth+90.0)*3.141592654/180.0;
  85.     newx = mydpy->turtx + 15.0*sin(angle);
  86.     newy = mydpy->turty + 15.0*cos(angle);
  87.     transline(INVERT,(int)oldx,(int)(scrunch*oldy),
  88.         (int)newx,(int)(scrunch*newy));
  89.     oldx = newx;
  90.     oldy = newy;
  91.     angle = (mydpy->turth-90.0)*3.141592654/180.0;
  92.     newx = mydpy->turtx + 15.0*sin(angle);
  93.     newy = mydpy->turty + 15.0*cos(angle);
  94.     transline(INVERT,(int)oldx,(int)(scrunch*oldy),
  95.         (int)newx,(int)(scrunch*newy));
  96.     scrunch = yscrunch;
  97. }
  98.  
  99. suninit()
  100. {
  101.     if (gfx_windowfd < 0) {
  102.         char *parent, *text, *gfx;
  103.         struct pixfont *pf = pf_default();
  104.         struct inputmask im;
  105.         struct screen scr;
  106.         int sig_mask;
  107.  
  108.         sig_mask = sigblock(1 << (SIGWINCH-1));
  109.  
  110.         /* Get window numbers from environment */
  111.  
  112.         if ((parent = getenv("LOGO_TOOL")) == NULL) {
  113.             printf("No LOGO_TOOL environment variable\n");
  114.             printf("Are you running logo instead of logotool?\n");
  115.             errhand();
  116.         }
  117.         if ((text = getenv("LOGO_TEXT")) == NULL) {
  118.             printf("No LOGO_TEXT environment variable\n");
  119.             printf("Are you running logo instead of logotool?\n");
  120.             errhand();
  121.         }
  122.         if ((gfx = getenv("LOGO_GFX")) == NULL) {
  123.             printf("No LOGO_GFX environment variable\n");
  124.             printf("Are you running logo instead of logotool?\n");
  125.             errhand();
  126.         }
  127.  
  128.         /* Open windows */
  129.  
  130.         if ((parentfd = open(parent, 0)) < 0) {
  131.             printf("Couldn't open parent window %s\n", parent);
  132.             printf("Sorry, cannot use the turtle!\n");
  133.             errhand();
  134.         }
  135.         if ((textfd = open(text, 0)) < 0) {
  136.             printf("Couldn't open ttysubwindow %s\n", text);
  137.             printf("Sorry, cannot use the turtle!\n");
  138.             errhand();
  139.         }
  140.         if ((real_gfx_fd = open(gfx, 0)) < 0) {
  141.             printf("Couldn't open takeover window %s\n", gfx);
  142.             printf("Sorry, cannot use the turtle!\n");
  143.             errhand();
  144.         }
  145.  
  146.         /* Find out how big we are */
  147.  
  148.         win_getrect(parentfd, &tool_rect);
  149.  
  150.         /* Create window */
  151.  
  152.         if ((gfx_windowfd = win_getnewwindow()) < 0) {
  153.             printf("Couldn't get a new window\n");
  154.             printf("Sorry, cannot use the turtle!\n");
  155.             errhand();
  156.         }
  157.         if (win_insertblanket(gfx_windowfd, real_gfx_fd) != 0) {
  158.             printf("Blanket window failed\n");
  159.             printf("Sorry, cannot use the turtle!\n");
  160.             errhand();
  161.         }
  162.         gfx_pixwin = pw_open(gfx_windowfd);
  163.  
  164.         /*
  165.          *  send input typed into gfxwindow to ttysubwindow;
  166.          *  save mouse movement and buttons for gfxsw;
  167.          *  want non-blocking read for gfxsw
  168.          */
  169.  
  170.         input_imnull(&im);
  171.         win_setinputcodebit(&im, LOC_MOVE);
  172.         win_setinputcodebit(&im, MS_LEFT);
  173.         win_setinputcodebit(&im, MS_MIDDLE);
  174.         win_setinputcodebit(&im, LOC_WINEXIT);
  175.         win_setinputmask(gfx_windowfd, &im, NULL, win_fdtonumber(textfd));
  176.         if (fcntl(gfx_windowfd, F_SETFL, FNDELAY) == -1) {
  177.             printf("Fcntl failed.\n");
  178.             printf("Please don't use the mouse!\n");
  179.         }
  180.         win_setcursor(gfx_windowfd, &logocur);
  181.  
  182.         /* Create retained pixrect and set SIGWINCH catcher */
  183.  
  184.         win_screenget(gfx_windowfd, &scr);    /* get screen dims */
  185.         screen_height = scr.scr_rect.r_height;
  186.         screen_width = scr.scr_rect.r_width;
  187.         gfx_pixwin->pw_prretained = 
  188.             mem_create(screen_height, screen_width, SCREEN_DEPTH);
  189.         font_height = pf->pf_defaultsize.y;  /* Get the font height */
  190.         signal(SIGWINCH, setwinchflag);
  191.         sigsetmask(sig_mask);
  192.  
  193.         /* Initialize screen size  and display */
  194.  
  195.         yscrunch = 1.0;
  196.         gfx_setsize(0);
  197.     } else
  198.         sunstate('s');        /* Just redisplay */
  199. }
  200.  
  201. sunfrom(x,y)
  202. NUMBER x,y;
  203. {
  204.     sunoldx = x;
  205.     sunoldy = y;
  206. }
  207.  
  208. sunto(x,y)
  209. NUMBER x,y;
  210. {
  211.     transline(sunpens[penerase], (int)sunoldx,(int)sunoldy,(int)x,(int)y);
  212. }
  213.  
  214. sunstate(which)
  215. {
  216.     struct rect r;
  217.  
  218.     win_getrect(parentfd, &r);    /* get size of parent */
  219.     fix_rect(&r);
  220.     switch (which) {
  221.     case 's':        /* Split screen - leave lines for text */
  222.             if (tlines > 0) {
  223.                 if (tlines * font_height <= r.r_height) {
  224.                     r.r_top += r.r_height - (tlines*font_height);
  225.                     r.r_height = tlines * font_height;
  226.                 }
  227.                 win_setrect(textfd, &r);
  228.                 putchar('\n');
  229.                 state = 's';
  230.                 break;
  231.             } /* else fallthrough */
  232.     case 'f':        /* Full screen - size of parent */
  233.                 /* must get rid of text window */
  234.             r.r_left = r.r_top = -2000;
  235.             win_setrect(textfd, &r);
  236.             putchar('\n');
  237.             state = which; /* could be either s or f */
  238.             break;
  239.     case 't':        /* Text - move graphics window off screen */
  240.             win_setrect(textfd, &r);
  241.             state = 't';
  242.             break;
  243.     case 'c':
  244.     case 'w':        /* Clear screen */
  245.             win_getrect(gfx_windowfd, &r);
  246.             pw_write(gfx_pixwin, 0, 0,
  247.                 r.r_width, r.r_height,
  248.                 PIX_CLR, NULL, 0, 0);
  249.             pr_rop(gfx_pixwin->pw_prretained,
  250.                 0, 0, screen_width, screen_height,
  251.                 PIX_CLR, NULL, 0, 0);
  252.             break;
  253.     }
  254. }
  255.  
  256. checkwindow()
  257. {
  258.     if (gfx_windowfd < 0)
  259.         return;
  260.     if (gfx_flags & FIX_PICTURE) {
  261.         repaint();
  262.     }
  263.     if (gfx_flags & FIX_SIZE) {
  264.         gfx_setsize(1);
  265.         repaint();
  266.     }
  267.     gfx_flags = 0;
  268. }
  269.  
  270. gfx_setsize(redraw)
  271. int redraw;
  272. {
  273.     int newx, newy;
  274.     struct rect r, p;
  275.  
  276.     win_getrect(parentfd, &r);
  277.     p = r;
  278.     fix_rect(&r);
  279.     win_setrect(real_gfx_fd, &r);
  280.  
  281.     if (redraw) {
  282.         sunstate(state);
  283.         if (p.r_width != tool_rect.r_width)
  284.             mydpy->xhigh += p.r_width - tool_rect.r_width;
  285.         if (p.r_height != tool_rect.r_height)
  286.             mydpy->ylow -= p.r_height - tool_rect.r_height;
  287.  
  288.         /* Move turtle inside window if it isn't */
  289.  
  290.         newx = mydpy->turtx;
  291.         newy = mydpy->turty;
  292.         if (mydpy->turtx > mydpy->xhigh)
  293.             newx = mydpy->xhigh;
  294.         if (mydpy->turtx < mydpy->xlow)
  295.             newx = mydpy->xlow;
  296.         if (mydpy->turty > mydpy->yhigh)
  297.             newy = mydpy->yhigh;
  298.         if (mydpy->turty < mydpy->ylow)
  299.             newy = mydpy->ylow;
  300.         if (newx != mydpy->turtx || newy != mydpy->turty) {
  301.             if (shown)
  302.                 sunturt(1);
  303.             mydpy->turtx = newx;
  304.             mydpy->turty = newy;
  305.             if (shown)
  306.                 sunturt(0);
  307.         }
  308.         tool_rect = p;
  309.     } else {
  310.         scalex = r.r_width / 2 - 1;
  311.         scaley = r.r_height - r.r_height / 2 + 1;
  312.         mydpy->xhigh = r.r_width / 2;
  313.         mydpy->xlow = -(mydpy->xhigh) + 1;
  314.         mydpy->yhigh = r.r_height / 2 + 3;
  315.         mydpy->ylow = -(mydpy->yhigh) + 4;
  316.         sunstate('s');
  317.     }
  318. }
  319.  
  320. static
  321. setwinchflag()
  322. {
  323.     struct rect r;
  324.  
  325.     win_getrect(parentfd, &r);
  326.     if (!rect_equal(&r, &tool_rect))
  327.         gfx_flags |= FIX_SIZE;
  328.     gfx_flags |= FIX_PICTURE;
  329.     if (sigwinch_ok)
  330.         checkwindow();
  331.  
  332. }
  333.  
  334. repaint()
  335. {
  336.     struct rect r;
  337.  
  338.     win_getrect(gfx_windowfd, &r);
  339.     pw_damaged(gfx_pixwin);
  340.     pw_write(gfx_pixwin, 0, 0, r.r_width, r.r_height,
  341.         PIX_SRC, gfx_pixwin->pw_prretained, 0, 0);
  342.     pw_donedamaged(gfx_pixwin);
  343. }
  344.  
  345. struct object *
  346. fill_region()
  347. {
  348.     int x = ltos_xcor((int)mydpy->turtx), y = ltos_ycor((int)mydpy->turty);
  349.     struct rect r;
  350.  
  351.     tcheck();
  352.     if (shown)
  353.         sunturt(1);
  354.     win_getrect(gfx_windowfd, &r);
  355.     fill(x, y, r.r_width, r.r_height);
  356.     if (shown)
  357.          sunturt(0);
  358.     return((struct object *) 0);
  359. }
  360.  
  361. #define getpixel    ((*(mpr_d(buf)->md_image+(x>>4))>>(15-(x&15)))&1)
  362. #define getline        pw_read(buf, 0, 0, gfx_width, 1, PIX_SRC, gfx_pixwin, 0, y); yinbuf = y
  363. #define putline        pw_vector(gfx_pixwin, leftend, y, rightend, y, PIX_SRC, NEWVALUE)
  364. #define didabove    (y == oldy+1 && leftend >= oldleft && rightend <= oldright)
  365. #define didbelow    (y == oldy-1 && leftend >= oldleft && rightend <= oldright)
  366. #define savex        savedx = x
  367. #define restorex    x = savedx
  368. #define push        stack[stackptr++] = x | (y << 16)
  369. #define pop        x = stack[--stackptr] & 65535; y = stack[stackptr] >> 16
  370. #define stacknotempty    (stackptr > 0)
  371. #define OLDVALUE    0
  372. #define NEWVALUE    1
  373. #define scanline                            \
  374.         savex;                            \
  375.         x = leftend;                        \
  376.         while (x <= rightend) {                    \
  377.             while(x <= rightend && getpixel != OLDVALUE)    \
  378.                 x++;                    \
  379.             if (x > rightend)                \
  380.                 break;                    \
  381.             push;                        \
  382.             while (x <= rightend && getpixel == OLDVALUE)    \
  383.                 x++;                    \
  384.         }                            \
  385.         restorex
  386.  
  387. fill(startx, starty, gfx_width, gfx_height)
  388. int startx, starty, gfx_width, gfx_height;
  389. {
  390.     register int x, y, stackptr = 0, savedx;
  391.     int oldy, leftend, rightend, oldleft, oldright, yinbuf = -1;
  392.     long stack[1000];
  393.     struct pixrect *buf;
  394.  
  395.     x = startx;
  396.     y = starty;
  397.     if (pw_get(gfx_pixwin, x, y) == NEWVALUE)    /* in case the pen */
  398.         pw_put(gfx_pixwin, x, y, OLDVALUE);    /* is down and this */
  399.     buf = mem_create(gfx_width, 1, 1);        /* point is written */
  400.     oldy = y;
  401.     push;
  402.     while (stacknotempty) {
  403.         pop;
  404.         if (y != yinbuf)
  405.             getline;
  406.         if (getpixel == NEWVALUE)
  407.             continue;
  408.         savex;
  409.         x--;
  410.         while (x >= 0 && getpixel == OLDVALUE)
  411.             x--;
  412.         leftend = x + 1;
  413.         restorex;
  414.         savex;
  415.         while (x < gfx_width && getpixel == OLDVALUE)
  416.             x++;
  417.         rightend = x - 1;
  418.         restorex;
  419.         putline;
  420.         if (!didabove && y > 0) {
  421.             y--;
  422.             getline;
  423.             scanline;
  424.             y++;
  425.         }
  426.         if (!didbelow && y < gfx_height-1) {
  427.             y++;
  428.             getline;
  429.             scanline;
  430.             y--;
  431.         }
  432.         oldy = y;
  433.         oldleft = leftend;
  434.         oldright = rightend;
  435.     }
  436.     pr_destroy(buf);
  437. }
  438.  
  439. struct object *
  440. text_size(arg)
  441. register struct object *arg;
  442. {
  443.     int lines;
  444.     NUMBER ncheck();
  445.  
  446.     lines = (int)ncheck(arg);
  447.     if (lines < 0) {
  448.         printf("textsize takes a positive input\n");
  449.         errhand();
  450.     } else {
  451.         tlines = lines;
  452.     }
  453.     if (state == 's')
  454.         sunstate('s');
  455. }
  456.  
  457. fix_rect(r)
  458. struct rect *r;
  459. {
  460.     r->r_width -= 2 * BORDER;
  461.     r->r_height -= font_height + 1 + BORDER;
  462.     r->r_top = font_height + 1;
  463.     r->r_left = BORDER;
  464. }
  465.  
  466. sigwinch_on()
  467. {
  468.     checkwindow();
  469.     sigwinch_ok = 1;
  470. }
  471.  
  472. sigwinch_off()
  473. {
  474.     sigwinch_ok = 0;
  475. }
  476.  
  477.