home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / GPT34SRC / TERM / UNIXPC.TRM < prev    next >
Text File  |  1993-05-11  |  15KB  |  541 lines

  1. /*
  2.  * $Id: unixpc.trm 3.38.2.42 1993/01/07 17:23:59 woo Exp woo $
  3.  *
  4.  */
  5.  
  6. /* GNUPLOT - unixpc.trm */
  7. /*
  8.  * Copyright (C) 1990 - 1993   
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software  is provided "as is" without express or implied warranty.
  21.  * 
  22.  * This file is included by ../term.c.
  23.  *
  24.  * This terminal driver supports:
  25.  *  Unix PC's (ATT 3b1)
  26.  *
  27.  * AUTHORS
  28.  *    John Campbell
  29.  * 
  30.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  31.  * 
  32.  */
  33.  
  34. /*
  35. >From: John Campbell (...!arizona!naucse!jdc)
  36.  
  37. I originally ported gnuplot to the ATT 3b1 (ATT7300) on 12/4/88, and then
  38. added the minimal code needed to bring it up to 2.0 level on 1/28/90.  The 
  39. 3b1, as I view it, is a 720x300 bitmapped, monochrome display (often people 
  40. don't use the top 12 scan lines and thus the effective size is 720x288).  I 
  41. tried to maximize the size of the graph area, by using these top 12 lines 
  42. (normally reserved) and set up a signal handler to restore them upon exit, 
  43. abort, etc.
  44.  
  45. Line styles were "fudged" (they do not know the aspect ratio).  The same
  46. line style may look different depending upon the slope of the curve.  Due to
  47. this only 4 line styles were implemented.  While more line types are possible,
  48. the current styles were chosen as distinguishable.
  49.  
  50. The 3b1 has 4 "special" rows at the bottom that I could not use in graphics
  51. mode.  It has been suggested that we use these lines for command prompting.
  52. Others have requested that we have a graphics window and a command window.
  53. My experience with gnuplot only includes relatively dumb graphics devices--
  54. hence gnuplot "looks and feels" normal to me the way I implemented it.
  55. I welcome either of these changes from someone else, however.
  56. */
  57.  
  58. #include <sys/window.h>   /* Started with tam.h--too much trouble. */
  59. #include <sys/signal.h>
  60. #include <errno.h>
  61.  
  62. #define uPC_HIGH_BIT    (0x8000)
  63.  
  64. typedef unsigned short Scr_type;
  65. typedef unsigned char Scr_kluge;
  66.  
  67. #define uPC_XMAX 720
  68. #define uPC_YMAX 300
  69.  
  70. #define uPC_XSIZE       45 /* Short ints. */
  71. #define uPC_YSIZE uPC_YMAX
  72.  
  73. Scr_type uPC_display[uPC_YSIZE][uPC_XSIZE];
  74. int uPC_width = 2*uPC_XSIZE;
  75. int uPC_sx=0, uPC_sy=0;
  76. int uPC_cur_linetype=0;
  77. int uPC_angle = 0;
  78. unsigned short uPC_raster_count=0;
  79. static Scr_type lookup[] = {
  80.       0x0001, 0x0002, 0x0004, 0x0008,
  81.       0x0010, 0x0020, 0x0040, 0x0080,
  82.       0x0100, 0x0200, 0x0400, 0x0800,
  83.       0x1000, 0x2000, 0x4000, 0x8000,
  84.    };
  85.  
  86. #define uPC_XLAST (uPC_XMAX - 1)
  87. #define uPC_YLAST (uPC_YMAX - 1)
  88.  
  89. #define uPC_VCHAR FNT5X9_VCHAR
  90. #define uPC_HCHAR FNT5X9_HCHAR
  91. #define uPC_VTIC  uPC_VCHAR/2  /* Was 8  */
  92. #define uPC_HTIC  uPC_HCHAR    /* Was 12 */
  93.  
  94. extern errno, sys_nerr;
  95. extern char *sys_errlist[];
  96.  
  97. static struct urdata uPC_ur = {(unsigned short *)uPC_display, 2*uPC_XSIZE, 0, 0,
  98.                            0, 0, 0, 0, uPC_XMAX, uPC_YMAX, SRCSRC, DSTOR, 0};
  99.  
  100. #define IfErrOut(e1,e2,s1,s2) if (e1 e2) {\
  101. fprintf(stderr, "%s:: %s %s\n", sys_errlist[errno], s1, s2);\
  102. uPC_fixwind(0);\
  103. exit(-1);}
  104.  
  105. uPC_init()
  106. {
  107. /* This routine will ioctl to change 0 size */
  108.    int i;
  109.    struct uwdata uw;
  110.    int uPC_fixwind();
  111.    short gw;
  112.  
  113. /* Check that we are on the bitmapped window. */
  114.    if (iswind() != 0) {
  115.       fprintf (stderr, "Sorry--must run from the bitmapped terminal\n");
  116.       exit(-1);
  117.    }
  118.    for (i=1; i<=16; i++) {
  119.       if (i != SIGINT && i != SIGFPE)  /* Two are caught in plot.c */
  120.          signal (i, uPC_fixwind);
  121.    }
  122.  
  123. /* Increase the screen size */
  124.    uw.uw_x = 0;
  125.    uw.uw_y = 0;   /* Leave room for top status line. */
  126.    uw.uw_width = uPC_XMAX;      /* 720 */
  127.    uw.uw_height = uPC_YMAX;     /* 288 normal--we clobber 12 (top row)*/
  128.    uw.uw_uflags = 1;         /* Creates with no border */
  129.  
  130.    IfErrOut (ioctl(1, WIOCSETD, &uw), <0, "ioctl failed on", "WIOCSETD");
  131. }
  132.  
  133.  
  134. uPC_graphics()
  135. {
  136. /* This routine will clear the uPC_display buffer and window. */
  137.    register Scr_type *j;
  138.    register int i;
  139.  
  140.    j = (Scr_type *)uPC_display;
  141.    i = uPC_YSIZE*uPC_XSIZE + 1;
  142.  
  143.    while (--i)
  144.       *j++ = 0;
  145. /*
  146.    Position the cursor to the bottom of the screen so when we come back to
  147.    text mode we are just below the graph.
  148. */
  149.    printf ("\033[25;1H");
  150.  
  151.    uPC_ur.ur_dstop = DSTSRC;   /* replace (clear screen). */
  152.    IfErrOut (ioctl(1, WIOCRASTOP, &uPC_ur), <0,
  153.       "ioctl failed", "WIOCRASTOP");
  154.    uPC_ur.ur_dstop = DSTOR;   /* Or in (show text) */
  155. }
  156.  
  157.  
  158. uPC_text()
  159. {
  160. /* This routine will flush the display. */
  161.  
  162.    IfErrOut (ioctl(1, WIOCRASTOP, &uPC_ur), <0,
  163.       "ioctl failed", "WIOCRASTOP");
  164. }
  165.  
  166.  
  167. uPC_linetype(linetype)
  168. int linetype;
  169. {
  170. /* This routine records the current linetype. */
  171.    if (uPC_cur_linetype != linetype) {
  172.       uPC_raster_count = 0;
  173.       uPC_cur_linetype = linetype;
  174.    }
  175. }
  176.  
  177.  
  178. uPC_move(x,y)
  179. unsigned int x,y;
  180. {
  181. /* This routine just records x and y in uPC_sx, uPC_sy */
  182.    uPC_sx = x;
  183.    uPC_sy = y;
  184. }
  185.  
  186.  
  187. /* Was just (*(a)|=(b)) */
  188. #define uPC_PLOT(a,b)   (uPC_cur_linetype != 0 ? uPC_plot_word (a,b) :\
  189.                                 (*(a)|=(b)))
  190.  
  191. uPC_plot_word(a,b)
  192. Scr_type *a, b;
  193. /*
  194.    Weak attempt to make line styles.  The real problem is the aspect
  195.    ratio.  This routine is called only when a bit is to be turned on in
  196.    a horizontal word.  A better line style routine would know something
  197.    about the slope of the line around the current point (in order to
  198.    change weighting).
  199.  
  200.    This yields 3 working linetypes plus a usable axis line type.
  201. */
  202. {
  203. /* Various line types */
  204.    switch (uPC_cur_linetype) {
  205.    case -1:
  206.    /* Distinguish between horizontal and vertical axis. */
  207.       if (uPC_sx > uPC_XMAX/8 && uPC_sx < 7*uPC_XMAX/8) {
  208.       /* Fuzzy tolerance because we don't know exactly where the y axis is */
  209.          if (++uPC_raster_count % 2 == 0) *(a) |= b;
  210.       }
  211.       else {
  212.       /* Due to aspect ratio, take every other y pixel and every third x. */
  213.          *(a) |= (b & 0x9999);
  214.       }
  215.    break;
  216.    case 1:
  217.    case 5:
  218.    /* Make a |    |----|    |----| type of line. */
  219.       if ((1<<uPC_raster_count) & 0xF0F0) *(a) |= b;
  220.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  221.    break;
  222.    case 2:
  223.    case 6:
  224.    /* Make a |----|----|----|--- |    | type of line. */
  225.       if ((1<<uPC_raster_count) & 0x0EFFF) *(a) |= b;
  226.       if (++uPC_raster_count > 19) uPC_raster_count = 0;
  227.    break;
  228.    case 3:
  229.    case 7:
  230.    /* Make a | -  | -  | -  | -  | type of line. */
  231.       if ((1<<uPC_raster_count) & 0x4444) *(a) |= b;
  232.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  233.    break;
  234.    case 4:
  235.    case 8:
  236.    default:
  237.       *(a) |= b;
  238.    break;
  239.    }
  240. }
  241.  
  242. uPC_vector(x,y)
  243. unsigned int x,y;
  244. {
  245. /* This routine calls line with x,y */
  246.    int x1 = uPC_sx, y1=uPC_sy, x2 = x, y2 = y;
  247.    register int  c, e, dx, dy, width;
  248.    register Scr_type mask, *a;
  249.  
  250. /* Record new sx, sy for next call to the vector routine. */
  251.    uPC_sx = x2;
  252.    uPC_sy = y2;
  253.  
  254.    a = &uPC_display[(uPC_YSIZE - 1) - y1][x1 >> 4];
  255.    mask = lookup[x1 & 0x0f];
  256.    width = uPC_width;
  257.  
  258.    if ((dx = x2 - x1) > 0) {
  259.       if ((dy = y2 - y1) > 0) {
  260.          if (dx > dy) {         /* dx > 0, dy > 0, dx > dy */
  261.             dy <<= 1;
  262.             e = dy - dx;
  263.             c = dx + 2;
  264.             dx <<= 1;
  265.  
  266.             while (--c) {
  267.                uPC_PLOT(a, mask);
  268.                if (e >= 0) {
  269.                   (Scr_kluge *)a -= width;
  270.                   e -= dx;
  271.                }
  272.                if (mask & uPC_HIGH_BIT) {
  273.                   mask = 1;
  274.                   a++;
  275.                } else
  276.                   mask <<= 1;
  277.                e += dy;
  278.             }
  279.          } else {            /* dx > 0, dy > 0, dx <= dy */
  280.             dx <<= 1;
  281.             e = dx - dy;
  282.             c = dy + 2;
  283.             dy <<= 1;
  284.  
  285.             while (--c) {
  286.                uPC_PLOT(a, mask);
  287.                if (e >= 0) {
  288.                   if (mask & uPC_HIGH_BIT) {
  289.                      mask = 1;
  290.                      a++;
  291.                   } else
  292.                      mask <<= 1;
  293.                   e -= dy;
  294.                }
  295.                (Scr_kluge *)a -= width;
  296.                e += dx;
  297.             }
  298.          }
  299.       } else {
  300.          dy = -dy;
  301.          if (dx > dy) {         /* dx > 0, dy <= 0, dx > dy */
  302.             dy <<= 1;
  303.             e = dy - dx;
  304.             c = dx + 2;
  305.             dx <<= 1;
  306.  
  307.             while (--c) {
  308.                uPC_PLOT(a, mask);
  309.                if (e >= 0) {
  310.                   (Scr_kluge *)a += width;
  311.                   e -= dx;
  312.                }
  313.                if (mask & uPC_HIGH_BIT) {
  314.                   mask = 1;
  315.                   a++;
  316.                } else
  317.                   mask <<= 1;
  318.                e += dy;
  319.             }
  320.          } else {            /* dx > 0, dy <= 0, dx <= dy */
  321.             dx <<= 1;
  322.             e = dx - dy;
  323.             c = dy + 2;
  324.             dy <<= 1;
  325.  
  326.             while (--c) {
  327.                uPC_PLOT(a, mask);
  328.                if (e >= 0) {
  329.                   if (mask & uPC_HIGH_BIT) {
  330.                      mask = 1;
  331.                      a++;
  332.                   } else
  333.                      mask <<= 1;
  334.                   e -= dy;
  335.                }
  336.                (Scr_kluge *)a += width;
  337.                e += dx;
  338.             }
  339.          }
  340.       }
  341.    } else {
  342.       dx = -dx;
  343.       if ((dy = y2 - y1) > 0) {
  344.          if (dx > dy) {         /* dx <= 0, dy > 0, dx > dy */
  345.             dy <<= 1;
  346.             e = dy - dx;
  347.             c = dx + 2;
  348.             dx <<= 1;
  349.  
  350.             while (--c) {
  351.                uPC_PLOT(a, mask);
  352.                if (e >= 0) {
  353.                   (Scr_kluge *)a -= width;
  354.                   e -= dx;
  355.                }
  356.                if (mask & 1) {
  357.                   mask = uPC_HIGH_BIT;
  358.                   a--;
  359.                } else
  360.                   mask >>= 1;
  361.                e += dy;
  362.             }
  363.          } else {            /* dx <= 0, dy > 0, dx <= dy */
  364.             dx <<= 1;
  365.             e = dx - dy;
  366.             c = dy + 2;
  367.             dy <<= 1;
  368.  
  369.             while (--c) {
  370.                uPC_PLOT(a, mask);
  371.                if (e >= 0) {
  372.                   if (mask & 1) {
  373.                      mask = uPC_HIGH_BIT;
  374.                      a--;
  375.                   } else
  376.                      mask >>= 1;
  377.                   e -= dy;
  378.                }
  379.                (Scr_kluge *)a -= width;
  380.                e += dx;
  381.             }
  382.          }
  383.       } else {
  384.          dy = -dy;
  385.          if (dx > dy) {         /* dx <= 0, dy <= 0, dx > dy */
  386.             dy <<= 1;
  387.             e = dy - dx;
  388.             c = dx + 2;
  389.             dx <<= 1;
  390.  
  391.             while (--c) {
  392.                uPC_PLOT(a, mask);
  393.                if (e >= 0) {
  394.                   (Scr_kluge *)a += width;
  395.                   e -= dx;
  396.                }
  397.                if (mask & 1) {
  398.                   mask = uPC_HIGH_BIT;
  399.                   a--;
  400.                } else
  401.                   mask >>= 1;
  402.                e += dy;
  403.             }
  404.          } else {            /* dx <= 0, dy <= 0, dx <= dy */
  405.             dx <<= 1;
  406.             e = dx - dy;
  407.             c = dy + 2;
  408.             dy <<= 1;
  409.  
  410.             while (--c) {
  411.                uPC_PLOT(a, mask);
  412.                if (e >= 0) {
  413.                   if (mask & 1) {
  414.                      mask = uPC_HIGH_BIT;
  415.                      a--;
  416.                   } else
  417.                      mask >>= 1;
  418.                   e -= dy;
  419.                }
  420.                (Scr_kluge *)a += width;
  421.                e += dx;
  422.             }
  423.          }
  424.       }
  425.    }
  426. }
  427.  
  428.  
  429. #ifdef uPC_NOT_USED
  430. /* Added by Russell Lang, eln272v@monu1.cc.monash.oz
  431.    This placement to the nearest character cell worked, and I'm leaving
  432.    it here so the calculations involved won't be lost!  (jdc)
  433. */
  434. uPC_put_text(x,y,str)
  435. unsigned int x,y;
  436. char str[];
  437. {
  438. /* This routine puts the text at the cursor location nearest
  439.    to (x,y).  Obviously the exact postion would look better */
  440.  
  441. /* Just use the ANSI escape sequence CUP (iswind said that was ok!) */
  442.    printf ("\033[%d;%dH%s\033[25;1H", (int)(24-(y-uPC_VCHAR/2)*25/uPC_YMAX), 
  443.                 (int)(x*80/uPC_XMAX), str); 
  444.    fflush (stdout);
  445. }
  446. #endif
  447.  
  448.  
  449. uPC_put_text(x,y,str)
  450. unsigned int x,y;
  451. char str[];
  452. {
  453.    if (uPC_angle == 1)
  454.       x += uPC_VCHAR/2;
  455.    else
  456.       y -= uPC_VCHAR/2;
  457.  
  458.    switch (uPC_angle) {
  459.       case 0:
  460.          for (; *str; ++str, x += uPC_HCHAR)
  461.             uPC_putc (x, y, *str, uPC_angle);
  462.       break;
  463.       case 1:
  464.          for (; *str; ++str, y += uPC_HCHAR)
  465.             uPC_putc (x, y, *str, uPC_angle);
  466.       break;
  467.    }
  468. }
  469.  
  470.  
  471. uPC_putc (x, y, c, angle)
  472. unsigned int x, y;
  473. int c, angle;
  474. /*
  475.    Put a character at an x,y location in the bit map (using the fnt5x9
  476.    array.  This is mostly just copied from the bitmap.c driver.
  477. */
  478. {
  479.    int i, j, k;
  480.    register Scr_type mask, *a;
  481.    char_row fc;
  482.    unsigned int pixelon;
  483.  
  484.    i = c - ' ';
  485.    for (j=0; j<FNT5X9_VBITS; j++) {
  486.       fc = fnt5x9[i][j];
  487.       for (k=0; k<FNT5X9_HBITS; k++) {
  488.          pixelon = ((unsigned int)(fc))>>k & 1;
  489.          if (pixelon) {
  490.             switch (angle) {
  491.             case 0:
  492.                mask = lookup[x+k+1 & 0x0f];
  493.                a = &uPC_display[(uPC_YSIZE - 1) - (y+j)][(x+k+1) >> 4];
  494.             break;
  495.             case 1:
  496.                mask = lookup[x-j & 0x0f];
  497.                a = &uPC_display[(uPC_YSIZE - 1) - (y+k+1)][(x-j) >> 4];
  498.             break;
  499.             }
  500.             *(a) |= (mask);  /* see uPC_PLOT macro */
  501.          }
  502.       }
  503.    }
  504. }
  505.  
  506.  
  507. uPC_text_angle (ang)
  508. int ang;
  509. {
  510.    uPC_angle = ang;
  511.    return TRUE;
  512. }
  513.  
  514.  
  515. uPC_reset()
  516. {
  517. /* Reset window to normal size. */
  518.    uPC_fixwind (0);
  519. }
  520.  
  521.  
  522.  
  523. uPC_fixwind(signo)
  524. int signo;
  525. {
  526.    static struct uwdata wreset = { 0, 12, 720, 288, 0x1};
  527.    struct utdata ut;
  528.  
  529. /* Reset the window to the right size. */
  530.    ioctl(1, WIOCSETD, &wreset);   /* 0, not wncur here! */
  531.  
  532. /* Scroll the screen once. (avoids typing over the same line) */
  533.    fprintf (stderr, "\n");
  534.  
  535.    if (signo) {
  536.       if (signo == SIGILL || signo == SIGTRAP || signo == SIGPWR)
  537.          signal (signo, SIG_DFL);
  538.       kill (0,signo);  /* Redo the signal (as if we never trapped it). */
  539.    }
  540. }
  541.