#include "tb.h"
#if NTB > 0
#include <sys/param.h>
#include <sys/tablet.h>
#include <sys/tty.h>
#if NeXT
#include <sys/proc.h>
#endif
struct tbconf {
short tbc_recsize;
short tbc_uiosize;
int tbc_sync;
int (*tbc_decode)();
char *tbc_run;
char *tbc_point;
char *tbc_stop;
char *tbc_start;
int tbc_flags;
#define TBF_POL 0x1
#define TBF_INPROX 0x2
};
static int tbdecode(), gtcodecode(), poldecode();
static int tblresdecode(), tbhresdecode();
struct tbconf tbconf[TBTYPE] = {
{ 0 },
{ 5, sizeof (struct tbpos), 0200, tbdecode, "6", "4" },
{ 5, sizeof (struct tbpos), 0200, tbdecode, "\1CN", "\1RT", "\2", "\4" },
{ 8, sizeof (struct gtcopos), 0200, gtcodecode },
{17, sizeof (struct polpos), 0200, poldecode, 0, 0, "\21", "\5\22\2\23",
TBF_POL },
{ 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CN", "\1PT", "\2", "\4",
TBF_INPROX },
{ 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CN", "\1PT", "\2", "\4",
TBF_INPROX },
{ 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CL\33", "\1PT\33", 0, 0},
{ 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CL\33", "\1PT\33", 0, 0},
};
struct tb {
int tbflags;
#define TBMAXREC 17
char cbuf[TBMAXREC];
union {
struct tbpos tbpos;
struct gtcopos gtcopos;
struct polpos polpos;
} rets;
#define NTBS 16
} tb[NTBS];
tbopen(dev, tp)
dev_t dev;
register struct tty *tp;
{
register struct tb *tbp;
if (tp->t_line == TABLDISC)
return (ENODEV);
ttywflush(tp);
for (tbp = tb; tbp < &tb[NTBS]; tbp++)
if (tbp->tbflags == 0)
break;
if (tbp >= &tb[NTBS])
return (EBUSY);
tbp->tbflags = TBTIGER|TBPOINT;
tp->t_cp = tbp->cbuf;
tp->t_inbuf = 0;
bzero((caddr_t)&tbp->rets, sizeof (tbp->rets));
tp->T_LINEP = (caddr_t)tbp;
tp->t_flags |= LITOUT;
return (0);
}
tbclose(tp)
register struct tty *tp;
{
register int s;
int modebits = TBPOINT|TBSTOP;
#ifndef NeXT
tbioctl(tp, BIOSMODE, &modebits, 0);
#else
tbioctl(tp, BIOSMODE, &modebits, 0, current_proc());
#endif
s = spltty();
((struct tb *)tp->T_LINEP)->tbflags = 0;
tp->t_cp = 0;
tp->t_inbuf = 0;
tp->t_rawq.c_cc = 0;
tp->t_canq.c_cc = 0;
tp->t_line = 0;
splx(s);
}
tbread(tp, uio)
register struct tty *tp;
struct uio *uio;
{
register struct tb *tbp = (struct tb *)tp->T_LINEP;
register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
int ret;
if ((tp->t_state&TS_CARR_ON) == 0)
return (EIO);
ret = uiomove(&tbp->rets, tc->tbc_uiosize, uio);
if (tc->tbc_flags&TBF_POL)
tbp->rets.polpos.p_key = ' ';
return (ret);
}
tbinput(c, tp)
register int c;
register struct tty *tp;
{
register struct tb *tbp = (struct tb *)tp->T_LINEP;
register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
if (tc->tbc_recsize == 0 || tc->tbc_decode == 0)
return;
if (c&tc->tbc_sync || tp->t_inbuf == tc->tbc_recsize) {
tp->t_cp = tbp->cbuf;
tp->t_inbuf = 0;
}
*tp->t_cp++ = c&0177;
if (++tp->t_inbuf == tc->tbc_recsize)
(*tc->tbc_decode)(tc, tbp->cbuf, &tbp->rets);
}
static
gtcodecode(tc, cp, tbpos)
struct tbconf *tc;
register char *cp;
register struct gtcopos *tbpos;
{
tbpos->pressure = *cp >> 2;
tbpos->status = (tbpos->pressure > 16) | TBINPROX;
tbpos->xpos = (*cp++ & 03) << 14;
tbpos->xpos |= *cp++ << 7;
tbpos->xpos |= *cp++;
tbpos->ypos = (*cp++ & 03) << 14;
tbpos->ypos |= *cp++ << 7;
tbpos->ypos |= *cp++;
tbpos->xtilt = *cp++;
tbpos->ytilt = *cp++;
tbpos->scount++;
}
static
tbdecode(tc, cp, tbpos)
struct tbconf *tc;
register char *cp;
register struct tbpos *tbpos;
{
register char byte;
byte = *cp++;
tbpos->status = (byte&0100) ? TBINPROX : 0;
byte &= ~0100;
if (byte > 036)
tbpos->status |= 1 << ((byte-040)/2);
tbpos->xpos = *cp++ << 7;
tbpos->xpos |= *cp++;
if (tbpos->xpos < 256)
tbpos->status &= ~TBINPROX;
tbpos->ypos = *cp++ << 7;
tbpos->ypos |= *cp++;
tbpos->scount++;
}
static
tblresdecode(tc, cp, tbpos)
struct tbconf *tc;
register char *cp;
register struct tbpos *tbpos;
{
*cp &= ~0100;
tbpos->status = (*cp++ >> 2) | TBINPROX;
if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
tbpos->status &= ~(020|TBINPROX);
tbpos->xpos = *cp++;
tbpos->xpos |= *cp++ << 6;
tbpos->ypos = *cp++;
tbpos->ypos |= *cp++ << 6;
tbpos->scount++;
}
static
tbhresdecode(tc, cp, tbpos)
struct tbconf *tc;
register char *cp;
register struct tbpos *tbpos;
{
char byte;
byte = *cp++;
tbpos->xpos = (byte & 03) << 14;
tbpos->xpos |= *cp++ << 7;
tbpos->xpos |= *cp++;
tbpos->ypos = *cp++ << 14;
tbpos->ypos |= *cp++ << 7;
tbpos->ypos |= *cp++;
tbpos->status = (byte >> 2) | TBINPROX;
if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
tbpos->status &= ~(020|TBINPROX);
tbpos->scount++;
}
static
poldecode(tc, cp, polpos)
struct tbconf *tc;
register char *cp;
register struct polpos *polpos;
{
polpos->p_x = cp[4] | cp[3]<<7 | (cp[9] & 0x03) << 14;
polpos->p_y = cp[6] | cp[5]<<7 | (cp[9] & 0x0c) << 12;
polpos->p_z = cp[8] | cp[7]<<7 | (cp[9] & 0x30) << 10;
polpos->p_azi = cp[11] | cp[10]<<7 | (cp[16] & 0x03) << 14;
polpos->p_pit = cp[13] | cp[12]<<7 | (cp[16] & 0x0c) << 12;
polpos->p_rol = cp[15] | cp[14]<<7 | (cp[16] & 0x30) << 10;
polpos->p_stat = cp[1] | cp[0]<<7;
if (cp[2] != ' ')
polpos->p_key = cp[2];
}
#ifndef NeXT
tbioctl(tp, cmd, data, flag)
struct tty *tp;
caddr_t data;
#else
tbtioctl(tp, cmd, data, flag, p)
struct tty *tp;
u_long cmd;
caddr_t data;
int flag;
struct proc *p;
#endif
{
register struct tb *tbp = (struct tb *)tp->T_LINEP;
switch (cmd) {
case BIOGMODE:
*(int *)data = tbp->tbflags & TBMODE;
break;
case BIOSTYPE:
if (tbconf[*(int *)data & TBTYPE].tbc_recsize == 0 ||
tbconf[*(int *)data & TBTYPE].tbc_decode == 0)
return (EINVAL);
tbp->tbflags &= ~TBTYPE;
tbp->tbflags |= *(int *)data & TBTYPE;
case BIOSMODE: {
register struct tbconf *tc;
tbp->tbflags &= ~TBMODE;
tbp->tbflags |= *(int *)data & TBMODE;
tc = &tbconf[tbp->tbflags & TBTYPE];
if (tbp->tbflags&TBSTOP) {
if (tc->tbc_stop)
ttyout(tc->tbc_stop, tp);
} else if (tc->tbc_start)
ttyout(tc->tbc_start, tp);
if (tbp->tbflags&TBPOINT) {
if (tc->tbc_point)
ttyout(tc->tbc_point, tp);
} else if (tc->tbc_run)
ttyout(tc->tbc_run, tp);
ttstart(tp);
break;
}
case BIOGTYPE:
*(int *)data = tbp->tbflags & TBTYPE;
break;
case TIOCSETD:
case TIOCGETD:
case TIOCGETP:
case TIOCGETC:
return (-1);
default:
return (ENOTTY);
}
return (0);
}
#endif