#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dev/video/meteor/ioctl_meteor.h>
#include <dev/video/bktr/ioctl_bt848.h>
static void
usage(void)
{
fprintf(stderr,
"usage: cxm_setchannel [-a {on|off}] [-c | -r | -s | -t] [-d unit] [-g geom]\n"
" [-m chnl_set] [chnl | freq]\n");
}
int
main(int argc, char *argv[])
{
char *ptr;
char *endptr;
char buf[255];
int afc;
int audio;
int c;
int channel_set;
int i;
int status;
int tfd;
int unit;
unsigned int channel;
unsigned int fraction;
unsigned int freq;
unsigned int x_size;
unsigned int y_size;
unsigned long device;
struct bktr_capture_area cap;
afc = -1;
audio = -1;
channel = 0;
channel_set = -1;
device = 0;
unit = 0;
freq = 0;
status = 0;
x_size = 0;
y_size = 0;
while ((c = getopt(argc, argv, "a:crstg:m:d:")) != -1)
switch (c) {
case 'a':
if (strcasecmp(optarg, "on") == 0)
afc = 1;
else if (strcasecmp(optarg, "off") == 0)
afc = 0;
else {
usage();
exit(1);
}
break;
case 'c':
device = METEOR_INPUT_DEV2;
audio = -1;
break;
case 'd':
unit = atoi(optarg);
break;
case 'r':
device = 0;
audio = AUDIO_INTERN;
break;
case 's':
device = METEOR_INPUT_DEV_SVIDEO;
audio = -1;
break;
case 't':
device = METEOR_INPUT_DEV1;
audio = -1;
break;
case 'g':
if (sscanf(optarg, "%ux%u", &x_size, &y_size) != 2 ||
x_size == 0 || y_size == 0) {
usage();
exit(1);
}
break;
case 'm':
channel_set = atoi(optarg);
if (channel_set < 0 || channel_set > CHNLSET_MAX) {
usage();
exit(1);
}
break;
default:
usage();
exit(1);
}
if (optind < argc) {
if ((ptr = strchr(argv[optind], '.')) != NULL) {
freq = strtol(argv[optind], &endptr, 10) * 1000;
if (ptr != endptr) {
usage();
exit(1);
}
ptr++;
fraction = strtol(ptr, &endptr, 10);
if (!isdigit(*ptr) || *endptr != '\0') {
usage();
exit(1);
}
for (i = endptr - ptr; i > 3; i--)
fraction /= 10;
for (; i < 3; i++)
fraction *= 10;
freq += fraction;
}
else
channel = atoi(argv[optind]);
}
if (afc == -1 && audio == -1 && !device && x_size == 0 &&
y_size == 0 && channel_set == -1 && !channel && !freq) {
usage();
exit(1);
}
sprintf(buf, "/dev/cxm%d", unit);
tfd = open(buf, O_RDONLY);
if (tfd < 0) {
warn("open() of /dev/cxm%d failed.", unit);
exit(1);
}
if (afc != -1) {
if (ioctl(tfd, TVTUNER_SETAFC, &afc) < 0) {
warn("ioctl( tfd, TVTUNER_SETAFC ) failed.");
status = 1;
}
}
if (device) {
if (ioctl(tfd, METEORSINPUT, &device) < 0) {
warn("ioctl( tfd, METEORSINPUT ) failed.");
status = 1;
}
}
if (audio != -1) {
if (ioctl(tfd, BT848_SAUDIO, &audio) < 0) {
warn("ioctl( tfd, BT848_SAUDIO ) failed.");
status = 1;
}
}
if (ioctl(tfd, BT848_GAUDIO, &audio) < 0) {
warn("ioctl( tfd, BT848_GAUDIO ) failed.");
status = 1;
}
if (x_size && y_size) {
memset(&cap, 0, sizeof(cap));
cap.x_size = x_size;
cap.y_size = y_size;
if (ioctl(tfd, BT848_SCAPAREA, &cap) < 0) {
warn("ioctl( tfd, BT848_SCAPAREA ) failed.");
status = 1;
}
}
if (channel_set != -1) {
if (ioctl(tfd, TVTUNER_SETTYPE, &channel_set) < 0) {
warn("ioctl( tfd, TVTUNER_SETTYPE ) failed.");
status = 1;
}
}
if (channel) {
if (ioctl(tfd, TVTUNER_SETCHNL, &channel) < 0) {
warn("ioctl( tfd, TVTUNER_SETCHNL ) failed.");
status = 1;
}
} else if (freq) {
if (audio == AUDIO_INTERN) {
freq = freq / 10;
if (ioctl(tfd, RADIO_SETFREQ, &freq) < 0) {
warn("ioctl( tfd, RADIO_SETFREQ ) failed.");
status = 1;
}
} else {
freq = (freq * 16) / 1000;
if (ioctl(tfd, TVTUNER_SETFREQ, &freq) < 0) {
warn("ioctl( tfd, TVTUNER_SETFREQ ) failed.");
status = 1;
}
}
}
close(tfd);
exit(status);
}