summaryrefslogtreecommitdiff
path: root/src/backlight.c
blob: 5011cb86f19183e8b94d5ec1abe0be4b0852cc17 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/***************************************************************************

 Copyright 2014 Intel Corporation.  All Rights Reserved.
 Copyright 2014 Red Hat, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sub license, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:

 The above copyright notice and this permission notice (including the
 next paragraph) shall be included in all copies or substantial portions
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 IN NO EVENT SHALL INTEL, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 **************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>

#include "backlight.h"
#include "fd.h"

#define BACKLIGHT_CLASS "/sys/class/backlight"

/* Enough for 10 digits of backlight + '\n' + '\0' */
#define BACKLIGHT_VALUE_LEN 12

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
#endif

/*
 * Unfortunately this is not as simple as I would like it to be. If selinux is
 * dropping dbus messages pkexec may block *forever*.
 *
 * Backgrounding pkexec by doing System("pkexec ...&") does not work because
 * that detaches pkexec from its parent at which point its security checks
 * fail and it refuses to execute the helper.
 *
 * So we're left with spawning a helper child which gets levels to set written
 * to it through a pipe. This turns the blocking forever problem from a hung
 * machine problem into a simple backlight control not working problem.
 */

#ifdef __OpenBSD__

#include <dev/wscons/wsconsio.h>

int backlight_set(struct backlight *b, int level)
{
	struct wsdisplay_param param;

	if (b->iface == NULL)
		return;

	if ((unsigned)level > b->max)
		level = b->max;

	memset(&param, 0, sizeof(param));
	param.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
	param.curval = level;

	return ioctl(xf86Info.consoleFd, WSDISPLAYIO_SETPARAM, &param);
}

int backlight_get(struct backlight *b)
{
	struct wsdisplay_param param;

	if (b->iface == NULL)
		return -1;

	memset(&param, 0, sizeof(param));
	param.param = WSDISPLAYIO_PARAM_BRIGHTNESS;

	if (ioctl(xf86Info.consoleFd, WSDISPLAYIO_GETPARAM, &param))
		return -1;

	return param.curval;
}

int backlight_open(struct backlight *b, char *iface)
{
	struct wsdisplay_param param;

	memset(&param, 0, sizeof(param));
	param.param = WSDISPLAYIO_PARAM_BRIGHTNESS;

	if (ioctl(xf86Info.consoleFd, WSDISPLAYIO_GETPARAM, &param) == -1)
		return -1;

	b->iface = strdup("wscons");
	if (b->iface == NULL)
		return -1;

	b->max = param.max;
	b->fd = -1;
	b->type = BL_PLATFORM;

	return param.curval;
}

#else

static int
is_sysfs_fd(int fd)
{
	struct stat st;
	return fstat(fd, &st) == 0 && major(st.st_dev) == 0;
}

static int
__backlight_open(const char *iface, const char *file, int mode)
{
	char buf[1024];
	int fd;

	snprintf(buf, sizeof(buf), "%s/%s/%s", BACKLIGHT_CLASS, iface, file);
	fd = open(buf, mode);
	if (fd == -1)
		return -1;

	if (!is_sysfs_fd(fd)) {
		close(fd);
		return -1;
	}

	return fd;
}

static int
__backlight_read(const char *iface, const char *file)
{
	char buf[BACKLIGHT_VALUE_LEN];
	int fd, val;

	fd = __backlight_open(iface, file, O_RDONLY);
	if (fd < 0)
		return -1;

	val = read(fd, buf, BACKLIGHT_VALUE_LEN - 1);
	if (val > 0) {
		buf[val] = '\0';
		val = atoi(buf);
	} else
		val = -1;
	close(fd);

	return val;
}

/* List of available kernel interfaces in priority order */
static const char *known_interfaces[] = {
	"dell_backlight",
	"gmux_backlight",
	"asus-laptop",
	"asus-nb-wmi",
	"eeepc",
	"thinkpad_screen",
	"mbp_backlight",
	"fujitsu-laptop",
	"sony",
	"samsung",
	"acpi_video1",
	"acpi_video0",
	"intel_backlight",
};

static enum backlight_type __backlight_type(const char *iface)
{
	char buf[1024];
	int fd, v;

	v = -1;
	fd = __backlight_open(iface, "type", O_RDONLY);
	if (fd >= 0) {
		v = read(fd, buf, sizeof(buf)-1);
		close(fd);
	}
	if (v > 0) {
		while (v > 0 && isspace(buf[v-1]))
			v--;
		buf[v] = '\0';

		if (strcmp(buf, "raw") == 0)
			v = BL_RAW;
		else if (strcmp(buf, "platform") == 0)
			v = BL_PLATFORM;
		else if (strcmp(buf, "firmware") == 0)
			v = BL_FIRMWARE;
		else
			v = BL_NAMED;
	} else
		v = BL_NAMED;

