home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / mouseptr.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  5KB  |  173 lines

  1. /* 2D pointer device (built-in) for Microsoft Mouse*/
  2.  
  3. // Pointers are an attempt to integrate device support across
  4. // REND386.  Why isn't anyone using them?
  5.  
  6. /* Original written by Dave Stampe, Aug. 1992 */
  7.  
  8.  
  9. /*
  10.  This code is part of the VR-386 project, created by Dave Stampe.
  11.  VR-386 is a desendent of REND386, created by Dave Stampe and
  12.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  13.  Stampre for VR-386.
  14.  
  15.  Copyright (c) 1994 by Dave Stampe:
  16.  May be freely used to write software for release into the public domain
  17.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  18.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  19.  this software or source code into their products!  Usually there is no
  20.  charge for under 50-100 items for low-cost or shareware products, and terms
  21.  are reasonable.  Any royalties are used for development, so equipment is
  22.  often acceptable payment.
  23.  
  24.  ATTRIBUTION:  If you use any part of this source code or the libraries
  25.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  26.  and any other authors in your documentation, source code, and at startup
  27.  of your program.  Let's keep the freeware ball rolling!
  28.  
  29.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  30.  REND386, improving programmer access by rewriting the code and supplying
  31.  a standard API.  If you write improvements, add new functions rather
  32.  than rewriting current functions.  This will make it possible to
  33.  include you improved code in the next API release.  YOU can help advance
  34.  VR-386.  Comments on the API are welcome.
  35.  
  36.  CONTACT: dstampe@psych.toronto.edu
  37. */
  38.  
  39.  
  40. #include <stdio.h>
  41. #include <dos.h>
  42. #include <bios.h>
  43.  
  44. #include "pointint.h"
  45. #include "vr_api.h"
  46.  
  47. /************** INTERNAL MOUSE SETUP ***********/
  48.  
  49. static PDRIVER *mouse_device;
  50.  
  51. static void mouseptr_quit()
  52. {
  53.   pointer_quit(mouse_device);
  54. }
  55.  
  56.  
  57. PDRIVER *mouseptr_init(char *mname)
  58. {
  59.  PDRIVER *p;
  60.  POINTER x;
  61.  
  62.  p = pointer_init(P_IS2DP, mname); /* setup mouse device */
  63.  if (p==NULL) return NULL;
  64.  
  65.  init_pointer(&x); /* so that defaults are OK */
  66.  pointer_tscale(p, 1000, 1000, 1000); /* scale mouse */
  67.  set_mouse_limits(p, screeninfo->xmax, screeninfo->ymax);
  68.  pointer_read(p, &x);
  69.  pointer_read(p, &x); /* save current position */
  70.  mouse_read(p, NULL, NULL, NULL);
  71.  
  72.  mouse_device = p;
  73.  atexit(mouseptr_quit);
  74.  
  75.  return p;
  76. }
  77.  
  78.  
  79.  
  80. /***************** MOUSE POINTER DRIVER *************/
  81.  
  82. pconfig mouse_pconfig = {
  83.     65536L, 65536L, 65536L, /* position res: mm/tick in <16.16>  */
  84.     320, 200, 200, -320, -200, -200, /* xyz ranges:                */
  85.     0, 0, 0, 0, 0, 0, 0, 0, 0, /* no rotation                       */
  86.     640, 480, /* mouse emulation limits (writable) */
  87.     P_HASB1 | P_HASB2 | P_HASX | P_HASY | P_HASZ | P_HASSCR, /* databits  */
  88.     P_CENTER | P_SCREEN, 0, 0, /* modes, nullkey, flexnum           */
  89.     1, 10, 60, /* delay, idelay, reads/sec          */
  90.     P_IS2DP | P_IS2D, /* uses  */
  91.     "Default Mouse Driver"
  92. };
  93.  
  94.  
  95. static mtype; /* if == P_IS2D, don't map Z with buttons */
  96. static x_acc, y_acc, z_acc, xs_acc, ys_acc;
  97.  
  98. pconfig *mouse_driver(int op, POINTER *p, int mode)
  99. {
  100.  union REGS r;
  101.  int type = FP_OFF(p); /* way to get integer arg. */
  102.  
  103.  switch(op)
  104.   {
  105.    case DRIVER_CMD:
  106.     if (!(type & P_CENTER)) break;
  107.     if (type & P_SCREEN)
  108.      {
  109.       xs_acc = mouse_pconfig.maxsx >> 1;
  110.       ys_acc = mouse_pconfig.maxsy >> 1;
  111.      }
  112.     if (type & P_POINTER)
  113.      {
  114.       x_acc = y_acc = z_acc = 0;
  115.      }
  116.     break;
  117.    case DRIVER_INIT:
  118.    case DRIVER_RESET:
  119.     r.x.ax = 0;
  120.     int86(0x33, &r, &r);
  121.     if (r.x.ax == 0) return NULL;
  122.     x_acc = y_acc = z_acc = 0;
  123.     xs_acc = mouse_pconfig.maxsx>>1;
  124.     ys_acc = mouse_pconfig.maxsy>>1;
  125.     break;
  126.  
  127.    case DRIVER_READ:/* pointer (2DP) read */
  128.     if (mode == P_POINTER)
  129.      {
  130.       r.x.ax = 3; /* read button status */
  131.       int86(0x33, &r, &r);
  132.       p->buttons = r.x.bx;
  133.       r.x.ax = 11; /* read motion counters */
  134.       int86(0x33, &r, &r);
  135.       x_acc += ((int) r.x.cx);
  136.       if (p->buttons & 0x02)
  137.        {
  138.     z_acc -= ((int) r.x.dx);
  139.     p->buttons &= 0x01;
  140.        }
  141.       else
  142.        y_acc -= ((int) r.x.dx);
  143.        p->x = x_acc;
  144.        p->y = y_acc;
  145.        p->z = z_acc;
  146.      }
  147.     else if (mode == P_SCREEN) /* mouse read (640x480) */
  148.      {
  149.       r.x.ax = 3; /* read button status */
  150.       int86(0x33, &r, &r);
  151.       p->buttons = r.x.bx;
  152.       r.x.ax = 11; /* read motion counters */
  153.       int86(0x33, &r, &r);
  154.       xs_acc += ((int) r.x.cx);
  155.       ys_acc += ((int) r.x.dx);
  156.       if (xs_acc < 0) xs_acc = 0;
  157.       if (ys_acc < 0) ys_acc = 0;
  158.       if (xs_acc > mouse_pconfig.maxsx) xs_acc = mouse_pconfig.maxsx;
  159.       if (ys_acc > mouse_pconfig.maxsy) ys_acc = mouse_pconfig.maxsy;
  160.       p->x = xs_acc;
  161.       p->y = ys_acc;
  162.      }
  163.     break;
  164.  
  165.    case DRIVER_CHECK:
  166.     break;
  167.    case DRIVER_QUIT:
  168.     break;
  169.   }
  170.  return &mouse_pconfig;
  171. }
  172.  
  173.