home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / VOGLE / DRIVERS / TEK.C < prev    next >
C/C++ Source or Header  |  2000-02-11  |  7KB  |  455 lines

  1.  
  2. /*
  3.  *    Driver for tekronix 401x or equivalent
  4.  */
  5. #include <stdio.h>
  6. #ifdef BSD
  7. #include <sgtty.h>
  8. #else
  9. #include <termios.h>
  10. #endif
  11. #include "vogle.h"
  12.  
  13. #define         MASK            037
  14. #define         BELL            '\007'
  15. #define         FF              '\014'
  16. #define         CAN             '\030'
  17. #define         SUB             '\032'
  18. #define         ESC             '\033'
  19. #define         GS              '\035'
  20. #define         US              '\037'
  21.  
  22. #define    TEK_X_SIZE        1023
  23. #define    TEK_Y_SIZE        767
  24.  
  25. #include <signal.h>
  26.  
  27. static int    LoYold = -1, HiYold = -1, HiXold = -1;
  28. static int    tlstx, tlsty;
  29. static int    click;            /* to emulate a mouse click */
  30.  
  31. /*
  32.  * noop
  33.  *
  34.  *      do nothing but return -1
  35.  */
  36. static int
  37. noop()
  38. {
  39.     return(-1);
  40. }
  41.  
  42. /*
  43.  * TEK_init
  44.  *
  45.  *    set up the graphics mode.
  46.  */
  47. int
  48. TEK_init()
  49. {
  50.     /*
  51.      *      actually only need to set modes in xhair and pause routines
  52.      */
  53.  
  54.     vdevice.depth = 1;
  55.  
  56.     putchar(GS);            /* enter graphics mode */
  57.  
  58.     vdevice.sizeX = vdevice.sizeY = TEK_Y_SIZE;
  59.     vdevice.sizeSx = TEK_X_SIZE;
  60.     vdevice.sizeSy = TEK_Y_SIZE;
  61.                   
  62.     tlstx = tlsty = -1;
  63.  
  64.         return(1);
  65. }
  66.  
  67.  
  68. /*
  69.  * TEK_exit
  70.  *
  71.  *    cleans up before going back to normal mode
  72.  */
  73. int
  74. TEK_exit()
  75. {
  76.     putchar(US);
  77.     putchar(CAN);
  78.  
  79.     return(0);
  80. }
  81.  
  82. /*
  83.  * out_bytes
  84.  *
  85.  *    output 2 optomized bytes to the terminal
  86.  */
  87. static void
  88. out_bytes(x,y)
  89.     int    x, y;
  90. {
  91.     int HiY, LoY, HiX, LoX;
  92.  
  93.     if (x < 0)            /* a bit of last minute checking */
  94.         x = 0;
  95.     if (y < 0)
  96.         y = 0;
  97.     if (x > 1023)
  98.         x = 1023;
  99.     if (y > 1023)
  100.         y = 1023;
  101.  
  102.     HiY = y / 32  +  32;
  103.     LoY = y % 32  +  96;
  104.     HiX = x / 32  +  32;
  105.     LoX = x % 32  +  64;
  106.  
  107.     /*
  108.      * optomize the output stream. Ref: Tektronix Manual.
  109.      */
  110.  
  111.     if (HiYold != HiY) {
  112.         printf("%c", HiY);
  113.         HiYold = HiY;
  114.     }
  115.  
  116.     if ((LoYold != LoY) || (HiXold != HiX)) {
  117.         printf("%c", LoY);
  118.         LoYold = LoY;
  119.         if (HiXold != HiX) {
  120.             printf("%c", HiX);
  121.             HiXold = HiX;
  122.         }
  123.     }
  124.  
  125.     printf("%c", LoX);
  126. }
  127.  
  128. /*
  129.  * TEK_draw
  130.  *
  131.  *    draw from the current graphics position to the new one (x, y)
  132.  */
  133. int
  134. TEK_draw(x, y)
  135.     int    x, y;
  136. {
  137.     if (tlstx != vdevice.cpVx || tlsty != vdevice.cpVy) {
  138.         putchar(GS);
  139.         LoYold = HiYold = HiXold = -1;  /* Force output of all bytes */
  140.         out_bytes(vdevice.cpVx, vdevice.cpVy);
  141.     }
  142.  
  143.     out_bytes(x, y);
  144.     tlstx = x;
  145.     tlsty = y;
  146.  
  147.     fflush(stdout);
  148.  
  149.     return(0);
  150. }
  151.  
  152. /*
  153.  * TEK_getkey
  154.  *
  155.  *    return the next key typed.
  156.  */
  157. int
  158. TEK_getkey()
  159. {
  160. #ifdef BSD
  161.     struct sgttyb    oldtty, newtty;
  162.     char        c;
  163.  
  164.     ioctl(0, TIOCGETP, &oldtty);
  165.  
  166.     newtty = oldtty;
  167.     newtty.sg_flags = RAW;
  168.  
  169.     ioctl(0, TIOCSETP, &newtty);
  170.  
  171.     read(0, &c, 1);
  172.  
  173.     ioctl(0, TIOCSETP, &oldtty);
  174. #else
  175.     struct termios   oldtty, newtty;
  176.     char            c;
  177.       
  178.     tcgetattr(0, &oldtty);
  179.     newtty = oldtty;
  180.     newtty.c_iflag = BRKINT | IXON | ISTRIP;
  181.     newtty.c_lflag = 0;
  182.     newtty.c_cc[VEOF] = 1;
  183.  
  184.     tcsetattr(0, TCSANOW, &newtty);
  185.  
  186.     read(0, &c, 1);
  187.  
  188.     tcsetattr(0, TCSANOW, &oldtty);
  189. #endif
  190.  
  191.     return(c);
  192. }
  193.  
  194. /*
  195.  * TEK_locator
  196.  *
  197.  *    get the position of the crosshairs. This gets a bit sticky since
  198.  * we have no mouse, and the crosshairs do not beahve like a mouse - even a rat!
  199.  * In this case the keys 1 to 9 are used, with each one returning a power of
  200.  * two.
  201.  */
  202. int
  203. TEK_locator(x, y)
  204.     int    *x, *y;
  205. {
  206.     char        buf[5];
  207.     int        i;
  208. #ifdef BSD
  209.     struct sgttyb   oldtty, newtty;
  210. #else
  211.     struct termios  oldtty, newtty;
  212. #endif
  213.  
  214.     if (click) {            /* for compatability with other devs */
  215.         click = 0;
  216.         return(0);
  217.     }
  218.  
  219.     click = 1;
  220.  
  221. #ifdef BSD
  222.     ioctl(0, TIOCGETP, &oldtty);
  223.  
  224.     newtty = oldtty;
  225.     newtty.sg_flags = RAW;
  226.  
  227.     ioctl(0, TIOCSETP, &newtty);
  228. #else
  229.     tcgetattr(0, &oldtty);
  230.  
  231.     newtty = oldtty;
  232.     newtty.c_iflag = BRKINT | IXON | ISTRIP;
  233.     newtty.c_lflag = 0;
  234.     newtty.c_cc[VEOF] = 1;
  235.  
  236.     tcsetattr(0, TCSANOW, &newtty);
  237. #endif
  238.  
  239.     fputs("\037\033\032", stdout); 
  240.     fflush(stdout);
  241.  
  242.     /* Tek 4010/4014 return 8 bytes upon cross-hair read:
  243.      *
  244.      *
  245.      *        0    character pressed
  246.      *
  247.      *        1,2    encoded x position
  248.      *        3,4    encoded y position
  249.      *        5,6    CR,LF        - ignored
  250.      *        7    EOF        - ignored
  251.      */
  252.  
  253.     /* first we read in the five meaningfull bytes */
  254.  
  255.     for (i = 0; i < 5; i++)
  256.         buf[i] = getchar();
  257.  
  258.     /* just in case we get the newline chars */
  259.  
  260. #ifdef BSD
  261.     ioctl(0, TIOCFLUSH, (char *)NULL);
  262. #else
  263.     tcflush(0, TCIFLUSH);
  264. #endif
  265.  
  266.     *x = ((buf[1] & MASK) << 5) | (buf[2] & MASK); 
  267.     *y = ((buf[3] & MASK) << 5) | (buf[4] & MASK);
  268.  
  269. #ifdef BSD
  270.     ioctl(0, TIOCSETP, &oldtty);
  271. #else
  272.     tcsetattr(0, TCSANOW, &oldtty);
  273. #endif
  274.  
  275.     tlstx = tlsty = -1;
  276.  
  277.     return(1 << ((int)buf[0] - '1'));
  278. }
  279.  
  280. /*
  281.  * TEK_clear
  282.  *
  283.  *    clear the screen.
  284.  *
  285.  *    NOTE - You may need to actually output a certain number of
  286.  *    NUL chars here to get the (at least) 1 second delay.
  287.  *    This may occur if running through a communication package
  288.  *    or with something like ethernet. If this is the case then
  289.  *    throw away the sleep(2) above and bung in a loop.
  290.  *    Here's a sample ...
  291.  *
  292.  *    for (i = 0; i < 960; i++) putchar(0);
  293.  *
  294.  *    (for 9600 baud rate)
  295.  *
  296.  */
  297. int
  298. TEK_clear()
  299. {
  300.     putchar(US);
  301.     putchar(ESC);
  302.     putchar(FF);
  303.     fflush(stdout);
  304.  
  305.     tlstx = tlsty = -1;
  306.  
  307.     sleep(2); /* for tekronix slow erase */
  308.  
  309.     return(0);
  310. }
  311.  
  312. /*
  313.  * TEK_font
  314.  *
  315.  *    set for large or small mode.
  316.  */
  317. int
  318. TEK_font(font)
  319.     char    *font;
  320. {
  321.     if (strcmp(font, "small") == 0) {
  322.         printf("\033:");
  323.         vdevice.hwidth = 8.0;
  324.         vdevice.hheight = 15.0;
  325.     } else if (strcmp(font, "large") == 0) {
  326.         printf("\0338");
  327.         vdevice.hwidth = 14.0;
  328.         vdevice.hheight = 17.0;
  329.     } else
  330.         return(0);
  331.  
  332.     tlstx = tlsty = -1;
  333.  
  334.     return(1);
  335. }
  336.  
  337. /*
  338.  * TEK_char
  339.  *
  340.  *    outputs one char
  341.  */
  342. int
  343. TEK_char(c)
  344.     char    c;
  345. {
  346.     if (tlstx != vdevice.cpVx || tlsty != vdevice.cpVy) {
  347.         putchar(GS);
  348.         LoYold = HiYold = HiXold = -1;  /* Force output of all bytes */
  349.         out_bytes(vdevice.cpVx, vdevice.cpVy);
  350.     }
  351.  
  352.     putchar(US);
  353.     putchar(c);
  354.  
  355.     tlstx = tlsty = -1;
  356.  
  357.     fflush(stdout);
  358.  
  359.     return(0);
  360. }
  361.  
  362. /*
  363.  * TEK_string
  364.  *
  365.  *    outputs a string
  366.  */
  367. int
  368. TEK_string(s)
  369.     char    *s;
  370. {
  371.     if (tlstx != vdevice.cpVx || tlsty != vdevice.cpVy) {    /* move to start */
  372.         putchar(GS);
  373.         LoYold = HiYold = HiXold = -1;  /* Force output of all bytes */
  374.         out_bytes(vdevice.cpVx, vdevice.cpVy);
  375.     }
  376.  
  377.     putchar(US);
  378.     fputs(s, stdout);
  379.  
  380.     tlstx = tlsty = -1;
  381.  
  382.     fflush(stdout);
  383.  
  384.     return(0);
  385. }
  386.  
  387. /*
  388.  * TEK_fill
  389.  *
  390.  *      "fill" a polygon
  391.  */
  392. int
  393. TEK_fill(n, x, y)
  394.     int     n, x[], y[];
  395. {
  396.     int     i;
  397.  
  398.     if (tlstx != x[0] || tlsty != y[0]) {
  399.         putchar(GS);
  400.         LoYold = HiYold = HiXold = -1;  /* Force output of all bytes */
  401.         out_bytes(x[0], y[0]);
  402.     }
  403.  
  404.     for (i = 1; i < n; i++)
  405.         out_bytes(x[i], y[i]);
  406.  
  407.     out_bytes(x[0], y[0]);
  408.  
  409.     fflush(stdout);
  410.  
  411.     tlstx = vdevice.cpVx = x[n - 1];
  412.     tlsty = vdevice.cpVy = y[n - 1];
  413.  
  414.     return(0);
  415. }
  416.  
  417. /*
  418.  * the device entry
  419.  */
  420. static DevEntry    tekdev = {
  421.     "tek",
  422.     "large",
  423.     "small",
  424.     noop,
  425.     TEK_char,
  426.     noop,
  427.     TEK_clear,
  428.     noop,
  429.     TEK_draw,
  430.     TEK_exit,
  431.     TEK_fill,
  432.     TEK_font,
  433.     noop,
  434.     TEK_getkey,
  435.     TEK_init,
  436.     TEK_locator,
  437.     noop,
  438.     noop,
  439.     TEK_string,
  440.     noop,
  441.     noop
  442. };
  443.  
  444. /*
  445.  * _TEK_devcpy
  446.  *
  447.  *      copy the tektronix device into vdevice.dev.
  448.  */
  449. void
  450. _TEK_devcpy()
  451. {
  452.         vdevice.dev = tekdev;
  453. }
  454.  
  455.