home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 617b.lha / PublicService / PublicService.c < prev    next >
C/C++ Source or Header  |  1992-02-01  |  5KB  |  193 lines

  1.  
  2. /* 
  3.    This is a program to show a small clock on the workbench screen telling
  4.    you what to do during the day.
  5.  
  6.    This is totally public domain; do as you wish with the source and
  7.    executable. Would you pay money for it?
  8. */
  9.  
  10.  
  11.  
  12. struct NewWindow newwin = {
  13.     100,30,        /* window XY origin relative to TopLeft of screen */
  14.     460,11,        /* window width and height */
  15.     0,1,        /* detail and block pens */
  16.     CLOSEWINDOW+ACTIVEWINDOW+INACTIVEWINDOW,    /* IDCMP flags */
  17.     WINDOWCLOSE+RMBTRAP+WINDOWDRAG
  18.         +NOCAREREFRESH+SMART_REFRESH,            /* other window flags */
  19.     NULL,        /* first gadget in gadget list */
  20.     NULL,        /* custom CHECKMARK imagery */
  21.     NULL,        /* window title */
  22.     NULL,        /* custom screen pointer */
  23.     NULL,        /* custom bitmap */
  24.     5,5,        /* minimum width and height */
  25.     640,256,    /* maximum width and height */
  26.     WBENCHSCREEN    /* destination screen type */
  27. };
  28.  
  29. struct MsgPort *timeport;
  30. struct timerequest timereq;
  31. void *IntuitionBase, *GfxBase;
  32. struct IntuiMessage *msg;
  33. struct Window *window;
  34. struct RastPort *rp;
  35. ULONG class;
  36. struct { int min,stop; } start,mtea,lunch,atea,quit;
  37.  
  38. main()
  39. {
  40.   readfile();
  41.   IntuitionBase = OpenLibrary("intuition.library",0);
  42.   GfxBase = OpenLibrary("graphics.library",0);
  43.  
  44.   window = OpenWindow(&newwin); rp = window->RPort;
  45.   SetWindowTitles(window,(void *)-1,"The Public Servant's Clock V1.0 by Michael Warner");
  46.   SetDrMd(rp,JAM1); 
  47.   drawtime();
  48.   timeport = CreatePort("clk_timeport",0);
  49.   OpenDevice("timer.device",0,&timereq,0);
  50.   timereq.tr_node.io_Message.mn_ReplyPort = timeport;
  51.   timereq.tr_node.io_Command=TR_ADDREQUEST;  
  52.   timereq.tr_node.io_Flags=0; timereq.tr_node.io_Error=0;
  53.   timereq.tr_time.tv_secs = 60; timereq.tr_time.tv_micro = 0;
  54.   SendIO(&timereq);
  55.  
  56.   FOREVER {
  57.     Wait(1<<window->UserPort->mp_SigBit|1<<timeport->mp_SigBit);
  58.     if (msg=GetMsg(window->UserPort)) {
  59.       class=msg->Class; ReplyMsg(msg);
  60.       switch (class) {
  61.         case ACTIVEWINDOW:
  62.         case INACTIVEWINDOW: drawtime(); break;
  63.         case CLOSEWINDOW: die("bye"); break;
  64.       }
  65.     }
  66.     else {
  67.       WaitIO(&timereq);        
  68.       drawtime();
  69.       timereq.tr_time.tv_secs = 60;
  70.       timereq.tr_time.tv_micro = 0;
  71.       SendIO(&timereq);
  72.     }
  73.   }
  74. }
  75.  
  76.  
  77. die(str)
  78.   char *str;
  79. {
  80.   if (strlen(str)) puts(str);
  81.   if (timeport) {
  82.     if (!CheckIO(&timereq)) {
  83.     AbortIO(&timereq); WaitIO(&timereq);
  84.     }
  85.     CloseDevice(&timereq);
  86.     DeletePort(timeport);
  87.   }
  88.   if (window) CloseWindow(window);
  89.   exit(0);
  90. }
  91.  
  92.  
  93. readfile()
  94. {
  95.   FILE *file;
  96.   int p1,p2,p3,p4;
  97.   char ch,str[80];
  98.  
  99.   file = fopen("s:PublicService.dat","r"); 
  100.   if (!file) die("Couldn't find s:PublicService.dat");
  101.  
  102.   while (!feof(file)) {
  103.     fgets(str,80,file);
  104.     sscanf(str,"%s %d.%d %d.%d\n",&ch,&p1,&p2,&p3,&p4);
  105.     p1 = p1*60+p2; p2 = p3*60+p4;    /* convert to minutes */
  106.     switch (ch) {
  107.       case 'S': start.min=p1;break;
  108.       case 'M': mtea.min=p1; mtea.stop=p2; break;
  109.       case 'L': lunch.min=p1; lunch.stop=p2; break;
  110.       case 'A': atea.min=p1; atea.stop=p2; break;
  111.       case 'Q': quit.min=p1; break;
  112.     }
  113.   }
  114.   fclose(file);
  115. }
  116.  
  117.  
  118. drawtime()        /* draw time in window */
  119. {
  120.   int min;
  121.   static struct DateStamp ds;
  122.   static char str[160];
  123.  
  124.   DateStamp((long *)&ds); min = ds.ds_Minute; 
  125.   str[0]=0;
  126.  
  127.   if (min<start.min) { drawstring("Are you here ALREADY?"); return(0); }
  128.   if (min<mtea.min) {
  129.     interval(str,mtea.min-min); strcat(str," until morning tea"); 
  130.     drawstring(str); return(0);
  131.   }
  132.   if (min<mtea.stop) { drawstring("You should be at morning tea!"); return(0); }
  133.   if (min<lunch.min) {
  134.     interval(str,lunch.min-min); strcat(str," until lunch"); 
  135.     drawstring(str); return(0);
  136.   }
  137.   if (min<lunch.stop) { drawstring("You should be at lunch!"); return(0); }
  138.   if (min<atea.min) {
  139.     interval(str,atea.min-min); strcat(str," until afternoon tea"); 
  140.     drawstring(str); return(0);
  141.   }
  142.   if (min<atea.stop) { drawstring("You should be at afternoon tea!"); return(0); }
  143.   if (min<quit.min) {
  144.     interval(str,quit.min-min); strcat(str," until knockoff time"); 
  145.     drawstring(str); return(0);
  146.   }
  147.   drawstring("Don't you have a home to go to?");  return(0);
  148. }
  149.  
  150. drawstring(str)
  151.   char *str;
  152. {
  153.   register int i;
  154.  
  155.   SetRast(rp,1); SetAPen(rp,0); 
  156.   Move(rp,4,7); Text(rp,str,strlen(str));
  157. }
  158.  
  159. char *units[20] = {" ","one","two","three","four","five",
  160.                   "six","seven","eight","nine","ten","eleven",
  161.                   "twelve","thirteen","fourteen","fifteen",
  162.                   "sixteen","seventeen","eighteen","nineteen"};
  163.  
  164. char *tens[6] = {" "," ","twenty","thirty","forty","fifty"};
  165.  
  166.  
  167. interval(str,minutes)
  168.   char *str; int minutes;
  169. {
  170.   register int hr,min;
  171.  
  172.   hr = minutes/60; min = minutes%60;
  173.  
  174.   if (hr) {
  175.     strcpy(str,units[hr]);
  176.     strcat(str," hour"); if (hr>1) strcat(str,"s");
  177.   }
  178.   if (min) {
  179.     if (hr) strcat(str," and ");
  180.     if (min<20) {
  181.       strcat(str,units[min]);
  182.     }
  183.     else {
  184.       strcat(str,tens[min/10]);
  185.       if (min%10) { 
  186.         strcat(str,"-"); strcat(str,units[min%10]); 
  187.       }
  188.     }
  189.     strcat(str," minute"); if (min>1) strcat(str,"s");
  190.   }
  191. }
  192.  
  193.