home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_02 / wmouse.c < prev    next >
C/C++ Source or Header  |  1991-04-02  |  7KB  |  383 lines

  1. /* wmouse.c
  2.  *
  3.  *    simplified mouse interface.
  4.  *        wmouse_init    - detects mouse.
  5.  *        wmouse_turn    - turn mouse ON or OFF. set cursor & scaling.
  6.  *                               call at start of wgets()
  7.  *                 and again at exit from wgets()
  8.  *
  9.  *        wmouse_location- get mouse position & see if buttons hit
  10.  *                 ( in text mode, sets character locations)
  11.  *                 ( in graphics mode, sets pixel & char loc)
  12.  *    
  13.  *        Assumes that any press of RIGHT button will be translated to ESCAPE
  14.  *        Therefore, mouse activity is only reported when:
  15.  *            LEFT- button depress, any mvt with button down, button release
  16.  *            CENTER- ditto.
  17.  *            RIGHT- only reports when button is first pressed. 
  18.  *
  19.  *
  20.  */
  21. #include "wsys.h"
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. static void calc_coords (void);
  30.  
  31.  
  32.  
  33.  
  34. static unsigned int prev_buttons =0;
  35.  
  36.  
  37.  
  38.  
  39.  
  40. /* wmouse_init
  41.  *    sets wmouse.wms_present
  42.  *
  43.  *    in text mode sets a software mouse cursor.
  44.  */
  45. void wmouse_init (void)
  46.     {
  47.     int hold, nb;
  48.  
  49.  
  50.  
  51.  
  52.  
  53.     PSEUDOREGS
  54.  
  55.     _AX = 0;
  56.     INTERRUPT (0x33);
  57.     nb   = _BX;
  58.     hold = _AX;
  59.  
  60.  
  61.     if ( hold )
  62.         {
  63.         /* a mouse is present */
  64.         wmouse.wms_present = nb;
  65.         }
  66.  
  67.  
  68.     if ( wmouse.wms_present &&  (wmode == 'T') )
  69.         {
  70.         /* setup software cursor = inverting = 0xffff, 0x7f00
  71.          *               char      = 0x0000, 0x07cc
  72.          */
  73.         if ( wmonitor == 'H' )
  74.             {
  75.             /* setup an underlining (=BLUE) cursor for HERCULES
  76.              * preserve any bright attributes underlying.
  77.              */
  78.             wmouse_textmask ( 0x08ff, 0x0100 );
  79.             }
  80.         else
  81.             {
  82.             /* setup a reversing cursor for COLOR monitors
  83.              */
  84.             wmouse_textmask ( 0xffff, 0x7f00 );
  85.             }
  86.  
  87.         }
  88.  
  89.     /* reset current button state (if changing mode )
  90.      */
  91.     prev_buttons = 0;
  92.  
  93.  
  94.     return; /* wmouse_init */
  95.  
  96.     }
  97.  
  98.  
  99.  
  100. /* mouse text_mode cursor
  101.  *    sets up a text mode cursor.
  102.  *    PARAMETERS:
  103.  *          screen mask is AND'd with char/attr under mouse.
  104.  *          cursor mask is XOR'd with char/attr.
  105.  *
  106.  *    RETURNS: void.
  107.  */
  108. void wmouse_textmask ( unsigned int screen_mask, unsigned int cursor_mask )
  109.     {
  110.     PSEUDOREGS
  111.  
  112.     _DX = cursor_mask;
  113.     _CX = screen_mask;
  114.     _BX = 0;         /* 0=software cursor, 1=hardware curosr */
  115.     _AX = 0x0A;        /* text mode cursor */
  116.     INTERRUPT ( 0x33 );
  117.     return;            /* wmouse_textmask */
  118.     }
  119.  
  120.  
  121.  
  122.  
  123. /* wmouse_turn ()
  124.  *    turns mouse ON or OFF
  125.  *    NOTE: must keep calls to this function in order
  126.  *        (alternate ON with OFF, must start with ON)
  127.  *     mouse driver actually keeps count of calls,
  128.  *        if you call repetitively, mouse will turn OFF
  129.  *        even if you call as ON.
  130.  *
  131.  *    When you turn the mouse ON, this routine sets mouse scaling values.
  132.  */
  133. void wmouse_turn ( int state )
  134.     {
  135.     int hold;
  136.  
  137.     PSEUDOREGS
  138.  
  139.  
  140.     if ( wmouse.wms_present )
  141.         {
  142.  
  143.         if ( state != OFF )
  144.             {
  145.             hold = 1;        /* 1=turn on 2 = turn off */
  146.  
  147.  
  148.             }
  149.         else
  150.             {
  151.             hold = 2;        /* turn mouse off */
  152.             }
  153.  
  154.         _AX = hold;
  155.         INTERRUPT (0x33);
  156.  
  157.         }
  158.  
  159.  
  160.     return;    /* wmouse_turn */
  161.  
  162.     }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. /* wmouse_location ()
  170.  *
  171.  *    get the curretn location of the mouse,
  172.  *        and number of times the left and right buttons were pressed.
  173.  *    NOTE: this routine counts key presses on the left
  174.  *                      and key releases on the right.
  175.  *        this allows the user to be tentative with the left key
  176.  *        but forces the user to be definitive with the right one.
  177.  *    RETURNS:
  178.  *        MOUSE if mouse moved, ENTER for left button, ESCAPE for right
  179.  *
  180.  */
  181. void  wmouse_location (void)
  182.     {
  183.  
  184.     int         xtemp,
  185.             ytemp;
  186.  
  187.     unsigned int      new_buttons,
  188.             released,
  189.             pressed;
  190.  
  191.     PSEUDOREGS
  192.  
  193.  
  194.  
  195.     if ( ! wmouse.wms_present )
  196.         {
  197.         return;
  198.         }
  199.  
  200.  
  201.  
  202.  
  203.     /* Check mouse status -
  204.      * this function returns mouse location and current button state.
  205.      * _BX returns buttons down = bit =1, L=0x01, R=0x02 C=0x04
  206.      */
  207.  
  208.     _AX =3;
  209.     INTERRUPT ( 0x33 );
  210.     new_buttons = _BX;
  211.     xtemp = _CX;
  212.     ytemp = _DX;
  213.  
  214.     /*
  215.      * NOTE mouse driver only guarantees meaning to the low-order bits.
  216.      * so black out bits we're not inerested in.
  217.      *
  218.      *    0x01 =left, 0x02 =right, 0x04 =center.
  219.      */
  220.     new_buttons &= 0x07;
  221.  
  222.  
  223.  
  224.     /* check for button releases (comparing against prev. button state)
  225.      *
  226.      * a bit will be TRUE if button was down (bit ON) and now is up.
  227.      * also check for new presses of the left button.
  228.      */
  229.     released = ( (~new_buttons) & ( prev_buttons) );
  230.     pressed  = ( ( new_buttons) & (~prev_buttons) );
  231.  
  232.  
  233.  
  234.     /* setup new button state in prev_buttons,
  235.      * for next call
  236.      */
  237.     prev_buttons = new_buttons;
  238.  
  239.  
  240.  
  241.     /* check button release flags
  242.      * button bits: 0x01 = L,     0x02= R,     0x04= C
  243.      */
  244.     if ( released & 0x01 )
  245.         {
  246.         /* left button was just released
  247.          */
  248.         wmouse.wms_internal |= (WMS_LEFT_RLS + WMS_MOVED);
  249.         }
  250.     if ( released & 0x04 )
  251.         {
  252.         wmouse.wms_internal |= WMS_CENTER_RLS;
  253.         }
  254.     if ( pressed & 0x01 )
  255.         {
  256.         wmouse.wms_internal |= (WMS_LEFT_PRS + WMS_MOVED);
  257.         }
  258.     if ( pressed & 0x02 )
  259.         {
  260.         wmouse.wms_internal |= WMS_RIGHT_PRS;
  261.         }
  262.     if ( pressed & 0x04 )
  263.         {
  264.         wmouse.wms_internal |= WMS_CENTER_PRS;
  265.         }
  266.     if ( released & 0x02 )
  267.         {
  268.         wmouse.wms_internal |= WMS_RIGHT_RLS;
  269.         }
  270.         
  271.     /* see if mouse moved with left or center buttons held down.
  272.      */
  273.     if ( ( new_buttons & 0x01   )        /* LEFT=0x01 or CENTER=0x04 */  
  274.        &&( ( wmouse.wms_xmickey != xtemp ) || ( wmouse.wms_ymickey != ytemp ) )
  275.        )
  276.         {
  277.         wmouse.wms_internal |= WMS_MOVED;
  278.         }
  279.          
  280.     /* check location if L button newly pressed 
  281.      * or mouse mvt with L button held down. 
  282.      */
  283.     if ( wmouse.wms_internal )
  284.         {
  285.         wmouse.wms_xmickey = xtemp;
  286.         wmouse.wms_ymickey = ytemp;
  287.         calc_coords ();
  288.         }
  289.  
  290.     wmouse.wms_used = wmouse.wms_internal;
  291.  
  292.     return;    /* wmouse_location */
  293.  
  294.     }
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. /* calc_coords () -  convert location from 'mickeys' to screen co-ords
  302.  *    mickeys go from 0-639 and 1-199
  303.  * screen coords are measured both in text units (chars)
  304.  *                  and   graphics units (pixels)
  305.  */
  306.  
  307.  
  308. static void calc_coords (void)
  309.     {
  310.     /* get absolute position on screen, ignoring windows
  311.      */
  312.     if ( wmode == 'T' )
  313.         {
  314.         wmouse.wms_xabs = wmouse.wms_xmickey/8;
  315.         wmouse.wms_yabs = wmouse.wms_ymickey/8;
  316.         }
  317.     #ifndef TEXTONLY
  318.     else
  319.         {
  320.         /* GRAPHICS MODE get pixel co-ords of mouse */
  321.         wmouse.wms_pxabs = wmouse.wms_xmickey;
  322.         wmouse.wms_pyabs = wmouse.wms_ymickey;
  323.  
  324.         wmouse.wms_xabs = wmouse.wms_pxabs / wpxchar;
  325.         wmouse.wms_yabs = wmouse.wms_pyabs / wpychar;
  326.  
  327.  
  328.  
  329.         }
  330.     #endif /* !TEXTONLY */
  331.  
  332.  
  333.  
  334.  
  335.     /* calculate mouse position relative to window
  336.      */
  337.     #ifndef TEXTONLY
  338.         if ( wmode == 'G' )
  339.             {
  340.             /* calculate pixel co-ords within window */
  341.             wmouse.wms_px = wmouse.wms_pxabs -
  342.                     ( w0->winleft* wpxchar );
  343.             wmouse.wms_py = wmouse.wms_pyabs -
  344.                     ( w0->wintop * wpychar );
  345.             }
  346.     #endif    /* ! TEXTONLY */
  347.  
  348.     /* text mode -co-ords */
  349.     wmouse.wms_x = wmouse.wms_xabs - ( w0->winleft );
  350.     wmouse.wms_y = wmouse.wms_yabs - ( w0->wintop  );
  351.  
  352.     if (
  353.          (  /* x-cord inside window */
  354.         wmouse.wms_xabs >= w0-> winleft
  355.          && wmouse.wms_xabs <= (w0->winleft + w0->winxmax)
  356.          )
  357.        &&
  358.          (    /* y co-ord inside window */
  359.         wmouse.wms_yabs >= w0-> wintop
  360.          && wmouse.wms_yabs <= (w0->wintop + w0->winymax)
  361.          )
  362.        )
  363.         {
  364.         /* mouse is inside window */
  365.         wmouse.wms_inwindow = 1;
  366.  
  367.         }
  368.     else     {
  369.         wmouse.wms_inwindow = 0;
  370.         }
  371.  
  372.  
  373.     return;    /* calc_coords() */
  374.     }
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382. /*------------------------ end of WMOUSE.C -------------------------*/
  383.