Bill Nottingham 0e0d15
/* Copyright 2005 Red Hat, Inc.
Bill Nottingham 0e0d15
 *
Bill Nottingham 0e0d15
 * Portions extraced from various ALSA code:
Bill Nottingham 0e0d15
 *  Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
Bill Nottingham 0e0d15
 *                   Jaroslav Kysela <perex@suse.cz>
Bill Nottingham 0e0d15
 *
Bill Nottingham 0e0d15
 * This software may be freely redistributed under the terms of the GNU
Bill Nottingham 0e0d15
 * public license.
Bill Nottingham 0e0d15
 *
Bill Nottingham 0e0d15
 * You should have received a copy of the GNU General Public License
Bill Nottingham 0e0d15
 * along with this program; if not, write to the Free Software
Bill Nottingham 0e0d15
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Bill Nottingham 0e0d15
 *
Bill Nottingham 0e0d15
 */
Bill Nottingham 6c53f8
Bill Nottingham 6c53f8
#include <stdio.h>
Bill Nottingham 6c53f8
#include <stdlib.h>
Bill Nottingham 6c53f8
#include <string.h>
Bill Nottingham 6c53f8
Bill Nottingham 6c53f8
#include <alsa asoundlib.h="">
Bill Nottingham 6c53f8
Bill Nottingham 6c53f8
#define ALSA_CONFIG_PATH	"/etc/asound.state"
Bill Nottingham 6c53f8
Bill Nottingham 6c53f8
int get_card_number()
Bill Nottingham 6c53f8
{
Martin Stransky 2351ac
  char *devname, *action;
Martin Stransky 2351ac
Martin Stransky 2351ac
  action = getenv("ACTION");
Martin Stransky 2351ac
  if (!action || strcmp(action, "add"))
Martin Stransky 2351ac
    return -1;
Martin Stransky 2351ac
  devname = getenv("DEVNAME");
Martin Stransky 2351ac
  if (!devname)
Martin Stransky 2351ac
    return -1;
Martin Stransky 2351ac
  if (!strncmp(devname, "/dev/snd/controlC", 17))
Martin Stransky 2351ac
    return atoi(devname + 17);
Martin Stransky 2351ac
  if (!strncmp(devname, "/dev/snd/pcmC", 13))
Martin Stransky 2351ac
    return atoi(devname + 13);
Martin Stransky 2351ac
  return -1;
Bill Nottingham 6c53f8
}
Bill Nottingham 6c53f8
Bill Nottingham 6c53f8
int has_config(int index)
Bill Nottingham 6c53f8
{
Martin Stransky 2351ac
  int rc = 0;
Martin Stransky 2351ac
  snd_config_t *config, *control;
Martin Stransky 2351ac
  snd_input_t *in;
Martin Stransky 2351ac
  snd_ctl_t *handle;
Martin Stransky 2351ac
  snd_ctl_card_info_t *info;
Martin Stransky 2351ac
  const char *id;
Martin Stransky 2351ac
  char path[32];
Martin Stransky 2351ac
Martin Stransky 2351ac
  rc = snd_config_top(&config);
Martin Stransky 2351ac
  if (rc < 0)
Martin Stransky 2351ac
    goto out;
Martin Stransky 2351ac
  rc = snd_input_stdio_open(&in, ALSA_CONFIG_PATH, "r");
Martin Stransky 2351ac
  if (rc >= 0) {
Martin Stransky 2351ac
    rc = snd_config_load(config, in);
Martin Stransky 2351ac
    snd_input_close(in);
Martin Stransky 2351ac
    if (rc < 0)
Martin Stransky 2351ac
      goto out;
Martin Stransky 2351ac
  }
Martin Stransky 2351ac
  sprintf(path, "hw:%d", index);
Martin Stransky 2351ac
  rc = snd_ctl_open(&handle, path, 0);
Martin Stransky 2351ac
  if (rc < 0)
Martin Stransky 2351ac
    goto out;
Martin Stransky 2351ac
  snd_ctl_card_info_alloca(&info);
Martin Stransky 2351ac
  rc = snd_ctl_card_info(handle, info);
Martin Stransky 2351ac
  if (rc < 0)
Martin Stransky 2351ac
    goto out_close;
Martin Stransky 2351ac
  id = snd_ctl_card_info_get_id(info);
Martin Stransky 2351ac
  rc = snd_config_searchv(config, &control, "state", id, "control", 0);
Bill Nottingham 6c53f8
out_close:
Martin Stransky 2351ac
  snd_ctl_close(handle);
Bill Nottingham 6c53f8
out:
Martin Stransky 2351ac
  return !rc;
Martin Stransky 2351ac
Bill Nottingham 6c53f8
}
Bill Nottingham 6c53f8
Bill Nottingham 6c53f8
int run_alsactl(int index)
Bill Nottingham 6c53f8
{
Martin Stransky 2351ac
  char *args[] = { "/sbin/alsactl", "restore", NULL, NULL };
Martin Stransky 2351ac
  char num[10];
Martin Stransky 2351ac
Martin Stransky 2351ac
  sprintf(num, "%d", index);
Martin Stransky 2351ac
  args[2] = num;
Martin Stransky 2351ac
  execv(args[0], args);
Martin Stransky 2351ac
  return 1;
Bill Nottingham 6c53f8
}
Bill Nottingham 6c53f8
Martin Stransky 2351ac
void frob_mixer(int index)
Bill Nottingham 6c53f8
{
Martin Stransky 2351ac
  char tmp[100];  
Martin Stransky 2351ac
  snprintf(tmp, 100, "%d", index);
Martin Stransky 2351ac
  tmp[99] = '\0';
Bill Nottingham 6a536a
  execl("/bin/alsaunmute","/bin/alsaunmute", tmp, "-v", NULL);
Bill Nottingham 6c53f8
}
Bill Nottingham 6c53f8
Bill Nottingham 6c53f8
int main(int argc, char **argv)
Bill Nottingham 6c53f8
{
Martin Stransky 2351ac
  int i;
Martin Stransky 2351ac
Martin Stransky 2351ac
  i = get_card_number();
Martin Stransky 2351ac
  if (i < 0)
Martin Stransky 2351ac
    return 0;
Martin Stransky 2351ac
  if (has_config(i))
Martin Stransky 2351ac
    return run_alsactl(i);
Martin Stransky 2351ac
  else
Martin Stransky 2351ac
    frob_mixer(i);
Martin Stransky 2351ac
  
Martin Stransky 2351ac
  return(0);
Bill Nottingham 6c53f8
}