sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

commit 655fa298091591c3d21f2c47c3615b52c2f2a192
parent 449890cd3657c607f2546cb6299e640a7ab74352
Author: wifiextender <router@archlinux.info>
Date:   Fri, 22 Jul 2016 00:10:05 +0200

New helper function

Diffstat:
Mdwm.suckless.org/dwmstatus/index.md | 1+
Adwm.suckless.org/dwmstatus/volume.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/dwm.suckless.org/dwmstatus/index.md b/dwm.suckless.org/dwmstatus/index.md @@ -35,6 +35,7 @@ Helper functions If you have simple C functions for gathering system information, please add them here as file or as code example. +* [Basic ALSA Volume API example with error checks and handling](volume.c) * [Support for ACPI battery status Linux](new-acpi-battery.c) * [Reading out a temperature from /sys](dwmstatus-temperature.c) * [Reading up-, and downspeeds of all network interfaces from /proc/net](dwmstatus-netusage.c) diff --git a/dwm.suckless.org/dwmstatus/volume.c b/dwm.suckless.org/dwmstatus/volume.c @@ -0,0 +1,100 @@ +/* + Copyright 07/20/2016 Aaron Caffrey https://github.com/wifiextender + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + 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., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. + + Compile with: + gcc -Wall -Wextra -O2 volume.c -o test -lasound +*/ + +#include <stdio.h> +#include <stdlib.h> + +#include <alsa/asoundlib.h> + +#define ALSA_ERR "Error: alsa failed" + +void exit_with_err(const char *); +void get_sound(void); + +int main(void) { + get_sound(); + return EXIT_SUCCESS; +} + +void +exit_with_err(const char *str1) { + printf("%s\n", str1); + exit(EXIT_FAILURE); +} + +void +get_sound(void) { + snd_mixer_t *handle = NULL; + snd_mixer_elem_t *elem = NULL; + snd_mixer_selem_id_t *s_elem = NULL; + long int vol, max, min; + + if (0 < (snd_mixer_open(&handle, 0))) { + exit_with_err(ALSA_ERR); + } + + if (0 < (snd_mixer_attach(handle, "default"))) { + goto error; + } + + if (0 < (snd_mixer_selem_register(handle, NULL, NULL))) { + goto error; + } + + if (0 < (snd_mixer_load(handle))) { + goto error; + } + + snd_mixer_selem_id_malloc(&s_elem); + if (NULL == s_elem) { + goto error; + } + + snd_mixer_selem_id_set_name(s_elem, "Master"); + if (NULL == (elem = snd_mixer_find_selem(handle, s_elem))) { + goto error; + } + + /* Use `set' to change the current volume value */ + if (0 < (snd_mixer_selem_get_playback_volume(elem, 0, &vol))) { + goto error; + } + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + + snd_mixer_selem_id_free(s_elem); + snd_mixer_close(handle); + + printf("%s %ld%%\n", "Volume set to", (vol * 100) / max); + return; + +/* stay cheesy no matter what */ +error: + if (NULL != s_elem) { + snd_mixer_selem_id_free(s_elem); + s_elem = NULL; + } + if (NULL != handle) { + snd_mixer_close(handle); + handle = NULL; + } + exit_with_err(ALSA_ERR); +}