1
0
Fork 0

Merge pull request #34 from parazyd/master

refactor vol_perc to not depend on alsa libraries
This commit is contained in:
Aaron Marcher 2016-12-30 23:01:14 +01:00 committed by GitHub
commit 2b148bf2ec
4 changed files with 33 additions and 34 deletions

View file

@ -23,7 +23,7 @@ The following information is included:
- Swap status (free swap, percentage, total swap and used swap) - Swap status (free swap, percentage, total swap and used swap)
- Temperature - Temperature
- Uptime - Uptime
- Volume percentage (ALSA) - Volume percentage (OSS/ALSA)
- WiFi signal percentage and ESSID - WiFi signal percentage and ESSID
Multiple entries per function (e.g. multiple batteries) are supported and everything can be reordered and customized via a C header file (similar to other suckless programs). Multiple entries per function (e.g. multiple batteries) are supported and everything can be reordered and customized via a C header file (similar to other suckless programs).
@ -50,6 +50,16 @@ If you use any other status bar or window manager you will have to figure it out
slstatus -o | other_status_bar & slstatus -o | other_status_bar &
### Specific function quirks
- Volume percentage
If there is no `/dev/mixer` on your system and you use ALSA, it means you have to load the OSS compatibility module by issuing:
```
# modprobe snd-pcm-oss
```
## Contributing ## Contributing
Hunt FIXME's in the code or do WTF you want! If it is useful, I will merge. Hunt FIXME's in the code or do WTF you want! If it is useful, I will merge.

View file

@ -34,7 +34,7 @@
- uid (uid of current user) [argument: NULL] - uid (uid of current user) [argument: NULL]
- uptime (uptime) [argument: NULL] - uptime (uptime) [argument: NULL]
- username (username of current user) [argument: NULL] - username (username of current user) [argument: NULL]
- vol_perc (alsa volume and mute status in percent) [argument: soundcard] - vol_perc (oss/alsa volume status (see README)) [argument: /dev/mixer]
- wifi_perc (wifi signal in percent) [argument: wifi card interface name] - wifi_perc (wifi signal in percent) [argument: wifi card interface name]
- wifi_essid (wifi essid) [argument: wifi card interface name] */ - wifi_essid (wifi essid) [argument: wifi card interface name] */
static const struct arg args[] = { static const struct arg args[] = {

View file

@ -7,7 +7,7 @@ X11INC = /usr/X11R6/include
X11LIB = /usr/X11R6/lib X11LIB = /usr/X11R6/lib
INCS = -I. -I/usr/include -I${X11INC} INCS = -I. -I/usr/include -I${X11INC}
LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE
# -Wno-unused-function for routines not activated by user # -Wno-unused-function for routines not activated by user

View file

@ -1,6 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <alsa/asoundlib.h>
#include <err.h> #include <err.h>
#include <fcntl.h> #include <fcntl.h>
#include <ifaddrs.h> #include <ifaddrs.h>
@ -18,6 +17,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/soundcard.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/utsname.h> #include <sys/utsname.h>
@ -641,41 +641,30 @@ uid(void)
static char * static char *
vol_perc(const char *card) vol_perc(const char *card)
{ {
int mute; unsigned int i;
long int vol, max, min; int v, afd, devmask;
snd_mixer_t *handle; char *vnames[] = SOUND_DEVICE_NAMES;
snd_mixer_elem_t *elem;
snd_mixer_selem_id_t *s_elem;
snd_mixer_open(&handle, 0); afd = open(card, O_RDONLY);
snd_mixer_attach(handle, card); if (afd < 0) {
snd_mixer_selem_register(handle, NULL, NULL); warn("Cannot open %s", card);
snd_mixer_load(handle); return smprintf(UNKNOWN_STR);
snd_mixer_selem_id_malloc(&s_elem);
snd_mixer_selem_id_set_name(s_elem, "Master");
elem = snd_mixer_find_selem(handle, s_elem);
if (elem == NULL) {
snd_mixer_selem_id_free(s_elem);
snd_mixer_close(handle);
warn("Failed to get volume percentage for %s", card);
return smprintf("%s", UNKNOWN_STR);
} }
snd_mixer_handle_events(handle); ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask);
snd_mixer_selem_get_playback_volume_range(elem, &min, &max); for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
snd_mixer_selem_get_playback_volume(elem, 0, &vol); if (devmask & (1 << i)) {
snd_mixer_selem_get_playback_switch(elem, 0, &mute); if (!strcmp("vol", vnames[i])) {
ioctl(afd, MIXER_READ(i), &v);
}
}
}
snd_mixer_selem_id_free(s_elem); close(afd);
snd_mixer_close(handle); if (v == 0) {
if (!mute)
return smprintf("mute"); return smprintf("mute");
else if (max == 0) }
return smprintf("0%%"); return smprintf("%d%%", v & 0xff);
else
return smprintf("%lu%%", ((uint_fast16_t)(vol * 100) / max));
} }
static char * static char *