#include <stdio.h>
#include <unicap.h>

#define MAX_DEVICES 64

unicap_handle_t
open_device ()
{
  int dev_count;
  int status = STATUS_SUCCESS;
  unicap_device_t devices[MAX_DEVICES];
  unicap_handle_t handle;
  int d = -1;

  for (dev_count = 0; SUCCESS (status) && (dev_count < MAX_DEVICES);
       dev_count++)
    {
      status = unicap_enumerate_devices (NULL, &devices[dev_count], dev_count); // (1)
      if (SUCCESS (status))
        printf ("%d: %s\n", dev_count, devices[dev_count].identifier);
      else
        break;
    }

  if (dev_count == 0)
    return NULL;                // no device selected

  while ((d < 0) || (d >= dev_count))
    {
      printf ("Open Device: ");
      scanf ("%d", &d);
    }

  unicap_open (&handle, &devices[d]);   // (2)

  return handle;
}


int
main (int argc, char **argv)
{
  unicap_handle_t handle;
  handle = open_device ();
  if (!handle)
    return -1;

  unicap_close (handle);        // (3)
  return 0;
}

