home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff298.lzh / DClock / DClock.c < prev    next >
C/C++ Source or Header  |  1989-12-29  |  10KB  |  424 lines

  1. /* DClock.c *****************************************************************
  2. *
  3. *    DClock --------    A  Dumb  Clock  Utility,  my  idea  of how a clock
  4. *            should really be like.
  5. *
  6. *            Since  I  own my Amiga I have known almost as many
  7. *            clock  utilities  as  there  are  bugs  in the ROM
  8. *            Operating  System  (sorry,  this was supposed to a
  9. *            joke).   None  really  satisfied  my  needs,  even
  10. *            though  a  lot of them had shelfloads of features.
  11. *            Their  windows always blocked the scrolling inside
  12. *            of  CLI  windows   which  didn't look very pretty.
  13. *            Sometimes  they  occupied a lot of memory, such as
  14. *            MachII.   Then  I  stumbled  upon  a MacIntosh and
  15. *            DMouse.   At  the  left  hand of a Mac display you
  16. *            will  find  a  small  clock  display.  Well, isn't
  17. *            there  a  Workbench  we  could  use to display our
  18. *            clock?   From  DMouse  I  borrowed  the very basic
  19. *            program  structure  (I  hope you don't mind Matt).
  20. *            To            avoid            the            slow
  21. *            Window/SMART_REFRESH/ScrollRaster()   interaction,
  22. *            the  clock  uses  the  barlayer  of  the Workbench
  23. *            screen.
  24. *            Once   trying   to  improve  the  handler  I  also
  25. *            incorporated   my   DisplayBeep.c  code  into  it.
  26. *            DisplayBeep  'beeps' both audible and visible, the
  27. *            old Intuition routine only flashed the display.  I
  28. *            also added the contents of my Click.c program.
  29. *
  30. *    Author --------    Olaf 'Olsen' Barthel, ED Electronic Design Hannover
  31. *            Brabeckstrasse 35
  32. *            D-3000 Hannover 71
  33. *
  34. *            Federal Republic of Germany
  35. *
  36. *    This  program  truly is in the PUBLIC DOMAIN.  Written on a cold
  37. *    and  damp  September  evening,  hoping the next morning would be
  38. *    better.
  39. *
  40. *    Compiled using Aztec C 3.6a, CygnusEd Professional & ARexx.
  41. *
  42. ****************************************************************************/
  43.  
  44.     /* These belong to ARP. */
  45.  
  46. #include <libraries/arpbase.h>
  47. #include <arpfunctions.h>
  48.  
  49. #include "DClock.h"
  50.  
  51.     /* ARP help messages. */
  52.  
  53. char *CLI_Template    = "QUIT/S,INFO/S,BEEP/K,CLICK/K,CLICKVOLUME/K,PRIORITY/K,TEXTCOLOUR/K,BACKCOLOUR/K,ALARM/K,ALARMTIME/K";
  54. char *CLI_Help        = "\nUsage: DClock [QUIT] [INFO] [BEEP ON|OFF] [CLICK ON|OFF] [CLICKVOLUME 0-64]\
  55. \n              [PRIORITY 0 - 127] [TEXTCOLOUR #] [BACKCOLOUR #]\n              [ALARM ON|OFF|INFO] [ALARMTIME HH:MM:SS]\n";
  56.  
  57.     /* Where to find the different arguments. */
  58.  
  59. #define ARG_QUIT    1
  60. #define ARG_INFO    2
  61. #define ARG_BEEP    3
  62. #define ARG_CLICK    4
  63. #define ARG_CLICKVOLUME    5
  64. #define ARG_PRIORITY    6
  65. #define ARG_TEXTCOLOUR    7
  66. #define ARG_BACKCOLOUR    8
  67. #define ARG_ALARM    9
  68. #define ARG_ALARMTIME    10
  69.  
  70.     /* Stub, don't need these. */
  71.  
  72. void _wb_parse() {}
  73. long Chk_Abort() { return(0); }
  74.  
  75.     /* main(argc,argv):
  76.      *
  77.      *    That's where all the trouble starts.
  78.      */
  79.  
  80. void
  81. main(argc,argv)
  82. long argc;
  83. char *argv[];
  84. {
  85.         /* Are we already running? */
  86.  
  87.     register struct DSeg *DSeg = (struct DSeg *)FindPort(PORTNAME);
  88.  
  89.         /* No argument and DClock's already running? */
  90.  
  91.     if(argc < 2 && DSeg)
  92.     {
  93.         Printf("Can't install DClock, handler process already running\7!\n");
  94.  
  95.         exit(0);
  96.     }
  97.  
  98.         /* User wants information. */
  99.  
  100.     if(argv[ARG_INFO])
  101.     {
  102.         Printf("\n\33[1m\33[33mDClock\33[0m\33[31m - A \33[33mD\33[31mumb \33[33mClock\33[31m. Renders time and date\n");
  103.         Printf("         into the Workbench title bar. Place\n");
  104.         Printf("         \33[33mDClock-Handler\33[31m in L:, \33[33mDClock\33[31m somewhere\n");
  105.         Printf("         in C: or in SYS:. Type '\33[1m\33[33mDClock\33[31m\33[0m' to\n");
  106.         Printf("         install, '\33[1m\33[33mDClock quit\33[31m\33[0m' to remove.\n\n");
  107.         Printf("         \33[33mDClock\33[31m is \33[1mfree\33[0m and in the \33[33m\33[1mPUBLIC-DOMAIN\33[31m\33[0m!\n\n");
  108.  
  109.         Printf("\33[1m\33[33mAuthor\33[31m\33[0m - Olaf Barthel of ED Electronic Design Hannover\n");
  110.         Printf("         Brabeckstrasse 35\n");
  111.         Printf("         D-3000 Hannover 71\n\n");
  112.  
  113.         Printf("     Federal Republic of Germany.\n\n");
  114.  
  115.         exit(0);
  116.     }
  117.  
  118.         /* Terminate dumb clock? */
  119.  
  120.     if(argv[ARG_QUIT])
  121.     {
  122.         Printf("Removing \33[1m\33[33mDClock\33[31m\33[0m, ");
  123.  
  124.             /* Segment not loaded. */
  125.  
  126.         if(!DSeg)
  127.         {
  128.             Printf("failed!\7\n");
  129.             exit(0);
  130.         }
  131.  
  132.             /* We are the caller. */
  133.  
  134.         DSeg -> Father = (struct Task *)FindTask(NULL);
  135.  
  136.             /* Child still present? */
  137.  
  138.         if(DSeg -> Child)
  139.         {
  140.             Signal(DSeg -> Child,SIGBREAKF_CTRL_D);
  141.             Wait(SIGBREAKF_CTRL_D);
  142.         }
  143.  
  144.             /* Remove port and associated data. */
  145.  
  146.         RemPort(&DSeg -> Port);
  147.         FreeMem(DSeg -> Port . mp_Node . ln_Name,sizeof(PORTNAME));
  148.  
  149.         if(DSeg -> Segment)
  150.             UnLoadPrg(DSeg -> Segment);
  151.  
  152.         FreeMem(DSeg,DSeg -> SegSize);
  153.  
  154.         Printf("OK.\n");
  155.  
  156.         exit(0);
  157.     }
  158.  
  159.         /* Create global communication structure. */
  160.  
  161.     if(!DSeg)
  162.     {
  163.         if(DSeg = (struct DSeg *)AllocMem(sizeof(struct DSeg),MEMF_PUBLIC | MEMF_CLEAR))
  164.         {
  165.             ULONG Micros;
  166.  
  167.                 /* Dummy MessagePort. */
  168.  
  169.             DSeg -> Port . mp_Flags        = PA_IGNORE;
  170.             DSeg -> Port . mp_Node . ln_Pri    = 0;
  171.             DSeg -> Port . mp_Node . ln_Type= NT_MSGPORT;
  172.             DSeg -> Port . mp_Node . ln_Name= AllocMem(sizeof(PORTNAME),MEMF_PUBLIC);
  173.             DSeg -> Child            = NULL;
  174.  
  175.                 /* For future expansion of this structure,
  176.                  * this will insure that any version of
  177.                  * DClock will be able to free the memory
  178.                  * occupied by the segment.
  179.                  */
  180.  
  181.             DSeg -> SegSize            = sizeof(struct DSeg);
  182.  
  183.                 /* Activate beep and click feature. */
  184.  
  185.             DSeg -> Beep            = TRUE;
  186.             DSeg -> Click            = TRUE;
  187.  
  188.                 /* So we have valid time counter. */
  189.  
  190.             CurrentTime(&DSeg -> LastSecs,&Micros);
  191.  
  192.                 /* Click volume and handler priority. */
  193.  
  194.             DSeg -> ClickVolume        = 64;
  195.             DSeg -> Priority        = 5;
  196.  
  197.                 /* Rendering colours. */
  198.  
  199.             DSeg -> TextColour        = 0;
  200.             DSeg -> BackColour        = 1;
  201.  
  202.             DSeg -> Alarm            = FALSE;
  203.  
  204.             DSeg -> AlarmHour        = 12;
  205.             DSeg -> AlarmMinute        = 0;
  206.             DSeg -> AlarmSecond        = 0;
  207.  
  208.                 /* Install the current revision number. */
  209.  
  210.             DSeg -> Revision        = REVISION;
  211.  
  212.                 /* Changed handler priority. */
  213.  
  214.             if(argv[ARG_PRIORITY])
  215.                 Printf("DClock: Handler priority set to %ld.\n",DSeg -> Priority = Atol(argv[ARG_PRIORITY]));
  216.  
  217.                 /* Init port. */
  218.  
  219.             strcpy(DSeg -> Port . mp_Node . ln_Name,PORTNAME);
  220.  
  221.             NewList(&DSeg -> Port . mp_MsgList);
  222.  
  223.             Printf("Installing \33[33m\33[1mDClock\33[0m\33[31m, ");
  224.  
  225.                 /* Load the handler code. */
  226.  
  227.             DSeg -> Segment = LoadPrg("DClock-Handler");
  228.  
  229.             if(!DSeg -> Segment)
  230.                 DSeg -> Segment = LoadPrg("L:DClock-Handler");
  231.  
  232.             if(!DSeg -> Segment)
  233.             {
  234.                 Printf("unable to find \33[33mL:DClock-Handler\33[31m\7!\n");
  235.  
  236.                 FreeMem(DSeg -> Port . mp_Node . ln_Name,sizeof(PORTNAME));
  237.             }
  238.             else
  239.             {
  240.                     /* Install the port and start the handler. */
  241.  
  242.                 AddPort(&DSeg -> Port);
  243.  
  244.                 CreateProc("DClock-Handler",DSeg -> Priority,DSeg -> Segment,4096);
  245.  
  246.                 Printf("OK. \33[33mDClock v1.%ld\33[31m, by ED Hannover. \33[1mPUBLIC DOMAIN\33[0m.\n",REVISION);
  247.             }
  248.         }
  249.     }
  250.  
  251.         /* Change click volume. */
  252.  
  253.     if(argv[ARG_CLICKVOLUME] && DSeg)
  254.     {
  255.         if(Atol(argv[ARG_CLICKVOLUME]) > 0)
  256.             Printf("DClock: Click volume set to %ld.\n",DSeg -> ClickVolume = Atol(argv[ARG_CLICKVOLUME]));
  257.         else
  258.             Puts(CLI_Help);
  259.     }
  260.  
  261.         /* Change handler priority? */
  262.  
  263.     if(argv[ARG_PRIORITY] && DSeg)
  264.     {
  265.         if(DSeg -> Child)
  266.         {
  267.             Printf("DClock: Handler priority set to %ld.\n",DSeg -> Priority = Atol(argv[ARG_PRIORITY]));
  268.  
  269.             SetTaskPri(DSeg -> Child,DSeg -> Priority);
  270.         }
  271.         else
  272.             Puts("DClock: Unable to find handler task.");
  273.     }
  274.  
  275.         /* Turn beeping on/off? */
  276.  
  277.     if(argv[ARG_BEEP] && DSeg)
  278.     {
  279.         if(!Strcmp(argv[ARG_BEEP],"OFF"))
  280.         {
  281.             Puts("DClock: Beep disabled.");
  282.  
  283.             DSeg -> Beep = FALSE;
  284.         }
  285.         else
  286.         {
  287.             if(!Strcmp(argv[ARG_BEEP],"ON"))
  288.             {
  289.                 Puts("DClock: Beep enabled.");
  290.  
  291.                 DSeg -> Beep = TRUE;
  292.             }
  293.             else
  294.                 Puts(CLI_Help);
  295.         }
  296.     }
  297.  
  298.         /* Turn clicking on/off? */
  299.  
  300.     if(argv[ARG_CLICK])
  301.     {
  302.         if(!Strcmp(argv[ARG_CLICK],"OFF"))
  303.         {
  304.             Puts("DClock: Click disabled.");
  305.  
  306.             DSeg -> Click = FALSE;
  307.         }
  308.         else
  309.         {
  310.             if(!Strcmp(argv[ARG_CLICK],"ON"))
  311.             {
  312.                 Puts("DClock: Click enabled.");
  313.  
  314.                 DSeg -> Click = TRUE;
  315.             }
  316.             else
  317.                 Puts(CLI_Help);
  318.         }
  319.     }
  320.  
  321.         /* Select new front colour? */
  322.  
  323.     if(argv[ARG_TEXTCOLOUR])
  324.     {
  325.         DSeg -> TextColour = Atol(argv[ARG_TEXTCOLOUR]);
  326.  
  327.         Printf("DClock: Text colour set to %ld.\n",DSeg -> TextColour);
  328.     }
  329.  
  330.         /* Select new background colour? */
  331.  
  332.     if(argv[ARG_BACKCOLOUR])
  333.     {
  334.         DSeg -> BackColour = Atol(argv[ARG_BACKCOLOUR]);
  335.  
  336.         Printf("DClock: Background colour set to %ld.\n",DSeg -> BackColour);
  337.     }
  338.  
  339.         /* Set/check alarm status. */
  340.  
  341.     if(argv[ARG_ALARM])
  342.     {
  343.         if(!Strcmp(argv[ARG_ALARM],"OFF"))
  344.         {
  345.             Puts("DClock: Alarm disabled.");
  346.  
  347.             DSeg -> Alarm = FALSE;
  348.         }
  349.         else
  350.         {
  351.             if(!Strcmp(argv[ARG_ALARM],"ON"))
  352.             {
  353.                 Puts("DClock: Alarm enabled.");
  354.  
  355.                 DSeg -> Alarm = TRUE;
  356.             }
  357.             else
  358.             {
  359.                 if(!Strcmp(argv[ARG_ALARM],"INFO"))
  360.                     Printf("DClock: Current alarm time is %02ld:%02ld:%02ld.\n",DSeg -> AlarmHour,DSeg -> AlarmMinute,DSeg -> AlarmSecond);
  361.                 else
  362.                     Puts(CLI_Help);
  363.             }
  364.         }
  365.     }
  366.  
  367.         /* Adjust alarm time. */
  368.  
  369.     if(argv[ARG_ALARMTIME])
  370.     {
  371.         char TimeBuff[3];
  372.         register long i,TheTime;
  373.  
  374.         TimeBuff[2] = 0;
  375.  
  376.         if(strlen(argv[ARG_ALARMTIME]) != 8)
  377.         {
  378.             Puts("DClock: Alarm time format = HH:MM:SS, example: 09:03:07.");
  379.             exit(10);
  380.         }
  381.  
  382.         TimeBuff[0] = argv[ARG_ALARMTIME][0];
  383.         TimeBuff[1] = argv[ARG_ALARMTIME][1];
  384.  
  385.         TheTime = Atol(TimeBuff);
  386.  
  387.         if(TheTime < 0 || TheTime > 23)
  388.         {
  389.             Puts("DClock: Illegal time value, Hours must be within 0 ... 23.");
  390.             exit(10);
  391.         }
  392.  
  393.         DSeg -> AlarmHour    = TheTime;
  394.  
  395.         TimeBuff[0] = argv[ARG_ALARMTIME][3];
  396.         TimeBuff[1] = argv[ARG_ALARMTIME][4];
  397.  
  398.         TheTime = Atol(TimeBuff);
  399.  
  400.         if(TheTime < 0 || TheTime > 59)
  401.         {
  402.             Puts("DClock: Illegal time value, Minutes must be within 0 ... 59.");
  403.             exit(10);
  404.         }
  405.  
  406.         DSeg -> AlarmMinute    = TheTime;
  407.  
  408.         TimeBuff[0] = argv[ARG_ALARMTIME][6];
  409.         TimeBuff[1] = argv[ARG_ALARMTIME][7];
  410.  
  411.         TheTime = Atol(TimeBuff);
  412.  
  413.         if(TheTime < 0 || TheTime > 59)
  414.         {
  415.             Puts("DClock: Illegal time value, Seconds must be within 0 ... 59.");
  416.             exit(10);
  417.         }
  418.  
  419.         DSeg -> AlarmSecond    = TheTime;
  420.  
  421.         Printf("DClock: Alarm time set to %02ld:%02ld:%02ld.\n",DSeg -> AlarmHour,DSeg -> AlarmMinute,DSeg -> AlarmSecond);
  422.     }
  423. }
  424.