spoon

set dwm status
git clone git://git.2f30.org/spoon
Log | Files | Refs | LICENSE

brightness.c (1111B)


      1 #include <err.h>
      2 #include <stdio.h>
      3 
      4 #ifdef __OpenBSD__
      5 #include <sys/ioctl.h>
      6 #include <fcntl.h>
      7 #include <time.h>
      8 #include <unistd.h>
      9 #include <dev/wscons/wsconsio.h>
     10 
     11 int
     12 brightnessread(void *arg, char *buf, size_t len)
     13 {
     14 	struct wsdisplay_param dpyp;
     15 	int fd;
     16 
     17 	if ((fd = open("/dev/ttyC0", O_RDONLY)) == -1) {
     18 		warn("couldn't open /dev/ttyC0");
     19 		return -1;
     20 	}
     21 	dpyp.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
     22 	if (ioctl(fd, WSDISPLAYIO_GETPARAM, &dpyp) == -1) {
     23 		warn("WSDISPLAYIO_PARAM_BRIGHTNESS ioctl");
     24 		return -1;
     25 	}
     26 	close(fd);
     27 	snprintf(buf, len, "%3d%%",
     28 	    100 * (dpyp.curval - dpyp.min) / (dpyp.max - dpyp.min));
     29 
     30 	return 0;
     31 }
     32 #elif __linux__
     33 #include <sys/wait.h>
     34 int
     35 brightnessread(void *arg, char *buf, size_t len)
     36 {
     37 	FILE *fp;
     38 	int brightness;
     39 
     40 	fp = popen("xbacklight", "r");
     41 	if (fp == NULL)
     42 		return -1;
     43 	if (fscanf(fp, "%d", &brightness) == EOF) {
     44 		if (ferror(fp)) {
     45 			pclose(fp);
     46 			return -1;
     47 		}
     48 	}
     49 	/* This system does not have a backlight so report it as 100% */
     50 	if (WEXITSTATUS(pclose(fp)) == 1)
     51 		brightness = 100;
     52 	snprintf(buf, len, "%3d%%", brightness);
     53 	return 0;
     54 }
     55 #endif