backlight: add OpenBSD support

Thu, 26 Mar 2020 12:35:53 +0100

author
David Demelier <markand@malikania.fr>
date
Thu, 26 Mar 2020 12:35:53 +0100
changeset 23
6081cc143d37
parent 22
1778aac34198
child 24
2da99b7156de

backlight: add OpenBSD support

INSTALL.md file | annotate | diff | comparison | revisions
Makefile file | annotate | diff | comparison | revisions
README.md file | annotate | diff | comparison | revisions
backlight.1 file | annotate | diff | comparison | revisions
backlight.c file | annotate | diff | comparison | revisions
--- a/INSTALL.md	Thu Mar 26 11:25:28 2020 +0100
+++ b/INSTALL.md	Thu Mar 26 12:35:53 2020 +0100
@@ -5,7 +5,8 @@
 -----------------
 
 - Linux: requires video ACPI module,
-- FreeBSD: requires the `acpi_video(4)` loaded.
+- FreeBSD: requires the `acpi_video(4)` loaded,
+- OpenBSD.
 
 Installation on Linux
 ---------------------
--- a/Makefile	Thu Mar 26 11:25:28 2020 +0100
+++ b/Makefile	Thu Mar 26 12:35:53 2020 +0100
@@ -18,7 +18,7 @@
 
 .POSIX:
 
-CC=             gcc
+CC=             cc
 CFLAGS=         -O3 -DNDEBUG
 
 PREFIX=         /usr/local
--- a/README.md	Thu Mar 26 11:25:28 2020 +0100
+++ b/README.md	Thu Mar 26 12:35:53 2020 +0100
@@ -18,5 +18,6 @@
 Example:
 
 	backlight increase
+	backlight increase 20
 	backlight set 90
 	backlight get
--- a/backlight.1	Thu Mar 26 11:25:28 2020 +0100
+++ b/backlight.1	Thu Mar 26 12:35:53 2020 +0100
@@ -24,10 +24,12 @@
 .Sh SYNOPSIS
 .Nm
 .Ar decrease
+.Op Ar amount
 .Nm
 .Ar get
 .Nm
 .Ar increase
+.Op Ar amount
 .Nm
 .Ar set
 .Ar percentage
@@ -41,11 +43,15 @@
 The following commands are available:
 .Bl -tag -width 10n
 .It Cm decrease
-Decrease to the next lower level.
+Decrease the specific
+.Ar amount
+percentage value (default: 10).
 .It Cm get
 Get the current level scaled to a percentage range.
 .It Cm increase
-Increase to the next upper level.
+Increase the specific
+.Ar amount
+percentage value (default: 10).
 .It Cm set
 Set brightness to the
 .Ar percentage
@@ -76,6 +82,17 @@
 .Sy acpi_video_load="YES"
 in
 .Pa /boot/loader.conf .
+.Ss OpenBSD
+On OpenBSD, the device
+.Pa /dev/ttyC0
+must be writable in your user but this is normally the case when you login.
+Otherwise you may simply run the program as root.
+.Pp
+Note: you may need to specify a bigger
+.Ar amount
+with the
+.Cm increase, decrease
+commands if your system use small percentages.
 .\" AUTHORS
 .Sh AUTHORS
 .Nm
--- a/backlight.c	Thu Mar 26 11:25:28 2020 +0100
+++ b/backlight.c	Thu Mar 26 12:35:53 2020 +0100
@@ -28,48 +28,65 @@
  * All of these functions are allowed to exit on failures.
  */
 
-/* Set the level explicitly to the nearest value */
+/* Set the level explicitly to the nearest value in range [0-100] */
 static void
 set(unsigned int);
 
-/* Get the current value */
+/* Get the current value in range [0-100] */
 static unsigned int
 get(void);
 
+static int
+gap(int argc, char **argv)
+{
+	int value = 10;
+
+	if (argc >= 2) {
+		errno = 0;
+		value = atoi(argv[1]);
+
+		if (errno || value > 100 || value < -100) {
+			fprintf(stderr, "invalid number: %s\n", argv[1]);
+			exit(1);
+		}
+	}
+
+	return value;
+}
+
 static void
-increase(void)
+increase(int gap)
 {
 	unsigned int value = get();
 
-	if (value + 2 > 100)
+	if (value + gap > 100)
 		value = 100;
 	else
-		value += 2;
+		value += gap;
 
 	set(value);
 }
 
 static void
-decrease(void)
+decrease(int gap)
 {
 	unsigned int value = get();
 
-	if ((int)value - 2 < 0)
+	if ((int)value - gap < 0)
 		value = 0;
 	else
-		value -= 2;
+		value -= gap;
 
 	set(value);
 }
 
-
 static void
 usage(const char *name)
 {
 	fprintf(stderr, "usage: %s decrease\n", name);
 	fprintf(stderr, "       %s get\n", name);
-	fprintf(stderr, "       %s increase\n", name);
-	fprintf(stderr, "       %s set percentage\n", name);
+	fprintf(stderr, "       %s increase [amount]\n", name);
+	fprintf(stderr, "       %s set percentage [amount]\n", name);
 	exit(1);
 }
 
@@ -284,6 +301,84 @@
 
 /* }}} */
 
+/* {{{ Support for OpenBSD */
+
+#elif defined(__OpenBSD__)
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <dev/wscons/wsconsio.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#define DEV "/dev/ttyC0"
+
+static int
+instance(int mode)
+{
+	int fd;
+
+	if ((fd = open(DEV, mode)) < 0)
+		err(1, "open");
+
+	return fd;
+}
+
+static void
+fetch(struct wsdisplay_param *param)
+{
+	int fd = instance(O_RDONLY);
+
+	param->param = WSDISPLAYIO_PARAM_BRIGHTNESS;
+
+	if (ioctl(fd, WSDISPLAYIO_GETPARAM, param) < 0)
+		err(1, "ioctl");
+
+	close(fd);
+}
+
+static void
+put(struct wsdisplay_param *param)
+{
+	int fd = instance(O_WRONLY);
+
+	param->param = WSDISPLAYIO_PARAM_BRIGHTNESS;
+
+	if (ioctl(fd, WSDISPLAYIO_SETPARAM, param) < 0)
+		err(1, "ioctl");
+
+	close(fd);
+}
+
+static void
+set(unsigned int v)
+{
+	struct wsdisplay_param param;
+
+	/* Get current max/min values to scale. */
+	fetch(&param);
+
+	/* Upscale and apply. */
+	param.curval = v * param.max / 100;
+	put(&param);
+}
+
+static unsigned int
+get(void)
+{
+	struct wsdisplay_param param;
+
+	fetch(&param);
+
+	return (param.curval - param.min) * 100 / (param.max - param.min);
+}
+
+/* }}} */
+
 /* {{{ Non supported shims */
 
 #else
@@ -336,9 +431,9 @@
 
 		set(value);
 	} else if (strcmp(argv[0], "increase") == 0)
-		increase();
+		increase(gap(argc, argv));
 	else if (strcmp(argv[0], "decrease") == 0)
-		decrease();
+		decrease(gap(argc, argv));
 	else
 		usage("backlight");
 }

mercurial