slstatus-backlight-4bd78c9.diff (2788B)
1 diff --git a/Makefile b/Makefile 2 index 3be46cc..93dc2c5 100644 3 --- a/Makefile 4 +++ b/Makefile 5 @@ -6,6 +6,7 @@ include config.mk 6 7 REQ = util 8 COM =\ 9 + components/backlight\ 10 components/battery\ 11 components/cpu\ 12 components/datetime\ 13 diff --git a/components/backlight.c b/components/backlight.c 14 new file mode 100644 15 index 0000000..74f4c08 16 --- /dev/null 17 +++ b/components/backlight.c 18 @@ -0,0 +1,59 @@ 19 +/* See LICENSE file for copyright and license details. */ 20 + 21 +#include <stddef.h> 22 + 23 +#include "../util.h" 24 + 25 +#if defined(__linux__) 26 + #include <limits.h> 27 + 28 + #define BRIGHTNESS_MAX "/sys/class/backlight/%s/max_brightness" 29 + #define BRIGHTNESS_CUR "/sys/class/backlight/%s/brightness" 30 + 31 + const char * 32 + backlight_perc(const char *card) 33 + { 34 + char path[PATH_MAX]; 35 + int max, cur; 36 + 37 + if (esnprintf(path, sizeof (path), BRIGHTNESS_MAX, card) < 0 || 38 + pscanf(path, "%d", &max) != 1) { 39 + return NULL; 40 + } 41 + 42 + if (esnprintf(path, sizeof (path), BRIGHTNESS_CUR, card) < 0 || 43 + pscanf(path, "%d", &cur) != 1) { 44 + return NULL; 45 + } 46 + 47 + if (max == 0) { 48 + return NULL; 49 + } 50 + 51 + return bprintf("%d%%", cur * 100 / max); 52 + } 53 +#elif defined(__OpenBSD__) 54 + #include <fcntl.h> 55 + #include <sys/ioctl.h> 56 + #include <sys/time.h> 57 + #include <dev/wscons/wsconsio.h> 58 + 59 + const char * 60 + backlight_perc(const char *unused) 61 + { 62 + int fd, err; 63 + struct wsdisplay_param wsd_param = { 64 + .param = WSDISPLAYIO_PARAM_BRIGHTNESS 65 + }; 66 + 67 + if ((fd = open("/dev/ttyC0", O_RDONLY)) < 0) { 68 + warn("could not open /dev/ttyC0"); 69 + return NULL; 70 + } 71 + if ((err = ioctl(fd, WSDISPLAYIO_GETPARAM, &wsd_param)) < 0) { 72 + warn("ioctl 'WSDISPLAYIO_GETPARAM' failed"); 73 + return NULL; 74 + } 75 + return bprintf("%d", wsd_param.curval * 100 / wsd_param.max); 76 + } 77 +#endif 78 diff --git a/config.def.h b/config.def.h 79 index 5f6c114..69c5d50 100644 80 --- a/config.def.h 81 +++ b/config.def.h 82 @@ -12,6 +12,9 @@ static const char unknown_str[] = "n/a"; 83 /* 84 * function description argument (example) 85 * 86 + * backlight_perc backlight percentage device name 87 + * (intel_backlight) 88 + * NULL on OpenBSD 89 * battery_perc battery percentage battery name (BAT0) 90 * NULL on OpenBSD/FreeBSD 91 * battery_state battery charging state battery name (BAT0) 92 diff --git a/slstatus.h b/slstatus.h 93 index f3b4979..e1759a0 100644 94 --- a/slstatus.h 95 +++ b/slstatus.h 96 @@ -1,5 +1,8 @@ 97 /* See LICENSE file for copyright and license details. */ 98 99 +/* backlight */ 100 +const char *backlight_perc(const char *); 101 + 102 /* battery */ 103 const char *battery_perc(const char *); 104 const char *battery_state(const char *);