s2dblocks

statusbar blocks for dwm
Log | Files | Refs

commit 716610af5291e311a921a3e683914d4332b894a0
parent c622ae87b9997ebeef692dedc7bcf2b279668c33
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 14 Apr 2022 13:27:08 +0100

Audio/vol handler

Diffstat:
Ahandlers/vol.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 102 insertions(+), 0 deletions(-)

diff --git a/handlers/vol.c b/handlers/vol.c @@ -0,0 +1,102 @@ +#include <stddef.h> +#ifdef __linux__ +#include <alsa/asoundlib.h> +#endif /* __linux__ */ +#include "../dwmbar.h" + +void +draw_on(void) { + s2d_rect(11, 3, 1, 3); + s2d_rect(2, 4, 3, 1); + s2d_rect(9, 4, 1, 2); + s2d_rect(1, 5, 1, 5); + s2d_rect(5, 5, 1, 5); + s2d_rect(7, 5, 1, 1); + s2d_rect(8, 6, 1, 3); + s2d_rect(10, 6, 1, 3); + s2d_rect(12, 6, 1, 3); + s2d_rect(7, 9, 1, 1); + s2d_rect(9, 9, 1, 2); + s2d_rect(11, 9, 1, 3); + s2d_rect(2, 10, 4, 1); +}; + +void +draw_off(void) { + s2d_rect(2, 4, 4, 1); + s2d_rect(1, 5, 1, 5); + s2d_rect(5, 5, 1, 5); + s2d_rect(7, 5, 1, 1); + s2d_rect(11, 5, 1, 1); + s2d_rect(8, 6, 1, 1); + s2d_rect(10, 6, 1, 1); + s2d_rect(9, 7, 1, 1); + s2d_rect(8, 8, 1, 1); + s2d_rect(10, 8, 1, 1); + s2d_rect(7, 9, 1, 1); + s2d_rect(11, 9, 1, 1); + s2d_rect(2, 10, 4, 1); +} + +int +main(void) { + int on = 0, percent = 0; + int ret; +#ifdef __linux__ + long min, max, vol; + snd_mixer_t *handle; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + + if ((ret = snd_mixer_open(&handle, 0)) < 0) { + s2d_print("could not open mixer: %s\n", snd_strerror(ret)); + s2d_finish(); + return 1; + } + if ((ret = snd_mixer_attach(handle, "default")) < 0) { + s2d_print("could not attach mixer: %s\n", snd_strerror(ret)); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + if ((ret = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { + s2d_print("could not register element: %s\n", snd_strerror(ret)); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + if ((ret = snd_mixer_load(handle)) < 0) { + s2d_print("could not load mixer: %s\n", snd_strerror(ret)); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + snd_mixer_selem_id_malloc(&sid); + snd_mixer_selem_id_set_index(sid, 0); + snd_mixer_selem_id_set_name(sid, "Master"); + if (!(elem = snd_mixer_find_selem(handle, sid))) { + s2d_print("could not find element 'Master'"); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_get_playback_volume(elem, 0, &vol); + snd_mixer_selem_get_playback_switch(elem, 0, &on); + percent = 100 * vol / max; + snd_mixer_selem_id_free(sid); + snd_mixer_close(handle); +#else + s2d_print("no handler for this OS"); + s2d_finish(); +#endif + if (on) + draw_on(); + else + draw_off(); + s2d_forward(-1); + s2d_forward(2); + s2d_bg(BRBG); + s2d_bar(0, 2, 3, bar_height - 4, 1, percent); + s2d_finish(); +}