home *** CD-ROM | disk | FTP | other *** search
- /*
- * lemik.c - ikonas driver for lemming software
- *
- * copyright (c) by Alan W. Paeth, 1987. All rights reserved.
- */
-
- #include "lem.h"
-
- #include <graphics/ik_const.h>
- #include <local/Locator.h>
- #include <errno.h>
- #include <sys/time.h>
- #include <sgtty.h>
- #include <signal.h>
-
- #define MASKREG 0
- #define STDIN 0
- #define OBITS 0xffffff
- #define RED 0x0000ff
- #define GREEN 0x00ff00
- #define BLACK 0x000000
- #define WHITE 0xffffff
-
- struct In_config *Id, *Iopen();
- struct In_state st;
- char *inDevice, *getenv(), readchar();
-
- /* cursor stuff */
-
- #define CURLEN 32
- #define CUROFFX 16
- #define CUROFFY 16
- #define LCLK 49
- #define LWYOFF 33
-
- #define SETCUR(x, y) cur[(y)] |= 1<<((x));
- #define HL(a,b) (((a)<<16)+((b)&0xffff))
-
- static int cur[CURLEN];
-
- /*
- * external routines
- */
-
- setpixelrunh(x, y, wid, val)
- {
- int i;
- for (i=0; i<wid; i++)
- Ik_diowr(IK_XY_ADDR, x+i, 511-y, val);
- }
-
- setpixelrunv(x, y, wid, val)
- {
- int i;
- for (i=0; i<wid; i++)
- Ik_diowr(IK_XY_ADDR, x, 511-y+i, val);
- }
-
- start()
- {
- /*
- * system defaults
- */
- screenw = 512;
- screenh = 512;
- cred = RED;
- cgreen = GREEN;
- cblack = BLACK;
- cwhite = WHITE;
- tickdot = 1;
- /*
- * initialization
- */
- startcbreak();
- Ik_open();
- Ik_init();
- /* Ik_zoom_pan(0, 0, 508, 480, 0, 0, 1, 1, 0, 1); */ /* last for 1:1,not 4:3 */
- Ik_mask_set(MASKREG, OBITS);
- cursorenable();
-
- if( (inDevice = getenv("INPUT") ) == NULL )
- inDevice = "ikonas";
- if ( (Id = Iopen(inDevice, I_DEFAULT)) == NULL) err("can't open tablet");
- Iwindow( Id, 0, 511, 0, 511, 0, 32 );
- Ictl(Id, I_ENABLE, 0); /* make the tablet free-running *sigh* */
- }
-
- stop()
- {
- cursordisable();
- Iclose(Id);
- Ik_close();
- stopcbreak();
- }
-
- getevent(xdown, ydown, xup, yup, ch)
- char *ch;
- int *xdown, *ydown, *xup, *yup;
- {
- int lastx, lasty, mousedesc, dev;
- mousedesc = fileno(Id->In_ifp);
- dev = checkevent(mousedesc, STDIN, 1); /* blocking (hah!) */
- if (dev == STDIN)
- {
- *ch = readchar();
- return(((*ch < ' ') || (*ch == '\177')) ? CNTRL : ALPHA);
- }
- if (dev == mousedesc)
- {
- Iread(Id, &st, COORDINATES);
- cursorplace(st.x, st.y);
- lastx = st.x;
- lasty = st.y;
- if (st.button == 0) return(NOEVT);
- else
- {
- *xdown = st.x;
- *ydown = st.y;
- do {
- Iread(Id, &st, COORDINATES);
- *yup = st.y;
- *xup = st.x;
- if ((lastx != st.x) || (lasty != st.y)) cursorplace(st.x, st.y);
- lastx = st.x;
- lasty = st.y;
- } while (st.button != 0);
- }
- return(MOUSE);
- }
- return(NOEVT);
- }
-
- charshow(str)
- char *str;
- {
- fprintf(stdout, "%s", str);
- fflush(stdout);
- }
-
- charunshow(n)
- {
- int i;
- for (i=0; i<n; i++) fprintf(stdout, "%c %c", '\b', '\b');
- fflush(stdout);
- }
-
- drawvec(x0, y0, x1, y1, col, wid, emph)
- {
- drawline(x0, y0, x1, y1, wid, col, emph);
- }
-
- erase()
- {
- Ik_clear();
- }
-
- /*
- * internal routines
- */
-
- cursorenable()
- {
- long i;
- int ncolor[IK_MAP_SIZE];
- Ik_set_mode( SET_DURING_RETRACE );
- Ik_mask_set(0, 0xffffff);
- Ik_dmard(IK_WD_ADDR, IK_COLOUR_MAPS, IK_MAP_SIZE*0, ncolor, IK_MAP_SIZE);
- for (i=0; i<IK_MAP_SIZE; i++) ncolor[i] = (-1) ^ ncolor[i];
- Ik_dmawr(IK_WD_ADDR, IK_COLOUR_MAPS, IK_MAP_SIZE*1, ncolor, IK_MAP_SIZE);
- Ik_dmawr(IK_WD_ADDR, IK_COLOUR_MAPS, IK_MAP_SIZE*3, ncolor, IK_MAP_SIZE);
- Ik_diowr( IK_WD_ADDR, IK_XBAR_SWITCH, 32, 32); /* guarantee incursor bit */
- for (i=0; i<CURLEN; i++) cur[i] = 0; /* clear cursor */
- for (i=0; i<CURLEN/4; i++) /* draw open + sign */
- {
- SETCUR(i+CURLEN/8, CURLEN/2);
- SETCUR(7*CURLEN/8-i, CURLEN/2);
- SETCUR(CURLEN/2, i+CURLEN/8);
- SETCUR(CURLEN/2, 7*CURLEN/8-i);
- }
- Ik_curs_map(cur, CURLEN/2, CURLEN/2);
- cursorplace(0, 0);
- }
-
- cursordisable()
- {
- int i;
- for (i=0; i<CURLEN; i++) cur[i] = 0;
- Ik_curs_map(cur, 0, 0);
- Ik_diowr( IK_WD_ADDR, IK_FBC_REGS, FBC_MODE, HL(LCLK, 0x40));
- }
-
- cursorplace(x, y)
- {
- Ik_diowr(IK_WD_ADDR, IK_FBC_REGS, FBC_CURSOR,
- HL((511-y)+LWYOFF-CUROFFY-5, x-CUROFFX));
- Ik_diowr( IK_WD_ADDR, IK_FBC_REGS, FBC_MODE, HL(LCLK, 0x44));
- }
-
- /*
- * UNIX code to test for pending STDIN characters (in CBREAK mode)
- * and capture ^Z interruptions for the sake of CBREAK problems.
- */
-
- int stopcatch();
-
- stopcbreak()
- {
- struct sgttyb tty;
- ioctl(STDIN, TIOCGETP, &tty);
- tty.sg_flags &= ~CBREAK;
- tty.sg_flags |= ECHO;
- ioctl(STDIN, TIOCSETP, &tty);
- signal(SIGTSTP, SIG_DFL);
- }
-
- startcbreak()
- {
- struct sgttyb tty;
- ioctl(STDIN, TIOCGETP, &tty);
- tty.sg_flags |= CBREAK;
- tty.sg_flags &= ~ECHO;
- ioctl(STDIN, TIOCSETP, &tty);
- signal(SIGTSTP, stopcatch);
- }
-
- stopcatch()
- {
- /* ^Z just typed. */
- stopcbreak();
- sigsetmask (sigblock(0) & ~(1<<(SIGTSTP-1))); /* turn SIGTSTP on */
- kill(0, SIGTSTP); /* STOP THYSELF! */
- /* you just returned */
- startcbreak(); /* restore action for next time */
- }
-
- checkevent(l, r, blockflag)
- {
- extern int errno;
- int rdesc, retcode, bits;
- struct timeval tv;
- do {
- rdesc = (1<<l) | (1<<r);
- bits = (l > r) ? l+1 : r+1;
- bzero(&tv, sizeof tv); /* zero timer -> poll, no waiting */
- retcode = select(bits, &rdesc, 0, 0, blockflag ? 0 : &tv);
- } while ((retcode < 0) && (errno == EINTR));
- if (retcode <= 0) return(-1);
- return((rdesc == (1<<l)) ? l : r);
- }
-
- char readchar()
- {
- char ch;
- read(STDIN, &ch, 1);
- return(ch);
- }
-
- writescan(x, y, pixels, outaddr, color)
- int x, y, pixels, *outaddr, color;
- {
- static int IKbuf[512];
- int words, *bufbase;
-
- /* register */
- register int next, *wordbase, regfontcolor;
-
- if (pixels <= 0) return; /* PARANOIA: should not ever happen */
-
- regfontcolor = color; /* for a little speed */
- words = (pixels - 1) / 32 + 1;
- bufbase = IKbuf;
- Ik_dmard(IK_XY_ADDR, x, y, IKbuf, pixels);
-
- ++words;
- while (--words)
- {
- next = *outaddr;
- ++outaddr;
- wordbase = bufbase;
- while (next)
- {
- if (next < 0) *wordbase = regfontcolor;
- ++wordbase;
- next<<=1;
- }
- bufbase += 32;
- }
- Ik_dmawr(IK_XY_ADDR, x, y, IKbuf, pixels);
- }
-