
# include <stdio.h>
# include <unistd.h>
# include <fcntl.h>
# include <sys/ioctl.h>
# include <sys/soundcard.h>

# define DEV_MIXER  "/dev/mixer"

/* The following definitions are from include/asm-arm/arch-sa1100/empeg.h
   in the beta9b kernel source */

# define EMPEG_MIXER_MAGIC 'm'

# define EMPEG_MIXER_READ_SOURCE _IOR(EMPEG_MIXER_MAGIC, 0, int)
# define EMPEG_MIXER_WRITE_SOURCE _IOW(EMPEG_MIXER_MAGIC, 0, int)

int main(int argc, char *argv[])
{
  int fd, opt, source = 0;
  char *source_str;

  while ((opt = getopt(argc, argv, "prl")) != -1) {
    switch (opt) {
    case 'p':
      source = SOUND_MASK_PCM;
      break;

    case 'r':
      source = SOUND_MASK_RADIO;
      break;

    case 'l':
      source = SOUND_MASK_LINE;
      break;

    default:
      fprintf(stderr, "Usage: %s [-p|-r|-l]\n", argv[0]);
      return 1;
    }
  }

  fd = open(DEV_MIXER, O_RDONLY);
  if (fd == -1) {
    perror(DEV_MIXER);
    return 2;
  }

  if (source) {
    if (ioctl(fd, EMPEG_MIXER_WRITE_SOURCE, &source) == -1) {
      perror("ioctl(EMPEG_MIXER_WRITE_SOURCE)");
      return 3;
    }
  }

  if (ioctl(fd, EMPEG_MIXER_READ_SOURCE, &source) == -1) {
    perror("ioctl(EMPEG_MIXER_READ_SOURCE)");
    return 4;
  }

  if (source & SOUND_MASK_PCM)
    source_str = "PCM";
  else if (source & SOUND_MASK_RADIO)
    source_str = "radio";
  else if (source & SOUND_MASK_LINE)
    source_str = "line";
  else
    source_str = "unknown";

  printf("%s source selected\n", source_str);

  if (close(fd) == -1) {
    perror("close");
    return 5;
  }

  return 0;
}
