/*
 * Copyright 1988, 1989, 1990, 1991 Massachusetts Institute of Technology
 */
#include "structs.h"

INPUT_ptr inpututil_open( devtbl, num)
     VCONF_DEVTABLE *devtbl;
     int num;
{
  INPUT_ptr inputter;
  char *cur_point, *strchr();
  int switchnum, channum, num_outputs, *outputs, i, j;

  if ( !devtbl || num<0 || (num+1)>devtbl->ndevs) {
    if (DEBUG) printf("inpututil_open: given bad devtable or number\n");
    return NULL;
  }
  if( devtbl->dev[num].type != INPUT_DEV ) {
    if (DEBUG) printf("inpututil_open: device is not an INPUT\n");
    return NULL;
  }
  inputter = (INPUT_ptr) calloc( 1, sizeof(INPUT) );
  if( inputter == NULL ) {
    if (DEBUG) printf("inpututil_open: failed malloc()\n");
    return NULL;
  }
  
  strcpy(inputter->devname,devtbl->dev[num].name);

  for ( i = 0; i < MAXSERVEROUTS; i++) {
    inputter->output_chans[i].switcher = -1;
    inputter->outputnum[i] = -1;
  }
  i = 0;

  cur_point = devtbl->dev[num].channel;
/*  if (DEBUG) printf ("channel = %s\n",devtbl->dev[num].channel);*/
  do {
    if (i > MAXSERVEROUTS) break;
    switchnum = atoi(cur_point);
    cur_point = strchr(cur_point, '-') + 1;
    channum = atoi(cur_point);
  /*  if (DEBUG) printf ("%d: switch %d, input %d\n",i,switchnum,channum);*/
    inputter->output_chans[i].switcher = switchnum;
    inputter->output_chans[i].channel = channum;
    outputs = NULL;
    newfindoutputnums(inputter->output_chans[i], &num_outputs, &outputs);
    for (j = 0; j < num_outputs; j++) 
      inputter->outputnum[outputs[j]] = i;
    if (outputs) free(outputs);
/*    if (DEBUG) printf("Index %d, switch %d, chan %d\n",i, switchnum,
		      channum);*/
    i++;
  } while ((cur_point=strchr(cur_point,',') + 1) != (char *)1);
  return(inputter);
}

INPUT_ptr inpututil_close( inputter )
     INPUT_ptr inputter;
{
  free((char *)inputter);
  return NULL;
}
