aboutsummaryrefslogtreecommitdiff
path: root/linux-bin/backlight.c
blob: 497fd729451418c0e56dc5354389dcf40dca4c60 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>

#define BACKLIGHT_DIR "/sys/class/backlight"

void
die(const char *msg)
{
	puts(msg);
	exit(1);
}

void
err(const char *msg)
{
	perror(msg);
	exit(1);
}

void*
ecalloc(size_t nmemb, size_t size)
{
	void *ret = calloc(nmemb, size);
	if (!ret)
		err("ecalloc");
	return ret;
}

char*
appendStr(size_t len, const char *a, const char *b)
{
	char *buf = ecalloc(len, sizeof(char));
	int ret = snprintf(buf, len, "%s%s", a, b);
	if (ret == -1 || ret > len)
		err("snprintf");
	return buf;
}

/* returns errno */
int
maxBrightness(const char *prefix, long *max)
{
	int e;
	char *buf = appendStr(256, prefix, "/max_brightness");

	int fd = open(buf, O_RDONLY);
	if (fd == -1) {
		e = errno;
		free(buf);
		return e;
	}

	memset(buf, '\0', 256);

	int b = read(fd, buf, 256);
	if (b==-1) {
		e = errno;
		free(buf);
		close(fd);
		return e;
	}

	for (int i=0; i<256 ; i++) {
		if (buf[i] == '\n') {
			buf[i] = '\0';
			break;
		}
	}

	long n = strtol(buf, NULL, 10);
	if (n == LONG_MIN || n == LONG_MAX || n == 0)
		err("strtol");

	free(buf);
	buf = NULL;
	close(fd);
	*max = n;

	return 0;
}

int
setBacklight(const char *prefix, int percent)
{
	char *fn = appendStr(256, prefix, "/brightness");

	int fd = open(fn, O_WRONLY);
	if (fd == -1) {
		free(fn);
		return -1;
	}

	int n = dprintf(fd, "%d\n", percent);
	if (n == -1) {
		close(fd);
		free(fn);
		return -1;
	}
	
	close(fd);
	free(fn);
	fn = NULL;

	return 0;
}

int
main(int argc, char **argv)
{
	uid_t euid = geteuid();
	char **a = argv+1;

	if (euid != 0)
		die("Program must be run as root/setuid");

	if (!*a)
		die("Brightness percentage must be supplied");


	long percent = strtol(*a, NULL, 10);
	if (percent <= LONG_MIN || percent >= LONG_MAX)
		err("invalid percent");
	if (percent < 0 || percent > 100)
		die("percent must be between 0 and 100");

	DIR *d = opendir(BACKLIGHT_DIR);
	if (!d)
		err("opendir");

	errno = 0;
	for (;;) {
		struct dirent *dent = readdir(d);
		if(!dent&&errno!=0)
			err("readdir");

		if (!dent)
			break;

		if (strcmp("..", dent->d_name)==0 || strcmp(".", dent->d_name)==0)
			continue;

		char *prefix = appendStr(1024, BACKLIGHT_DIR"/", dent->d_name);

		printf("found backlight: %s\n", prefix);
	
		int ret;
		long max = 0;
		ret = maxBrightness(prefix, &max);
		if (ret != 0) {
			const char *msg = strerror(ret);
			printf("Error getting max brightness for: %s : %s\n",
					prefix, msg);
			free(prefix);
			prefix = NULL;
			continue;
		}

		float p = (double)max*((double)percent/100.0);

		ret = setBacklight(prefix, (int)p);
		if (ret != 0) {
			perror("setBacklight");
		}

		free(prefix);
		prefix = NULL;
	}
}