#ifndef __INC_POS_PCOM_EXT_CLOCK_C
#define __INC_POS_PCOM_EXT_CLOCK_C
/*******************************************************************
 $CRT 17 Oct 1996 : hp

 $AUT Holger Burkarth
 $DAT >>Clock.c<<   29 Jan 1997    13:22:27 - (C) ProDAD
*******************************************************************/

//##ex mcpp:cppc -gs -o pos:pos/Ex/Clock p:pLib/StartCode.o p:/pOS_RKRM/pIntui/Clock.c p:pLib/StdIO.o -l pOSStub -l pOS

/***********************************************************
  pOS programing example - Copyright (C) 1995-97 proDAD

  This code was written as an easy to understand example,
  how to program pOS features. It is provided 'as-is',
  without any express or implied warranty.

  Permission is hereby granted to use, copy and modify
  this source code for any purpose, without fee, subject
  to the following conditions:

    (1) This notice may not be removed or altered from any
        source distribution.

    (2) Altered source versions must be plainly marked as
        such, and must not be misrepresented as being
        the original source code.

    (3) If only executable code is distributed, then the
        accompanying documentation have to state that
        "this software is based in part on examples of
        the pOS developer packet".

    (4) Permission for use of this code is granted only
        if the user accepts full responsibility for any
        undesirable consequences. proDAD accept NO LIABILITY
        for damages of any kind.

  ©proDAD
***********************************************************/

/*\
*** Example:
***
\*/


#define __COMPUTER_AMIGA 1
#define NOMYDEBUG

#include "p:pExec/Types.h"
#include "p:pDOS/ArgTags.h"
#include "p:pDOS/DosSig.h"
#include "p:pDOS/DosErrors.h"
#include "p:pGadget/Gadget.h"
#include "p:pScreen/ScrTags.h"
#include "p:pScreen/Window.h"
#include "p:pScreen/Screen.h"
#include "p:pIntui/IntuMsg.h"
#include "p:pIntui/Tags.h"
#include "p:pPrefs/PrefsTags.h"
#include "p:pProto/pLibExt.h"
#include "p:pProto/pExec2.h"
#include "p:pProto/pDOS2.h"
#include "p:pProto/pIntui2.h"

#ifdef _____ME_____
  #include "grund/inc_string.h"
  #include "grund/inc_stdio.h"
#else
 #ifdef __cplusplus
 extern "C" {
 #endif
  #include <string.h>
  #include <stdio.h>
 #ifdef __cplusplus
 }
 #endif
#endif


const CHAR *HelpText=
""
;

const CHAR *PrgHeader=
"A little clock";

const CHAR *PrgVerText=
"$VER: 1.0 ("__DATE2__") (Copyright 1996 by proDAD) (Created by Holger Papajewski)";


struct pOS_IntuiDevice *gb_IntuiBase;


