tstatus/alsa.c

76 lines
1.4 KiB
C
Raw Normal View History

2022-09-25 10:35:26 -05:00
/* see LICENSE file for details on license */
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <alsa/asoundlib.h>
#include <alloca.h>
2022-09-25 10:35:26 -05:00
#include "module.h"
int audio_volume(long *outvol) {
int ret = 0;
snd_mixer_t *handle;
snd_mixer_elem_t *elem;
snd_mixer_selem_id_t *sid;
static const char *mix_name = "Master";
static const char *card = "default";
static int mix_index = 0;
long minv, maxv;
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, mix_index);
snd_mixer_selem_id_set_name(sid, mix_name);
if((snd_mixer_open(&handle, 0)) < 0)
return -1;
if((snd_mixer_attach(handle, card)) < 0) {
snd_mixer_close(handle);
return -1;
}
if((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
snd_mixer_close(handle);
return -1;
}
ret = snd_mixer_load(handle);
if(ret < 0) {
snd_mixer_close(handle);
return -1;
}
elem = snd_mixer_find_selem(handle, sid);
if(!elem) {
snd_mixer_close(handle);
return -1;
}
snd_mixer_selem_get_playback_volume_range (elem, &minv, &maxv);
if(snd_mixer_selem_get_playback_volume(elem, 0, outvol) < 0) {
snd_mixer_close(handle);
return -1;
}
/* make the value bound to 100 */
*outvol -= minv;
maxv -= minv;
minv = 0;
*outvol = 100 * (*outvol) / maxv;
snd_mixer_close(handle);
return 0;
}
int alsa_update(struct module *module) {
long vol = -1;
audio_volume(&vol);
snprintf((char *)&module->buffer, MODULE_BUFFER_LEN, "%li", vol);
2022-09-25 10:35:26 -05:00
return 0;
}