backlight.c

Fri, 11 Oct 2019 12:58:48 +0200

author
David Demelier <markand@malikania.fr>
date
Fri, 11 Oct 2019 12:58:48 +0200
changeset 15
282ec7c30c04
parent 11
637ef49be6c9
child 16
0d923f0155dd
permissions
-rw-r--r--

backlight: now supports more commands

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 /* Increase to the nearest upper level */
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 increase(void);
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 /* Decrease to the nearest lower level */
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
36 static void
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
37 decrease(void);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
38
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
39 /* Set the level explicitly to the nearest value */
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
40 static void
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
41 set(unsigned int);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
42
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
43 /* Get the current value */
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
44 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
45 get(void);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
46
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 static void
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 usage(const char *name)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 {
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
50 fprintf(stderr, "usage: %s decrease\n", name);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
51 fprintf(stderr, " %s get\n", name);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
52 fprintf(stderr, " %s increase\n", name);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
53 fprintf(stderr, " %s set percentage\n", name);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 exit(1);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
56
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 static void
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 die(const char *fmt, ...)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 va_list ap;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
61
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 va_start(ap, fmt);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 fprintf(stderr, "abort: ");
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
64 vfprintf(stderr, fmt, ap);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 va_end(ap);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
66
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 exit(1);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
69
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 /* {{{ Support for FreeBSD */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
71
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
72 #if defined(__FreeBSD__)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
73
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 #include <sys/types.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 #include <sys/sysctl.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
76
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
77 static int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 set(enum action type)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 int current, next = 0, i;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 int msg[100];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 size_t len, nextlen;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
83
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 /* First, get all values availables */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
85 len = sizeof (msg);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
86
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
87 if (sysctlbyname("hw.acpi.video.lcd0.levels", msg, &len, NULL, 0) == -1)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 die("sysctl: %s\n", strerror(errno));
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
89
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 /* Get the current value */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 len = sizeof (current);
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 if (sysctlbyname("hw.acpi.video.lcd0.brightness", &current, &len, NULL, 0) == -1)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 die("sysctl: %s\n", strerror(errno));
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 /* First find the index of 0 */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 for (i = 0; msg[i] != 0; ++i)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 continue;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
99
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 /* Find the current value index in msg */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 for (; msg[i] != current; ++i)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 continue;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
103
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
104 if (type == Up) {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
105 if (msg[i] >= 100)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
106 return msg[i];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
107
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
108 next = msg[i + 1];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 } else {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
110 if (msg[i] == 0)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
111 return 0;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
112
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
113 next = msg[i - 1];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
114 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
115
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
116 nextlen = sizeof (next);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
117
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
118 if (sysctlbyname("hw.acpi.video.lcd0.brightness", &current, &len, &next, nextlen) == -1)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
119 die("sysctl: %s\n", strerror(errno));
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
120
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
121 return next;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
122 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
123
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
124 /* }}} */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
125
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
126 /* {{{ Support for Linux */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
127
9
bd47bd0c977a backlight: fix for FreeBSD and pet a warning
David Demelier <markand@malikania.fr>
parents: 7
diff changeset
128 #elif defined(__linux__)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
129
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
130 #include <sys/types.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
131 #include <sys/stat.h>
11
637ef49be6c9 backlight: use _XOPEN_SOURCE=700
David Demelier <markand@malikania.fr>
parents: 10
diff changeset
132 #include <dirent.h>
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
133 #include <fcntl.h>
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
134 #include <unistd.h>
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
135
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
136 #define SYS_PATH "/sys/class/backlight"
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
137
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
138 static unsigned int
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
139 read_int(int dfd, const char *file)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
140 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
141 char buf[32] = {0};
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
142 int fd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
143
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
144 if ((fd = openat(dfd, file, O_RDONLY)) < 0)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
145 die("open: %s\n", strerror(errno));
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
146 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
147 die("read: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
148
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
149 close(fd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
150
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
151 return atoi(buf);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
152 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
153
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
154 static int
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
155 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
156 {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
157 char buf[32] = {0};
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
158 int length;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
159 int fd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
160
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
161 if ((fd = openat(dfd, file, O_WRONLY)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
162 die("open: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
163 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
164 die("snprintf: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
165 if (write(fd, buf, length) != length)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
166 die("write: %s\n", strerror(errno));
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 /* Re-read in case kernel change the value. */
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
169 return read_int(dfd, "brightness");
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
170 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
171
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
172 static int
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
173 try_adaptor(int parent_fd, const char *path)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
174 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
175 int dfd;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
176 struct stat st;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
177
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
178 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
179 return -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
180 if (fstatat(dfd, "brightness", &st, 0) < 0) {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
181 close(dfd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
182 return -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
183 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
184
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
185 return dfd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
186 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
187
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
188 static int
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
189 find_adaptor(void)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
190 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
191 int dfd, adaptorfd = -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
192 DIR *dirp;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
193 struct dirent *dirent;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
194
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
195 if ((dfd = open(SYS_PATH, O_RDONLY)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
196 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
197 if (!(dirp = fdopendir(dfd)))
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
198 die("fdopendir: %s: %s\n", SYS_PATH, strerror(errno));
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
199
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
200 while ((dirent = readdir(dirp))) {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
201 if (strcmp(dirent->d_name, ".") == 0 ||
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
202 strcmp(dirent->d_name, "..") == 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
203 continue;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
204
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
205 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
206 break;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
207 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
208
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
209 closedir(dirp);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
210 close(dfd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
211
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
212 if (adaptorfd < 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
213 die("could not find card adaptor\n");
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
214
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
215 return adaptorfd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
216 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
217
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
218 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
219 downscale(int dfd, unsigned int value)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
220 {
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
221 return value * 100 / read_int(dfd, "max_brightness");
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
222 }
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
223
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
224 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
225 upscale(int dfd, unsigned int value)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
226 {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
227 return value * read_int(dfd, "max_brightness") / 100;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
228 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
229
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
230 static void
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
231 set(unsigned int percent)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
232 {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
233 int dfd = find_adaptor();
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
234 unsigned int value = upscale(dfd, percent);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
235
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
236 write_int(dfd, "brightness", value);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
237 }
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
238
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
239 static void
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
240 increase(void)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
241 {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
242 unsigned int value = get();
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
243
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
244 if (value + 2 > 100)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
245 value = 100;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
246 else
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
247 value += 2;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
248
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
249 set(value);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
250 }
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
251
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
252 static void
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
253 decrease(void)
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 unsigned int value = get();
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
256
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
257 if ((int)value - 2 < 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
258 value = 0;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
259 else
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
260 value -= 2;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
261
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
262 set(value);
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
263 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
264
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
265 static unsigned int
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
266 get(void)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
267 {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
268 int dfd = find_adaptor();
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
269 int value = read_int(dfd, "brightness");
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
270
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
271 return downscale(dfd, value);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
272 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
273
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
274 #else
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
275
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
276 /* Not supported */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
277 static int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
278 set(int type)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
279 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
280 (void)type;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
281
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
282 die("backlight is not supported on this system");
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
283 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
284
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
285 #endif
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
286
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
287 /* }}} */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
288
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
289 int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
290 main(int argc, char *argv[])
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
291 {
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
292 -- argc;
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
293 ++ argv;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
294
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
295 if (argc < 1)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
296 usage(argv[0]);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
297
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
298 if (strcmp(argv[0], "get") == 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
299 printf("%d\n", get());
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
300 else if (strcmp(argv[0], "set") == 0) {
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
301 if (argc != 2)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
302 usage(argv[0]);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
303
15
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
304 set(atoi(argv[1]));
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
305 } else if (strcmp(argv[0], "increase") == 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
306 increase();
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
307 else if (strcmp(argv[0], "decrease") == 0)
282ec7c30c04 backlight: now supports more commands
David Demelier <markand@malikania.fr>
parents: 11
diff changeset
308 decrease();
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
309 }

mercurial