//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// 	Vtdapi.c
//	Luiz Marques Afonso - Agosto/95
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#include <windows.h>
#include <mmsystem.h>
#include <dos.h>

#include "vtdapi.h"

#if defined( VTDAPI )
static void far
	( far *EntryPoint )( void ) ;

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
BOOL
	PegaEntryPointVTDAPI( void )
{
	struct REGPACK
		regs;
	VTDAPI_Get_Version_Parameters
		getVerParms;

	regs.r_ax = 0x1684 ;
	regs.r_bx = 0x442 ;
	regs.r_di = regs.r_ds = regs.r_es = 0x0000 ;
	intr( 0x2f, & regs ) ;
	EntryPoint = MK_FP( regs.r_es, regs.r_di ) ;

	if( EntryPoint )
	{
		// Ve qual e'a versao do VTD
		_BX = FP_OFF( &getVerParms ) ;
		_ES = FP_SEG( &getVerParms ) ;
		_AX = 0 ;
		( * EntryPoint )() ;
		if( getVerParms.DAPI_Version < 0x30A )
			return( FALSE );
	}

	return( TRUE );
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
WORD
	SetTimerVTDAPI( WORD period, WORD resolution, FARPROC callback, DWORD dwUserData )
{
	VTDAPI_Timer_Parameters
		timerParms;

	timerParms.VTDAPI_Timer_Period      = period;
	timerParms.VTDAPI_Timer_Resolution  = resolution;
	timerParms.VTDAPI_Timer_IPCS        = (ULONG) callback;
	timerParms.VTDAPI_Timer_Inst        = dwUserData;
	timerParms.VTDAPI_Timer_Flags       = TIME_PERIODIC ;

	// Instala o timer
	_BX = FP_OFF( &timerParms ) ;
	_ES = FP_SEG( &timerParms ) ;
	_AX = VTDAPI_Timer_Start;
	( * EntryPoint )() ;

	return( _AX );	// Já ia retornar em AX mesmo ...
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
BOOL
	KillTimerVTDAPI( WORD *pTimerID )
{
	//[LMA 01/08/95] - Bug na documentação: o id do timer não vai em CX.
	//Na versão 0x30A, ES:BX é um ponteiro para o id do timer.
	//Em CX, só no VTD do Windows 95
	//_CX = timerID;
	_ES = FP_SEG( pTimerID );
	_BX = FP_OFF( pTimerID );
	_AX = VTDAPI_Timer_Stop;
	( * EntryPoint )() ;

	return( _AX == TIMERR_NOERROR );
}

#endif
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
