home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / PC / REND386 / JIREND / POINTER.C < prev    next >
C/C++ Source or Header  |  1993-04-11  |  9KB  |  332 lines

  1. /* Pointer device driver interface routines */
  2. /* Written by Dave Stampe, Aug. 1992 */
  3. // modified April 93 for Logitech tracker
  4. //#define LOGITECH_AVAIL
  5.   
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <alloc.h>
  9. #include <string.h>
  10. #include "rend386.h"  /* for MATRIX definition */
  11. #include "pointer.h"
  12. #include "eprint.h"
  13.   
  14. extern long scale_16(long s, long a, long x); /* used to do scaling */
  15.                                               /* (x+a)*s            */
  16.   
  17. extern long calc_scale_16(long a, long b, long s); /* computes scaling factor */
  18.                                                    /* (a+b)/2/s          */
  19.   
  20. // NOTE: Basic prototype for a Driver function is:
  21. //    pconfig *device_driver(int op, POINTER *p, int mode)
  22. //  however, we cant use prototypes because functions dont always
  23. //  pass the MODE parameter! This should be fixed!
  24. extern pconfig *mouse_driver();
  25. extern pconfig *glove_driver();
  26. extern void *load_driver(char *m);
  27.   
  28. #ifdef LOGITECH_AVAIL
  29. extern pconfig *logitech_driver();
  30. int logitech_port = 0;  /* used to pass logitech port */
  31. #endif // LOGITECH_AVAIL
  32.   
  33.   
  34. PDRIVER *pointer_init(int type, char *drfile)
  35. {
  36.    pconfig *(*pd)();
  37.    pdrblock *p;
  38.    pconfig *d;
  39.    eprintf("pointer init: %d %s\n", type, drfile);
  40.   
  41.    if (drfile == NULL) return NULL;
  42.   
  43.    if (!stricmp(drfile, "mouse"))
  44.    {
  45.       p = malloc(sizeof(pdrblock));
  46.       if (p == NULL) return NULL;
  47.       d = mouse_driver(DRIVER_INIT, type);
  48.       p->driver_pointer = mouse_driver;
  49.    }
  50.    else if (!stricmp(drfile, "pglove"))
  51.    {
  52.       p = malloc(sizeof(pdrblock));
  53.       if (p == NULL) return NULL;
  54.       d = glove_driver(DRIVER_INIT, type);
  55.       p->driver_pointer = glove_driver;
  56.    }
  57.    #ifdef LOGITECH_AVAIL
  58.    else if (!strnicmp(drfile, "logitech", 8))
  59.    {
  60.       eprintf("logitech Head Tracker Requested\n");
  61.   
  62.          /* select com2 based on trailing char. */
  63.       logitech_port = (drfile[8]=='2') ? 2 : 1;
  64.       p = malloc(sizeof(pdrblock));
  65.       if (p == NULL) return NULL;
  66.       d = logitech_driver(DRIVER_INIT, type);
  67.       p->driver_pointer = logitech_driver;
  68.    }
  69.    #endif // LOGITECH_AVAIL
  70.   
  71.    else
  72.    {
  73.       pd = load_driver(drfile);
  74.       if (pd == NULL) return NULL;
  75.       p = malloc(sizeof(pdrblock));
  76.       if (p == NULL) return NULL;
  77.       p->driver_pointer = pd =
  78.       MK_FP(FP_SEG(pd), 16+FP_OFF(pd)); /* entry point */
  79.       d = pd(DRIVER_INIT, type);
  80.       if (d == NULL) return NULL;
  81.    }
  82.   
  83.    p->pdata = d;
  84.    p->xmult = p->ymult = p->zmult = 65536L;
  85.    p->xadd = p->yadd = p->zadd = 0;
  86.    p->xrmult = p->yrmult = p->zrmult = 65536L;
  87.    p->xradd = p->yradd = p->zradd = 0;
  88.    return p;
  89. }
  90.   
  91. /* makes sure device is available, OK */
  92. /* returns *pconfig or NULL           */
  93.   
  94. pconfig *pointer_check(PDRIVER *p)
  95. {
  96.    if (p)
  97.       return (((pdrblock *)p)->driver_pointer) (DRIVER_CHECK);
  98.    else
  99.       return NULL;
  100. }
  101.   
  102. /* recenters, recalibrates, etc */
  103. void pointer_reset(PDRIVER *p)
  104. {
  105.    if (p == NULL) return;
  106.    (((pdrblock *)p)->driver_pointer) (DRIVER_RESET);
  107. }
  108.   
  109. /* reads pointer, scales data and        */
  110. /* returns bitwise OR of the following:  */
  111. int pointer_read(PDRIVER *p, POINTER *pt)
  112. {
  113.    POINTER *op = &((pdrblock *)p)->oldp;
  114.    pdrblock *pd = (pdrblock *)p;
  115.    int has = pd->pdata->databits;
  116.    int changed = 0;
  117.    int sx, sy;
  118.   
  119.    if (p == NULL || pt == NULL) return 0;
  120.    if ((p->driver_pointer) (DRIVER_READ, pt, P_POINTER) == NULL)
  121.    {
  122.       memcpy( pt, op, sizeof(POINTER) ); /* handle no new data */
  123.       pt->wpos_valid = 0;
  124.       pt->changed = 0;
  125.       op->changed = 0;
  126.       return 0;
  127.    }
  128.   
  129.    if (has & P_HASX)
  130.    {
  131.       pt->x = scale_16(pd->xmult, pd->xadd, pt->x);
  132.       if ((pt->dx = pt->x - op->x) != 0) changed |= PNEW_POS;
  133.    }
  134.    else pt->x = 0;
  135.    if (has & P_HASY)
  136.    {
  137.       pt->y = scale_16(pd->ymult, pd->yadd, pt->y);
  138.       if ((pt->dy = pt->y - op->y) != 0) changed |= PNEW_POS;
  139.    }
  140.    else pt->y = 0;
  141.    if (has & P_HASZ)
  142.    {
  143.       pt->z = scale_16(pd->zmult, pd->zadd, pt->z);
  144.       if ((pt->dz = pt->z - op->z) != 0) changed |= PNEW_POS;
  145.    }
  146.    else pt->z = 0;
  147.   
  148.    if (has & P_HASRX)
  149.    {
  150.       pt->rx = scale_16(pd->xrmult, pd->xradd, pt->rx);
  151.       if ((pt->drx = pt->rx - op->rx) != 0) changed |= PNEW_ROT;
  152.    }
  153.    else pt->rx = 0;
  154.    if (has & P_HASRY)
  155.    {
  156.       pt->ry = scale_16(pd->yrmult, pd->yradd, pt->ry);
  157.       if ((pt->dry = pt->ry - op->ry) != 0) changed |= PNEW_ROT;
  158.    }
  159.    else pt->ry = 0;
  160.    if (has & P_HASRZ)
  161.    {
  162.       pt->rz = scale_16(pd->zrmult, pd->zradd, pt->rz);
  163.       if ((pt->drz = pt->rz - op->rz) != 0) changed |= PNEW_ROT;
  164.    }
  165.    else pt->rz = 0;
  166.   
  167.    pt->buttons &= has&0x0007 ;
  168.    if ((has&0x0007) && pt->buttons != op->buttons) changed |= PNEW_BUT;
  169.   
  170.    if ((has&P_HASGEST) && pt->gesture != op->gesture) changed |= PNEW_GEST;
  171.   
  172.    if ((has&P_HASKEYS) && pt->keys != op->keys) changed |= PNEW_KEY;
  173.   
  174.    if (has & P_HASFLEX)
  175.    {
  176.       int i;
  177.       int n = pd->pdata->flexnum;
  178.   
  179.       for (i = 0; i < n; i++)
  180.          if (pt->flex[i] != op->flex[i])
  181.          {
  182.             changed |= PNEW_FLEX;
  183.             break;
  184.          }
  185.    }
  186.    if (changed)
  187.    {
  188.       memcpy(op, pt, sizeof(POINTER));
  189.       pt->wpos_valid = 0;
  190.    }
  191.    pt->changed = changed;
  192.    op->changed = changed;
  193.    return changed;
  194. }
  195.   
  196.   
  197. int mouse_read(PDRIVER *p, int *xp, int *yp, unsigned *bp)
  198. {
  199.    POINTER pt;
  200.    POINTER *opt = &(p->oldp);
  201.    int c = 0;
  202.   
  203.    if (p == NULL) return 0;
  204.    if (!(p->pdata->databits & P_HASSCR)) return 0;
  205.   
  206.    (p->driver_pointer) (DRIVER_READ, &pt, P_SCREEN);
  207.   
  208.    if (p->oldsx != pt.x || p->oldsy != pt.y)
  209.       c |= PNEW_POS;
  210.    if (opt->buttons != pt.buttons)
  211.       c |= PNEW_BUT;
  212.   
  213.    p->oldsx = pt.x;
  214.    p->oldsy = pt.y;
  215.    opt->buttons = pt.buttons ;
  216.   
  217.    if (xp) *xp = pt.x;
  218.    if (yp) *yp = pt.y;
  219.    if (bp) *bp = pt.buttons;
  220.   
  221.    opt->changed = c;
  222.   
  223.    return c;
  224. }
  225.   
  226.   
  227. int mouse_last(PDRIVER *p, int *xp, int *yp, int *bp)
  228. {
  229.    if (p == NULL) return 0;
  230.   
  231.    if (xp) *xp = p->oldsx;
  232.    if (yp) *yp = p->oldsy;
  233.    if (bp) *bp = (&(p->oldp))->buttons;
  234.    return (&(p->oldp))->changed;
  235. }
  236.   
  237. void set_mouse_limits(PDRIVER *p, int maxx, int maxy)
  238. {
  239.    if (p == NULL) return; /* NOTE: MODIFIES DRIVER */
  240.    p->pdata->maxsx = maxx;
  241.    p->pdata->maxsy = maxy;
  242.    (p->driver_pointer) (DRIVER_CMD, P_SCREEN | P_CENTER);
  243.    mouse_read(p, NULL, NULL, NULL);
  244. }
  245.   
  246. /* disconnects driver */
  247. void pointer_quit(PDRIVER *p)
  248. {
  249.    if (p == NULL) return;
  250.    (((pdrblock *)p)->driver_pointer) (DRIVER_QUIT);
  251. }
  252.   
  253. /* changes device mode */
  254. pconfig *device_command(PDRIVER *p, int command)
  255. {
  256.    pconfig *pc;
  257.   
  258.    if (p == NULL) return NULL;
  259.    pc = (((pdrblock *)p)->driver_pointer) (DRIVER_CMD, command);
  260.    p->pdata = pc;
  261.    return pc;
  262. }
  263.   
  264. /* sets scaling (+/- given value, centered at 0) */
  265.   
  266. void pointer_tscale(PDRIVER *p, long x, long y, long z)
  267. {
  268.    long mx = p->pdata->maxx;
  269.    long mn = p->pdata->minx;
  270.    p->xmult = calc_scale_16(mx, mn, x);
  271.    p->xadd = (mn-mx)/2;
  272.   
  273.    mx = p->pdata->maxy;
  274.    mn = p->pdata->miny;
  275.    p->ymult = calc_scale_16(mx, mn, y);
  276.    p->yadd = (mn-mx)/2;
  277.   
  278.    if (p->pdata->type != P_IS2DP) /* use y scaling for pseudo-z axis */
  279.    {
  280.       mx = p->pdata->maxz;
  281.       mn = p->pdata->minz;
  282.    }
  283.    p->zmult = calc_scale_16(mx, mn, z);
  284.    p->zadd = (mn-mx)/2;
  285. }
  286.   
  287.   
  288. void pointer_rscale(PDRIVER *p, long rx, long ry, long rz)
  289. {
  290.    long mx = p->pdata->maxxr;
  291.    long mn = p->pdata->minxr;
  292.    p->xrmult = calc_scale_16(mx, mn, rx);
  293.    p->xradd = (mn-mx)/2;
  294.   
  295.    mx = p->pdata->maxyr;
  296.    mn = p->pdata->minyr;
  297.    p->yrmult = calc_scale_16(mx, mn, ry);
  298.    p->yradd = (mn-mx)/2;
  299.   
  300.    mx = p->pdata->maxzr;
  301.    mn = p->pdata->minzr;
  302.    p->zrmult = calc_scale_16(mx, mn, rz);
  303.    p->zradd = (mn-mx)/2;
  304. }
  305.   
  306. void pointer_abscale(PDRIVER *p, long xs, long ys, long zs,
  307. long xrs, long yrs, long zrs)
  308. {
  309.    p->xmult = scale_16(xs, 0, p->pdata->xres); /* set up to scale tick resolution */
  310.    p->ymult = scale_16(ys, 0, p->pdata->yres);
  311.    p->zmult = scale_16(zs, 0, p->pdata->zres);
  312.    p->xrmult = scale_16(xrs, 0, p->pdata->xrres); /* some weirdness with rot. scaling */
  313.    p->yrmult = scale_16(yrs, 0, p->pdata->yrres); /* as tick res. not really expressible */
  314.    p->zrmult = scale_16(zrs, 0, p->pdata->zrres); /* in <16.16>: so set tickres>>16 or so */
  315.    p->xadd = p->yadd = p->zadd = 0; /* look at PG y-rot for example */
  316.    p->xradd = p->yradd = p->zradd = 0;
  317. }
  318.   
  319.   
  320. void init_pointer(POINTER *p) /* initialize pointer structure */
  321. {
  322.    memset( p, 0, sizeof(POINTER));
  323.    p->wpos[0][0] = p->wpos[1][1] = p->wpos[2][2] = 536870912L;
  324. }
  325.   
  326. int last_pointer(PDRIVER *d, POINTER *p) /* copy of last read value */
  327. {
  328.    memcpy(p, &(d->oldp), sizeof(POINTER));
  329.    return(p->changed);
  330. }
  331.   
  332.