home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.5 / Examples / Workbench / ClockIcon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-30  |  7.2 KB  |  288 lines

  1. /*
  2.  * COPYRIGHT:
  3.  *
  4.  *   Unless otherwise noted, all files are Copyright (c) 1999 Amiga, Inc.
  5.  *   All rights reserved.
  6.  *
  7.  * DISCLAIMER:
  8.  *
  9.  *   This software is provided "as is". No representations or warranties
  10.  *   are made with respect to the accuracy, reliability, performance,
  11.  *   currentness, or operation of this software, and all use is at your
  12.  *   own risk. Neither Amiga nor the authors assume any responsibility
  13.  *   or liability whatsoever with respect to your use of this software.
  14.  */
  15.  
  16. #include <exec/libraries.h>
  17. #include <devices/timer.h>
  18. #include <intuition/intuition.h>
  19. #include <utility/date.h>
  20. #include <workbench/workbench.h>
  21. #include <dos/dos.h>
  22. #include <stdarg.h>
  23.  
  24. #include <clib/graphics_protos.h>
  25. #include <clib/intuition_protos.h>
  26. #include <clib/utility_protos.h>
  27. #include <clib/timer_protos.h>
  28. #include <clib/exec_protos.h>
  29. #include <clib/dos_protos.h>
  30. #include <clib/wb_protos.h>
  31.  
  32. #include <pragmas/graphics_pragmas.h>
  33. #include <pragmas/intuition_pragmas.h>
  34. #include <pragmas/utility_pragmas.h>
  35. #include <pragmas/timer_pragmas.h>
  36. #include <pragmas/exec_sysbase_pragmas.h>
  37. #include <pragmas/dos_pragmas.h>
  38. #include <pragmas/wb_pragmas.h>
  39.  
  40. /****************************************************************************/
  41.  
  42. #define OK (0)
  43.  
  44. /****************************************************************************/
  45.  
  46. void kprintf(const char *,...);
  47.  
  48. /****************************************************************************/
  49.  
  50. extern struct Library * IntuitionBase;
  51. extern struct Library * GfxBase;
  52. extern struct Library * UtilityBase;
  53. extern struct Library * WorkbenchBase;
  54. extern struct Library * SysBase;
  55.  
  56. /****************************************************************************/
  57.  
  58. struct Device * TimerBase;
  59. struct MsgPort * TimePort;
  60. struct timerequest * TimeRequest;
  61.  
  62. /****************************************************************************/
  63.  
  64. UWORD __chip ClockI1Data[] =
  65. {
  66.     /* Plane 0 */
  67.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x8000,0x8000,0x0000,0x0000,
  68.     0x0000,0x0000,0x8000,0x0000,0x0000,0x0000,0x0000,0x8000,
  69.     0x0000,0x0000,0x0000,0x0000,0x8000,0x0000,0x0000,0x0000,
  70.     0x0000,0x8000,0x0000,0x0000,0x0000,0x0000,0x8000,0x0000,
  71.     0x0000,0x0000,0x0000,0x8000,0x0000,0x0000,0x0000,0x0000,
  72.     0x8000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  73.     0x0000,0x0000,
  74.  
  75.     /* Plane 1 */
  76.     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  77.     0x0000,0x4000,0x0000,0x0000,0x0000,0x0000,0x4000,0x0000,
  78.     0x0000,0x0000,0x0000,0x4000,0x0000,0x0000,0x0000,0x0000,
  79.     0x4000,0x0000,0x0000,0x0000,0x0000,0x4000,0x0000,0x0000,
  80.     0x0000,0x0000,0x4000,0x0000,0x0000,0x0000,0x0000,0x4000,
  81.     0x0000,0x0000,0x0000,0x0000,0x4000,0x7FFF,0xFFFF,0xFFFF,
  82.     0xFFFF,0xC000
  83. };
  84.  
  85. struct Image ClockI1 =
  86. {
  87.     0, 0,        /* Upper left corner */
  88.     66, 10, 2,    /* Width, Height, Depth */
  89.     ClockI1Data,    /* Image data */
  90.     0x0003, 0x0000,    /* PlanePick, PlaneOnOff */
  91.     NULL        /* Next image */
  92. };
  93.  
  94. struct DiskObject Clock =
  95. {
  96.     WB_DISKMAGIC,        /* Magic Number */
  97.     WB_DISKVERSION,        /* Version */
  98.     {            /* Embedded Gadget Structure */
  99.         NULL,            /* Next Gadget Pointer */
  100.         0, 0, 66, 11,        /* Left,Top,Width,Height */
  101.         GFLG_GADGIMAGE | GFLG_GADGHCOMP,    /* Flags */
  102.         GACT_RELVERIFY,        /* Activation Flags */
  103.         BOOLGADGET,        /* Gadget Type */
  104.         (APTR)&ClockI1,        /* Render Image */
  105.         NULL,            /* Select Image */
  106.         NULL,            /* Gadget Text */
  107.         NULL,            /* Mutual Exclude */
  108.         NULL,            /* Special Info */
  109.         0,            /* Gadget ID */
  110.         (APTR) 0x0001,        /* User Data (Revision) */
  111.     },
  112.     WBTOOL,            /* Icon Type */
  113.     NULL,            /* Default Tool */
  114.     NULL,            /* Tool Type Array */
  115.     NO_ICON_POSITION,    /* Current X */
  116.     NO_ICON_POSITION,    /* Current Y */
  117.     NULL,            /* Drawer Structure */
  118.     NULL,            /* Tool Window */
  119.     0            /* Stack Size */
  120. };
  121.  
  122. /****************************************************************************/
  123.  
  124. struct TextFont * TopazFont;
  125.  
  126. struct TextAttr Topaz =
  127. {
  128.     "topaz.font",
  129.     8,
  130.     FS_NORMAL,
  131.     FPF_ROMFONT|FPF_DESIGNED
  132. };
  133.  
  134. /****************************************************************************/
  135.  
  136. VOID
  137. SPrintf(STRPTR buffer, STRPTR formatString,...)
  138. {
  139.     va_list varArgs;
  140.  
  141.     va_start(varArgs,formatString);
  142.     RawDoFmt(formatString,varArgs,(VOID (*)())"\x16\xC0\x4E\x75",buffer);
  143.     va_end(varArgs);
  144. }
  145.  
  146. /****************************************************************************/
  147.  
  148. LONG __saveds __asm __interrupt
  149. ClockHookFunc(
  150.     register __a0 struct Hook *        hook,
  151.     register __a2 APTR            unused,
  152.     register __a1 struct AppIconRenderMsg *    arm)
  153. {
  154.     struct timeval now;
  155.     struct ClockData clock;
  156.     struct RastPort rp = (*arm->arm_RastPort);
  157.     UBYTE buffer[20];
  158.     LONG left,top;
  159.  
  160.     GetSysTime(&now);
  161.  
  162.     Amiga2Date(now.tv_secs,&clock);
  163.  
  164.     SPrintf(buffer,"%02ld:%02ld:%02ld",clock.hour,clock.min,clock.sec);
  165.  
  166.     SetFont(&rp,TopazFont);
  167.     SetABPenDrMd(&rp,2,1,JAM2);
  168.  
  169.     left = arm->arm_Left;
  170.     top = arm->arm_Top;
  171.  
  172.     RectFill(&rp,left,top,
  173.         left + arm->arm_Width - 1,
  174.         top + arm->arm_Height - 1);
  175.  
  176.     SetAPen(&rp,2);
  177.     Move(&rp,left + 1,top + 1 + rp.TxBaseline);
  178.     Text(&rp,buffer,8);
  179.  
  180.     return(FALSE);
  181. }
  182.  
  183. struct Hook ClockHook =
  184. {
  185.     {NULL},
  186.     (HOOKFUNC)ClockHookFunc
  187. };
  188.  
  189. /****************************************************************************/
  190.  
  191. int
  192. main(int argc,char ** argv)
  193. {
  194.     TimePort = CreateMsgPort();
  195.     if(TimePort != NULL)
  196.     {
  197.         TimeRequest = (struct timerequest *)CreateIORequest(TimePort,sizeof(*TimeRequest));
  198.         if(TimeRequest != NULL)
  199.         {
  200.             if(OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)TimeRequest,0) == OK)
  201.             {
  202.                 TimerBase = TimeRequest->tr_node.io_Device;
  203.  
  204.                 if(WorkbenchBase->lib_Version >= 44)
  205.                 {
  206.                     TopazFont = OpenFont(&Topaz);
  207.                     if(TopazFont != NULL)
  208.                     {
  209.                         struct MsgPort * appPort;
  210.  
  211.                         appPort = CreateMsgPort();
  212.                         if(appPort != NULL)
  213.                         {
  214.                             struct AppIcon * appIcon;
  215.                             struct Message * msg;
  216.  
  217.                             appIcon = AddAppIcon(0,0,"Clock",appPort,0,&Clock,
  218.                                 WBAPPICONA_RenderHook,&ClockHook,
  219.                             TAG_DONE);
  220.                             if(appIcon != NULL)
  221.                             {
  222.                                 ULONG signals;
  223.                                 BOOL done = FALSE;
  224.  
  225.                                 TimeRequest->tr_node.io_Command    = TR_ADDREQUEST;
  226.                                 TimeRequest->tr_time.tv_secs    = 1;
  227.                                 TimeRequest->tr_time.tv_micro    = 0;
  228.                                 SendIO((struct IORequest *)TimeRequest);
  229.  
  230.                                 do
  231.                                 {
  232.                                     signals = Wait(SIGBREAKF_CTRL_C | (1UL << TimePort->mp_SigBit) | (1UL << appPort->mp_SigBit));
  233.  
  234.                                     if(signals & (1UL << TimePort->mp_SigBit))
  235.                                     {
  236.                                         WaitIO((struct IORequest *)TimeRequest);
  237.  
  238.                                         WorkbenchControl(NULL,
  239.                                             WBCTRLA_RedrawAppIcon,appIcon,
  240.                                         TAG_DONE);
  241.  
  242.                                         TimeRequest->tr_node.io_Command    = TR_ADDREQUEST;
  243.                                         TimeRequest->tr_time.tv_secs    = 1;
  244.                                         TimeRequest->tr_time.tv_micro    = 0;
  245.                                         SendIO((struct IORequest *)TimeRequest);
  246.                                     }
  247.  
  248.                                     if(signals & (1UL << appPort->mp_SigBit))
  249.                                     {
  250.                                         while((msg = GetMsg(appPort)) != NULL)
  251.                                             ReplyMsg(msg);
  252.                                     }
  253.  
  254.                                     if(signals & SIGBREAKF_CTRL_C)
  255.                                     {
  256.                                         done = TRUE;
  257.                                     }
  258.                                 }
  259.                                 while(NOT done);
  260.  
  261.                                 RemoveAppIcon(appIcon);
  262.                             }
  263.  
  264.                             AbortIO((struct IORequest *)TimeRequest);
  265.                             WaitIO((struct IORequest *)TimeRequest);
  266.  
  267.                             while((msg = GetMsg(appPort)) != NULL)
  268.                                 ReplyMsg(msg);
  269.  
  270.                             DeleteMsgPort(appPort);
  271.                         }
  272.  
  273.                         CloseFont(TopazFont);
  274.                     }
  275.                 }
  276.  
  277.                 CloseDevice((struct IORequest *)TimeRequest);
  278.             }
  279.  
  280.             DeleteIORequest((struct IORequest *)TimeRequest);
  281.         }
  282.  
  283.         DeleteMsgPort(TimePort);
  284.     }
  285.  
  286.     return(0);
  287. }
  288.