home *** CD-ROM | disk | FTP | other *** search
- /*
- * lemx.c - lemming driver for X window software
- *
- * copyright (c) by Alan W. Paeth, 1987. All rights reserved.
- */
-
- #include "lem.h"
- #include <X/Xlib.h>
-
- #define EV (ButtonPressed | ButtonReleased | KeyPressed | ExposeWindow | ExposeRegion)
-
- Window win;
- int xsave, ysave, drawcol, erasecol, selectcol, markoncol, markoffcol, lut;
- int sysmsgloc;
- char sysmsg[120];
- unsigned char maptab[256]; /* bit reversing table */
-
- start()
- {
- screenw = 512;
- screenh = 512;
- if ((XOpenDisplay("")) == NULL) err("display open failed");
- win = XCreateWindow(RootWindow, screenw/2, screenh/2, screenw, screenh,
- 2, WhitePixmap, BlackPixmap);
- if (win == NULL) err("window open failed");
- lut = DisplayCells() > 2;
- XMapWindow(win);
- XSelectInput(win, EV);
- if (lut)
- {
- cblack = findcolor(0,0,0);
- cwhite = findcolor(1,1,1);
- cred = findcolor(1,0,0);
- cgreen = findcolor(0,1,0);
- tickdot = 0;
- }
- else
- {
- cwhite = 1;
- cblack = 0;
- cred = 2;
- cgreen = 1;
- tickdot = 1;
- }
- setcursor();
- }
-
- stop()
- {
- }
-
- getevent(xdn, ydn, xup, yup, ch)
- int *xdn, *ydn, *xup, *yup;
- char *ch;
- {
- int ret, count;
- XEvent ev;
- XWindowEvent(win, EV, &ev);
- ret = NOEVT;
- switch(ev.type)
- {
- case ButtonPressed: xsave = mx(&ev);
- ysave = my(&ev);
- break;
- case ButtonReleased: *xdn = xsave;
- *ydn = ysave;
- *xup = mx(&ev);
- *yup = my(&ev);
- ret = MOUSE;
- break;
- case KeyPressed: *ch = *XLookupMapping(&ev, &count);
- if (*ch)
- {
- if ((*ch >= ' ') && (*ch != '\177'))
- ret = ALPHA;
- else if (*ch > 0) ret = CNTRL;
- }
- break;
- case ExposeRegion:
- case ExposeWindow: checkwindowsize();
- redraw();
- break;
- }
- XFlush();
- return(ret);
- }
-
- checkwindowsize()
- {
- WindowInfo winfo;
- XQueryWindow(win, &winfo);
- screenw = winfo.width;
- screenh = winfo.height;
- }
-
- setcursor()
- {
- Cursor c;
- static short cur_bgnd[] = {
- 0x01c0, 0x01c0, 0x01c0, 0x01c0, 0x03e0, 0x0630, 0x7c1f, 0x7c1f,
- 0x7c1f, 0x0630, 0x03e0, 0x01c0, 0x01c0, 0x01c0, 0x01c0, 0x0000 };
- static short cur_fgnd[] = {
- 0x0080, 0x0080, 0x0080, 0x0080, 0x03e0, 0x0630, 0x0410, 0x7c1f,
- 0x0410, 0x0630, 0x03e0, 0x0080, 0x0080, 0x0080, 0x0080, 0x0000 };
- c = XCreateCursor(16, 16, cur_fgnd, cur_bgnd, 7,7,cwhite,cblack,GXcopy);
- XDefineCursor(win, c);
- }
-
- setpixelrunh(x0, y0, wid, col, dotflag)
- {
- if (col == cred)
- {
- col = (dotflag) ? cblack: cwhite; /* checkerboard select */
- }
- XPixSet(win, x0, (screenh-1)-y0, wid, 1, col);
- }
-
- setpixelrunv(x0, y0, wid, col, dotflag)
- {
- if (col == cred)
- {
- col = (dotflag) ? cblack: cwhite; /* checkerboard select */
- }
- XPixSet(win, x0, (screenh-1)-y0, 1, wid, col);
- }
-
- drawvec(x0, y0, x1, y1, col, wid, emph)
- {
- #define SOLID SolidLine
- #define DASHES XMakePattern(0x00ff,16,1)
- #define DOTS XMakePattern(0x0303,16,1)
- Vertex chain[2];
- int pattern;
- if (lut)
- {
- /* XLine(win,x0,screenh-y0,x1,screenh-y1,wid,wid,col,GXcopy,AllPlanes); */
- chain[0].x = x0 - wid/2;
- chain[0].y = screenh-y0 - wid/2;
- chain[0].flags = VertexDrawLastPoint;
- chain[1].x = x1 - wid/2;
- chain[1].y = screenh-y1 - wid/2;
- chain[1].flags = VertexDrawLastPoint;
- pattern = (emph==EMPHNONE) ? SOLID : ((emph==EMPHBOLD) ? DASHES : DOTS);
- XDrawDashed(win, chain, 2, wid, wid, col, pattern, GXcopy, AllPlanes);
- }
- else drawline(x0-wid/2, y0+wid/2, x1-wid/2, y1+wid/2, wid, col, emph);
- /* SUN version does not understand dots and dashes -- we must do it all */
- }
-
- charshow(str)
- char *str;
- {
- int i;
- for(i=0; i<strlen(str); i++) sysmsg[sysmsgloc++] = str[i];
- sysmsg[sysmsgloc] = '\0';
- fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cgreen);
- XFlush();
- }
-
- charunshow(n)
- {
- fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cblack);
- while (n--)
- {
- if (sysmsgloc) sysmsg[--sysmsgloc] = '\0';
- }
- fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cgreen);
- XFlush();
- }
-
- erase()
- {
- XPixSet(win, 0, 0, screenw, screenh, cblack);
- fontwrite(SYSFONT, 10, 10, sysmsg, EMPHNONE, cgreen);
- XFlush();
- }
-
- /*
- * internal stuff
- */
-
- findcolor(r, g, b)
- int r, g, b;
- {
- Color c;
- c.red = r*65535;
- c.green= g*65535;
- c.blue = b*65535;
- if (!XGetHardwareColor(&c))
- {
- /* err("no more room in color table"); */
- return(b*4+g*2+r);
- }
- return(c.pixel);
- }
-
- mx(ev)
- XKeyEvent *ev;
- {
- return(ev->x);
- }
-
- my(ev)
- XKeyEvent *ev;
- {
- return(screenh - ev->y + 1);
- }
-
- writescan(x, y, pixels, outaddr, color)
- int x, y, pixels, *outaddr, color;
- {
- if (pixels <= 0) return; /* PARANOIA: should not ever happen */
- if (!lut && color == cred)
- {
- register int i, lim;
- lim = (pixels+31)/32;
- if ((x^y) & 0x1) for(i=0; i<lim; i++) outaddr[i] &= 0x55555555;
- else for(i=0; i<lim; i++) outaddr[i] &= 0xaaaaaaaa;
- color = cwhite;
- }
- swapshorts(outaddr, (pixels+31)/8);
- XBitmapBitsPut(win, x, y, pixels, 1,
- outaddr, color, cblack, 0, GXcopy, AllPlanes);
- }
-
- swapshorts(buf, len)
- register unsigned char *buf;
- {
- unsigned char c1, c2, c3, c4;
- register int count, i;
- if (!maptab[1]) maptabinit();
- count = len/4;
- for (i=0; i<count; i++)
- {
- c1 = *buf++;
- c2 = *buf++;
- c3 = *buf++;
- c4 = *buf;
- *buf-- = maptab[c1];
- *buf-- = maptab[c2];
- *buf-- = maptab[c3];
- *buf = maptab[c4];
- buf += 4;
- }
- }
-
- maptabinit()
- {
- register int i, j;
- unsigned char mask;
- for(i = 0; i<256; i++)
- {
- mask = 0;
- for(j = 0; j < 8; j++) if (i & (1<<j)) mask |= 1<<(7-j);
- maptab[i] = mask;
- }
- }
-