sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

slstatus-backlight-20260703-86133eb.diff (4079B)


      1 From 86133eb0523849ab11b2802abd5715ee2517668b Mon Sep 17 00:00:00 2001
      2 From: c2ny <c2ny@proton.me>
      3 Date: Fri, 3 Jul 2026 00:14:09 -0400
      4 Subject: [PATCH] Add backlight component
      5 
      6 This is an update to the patch that adds BSD support.
      7 This fixes the fd leak on OpenBSD and FreeBSD.
      8 ---
      9  Makefile               |  1 +
     10  components/backlight.c | 93 ++++++++++++++++++++++++++++++++++++++++++
     11  config.def.h           |  3 ++
     12  slstatus.h             |  3 ++
     13  4 files changed, 100 insertions(+)
     14  create mode 100644 components/backlight.c
     15 
     16 diff --git a/Makefile b/Makefile
     17 index 7a18274..a7eacfa 100644
     18 --- a/Makefile
     19 +++ b/Makefile
     20 @@ -6,6 +6,7 @@ include config.mk
     21  
     22  REQ = util
     23  COM =\
     24 +	components/backlight\
     25  	components/battery\
     26  	components/cat\
     27  	components/cpu\
     28 diff --git a/components/backlight.c b/components/backlight.c
     29 new file mode 100644
     30 index 0000000..4c8af53
     31 --- /dev/null
     32 +++ b/components/backlight.c
     33 @@ -0,0 +1,93 @@
     34 +/* See LICENSE file for copyright and license details. */
     35 +
     36 +#include <stddef.h>
     37 +
     38 +#include "../util.h"
     39 +
     40 +#if defined(__linux__)
     41 +	#include <limits.h>
     42 +
     43 +	#define BRIGHTNESS_MAX "/sys/class/backlight/%s/max_brightness"
     44 +	#define BRIGHTNESS_CUR "/sys/class/backlight/%s/brightness"
     45 +
     46 +	const char *
     47 +	backlight_perc(const char *card)
     48 +	{
     49 +		char path[PATH_MAX];
     50 +		int max, cur;
     51 +
     52 +		if (esnprintf(path, sizeof (path), BRIGHTNESS_MAX, card) < 0 ||
     53 +			pscanf(path, "%d", &max) != 1) {
     54 +			return NULL;
     55 +		}
     56 +
     57 +		if (esnprintf(path, sizeof (path), BRIGHTNESS_CUR, card) < 0 ||
     58 +			pscanf(path, "%d", &cur) != 1) {
     59 +			return NULL;
     60 +		}
     61 +
     62 +		if (max == 0) {
     63 +			return NULL;
     64 +		}
     65 +
     66 +		return bprintf("%d%%", cur * 100 / max);
     67 +	}
     68 +#elif defined(__OpenBSD__)
     69 +	#include <fcntl.h>
     70 +	#include <sys/ioctl.h>
     71 +	#include <sys/time.h>
     72 +	#include <dev/wscons/wsconsio.h>
     73 +	#include <unistd.h>
     74 +
     75 +	const char *
     76 +	backlight_perc(const char *unused)
     77 +	{
     78 +		int fd, err;
     79 +		struct wsdisplay_param wsd_param = {
     80 +			.param = WSDISPLAYIO_PARAM_BRIGHTNESS
     81 +		};
     82 +
     83 +		if ((fd = open("/dev/ttyC0", O_RDONLY)) < 0) {
     84 +			warn("could not open /dev/ttyC0");
     85 +			return NULL;
     86 +		}
     87 +		if ((err = ioctl(fd, WSDISPLAYIO_GETPARAM, &wsd_param)) < 0) {
     88 +			warn("ioctl 'WSDISPLAYIO_GETPARAM' failed");
     89 +			close(fd);
     90 +			return NULL;
     91 +		}
     92 +
     93 +		close(fd);
     94 +		return bprintf("%d", wsd_param.curval * 100 / wsd_param.max);
     95 +	}
     96 +#elif defined(__FreeBSD__)
     97 +	#include <fcntl.h>
     98 +	#include <stdio.h>
     99 +	#include <sys/ioctl.h>
    100 +	#include <sys/backlight.h>
    101 +	#include <unistd.h>
    102 +
    103 +	#define FBSD_BACKLIGHT_DEV "/dev/backlight/%s"
    104 +
    105 +	const char *
    106 +	backlight_perc(const char *card)
    107 +	{
    108 +		char buf[256];
    109 +		struct backlight_props props;
    110 +		int fd;
    111 +		
    112 +		snprintf(buf, sizeof(buf), FBSD_BACKLIGHT_DEV, card);
    113 +		if ((fd = open(buf, O_RDWR)) == -1) {
    114 +			warn("could not open %s", card);
    115 +			return NULL;
    116 +		}
    117 +		if (ioctl(fd, BACKLIGHTGETSTATUS, &props) == -1){
    118 +			warn("Cannot query the backlight device");
    119 +			close(fd);
    120 +			return NULL;
    121 +		}
    122 +
    123 +		close(fd);
    124 +		return bprintf("%d", props.brightness);
    125 +	}
    126 +#endif
    127 diff --git a/config.def.h b/config.def.h
    128 index 100093e..69e1624 100644
    129 --- a/config.def.h
    130 +++ b/config.def.h
    131 @@ -12,6 +12,9 @@ static const char unknown_str[] = "n/a";
    132  /*
    133   * function            description                     argument (example)
    134   *
    135 + * backlight_perc      backlight percentage            device name
    136 + *                                                     (intel_backlight, numbered on FreeBSD)
    137 + *                                                     NULL on OpenBSD
    138   * battery_perc        battery percentage              battery name (BAT0)
    139   *                                                     NULL on OpenBSD/FreeBSD
    140   * battery_remaining   battery remaining HH:MM         battery name (BAT0)
    141 diff --git a/slstatus.h b/slstatus.h
    142 index 394281c..29d3b01 100644
    143 --- a/slstatus.h
    144 +++ b/slstatus.h
    145 @@ -1,5 +1,8 @@
    146  /* See LICENSE file for copyright and license details. */
    147  
    148 +/* backlight */
    149 +const char *backlight_perc(const char *);
    150 +
    151  /* battery */
    152  const char *battery_perc(const char *);
    153  const char *battery_remaining(const char *);
    154 -- 
    155 2.53.0
    156