diff options
author | Ali H. Fardan <raiz@firemail.cc> | 2016-09-05 01:18:55 +0300 |
---|---|---|
committer | Ali H. Fardan <raiz@firemail.cc> | 2016-09-05 01:18:55 +0300 |
commit | 52d19f955e67712eab50f582046013246b6bc75d (patch) | |
tree | 2e586cf1f5c94f704ae613170ee8f5a9283b462a /slstatus.c | |
parent | 9fa858daeafb5398cdf19af9483b2854fe6e9772 (diff) |
imported a new vol_perc() function, this should fix #12 (UNTESTED)
Diffstat (limited to 'slstatus.c')
-rw-r--r-- | slstatus.c | 57 |
1 files changed, 22 insertions, 35 deletions
@@ -481,50 +481,37 @@ uid(void) } -static char * -vol_perc(const char *soundcard) -{ - /* - * TODO: FIXME: - * https://github.com/drkh5h/slstatus/issues/12 - */ - int mute = 0; - long vol = 0, max = 0, min = 0; +static char * +vol_perc(const char *snd_card) +{ /* thanks to botika for this function */ + long int vol, max, min; snd_mixer_t *handle; - snd_mixer_elem_t *pcm_mixer, *mas_mixer; - snd_mixer_selem_id_t *vol_info, *mute_info; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *s_elem; snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, soundcard); + snd_mixer_attach(handle, snd_card); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); + snd_mixer_selem_id_malloc(&s_elem); + snd_mixer_selem_id_set_name(s_elem, "Master"); + elem = snd_mixer_find_selem(handle, s_elem); - snd_mixer_selem_id_malloc(&vol_info); - snd_mixer_selem_id_malloc(&mute_info); - if (vol_info == NULL || mute_info == NULL) { - fprintf(stderr, "Could not get alsa volume.\n"); + if (elem == NULL) { + snd_mixer_selem_id_free(s_elem); + snd_mixer_close(handle); + perror("alsa error: "); return smprintf(UNKNOWN_STR); } - snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); - snd_mixer_selem_id_set_name(mute_info, ALSA_CHANNEL); - pcm_mixer = snd_mixer_find_selem(handle, vol_info); - mas_mixer = snd_mixer_find_selem(handle, mute_info); - - snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); - snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); - snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); - - if (vol_info) - snd_mixer_selem_id_free(vol_info); - if (mute_info) - snd_mixer_selem_id_free(mute_info); - if (handle) - snd_mixer_close(handle); - if (!mute) - return smprintf("mute"); - else - return smprintf("%d%%", (vol * 100) / max); + snd_mixer_handle_events(handle); + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_get_playback_volume(elem, 0, &vol); + + snd_mixer_selem_id_free(s_elem); + snd_mixer_close(handle); + + return smprintf("%d", (vol * 100) / max); } static char * |