[hackers] [slstatus][PATCH] NetBSD support excluding bat.c, wifi.c untested

From: Sotiris Lamprinidis <sotiris_AT_lamprinidis.com>
Date: Thu, 27 Jun 2024 13:44:14 +0200

---
 components/cpu.c         | 48 +++++++++++++++++++++++++++
 components/entropy.c     |  2 +-
 components/ip.c          |  3 ++
 components/netspeeds.c   |  2 +-
 components/ram.c         | 72 ++++++++++++++++++++++++++++++++++++++++
 components/swap.c        |  2 +-
 components/temperature.c | 67 +++++++++++++++++++++++++++++++++++++
 components/volume.c      | 24 ++++++++++++++
 components/wifi.c        | 13 ++++++--
 config.def.h             |  4 ++-
 config.mk                |  1 +
 11 files changed, 231 insertions(+), 7 deletions(-)
diff --git a/components/cpu.c b/components/cpu.c
index d0d03c7..0d793cb 100644
--- a/components/cpu.c
+++ b/components/cpu.c
_AT_@ -139,6 +139,54 @@
 			warn("sysctlbyname 'kern.cp_time':");
 			return NULL;
 		}
+		if (b[0] == 0)
+			return NULL;
+
+		sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) -
+		      (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]);
+
+		if (sum == 0)
+			return NULL;
+
+		return bprintf("%d", 100 *
+		               ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
+		                 a[CP_INTR]) -
+		                (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
+		                 b[CP_INTR])) / sum);
+	}
+#elif defined(__NetBSD__)
+  #include <sys/param.h>
+  #include <sys/sched.h>
+  #include <sys/sysctl.h>
+
+  const char *
+  cpu_freq(const char *unused)
+  {
+    int freq;
+    size_t size;
+
+    if (sysctlbyname("machdep.cpu.frequency.current", &freq, &size, NULL, 0) < 0 || !size) {
+      warn("sysctlbyname machdep.cpu.frequency.current");
+    };
+
+    return fmt_human(freq * 1E6, 1000);
+  }
+
+
+	const char *
+	cpu_perc(const char *unused)
+	{
+		static int64_t a[CPUSTATES];
+    size_t size;
+    int64_t b[CPUSTATES], sum;
+
+		size = sizeof(a);
+    memcpy(b, a, sizeof(b));
+		if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) < 0 || !size) {
+			warn("sysctlbyname 'kern.cp_time':");
+			return NULL;
+		}
+
 		if (b[0] == 0)
 			return NULL;
 
diff --git a/components/entropy.c b/components/entropy.c
index 65010b0..ae31437 100644
--- a/components/entropy.c
+++ b/components/entropy.c
_AT_@ -18,7 +18,7 @@
 
 		return bprintf("%ju", num);
 	}
-#elif defined(__OpenBSD__) | defined(__FreeBSD__)
+#elif defined(__OpenBSD__) | defined(__FreeBSD__) | defined(__NetBSD__)
 	const char *
 	entropy(const char *unused)
 	{
diff --git a/components/ip.c b/components/ip.c
index 9476549..bf98fd7 100644
--- a/components/ip.c
+++ b/components/ip.c
_AT_@ -9,6 +9,9 @@
 #elif defined(__FreeBSD__)
 	#include <netinet/in.h>
 	#include <sys/socket.h>
+#elif defined(__NetBSD__)
+	#include <netinet/in.h>
+	#include <sys/socket.h>
 #endif
 
 #include "../slstatus.h"
diff --git a/components/netspeeds.c b/components/netspeeds.c
index cde6fa9..a7a40fa 100644
--- a/components/netspeeds.c
+++ b/components/netspeeds.c
_AT_@ -52,7 +52,7 @@
 		return fmt_human((txbytes - oldtxbytes) * 1000 / interval,
 		                 1024);
 	}
-#elif defined(__OpenBSD__) | defined(__FreeBSD__)
+#elif defined(__OpenBSD__) | defined(__FreeBSD__) | defined(__NetBSD__)
 	#include <ifaddrs.h>
 	#include <net/if.h>
 	#include <string.h>
diff --git a/components/ram.c b/components/ram.c
index 15c4b74..6b3acc9 100644
--- a/components/ram.c
+++ b/components/ram.c
_AT_@ -209,4 +209,76 @@
 
 		return fmt_human(active * getpagesize(), 1024);
 	}
