home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 2.ddi / TOOLS.2 / EXAMPLES / MOUSEHAN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  6.8 KB  |  220 lines

  1. /**
  2. *    MOUSEHAN.C    Simple mouse interrupt handler.
  3. *
  4. *  This program shows the same menu as the MNEXAMPL sample program,
  5. *  with the addition of a simple mouse interrupt handler, mouse_handler().
  6. *  Every time the mouse moves, the handler receives control and records
  7. *  the current mouse position.    The intervention function display()
  8. *  continuously displays the mouse's position and the number of calls
  9. *  to the mouse interrupt handler.
  10. *
  11. *  Version    6.00 (C)Copyright Blaise Computing Inc. 1989
  12. *
  13. **/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <binterv.h>
  18. #include <bintrupt.h>
  19. #include <bmenu.h>
  20. #include <bmouse.h>
  21. #include <bvideo.h>
  22.  
  23. int cdecl main(void);
  24. void      cleanup(int);
  25. void      mouse_handler(const ALLREG *);
  26. void      display(IV_EVENT *);
  27.  
  28. #define  MOUSE_STACKSIZE    1000    /* Size of mouse handler stack. */
  29. #define  INTERV_STACKSIZE   1000    /* Size of intervention        */
  30.                     /* function stack.            */
  31.  
  32. unsigned long num_calls = 0;        /* Global variables maintained  */
  33. int          mouse_row,mouse_col;  /* by mouse_handler(),        */
  34.                     /* displayed by display().        */
  35.  
  36. int main()
  37. {
  38.     BMENU  *pmenu;
  39.     BORDER  bord;
  40.     WHERE   location;
  41.     int     mode,columns,act_page;
  42.     int     cursor_was_off,cursor_row,cursor_col,high,low;
  43.     int     ercode,row,col,ch,keycode;
  44.     static char mouse_stack[MOUSE_STACKSIZE];
  45.     static char interv_stack[INTERV_STACKSIZE];
  46.                   /* Display mouse position on every      */
  47.                   /* clock tick.                  */
  48.     static IV_TIME interval = {1,IV_TM_INTERVAL};
  49.  
  50.     /* Create the menu and window structures in memory.         */
  51.  
  52.     pmenu = mncreate(3,15,    /* Dimensions of window data area.  */
  53.         SC_CYAN,    /* Attributes of normal menu items. */
  54.         REVERSE,    /* Attributes of highlight bar.     */
  55.         NORMAL,     /* Attributes of protected items.   */
  56.         NORMAL);    /* Attributes of item descriptions. */
  57.  
  58.     if (pmenu == NIL)
  59.     return b_wnerr;     /* Quit if failure.            */
  60.                 /* (b_wnerr records the most        */
  61.                 /* recent menu or window error.)    */
  62.  
  63.     /* Build the menu items and assign keys to select them.        */
  64.  
  65.     if (NIL == mnitmkey(pmenu,0,0,MN_NOPROTECT,"Yes","yY",MN_SELECT))
  66.     return b_wnerr;
  67.     if (NIL == mnitmkey(pmenu,1,0,MN_NOPROTECT,"No","nN",MN_SELECT))
  68.     return b_wnerr;
  69.     if (NIL == mnitmkey(pmenu,2,0,MN_NOPROTECT,"Maybe","mM",MN_SELECT))
  70.     return b_wnerr;
  71.  
  72.     /* Choose style of border.                        */
  73.  
  74.     bord.type = BBRD_DDDD;    /* Box drawn with double lines.     */
  75.     bord.attr = SC_MAGENTA;    /* Border will be magenta on black. */
  76.  
  77.     /* Choose where to display the menu:  the active page on the    */
  78.     /* current display device.                        */
  79.  
  80.     location.dev    = scmode(&mode,&columns,&act_page);
  81.     location.page    = act_page;
  82.     location.corner.row = 10;
  83.     location.corner.col =  5;
  84.  
  85.     /* If mouse is installed, turn mouse cursor on, select a mouse  */
  86.     /* usage style, and install mouse interrupt handler and the     */
  87.     /* display function.                        */
  88.  
  89.     if (MO_OK == mohide(MO_SHOW))
  90.     {
  91.     mnmstyle(pmenu,MN_MOU_CLICK,MO_LEFT);
  92.     mohandlr(mouse_handler,MO_MOVE,
  93.          mouse_stack,sizeof(mouse_stack),MO_INSTALL);
  94.     ivinstal(display,"MOUSEHANdisplay",
  95.          interv_stack,sizeof(interv_stack),
  96.          NIL,0,
  97.          &interval,1,
  98.          IV_NO_DOS_NEED | IV_NO_DKEY_NEED | IV_NO_FLOAT_NEED);
  99.     }
  100.     else
  101.     printf("No mouse driver is installed.\n");
  102.  
  103.     /* Save former cursor position and size.                */
  104.  
  105.     cursor_was_off = sccurst(&cursor_row,&cursor_col,&high,&low);
  106.  
  107.     /* Actually display the menu                    */
  108.  
  109.     if (NIL == mndsplay(pmenu,&location,&bord))
  110.     cleanup(b_wnerr);    /* Quit if failure.            */
  111.  
  112.     /* Await the user's selection.  Start the highlight bar at the  */
  113.     /* item located at (0,0).  Beep if unknown keystrokes are        */
  114.     /* pressed.  Discard the menu when done.                */
  115.  
  116.     ercode = mnread(pmenu,0,0,&row,&col,&ch,&keycode,
  117.             MN_UNKNOWN_BEEP | MN_DESTROY);
  118.  
  119.     /* Turn the mouse cursor back off.                    */
  120.  
  121.     mohide(MO_HIDE);
  122.  
  123.     /* Restore the cursor.                        */
  124.  
  125.     sccurset(cursor_row,cursor_col);
  126.     scpgcur(cursor_was_off,high,low,CUR_NO_ADJUST);
  127.  
  128.     /* Report the user's selection:  error code, final key, and     */
  129.     /* location of item selected.                    */
  130.  
  131.     printf("Error code from MNREAD:  %d\n",ercode);
  132.     printf("Keystroke:  ch = %d, keycode = %d\n",ch,keycode);
  133.     printf("Item location:  row = %d, column = %d\n",row,col);
  134.  
  135.     cleanup(0);         /* Success.                */
  136.  
  137.     return 255;         /* We shouldn't fall through to     */
  138.                 /* this statement.            */
  139. }
  140.  
  141. /**
  142. *
  143. * Name        cleanup -- Remove interrupt handlers and terminate.
  144. *
  145. * Synopsis    cleanup(errno);
  146. *
  147. *        int errno    Error code to be returned as ERRORLEVEL.
  148. *
  149. * Description    This function removes the intervention code and the
  150. *        mouse interrupt handler and terminates the program.
  151. *
  152. **/
  153.  
  154. void cleanup(errno)
  155. int errno;
  156. {
  157.     ivdisabl(ivctrl());     /* Disable intervention function (no    */
  158.                 /* damage if it wasn't installed).      */
  159.  
  160.     /* Note:  resetting the mouse driver may also reinstall the     */
  161.     /* mouse's handler for asynch interrupts, making it awkward to  */
  162.     /* disable intervention code.  Therefore for some programs it's */
  163.     /* better to disable intervention code first.  (Actually, this  */
  164.     /* program doesn't need this precaution, because by default     */
  165.     /* intevention code doesn't filter asynch interrupts.)          */
  166.  
  167.     moreset();            /* Turn mouse cursor off and disable    */
  168.                 /* mouse interrupt handler.         */
  169.     exit(errno);
  170. }
  171.  
  172. /**
  173. *
  174. * Name        mouse_handler -- Mouse interrupt handler.
  175. *
  176. * Synopsis    (Not to be called directly from C code:  to be called
  177. *        by mouse interrupt dispatcher only.)
  178. *
  179. * Description    This function is called whenever the mouse moves.  It
  180. *        records the number of calls and the current mouse
  181. *        position (in terms of character rows and columns) in
  182. *        global variables.
  183. *
  184. **/
  185.  
  186. void mouse_handler(pregs)
  187. const ALLREG *pregs;
  188. {
  189.     ++num_calls;
  190.     mouse_row = pregs->dx.x >> 3;
  191.     mouse_col = pregs->cx.x >> 3;
  192. }
  193.  
  194. /**
  195. *
  196. * Name        display -- Intervention function to report mouse position.
  197. *
  198. * Synopsis    (Not to be called directly from C code:  to be called
  199. *        by intervention scheduler only.)
  200. *
  201. * Description    This function is called on every clock tick.  It
  202. *        displays the current location of the mouse and the
  203. *        number of times the mouse interrupt handler has been
  204. *        called.
  205. *
  206. **/
  207.  
  208. void display(pevent)
  209. IV_EVENT *pevent;
  210. {
  211.     char message[81];
  212.  
  213.     sprintf(message,"Calls to mouse interrupt handler: %8u",num_calls);
  214.     vidspmsg(4,35,SC_WHITE | INTENSITY,SC_BLACK,message);
  215.     sprintf(message,"Row = %2d, Column = %2d",mouse_row,mouse_col);
  216.     vidspmsg(5,35,SC_WHITE | INTENSITY,SC_BLACK,message);
  217.  
  218.     pevent++;              /* Suppress compiler warning.        */
  219. }
  220.