home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / cpluspls / tsr100je.zip / TICK.CPP < prev    next >
C/C++ Source or Header  |  1993-03-22  |  3KB  |  97 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      TICK.CPP: Once-a-second tick demo for DOS TSR class.
  4. //      (c) J.English 1993 (je@unix.brighton.ac.uk)
  5. //
  6. //      This example makes an irritating beep every second after the
  7. //      hotkey is pressed.  Try removing the call to "sync()" and see
  8. //      what happens.
  9. //
  10. //--------------------------------------------------------------------------
  11.  
  12. #include <iostream.h>
  13. #include <stdlib.h>
  14. #include <dos.h>
  15. #include <ctype.h>
  16. #include "tsr.h"
  17.  
  18. //----- Error messages for TSR status values -----
  19. const char* status_msg [] = {
  20.     "success",
  21.     "DOS version 3 or higher required",
  22.     "multiple TSR instances declared in program", 
  23.     "can't allocate stack",
  24.     "can't allocate multiplex function code",
  25.     "already loaded",
  26.     "can't make program resident",
  27.     "startup error"
  28. };
  29.  
  30. //----- Error messages for unload result -----
  31. const char* unload_msg [] = {
  32.     "unloaded",
  33.     "not loaded",
  34.     "can't be unhooked",
  35.     "can't be unloaded",
  36.     "environment can't be unloaded"
  37. };
  38.  
  39. //----- The derived TSR class -----
  40. class Ticker : public TSR
  41. {
  42.   private:
  43.     virtual void main (int hotkey);     // body of clock TSR
  44.     int active;                         // true if ticking
  45.   public:
  46.     Ticker () :                         // constructor
  47.         TSR ("TSR ticking demo"),       // ... call base class constructor
  48.         active (0)                      // ... no ticking initially
  49.         { }                             // ... nothing else needed
  50. };
  51.  
  52. //----- The main program -----
  53. int main (int argc, char** argv)
  54. {
  55.     Ticker ticker;                      // create TSR instance
  56.  
  57.     switch (argc)
  58.     {
  59.       case 1:                           // invoked by TICK -- load it
  60.         cout << "Installing " << ticker.name() << " in memory\n";
  61.         cout << "Press both shift keys to activate and deactivate\n";
  62.  
  63.         ticker.run (TSR::LSHIFT + TSR::RSHIFT, 18);
  64.  
  65.         cout << "Error installing " << ticker.name() << ": "
  66.              << status_msg [ticker.status()] << "\n";
  67.         break;
  68.  
  69.       case 2:                           // invoked by TICK/U -- unload it
  70.         if (argv[1][0] == '/' && toupper (argv[1][1]) == 'U')
  71.         {   cout << ticker.name() << " "
  72.                  << unload_msg [ticker.unload()] << "\n";
  73.             break;
  74.         }
  75.  
  76.       default:                          // give usage message otherwise
  77.         cout << "Usage: TICK [/U]\n";
  78.         return -1;
  79.     }
  80.     return ticker.status ();
  81. }
  82.  
  83. //----- Resident part of TSR -----
  84. void Ticker::main (int hotkey)
  85. {
  86.     if (hotkey)
  87.     {   active = !active;               // hotkey toggles visibility
  88.         sync ();                        // 1-second timeslice starts now
  89.     }
  90.     if (!active)
  91.         return;                         // nothing to do if inactive
  92.  
  93.     sound (880);                        // peep for 100 ms
  94.     delay (100);
  95.     nosound ();
  96. }
  97.