+
+#elif defined(__NetBSD__)
+	#define LOG1024 10
+	#define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024))
+	#include <uvm/uvm_extern.h>
+	#include <sys/sysctl.h>
+
+	static inline int
+	load_uvmexp(struct uvmexp_sysctl *uvmexp)
+	{
+		size_t size;
+
+		size = sizeof(*uvmexp);
+
+		if (sysctlbyname("vm.uvmexp2",uvmexp, &size, NULL, 0) > 0) {
+			warn("sysctlbyname 'vm.uvmexp2':");
+			return 1;
+		}
+
+		return 0;
+	}
+
+	const char *
+	ram_free(const char *unused)
+	{
+		struct uvmexp_sysctl uvmexp;
+		int free_pages;
+
+		if (load_uvmexp(&uvmexp) > 0)
+			return NULL;
+
+		free_pages = uvmexp.npages - uvmexp.active;
+		return fmt_human(pagetok(free_pages, uvmexp.pageshift) *
+				 1024, 1024);
+	}
+
+	const char *
+	ram_perc(const char *unused)
+	{
+		struct uvmexp_sysctl uvmexp;
+		int percent;
+
+		if (load_uvmexp(&uvmexp) > 0)
+			return NULL;
+
+		percent = uvmexp.active * 100 / uvmexp.npages;
+		return bprintf("%d", percent);
+	}
+
+	const char *
+	ram_total(const char *unused)
+	{
+		struct uvmexp_sysctl uvmexp;
+
+		if (load_uvmexp(&uvmexp) > 0)
+			return NULL;
+
+		return fmt_human(pagetok(uvmexp.npages,
+					 uvmexp.pageshift) * 1024, 1024);
+	}
+
+	const char *
+	ram_used(const char *unused)
+	{
+		struct uvmexp_sysctl uvmexp;
+
+		if (load_uvmexp(&uvmexp) > 0)
+			return NULL;
+
+		return fmt_human(pagetok(uvmexp.active,
+					 uvmexp.pageshift) * 1024, 1024);
+	}
 #endif
diff --git a/components/swap.c b/components/swap.c
index f270d93..9396c39 100644
--- a/components/swap.c
+++ b/components/swap.c
_AT_@ -99,7 +99,7 @@
 
 		return fmt_human((total - free - cached) * 1024, 1024);
 	}
-#elif defined(__OpenBSD__)
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
 	#include <stdlib.h>
 	#include <sys/swap.h>
 	#include <sys/types.h>
diff --git a/components/temperature.c b/components/temperature.c
index 7cf1394..4dd6988 100644
--- a/components/temperature.c
+++ b/components/temperature.c
_AT_@ -70,4 +70,71 @@
 		/* kelvin to decimal celcius */
 		return bprintf("%d.%d", (temp - 2731) / 10, abs((temp - 2731) % 10));
 	}
+#elif defined(__NetBSD__)
+  #include <fcntl.h>
+  #include <prop/proplib.h>
+  #include <sys/envsys.h>
+  #include <stdlib.h>
+  #include <string.h>
+  #include <unistd.h>
+  #define MKMC 273150000
+  #define SCALE 1000000
+
+	const char *
+	temp(const char *zone)
+	{
+    int fd;
+    int val = 0;
+    prop_dictionary_t dict = NULL;
+    prop_array_t arr;
+    prop_object_iterator_t iter1 = NULL;
+    prop_object_iterator_t iter2 = NULL;
+    prop_object_t obj1, obj2, obj3;
+
+    if ((fd = open("/dev/sysmon", O_RDONLY)) == -1)
+      return NULL;
+
+    if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict) == -1)
+      goto cleanup;
+
+    if (prop_dictionary_count(dict) == 0)
+      goto cleanup;
+
+    if ((iter1 = prop_dictionary_iterator(dict)) == NULL)
+      goto cleanup;
+
+    while ((obj1 = prop_object_iterator_next(iter1)) != NULL) {
+      arr = prop_dictionary_get_keysym(dict, obj1);
+      if (prop_object_type(arr) != PROP_TYPE_ARRAY)
+        goto cleanup;
+
+      if (!(iter2 = prop_array_iterator(arr)))
+        goto cleanup;
+
+      while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
+        obj3 = prop_dictionary_get(obj2, "description");
+        if (obj3 &&
+            strcmp(zone, prop_string_cstring_nocopy(obj3)) == 0) {
+          obj3 = prop_dictionary_get(obj2, "cur-value");
+          val = prop_number_integer_value(obj3);
+          goto cleanup;
+        }
+      }
+      prop_object_iterator_release(iter2);
+      iter2 = NULL;
+    }
+
+cleanup:
+    if (iter2 != NULL)
+      prop_object_iterator_release(iter2);
+    if (iter1 != NULL)
+      prop_object_iterator_release(iter1);
+    if (dict != NULL)
+      prop_object_release(dict);
+    close(fd);
+
+    if (val > 0)
+      return bprintf("%d.%d", (val - MKMC) / SCALE, abs((val - MKMC) % SCALE));
+    return NULL;
+	}
 #endif
