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