home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM System Amiga C Club */
- /* Chapter: Messages Tulevagen 22 */
- /* File: Example2.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-05-02 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
- /* This program will open the Timer Device to which we */
- /* connect a message port. When the Timer Device has */
- /* done our request (waiting for 10 seconds) it will */
- /* send a message to the message port which tells us */
- /* that the time has passed. */
- /* */
- /* This example demonstrates how you can give the */
- /* system (like the Timer Device) a message port */
- /* through which the system can communicate with us. */
- /* Although we are using the Timer Device, it is only */
- /* as a demonstration on how to work with message */
- /* ports. For more information about the Timer Device, */
- /* see chapter Timer Device. */
-
-
-
- #include <exec/types.h> /* STRPTR */
- #include <exec/ports.h> /* struct Message */
- #include <exec/memory.h> /* MEMF_PUBLIC */
- #include <exec/nodes.h> /* NT_MESSAGE */
- #include <devices/timer.h> /* TIMERNAME */
-
-
-
- /* Declare a pointer to our message port: */
- struct MsgPort *replymp;
-
-
- /* Timer Device: */
- struct timerequest *tr;
- BOOL error = TRUE;
-
-
-
- /* Declare the functions: */
- void clean_up();
- void main();
-
-
-
- void main()
- {
- /* 1. Create a message port through which the */
- /* system will be able to communicate with us: */
- replymp = (struct MsgPort *)
- CreatePort( NULL, 0 );
- /* Check if we have successfully created a port: */
- if( !replymp )
- clean_up( "Could not create the reply port!" );
-
-
- /* 2. Try to alloacte and initialize a timerequest */
- /* structure, to which we will send our requests to: */
- tr = (struct timerequest *)
- CreateExtIO( replymp, sizeof( struct timerequest) );
- /* Have we successfully created the timerequest structure? */
- if( !tr )
- clean_up( "Could not create the IO!" );
-
-
- /* 3. Open the Timer Device: */
- error = OpenDevice( TIMERNAME, UNIT_VBLANK, tr ,0 );
- /* Have we successfully opened the device? */
- if( error )
- clean_up( "Could not open the Timer Device!" );
-
-
- /* 4. Set our request: (Wait 10 seconds) */
- tr->tr_node.io_Command = TR_ADDREQUEST;
- tr->tr_time.tv_secs = 10;
- tr->tr_time.tv_micro = 0;
-
-
- /* 5. Do our request and return immediately: */
- SendIO( tr );
-
-
-
- /* 6. Wait until we receive a message from the */
- /* Timer Device. (10 seconds have passed.) */
- WaitPort( replymp );
-
-
-
- /* 7. Remove all messages before we quit: (Note that we */
- /* should NOT reply and send the message back. The */
- /* message belongs actually to us since it is a part */
- /* of the "request block". Only other program's */
- /* messages should be replied.) */
- while( GetMsg( replymp ) )
- /* Do nothing... */;
-
-
-
- /* 8. Clean up and leave: */
- clean_up( "The End!" );
- }
-
-
-
- void clean_up( text )
- STRPTR text;
- {
- /* Close the Timer Device: */
- if( !error )
- CloseDevice( tr );
-
-
- /* Delete the timerequest structure: */
- if( tr )
- DeleteExtIO( tr, sizeof( struct timerequest) );
-
-
- /* Close the message port: */
- if( replymp )
- DeletePort( replymp);
-
-
- /* Print the message: */
- printf( "%s\n", text );
-
-
- /* Quit: */
- exit( 0 );
- }
-
-