sndiomixer.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2009 Jacob Meuser <jakemsr@sdf.lonestar.org>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. # include "config.h"
  18. #endif
  19. #ifdef WANT_SNDIO
  20. #include <poll.h>
  21. #include <string.h>
  22. #include <sndio.h>
  23. #include "nmixer.h"
  24. void *readVols(void *v)
  25. {
  26. struct sdata *s = (struct sdata *)v;
  27. struct pollfd pfd;
  28. nfds_t nfds;
  29. char buf[1], data[2], status = 0;
  30. int count = 0;
  31. while (s->run_rt) {
  32. nfds = mio_pollfd(s->hdl, &pfd, POLLIN);
  33. if (poll(&pfd, nfds, 100) < 0)
  34. break;
  35. if (!(mio_revents(s->hdl, &pfd) & POLLIN))
  36. continue;
  37. if (!mio_read(s->hdl, buf, 1))
  38. break;
  39. if (buf[0] & 0x80) { /* status byte */
  40. status = buf[0];
  41. count = 0;
  42. continue;
  43. }
  44. if ((status & 0xf0) != 0xb0) /* not a controller */
  45. continue;
  46. data[count++] = buf[0];
  47. if (count < 2) /* message not complete */
  48. continue;
  49. if (data[0] != 0x07) /* not volume */
  50. continue;
  51. s->cvol[status & 0xf] = data[1] * 100 / 127;
  52. count = 0;
  53. }
  54. pthread_exit(NULL);
  55. }
  56. SndioMixer::SndioMixer(const char *mixerDevice, baseMixer *next) : baseMixer(mixerDevice, next)
  57. {
  58. int i;
  59. s.hdl = mio_open(mixerDevice, MIO_OUT | MIO_IN, 0);
  60. if (!s.hdl) {
  61. /* printf("could not open aucat volume socket\n"); */
  62. return;
  63. }
  64. s.dev = strdup(mixerDevice);
  65. /* separate device for each channel */
  66. for (i = 0; i < 16; i++) {
  67. AddDevice(i);
  68. /* not sure how to get volume of already connected clients */
  69. s.cvol[i] = 100;
  70. }
  71. s.run_rt = 1;
  72. pthread_create(&rt, NULL, readVols, (void *)&s);
  73. }
  74. SndioMixer::~SndioMixer()
  75. {
  76. s.run_rt = 0;
  77. pthread_join(rt, NULL);
  78. if (s.hdl)
  79. mio_close(s.hdl);
  80. s.hdl = NULL;
  81. }
  82. bool SndioMixer::Set(int device, struct volume *vol)
  83. {
  84. char msg[3];
  85. int v, vm;
  86. if (!s.hdl)
  87. return false;
  88. if (device < 0 || device > 16)
  89. return false;
  90. v = (vol->left + vol->right) / 2;
  91. vm = v * 127 / 100;
  92. if (vm < 1)
  93. vm = 1;
  94. else if (vm > 127)
  95. vm = 127;
  96. msg[0] = 0xb0 | device;
  97. msg[1] = 0x07;
  98. msg[2] = vm;
  99. if (mio_write(s.hdl, msg, 3) != 3) {
  100. /* printf("mio_write failed\n"); */
  101. return false;
  102. }
  103. s.cvol[device] = v;
  104. return true;
  105. }
  106. bool SndioMixer::Get(int device, struct volume *vol)
  107. {
  108. if (!s.hdl)
  109. return false;
  110. if (device < 0 || device > 16)
  111. return false;
  112. vol->left = vol->right = s.cvol[device];
  113. return true;
  114. }
  115. const char *SndioMixer::Label(int device)
  116. {
  117. char *dev;
  118. dev = (char *)malloc(128);
  119. snprintf(dev, 128, "%s.%d", s.dev, device);
  120. return dev;
  121. }
  122. #endif /* WANT_SNDIO */