home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 465.lha / Clock / clock.c < prev    next >
C/C++ Source or Header  |  1991-01-05  |  5KB  |  183 lines

  1.  
  2. /************************************************************************
  3.  *                                    *
  4.  * Clock v1.4                                *
  5.  *                                    *
  6.  *   © Stuart Mitchell - November 1990                    *
  7.  *                                    *
  8.  *   A simple clock with optional pop-to-front & memory display     *
  9.  *                                    *
  10.  * NB: 1) Should be linked with cback.o instead of c.o to provide a LSR *
  11.  *    program which then allows original CLI to be closed.        *
  12.  *     2) To enable pop-to-front compile with -dPOPUP option        *
  13.  *     3) To enable memory display compile with -dMEMCLOCK option    *
  14.  *                                    *
  15.  * v1.0 - Initial release                        *
  16.  * v1.1 - fixed timing bug (minclock woken up 4 times too often!!)      *
  17.  * v1.2 - Added code to call RawDoFmt                    *
  18.  * v1.3 - Fixed memory loss bug (call _exit instead of Exit)            *
  19.  * v1.4 - Altered to use ARP's resource tracking,cached libraries...    *
  20.  *                                    *
  21.  ************************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <exec/types.h>
  25. #include <exec/exec.h>
  26. #include <devices/timer.h>
  27. #include <intuition/intuition.h>
  28. #include <intuition/intuitionbase.h>
  29. #include <libraries/arpbase.h>
  30. #include <proto/arp.h>
  31. #include <proto/dos.h>
  32. #include <proto/intuition.h>
  33. #include <proto/graphics.h>
  34. #include <proto/exec.h>
  35.  
  36. void do_exit(int);
  37.  
  38. #ifdef MEMCLOCK
  39. #define PROGNAME "Memclock v1.4"
  40. #else
  41. #define PROGNAME "MinClock v1.4"
  42. #endif
  43.  
  44. long _stack=2000;            /* Definitions for cback.o */
  45. char *_procname=PROGNAME;
  46. long _priority=0;
  47. long _BackGroundIO=0;
  48.  
  49. static struct IntuitionBase    *IntuitionBase;
  50. static struct GfxBase           *GfxBase;
  51. struct ArpBase               *ArpBase;
  52. static struct Window           *Window;
  53. static struct timerequest      Time_Req;
  54. static struct MsgPort           *Timer_Port;
  55. static struct DateStamp        Date;
  56. static struct IntuiMessage     *Msg;
  57.  
  58. char date[LEN_DATSTRING],time[LEN_DATSTRING],day[LEN_DATSTRING];
  59. static struct DateTime datetime={ {0},FORMAT_CDN,0,NULL,date,time };
  60.  
  61. #ifdef MEMCLOCK
  62. #define BUFSIZE         32
  63. #define WIDTH           250
  64. #define XPOS            337
  65. #else
  66. #define BUFSIZE         18
  67. #define WIDTH           140
  68. #define XPOS            447
  69. #endif
  70.  
  71. static BYTE Buffer[26];        /* holds time string */
  72.  
  73. static struct NewWindow nw={
  74.   XPOS,0,WIDTH,10,2,1,CLOSEWINDOW,SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG,
  75.   NULL,NULL,(UBYTE *)PROGNAME,NULL,NULL,0,0,0,0,WBENCHSCREEN };
  76.  
  77. static struct IntuiText Date_Text={
  78.   2,1,JAM2,0,0,NULL,Buffer,NULL };
  79.  
  80. void _main()
  81. {
  82. #ifdef POPUP
  83.   long lock;
  84. #endif
  85.   if(!(ArpBase=(struct ArpBase *)OpenLibrary("arp.library",39)))
  86.     do_exit(1);
  87.  
  88.   IntuitionBase=(struct IntuitionBase *)ArpBase->IntuiBase;
  89.   GfxBase=(struct GfxBase *)ArpBase->GfxBase;
  90.  
  91.   if(Getenv("dateformat",Buffer,5)==NULL)
  92.     datetime.dat_Format=FORMAT_DOS;
  93.   else
  94.     datetime.dat_Format=Atol(Buffer);
  95.  
  96.   if(datetime.dat_Format==FORMAT_DOS) {
  97.     nw.LeftEdge-=8;
  98.     nw.Width+=8;
  99.   };
  100.  
  101.   if((Timer_Port=CreatePort("Timer Port",0))==NULL)
  102.     do_exit(1);
  103.  
  104.   if(OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)&Time_Req,0)!=NULL)
  105.     do_exit(1) ;
  106.  
  107.   Time_Req.tr_node.io_Message.mn_ReplyPort=Timer_Port;
  108.   Time_Req.tr_node.io_Command=TR_ADDREQUEST;
  109.   Time_Req.tr_node.io_Flags=0;
  110.   Time_Req.tr_node.io_Error=0;
  111.  
  112.   if((Window=OpenWindow((struct NewWindow *)&nw))==NULL)
  113.     do_exit(20);
  114.  
  115.   SetRast(Window->RPort,1);
  116.  
  117.   for(;;) {
  118.  
  119. /* calculate time from system clock */
  120.  
  121.     DateStamp((struct DateStamp *)&datetime.dat_Stamp);
  122.     StamptoStr(&datetime);
  123.  
  124. #ifdef MEMCLOCK
  125.     SPrintf(Buffer,"C:%04ld F:%04ld %s %s",AvailMem(MEMF_CHIP)>>10,
  126.             AvailMem(MEMF_FAST)>>10,date,time);
  127. #else
  128.     SPrintf(Buffer,"%s %s",date,time);
  129. #endif
  130.  
  131.     PrintIText(Window->RPort,&Date_Text,2,1);   /* put into window */
  132.  
  133. /* NB : Window only 'pops to front' if Workbench screen is also at the
  134.   front, otherwise Dpaint (& others?) get very upset */
  135.  
  136. #ifdef POPUP
  137.     lock=LockIBase(0);
  138.     if(IntuitionBase->FirstScreen==Window->WScreen)
  139.       WindowToFront(Window);
  140.     UnlockIBase(lock);
  141. #endif
  142.  
  143. /* set up next wakeup time */
  144.  
  145.     Time_Req.tr_time.tv_secs=1;
  146.     Time_Req.tr_time.tv_micro=0;
  147.     SendIO(&Time_Req.tr_node);
  148.  
  149. /* sleep until woken either by Close Gadget selection or timer event */
  150.  
  151.     Wait(1<<Window->UserPort->mp_SigBit | 1<<Timer_Port->mp_SigBit);
  152.  
  153.     while((Msg=(struct IntuiMessage *)GetMsg(Window->UserPort))!=NULL) {
  154.       if(Msg->Class==CLOSEWINDOW) {
  155.     ReplyMsg((struct Message *)Msg);
  156.     do_exit(0);
  157.       }
  158.       ReplyMsg((struct Message *)Msg);
  159.     }
  160.     GetMsg(Timer_Port);         /* must be timer wakeup ... */
  161.   }
  162. }
  163.  
  164. /*********************************************************************/
  165.  
  166. void do_exit(code)
  167. int code;
  168. {
  169.   AbortIO(&Time_Req.tr_node);
  170.   if(Window)
  171.     CloseWindow(Window);
  172.   if(Time_Req.tr_node.io_Message.mn_ReplyPort)
  173.     CloseDevice((struct IORequest *)&Time_Req);
  174.   if(Timer_Port)
  175.     DeletePort((struct MsgPort *)Timer_Port);
  176.   if(ArpBase)
  177.     CloseLibrary((struct Library *)ArpBase);
  178.   _exit(code);
  179. }
  180.  
  181. void MemCleanup() {}
  182.  
  183.