home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / devel5 / pointer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-10  |  8.9 KB  |  336 lines

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