backlight.c

Fri, 11 Oct 2019 14:18:17 +0200

author
David Demelier <markand@malikania.fr>
date
Fri, 11 Oct 2019 14:18:17 +0200
changeset 17
37d1b7dd73ee
parent 16
0d923f0155dd
child 18
281cc3a22f1b
permissions
-rw-r--r--

backlight: update FreeBSD support

7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * main.c -- adjust laptop backlight using ACPI
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2010-2019 David Demelier <markand@malikania.fr>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and distribute this software for any
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <errno.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <stdarg.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <stdio.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 #include <stdlib.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include <string.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
24
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
25 /*
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
26 * Functions to implement for your system:
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
27 *
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
28 * All of these functions are allowed to exit on failures.
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
29 */
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
30
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
31 /* Set the level explicitly to the nearest value */
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
32 static void
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
33 set(unsigned int);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
34
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
35 /* Get the current value */
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
36 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
37 get(void);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
38
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 static void
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
40 increase(void)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
41 {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
42 unsigned int value = get();
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
43
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
44 if (value + 2 > 100)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
45 value = 100;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
46 else
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
47 value += 2;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
48
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
49 set(value);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
50 }
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
51
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
52 static void
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
53 decrease(void)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
54 {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
55 unsigned int value = get();
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
56
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
57 if ((int)value - 2 < 0)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
58 value = 0;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
59 else
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
60 value -= 2;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
61
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
62 set(value);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
63 }
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
64
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
65
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
66 static void
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 usage(const char *name)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 {
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
69 fprintf(stderr, "usage: %s decrease\n", name);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
70 fprintf(stderr, " %s get\n", name);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
71 fprintf(stderr, " %s increase\n", name);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
72 fprintf(stderr, " %s set percentage\n", name);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 exit(1);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
75
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 static void
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
77 die(const char *fmt, ...)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 va_list ap;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
80
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 va_start(ap, fmt);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 fprintf(stderr, "abort: ");
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 vfprintf(stderr, fmt, ap);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 va_end(ap);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
85
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 exit(1);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
87 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
88
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 /* {{{ Support for FreeBSD */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
90
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 #if defined(__FreeBSD__)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
92
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 #include <sys/types.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 #include <sys/sysctl.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
95
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
96 static int
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
97 upper_bound(int *levels, size_t length, unsigned int percent)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 {
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
99 size_t low = 0;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
100 size_t high = length - 1;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
101
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
102 while (low < high) {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
103 size_t mid = (low + high) / 2;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
104
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
105 if (percent <= (unsigned)levels[mid])
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
106 high = mid;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
107 else
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
108 low = mid + 1;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
109 }
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
110
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
111 return low;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
112 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
113
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
114 static int
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
115 min(const void *v1, const void *v2)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
116 {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
117 return *(int *)v1 - *(int *)v2;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
118 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
119
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
120 static void
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
121 sort(int *tab, size_t size)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
122 {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
123 qsort(tab, size, sizeof (int), min);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
124 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
125
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
126 static void
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
127 set(unsigned int percent)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
128 {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
129 int levels[200] = {0};
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
130 size_t length = sizeof (levels);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
131
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
132 if (sysctlbyname("hw.acpi.video.lcd0.levels", levels, &length, NULL, 0) == -1)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
133 die("sysctl: %s\n", strerror(errno));
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
134
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
135 /* Find appropriate nearest level */
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
136 if (percent > 0 && percent < 100) {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
137 sort(levels, length);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
138 percent = levels[upper_bound(levels, length, percent)];
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
139 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
140
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
141 printf("===> setting level to %u\n", percent);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
142 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
143
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
144 static unsigned int
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
145 get(void)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
146 {
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
147 int current = 0;
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
148 size_t length = sizeof (int);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
149
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
150 if (sysctlbyname("hw.acpi.video.lcd0.brightness", &current, &length, NULL, 0) == -1)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
151 die("sysctl: %s\n", strerror(errno));
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
152
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
153 return current;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
154 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
155
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
156 /* }}} */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
157
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
158 /* {{{ Support for Linux */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
159
9
bd47bd0c977a backlight: fix for FreeBSD and pet a warning
David Demelier <markand@malikania.fr>
parents: 7
diff changeset
160 #elif defined(__linux__)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
161
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
162 #include <sys/types.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
163 #include <sys/stat.h>
11
637ef49be6c9 backlight: use _XOPEN_SOURCE=700
David Demelier <markand@malikania.fr>
parents: 10
diff changeset
164 #include <dirent.h>
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
165 #include <fcntl.h>
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
166 #include <unistd.h>
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
167
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
168 #define SYS_PATH "/sys/class/backlight"
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
169
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
170 static unsigned int
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
171 read_int(int dfd, const char *file)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
172 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
173 char buf[32] = {0};
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
174 int fd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
175
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
176 if ((fd = openat(dfd, file, O_RDONLY)) < 0)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
177 die("open: %s\n", strerror(errno));
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
178 if (read(fd, &buf[0], sizeof (buf) - 1) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
179 die("read: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
180
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
181 close(fd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
182
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
183 return atoi(buf);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
184 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
185
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
186 static int
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
187 write_int(int dfd, const char *file, unsigned int value)
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
188 {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
189 char buf[32] = {0};
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
190 int length;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
191 int fd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
192
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
193 if ((fd = openat(dfd, file, O_WRONLY)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
194 die("open: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
195 if ((length = snprintf(buf, sizeof (buf), "%d\n", value)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
196 die("snprintf: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
197 if (write(fd, buf, length) != length)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
198 die("write: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
199
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
200 /* Re-read in case kernel change the value. */
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
201 return read_int(dfd, "brightness");
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
202 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
203
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
204 static int
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
205 try_adaptor(int parent_fd, const char *path)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
206 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
207 int dfd;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
208 struct stat st;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
209
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
210 if ((dfd = openat(parent_fd, path, O_RDONLY)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
211 return -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
212 if (fstatat(dfd, "brightness", &st, 0) < 0) {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
213 close(dfd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
214 return -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
215 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
216
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
217 return dfd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
218 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
219
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
220 static int
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
221 find_adaptor(void)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
222 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
223 int dfd, adaptorfd = -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
224 DIR *dirp;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
225 struct dirent *dirent;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
226
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
227 if ((dfd = open(SYS_PATH, O_RDONLY)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
228 die("open: %s: %s\n", SYS_PATH, strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
229 if (!(dirp = fdopendir(dfd)))
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
230 die("fdopendir: %s: %s\n", SYS_PATH, strerror(errno));
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
231
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
232 while ((dirent = readdir(dirp))) {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
233 if (strcmp(dirent->d_name, ".") == 0 ||
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
234 strcmp(dirent->d_name, "..") == 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
235 continue;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
236
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
237 if ((adaptorfd = try_adaptor(dfd, dirent->d_name)) >= 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
238 break;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
239 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
240
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
241 closedir(dirp);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
242 close(dfd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
243
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
244 if (adaptorfd < 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
245 die("could not find card adaptor\n");
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
246
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
247 return adaptorfd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
248 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
249
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
250 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
251 downscale(int dfd, unsigned int value)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
252 {
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
253 return value * 100 / read_int(dfd, "max_brightness");
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
254 }
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
255
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
256 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
257 upscale(int dfd, unsigned int value)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
258 {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
259 return value * read_int(dfd, "max_brightness") / 100;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
260 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
261
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
262 static void
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
263 set(unsigned int percent)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
264 {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
265 int dfd = find_adaptor();
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
266 unsigned int value = upscale(dfd, percent);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
267
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
268 write_int(dfd, "brightness", value);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
269 }
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
270
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
271 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
272 get(void)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
273 {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
274 int dfd = find_adaptor();
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
275 int value = read_int(dfd, "brightness");
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
276
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
277 return downscale(dfd, value);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
278 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
279
16
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
280 /* }}} */
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
281
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
282 /* {{{ Non supported shims */
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
283
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
284 #else
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
285
16
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
286 static void
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
287 notsupported(void)
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
288 {
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
289 die("backlight is not supported on this system\n");
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
290 }
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
291
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
292 static void
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
293 set(unsigned int v)
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
294 {
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
295 (void)v;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
296
16
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
297 notsupported();
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
298 }
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
299
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
300 static unsigned int
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
301 get(void)
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
302 {
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
303 notsupported();
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
304
0d923f0155dd doc: improve documentation
David Demelier <markand@malikania.fr>
parents: 15
diff changeset
305 return -1;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
306 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
307
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
308 #endif
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
309
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
310 /* }}} */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
311
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
312 int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
313 main(int argc, char *argv[])
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
314 {
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
315 -- argc;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
316 ++ argv;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
317
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
318 if (argc < 1)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
319 usage(argv[0]);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
320
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
321 if (strcmp(argv[0], "get") == 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
322 printf("%d\n", get());
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
323 else if (strcmp(argv[0], "set") == 0) {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
324 if (argc != 2)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
325 usage(argv[0]);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
326
17
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
327 unsigned int value = atoi(argv[1]);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
328
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
329 if (value > 100)
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
330 die("value %u is out of range\n", value);
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
331
37d1b7dd73ee backlight: update FreeBSD support
David Demelier <markand@malikania.fr>
parents: 16
diff changeset
332 set(value);
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
333 } else if (strcmp(argv[0], "increase") == 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
334 increase();
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
335 else if (strcmp(argv[0], "decrease") == 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
336 decrease();
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
337 }

mercurial