home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / PC / REND386 / DEVEL5 / POINTER.C < prev    next >
C/C++ Source or Header  |  1992-09-21  |  8KB  |  310 lines

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