home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / mouse03g.zip / MOUSE1G.C < prev    next >
C/C++ Source or Header  |  1995-10-18  |  8KB  |  205 lines

  1. /* This code is designed to be inserted into a TURBO-C source. */
  2. /* Variable __TURBOC__ is defined by the turbo-c compiler. */
  3. /* Usage: Call mousereset(). Call mousehook(). Mouse setup done!
  4.  
  5.           If you enter a menu, where mouse action should be slower,
  6.           then call menumouse(1). On menu exit call menumouse(0).
  7.  
  8.           Upon program exit call mousereset(). This resets the
  9.           mouse driver (either a device or tsr from mouse manufacturer)
  10.           interrupt service routine [warning: REQUIRED!].
  11. */
  12. #ifdef __TURBOC__
  13. #pragma inline
  14. /*
  15.  * MOUSE SENSITIVITY. A curious feature in mouse code is sensitivity to
  16.  * movement: settling down on a character is subject to much error. In the
  17.  * code below this problem is attacked.
  18.  *
  19.  *     Ideally, the cursor should lock onto a line and not be bumped off
  20.  *     easily as the cursor scans left and right. Further, up and down
  21.  *     motion should lock the cursor onto a column. I should like to scan
  22.  *     up and down columns of numbers with the mouse exactly as I do with
  23.  *     the cursor keys. The code below is an attempt to realize these
  24.  *     features that falls short of perfection.
  25.  *
  26.  * MOUSE DRIVER DEFAULTS. The TSR resets the mouse driver and but sets no
  27.  * defaults for the mouse controls: X,Y sensitivity, doubler.
  28.  */
  29.  
  30. #define leftkey                 4b00h
  31. #define rightkey                4d00h
  32. #define upkey                   4800h
  33. #define downkey                 5000h
  34. #define retkey                  1c0dh
  35. #define esckey                  011bh
  36.  
  37. #define mouse                   51         /* interrupt 33h */
  38. #define resetmousedriver         0         /* reset mouse driver */
  39. #define pressmouse               5         /* mouse button press status */
  40. #define mousemotion             11         /* mouse cursor motion */
  41. #define seteventhandler         12         /* set mask and event handler */
  42. #define eventmask               0000000000001011b
  43. /* evenmask bits 0,1,4: movement, left & right press */
  44.  
  45. /*
  46.  Mouse driver resets to 8 and 16 respectively with function 00h.
  47.  Znix and Merit both worked well with settings 8/16.
  48. */
  49. int hsens=8,vsens=16;
  50. static int hpos=0;      /* store mouse cursor position X */
  51. static int vpos=0;      /* store mouse cursor position Y */
  52.  
  53. static
  54. stuffbuffer(){
  55. asm     cli                     /* Prevent interrupts from stuffing buffer */
  56. asm     push es                 /* typeahead buffer start at *[0040:0080] */
  57. asm     push bx                 /* and end at *[0040:0082]. */
  58.                                 /* At segment 40h and offset 01ah is */
  59. asm     mov di,40h              /* the offset into the typeahead buffer */
  60. asm     mov es,di               /* for the character at the head. At */
  61. asm     mov bx,es:[1ch]         /* segment 40h and offset 01ch is the */
  62. asm     mov si,bx               /* offset into the typeahead buffer for */
  63. asm     add bx,2                /* the last character. The buffer is full */
  64. asm     cmp bx,es:[1ah]         /* if *[01ch]+2 == *[01ah]. */
  65. asm     je quit                 /* Full? Then don't add any more chars */
  66. asm     cmp bx,es:[82h]         /* Offset less than end of buffer? */
  67. asm     jl isroom               /* Yes. Then go ahead and stuff it. */
  68. asm     mov bx,es:[80h]         /* Else wrap around to buffer start */
  69. isroom:
  70. asm     mov es:[si],cx          /* CX=scan code to be stuffed */
  71. asm     mov es:[1ch],bx         /* BX=new offset to last buffer character */
  72. quit:
  73. asm     pop bx
  74. asm     pop es
  75. asm     sti                     /* let other routines stuff the buffer */
  76. }
  77.  
  78. static
  79. manystuffbuffer(){
  80. /* AX=amount in mickeys mouse cursor has moved. */
  81. /* BX=sensitivity in mickeys */
  82. /* CX=key scan code to stuff into keyboard buffer */
  83. manyloop:
  84. asm     call stuffbuffer
  85. asm     sub ax,bx
  86. asm     cmp ax,bx
  87. asm     jge manyloop
  88. }
  89.  
  90. static
  91. far mouseeventhandler() {
  92. asm     push ax
  93. asm     push bx
  94. asm     push cx
  95. asm     push dx
  96. asm     push ds                 /* ds=mouse driver data segment */
  97. asm     mov ax, SEG _DATA       /* Get data segment for our data */
  98. asm     mov ds,ax               /* Set data segment */
  99. /*
  100.  Press Mouse supplies: BX=button status, CX=X mickeys, DX=Y mickeys
  101.                        The signed integers CX, DX measure UP==+, RIGHT==+
  102.                        Clears all button counts on each call.
  103. */
  104. asm     mov ax,pressmouse       /* get status of button press */
  105. asm     mov bx,0                /* for left button */
  106. asm     int mouse
  107. asm     and ax,1                /* Press left button? */
  108. asm     jz check_rightbutton    /* No? Then check right button. */
  109. asm     mov cx,retkey           /* Yes. Then plug in RETURN key. */
  110. asm     call stuffbuffer        /* near function call */
  111. check_rightbutton:
  112. asm     mov ax,pressmouse       /* get status of button press */
  113. asm     mov bx,2                /* for right button */
  114. asm     int mouse
  115. asm     and ax,2                /* press right button? */
  116. asm     jz checkcursor          /* No? Then check cursor keys. */
  117. asm     mov cx,esckey           /* Yes. Then plug in ESC key. */
  118. asm     call stuffbuffer        /* near function call */
  119. checkcursor:
  120. asm     mov ax,mousemotion      /* Has the mouse cursor moved? */
  121. asm     int mouse
  122. asm     mov ax,ds:hpos          /* save new horizontal position */
  123. asm     add ax,cx               /* cx=X coord supplied by mouse driver */
  124. asm     mov ds:hpos,ax
  125. asm     cmp ax,0                /* Has the mouse cursor moved? */
  126. asm     je  motionvertical      /* No change, then check vertical */
  127. asm     jg motionhorizontal     /* Make positive */
  128. asm     not ax
  129. motionhorizontal:
  130. asm     mov bx,ds:hsens
  131. asm     cmp ax,bx
  132. asm     jl motionvertical       /* didn't move enough for a change */
  133. asm     cmp ds:hpos,0
  134. asm     mov cx,rightkey
  135. asm     jg  stuffit1            /* If positive, then stuff cursor Right */
  136. asm     mov cx,leftkey
  137. stuffit1:
  138. asm     jmp stuffit2
  139. motionvertical:
  140. asm     mov ax,ds:vpos          /* save new vertical position */
  141. asm     add ax,dx               /* dx=Y coord supplied by mouse driver */
  142. asm     mov ds:vpos,ax
  143. asm     cmp ax,0
  144. asm     je eventreturn          /* no change, all done */
  145. asm     jg testvertsens         /* If positive, then stuff cursor Up */
  146. asm     not ax
  147. testvertsens:
  148. asm     mov bx,ds:vsens
  149. asm     cmp ax,bx
  150. asm     jl eventreturn          /* didn't move enough for a change */
  151. asm     cmp ds:vpos,0
  152. asm     mov cx,downkey
  153. asm     jg stuffit2
  154. asm     mov cx,upkey
  155. stuffit2:
  156. asm     call manystuffbuffer    /* stuff key CX into keyboard buffer */
  157. asm     mov ds:vpos,0           /* reset vertical saved position */
  158. asm     mov ds:hpos,0           /* reset horizontal saved position */
  159. eventreturn:
  160. asm     pop ds
  161. asm     pop dx
  162. asm     pop cx
  163. asm     pop bx
  164. asm     pop ax
  165. }
  166.  
  167. /* Returns 1 if it worked, 0 if it didn't */
  168. mousereset(){
  169. asm     mov ax,hsens            /* Don't hookup mouse if hsens==0 */
  170. asm     cmp ax,0                /* hsens,vsens set in PI.SET */
  171. asm     je nomouse
  172. asm     mov ax,resetmousedriver /* reset mouse driver to defaults */
  173. asm     int mouse
  174. asm     cmp ax,0                /* bx=number of buttons, not used yet */
  175. asm     mov ax,0
  176. asm     je nomouse              /* exit if no mouse driver loaded */
  177. asm     mov ax,1
  178. nomouse:;
  179. }
  180.  
  181. mousehook(){
  182. asm     mov ax,cs               /* Hook mouse driver to event handler */
  183. asm     mov es,ax               /* es=segment of event handler */
  184. asm     mov dx,offset mouseeventhandler
  185. asm     mov ax,seteventhandler  /* mouse driver function code */
  186. asm     mov cx,eventmask        /* signal mask for interrupt */
  187. asm     int mouse
  188. }
  189.  
  190. static int vsensx,hsensx;
  191.  
  192. /* Slow down the mouse in menus */
  193. menumouse(flag) int flag; {
  194.   if(flag){
  195.     vsensx=vsens; hsensx=hsens;
  196.     vsens= 2*vsens; hsens= 8*hsens;
  197.   } else {
  198.     vsens=vsensx; hsens=hsensx;
  199.   }
  200. }
  201.  
  202. #else
  203. menumouse(){}
  204. #endif
  205.