volume.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* See LICENSE file for copyright and license details. */
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/ioctl.h>
  6. #include <unistd.h>
  7. #include "../util.h"
  8. #if defined(__OpenBSD__)
  9. #include <sys/audioio.h>
  10. const char *
  11. vol_perc(const char *card)
  12. {
  13. static int cls = -1;
  14. mixer_devinfo_t mdi;
  15. mixer_ctrl_t mc;
  16. int afd = -1, m = -1, v = -1;
  17. if ((afd = open(card, O_RDONLY)) < 0) {
  18. warn("open '%s':", card);
  19. return NULL;
  20. }
  21. for (mdi.index = 0; cls == -1; mdi.index++) {
  22. if (ioctl(afd, AUDIO_MIXER_DEVINFO, &mdi) < 0) {
  23. warn("ioctl 'AUDIO_MIXER_DEVINFO':");
  24. close(afd);
  25. return NULL;
  26. }
  27. if (mdi.type == AUDIO_MIXER_CLASS &&
  28. !strncmp(mdi.label.name,
  29. AudioCoutputs,
  30. MAX_AUDIO_DEV_LEN))
  31. cls = mdi.index;
  32. }
  33. for (mdi.index = 0; v == -1 || m == -1; mdi.index++) {
  34. if (ioctl(afd, AUDIO_MIXER_DEVINFO, &mdi) < 0) {
  35. warn("ioctl 'AUDIO_MIXER_DEVINFO':");
  36. close(afd);
  37. return NULL;
  38. }
  39. if (mdi.mixer_class == cls &&
  40. ((mdi.type == AUDIO_MIXER_VALUE &&
  41. !strncmp(mdi.label.name,
  42. AudioNmaster,
  43. MAX_AUDIO_DEV_LEN)) ||
  44. (mdi.type == AUDIO_MIXER_ENUM &&
  45. !strncmp(mdi.label.name,
  46. AudioNmute,
  47. MAX_AUDIO_DEV_LEN)))) {
  48. mc.dev = mdi.index, mc.type = mdi.type;
  49. if (ioctl(afd, AUDIO_MIXER_READ, &mc) < 0) {
  50. warn("ioctl 'AUDIO_MIXER_READ':");
  51. close(afd);
  52. return NULL;
  53. }
  54. if (mc.type == AUDIO_MIXER_VALUE)
  55. v = mc.un.value.num_channels == 1 ?
  56. mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] :
  57. (mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] >
  58. mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] ?
  59. mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] :
  60. mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
  61. else if (mc.type == AUDIO_MIXER_ENUM)
  62. m = mc.un.ord;
  63. }
  64. }
  65. close(afd);
  66. return bprintf("%d", m ? 0 : v * 100 / 255);
  67. }
  68. #else
  69. #include <sys/soundcard.h>
  70. const char *
  71. vol_perc(const char *card)
  72. {
  73. size_t i;
  74. int v, afd, devmask;
  75. char *vnames[] = SOUND_DEVICE_NAMES;
  76. if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) {
  77. warn("open '%s':", card);
  78. return NULL;
  79. }
  80. if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
  81. warn("ioctl 'SOUND_MIXER_READ_DEVMASK':");
  82. close(afd);
  83. return NULL;
  84. }
  85. for (i = 0; i < LEN(vnames); i++) {
  86. if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
  87. if (ioctl(afd, MIXER_READ(i), &v) < 0) {
  88. warn("ioctl 'MIXER_READ(%ld)':", i);
  89. close(afd);
  90. return NULL;
  91. }
  92. }
  93. }
  94. close(afd);
  95. return bprintf("%d", v & 0xff);
  96. }
  97. #endif