forked from IMbackK/charge-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backlight.c
55 lines (44 loc) · 1.28 KB
/
backlight.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "backlight.h"
#define _POSIX_SOURCE
#define _DEFAULT_SOURCE
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <linux/limits.h>
#include <fcntl.h>
#include "log.h"
int open_brightness_file(int *max_bright)
{
DIR *dir;
struct dirent *entry;
if ((dir = opendir(BACKLIGHT_SYSFS_PATH)) == NULL) {
ERROR("Can not open dir %s", BACKLIGHT_SYSFS_PATH);
return -1;
}
seekdir(dir, 2);
entry = readdir(dir);
if(entry == NULL) {
ERROR("No backlight available");
closedir(dir);
return -1;
}
char buf[PATH_MAX];
snprintf(buf, PATH_MAX, "%s%s%s", BACKLIGHT_SYSFS_PATH, entry->d_name, BACKLIGHT_MAX_BRIGHTNESS_FILE);
int max_bright_fd = open(buf, O_RDONLY);
if (max_bright_fd < 0) {
ERROR("No max_brightness available at %s", buf);
closedir(dir);
return -1;
}
if(read(max_bright_fd, buf, PATH_MAX) <= 0 || (*max_bright = atoi(buf)) == 0) {
ERROR("could not read max_brightness");
close(max_bright_fd);
return -1;
}
close(max_bright_fd);
snprintf(buf, PATH_MAX, "%s%s%s", BACKLIGHT_SYSFS_PATH, entry->d_name, BACKLIGHT_BRIGHTNESS_FILE);
closedir(dir);
return open(buf, O_WRONLY);
}