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

  1. /* wSysRrq.c
  2.  *      Sys Rq interrupt handler
  3.  *      Int (0x15), with _AH = 0x85 on entry.
  4.  *        pressed= _AL = 0, released _AL = 1
  5.  *
  6.  *      this interrupt is generated whenever ALT-SysRq is pressed
  7.  *
  8.  *    this routine MUST be compiled under TurboC...
  9.  *        with register variables ON and stack warning OFF
  10.  *        
  11.  *    You may have to increase the stack size if your use a complex function
  12.  *        as your SysRq handler.
  13.  *
  14.  *    It will not work well with programs
  15.  *        that use interrupt 0x21 to read the keyboard.
  16.  *
  17.  *    Your interrupt routine will be ignored when running as a virtual task 
  18.  *        under DeskView 
  19.  *    
  20.  *
  21.  */
  22. #ifndef __TURBOC__
  23.     #error Sorry, this routine will not compile under MICROSOFT C
  24. #endif
  25.  
  26.  
  27. #include "wsys.h"
  28.  
  29. #define INT_SYSRQ  (0x15)
  30.  
  31.  
  32. static void interrupt  handler  
  33.         (unsigned bp, unsigned di, unsigned si, unsigned ds, unsigned es, 
  34.          unsigned dx, unsigned cx, unsigned bx, unsigned ax, unsigned ip,
  35.          unsigned cs );
  36.  
  37. static void interrupt  (*old_handler) (void) = NULL; 
  38.  
  39.  
  40. static char reentrant =0;
  41.  
  42. static void (*func)(void) = NULL;    /* function to call */    
  43.  
  44. static int exit_installed =0;        /* atexit functions */
  45. static void cleanup_SysRq (void);
  46.  
  47.  
  48. void  wSysRq_install ( void (*u_func)() )
  49.     {
  50.     disable();
  51.     old_handler = getvect ( INT_SYSRQ );
  52.     setvect ( INT_SYSRQ, handler );
  53.     func = u_func;
  54.     enable();
  55.     
  56.     if ( ! exit_installed )
  57.         {
  58.         exit_installed =1;
  59.         if ( 00 != atexit ( cleanup_SysRq ) )
  60.             {
  61.             werror ('W', "SysRq installation failed");
  62.             }
  63.         }
  64.     return;       /* wSysRq_install */
  65.     }
  66.  
  67. void  wSysRq_remove ( void )
  68.     {
  69.     disable();
  70.     setvect ( INT_SYSRQ, old_handler );
  71.     old_handler = NULL;
  72.     func = NULL;
  73.     enable();
  74.     return;       /* wSysRq_install */
  75.     }
  76.  
  77.  
  78. static void cleanup_SysRq (void)
  79.     {
  80.     if ( func )
  81.         {
  82.         wSysRq_remove ();
  83.         }
  84.     return;        /* cleanup_SysRq */
  85.     }
  86.  
  87.  
  88.  
  89.  
  90. /* Interrupt handler - activated on key press and on key release
  91.  *     tests for keypress only, makes sure not a double keypress (reentrant)
  92.  *     tests for inDOS() -- will only work if not using DOS to poll keyboard.
  93.  * 
  94.  *  NOTE: pragmas which follow: the first is OK for TurboC v2.0
  95.  *            others require the TurboC++ version
  96.  *            (just take them out & make sure compiler opts are set correctly)
  97.  */
  98. #pragma warn -par
  99. #pragma -N-
  100. #pragma -r
  101.  
  102.  
  103. static void interrupt  handler  
  104.         (unsigned bp, unsigned di, unsigned si, unsigned ds, unsigned es, 
  105.          unsigned dx, unsigned cx, unsigned bx, unsigned ax, unsigned ip,
  106.          unsigned cs )
  107.     {
  108.  
  109.     if ( (ax == 0x8500u) )    
  110.          {
  111.         if ( reentrant == 0 )
  112.             {
  113.             if ( ! w_inDOS() ) 
  114.                 {
  115.                 reentrant = 1;
  116.                 if ( func )
  117.                     {
  118.                     (*func)();
  119.                     }
  120.                 reentrant = 0;
  121.                 }
  122.             }
  123.         }
  124.     (*old_handler)();    
  125.     return;        /* handler */
  126.     }
  127.  
  128.  
  129.  
  130. /*---------------------- end of WSYSRQ.C --------------------------------*/
  131.  
  132.