default.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <err.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/stat.h>
  7. #include "../../lib/util.h"
  8. #include "../../components_config.h"
  9. #ifndef VOLUME_SYM
  10. # define VOLUME_SYM ""
  11. #endif
  12. #ifndef VOLUME_PERCENT
  13. # define VOLUME_PERCENT " %"
  14. #endif
  15. #include <sys/soundcard.h>
  16. void
  17. vol_perc(char *out, const char *card, uint32_t __unused _i, void __unused *_p)
  18. {
  19. size_t i;
  20. int v, afd, devmask;
  21. char *vnames[] = SOUND_DEVICE_NAMES;
  22. if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) {
  23. warn("open '%s'", card);
  24. ERRRET(out);
  25. }
  26. if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
  27. warn("ioctl 'SOUND_MIXER_READ_DEVMASK'");
  28. close(afd);
  29. ERRRET(out);
  30. }
  31. for (i = 0; i < LEN(vnames); i++) {
  32. if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
  33. if (ioctl(afd, MIXER_READ(i), &v) < 0) {
  34. warn("ioctl 'MIXER_READ(%ld)'", i);
  35. close(afd);
  36. ERRRET(out);
  37. }
  38. }
  39. }
  40. close(afd);
  41. bprintf(out, "%s%3d%s", VOLUME_SYM, v & 0xff, VOLUME_PERCENT);
  42. }