home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MOUSE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  9KB  |  360 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** A series of routines to provide access to MicroSoft (and compatible)
  5. ** mice.  Consult your mouse documentation for detailed information regarding
  6. ** each mouse driver function.
  7. **
  8. ** by Bob Jarvis w/ modifications by Bob Stout & Bruce Wedding
  9. */
  10.  
  11. #include <dos.h>
  12. #include "mouse.h"
  13.  
  14. int mouse_present = 0;  /* globally visible */
  15.  
  16. #define DOS_INT 0x21
  17.  
  18. #define MOUSE(workregs) int86(MSMOUSE,&workregs,&workregs)
  19. #define MOUSEX(workregs,sregs) int86x(DOS_INT,&workregs,&workregs,&sregs)
  20.  
  21. /*
  22. ** Uses driver function 0 to initialize the mouse software to its default
  23. ** settings.  If no mouse is present it returns 0.  If a mouse is present, it
  24. ** returns -1, and places the value of the mouse type (2 = MicroSoft,
  25. ** 3 = Mouse Systems, other values are possible) in *mousetype.  Also
  26. ** initializes the global variable mouse_present (0 = no mouse, !0 = mouse
  27. ** is available).
  28. */
  29.  
  30. int ms_reset(int *mousetype)
  31. {
  32.       union REGS workregs;
  33.       struct SREGS sregs;
  34.  
  35.       /* check the vector     */
  36.  
  37.       segread (&sregs);
  38.       workregs.h.ah = 0x35;     /* DOS get vector */
  39.       workregs.h.al = 0x33;     /* mouse vector   */
  40.       intdosx(&workregs, &workregs, &sregs);
  41.  
  42.       /* ES:BX now contains the pointer to the interrupt handler */
  43.  
  44.       if (sregs.es == 0 && workregs.x.bx == 0)
  45.             return mouse_present = 0;
  46.  
  47.       workregs.x.ax = 0;
  48.       MOUSE(workregs);
  49.       *mousetype = workregs.x.bx;
  50.       mouse_present = workregs.x.ax;
  51.       return(mouse_present);
  52. }
  53.  
  54. /*
  55. ** Makes the mouse cursor visible.
  56. */
  57.  
  58. void ms_show_cursor(void)
  59. {
  60.       union REGS workregs;
  61.  
  62.       workregs.x.ax = 1;
  63.       MOUSE(workregs);
  64. }
  65.  
  66. /*
  67. ** Hides the mouse cursor.  Should be called before changing any portion of
  68. ** the screen under the mouse cursor.
  69. */
  70.  
  71. void ms_hide_cursor(void)
  72. {
  73.       union REGS workregs;
  74.  
  75.       workregs.x.ax = 2;
  76.       MOUSE(workregs);
  77. }
  78.  
  79. /*
  80. ** Obtains information about the mouse position and button status.
  81. ** Places the current horizontal and vertical positions in *horizpos and
  82. ** *vertpos, respectively.  Returns the mouse button status, which is
  83. ** mapped at the bit level as follows:
  84. **    Bit 0 - left button    \
  85. **    Bit 1 - right button    >-- 0 = button up, 1 = button down
  86. **    Bit 2 - middle button  /
  87. */
  88.  
  89. int ms_get_mouse_pos(int *horizpos, int *vertpos) /* Returns button status */
  90. {
  91.       union REGS workregs;
  92.  
  93.       workregs.x.ax = 3;
  94.       MOUSE(workregs);
  95.       *horizpos = workregs.x.cx;
  96.       *vertpos  = workregs.x.dx;
  97.       return(workregs.x.bx);
  98. }
  99.  
  100. /*
  101. ** Moves the mouse cursor to a new position.
  102. */
  103.  
  104. void ms_set_mouse_pos(int horizpos, int vertpos)
  105. {
  106.       union REGS workregs;
  107.  
  108.       workregs.x.ax = 4;
  109.       workregs.x.cx = horizpos;
  110.       workregs.x.dx = vertpos;
  111.       MOUSE(workregs);
  112. }
  113.  
  114. /*
  115. ** Obtains information about the last time the specified button
  116. ** (0 = left, 1 = right, 2 = middle) was pressed.  Returns the current
  117. ** button status (same format as return from ms_get_mouse_pos() above).
  118. */
  119.  
  120. int ms_button_press_status(int  button,
  121.                            int *press_count,
  122.                            int *column,
  123.                            int *row)
  124. {
  125.       union REGS workregs;
  126.  
  127.       workregs.x.ax = 5;
  128.       workregs.x.bx = button;
  129.       MOUSE(workregs);
  130.       *press_count = workregs.x.bx;
  131.       *column = workregs.x.cx;
  132.       *row = workregs.x.dx;
  133.       return(workregs.x.ax);
  134. }
  135.  
  136. /*
  137. ** Similar to above but obtains information about the last release of the
  138. ** specified button.
  139. */
  140.  
  141. int ms_button_release_status(int  button,
  142.                              int *release_count,
  143.                              int *column,
  144.                              int *row)
  145. {
  146.       union REGS workregs;
  147.  
  148.       workregs.x.ax = 6;
  149.       workregs.x.bx = button;
  150.       MOUSE(workregs);
  151.       *release_count = workregs.x.bx;
  152.       *column = workregs.x.cx;
  153.       *row = workregs.x.dx;
  154.       return(workregs.x.ax);
  155. }
  156.  
  157. /*
  158. ** Forces the mouse cursor to remain within the range specified.
  159. */
  160.  
  161. void ms_restrict_horiz(int min, int max)
  162. {
  163.       union REGS workregs;
  164.  
  165.       workregs.x.ax = 7;
  166.       workregs.x.cx = min;
  167.       workregs.x.dx = max;
  168.       MOUSE(workregs);
  169. }
  170.  
  171. /*
  172. ** Forces the mouse cursor to remain within the range specified.
  173. */
  174.  
  175. void ms_restrict_vert(int min, int max)
  176. {
  177.       union REGS workregs;
  178.  
  179.       workregs.x.ax = 8;
  180.       workregs.x.cx = min;
  181.       workregs.x.dx = max;
  182.       MOUSE(workregs);
  183. }
  184.  
  185. void ms_define_window(int left, int top, int right, int bottom)
  186. {
  187.       ms_restrict_horiz(left,right);
  188.       ms_restrict_vert(top,bottom);
  189. }
  190.  
  191. /*
  192. ** Allows the user to set the graphics cursor to a new shape.  Check your
  193. ** mouse reference manual for full information about the use of this function.
  194. */
  195.  
  196. void ms_set_graphics_cursor(int      horiz_hotspot,
  197.                             int      vert_hotspot,
  198.                             unsigned seg_shape_tables,
  199.                             unsigned offset_shape_tables)
  200. {
  201.       union REGS workregs;
  202.       struct SREGS segregs;
  203.  
  204.       workregs.x.ax = 9;
  205.       workregs.x.bx = horiz_hotspot;
  206.       workregs.x.cx = vert_hotspot;
  207.       workregs.x.dx = offset_shape_tables;
  208.       segregs.es  = seg_shape_tables;
  209.       MOUSEX(workregs, segregs);
  210. }
  211.  
  212. /*
  213. ** Selects either the software or hardware cursor and sets the start and stop
  214. ** scan lines (for the hardware cursor) or the screen and cursor masks (for
  215. ** the software cursor).  Consult your mouse reference for more information.
  216. */
  217.  
  218. void ms_set_text_cursor(int type, int screen_mask, int cursor_mask)
  219. {
  220.       union REGS workregs;
  221.  
  222.       workregs.x.ax = 10;
  223.       workregs.x.bx = type;
  224.       workregs.x.cx = screen_mask;
  225.       workregs.x.dx = cursor_mask;
  226.       MOUSE(workregs);
  227. }
  228.  
  229. /*
  230. ** Obtains the horizontal and vertical raw motion counts since the last
  231. ** request.
  232. */
  233.  
  234. void ms_read_motion_counters(int *horiz, int *vert)
  235. {
  236.       union REGS workregs;
  237.  
  238.       workregs.x.ax = 11;
  239.       MOUSE(workregs);
  240.       *horiz = workregs.x.cx;
  241.       *vert  = workregs.x.dx;
  242. }
  243.  
  244. /*
  245. ** Sets up a subroutine to be called when a given event occurs.
  246. ** NOTE: Use with extreme care.  The function whose address is provided MUST
  247. **    terminate with a far return (i.e. must be compiled using large model).
  248. **    Also, no DOS or BIOS services may be used, as the user-defined function
  249. **    is (in effect) an extension to an interrupt service routine.
  250. */
  251.  
  252. void ms_set_event_subroutine(int      mask,
  253.                              unsigned seg_routine,
  254.                              unsigned offset_routine)
  255. {
  256.       union REGS workregs;
  257.       struct SREGS segregs;
  258.  
  259.       workregs.x.ax = 12;
  260.       workregs.x.cx = mask;
  261.       workregs.x.dx = offset_routine;
  262.       segregs.es  = seg_routine;
  263.       MOUSEX(workregs, segregs);
  264. }
  265.  
  266. /*
  267. ** Turns light pen emulation mode on.
  268. */
  269.  
  270. void ms_light_pen_on(void)
  271. {
  272.       union REGS workregs;
  273.  
  274.       workregs.x.ax = 13;
  275.       MOUSE(workregs);
  276. }
  277.  
  278. /*
  279. ** turns light pen emulation mode off.
  280. */
  281.  
  282. void ms_light_pen_off(void)
  283. {
  284.       union REGS workregs;
  285.  
  286.       workregs.x.ax = 14;
  287.       MOUSE(workregs);
  288. }
  289.  
  290. /*
  291. ** Sets the sensitivity of the mouse.  Defaults are 8 and 16 for horizontal
  292. ** and vertical sensitivity (respectively).
  293. */
  294.  
  295. void ms_set_sensitivity(int horiz, int vert)
  296. {
  297.       union REGS workregs;
  298.  
  299.       workregs.x.ax = 15;
  300.       workregs.x.cx = horiz;
  301.       workregs.x.dx = vert;
  302.       MOUSE(workregs);
  303. }
  304.  
  305. /*
  306. ** Sets up a region of the screen inside of which the mouse cursor will
  307. ** automatically be 'hidden'.
  308. */
  309.  
  310. void ms_protect_area(int left, int top, int right, int bottom)
  311. {
  312.       union REGS workregs;
  313.  
  314.       workregs.x.ax = 16;
  315.       workregs.x.cx = left;
  316.       workregs.x.dx = top;
  317.       workregs.x.si = right;
  318.       workregs.x.di = bottom;
  319.       MOUSE(workregs);
  320. }
  321.  
  322. /*
  323. * Similar to ms_set_graphics_cursor() but allows a larger cursor.  Consult
  324. ** your mouse documentation for information on how to use this function.
  325. */
  326.  
  327. int ms_set_large_graphics_cursor(int      width,
  328.                                  int      height,
  329.                                  int      horiz_hotspot,
  330.                                  int      vert_hotspot,
  331.                                  unsigned seg_shape_tables,
  332.                                  unsigned offset_shape_tables)
  333. {
  334.       union REGS workregs;
  335.       struct SREGS segregs;
  336.  
  337.       workregs.x.ax = 18;
  338.       workregs.x.bx = (width << 8) + horiz_hotspot;
  339.       workregs.x.cx = (height << 8) + vert_hotspot;
  340.       workregs.x.dx = offset_shape_tables;
  341.       segregs.es  = seg_shape_tables;
  342.       MOUSEX(workregs, segregs);
  343.       if(workregs.x.ax == (unsigned)-1)
  344.             return(workregs.x.ax); /* Return -1 if function 18 supported */
  345.       else  return(0);             /* else return 0                      */
  346. }
  347.  
  348. /*
  349. ** Sets the threshold value for doubling cursor motion.  Default value is 64.
  350. */
  351.  
  352. void ms_set_doublespeed_threshold(int speed)
  353. {
  354.       union REGS workregs;
  355.  
  356.       workregs.x.ax = 19;
  357.       workregs.x.dx = speed;
  358.       MOUSE(workregs);
  359. }
  360.