home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / lemming / part01 / lemik.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-05  |  6.0 KB  |  290 lines

  1. /*
  2.  * lemik.c - ikonas driver for lemming software
  3.  *
  4.  * copyright (c) by Alan W. Paeth, 1987. All rights reserved.
  5.  */
  6.  
  7. #include "lem.h"
  8.  
  9. #include <graphics/ik_const.h>
  10. #include <local/Locator.h>
  11. #include <errno.h>
  12. #include <sys/time.h>
  13. #include <sgtty.h>
  14. #include <signal.h>
  15.  
  16. #define MASKREG 0
  17. #define STDIN 0
  18. #define OBITS 0xffffff
  19. #define RED   0x0000ff
  20. #define GREEN 0x00ff00
  21. #define BLACK 0x000000
  22. #define WHITE 0xffffff
  23.  
  24. struct In_config *Id, *Iopen();
  25. struct In_state st;
  26. char *inDevice, *getenv(), readchar();
  27.  
  28. /* cursor stuff */
  29.  
  30. #define CURLEN 32
  31. #define CUROFFX 16
  32. #define CUROFFY 16
  33. #define LCLK 49
  34. #define LWYOFF 33
  35.  
  36. #define SETCUR(x, y) cur[(y)] |= 1<<((x));
  37. #define HL(a,b) (((a)<<16)+((b)&0xffff))
  38.  
  39. static int cur[CURLEN];
  40.  
  41. /*
  42.  * external routines
  43.  */
  44.  
  45. setpixelrunh(x, y, wid, val)
  46.     {
  47.     int i;
  48.     for (i=0; i<wid; i++)
  49.     Ik_diowr(IK_XY_ADDR, x+i, 511-y, val);
  50.     }
  51.  
  52. setpixelrunv(x, y, wid, val)
  53.     {
  54.     int i;
  55.     for (i=0; i<wid; i++)
  56.     Ik_diowr(IK_XY_ADDR, x, 511-y+i, val);
  57.     }
  58.  
  59. start()
  60.     {
  61. /*
  62.  * system defaults
  63.  */
  64.     screenw = 512;
  65.     screenh = 512;
  66.     cred = RED;
  67.     cgreen = GREEN;
  68.     cblack = BLACK;
  69.     cwhite = WHITE;
  70.     tickdot = 1;
  71. /*
  72.  * initialization
  73.  */
  74.     startcbreak();
  75.     Ik_open();
  76.     Ik_init();
  77. /* Ik_zoom_pan(0, 0, 508, 480, 0, 0, 1, 1, 0, 1); */ /* last for 1:1,not 4:3 */
  78.     Ik_mask_set(MASKREG, OBITS);
  79.     cursorenable();
  80.  
  81.     if( (inDevice = getenv("INPUT") ) == NULL )
  82.     inDevice = "ikonas";
  83.     if ( (Id = Iopen(inDevice, I_DEFAULT)) == NULL) err("can't open tablet");
  84.     Iwindow( Id, 0, 511, 0, 511, 0, 32 );
  85.     Ictl(Id, I_ENABLE, 0);    /* make the tablet free-running *sigh* */
  86.     }
  87.  
  88. stop()
  89.     {
  90.     cursordisable();
  91.     Iclose(Id);
  92.     Ik_close();
  93.     stopcbreak();
  94.     }
  95.  
  96. getevent(xdown, ydown, xup, yup, ch)
  97.     char *ch;
  98.     int *xdown, *ydown, *xup, *yup;
  99.     {
  100.     int lastx, lasty, mousedesc, dev;
  101.     mousedesc = fileno(Id->In_ifp);
  102.     dev = checkevent(mousedesc, STDIN, 1);    /*  blocking (hah!) */
  103.     if (dev == STDIN)
  104.     {
  105.     *ch = readchar();
  106.     return(((*ch < ' ') || (*ch == '\177')) ? CNTRL : ALPHA);
  107.     }
  108.     if (dev == mousedesc)
  109.     {
  110.     Iread(Id, &st, COORDINATES);
  111.     cursorplace(st.x, st.y);
  112.     lastx = st.x;
  113.     lasty = st.y;
  114.     if (st.button == 0) return(NOEVT);
  115.     else
  116.         {
  117.         *xdown = st.x;
  118.         *ydown = st.y;
  119.         do  {
  120.         Iread(Id, &st, COORDINATES);
  121.         *yup = st.y;
  122.         *xup = st.x;
  123.         if ((lastx != st.x) || (lasty != st.y)) cursorplace(st.x, st.y);
  124.         lastx = st.x;
  125.         lasty = st.y;
  126.         } while (st.button != 0);
  127.         }
  128.     return(MOUSE);
  129.     }
  130.     return(NOEVT);
  131.     }
  132.  
  133. charshow(str)
  134.     char *str;
  135.     {
  136.     fprintf(stdout, "%s", str);
  137.     fflush(stdout);
  138.     }
  139.  
  140. charunshow(n)
  141.     {
  142.     int i;
  143.     for (i=0; i<n; i++) fprintf(stdout, "%c %c", '\b', '\b');
  144.     fflush(stdout);
  145.     }
  146.  
  147. drawvec(x0, y0, x1, y1, col, wid, emph)
  148.     {
  149.     drawline(x0, y0, x1, y1, wid, col, emph);
  150.     }
  151.  
  152. erase()
  153.     {
  154.     Ik_clear();
  155.     }
  156.  
  157. /*
  158.  * internal routines
  159.  */
  160.  
  161. cursorenable()
  162.     {
  163.     long i;
  164.     int ncolor[IK_MAP_SIZE];
  165.     Ik_set_mode( SET_DURING_RETRACE );
  166.     Ik_mask_set(0, 0xffffff);
  167.     Ik_dmard(IK_WD_ADDR, IK_COLOUR_MAPS, IK_MAP_SIZE*0, ncolor, IK_MAP_SIZE);
  168.     for (i=0; i<IK_MAP_SIZE; i++) ncolor[i] = (-1) ^ ncolor[i];
  169.     Ik_dmawr(IK_WD_ADDR, IK_COLOUR_MAPS, IK_MAP_SIZE*1, ncolor, IK_MAP_SIZE);
  170.     Ik_dmawr(IK_WD_ADDR, IK_COLOUR_MAPS, IK_MAP_SIZE*3, ncolor, IK_MAP_SIZE);
  171.     Ik_diowr( IK_WD_ADDR, IK_XBAR_SWITCH, 32, 32); /* guarantee incursor bit */
  172.     for (i=0; i<CURLEN; i++) cur[i] = 0;    /* clear cursor */
  173.     for (i=0; i<CURLEN/4; i++)            /* draw open + sign */
  174.     {
  175.     SETCUR(i+CURLEN/8,   CURLEN/2);
  176.     SETCUR(7*CURLEN/8-i, CURLEN/2);
  177.     SETCUR(CURLEN/2,     i+CURLEN/8);
  178.     SETCUR(CURLEN/2,     7*CURLEN/8-i);
  179.     }
  180.     Ik_curs_map(cur, CURLEN/2, CURLEN/2);
  181.     cursorplace(0, 0);
  182.     }
  183.  
  184. cursordisable()
  185.     {
  186.     int i;
  187.     for (i=0; i<CURLEN; i++) cur[i] = 0;
  188.     Ik_curs_map(cur, 0, 0);
  189.     Ik_diowr( IK_WD_ADDR, IK_FBC_REGS, FBC_MODE, HL(LCLK,  0x40));
  190.     }
  191.  
  192. cursorplace(x, y)
  193.     {
  194.     Ik_diowr(IK_WD_ADDR, IK_FBC_REGS, FBC_CURSOR,
  195.     HL((511-y)+LWYOFF-CUROFFY-5, x-CUROFFX));
  196.     Ik_diowr( IK_WD_ADDR, IK_FBC_REGS, FBC_MODE, HL(LCLK,  0x44));
  197.     }
  198.  
  199. /*
  200.  * UNIX code to test for pending STDIN characters (in CBREAK mode)
  201.  * and capture ^Z interruptions for the sake of CBREAK problems.
  202.  */
  203.  
  204. int stopcatch();
  205.  
  206. stopcbreak()
  207.     {
  208.     struct sgttyb tty;
  209.     ioctl(STDIN, TIOCGETP, &tty);
  210.     tty.sg_flags &= ~CBREAK;
  211.     tty.sg_flags |= ECHO;
  212.     ioctl(STDIN, TIOCSETP, &tty);
  213.     signal(SIGTSTP, SIG_DFL);
  214.     }
  215.  
  216. startcbreak()
  217.     {
  218.     struct sgttyb tty;
  219.     ioctl(STDIN, TIOCGETP, &tty);
  220.     tty.sg_flags |= CBREAK;
  221.     tty.sg_flags &= ~ECHO;
  222.     ioctl(STDIN, TIOCSETP, &tty);
  223.     signal(SIGTSTP, stopcatch);
  224.     }
  225.  
  226. stopcatch()
  227.     {
  228. /* ^Z just typed.  */
  229.     stopcbreak();
  230.     sigsetmask (sigblock(0) & ~(1<<(SIGTSTP-1))); /* turn SIGTSTP on */
  231.     kill(0, SIGTSTP);            /* STOP THYSELF! */
  232. /* you just returned */
  233.     startcbreak();            /* restore action for next time */
  234.     }
  235.  
  236. checkevent(l, r, blockflag)
  237.     {
  238.     extern int errno;
  239.     int rdesc, retcode, bits;
  240.     struct timeval tv;
  241.     do    {
  242.     rdesc = (1<<l) | (1<<r);
  243.     bits = (l > r) ? l+1 : r+1;
  244.     bzero(&tv, sizeof tv);        /* zero timer -> poll, no waiting */
  245.     retcode = select(bits, &rdesc, 0, 0, blockflag ? 0 : &tv);
  246.     } while ((retcode < 0) && (errno == EINTR));
  247.     if (retcode <= 0) return(-1);
  248.     return((rdesc == (1<<l)) ? l : r);
  249.     }
  250.  
  251. char readchar()
  252.     {
  253.     char ch;
  254.     read(STDIN, &ch, 1);
  255.     return(ch);
  256.     }
  257.  
  258. writescan(x, y, pixels, outaddr, color)
  259.     int x, y, pixels, *outaddr, color;
  260.     {
  261.     static int IKbuf[512];
  262.     int words, *bufbase;
  263.  
  264. /* register */
  265.     register int next, *wordbase, regfontcolor;
  266.  
  267.     if (pixels <= 0) return;        /* PARANOIA: should not ever happen */
  268.  
  269.     regfontcolor = color;        /* for a little speed */
  270.     words = (pixels - 1) / 32 + 1;
  271.     bufbase = IKbuf;
  272.     Ik_dmard(IK_XY_ADDR, x, y, IKbuf, pixels);
  273.  
  274.     ++words;
  275.     while (--words)
  276.     {
  277.     next = *outaddr;
  278.     ++outaddr;
  279.     wordbase = bufbase;
  280.     while (next)
  281.         {
  282.         if (next < 0) *wordbase = regfontcolor;
  283.         ++wordbase;
  284.         next<<=1;
  285.         }
  286.     bufbase += 32;
  287.     }
  288.     Ik_dmawr(IK_XY_ADDR, x, y, IKbuf, pixels);
  289.     }
  290.