home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 488.lha / Timer / test.c < prev    next >
C/C++ Source or Header  |  1991-02-08  |  5KB  |  197 lines

  1. /**************************************
  2. *  TEST.C  08/04/90
  3. *  Written by Timm Martin
  4. *  This source code is public domain.
  5. ***************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <functions.h>
  9. #include <intuition/intuition.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <stdio.h>
  12. #include "timer.h"
  13.  
  14. /********************
  15. *  SHARED VARIABLES
  16. *********************/
  17.  
  18. struct IntuitionBase *IntuitionBase = NULL;
  19. struct Window        *win           = NULL;
  20.  
  21. /************************
  22. *  NEW WINDOW STRUCTURE
  23. *************************/
  24.  
  25. struct NewWindow new_window =
  26. {
  27.   /* SHORT           LeftEdge    */ 0,
  28.   /* SHORT           TopEdge     */ 0,
  29.   /* SHORT           Width       */ 640,
  30.   /* SHORT           Height      */ 40,
  31.   /* UBYTE           DetailPen   */ 0,
  32.   /* UBYTE           BlockPen    */ 1,
  33.   /* ULONG           IDCMPFlags  */ CLOSEWINDOW|MOUSEBUTTONS,
  34.   /* ULONG           Flags       */ ACTIVATE|NOCAREREFRESH|SIMPLE_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING,
  35.   /* struct Gadget * FirstGadget */ NULL,
  36.   /* struct Image *  CheckMark   */ NULL,
  37.   /* UBYTE *         Title       */ (STRPTR)"Timer Test Program",
  38.   /* struct Screen * Screen      */ NULL,
  39.   /* struct BitMap * BitMap      */ NULL,
  40.   /* SHORT           MinWidth    */ 230,
  41.   /* SHORT           MinHeight   */ 20,
  42.   /* USHORT          MaxWidth    */ 640,
  43.   /* USHORT          MaxHeight   */ 200,
  44.   /* USHORT          Type        */ WBENCHSCREEN,
  45. };
  46.  
  47. /*************
  48. *  FUNCTIONS
  49. **************/
  50.  
  51. void main( int argc, char *argv[] );
  52. void program_end( char *error );
  53. void program_begin( void );
  54. void window_input( void );
  55.  
  56. /***********
  57. *  M A I N
  58. ************/
  59.  
  60. void main( int argc, char *argv[] )
  61. {
  62.   /* quit if not run from the CLI -- nowhere to send output */
  63.   if (argc)
  64.   {
  65.     program_begin();
  66.     window_input();
  67.   }
  68.   program_end( NULL );
  69. }
  70.  
  71. /*****************
  72. *  PROGRAM BEGIN
  73. ******************/
  74.  
  75. /*
  76. This procedure opens the Intuition library, the window, and the timer device.
  77. If any of these things fail, the program ends.
  78. */
  79.  
  80. void program_begin( void )
  81. {
  82.   if (!(IntuitionBase = (struct IntuitionBase *)
  83.                         OpenLibrary( "intuition.library", 0L )))
  84.     program_end( "intuition" );
  85.  
  86.   if (!(win = OpenWindow( &new_window )))
  87.     program_end( "window" );
  88.  
  89.   if (!timer_open())
  90.     program_end( "timer" );
  91. }
  92.  
  93. /***************
  94. *  PROGRAM END
  95. ****************/
  96.  
  97. /*
  98. This procedure closes the timer device, the window, and the Intuition
  99. library.  It also prints an error message (if any) to the CLI.
  100. */
  101.  
  102. void program_end( char *error )
  103. {
  104.   if (error) printf( "FAILED: %s\n", error );
  105.  
  106.   timer_close();
  107.  
  108.   if (win)           CloseWindow( win );
  109.   if (IntuitionBase) CloseLibrary( IntuitionBase );
  110. }
  111.  
  112. /****************
  113. *  WINDOW INPUT
  114. *****************/
  115.  
  116. /*
  117. This procedure monitors for window input.  It returns when the user clicks on
  118. the window close gadget.
  119. */
  120.  
  121. void window_input( void )
  122. {
  123.   BOOL finished;
  124.   struct IntuiMessage *imessage;
  125.   BOOL winput;
  126.  
  127.   printf( "\nHere's a series of syncrhonous waits:\n\n" );
  128.   printf( "Waiting for 2 seconds...\n" );
  129.   timer_wait( 2 * MICROS_PER_SEC );
  130.   printf( "Waiting for 1 second...\n" );
  131.   timer_wait( 1 * MICROS_PER_SEC );
  132.   printf( "Waiting for 5 seconds...\n" );
  133.   timer_wait( 5 * MICROS_PER_SEC );
  134.   printf( "Waiting for 1/2 second...\n" );
  135.   timer_wait( MICROS_PER_SEC / 2 );
  136.  
  137.   printf( "\nAnd now for some asynchronous waits...\n" );
  138.   printf( "A message will be displayed every two seconds\n" );
  139.   printf( "if you don't click the left mouse button in the test window:\n\n" );
  140.  
  141.   finished = FALSE;
  142.   while (!finished)
  143.   {
  144.     /* ask the timer to notify you in 2 seconds */
  145.     timer_start( 2 * MICROS_PER_SEC );
  146.     /* assume there is no window input */
  147.     winput = FALSE;
  148.  
  149.     /* wait for input from the window and the timer device */
  150.     Wait( 1L<<win->UserPort->mp_SigBit | 1L<<timer_port->mp_SigBit );
  151.  
  152.     /* for each window input message */
  153.     while (imessage = (struct IntuiMessage *)GetMsg( win->UserPort ))
  154.     {
  155.       switch (imessage->Class)
  156.       {
  157.         case CLOSEWINDOW:
  158.           /* end the program */
  159.           finished = TRUE;
  160.           break;
  161.         case MOUSEBUTTONS:
  162.           /* if clicked down the left mouse button */
  163.           if (imessage->Code == SELECTDOWN)
  164.           {
  165.             /* print a message */
  166.             printf( "Mouse Clicked!\n" );
  167.             /* And record the fact that an input occurred.  Must do this
  168.              * because the timer request may elapse while we are inside
  169.              * this loop.
  170.              */
  171.             winput = TRUE;
  172.           }
  173.           break;
  174.       }
  175.       /* reply to the message to free its memory */
  176.       ReplyMsg( (struct Message *)imessage );
  177.     }
  178.  
  179.     /* did the timer request elapse? */
  180.     if (timer_test())
  181.     {
  182.       /* if 'winput' is FALSE, then the timer elapsed before there was any
  183.        * window input.  If 'winput' is TRUE, then the timer elapsed after
  184.        * the window input but before it reached this test.
  185.        */
  186.       if (!winput) printf( "2 Seconds Expired!\n" );
  187.     }
  188.     /* if the timer request didn't elapse, cancel it so we can issue another
  189.      * request
  190.      */
  191.     else
  192.       timer_abort();
  193.   }
  194.  
  195.   printf( "\nSee ya!\n\n" );
  196. }
  197.