slstatus-backlight-20240127-f68f492.diff (4098B)
1 From aadea01ca9cd99932500f4988d7c57f6bc5fa6c5 Mon Sep 17 00:00:00 2001 2 From: Son Phan Trung <phantrungson17@gmail.com> 3 Date: Sat, 27 Jan 2024 21:10:41 +0700 4 Subject: [PATCH] Add backlight module for slstatus. 5 6 FreeBSD support is added, with these things to keep in mind: 7 - Device names are numbered compared to Linux (e.g. "intel_backlight0" instead "intel_backlight). 8 - Max number is hardcoded to 100. 9 --- 10 Makefile | 1 + 11 components/backlight.c | 86 ++++++++++++++++++++++++++++++++++++++++++ 12 config.def.h | 3 ++ 13 slstatus.h | 3 ++ 14 4 files changed, 93 insertions(+) 15 create mode 100644 components/backlight.c 16 17 diff --git a/Makefile b/Makefile 18 index 7a18274..a7eacfa 100644 19 --- a/Makefile 20 +++ b/Makefile 21 @@ -6,6 +6,7 @@ include config.mk 22 23 REQ = util 24 COM =\ 25 + components/backlight\ 26 components/battery\ 27 components/cat\ 28 components/cpu\ 29 diff --git a/components/backlight.c b/components/backlight.c 30 new file mode 100644 31 index 0000000..46240f6 32 --- /dev/null 33 +++ b/components/backlight.c 34 @@ -0,0 +1,86 @@ 35 +/* See LICENSE file for copyright and license details. */ 36 + 37 +#include <stddef.h> 38 + 39 +#include "../util.h" 40 + 41 +#if defined(__linux__) 42 + #include <limits.h> 43 + 44 + #define BRIGHTNESS_MAX "/sys/class/backlight/%s/max_brightness" 45 + #define BRIGHTNESS_CUR "/sys/class/backlight/%s/brightness" 46 + 47 + const char * 48 + backlight_perc(const char *card) 49 + { 50 + char path[PATH_MAX]; 51 + int max, cur; 52 + 53 + if (esnprintf(path, sizeof (path), BRIGHTNESS_MAX, card) < 0 || 54 + pscanf(path, "%d", &max) != 1) { 55 + return NULL; 56 + } 57 + 58 + if (esnprintf(path, sizeof (path), BRIGHTNESS_CUR, card) < 0 || 59 + pscanf(path, "%d", &cur) != 1) { 60 + return NULL; 61 + } 62 + 63 + if (max == 0) { 64 + return NULL; 65 + } 66 + 67 + return bprintf("%d%%", cur * 100 / max); 68 + } 69 +#elif defined(__OpenBSD__) 70 + #include <fcntl.h> 71 + #include <sys/ioctl.h> 72 + #include <sys/time.h> 73 + #include <dev/wscons/wsconsio.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 + return NULL; 90 + } 91 + return bprintf("%d", wsd_param.curval * 100 / wsd_param.max); 92 + } 93 +#elif defined(__FreeBSD__) 94 + #include <fcntl.h> 95 + #include <stdio.h> 96 + #include <sys/ioctl.h> 97 + #include <sys/backlight.h> 98 + 99 + #define FBSD_BACKLIGHT_DEV "/dev/backlight/%s" 100 + 101 + const char * 102 + backlight_perc(const char *card) 103 + { 104 + char buf[256]; 105 + struct backlight_props props; 106 + int fd; 107 + 108 + snprintf(buf, sizeof(buf), FBSD_BACKLIGHT_DEV, card); 109 + if ((fd = open(buf, O_RDWR)) == -1) { 110 + warn("could not open %s", card); 111 + return NULL; 112 + } 113 + if (ioctl(fd, BACKLIGHTGETSTATUS, &props) == -1){ 114 + warn("Cannot query the backlight device"); 115 + return NULL; 116 + } 117 + 118 + return bprintf("%d", props.brightness); 119 + } 120 +#endif 121 diff --git a/config.def.h b/config.def.h 122 index d805331..d56051d 100644 123 --- a/config.def.h 124 +++ b/config.def.h 125 @@ -12,6 +12,9 @@ static const char unknown_str[] = "n/a"; 126 /* 127 * function description argument (example) 128 * 129 + * backlight_perc backlight percentage device name 130 + * (intel_backlight, numbered on FreeBSD) 131 + * NULL on OpenBSD 132 * battery_perc battery percentage battery name (BAT0) 133 * NULL on OpenBSD/FreeBSD 134 * battery_remaining battery remaining HH:MM battery name (BAT0) 135 diff --git a/slstatus.h b/slstatus.h 136 index 8ef5874..dc7e2d0 100644 137 --- a/slstatus.h 138 +++ b/slstatus.h 139 @@ -1,5 +1,8 @@ 140 /* See LICENSE file for copyright and license details. */ 141 142 +/* backlight */ 143 +const char *backlight_perc(const char *); 144 + 145 /* battery */ 146 const char *battery_perc(const char *); 147 const char *battery_remaining(const char *); 148 -- 149 2.42.0 150