	if (v == BL_NAMED) {
		int i;
		for (i = 0; i < ARRAY_SIZE(known_interfaces); i++) {
			if (strcmp(iface, known_interfaces[i]) == 0)
				break;
		}
		v += i;
	}

	return v;
}

enum backlight_type backlight_exists(const char *iface)
{
	if (__backlight_read(iface, "brightness") < 0)
		return BL_NONE;

	if (__backlight_read(iface, "max_brightness") <= 0)
		return BL_NONE;

	return __backlight_type(iface);
}

static int __backlight_init(struct backlight *b, char *iface, int fd)
{
	b->fd = fd_set_cloexec(fd_set_nonblock(fd));
	b->iface = iface;
	return 1;
}

static int __backlight_direct_init(struct backlight *b, char *iface)
{
	int fd;

	fd = __backlight_open(iface, "brightness", O_RDWR);
	if (fd < 0)
		return 0;

	return __backlight_init(b, iface, fd);
}

static int __backlight_helper_init(struct backlight *b, char *iface)
{
#if USE_BACKLIGHT_HELPER
	struct stat st;
	char *env[] = { NULL };
	int use_pkexec = 0;
	int fds[2];

	/* If system policy is to disallow setuid helpers,
	 * we fallback to invoking PolicyKit. However, as pkexec
	 * is quite troublesome and not universally available, we
	 * still try the old fashioned and simple method first.
	 * Either way, we have to trust that it is our backlight-helper
	 * that is run and that we have scrutinised it carefully.
	 */
	if (stat(LIBEXEC_PATH "/xf86-video-intel-backlight-helper", &st))
		return 0;

	if ((st.st_mode & (S_IFREG | S_ISUID | S_IXUSR)) != (S_IFREG | S_ISUID | S_IXUSR)) {
		if (system("pkexec --version"))
			return 0;

		use_pkexec = 1;
	}

	if (pipe(fds))
		return 0;

	switch ((b->pid = fork())) {
	case 0:
		close(fds[1]);
		dup2(fds[0], 0);
		close(fds[0]);
		if (use_pkexec) {
			execlp("pkexec", "pkexec",
			       LIBEXEC_PATH "/xf86-video-intel-backlight-helper",
			       iface, (char *)0);
		} else {
			execle(LIBEXEC_PATH "/xf86-video-intel-backlight-helper",
			       "xf86-video-intel-backlight-helper",
			       iface, (char *)0, env);
		}
		_exit(1);
		/* unreachable fallthrough */
	case -1:
		close(fds[1]);
		close(fds[0]);
		return 0;

	default:
		close(fds[0]);
		return __backlight_init(b, iface, fds[1]);
	}
#else
	return 0;
#endif
}

static char *
__backlight_find(void)
{
	char *best_iface = NULL;
	unsigned best_type = INT_MAX;
	DIR *dir;
	struct dirent *de;

	dir = opendir(BACKLIGHT_CLASS);
	if (dir == NULL)
		return NULL;

	while ((de = readdir(dir))) {
		int v;

		if (*de->d_name == '.')
			continue;

		/* Fallback to priority list of known iface for old kernels */
		v = backlight_exists(de->d_name);
		if (v < best_type) {
			char *copy = strdup(de->d_name);
			if (copy) {
				free(best_iface);
				best_iface = copy;
				best_type = v;
			}
		}
	}
	closedir(dir);

	return best_iface;
}

int backlight_open(struct backlight *b, char *iface)
{
	int level;

	if (iface == NULL)
		iface = __backlight_find();
	if (iface == NULL)
		return -1;

	b->type = __backlight_type(iface);

	b->max = __backlight_read(iface, "max_brightness");
	if (b->max <= 0)
		return -1;

	level = __backlight_read(iface, "brightness");
	if (level < 0)
		return -1;

	if (!__backlight_direct_init(b, iface) &&
	    !__backlight_helper_init(b, iface))
		return -1;

	return level;
}

int backlight_set(struct backlight *b, int level)
{
	char val[BACKLIGHT_VALUE_LEN];
	int len, ret = 0;

	if (b->iface == NULL)
		return 0;

	if ((unsigned)level > b->max)
		level = b->max;

	len = snprintf(val, BACKLIGHT_VALUE_LEN, "%d\n", level);
	if (write(b->fd, val, len) != len)
		ret = -1;

	return ret;
}

int backlight_get(struct backlight *b)
{
	int level;

	if (b->iface == NULL)
		return -1;

	level = __backlight_read(b->iface, "brightness");
	if (level > b->max)
		level = b->max;
	else if (level < 0)
		level = -1;
	return level;
}
#endif

void backlight_disable(struct backlight *b)
{
	if (b->iface == NULL)
		return;

	if (b->fd != -1)
		close(b->fd);

	free(b->iface);
	b->iface = NULL;
}

void backlight_close(struct backlight *b)
{
	backlight_disable(b);
	if (b->pid)
		waitpid(b->pid, NULL, 0);
}