Xinqi Bao's Git

Add Michael Buch to LICENSE
[slstatus.git] / components / backlight.c
1 /* See LICENSE file for copyright and license details. */
2 #include "../util.h"
3
4 #if defined(__linux__)
5 #include <limits.h>
6
7 #define BRIGHTNESS_MAX "/sys/class/backlight/%s/max_brightness"
8 #define BRIGHTNESS_CUR "/sys/class/backlight/%s/brightness"
9
10 const char *
11 backlight_perc(const char *card)
12 {
13 char path[PATH_MAX];
14 int max, cur;
15
16 if (esnprintf(path, sizeof (path), BRIGHTNESS_MAX, card) < 0 ||
17 pscanf(path, "%d", &max) != 1) {
18 return NULL;
19 }
20
21 if (esnprintf(path, sizeof (path), BRIGHTNESS_CUR, card) < 0 ||
22 pscanf(path, "%d", &cur) != 1) {
23 return NULL;
24 }
25
26 if (max == 0) {
27 return NULL;
28 }
29
30 return bprintf("%d", cur * 100 / max);
31 }
32 #endif