home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
system
/
messages
/
example3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-27
|
5KB
|
195 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM System Amiga C Club */
/* Chapter: Messages Tulevagen 22 */
/* File: Example3.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 two ports. The first port is */
/* used by the Timer Device while the second port will */
/* not be used at all (it is a dummy port). The reason */
/* why this example opens an extra port is because I */
/* want to demonstrate how you can handle two message */
/* ports simultaniously. With this technique you can */
/* monitor up to 32 ports (the system usually needs */
/* some singnals, but you have at least 16 signals for */
/* yourself). */
#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 two pointers to our message ports: */
struct MsgPort *timerport, *dummyport;
struct Message *msg;
/* Timer Device: */
struct timerequest *tr;
BOOL error = TRUE;
/* Declare the functions: */
void clean_up();
void main();
void main()
{
/* We will store the port's signal bit values in these */
/* variables: (Up to 32 bits, which is enough) */
ULONG dummy_sig;
ULONG timer_sig;
ULONG received_sig;
/* The dummy port: */
/* Create a message port: (No name, priority 0) */
dummyport = (struct MsgPort *) CreatePort( NULL, 0 );
/* Check if we have successfully created the port: */
if( !dummyport )
clean_up( "Could not create the dummy port!" );
/* The Timer Device's port: */
/* Create a message port through which the system */
/* will be able to communicate with us: */
timerport = (struct MsgPort *)
CreatePort( NULL, 0 );
/* Check if we have successfully created a port: */
if( !timerport )
clean_up( "Could not create the timer port!" );
/* Try to alloacte and initialize a timerequest */
/* structure, to which we will send our requests to: */
tr = (struct timerequest *)
CreateExtIO( timerport, sizeof( struct timerequest) );
/* Have we successfully created the timerequest structure? */
if( !tr )
clean_up( "Could not create the IO!" );
/* 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!" );
/* 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;
/* Do our request and return immediately: */
SendIO( tr );
/* Get the ports signal values and transform them */
/* into bit field signals: */
dummy_sig = 1 << dummyport->mp_SigBit;
timer_sig = 1 << timerport->mp_SigBit;
/* Wait until we receive a message from either */
/* the timer or dummy message port: */
received_sig = Wait( dummy_sig | timer_sig );
/* Check which message port sent the signal: */
/* Was it the dummy port? */
if( received_sig & dummy_sig )
{
printf( "The dummy message port! (Que?)\n" );
/* Remove all messages: */
while( msg = (struct Message *) GetMsg( dummyport ) )
/* Do nothing... */;
}
/* Was it the timer port? */
if( received_sig & timer_sig )
{
printf( "The timer message port!\n" );
/* Remove all messages: */
while( msg = (struct Message *) GetMsg( timerport ) )
/* Do nothing... */;
}
/* 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 timer's message port: */
if( timerport )
DeletePort( timerport);
/* Close the dummy message port: */
if( dummyport )
DeletePort( dummyport );
/* Print the message: */
printf( "%s\n", text );
/* Quit: */
exit( 0 );
}