#include <string.h>
#include <stdlib.h>
#include <stdio.h>
+#include <yajl/yajl_gen.h>
+#include <yajl/yajl_version.h>
#ifdef LINUX
#include <alsa/asoundlib.h>
#include <alloca.h>
#endif
+#ifdef __FreeBSD__
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/soundcard.h>
+#endif
+
+#ifdef __OpenBSD__
+#include <fcntl.h>
+#include <unistd.h>
+#include <soundcard.h>
+#endif
+
#include "i3status.h"
#include "queue.h"
}
#endif
-void print_volume(const char *fmt, const char *device, const char *mixer, int mixer_idx) {
-/* Printing volume only works with ALSA at the moment */
+void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx) {
+ char *outwalk = buffer;
+
+ /* Printing volume only works with ALSA at the moment */
+ if (output_format == O_I3BAR) {
+ char *instance;
+ asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
+ INSTANCE(instance);
+ free(instance);
+ }
#ifdef LINUX
/* Check if we already opened the mixer and get the handle
* from cache if so */
const char *walk = fmt;
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
- putchar(*walk);
+ *(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "volume")) {
- printf("%d%%", avg);
+ outwalk += sprintf(outwalk, "%d%%", avg);
walk += strlen("volume");
}
}
#endif
+#if defined(__FreeBSD__) || defined(__OpenBSD__)
+ char mixerpath[] = "/dev/mixer";
+ int mixfd, vol, devmask = 0;
+
+ if ((mixfd = open(mixerpath, O_RDWR)) < 0)
+ return;
+ if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
+ return;
+ if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
+ return;
+
+ const char *walk = fmt;
+ for (; *walk != '\0'; walk++) {
+ if (*walk != '%') {
+ *(outwalk++) = *walk;
+ continue;
+ }
+ if (BEGINS_WITH(walk+1, "volume")) {
+ outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
+ walk += strlen("volume");
+ }
+ }
+ close(mixfd);
+#endif
+
+ *outwalk = '\0';
+ OUTPUT_FULL_TEXT(buffer);
}