/**************************************
*  TEST.C  08/04/90
*  Written by Timm Martin
*  This source code is public domain.
***************************************/

#include <exec/types.h>
#include <functions.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <stdio.h>
#include "timer.h"

/********************
*  SHARED VARIABLES
*********************/

struct IntuitionBase *IntuitionBase = NULL;
struct Window        *win           = NULL;

/************************
*  NEW WINDOW STRUCTURE
*************************/

struct NewWindow new_window =
{
  /* SHORT           LeftEdge    */ 0,
  /* SHORT           TopEdge     */ 0,
  /* SHORT           Width       */ 640,
  /* SHORT           Height      */ 40,
  /* UBYTE           DetailPen   */ 0,
  /* UBYTE           BlockPen    */ 1,
  /* ULONG           IDCMPFlags  */ CLOSEWINDOW|MOUSEBUTTONS,
  /* ULONG           Flags       */ ACTIVATE|NOCAREREFRESH|SIMPLE_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING,
  /* struct Gadget * FirstGadget */ NULL,
  /* struct Image *  CheckMark   */ NULL,
  /* UBYTE *         Title       */ (STRPTR)"Timer Test Program",
  /* struct Screen * Screen      */ NULL,
  /* struct BitMap * BitMap      */ NULL,
  /* SHORT           MinWidth    */ 230,
  /* SHORT           MinHeight   */ 20,
  /* USHORT          MaxWidth    */ 640,
  /* USHORT          MaxHeight   */ 200,
  /* USHORT          Type        */ WBENCHSCREEN,
};

/*************
*  FUNCTIONS
**************/

void main( int argc, char *argv[] );
void program_end( char *error );
void program_begin( void );
void window_input( void );

/***********
*  M A I N
************/

void main( int argc, char *argv[] )
{
  /* quit if not run from the CLI -- nowhere to send output */
  if (argc)
  {
    program_begin();
    window_input();
  }
  program_end( NULL );
}

/*****************
*  PROGRAM BEGIN
******************/

/*
This procedure opens the Intuition library, the window, and the timer device.
If any of these things fail, the program ends.
*/

void program_begin( void )
{
  if (!(IntuitionBase = (struct IntuitionBase *)
                        OpenLibrary( "intuition.library", 0L )))
    program_end( "intuition" );

  if (!(win = OpenWindow( &new_window )))
    program_end( "window" );

  if (!timer_open())
    program_end( "timer" );
}

/***************
*  PROGRAM END
****************/

/*
This procedure closes the timer device, the window, and the Intuition
library.  It also prints an error message (if any) to the CLI.
*/

void program_end( char *error )
{
  if (error) printf( "FAILED: %s\n", error );

  timer_close();

  if (win)           CloseWindow( win );
  if (IntuitionBase) CloseLibrary( IntuitionBase );
}

/****************
*  WINDOW INPUT
*****************/

/*
This procedure monitors for window input.  It returns when the user clicks on
the window close gadget.
*/

void window_input( void )
{
  BOOL finished;
  struct IntuiMessage *imessage;
  BOOL winput;

  printf( "\nHere's a series of syncrhonous waits:\n\n" );
  printf( "Waiting for 2 seconds...\n" );
  timer_wait( 2 * MICROS_PER_SEC );
  printf( "Waiting for 1 second...\n" );
  timer_wait( 1 * MICROS_PER_SEC );
  printf( "Waiting for 5 seconds...\n" );
  timer_wait( 5 * MICROS_PER_SEC );
  printf( "Waiting for 1/2 second...\n" );
  timer_wait( MICROS_PER_SEC / 2 );

  printf( "\nAnd now for some asynchronous waits...\n" );
  printf( "A message will be displayed every two seconds\n" );
  printf( "if you don't click the left mouse button in the test window:\n\n" );

  finished = FALSE;
  while (!finished)
  {
    /* ask the timer to notify you in 2 seconds */
    timer_start( 2 * MICROS_PER_SEC );
    /* assume there is no window input */
    winput = FALSE;

    /* wait for input from the window and the timer device */
    Wait( 1L<<win->UserPort->mp_SigBit | 1L<<timer_port->mp_SigBit );

    /* for each window input message */
    while (imessage = (struct IntuiMessage *)GetMsg( win->UserPort ))
    {
      switch (imessage->Class)
      {
        case CLOSEWINDOW:
          /* end the program */
          finished = TRUE;
          break;
        case MOUSEBUTTONS:
          /* if clicked down the left mouse button */
          if (imessage->Code == SELECTDOWN)
          {
            /* print a message */
            printf( "Mouse Clicked!\n" );
            /* And record the fact that an input occurred.  Must do this
             * because the timer request may elapse while we are inside
             * this loop.
             */
            winput = TRUE;
          }
          break;
      }
      /* reply to the message to free its memory */
      ReplyMsg( (struct Message *)imessage );
    }

    /* did the timer request elapse? */
    if (timer_test())
    {
      /* if 'winput' is FALSE, then the timer elapsed before there was any
       * window input.  If 'winput' is TRUE, then the timer elapsed after
       * the window input but before it reached this test.
       */
      if (!winput) printf( "2 Seconds Expired!\n" );
    }
    /* if the timer request didn't elapse, cancel it so we can issue another
     * request
     */
    else
      timer_abort();
  }

  printf( "\nSee ya!\n\n" );
}
