volume.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <thread.h>
  4. #include <fcall.h>
  5. #include "pool.h"
  6. #include "playlist.h"
  7. int minvolume, maxvolume;
  8. void
  9. volumeproc(void *)
  10. {
  11. int fd, n, nf, i, nlines;
  12. static char buf[1024];
  13. char *lines[32];
  14. char *fields[8];
  15. char *subfields[9];
  16. int volume[8], nvolumes;
  17. threadsetname("volumeproc");
  18. close(srvfd[1]);
  19. fd = open("/dev/audioctl", OREAD);
  20. if (fd < 0)
  21. threadexits(nil);
  22. for(;;){
  23. n = read(fd, buf, sizeof buf -1);
  24. if (n == 0)
  25. continue;
  26. if (n < 0){
  27. fprint(2, "volumeproc: read: %r\n");
  28. threadexits("volumeproc");
  29. }
  30. buf[n] = '\0';
  31. nlines = getfields(buf, lines, nelem(lines), 1, "\n");
  32. for(i = 0; i < nlines; i++){
  33. nf = tokenize(lines[i], fields, nelem(fields));
  34. if (nf == 0)
  35. continue;
  36. if (nf != 6 || strcmp(fields[0], "volume") || strcmp(fields[1], "out"))
  37. continue;
  38. minvolume = strtol(fields[3], nil, 0);
  39. maxvolume = strtol(fields[4], nil, 0);
  40. if (minvolume >= maxvolume)
  41. continue;
  42. nvolumes = tokenize(fields[2], subfields, nelem(subfields));
  43. if (nvolumes <= 0 || nvolumes > 8)
  44. sysfatal("bad volume data");
  45. if (debug)
  46. fprint(2, "volume changed to '");
  47. for (i = 0; i < nvolumes; i++){
  48. volume[i] = strtol(subfields[i], nil, 0);
  49. volume[i]= 100*(volume[i]- minvolume)/(maxvolume-minvolume);
  50. if (debug)
  51. fprint(2, " %d", volume[i]);
  52. }
  53. if (debug)
  54. fprint(2, "'\n");
  55. while (i < 8)
  56. volume[i++] = Undef;
  57. send(volumechan, volume);
  58. }
  59. }
  60. }
  61. void
  62. volumeset(int *v)
  63. {
  64. int fd, i;
  65. char buf[256], *p;
  66. fd = open("/dev/audioctl", OWRITE);
  67. if (fd < 0){
  68. fd = open("/dev/volume", OWRITE);
  69. if (fd < 0){
  70. fprint(2, "Can't set volume: %r");
  71. return;
  72. }
  73. fprint(fd, "audio out %d", v[0]);
  74. v[1] = Undef;
  75. send(volumechan, volume);
  76. } else {
  77. p = buf;
  78. for (i = 0; i < 8; i++){
  79. if (v[i] == Undef) break;
  80. p = seprint(p, buf+sizeof buf, (p==buf)?"volume out '%d":" %d",
  81. minvolume + v[i] * (maxvolume-minvolume) / 100);
  82. }
  83. p = seprint(p, buf+sizeof buf, "'\n");
  84. write(fd, buf, p-buf);
  85. }
  86. close(fd);
  87. }