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

  1. //--------------------------------------------------------------------------
  2. //
  3. //      CLOCK.CPP: Pop-up clock demo for DOS TSR class.
  4. //      (c) J.English 1993 (je@unix.brighton.ac.uk)
  5. //
  6. //      This example pops up a clock in the top right-hand corner of the
  7. //      screen when the hotkey is pressed.
  8. //
  9. //--------------------------------------------------------------------------
  10.  
  11. #include <iostream.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <dos.h>
  16. #include <ctype.h>
  17. #include "tsr.h"
  18.  
  19. //----- Error messages for TSR status values -----
  20. const char* status_msg [] = {
  21.     "success",
  22.     "DOS version 3 or higher required",
  23.     "multiple TSR instances declared in program", 
  24.     "can't allocate stack",
  25.     "can't allocate multiplex function code",
  26.     "already loaded",
  27.     "can't make program resident",
  28.     "startup error"
  29. };
  30.  
  31. //----- Error messages for unload result -----
  32. const char* unload_msg [] = {
  33.     "unloaded",
  34.     "not loaded",
  35.     "can't be unhooked",
  36.     "can't be unloaded",
  37.     "environment can't be unloaded"
  38. };
  39.  
  40. //----- The derived TSR class -----
  41. class PopUpClock : public TSR
  42. {
  43.   private:
  44.     virtual void main (int hotkey);     // body of clock TSR
  45.     int visible;                        // true if clock is visible
  46.   public:
  47.     PopUpClock () :                     // constructor
  48.         TSR ("TSR clock demo"),         // ... call base class constructor
  49.         visible (0)                     // ... clock initially invisible
  50.         { }                             // ... nothing else needed
  51. };
  52.  
  53. //----- The main program -----
  54. int main (int argc, char** argv)
  55. {
  56.     PopUpClock clock;                   // create clock instance
  57.  
  58.     switch (argc)
  59.     {
  60.       case 1:                           // invoked by CLOCK -- load it
  61.         cout << "Installing " << clock.name() << " in memory\n";
  62.         cout << "Press CTRL-SPACE to activate and deactivate\n";
  63.  
  64.         clock.run (TSR::CTRL + TSR::SPACE, 6);  // timeslice = 6 x 55 = 330ms
  65.                                                 // (error if this call returns)
  66.         cout << "Error installing " << clock.name() << ": "
  67.              << status_msg [clock.status()] << "\n";
  68.         break;
  69.  
  70.       case 2:                           // invoked by CLOCK/U -- unload it
  71.         if (argv[1][0] == '/' && toupper (argv[1][1]) == 'U')
  72.         {   cout << clock.name() << " "
  73.                  << unload_msg [clock.unload()] << "\n";
  74.             break;
  75.         }
  76.  
  77.       default:                          // give usage message otherwise
  78.         cout << "Usage: CLOCK [/U]\n";
  79.         return -1;
  80.     }
  81.     return clock.status ();
  82. }
  83.  
  84. //----- Resident part of TSR, executed every 6 clock ticks -----
  85. void PopUpClock::main (int hotkey)
  86. {
  87.     if (hotkey)
  88.         visible = !visible;                 // hotkey toggles visibility
  89.     if (!visible)
  90.         return;                             // nothing to do if invisible
  91.  
  92.     union REGS r;                           // get video mode
  93.     r.h.ah = 0x0F;
  94.     int86 (0x10, &r, &r);
  95.  
  96.     if (r.h.al > 3 && r.h.al != 7)          // exit if not text mode (0-3, 7)
  97.         return;
  98.  
  99.     struct time now;                        // get current time
  100.     gettime (&now);
  101.  
  102.     char hms [11];                          // format time into string
  103.     sprintf (hms, " %02d:%02d:%02d ",
  104.              now.ti_hour, now.ti_min, now.ti_sec);
  105.  
  106.     char far* screen =                      // get address of screen
  107.         (char far*) MK_FP(0xB800 + r.h.bh * 256, (r.h.ah - strlen(hms)) * 2);
  108.  
  109.     char* c = hms;                          // output formatted time
  110.     while (*c != '\0')
  111.     {   *screen++ = *c++;
  112.         *screen++ = 0x1E;
  113.     }
  114. }
  115.