home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 019.lha / Aterm / atermclock.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  10KB  |  325 lines

  1. /* AtermClock.c */
  2. /*
  3.  * Business first... This code is NOT to be used for commercial purposes.
  4.  * You are granted a limited license to distribute it on a not-for-profit
  5.  * basis by any means, including, but not limited to, paper, magnetic media,
  6.  * or telecommunications. We have no objection to its appearance on a 
  7.  * commercial communications network provided it may be obtained for no
  8.  * cost over and above the standard connect time charges.
  9.  *  In addition, we would appreciate it if anyone modifying this code
  10.  * would attempt to get the modifications back to us so that we can
  11.  * keep some semblance of continuity of versions.
  12.  *
  13.  * Jeff Lydiatt
  14.  * Larry Phillips, Compuserve, 76703,4322
  15.  *
  16.  * ATerm 7.0 Clock/Date (c)1987 by Jeff Lydiatt, Vancouver Canada
  17.  *
  18.  * This program runs as a sub-process called from the main terminal
  19.  * program.  The clock is located on the top line of the current window,
  20.  * and displays the current time, updated every second.  On the 15 and
  21.  * 45 second mark, the weekday is displayed.  The date is displayed on
  22.  * the even minute and half minute. This code is designed to mimic my favorite
  23.  * clock/calendar, "Klock" by Jack Radigan.  Wish I had his code when I did
  24.  * this, but as far as I know he hasn't released it.
  25.  *
  26.  *    This code is a separate process that must be loaded and initialized
  27.  * by the Aterm "ClockLoader" module.  This process waits for a message passed
  28.  * by the loader giving the address of a window where the clock is displayed.
  29.  * When the message from the loader is replied to, AtermClock then opens
  30.  * two separate timer devices.  The first is used to capture the current
  31.  * system date/time which AtermClock decodes for the display.  The second
  32.  * timer wakes AtermClock up every second so it can update the clock/calendar
  33.  * display.  A subsequent message passed from the ClockLoader module is
  34.  * assumed to be a shut down message which will cause the clock to wind up
  35.  * it's operation and release any resources it uses before "falling off the
  36.  * end of the world."
  37.  *
  38.  * Note: This code was compiled and tested under Aztec C version 3.02.   I've
  39.  * tried to keep the code clean so that it can also be compiled and linked
  40.  * with the Lattice Compiler and Amiga Loader.  I hasn't been tested under
  41.  * Lattice however.
  42.  */
  43.  
  44. #include     <exec/types.h>
  45. #include     <exec/memory.h>
  46. #include     <exec/ports.h>
  47. #include    <exec/tasks.h>
  48. #include    <exec/execbase.h>
  49. #include    <libraries/dosextens.h>
  50. #include     <functions.h>
  51. #include    <devices/timer.h>
  52. #include    <intuition/intuition.h>
  53. #include    <time.h>
  54. #include    "Clock.h"
  55.  
  56. #define RED          3 /* The color red            */
  57. #define BLACK          2 /* The color Black            */
  58. #define FPEN         RED /* Color of Clock Characters   */
  59. #define BPEN           BLACK /* BackGround Color for Clock  */
  60. #define STRLEN         14 /* Length of Clock Text String */
  61. #define CLOCKX        264 /* Pixel Column to start clock */
  62. #define CLOCKY          0 /* Top left Corner for the Clock */
  63. #define INTUITION_REV      0L
  64. #define GRAPHICS_REV       0L
  65.  
  66. struct GfxBase *GfxBase = NULL;
  67. struct IntuitionBase *IntuitionBase = NULL;
  68.  
  69. static struct timerequest timeReq, dateReq;
  70. static struct MsgPort *timePort = NULL;
  71. static struct MsgPort *datePort = NULL;
  72. static BOOL   timeOpened = FALSE;
  73. static BOOL   dateOpened = FALSE;
  74. static struct Window *myWindow; 
  75. static UBYTE  displayStr[STRLEN+1];
  76. static struct IntuiText iText =
  77.    { FPEN, BPEN, JAM2, 0, 0, NULL, displayStr, NULL };
  78. static struct Gadget ClockGadget =
  79.   {
  80.      NULL,        /* *NextGadget */
  81.      CLOCKX, CLOCKY, 0, 0,/* Leftedge, TopEdge, Width, Height */
  82.      NULL,        /* Flags */
  83.      TOPBORDER,        /* Activation Flags */
  84.      BOOLGADGET,    /* Gadget Type - This is the simplest */
  85.      NULL,        /* Gadget Render */
  86.      NULL,        /* Select Render */
  87.      &iText,        /* *GadgetText   */
  88.      NULL,        /* Mutual Exclude */
  89.      NULL,        /* SpecialInfo */
  90.      0,            /* Gadget Id */
  91.      NULL        /* Ptr to UserData */
  92.   };
  93. static BOOL gadgetON = FALSE;
  94.  
  95. /*-------------------------------------------------------------*/
  96. /*    OpenTimer: return TRUE if timer opened OK           */
  97. /*-------------------------------------------------------------*/
  98.  
  99. static BOOL OpenTimer()
  100. {
  101.    BOOL error;
  102.    register struct timerequest *t;
  103.    register struct MsgPort *port;
  104.       
  105.    /*---- Timer to let me wake up every second ----*/
  106.  
  107.    if ( (port = CreatePort( "Second.Timer", 0L)) == NULL )
  108.       return FALSE;
  109.    else
  110.       timePort = port;
  111.    t = &timeReq;
  112.    if (OpenDevice(TIMERNAME, UNIT_VBLANK, t, 0L) != 0)
  113.      {
  114.     DeletePort( port );
  115.     timePort = NULL;
  116.     return FALSE;
  117.      }
  118.    timeOpened = TRUE;
  119.  
  120.    /*---- Timer get the date ----*/
  121.  
  122.    if ( (port = CreatePort( "Date.Timer", 0L)) == NULL )
  123.       return FALSE;
  124.    else
  125.       datePort = port;
  126.  
  127.    t = &dateReq;
  128.    if (OpenDevice(TIMERNAME, UNIT_VBLANK, t, 0L) != 0)
  129.      {
  130.     DeletePort( port );
  131.     datePort = NULL;
  132.     return FALSE;
  133.      }
  134.    dateOpened = TRUE;
  135.  
  136.    return TRUE;
  137. }
  138.  
  139. /*-------------------------------------------------------------*/
  140. /*    CloseTimer: All Done with the timer.               */
  141. /*-------------------------------------------------------------*/
  142.  
  143. static void CloseTimer()
  144. {
  145.    if ( timeOpened )
  146.       CloseDevice( &timeReq );
  147.    if ( timePort != NULL )
  148.     DeletePort( timePort );
  149.    if ( dateOpened )
  150.       CloseDevice( &dateReq );
  151.    if ( datePort != NULL )
  152.     DeletePort( datePort );
  153. }
  154.  
  155.    /*------------------------------------------------------------*/
  156.    /*    display: display the given string in the current window. */
  157.    /*------------------------------------------------------------*/
  158.  
  159. static void display( str )
  160. register char *str;
  161. {
  162.    register int i;
  163.  
  164.    for ( i=0; i < STRLEN; i++ )
  165.        displayStr[i] = *str++;
  166.    displayStr[i] = '\0';
  167.  
  168.    if ( gadgetON == FALSE )
  169.      {
  170.     AddGadget( myWindow, &ClockGadget, (SHORT)(-1) );
  171.     gadgetON = TRUE;
  172.      } 
  173.    RefreshGadgets( &ClockGadget, myWindow, NULL );
  174. }
  175.  
  176.    /*------------------------------------------------------------*/
  177.    /*    DisplayCLock: Show date, time, or weekday from date.     */
  178.    /*------------------------------------------------------------*/
  179.  
  180. static void DisplayClock()
  181. {
  182.    char str[ STRLEN ];
  183.  
  184.    static char *month[] =
  185.      {
  186.     "???",
  187.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  188.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  189.  
  190.    static char *weekday[] =
  191.       { "    Sunday    ",
  192.         "    Monday    ",
  193.         "   Tuesday    ",
  194.         "   Wednesday  ",
  195.         "   Thursday   ",
  196.         "    Friday    ",
  197.         "    Saturday  "};
  198.  
  199.    unsigned long clock;
  200.    short mon, day, year, sec, min, hour, n;
  201.    register struct timerequest *t = &dateReq;
  202.    
  203.    t->tr_node.io_Command = TR_GETSYSTIME;
  204.    t->tr_node.io_Flags = IOF_QUICK;
  205.    t->tr_node.io_Error = 0;
  206.    t->tr_node.io_Message.mn_ReplyPort = datePort;
  207.    DoIO( t );
  208.    clock = t->tr_time.tv_secs + (t->tr_time.tv_micro + 500000) / 1000000;
  209.    sec = clock % 60; clock /= 60;
  210.    min = clock % 60; clock /= 60;
  211.    hour= clock % 24; clock /= 24;
  212.     
  213.    switch( sec )
  214.      {
  215.        case  1:
  216.        case 16:
  217.        case 31:   
  218.        case 46: break; /* do nothing */
  219.  
  220.        case 15:
  221.        case 45: /* Display day of Week */
  222.         display( weekday[ (int)(clock % 7) ] );
  223.            break;
  224.  
  225.        case  0:
  226.        case 30: /* Display Current Date */
  227.         n = (int)clock - 2251;
  228.         year = (4 * n + 3) / 1461;
  229.         n -= ( 1461 * year ) / 4;
  230.         year += 1984;
  231.         mon = ( 5 * n + 2 ) / 153;
  232.         day = n - ( 153 * mon + 2 ) / 5 + 1;
  233.         mon += 3;
  234.         if ( mon > 12)
  235.          {
  236.            year++;
  237.            mon -= 12;
  238.          }
  239.         if ( !( (1 <= mon) && (mon <= 12) ) )
  240.            mon = 0;  
  241.         sprintf( str, " %s %02d, %4d ", month[ mon ],
  242.                  day, year );
  243.         display( str );
  244.         break;
  245.  
  246.        default: /* Display the current Time */     
  247.         sprintf( str, "   %02d:%02d:%02d   ",
  248.         hour, min, sec);
  249.           display( str );
  250.       }
  251. }
  252.  
  253. /*-------------------------------------------------------------*/
  254. /*    StartTimer: launch the timer.                   */
  255. /*-------------------------------------------------------------*/
  256.  
  257. static void StartTimer()
  258. {
  259.    register struct timerequest *t = &timeReq;
  260.  
  261.    t->tr_time.tv_secs = 1;
  262.    t->tr_time.tv_micro = 0;
  263.    t->tr_node.io_Command = TR_ADDREQUEST;
  264.    t->tr_node.io_Error = 0;
  265.    t->tr_node.io_Message.mn_ReplyPort = timePort;
  266.    SendIO( t );
  267. }
  268.  
  269.    /*------------------------------------------------------------*/
  270.    /*        The Clock Task itself.                 */
  271.    /*------------------------------------------------------------*/
  272.  
  273. main()
  274. {
  275.    ULONG waitMask;
  276.    struct ParmMsg *msg;
  277.    struct MsgPort *myPort;
  278.    register struct Process *myProcess;
  279.    BOOL timerOpened = FALSE;
  280.  
  281.    /*--------Get my Process location, and tell it's Port. ----------*/
  282.  
  283.     myProcess = (struct Process *) FindTask(NULL); /* Addr of My Process */ 
  284.     myPort = &myProcess->pr_MsgPort;
  285.  
  286.    /*-------- Wait For Caller to Send Address of his window --------*/
  287.  
  288.     WaitPort( myPort );
  289.     msg = (struct ParmMsg * ) GetMsg( myPort );
  290.     myWindow = msg->windowPtr;
  291.     ReplyMsg( msg ); /* Tell Caller I got the window address */
  292.  
  293.     waitMask = 1L << myPort->mp_SigBit;
  294.     GfxBase = ( struct GfxBase * ) OpenLibrary("graphics.library",
  295.         GRAPHICS_REV);
  296.     IntuitionBase = ( struct IntuitionBase * ) OpenLibrary(
  297.         "intuition.library", INTUITION_REV );
  298.  
  299.     if ( GfxBase != NULL && IntuitionBase != NULL && OpenTimer() )
  300.       {
  301.     waitMask |= 1L << timePort->mp_SigBit;
  302.     StartTimer();
  303.     timerOpened = TRUE;
  304.       }
  305.  
  306.     while( (msg = (struct ParmMsg *)GetMsg( myPort )) == NULL )
  307.       {
  308.     Wait( waitMask );
  309.     if ( timerOpened && (CheckIO( &timeReq.tr_node ) != NULL) )
  310.       {
  311.          (void)GetMsg( timePort );
  312.          StartTimer();
  313.          DisplayClock();
  314.       }
  315.        }
  316.  
  317.    if ( GfxBase != NULL )
  318.       CloseLibrary( GfxBase );
  319.    if ( IntuitionBase != NULL )
  320.       CloseLibrary( IntuitionBase );
  321.    CloseTimer();
  322.    ReplyMsg( msg );
  323.    exit( 0 );
  324. }
  325.