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