(Page 5 of 11 in this chapter)
/*
* This function blocks until an event message is available
* and returns the received event to the calling routine.
* MUX_HANDLE is typedef'd in adidef.h for portability.
*/
void myWaitForEvent( unsigned drvid,
MUX_HANDLE muxhandle,
ADI_EVENT *event )
{
...
/*
* adiFetchAndProcess() assigns a non-zero event id
* when an event is generated for the app.
*/
event->id = 0 ;
while( event->id == 0 )
{
#ifdef UNIX
struct pollfd fds[1];
fds[0].events = POLLIN;
fds[0].fd = muxhandle;
if ( poll( fds, 1, -1 ) < 0 )
{
if ( errno == EINTR )
continue ;
perror("poll") ;
exit( 1 );
}
#elif defined OS2
APIRET ret;
if( ( ret = (int)DosWaitEventSem( muxhandle,
(unsigned)SEM_INDEFINITE_WAIT ) ) != 0 )
{
printf( "Error on DosWaitEventSem: %d\n", ret );
exit( 1 );
}
#elif defined WIN32
if( WaitForSingleObject( muxhandle, INFINITE )
==WAIT_FAILED )
{
printf( "Error on WaitForSingleObject: %d\n",
GetLastError() );
exit( 1 );
}
#endif
/*
* Allow ADI to fetch the event and process it through its
* internal state machine.
*/
adiFetchAndProcess( drvid, event );
}
return;
}
unsigned driver_id ; /* global variable -- see signal handler */
struct sigaction sigact =
{
0,
MySignalHandler,
0
};
adiOpenDriver( &driver_id, &muxhandle );
if ( sigaction( SIGPOLL, &sigact, NULL ) != 0 )
perror("sigaction");
if ( ioctl( muxhandle, I_SETSIG, S_RDNORM ) != 0 )
perror("ioctl");
void MySignalHandler()
{
ADI_EVENT adi_event ;
DWORD adiret ;
do
{
adiret = adiFetchAndProcess( driver_id, &adi_event );
if ( adiret == SUCCESS )
/* process event */
} until ( adiret == ADIERR_RECEIVE_FAILED ) ;
}
(Page 5 of 11 in this chapter)Copyright 1996 Natural MicroSystems, Inc. All Rights Reserved.