Since FreeBSD 14.0 the base system offers a mixer library.

---
 components/volume.c | 23 +++++++++++++++++++++++
 config.mk           |  1 +
 2 files changed, 24 insertions(+)

diff --git a/components/volume.c b/components/volume.c
index b6665da..45bbc09 100644
--- a/components/volume.c
+++ b/components/volume.c
@@ -2,6 +2,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/param.h> /* for __FreeBSD_version */
 #include <sys/ioctl.h>
 #include <unistd.h>
 
@@ -180,6 +181,28 @@
 
                return bprintf("%d", value);
        }
+#elif defined(__FreeBSD__) && (__FreeBSD_version >= 1400000)
+       #include <mixer.h>
+
+       const char *
+       vol_perc(const char *card)
+       {
+               struct mixer *m;
+               float v;
+               int n = 0;
+
+               if ((m = mixer_open(card)) == NULL) {
+                       warn("mixer_open: %s", card);
+                       return NULL;
+               }
+               if (!MIX_ISMUTE(m, m->dev->devno)) {
+                       v = (m->dev->vol.left + m->dev->vol.right) / 2.0f;
+                       n = MIX_VOLDENORM(v);
+               }
+               (void)mixer_close(m);
+
+               return bprintf("%d", n);
+       }
 #else
        #include <sys/soundcard.h>
 
diff --git a/config.mk b/config.mk
index 2516e6e..617afe6 100644
--- a/config.mk
+++ b/config.mk
@@ -16,6 +16,7 @@ CFLAGS   = -std=c99 -pedantic -Wall -Wextra -Os
 LDFLAGS  = -L$(X11LIB) -s
 # OpenBSD: add -lsndio
 # FreeBSD: add -lkvm
+#         if >= 14.0 also -lmixer
 LDLIBS   = -lX11
 
 # compiler and linker
-- 
2.32.0
>From 7db2c6b6b9c46a1a46071673c6aa1bc845ff48e6 Mon Sep 17 00:00:00 2001
From: Christos Margiolis <chris...@margiolis.net>
Date: Fri, 15 Oct 2021 02:54:11 +0300
Subject: [PATCH] components/volume: Add FreeBSD-specific code

---
 components/volume.c | 23 +++++++++++++++++++++++
 config.mk           |  1 +
 2 files changed, 24 insertions(+)

diff --git a/components/volume.c b/components/volume.c
index b6665da..45bbc09 100644
--- a/components/volume.c
+++ b/components/volume.c
@@ -2,6 +2,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/param.h> /* for __FreeBSD_version */
 #include <sys/ioctl.h>
 #include <unistd.h>
 
@@ -180,6 +181,28 @@
 
 		return bprintf("%d", value);
 	}
+#elif defined(__FreeBSD__) && (__FreeBSD_version >= 1400000)
+	#include <mixer.h>
+
+	const char *
+	vol_perc(const char *card)
+	{
+		struct mixer *m;
+		float v;
+		int n = 0;
+
+		if ((m = mixer_open(card)) == NULL) {
+			warn("mixer_open: %s", card);
+			return NULL;
+		}
+		if (!MIX_ISMUTE(m, m->dev->devno)) {
+			v = (m->dev->vol.left + m->dev->vol.right) / 2.0f;
+			n = MIX_VOLDENORM(v);
+		}
+		(void)mixer_close(m);
+
+		return bprintf("%d", n);
+	}
 #else
 	#include <sys/soundcard.h>
 
diff --git a/config.mk b/config.mk
index 2516e6e..617afe6 100644
--- a/config.mk
+++ b/config.mk
@@ -16,6 +16,7 @@ CFLAGS   = -std=c99 -pedantic -Wall -Wextra -Os
 LDFLAGS  = -L$(X11LIB) -s
 # OpenBSD: add -lsndio
 # FreeBSD: add -lkvm
+# 	   if >= 14.0 also -lmixer
 LDLIBS   = -lX11
 
 # compiler and linker
-- 
2.32.0

Reply via email to