home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / system / messages / example2.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  4KB  |  146 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM System                  Amiga C Club       */
  7. /* Chapter: Messages                    Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will open the Timer Device to which we */
  21. /* connect a message port. When the Timer Device has   */
  22. /* done our request (waiting for 10 seconds) it will   */
  23. /* send a message to the message port which tells us   */
  24. /* that the time has passed.                           */
  25. /*                                                     */
  26. /* This example demonstrates how you can give the      */
  27. /* system (like the Timer Device) a message port       */
  28. /* through which the system can communicate with us.   */
  29. /* Although we are using the Timer Device, it is only  */
  30. /* as a demonstration on how to work with message      */
  31. /* ports. For more information about the Timer Device, */
  32. /* see chapter Timer Device.                           */
  33.  
  34.  
  35.  
  36. #include <exec/types.h>    /* STRPTR         */
  37. #include <exec/ports.h>    /* struct Message */
  38. #include <exec/memory.h>   /* MEMF_PUBLIC    */
  39. #include <exec/nodes.h>    /* NT_MESSAGE     */
  40. #include <devices/timer.h> /* TIMERNAME */
  41.  
  42.  
  43.  
  44. /* Declare a pointer to our message port: */
  45. struct MsgPort *replymp;
  46.  
  47.  
  48. /* Timer Device: */
  49. struct timerequest *tr;
  50. BOOL error = TRUE;
  51.  
  52.  
  53.  
  54. /* Declare the functions: */
  55. void clean_up();
  56. void main();
  57.  
  58.  
  59.  
  60. void main()
  61. {
  62.   /* 1. Create a message port through which the  */
  63.   /* system will be able to communicate with us: */
  64.   replymp = (struct MsgPort *)
  65.     CreatePort( NULL, 0 );
  66.   /* Check if we have successfully created a port: */ 
  67.   if( !replymp )
  68.     clean_up( "Could not create the reply port!" );
  69.  
  70.  
  71.   /* 2. Try to alloacte and initialize a timerequest   */
  72.   /* structure, to which we will send our requests to: */
  73.   tr = (struct timerequest *)
  74.     CreateExtIO( replymp, sizeof( struct timerequest) );
  75.   /* Have we successfully created the timerequest structure? */
  76.   if( !tr )
  77.     clean_up( "Could not create the IO!" );
  78.  
  79.  
  80.   /* 3. Open the Timer Device: */
  81.   error = OpenDevice( TIMERNAME, UNIT_VBLANK, tr ,0 );
  82.   /* Have we successfully opened the device? */
  83.   if( error )
  84.     clean_up( "Could not open the Timer Device!" );
  85.  
  86.  
  87.   /* 4. Set our request: (Wait 10 seconds) */
  88.   tr->tr_node.io_Command = TR_ADDREQUEST;
  89.   tr->tr_time.tv_secs = 10;
  90.   tr->tr_time.tv_micro = 0;
  91.  
  92.  
  93.   /* 5. Do our request and return immediately: */
  94.   SendIO( tr );
  95.  
  96.  
  97.  
  98.   /* 6. Wait until we receive a message from the */
  99.   /*    Timer Device. (10 seconds have passed.)  */
  100.   WaitPort( replymp );
  101.  
  102.  
  103.  
  104.   /* 7. Remove all messages before we quit: (Note that we */
  105.   /*    should NOT reply and send the message back. The   */
  106.   /*    message belongs actually to us since it is a part */
  107.   /*    of the "request block". Only other program's      */
  108.   /*    messages should be replied.)                      */
  109.   while( GetMsg( replymp ) )
  110.     /* Do nothing... */;
  111.  
  112.  
  113.  
  114.   /* 8. Clean up and leave: */  
  115.   clean_up( "The End!" );
  116. }
  117.  
  118.  
  119.  
  120. void clean_up( text )
  121. STRPTR text;
  122. {
  123.   /* Close the Timer Device: */
  124.   if( !error )
  125.     CloseDevice( tr );
  126.  
  127.  
  128.   /* Delete the timerequest structure: */
  129.   if( tr )
  130.     DeleteExtIO( tr, sizeof( struct timerequest) );
  131.  
  132.  
  133.   /* Close the message port: */ 
  134.   if( replymp )
  135.     DeletePort( replymp);
  136.  
  137.  
  138.   /* Print the message: */
  139.   printf( "%s\n", text );
  140.  
  141.  
  142.   /* Quit: */
  143.   exit( 0 );
  144. }
  145.  
  146.