home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / clock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  5.2 KB  |  222 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: clock.c,v 4.2 88/06/22 14:37:25 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/clock.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/clock.c,v $$Revision: 4.2 $";
  12.  
  13. /* get today's date */
  14.  
  15. #include <errno.h>
  16. #include <time.h>
  17. #include <signal.h>
  18. #include "term.h"
  19.  
  20. #define FONT        14            /* default font */
  21. #define POLL        15            /* poll interval */
  22. #define FIX(x)        ((x)>12?(x)-12:(x))
  23. #define dprintf        if (debug) fprintf
  24.  
  25. static char line[MAXLINE];
  26. static int x, y;        /* starting coord for clock */
  27. static int fired = 0;        /* alarm fired */
  28. static int armed = 0;        /* alarm armed */
  29. static int covered = 0;        /* window is covered */
  30. static int fx, fy;        /* font size */
  31. static int wx, wy;        /* window size */
  32. static char *alarm_time = NULL;
  33. static int debug;
  34.  
  35. static struct menu_entry enable[] = {
  36.     "ALARM CLOCK","",
  37.     "enable alarm","+\r",
  38. };
  39.  
  40. static struct menu_entry disable[] = {
  41.     "ALARM CLOCK","",
  42.     "disable alarm","-\r",
  43. };
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char **argv;
  48.    {
  49.    int update(), clean();
  50.    char *today();
  51.    char buff[MAXLINE];
  52.    int f_flag=0, s_flag=0, b_flag=0;
  53.    int font;
  54.    register int i;
  55.    char *getenv();
  56.  
  57.    ckmgrterm( *argv );
  58.  
  59.    debug = (getenv("DEBUG") != NULL);
  60.    for(i=1;i<argc;i++)
  61.       if (*argv[i] == '-') switch (argv[i][1]) {
  62.          case 'f':    /* -f<fontnumber>   select alternate font */
  63.          f_flag++;
  64.          font=atoi(argv[i]+2);
  65.          break;
  66.          case 'b':    /* -b   bury the window immediately */
  67.          b_flag++;
  68.          break;
  69.          case 's':    /* -s   don't resize window, center the display */
  70.          s_flag++;
  71.          break;
  72.          }
  73.       else {
  74.          alarm_time = argv[i];
  75.          dprintf(stderr,"Setting alarm time to: %s\n",alarm_time);
  76.          armed=1;
  77.          };
  78.  
  79.    m_setup(0);
  80.    signal(SIGALRM,update);
  81.    signal(SIGINT,clean);
  82.    signal(SIGTERM,clean);
  83.    m_ttyset();
  84.    if (armed) {
  85.       m_push(P_MENU|P_EVENT|P_FLAGS|P_FONT);
  86.       menu_load(1,2,enable);
  87.       menu_load(2,2,disable);
  88.       m_selectmenu(1+armed);
  89.       }
  90.    else
  91.       m_push(P_EVENT|P_FLAGS|P_FONT);
  92.    m_setmode(M_NOWRAP);
  93.    m_setmode(M_ABS);
  94.    m_setevent(BUTTON_1,"A\r");
  95.    m_setevent(BUTTON_1U,"X\r");
  96.    m_setevent(RESHAPE,"X\r");
  97.    m_setevent(REDRAW,"X\r");
  98.    m_setevent(COVERED,"C\r");
  99.    m_setevent(UNCOVERED,"E\r");
  100.  
  101.    if (f_flag)
  102.       m_font(font);
  103.    else
  104.       m_font(FONT);
  105.    if (!s_flag)
  106.       m_size(5,1);
  107.    if (b_flag)
  108.       m_clearmode(M_ACTIVATE);
  109.  
  110.    setxy();
  111.    update();
  112.    while (1) {
  113.       extern int   errno;
  114.  
  115.       errno = 0;
  116.       *line = '\0';
  117.       if (m_gets(line) == NULL  &&  errno  &&  errno != EINTR)
  118.          clean();
  119.       alarm(0);
  120.       dprintf(stderr,"Got: %s\n",line);
  121.       switch (*line) {
  122.          case 'C':            /* covered */
  123.             covered = 1;
  124.             break;
  125.          case 'E':            /* exposed */
  126.             covered = 0;
  127.             break;
  128.          case '+':            /* enable alarm */
  129.             dprintf(stderr,"Arming alarm\n");
  130.             armed = 1;
  131.             m_selectmenu(armed+1);
  132.             m_flush();
  133.             break;
  134.          case '-':            /* disable alarm */
  135.             dprintf(stderr,"Disarming alarm\n");
  136.             armed = 0;
  137.             fired = 0;
  138.             m_selectmenu(armed+1);
  139.             m_flush();
  140.             break;
  141.          case 'A':            /* show alarm time */
  142.             if (alarm_time) {
  143.                m_moveprint(x,y,alarm_time);
  144.                m_movecursor(wx+fx,y);
  145.                m_flush();
  146.                m_gets(line);
  147.                fired = 0;
  148.                }
  149.             break; 
  150.          case 'X':            /* redraw, reshape */
  151.             setxy();
  152.             break;
  153.          }
  154.       update();
  155.       }
  156.    }
  157.  
  158. int
  159. update()
  160.    {
  161.    char *today();
  162.    char *s = today();
  163.    m_moveprint(x,y,s);
  164.    m_movecursor(wx+fx,y);
  165.    if (fired || (armed && alarm_time && strcmp(alarm_time,s)==0)) {
  166.       dprintf(stderr,"Fireing alarm %s\n",fired?"":"FIRST TIME");
  167.       fired = 1;
  168.       if (covered) {
  169.          m_setmode(M_NOINPUT);
  170.          m_setmode(M_ACTIVATE);
  171.          }
  172.       m_setmode(M_WOB);
  173.       m_printstr("\007");
  174.       m_flush();
  175.       sleep(2);
  176.       m_clearmode(M_WOB);
  177.       if (covered) {
  178.          m_clearmode(M_ACTIVATE); 
  179.          m_clearmode(M_NOINPUT);
  180.          }
  181.       }
  182.    m_flush();
  183.    signal(SIGALRM,update);
  184.    alarm(POLL);
  185.    }
  186.    
  187. char * 
  188. today()
  189.    {
  190.    struct tm *tme, *localtime();
  191.    static char result[9];
  192.    long tmp,time();
  193.  
  194.    tmp = time(0);
  195.    tme = localtime(&tmp);
  196.    sprintf(result,"%02d:%02d",FIX(tme->tm_hour),tme->tm_min);
  197.    return(result);
  198.    }
  199.  
  200. clean()
  201.    {
  202.    m_pop();
  203.    m_ttyreset();
  204.    exit(1);
  205.    }
  206.  
  207. setxy()
  208.    {
  209.    char *today();
  210.  
  211.    get_font(&fx,&fy);
  212.    get_size(0,0,&wx,&wy);
  213.    x = (wx - strlen(today())*fx)/2;
  214.    y = fy + (wy - fy)/2;
  215.    if (x<0) x = 0;
  216.    if (y<0) y = 0;
  217.    m_clear();
  218.    dprintf(stderr,"Setting %d x %d at %d x %d in %d x %d\n",
  219.           strlen(today())*fx,fy,x,y,wx,wy);
  220.    m_flush();
  221.    }
  222.