home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / TICKTRAP.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  109 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  TICKTRAP.C - Trap the PC timer interrupt.
  5. **
  6. **  public domain by Bob Stout
  7. */
  8.  
  9. #include <dos.h>
  10. #include "ticktrap.h"
  11.  
  12. static int (*newptr)(void);
  13. static void endtrap(void);
  14.  
  15. #if defined(__ZTC__) || defined(__SC__)
  16.  
  17. static int do_new(struct INT_DATA *idp);
  18. static struct INT_DATA idp;
  19.  
  20. /*
  21. **  Installs a user-defined funtion in an Int 1Ch ISR. The user-supplied
  22. **  function should take no arguments and should return an int. If zero
  23. **  is returned from the user-supplied function, the previous Int 1Ch ISR
  24. **  is called upon exit. If non-zero is returned from the user-supplied
  25. **  function, the previous ISR is not called.
  26. **
  27. **  Side effect: Registers endtrap() with atexit() to restore Int 1Ch
  28. **               upon program termination.
  29. */
  30.  
  31. void ticktrap(int (*fptr)(void))
  32. {
  33.       newptr = fptr;
  34.       int_intercept(0x1c, do_new, 0);
  35.       atexit(endtrap);
  36. }
  37.  
  38. /*
  39. **  Calls the previous Int 1Ch ISR
  40. */
  41.  
  42. void tickchain(void)
  43. {
  44.       int_prev(&idp);
  45. }
  46.  
  47. static int do_new(struct INT_DATA *idp)
  48. {
  49.       return (*newptr)();
  50. }
  51.  
  52. static void endtrap(void)
  53. {
  54.       int_restore(0x1c);
  55. }
  56.  
  57. #else /* It's not Symantec or Zortech */
  58.  
  59. static void (INTERRUPT FAR *old1c)(void);
  60.  
  61. static void INTERRUPT FAR do_new(void);
  62.  
  63. /*
  64. **  Installs a user-defined funtion in an Int 1Ch ISR. The user-supplied
  65. **  function should take no arguments and should return an int. If zero
  66. **  is returned from the user-supplied function, the previous Int 1Ch ISR
  67. **  is called upon exit. If non-zero is returned from the user-supplied
  68. **  function, the previous ISR is not called.
  69. **
  70. **  Side effect: Registers endtrap() with atexit() to restore Int 1Ch
  71. **               upon program termination.
  72. */
  73.  
  74. void ticktrap(int (*fptr)(void))
  75. {
  76.       old1c = _dos_getvect(0x1c);
  77.       newptr = fptr;
  78.       disable();
  79.       _dos_setvect(0x1c, do_new);
  80.       enable();
  81.       atexit(endtrap);
  82. }
  83.  
  84. /*
  85. **  Calls the previous Int 1Ch ISR
  86. */
  87.  
  88. void tickchain(void)
  89. {
  90.       (*old1c)();
  91. }
  92.  
  93. static void INTERRUPT FAR do_new(void)
  94. {
  95.       int retval;
  96.       retval = (*newptr)();
  97.       if (0 == retval)
  98.             (*old1c)();
  99. }
  100.  
  101. static void endtrap(void)
  102. {
  103.       disable();
  104.       _dos_setvect(0x1c, old1c);
  105.       enable();
  106. }
  107.  
  108. #endif
  109.