
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>

#include <dev/bktr/ioctl_bt848.h>
#include <dev/bktr/ioctl_meteor.h>

static const char	bktr[] = "/dev/bktr";

int
main (ac, av)
	int	ac;
	char	*av[];
{
	unsigned long	oldval;
	unsigned long	iocval;
	int		iocret;
	int		fd;
	char		*endp;

	fd = open (bktr, O_RDWR, 0);

	if (fd < 0) {
		perror (bktr);
		exit (-1);
	}

	printf ("fd = %d\n", fd);

	if (ac > 1) {
		iocval = strtoul (av[1], &endp, 0);

		if ((endp <= av[1]) || (*endp && ! isspace (*endp))) {
			errno = EINVAL;

			fprintf (stderr, "%s: %s %s\r\n",
				 av[0], av[1], strerror (errno));
			exit (-1);
		}

#if 0
#define METEOR_INPUT_DEV0       0x01000 /* camera input 0 -- default */
#define METEOR_INPUT_DEV_RCA    METEOR_INPUT_DEV0
#define METEOR_INPUT_DEV1       0x02000 /* camera input 1 */
#define METEOR_INPUT_DEV2       0x04000 /* camera input 2 */
#define METEOR_INPUT_DEV3       0x08000 /* camera input 3 */
#define METEOR_INPUT_DEV_RGB    0x0a000 /* for rgb version of meteor */
#define METEOR_INPUT_DEV_SVIDEO 0x06000 /* S-video input port */
#endif

		iocval &= 0xf;
		switch (iocval) {
		case	0x1:
		case	0x2:
		case	0x4:
		case	0x6:
		case	0x8:
		case	0xa:
			break;

		default:
			errno = EINVAL;
			fprintf (stderr, "%s: %s %s\r\n",
				 av[0], av[1], strerror (errno));
			exit (-1);
			break;
		}

		iocval << 12;

		if ((iocret = ioctl (fd, METEORGINPUT, &oldval)) < 0) {
			fprintf (stderr, "%s: %s\r\n", 
				 bktr, strerror (errno));
			exit (-1);
		}

		oldval &= ~0xf000;
		iocval |= oldval;

		if ((iocret = ioctl (fd, METEORSINPUT, &iocval)) < 0) {
			fprintf (stderr, "%s: %s\r\n", 
				 bktr, strerror (errno));
			exit (-1);
		}
	}

	if ((iocret = ioctl (fd, METEORGINPUT, &iocval)) < 0) {
		fprintf (stderr, "%s: %s\r\n", 
			 bktr, strerror (errno));
		exit (-1);
	}

	printf ("input channel = %d %lx %lu\n", 
		iocret, iocval, (iocval >> 12));

	exit (0);
}

