home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / lemming / part02 / lemx.c < prev   
Encoding:
C/C++ Source or Header  |  1987-08-05  |  5.5 KB  |  258 lines

  1. /*
  2.  * lemx.c - lemming driver for X window software
  3.  *
  4.  * copyright (c) by Alan W. Paeth, 1987. All rights reserved.
  5.  */
  6.  
  7. #include "lem.h"
  8. #include <X/Xlib.h>
  9.  
  10. #define EV (ButtonPressed | ButtonReleased | KeyPressed | ExposeWindow | ExposeRegion)
  11.  
  12. Window win;
  13. int xsave, ysave, drawcol, erasecol, selectcol, markoncol, markoffcol, lut;
  14. int sysmsgloc;
  15. char sysmsg[120];
  16. unsigned char maptab[256];    /* bit reversing table */
  17.  
  18. start()
  19.     {
  20.     screenw = 512;
  21.     screenh = 512;
  22.     if ((XOpenDisplay("")) == NULL) err("display open failed");
  23.     win = XCreateWindow(RootWindow, screenw/2, screenh/2, screenw, screenh,
  24.                 2, WhitePixmap, BlackPixmap);
  25.     if (win == NULL) err("window open failed");
  26.     lut = DisplayCells() > 2;
  27.     XMapWindow(win);
  28.     XSelectInput(win, EV);
  29.     if (lut)
  30.     {
  31.     cblack = findcolor(0,0,0);
  32.     cwhite = findcolor(1,1,1);
  33.     cred   = findcolor(1,0,0);
  34.     cgreen = findcolor(0,1,0);
  35.     tickdot = 0;
  36.     }
  37.     else
  38.     {
  39.     cwhite = 1;
  40.     cblack = 0;
  41.     cred   = 2;
  42.     cgreen = 1;
  43.     tickdot = 1;
  44.     }
  45.     setcursor();
  46.     }
  47.  
  48. stop()
  49.     {
  50.     }
  51.  
  52. getevent(xdn, ydn, xup, yup, ch)
  53.     int *xdn, *ydn, *xup, *yup;
  54.     char *ch;
  55.     {
  56.     int ret, count;
  57.     XEvent ev;
  58.     XWindowEvent(win, EV, &ev);
  59.     ret = NOEVT;
  60.     switch(ev.type)
  61.     {
  62.     case ButtonPressed:    xsave = mx(&ev);
  63.                 ysave = my(&ev);
  64.                 break;
  65.     case ButtonReleased:    *xdn = xsave;
  66.                 *ydn = ysave;
  67.                 *xup = mx(&ev);
  68.                 *yup = my(&ev);
  69.                 ret = MOUSE;
  70.                 break;
  71.     case KeyPressed:    *ch = *XLookupMapping(&ev, &count);
  72.                 if (*ch)
  73.                    {
  74.                    if ((*ch >= ' ') && (*ch != '\177'))
  75.                        ret = ALPHA;
  76.                    else if (*ch > 0) ret = CNTRL;
  77.                    }     
  78.                 break;
  79.     case ExposeRegion:
  80.     case ExposeWindow:    checkwindowsize();
  81.                 redraw();
  82.                 break;
  83.     }
  84.     XFlush();
  85.     return(ret);
  86.     }
  87.  
  88. checkwindowsize()
  89.     {
  90.     WindowInfo winfo;
  91.     XQueryWindow(win, &winfo);
  92.     screenw = winfo.width;
  93.     screenh = winfo.height;
  94.     }
  95.  
  96. setcursor()
  97.     {
  98.     Cursor c;
  99.     static short cur_bgnd[] = {
  100.     0x01c0, 0x01c0, 0x01c0, 0x01c0, 0x03e0, 0x0630, 0x7c1f, 0x7c1f,
  101.     0x7c1f, 0x0630, 0x03e0, 0x01c0, 0x01c0, 0x01c0, 0x01c0, 0x0000 };
  102.     static short cur_fgnd[] = {
  103.     0x0080, 0x0080, 0x0080, 0x0080, 0x03e0, 0x0630, 0x0410, 0x7c1f,
  104.     0x0410, 0x0630, 0x03e0, 0x0080, 0x0080, 0x0080, 0x0080, 0x0000 };
  105.     c = XCreateCursor(16, 16, cur_fgnd, cur_bgnd, 7,7,cwhite,cblack,GXcopy);
  106.     XDefineCursor(win, c);
  107.     }
  108.  
  109. setpixelrunh(x0, y0, wid, col, dotflag)
  110.     {
  111.     if (col == cred)
  112.     {
  113.     col = (dotflag) ? cblack: cwhite;    /* checkerboard select */
  114.     }
  115.     XPixSet(win, x0, (screenh-1)-y0, wid, 1, col);
  116.     }
  117.  
  118. setpixelrunv(x0, y0, wid, col, dotflag)
  119.     {
  120.     if (col == cred)
  121.     {
  122.     col = (dotflag) ? cblack: cwhite;    /* checkerboard select */
  123.     }
  124.     XPixSet(win, x0, (screenh-1)-y0, 1, wid, col);
  125.     }
  126.  
  127. drawvec(x0, y0, x1, y1, col, wid, emph)
  128.     {
  129. #define SOLID  SolidLine
  130. #define DASHES XMakePattern(0x00ff,16,1)
  131. #define DOTS   XMakePattern(0x0303,16,1)
  132.     Vertex chain[2];
  133.     int pattern;
  134.     if (lut)
  135.     {
  136. /*    XLine(win,x0,screenh-y0,x1,screenh-y1,wid,wid,col,GXcopy,AllPlanes); */
  137.     chain[0].x = x0 - wid/2;
  138.     chain[0].y = screenh-y0 - wid/2;
  139.     chain[0].flags = VertexDrawLastPoint;
  140.     chain[1].x = x1 - wid/2;
  141.     chain[1].y = screenh-y1 - wid/2;
  142.     chain[1].flags = VertexDrawLastPoint;
  143.     pattern = (emph==EMPHNONE) ? SOLID : ((emph==EMPHBOLD) ? DASHES : DOTS);
  144.     XDrawDashed(win, chain, 2, wid, wid, col, pattern, GXcopy, AllPlanes);
  145.     }
  146.     else drawline(x0-wid/2, y0+wid/2, x1-wid/2, y1+wid/2, wid, col, emph);
  147. /* SUN version does not understand dots and dashes -- we must do it all */
  148.     }
  149.  
  150. charshow(str)
  151.     char *str;
  152.     {
  153.     int i;
  154.     for(i=0; i<strlen(str); i++) sysmsg[sysmsgloc++] = str[i];
  155.     sysmsg[sysmsgloc] = '\0';
  156.     fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cgreen);
  157.     XFlush();
  158.     }
  159.  
  160. charunshow(n)
  161.     {
  162.     fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cblack);
  163.     while (n--)
  164.     {
  165.     if (sysmsgloc) sysmsg[--sysmsgloc] = '\0';
  166.     }
  167.     fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cgreen);
  168.     XFlush();
  169.     }
  170.  
  171. erase()
  172.     {
  173.     XPixSet(win, 0, 0, screenw, screenh, cblack);
  174.     fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cgreen);
  175.     XFlush();
  176.     }
  177.  
  178. /*
  179.  * internal stuff
  180.  */
  181.  
  182. findcolor(r, g, b)
  183.     int r, g, b;
  184.     {
  185.     Color c;
  186.     c.red = r*65535;
  187.     c.green= g*65535;
  188.     c.blue = b*65535;
  189.     if (!XGetHardwareColor(&c))
  190.     {
  191. /*    err("no more room in color table"); */
  192.     return(b*4+g*2+r);
  193.     }
  194.     return(c.pixel);
  195.     }
  196.  
  197. mx(ev)
  198.     XKeyEvent *ev;
  199.     {
  200.     return(ev->x);
  201.     }
  202.  
  203. my(ev)
  204.     XKeyEvent *ev;
  205.     {
  206.     return(screenh - ev->y + 1);
  207.     }
  208.  
  209. writescan(x, y, pixels, outaddr, color)
  210.     int x, y, pixels, *outaddr, color;
  211.     {
  212.     if (pixels <= 0) return;    /* PARANOIA: should not ever happen */
  213.     if (!lut && color == cred)
  214.     {
  215.     register int i, lim;
  216.     lim = (pixels+31)/32;
  217.     if ((x^y) & 0x1) for(i=0; i<lim; i++) outaddr[i] &= 0x55555555;
  218.     else for(i=0; i<lim; i++) outaddr[i] &= 0xaaaaaaaa;
  219.     color = cwhite;
  220.     }
  221.     swapshorts(outaddr, (pixels+31)/8);
  222.     XBitmapBitsPut(win, x, y, pixels, 1,
  223.     outaddr, color, cblack, 0, GXcopy, AllPlanes);
  224.     }
  225.  
  226. swapshorts(buf, len)
  227.     register unsigned char *buf;
  228.     {
  229.     unsigned char c1, c2, c3, c4;
  230.     register int count, i;
  231.     if (!maptab[1]) maptabinit();
  232.     count = len/4;
  233.     for (i=0; i<count; i++)
  234.     {
  235.     c1 = *buf++;
  236.     c2 = *buf++;
  237.     c3 = *buf++;
  238.     c4 = *buf;
  239.     *buf-- = maptab[c1];
  240.     *buf-- = maptab[c2];
  241.     *buf-- = maptab[c3];
  242.     *buf   = maptab[c4];
  243.     buf += 4;
  244.     }
  245.     }
  246.  
  247. maptabinit()
  248.     {
  249.     register int i, j;
  250.     unsigned char mask;
  251.     for(i = 0; i<256; i++)
  252.     {
  253.     mask = 0;
  254.     for(j = 0; j < 8; j++) if (i & (1<<j)) mask |= 1<<(7-j);
  255.     maptab[i] = mask;
  256.     }
  257.     }
  258.