/*----------------------------------
-----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
VOID main()
{
  struct pOS_DosArgs* Args;
  ULONG Ops[1]={0};

  gb_IntuiBase=(pOS_IntuiDevice*)pOS_OpenLibrary("pintui.library",0);

  /* Allways call pOS_ReadDosArgs(). */
  /* Also if you haven't any arguments. */
  Args=pOS_ReadDosArgs( "", Ops, 1,
    ARGTAG_PrgHeaderText, (ULONG)PrgHeader,    /* short programm discription */
    ARGTAG_HelpText,      (ULONG)HelpText,     /* help text */
    ARGTAG_PrgVerText,    (ULONG)PrgVerText,   /* version string */
    TAG_DONE);

  if(Args) {
    pOS_Screen *Scr;

    if( Scr = pOS_LockPubScreen(NULL)) {
      const struct pOS_DrawInfo *const Dri = Scr->scr_DrawInfo;
            struct pOS_Gadget   *TimeDisp, *Grp;
            struct pOS_Window   *Win;

      TimeDisp = (pOS_Gadget*)pOS_NewIObject( NULL,
        "PREFS:Time_Prefs.library/TimeDispGad.class", 0,
        ICLTAG_DrawInfo,        (ULONG)Dri,
        ICLTAG_Gwk,             1,
        PRFTIDSTAG_CurrentTime, TRUE,
        TAG_DONE );

      if( TimeDisp ) {
        Grp = (pOS_Gadget*)pOS_NewIObject( NULL,
          Dri->dri_Names[SCRNAM_GGroupClass], 0,
          ICLTAG_DrawInfo,        (ULONG)Dri,
          ICLTAG_AutoDelete,      TRUE,
          GRPGADTAG_BorLeft,      4,
          GRPGADTAG_BorTop,       4,
          GRPGADTAG_BorRight,     4,
          GRPGADTAG_BorBottom,    4,
          GRPGADTAG_AddGadget,    (ULONG)TimeDisp,
          TAG_DONE );

        Win = pOS_OpenWindow(
          SCRTAG_Title,   (ULONG)"Clock",
          SCRTAG_Screen,  (ULONG)Scr,
          SCRTAG_Flags,   WINFLGF_DepthGadget | WINFLGF_SimpleRefresh |
                          WINFLGF_Activate | WINFLGF_CloseGadget |
                          WINFLGF_Dragbar | WINFLGF_SizeGadget |
                          WINFLGF_SizeBBottom,
          SCRTAG_IDCMP,   IDCMP_CloseWindow | IDCMP_StdSysMsg,
          SCRTAG_ZipGadget,       TRUE,
          SCRTAG_AutoSizeGadget,  (ULONG)Grp,
          SCRTAG_AutoAdjust,      TRUE,
          SCRTAG_UnderMouse,      TRUE,
          TAG_DONE );

        if( Win ) {
          struct pOS_MsgPort  ReplyPort;
          struct pOS_TimerIO *TimerIO;

          pOS_ConstructMsgPort( &ReplyPort );
          if( TimerIO=(pOS_TimerIO*)pOS_CreateIORequest(&ReplyPort,sizeof(pOS_TimerIO))) {
            pOS_OpenDevice("ptimer.device",TIMERUNIT_VBlank,(pOS_IORequest*)TimerIO,0,0);
            if( TimerIO->tio_Error == 0 ) {
              struct pOS_IntuiMessage *Msg;
              BOOL  Run = TRUE;
              ULONG Signal;
              ULONG WinSig = (1L << Win->win_UserPort->mp_SigBit);
              ULONG TimeSig= (1L << ReplyPort.mp_SigBit);
              ULONG SigMask = WinSig | TimeSig | DOSSIGF_CTRL_C;


              TimerIO->tio_Command=TRCMD_AddRequest;
              TimerIO->tio_Time.tv_Secs=1;
              TimerIO->tio_Time.tv_Micro=0;
              pOS_SendIO((pOS_IORequest*)TimerIO);

              while( Run ) {

                Signal = pOS_WaitSignal( SigMask);

                if( Signal & DOSSIGF_CTRL_C ) Run = FALSE;

                if( Signal & TimeSig ) {
                  pOS_WaitIO((pOS_IORequest*)TimerIO);

                  TimerIO->tio_Command=TRCMD_AddRequest;
                  TimerIO->tio_Time.tv_Secs=1;
                  TimerIO->tio_Time.tv_Micro=0;
                  pOS_SendIO((pOS_IORequest*)TimerIO);

                  pOS_SetGadgetAttrs( Win, TimeDisp,
                    PRFTIDSTAG_CurrentTime, TRUE, TAG_DONE );
                }

                if( Signal & WinSig ) {
                  while( Msg=(pOS_IntuiMessage*)pOS_GetMsg(Win->win_UserPort)) {

                    switch( Msg->im_Class ) {
                      case IDCMP_CloseWindow:
                        Run = FALSE;
                        break;

                      default:
                        break;
                    }

                    if( pOS_SysIMessage(Msg) == FALSE )
                      pOS_ReplyMsg( (pOS_Message*)Msg );
                  }
                }
              }
              pOS_AbortIO((pOS_IORequest*)TimerIO);
              pOS_WaitIO((pOS_IORequest*)TimerIO);

              pOS_CloseDevice((pOS_IORequest*)TimerIO);
            }
            else printf("Cannot open ptimer.device.\n");
            pOS_DeleteIORequest((pOS_IORequest*)TimerIO);
          }
          pOS_DestructMsgPort( &ReplyPort );
          pOS_CloseWindow( Win );
        }
        else printf("Cannot open TimeDispGad.\n");
      }
      else printf("Cannot open window.\n");
      pOS_UnlockPubScreen( Scr );
    }
    else printf("Cannot lock public screen.\n");
    pOS_DeleteDosArgs( Args );
  }

  pOS_CloseLibrary((pOS_Library*)gb_IntuiBase);
}

#endif
