home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / gnuplot1.10A / part05 / unixpc.trm next >
Encoding:
Text File  |  1989-09-09  |  11.9 KB  |  462 lines

  1. /*
  2. From: John Campbell (...!arizona!naucse!jdc)
  3.  
  4. I ported gnuplot to the ATT 3b1 (ATT7300) on 12/4/88.  The 3b1, as I view
  5. it, is a 720x300 bitmapped, monochrome display (often people don't use
  6. the top 12 scan lines and thus the effective size is 720x288).  I tried to
  7. maximize the size of the graph area, by using these top 12 lines (normally
  8. reserved) and set up a signal handler to restore them upon exit, abort, etc.
  9.  
  10. Line styles were "fudged" (they do not know the aspect ratio).  The same
  11. line style may look different depending upon the slope of the curve.  Due to
  12. this only 4 line styles were implemented.  While more line types are possible,
  13. the current styles were chosen as distinguishable.
  14.  
  15. The 3b1 has 4 "special" rows at the bottom that I could not use in graphics
  16. mode.  I chose to use these lines for the plot scale--normally found at the
  17. bottom right corner of the plot.  I wanted to use the four bottom lines as
  18. prompt/command lines, but did not find a way to do this.  If someone ever
  19. writes a device driver to "open" these rows as a small separate window, I
  20. could implement my original idea.
  21. */
  22.  
  23. #include <sys/window.h>   /* Started with tam.h--too much trouble. */
  24. #include <sys/signal.h>
  25. #include <errno.h>
  26.  
  27. #define uPC_HIGH_BIT    (0x8000)
  28.  
  29. typedef unsigned short Scr_type;
  30. typedef unsigned char Scr_kluge;
  31.  
  32. #define uPC_XMAX 720
  33. #define uPC_YMAX 300
  34.  
  35. #define uPC_XSIZE       45 /* Short ints. */
  36. #define uPC_YSIZE uPC_YMAX
  37.  
  38. Scr_type uPC_display[uPC_YSIZE][uPC_XSIZE];
  39. int uPC_width = 2*uPC_XSIZE;
  40. int uPC_sx=0, uPC_sy=0;
  41. int uPC_cur_linetype=0;
  42. unsigned short uPC_raster_count=0;
  43.  
  44. #define uPC_XLAST (uPC_XMAX - 1)
  45. #define uPC_YLAST (uPC_YMAX - 1)
  46.  
  47. #define uPC_VCHAR 12
  48. #define uPC_HCHAR  9
  49. #define uPC_VTIC   8
  50. #define uPC_HTIC  12
  51.  
  52. extern errno, sys_nerr;
  53. extern char *sys_errlist[];
  54.  
  55. static struct urdata uPC_ur = {(unsigned short *)uPC_display, 2*uPC_XSIZE, 0, 0,
  56.                            0, 0, 0, 0, uPC_XMAX, uPC_YMAX, SRCSRC, DSTOR, 0};
  57.  
  58. #define IfErrOut(e1,e2,s1,s2) if (e1 e2) {\
  59. fprintf(stderr, "%s:: %s %s\n", sys_errlist[errno], s1, s2);\
  60. uPC_fixwind(0);\
  61. exit(-1);}
  62.  
  63. uPC_init()
  64. {
  65. /* This routine will ioctl to change 0 size */
  66.    int i;
  67.    struct uwdata uw;
  68.    int uPC_fixwind();
  69.    short gw;
  70.  
  71. /* Check that we are on the bitmapped window. */
  72.    if (iswind() != 0) {
  73.       fprintf (stderr, "Sorry--must run from the bitmapped terminal\n");
  74.       exit(-1);
  75.    }
  76.    for (i=1; i<=16; i++) {
  77.       if (i != SIGINT && i != SIGFPE)  /* Two are caught in plot.c */
  78.          signal (i, uPC_fixwind);
  79.    }
  80.  
  81. /* Increase the screen size */
  82.    uw.uw_x = 0;
  83.    uw.uw_y = 0;   /* Leave room for top status line. */
  84.    uw.uw_width = uPC_XMAX;      /* 720 */
  85.    uw.uw_height = uPC_YMAX;     /* 288 normal--we clobber 12 (top row)*/
  86.    uw.uw_uflags = 1;         /* Creates with no border */
  87.  
  88.    IfErrOut (ioctl(0, WIOCSETD, &uw), <0, "ioctl failed on", "WIOCSETD");
  89. }
  90.  
  91.  
  92. uPC_graphics()
  93. {
  94. /* This routine will clear the uPC_display buffer and window. */
  95.    register Scr_type *j;
  96.    register int i;
  97.  
  98.    j = (Scr_type *)uPC_display;
  99.    i = uPC_YSIZE*uPC_XSIZE + 1;
  100.  
  101.    while (--i)
  102.       *j++ = 0;
  103.  
  104.    uPC_ur.ur_dstop = DSTSRC;   /* replace (clear screen). */
  105.    IfErrOut (ioctl(0, WIOCRASTOP, &uPC_ur), <0,
  106.       "ioctl failed", "WIOCRASTOP");
  107.    uPC_ur.ur_dstop = DSTOR;   /* Or in (show text) */
  108. }
  109.  
  110.  
  111. uPC_text()
  112. {
  113. /* This routine will flush the display. */
  114.  
  115.    IfErrOut (ioctl(0, WIOCRASTOP, &uPC_ur), <0,
  116.       "ioctl failed", "WIOCRASTOP");
  117. /* Now position the cursor at the second to the last row--better later? */
  118.    wgoto (0, 24, 0);
  119. }
  120.  
  121.  
  122. uPC_linetype(linetype)
  123. int linetype;
  124. {
  125. /* This routine records the current linetype. */
  126.    if (uPC_cur_linetype != linetype) {
  127.       uPC_raster_count = 0;
  128.       uPC_cur_linetype = linetype;
  129.    }
  130. }
  131.  
  132.  
  133. uPC_move(x,y)
  134. unsigned int x,y;
  135. {
  136. /* This routine just records x and y in uPC_sx, uPC_sy */
  137.    uPC_sx = x;
  138.    uPC_sy = y;
  139. }
  140.  
  141.  
  142. /* Was just (*(a)|=(b)) */
  143. #define uPC_PLOT(a,b)   (uPC_cur_linetype != 0 ? uPC_plot_word (a,b) :\
  144.                                 *(a)|=(b))
  145.  
  146. uPC_plot_word(a,b)
  147. Scr_type *a, b;
  148. /*
  149.    Weak attempt to make line styles.  The real problem is the aspect
  150.    ratio.  This routine is called only when a bit is to be turned on in
  151.    a horizontal word.  A better line style routine would know something
  152.    about the slope of the line around the current point (in order to
  153.    change weighting).
  154.  
  155.    This yields 3 working linetypes plus a usable axis line type.
  156. */
  157. {
  158. /* Various line types */
  159.    switch (uPC_cur_linetype) {
  160.    case -1:
  161.    /* Distinguish between horizontal and vertical axis. */
  162.       if (uPC_sx > uPC_XMAX/8 && uPC_sx < 7*uPC_XMAX/8) {
  163.       /* Fuzzy tolerance because we don't know exactly where the y axis is */
  164.          if (++uPC_raster_count % 2 == 0) *(a) |= b;
  165.       }
  166.       else {
  167.       /* Due to aspect ratio, take every other y pixel and every third x. */
  168.          *(a) |= (b & 0x9999);
  169.       }
  170.    break;
  171.    case 1:
  172.    case 5:
  173.    /* Make a |    |----|    |----| type of line. */
  174.       if ((1<<uPC_raster_count) & 0xF0F0) *(a) |= b;
  175.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  176.    break;
  177.    case 2:
  178.    case 6:
  179.    /* Make a |----|----|----|--- |    | type of line. */
  180.       if ((1<<uPC_raster_count) & 0x0EFFF) *(a) |= b;
  181.       if (++uPC_raster_count > 19) uPC_raster_count = 0;
  182.    break;
  183.    case 3:
  184.    case 7:
  185.    /* Make a | -  | -  | -  | -  | type of line. */
  186.       if ((1<<uPC_raster_count) & 0x4444) *(a) |= b;
  187.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  188.    break;
  189.    case 4:
  190.    case 8:
  191.    default:
  192.       *(a) |= b;
  193.    break;
  194.    }
  195. }
  196.  
  197. uPC_vector(x,y)
  198. unsigned int x,y;
  199. {
  200. /* This routine calls line with x,y */
  201.    int x1 = uPC_sx, y1=uPC_sy, x2 = x, y2 = y;
  202.    register int  c, e, dx, dy, width;
  203.    register Scr_type mask, *a;
  204.    static Scr_type lookup[] = {
  205.       0x0001, 0x0002, 0x0004, 0x0008,
  206.       0x0010, 0x0020, 0x0040, 0x0080,
  207.       0x0100, 0x0200, 0x0400, 0x0800,
  208.       0x1000, 0x2000, 0x4000, 0x8000,
  209.    };
  210.  
  211. /* Record new sx, sy for next call to the vector routine. */
  212.    uPC_sx = x2;
  213.    uPC_sy = y2;
  214.  
  215.    a = &uPC_display[(uPC_YSIZE - 1) - y1][x1 >> 4];
  216.    mask = lookup[x1 & 0x0f];
  217.    width = uPC_width;
  218.  
  219.    if ((dx = x2 - x1) > 0) {
  220.       if ((dy = y2 - y1) > 0) {
  221.          if (dx > dy) {         /* dx > 0, dy > 0, dx > dy */
  222.             dy <<= 1;
  223.             e = dy - dx;
  224.             c = dx + 2;
  225.             dx <<= 1;
  226.  
  227.             while (--c) {
  228.                uPC_PLOT(a, mask);
  229.                if (e >= 0) {
  230.                   (Scr_kluge *)a -= width;
  231.                   e -= dx;
  232.                }
  233.                if (mask & uPC_HIGH_BIT) {
  234.                   mask = 1;
  235.                   a++;
  236.                } else
  237.                   mask <<= 1;
  238.                e += dy;
  239.             }
  240.          } else {            /* dx > 0, dy > 0, dx <= dy */
  241.             dx <<= 1;
  242.             e = dx - dy;
  243.             c = dy + 2;
  244.             dy <<= 1;
  245.  
  246.             while (--c) {
  247.                uPC_PLOT(a, mask);
  248.                if (e >= 0) {
  249.                   if (mask & uPC_HIGH_BIT) {
  250.                      mask = 1;
  251.                      a++;
  252.                   } else
  253.                      mask <<= 1;
  254.                   e -= dy;
  255.                }
  256.                (Scr_kluge *)a -= width;
  257.                e += dx;
  258.             }
  259.          }
  260.       } else {
  261.          dy = -dy;
  262.          if (dx > dy) {         /* dx > 0, dy <= 0, dx > dy */
  263.             dy <<= 1;
  264.             e = dy - dx;
  265.             c = dx + 2;
  266.             dx <<= 1;
  267.  
  268.             while (--c) {
  269.                uPC_PLOT(a, mask);
  270.                if (e >= 0) {
  271.                   (Scr_kluge *)a += width;
  272.                   e -= dx;
  273.                }
  274.                if (mask & uPC_HIGH_BIT) {
  275.                   mask = 1;
  276.                   a++;
  277.                } else
  278.                   mask <<= 1;
  279.                e += dy;
  280.             }
  281.          } else {            /* dx > 0, dy <= 0, dx <= dy */
  282.             dx <<= 1;
  283.             e = dx - dy;
  284.             c = dy + 2;
  285.             dy <<= 1;
  286.  
  287.             while (--c) {
  288.                uPC_PLOT(a, mask);
  289.                if (e >= 0) {
  290.                   if (mask & uPC_HIGH_BIT) {
  291.                      mask = 1;
  292.                      a++;
  293.                   } else
  294.                      mask <<= 1;
  295.                   e -= dy;
  296.                }
  297.                (Scr_kluge *)a += width;
  298.                e += dx;
  299.             }
  300.          }
  301.       }
  302.    } else {
  303.       dx = -dx;
  304.       if ((dy = y2 - y1) > 0) {
  305.          if (dx > dy) {         /* dx <= 0, dy > 0, dx > dy */
  306.             dy <<= 1;
  307.             e = dy - dx;
  308.             c = dx + 2;
  309.             dx <<= 1;
  310.  
  311.             while (--c) {
  312.                uPC_PLOT(a, mask);
  313.                if (e >= 0) {
  314.                   (Scr_kluge *)a -= width;
  315.                   e -= dx;
  316.                }
  317.                if (mask & 1) {
  318.                   mask = uPC_HIGH_BIT;
  319.                   a--;
  320.                } else
  321.                   mask >>= 1;
  322.                e += dy;
  323.             }
  324.          } else {            /* dx <= 0, dy > 0, dx <= dy */
  325.             dx <<= 1;
  326.             e = dx - dy;
  327.             c = dy + 2;
  328.             dy <<= 1;
  329.  
  330.             while (--c) {
  331.                uPC_PLOT(a, mask);
  332.                if (e >= 0) {
  333.                   if (mask & 1) {
  334.                      mask = uPC_HIGH_BIT;
  335.                      a--;
  336.                   } else
  337.                      mask >>= 1;
  338.                   e -= dy;
  339.                }
  340.                (Scr_kluge *)a -= width;
  341.                e += dx;
  342.             }
  343.          }
  344.       } else {
  345.          dy = -dy;
  346.          if (dx > dy) {         /* dx <= 0, dy <= 0, dx > dy */
  347.             dy <<= 1;
  348.             e = dy - dx;
  349.             c = dx + 2;
  350.             dx <<= 1;
  351.  
  352.             while (--c) {
  353.                uPC_PLOT(a, mask);
  354.                if (e >= 0) {
  355.                   (Scr_kluge *)a += width;
  356.                   e -= dx;
  357.                }
  358.                if (mask & 1) {
  359.                   mask = uPC_HIGH_BIT;
  360.                   a--;
  361.                } else
  362.                   mask >>= 1;
  363.                e += dy;
  364.             }
  365.          } else {            /* dx <= 0, dy <= 0, dx <= dy */
  366.             dx <<= 1;
  367.             e = dx - dy;
  368.             c = dy + 2;
  369.             dy <<= 1;
  370.  
  371.             while (--c) {
  372.                uPC_PLOT(a, mask);
  373.                if (e >= 0) {
  374.                   if (mask & 1) {
  375.                      mask = uPC_HIGH_BIT;
  376.                      a--;
  377.                   } else
  378.                      mask >>= 1;
  379.                   e -= dy;
  380.                }
  381.                (Scr_kluge *)a += width;
  382.                e += dx;
  383.             }
  384.          }
  385.       }
  386.    }
  387. }
  388.  
  389.  
  390. uPC_lrput_text(row,str)
  391. unsigned int row;
  392. char str[];
  393. {
  394.    int col = 80-strlen(str), num, i;
  395.    struct utdata ut;
  396.    char *txt=ut.ut_text;
  397.  
  398. /* Fill in the pad. */
  399.    for (i = 0; i < col; i++)
  400.       txt[i] = ' ';
  401. /* Then stick in the text. */
  402.    txt[i] = '\0';
  403.    strcat (txt, str);
  404.  
  405.    if (row > 2)
  406.       puts (txt);
  407.    else {
  408.    /* This will fit on the 2 bottom "non-graphic" lines. */
  409.       switch (row) {
  410.       case 0: ut.ut_num =  WTXTSLK1; break;
  411.       case 1: ut.ut_num =  WTXTSLK2; break;
  412.       }
  413.       ioctl (0, WIOCSETTEXT, &ut);
  414.    }
  415.    wgoto (1, 24, 0);
  416. }
  417.  
  418. uPC_ulput_text(row,str)
  419. unsigned int row;
  420. char str[];
  421. {
  422. /* This routine puts the text in the upper left corner. */
  423.  
  424. /* Just use the ANSI escape sequence CUP (iswind said that was ok!) */
  425.    printf ("\033[%d;%dH%s\033[25;1H", row+2, 2, str); /* +1 +2 ? */
  426.    fflush (stdout);
  427. }
  428.  
  429.  
  430. uPC_reset()
  431. {
  432. /* Reset window to normal size. */
  433.    uPC_fixwind (0);
  434. }
  435.  
  436.  
  437.  
  438. uPC_fixwind(signo)
  439. int signo;
  440. {
  441.    static struct uwdata wreset = { 0, 12, 720, 288, 0x1};
  442.    struct utdata ut;
  443.  
  444. /* Reset the window to the right size. */
  445.    ioctl(0, WIOCSETD, &wreset);   /* 0, not wncur here! */
  446.  
  447. /* Clear the lines affected by an _lrput_text. */
  448.    ut.ut_text[0] = '\0';
  449.    ut.ut_num =  WTXTSLK1;
  450.    ioctl(0, WIOCSETTEXT, &ut);
  451.    ut.ut_num =  WTXTSLK2;
  452.    ioctl(0, WIOCSETTEXT, &ut);
  453. /* Scroll the screen once. (avoids typing over the same line) */
  454.    fprintf (stderr, "\n");
  455.  
  456.    if (signo) {
  457.       if (signo == SIGILL || signo == SIGTRAP || signo == SIGPWR)
  458.          signal (signo, SIG_DFL);
  459.       kill (0,signo);  /* Redo the signal (as if we never trapped it). */
  460.    }
  461. }
  462.