0

Hi I am trying to write a status bar for my window manager in C, the sound applet fails. I am using alsa and I am getting this error:

ALSA lib conf.c:5162:(_snd_config_evaluate) Cannot open shared library (null) (Error loading shared library /lib/libasound.so.2: No file descriptors available)
ALSA lib conf.c:5690:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib control.c:1528:(snd_ctl_open_noupdate) Invalid CTL default
ALSA lib conf.c:5162:(_snd_config_evaluate) Cannot open shared library (null) (Error loading shared library /lib/libasound.so.2: No file descriptors available)
ALSA lib conf.c:5690:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib control.c:1528:(snd_ctl_open_noupdate) Invalid CTL default

The function:

static inline long volume_percent(long volume, long max) {
    return (long) ((double) volume / (double) max * 100);
}

long get_sound_perecent() {
    snd_mixer_t *mixer;
    long ret = -1;
    if (!snd_mixer_open(&mixer, 1) && !snd_mixer_attach(mixer, "default")) {
        if (!snd_mixer_selem_register(mixer, NULL, NULL) 
                && !snd_mixer_load (mixer)) {
            snd_mixer_selem_id_t *sid;
            snd_mixer_selem_id_alloca(&sid);
            snd_mixer_selem_id_set_index(sid, 0);
            snd_mixer_selem_id_set_name(sid, "Master");
            snd_mixer_elem_t *elem = snd_mixer_find_selem(mixer, sid);
            if (elem) {
                long min, max, volume;
                snd_mixer_selem_get_playback_volume_range(elem,
                        &min, &max);
                int err = snd_mixer_selem_get_playback_volume(
                        elem, 
                        SND_MIXER_SCHN_FRONT_LEFT, 
                        &volume);
                ret = !err? volume_percent(volume, max) : -1;
            }
        }
        snd_mixer_close(mixer);
    }
    return ret;
}

It is worth mentioning that, I am calling this function every 10000 microseconds. If you want you can check the full code on https://github.com/RHL120/status120. Thanks for helping me out! EDIT: I forgot to mention that it works for a while but then it stops and prints the errors.

4

0 回答 0