#if ( !defined(lint) && !defined(SABER))
  static char rcsid_module_c[] = "$Header: driver.c,v 1.1 88/08/26 12:46:19 kit Locked $";
  static char rcs_version[] = "$Athena: driver.c,v 1.1 88/08/26 12:46:19 kit Locked $";
#endif

/*
 *  This is the file buttons.c for xvdp.  It created all the buttons
 *  for player display.
 *
 *  Created:    8/25/88
 *  By:         Chris D. Peterson 
 *
 *      $Source: /u1/Video/tools/Xvdp/RCS/driver.c,v $
 *      $Author: kit $
 *      $Header: driver.c,v 1.1 88/08/26 12:46:19 kit Locked $
 *	
 *  	Copyright 1988 by the Massachusetts Institute of Technology.
 *
 *	For further information on copyright and distribution 
 *	see the file mit-copyright.h
 */

#include "mit-copyright.h"

#include <stdio.h>
#include <strings.h>
#include <X11/Intrinsic.h>	/* Boolean, TRUE and FALSE defined here. */
#include "video_conf.h"
#include "rpd_driver.h"

#include "xvdp.h"

Boolean index_state;
RPD_ptr rpd;			/* rpd driver pointer. */
VCONF_DEVTABLE	vdt;		/* video device table */

/*	Function Name: StartDriver
 *	Description: Initializes the driver.
 *	Arguments: model - model type of driver.
 *                 tty - the tty port.
 *                 baud - the baud rate for the communication.
 *	Returns: none
 */

void
StartDriver(model, tty, baud)
char *model, *tty;
int baud;
{

  printf("Starting the video driver for a %s on %s at %d baud.\n",
	 model, tty, baud);

  vdt.dev[0].type = RPD_DEV;
  strcpy(vdt.dev[0].model, model);
  strcpy(vdt.dev[0].tty, tty);
  vdt.dev[0].baud = baud;
  vdt.dev[0].parity = 0;
  vdt.ndevs = 1;
  
  if( (rpd = rpdutil_open( &vdt, 0 )) == NULL ) {
    fprintf(stderr,"unable to open rpd %s on %s\n",
	    rpd->devname, rpd->port );
    exit(1);
  }

  (void) (*rpd->cmd)(rpd, LOAD); /* load the disk onto the machine. */
}

/*	Function Name: ShutdownDriver
 *	Description:   Unloads the disk prior to shutdown.
 *	Arguments:     none.
 *	Returns:       none.
 */

void
ShutdownDriver()
{
  (void) (*rpd->cmd)(rpd, UNLOAD);
}

/*	Function Name: ToggleIndex
 *	Description: Toggles the index state.
 *	Arguments: none.
 *	Returns: TRUE if successful.
 */

Boolean
ToggleIndex()
{
  /* index_state and rpd are global variables. */
  int result;

  if (index_state)
    result = (*rpd->cmd)(rpd, INDEX_OFF);
  else
    result = (*rpd->cmd)(rpd, INDEX_ON);  

  index_state = !index_state;
  
  return ( result == 0 );
}

/*	Function Name: VideoForward
 *	Description:  rolls the video forward at the stated speed.
 *	Arguments: speed - the speed at which to run this forward.
 *	Returns: TRUE if successful.
 */

Boolean
VideoForward(speed)
int speed;
{
  int result;
  /* rpd is a global variable. */

  switch (speed) {
  case FRAME:
    result = (*rpd->jog)(rpd,1);
    break;
  case SLOW:
    result = (*rpd->varspeed)(rpd,F_SLOW);
    break;
  case NORMAL:
    result = (*rpd->varspeed)(rpd,F_PLAY);
    break;
  case FAST:
    result = (*rpd->varspeed)(rpd,F_FAST);
    break;
  default:
    PrintWarning("Unknow speed in VideoForward.");
    break;
  }
    
  return( result == 0 );
}

/*	Function Name: VideoReverse
 *	Description:  rolls the video forward at the stated speed.
 *	Arguments: speed - the speed at which to run this forward.
 *	Returns: TRUE if successful.
 */

Boolean
VideoReverse(speed)
int speed;
{
  int result;
  /* rpd is a global variable. */

  switch (speed) {
  case FRAME:
    result = (*rpd->jog)(rpd, -1);
    break;
  case SLOW:
    result = (*rpd->varspeed)(rpd, R_SLOW);
    break;
  case NORMAL:
    result = (*rpd->varspeed)(rpd, R_PLAY);
    break;
  case FAST:
    result = (*rpd->varspeed)(rpd, R_FAST);
    break;
  default:
    PrintWarning("Unknow speed in VideoForward.");
    break;
  }
    
  return( result == 0 );
}

/*	Function Name: VideoReset
 *	Description: resets the video.
 *	Arguments:  rpd_ptr - pointer to the rpd device.
 *	Returns: TRUE if successful.
 */

VideoReset()
{
  /* rpd and index_state are global variables. */
  int result;

  result = (*rpd->reset)(rpd);
  if (result == 0) {
    index_state = FALSE;
    return(TRUE);
  }
  return(FALSE);
}

/*	Function Name: VideoStop
 *	Description: Stops video.
 *	Arguments: none.
 *	Returns: TRUE if successful.
 */

Boolean
VideoStop()
{
  /* rpd is a global variable. */

  return( (*rpd->varspeed)(rpd,STOP) == 0 );
}

/*	Function Name: VideoCurrentFrame
 *	Description: returns number of current frame.
 *	Arguments: none.
 *	Returns: the current frame.
 */

int
VideoCurrentFrame()
{
  /* rpd is a global variable. */

  return( (int) (*rpd->getframe)(rpd) );
}

/*	Function Name: DoVideoSearch
 *	Description: goes to the frame chosen.
 *	Arguments: frame - the frame to goto,
 *	Returns: none.
 */

Boolean
DoVideoSearch(frame)
int frame;
{
  /* rpd is a global variable. */

  return ( (*rpd->search)(rpd, frame, 1) == 0);
}



