+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+ char *mixerpath;
+ char defaultmixer[] = "/dev/mixer";
+ int mixfd, vol, devmask = 0;
+ pbval = 1;
+
+ if (mixer_idx > 0)
+ asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
+ else
+ mixerpath = defaultmixer;
+
+ if ((mixfd = open(mixerpath, O_RDWR)) < 0)
+ return;
+
+ if (mixer_idx > 0)
+ free(mixerpath);
+
+ if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
+ return;
+ if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
+ return;
+
+ if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
+ START_COLOR("color_degraded");
+ pbval = 0;
+ }
+
+ 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
+
+out:
+ *outwalk = '\0';
+ OUTPUT_FULL_TEXT(buffer);
+
+ if (!pbval)
+ END_COLOR;