home *** CD-ROM | disk | FTP | other *** search
- /**
- * MOUSEHAN.C Simple mouse interrupt handler.
- *
- * This program shows the same menu as the MNEXAMPL sample program,
- * with the addition of a simple mouse interrupt handler, mouse_handler().
- * Every time the mouse moves, the handler receives control and records
- * the current mouse position. The intervention function display()
- * continuously displays the mouse's position and the number of calls
- * to the mouse interrupt handler.
- *
- * Version 6.00 (C)Copyright Blaise Computing Inc. 1989
- *
- **/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <binterv.h>
- #include <bintrupt.h>
- #include <bmenu.h>
- #include <bmouse.h>
- #include <bvideo.h>
-
- int cdecl main(void);
- void cleanup(int);
- void mouse_handler(const ALLREG *);
- void display(IV_EVENT *);
-
- #define MOUSE_STACKSIZE 1000 /* Size of mouse handler stack. */
- #define INTERV_STACKSIZE 1000 /* Size of intervention */
- /* function stack. */
-
- unsigned long num_calls = 0; /* Global variables maintained */
- int mouse_row,mouse_col; /* by mouse_handler(), */
- /* displayed by display(). */
-
- int main()
- {
- BMENU *pmenu;
- BORDER bord;
- WHERE location;
- int mode,columns,act_page;
- int cursor_was_off,cursor_row,cursor_col,high,low;
- int ercode,row,col,ch,keycode;
- static char mouse_stack[MOUSE_STACKSIZE];
- static char interv_stack[INTERV_STACKSIZE];
- /* Display mouse position on every */
- /* clock tick. */
- static IV_TIME interval = {1,IV_TM_INTERVAL};
-
- /* Create the menu and window structures in memory. */
-
- pmenu = mncreate(3,15, /* Dimensions of window data area. */
- SC_CYAN, /* Attributes of normal menu items. */
- REVERSE, /* Attributes of highlight bar. */
- NORMAL, /* Attributes of protected items. */
- NORMAL); /* Attributes of item descriptions. */
-
- if (pmenu == NIL)
- return b_wnerr; /* Quit if failure. */
- /* (b_wnerr records the most */
- /* recent menu or window error.) */
-
- /* Build the menu items and assign keys to select them. */
-
- if (NIL == mnitmkey(pmenu,0,0,MN_NOPROTECT,"Yes","yY",MN_SELECT))
- return b_wnerr;
- if (NIL == mnitmkey(pmenu,1,0,MN_NOPROTECT,"No","nN",MN_SELECT))
- return b_wnerr;
- if (NIL == mnitmkey(pmenu,2,0,MN_NOPROTECT,"Maybe","mM",MN_SELECT))
- return b_wnerr;
-
- /* Choose style of border. */
-
- bord.type = BBRD_DDDD; /* Box drawn with double lines. */
- bord.attr = SC_MAGENTA; /* Border will be magenta on black. */
-
- /* Choose where to display the menu: the active page on the */
- /* current display device. */
-
- location.dev = scmode(&mode,&columns,&act_page);
- location.page = act_page;
- location.corner.row = 10;
- location.corner.col = 5;
-
- /* If mouse is installed, turn mouse cursor on, select a mouse */
- /* usage style, and install mouse interrupt handler and the */
- /* display function. */
-
- if (MO_OK == mohide(MO_SHOW))
- {
- mnmstyle(pmenu,MN_MOU_CLICK,MO_LEFT);
- mohandlr(mouse_handler,MO_MOVE,
- mouse_stack,sizeof(mouse_stack),MO_INSTALL);
- ivinstal(display,"MOUSEHANdisplay",
- interv_stack,sizeof(interv_stack),
- NIL,0,
- &interval,1,
- IV_NO_DOS_NEED | IV_NO_DKEY_NEED | IV_NO_FLOAT_NEED);
- }
- else
- printf("No mouse driver is installed.\n");
-
- /* Save former cursor position and size. */
-
- cursor_was_off = sccurst(&cursor_row,&cursor_col,&high,&low);
-
- /* Actually display the menu */
-
- if (NIL == mndsplay(pmenu,&location,&bord))
- cleanup(b_wnerr); /* Quit if failure. */
-
- /* Await the user's selection. Start the highlight bar at the */
- /* item located at (0,0). Beep if unknown keystrokes are */
- /* pressed. Discard the menu when done. */
-
- ercode = mnread(pmenu,0,0,&row,&col,&ch,&keycode,
- MN_UNKNOWN_BEEP | MN_DESTROY);
-
- /* Turn the mouse cursor back off. */
-
- mohide(MO_HIDE);
-
- /* Restore the cursor. */
-
- sccurset(cursor_row,cursor_col);
- scpgcur(cursor_was_off,high,low,CUR_NO_ADJUST);
-
- /* Report the user's selection: error code, final key, and */
- /* location of item selected. */
-
- printf("Error code from MNREAD: %d\n",ercode);
- printf("Keystroke: ch = %d, keycode = %d\n",ch,keycode);
- printf("Item location: row = %d, column = %d\n",row,col);
-
- cleanup(0); /* Success. */
-
- return 255; /* We shouldn't fall through to */
- /* this statement. */
- }
-
- /**
- *
- * Name cleanup -- Remove interrupt handlers and terminate.
- *
- * Synopsis cleanup(errno);
- *
- * int errno Error code to be returned as ERRORLEVEL.
- *
- * Description This function removes the intervention code and the
- * mouse interrupt handler and terminates the program.
- *
- **/
-
- void cleanup(errno)
- int errno;
- {
- ivdisabl(ivctrl()); /* Disable intervention function (no */
- /* damage if it wasn't installed). */
-
- /* Note: resetting the mouse driver may also reinstall the */
- /* mouse's handler for asynch interrupts, making it awkward to */
- /* disable intervention code. Therefore for some programs it's */
- /* better to disable intervention code first. (Actually, this */
- /* program doesn't need this precaution, because by default */
- /* intevention code doesn't filter asynch interrupts.) */
-
- moreset(); /* Turn mouse cursor off and disable */
- /* mouse interrupt handler. */
- exit(errno);
- }
-
- /**
- *
- * Name mouse_handler -- Mouse interrupt handler.
- *
- * Synopsis (Not to be called directly from C code: to be called
- * by mouse interrupt dispatcher only.)
- *
- * Description This function is called whenever the mouse moves. It
- * records the number of calls and the current mouse
- * position (in terms of character rows and columns) in
- * global variables.
- *
- **/
-
- void mouse_handler(pregs)
- const ALLREG *pregs;
- {
- ++num_calls;
- mouse_row = pregs->dx.x >> 3;
- mouse_col = pregs->cx.x >> 3;
- }
-
- /**
- *
- * Name display -- Intervention function to report mouse position.
- *
- * Synopsis (Not to be called directly from C code: to be called
- * by intervention scheduler only.)
- *
- * Description This function is called on every clock tick. It
- * displays the current location of the mouse and the
- * number of times the mouse interrupt handler has been
- * called.
- *
- **/
-
- void display(pevent)
- IV_EVENT *pevent;
- {
- char message[81];
-
- sprintf(message,"Calls to mouse interrupt handler: %8u",num_calls);
- vidspmsg(4,35,SC_WHITE | INTENSITY,SC_BLACK,message);
- sprintf(message,"Row = %2d, Column = %2d",mouse_row,mouse_col);
- vidspmsg(5,35,SC_WHITE | INTENSITY,SC_BLACK,message);
-
- pevent++; /* Suppress compiler warning. */
- }