home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / s / s001 / 1.ddi / TS / SRC / ONKEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-08  |  2.7 KB  |  91 lines

  1.                 /******************************************
  2.            *            ONKEY.C             *
  3.                * Copyright TimeSlice, Inc. 1985, 86, 87. *
  4.                ******************************************/
  5.  
  6. #include <ts.h>
  7.  
  8. SHIFT *_shfrnt;                /*head of shift structs list*/
  9.  
  10. void kbd_isr();
  11.  
  12. /***
  13. * ON_KEY( STATUS, FUNCPTR )
  14. * This function attaches an isr to interrupt # 9. The isr executes a user
  15. * function if the keyboard shift status is equal to a trigger value (hot key).
  16. ***/
  17. void on_key( status , func )
  18. int  status ;            /* hot key value */
  19. void (*func)() ;            /* user function to execute if hot key pressed */
  20. {
  21.   SHIFT    *sp;
  22.   char *malloc();
  23.   int cpuflags;
  24.   static installed;
  25.  
  26.   critstart( DOS_CRCLASS );
  27.  
  28.   /*if called for the first time, install kbd_isr*/
  29.   if( !installed ) {
  30.     intatt( 0x09, 0x300, kbd_isr, REPLACE ) ;
  31.     installed = 1;
  32.   }
  33.  
  34.   /*search for existing SHIFT with same status*/
  35.   for( sp = _shfrnt ; sp && sp->status!=status ; sp = sp->nxt );
  36.  
  37.   /*if not found, allocate a new SHIFT and put it in front of _shfrnt list*/
  38.   if( !sp ) {
  39. #if defined(TURBOC)
  40.     sp = (SHIFT *)_ts_malloc( sizeof( SHIFT ) );
  41. #else
  42.     sp = (SHIFT *)malloc( sizeof( SHIFT ) );
  43. #endif
  44.     cpuflags = cli();
  45.     sp->nxt = _shfrnt;
  46.     _shfrnt = sp;
  47.     putf( cpuflags );
  48.     sp->status = status;
  49.   }
  50.   sp->infunc = 0;            /*always reset when onkey called*/
  51.   sp->func = func;
  52.   critend( DOS_CRCLASS );
  53. }
  54.  
  55. /*****
  56. * KBD_ISR() 
  57. * This isr is attached to the keyboard interrupt.
  58. * It obtains the keyboard shift status and compares it with
  59. * the status contained in the shift structure. If keyboard status
  60. * is a superset of the SHIFT status, then it enables interrupts, 
  61. * sets a flag to indicate that the user function is being executed
  62. * and then executes it.
  63. *****/
  64. void kbd_isr( ip, ro, rseg )
  65. INTLST *ip ;
  66. int    ro ,
  67.     rseg ;
  68. {                                    
  69.   int cpuflags ;
  70.   SHIFT *sp;                /* Pointer to shift structure */
  71.   char status ;
  72.  
  73.   oisr( ip , ro , rseg );        /* Execute original Keyboard ISR */
  74.   peek( 0x40, 0x17, &status, sizeof( status ) ); /*get kbd shift status*/
  75.  
  76.   /*look if there is a SHIFT for that status*/
  77.   for( sp = _shfrnt ; sp ; sp = sp->nxt )
  78.     if( (sp->status & status) == sp->status )
  79.       break;
  80.  
  81.   /*only if found and not already in func, execute func*/
  82.   if( sp && !sp->infunc ) {
  83.     cpuflags = getf() ;            /*get a copy of the cpu flags */
  84.     sp->infunc = 1 ;            /*set infunc flag for recursive calls*/
  85.     putf( cpuflags | 0x200 ) ;        /*enable interrupts for the duration of the user function */
  86.     (*sp->func)() ;            /*execute user function */
  87.     sp->infunc = 0 ;            /* reset flag */
  88.     putf( cpuflags ) ;            /* restore cpu flags */
  89.   }
  90. }
  91.