backlight: update FreeBSD support

Fri, 11 Oct 2019 14:18:17 +0200

author
David Demelier <markand@malikania.fr>
date
Fri, 11 Oct 2019 14:18:17 +0200
changeset 17
37d1b7dd73ee
parent 16
0d923f0155dd
child 18
281cc3a22f1b

backlight: update FreeBSD support

backlight.c file | annotate | diff | comparison | revisions
--- a/backlight.c	Fri Oct 11 13:12:26 2019 +0200
+++ b/backlight.c	Fri Oct 11 14:18:17 2019 +0200
@@ -28,14 +28,6 @@
  * All of these functions are allowed to exit on failures.
  */
 
-/* Increase to the nearest upper level */
-static void
-increase(void);
-
-/* Decrease to the nearest lower level */
-static void
-decrease(void);
-
 /* Set the level explicitly to the nearest value */
 static void
 set(unsigned int);
@@ -45,6 +37,33 @@
 get(void);
 
 static void
+increase(void)
+{
+	unsigned int value = get();
+
+	if (value + 2 > 100)
+		value = 100;
+	else
+		value += 2;
+
+	set(value);
+}
+
+static void
+decrease(void)
+{
+	unsigned int value = get();
+
+	if ((int)value - 2 < 0)
+		value = 0;
+	else
+		value -= 2;
+
+	set(value);
+}
+
+
+static void
 usage(const char *name)
 {
 	fprintf(stderr, "usage: %s decrease\n", name);
@@ -75,50 +94,63 @@
 #include <sys/sysctl.h>
 
 static int
-set(enum action type)
+upper_bound(int *levels, size_t length, unsigned int percent)
 {
-	int current, next = 0, i;
-	int msg[100];
-	size_t len, nextlen;
+	size_t low = 0;
+	size_t high = length - 1;
+
+	while (low < high) {
+		size_t mid = (low + high) / 2;
+
+		if (percent <= (unsigned)levels[mid])
+		    high = mid;
+		else
+		    low = mid + 1;
+	}
+
+	return low;
+}
 
-	/* First, get all values availables */
-	len = sizeof (msg);
+static int
+min(const void *v1, const void *v2)
+{
+	return *(int *)v1 - *(int *)v2;
+}
 
-	if (sysctlbyname("hw.acpi.video.lcd0.levels", msg, &len, NULL, 0) == -1)
-		die("sysctl: %s\n", strerror(errno));
+static void
+sort(int *tab, size_t size)
+{
+	qsort(tab, size, sizeof (int), min);
+}
 
-	/* Get the current value */
-	len = sizeof (current);
+static void
+set(unsigned int percent)
+{
+	int levels[200] = {0};
+	size_t length = sizeof (levels);
 
-	if (sysctlbyname("hw.acpi.video.lcd0.brightness", &current, &len, NULL, 0) == -1)
+	if (sysctlbyname("hw.acpi.video.lcd0.levels", levels, &length, NULL, 0) == -1)
 		die("sysctl: %s\n", strerror(errno));
 
-	/* First find the index of 0 */
-	for (i = 0; msg[i] != 0; ++i)
-		continue;
-
-	/* Find the current value index in msg */
-	for (; msg[i] != current; ++i)
-		continue;
-
-	if (type == Up) {
-		if (msg[i] >= 100)
-			return msg[i];
-
-		next = msg[i + 1];
-	} else {
-		if (msg[i] == 0)
-			return 0;
-
-		next = msg[i - 1];
+	/* Find appropriate nearest level */
+	if (percent > 0 && percent < 100) {
+		sort(levels, length);
+		percent = levels[upper_bound(levels, length, percent)];
 	}
 
-	nextlen = sizeof (next);
+	printf("===> setting level to %u\n", percent);
+}
 
-	if (sysctlbyname("hw.acpi.video.lcd0.brightness", &current, &len, &next, nextlen) == -1)
+static unsigned int
+get(void)
+{
+	int current = 0;
+	size_t length = sizeof (int);
+
+	if (sysctlbyname("hw.acpi.video.lcd0.brightness", &current, &length, NULL, 0) == -1)
 		die("sysctl: %s\n", strerror(errno));
 
-	return next;
+	return current;
 }
 
 /* }}} */
@@ -236,32 +268,6 @@
 	write_int(dfd, "brightness", value);
 }
 
-static void
-increase(void)
-{
-	unsigned int value = get();
-
-	if (value + 2 > 100)
-		value = 100;
-	else
-		value += 2;
-
-	set(value);
-}
-
-static void
-decrease(void)
-{
-	unsigned int value = get();
-
-	if ((int)value - 2 < 0)
-		value = 0;
-	else
-		value -= 2;
-
-	set(value);
-}
-
 static unsigned int
 get(void)
 {
@@ -284,18 +290,6 @@
 }
 
 static void
-increase(void)
-{
-	notsupported();
-}
-
-static void
-decrease(void)
-{
-	notsupported();
-}
-
-static void
 set(unsigned int v)
 {
 	(void)v;
@@ -330,7 +324,12 @@
 		if (argc != 2)
 			usage(argv[0]);
 
-		set(atoi(argv[1]));
+		unsigned int value = atoi(argv[1]);
+
+		if (value > 100)
+			die("value %u is out of range\n", value);
+
+		set(value);
 	} else if (strcmp(argv[0], "increase") == 0)
 		increase();
 	else if (strcmp(argv[0], "decrease") == 0)

mercurial