diff --git a/components/volume.c b/components/volume.c
index 6cec556..ecd5630 100644
--- a/components/volume.c
+++ b/components/volume.c
_AT_@ -182,6 +182,30 @@
 
 		return bprintf("%d", value);
 	}
+#elif defined(__NetBSD__)
+  #include <fcntl.h>
+  #include <sys/ioctl.h>
+  #include <sys/audioio.h>
+  #include <unistd.h>
+
+	const char *
+	vol_perc(const char *unused)
+	{
+    int fd;
+    int gain;
+    audio_info_t info;
+
+    if ((fd = open("/dev/audio", O_RDONLY)) == -1)
+      return NULL;
+
+    if (ioctl(fd, AUDIO_GETINFO, &info) == -1) {
+      close(fd);
+      return NULL;
+    }
+    gain = info.play.gain;
+    close(fd);
+		return bprintf("%d", (100*gain) / AUDIO_MAX_GAIN);
+	}
 #else
 	#include <sys/soundcard.h>
 
diff --git a/components/wifi.c b/components/wifi.c
index 4543d32..cfa9583 100644
--- a/components/wifi.c
+++ b/components/wifi.c
_AT_@ -169,7 +169,11 @@
 
 		return NULL;
 	}
-#elif defined(__FreeBSD__)
+#elif defined(__FreeBSD__) || defined(__NetBSD__)
+#if defined (__NetBSD__)
+  /* XXX untested */
+  #define COMPAT_FREEBSD_NET80211
+#endif
 	#include <net/if.h>
 	#include <net80211/ieee80211_ioctl.h>
 
_AT_@ -224,8 +228,11 @@
 
 			len = sizeof(info);
 			if (load_ieee80211req(sockfd, interface, &info, IEEE80211_IOC_STA_INFO, &len)) {
-				rssi_dbm = info.sta.info[0].isi_noise +
- 					         info.sta.info[0].isi_rssi / 2;
+				rssi_dbm =
+#if defined (__FreeBSD__)
+          info.sta.info[0].isi_noise +
+#endif
+           info.sta.info[0].isi_rssi / 2;
 
 				fmt = bprintf("%d", RSSI_TO_PERC(rssi_dbm));
 			}
diff --git a/config.def.h b/config.def.h
index d805331..78156b1 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -55,11 +55,13 @@ static const char unknown_str[] = "n/a";
  *                                                     NULL on OpenBSD
  *                                                     thermal zone on FreeBSD
  *                                                     (tz0, tz1, etc.)
+ *                                                     sensor on NetBSD
+ *                                                     ("cpu0 temperature",etc.)
  * uid                 UID of current user             NULL
  * uptime              system uptime                   NULL
  * username            username of current user        NULL
  * vol_perc            OSS/ALSA volume in percent      mixer file (/dev/mixer)
- *                                                     NULL on OpenBSD/FreeBSD
+ *                                                     NULL on OpenBSD/FreeBSD/NetBSD
  * wifi_essid          WiFi ESSID                      interface name (wlan0)
  * wifi_perc           WiFi signal in percent          interface name (wlan0)
  */
diff --git a/config.mk b/config.mk
index 07af883..0798e43 100644
--- a/config.mk
+++ b/config.mk
_AT_@ -16,6 +16,7 @@ CFLAGS   = -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter -Os
 LDFLAGS  = -L$(X11LIB) -s
 # OpenBSD: add -lsndio
 # FreeBSD: add -lkvm -lsndio
+# NetBSD: add -lprop
 LDLIBS   = -lX11
 
 # compiler and linker
-- 
2.45.2
Received on Thu Jun 27 2024 - 13:44:14 CEST

This archive was generated by hypermail 2.3.0 : Thu Jun 27 2024 - 11:48:37 CEST