backlight.c

Fri, 20 Sep 2019 07:19:30 +0200

author
David Demelier <markand@malikania.fr>
date
Fri, 20 Sep 2019 07:19:30 +0200
changeset 10
2b12d8bee90d
parent 9
bd47bd0c977a
child 11
637ef49be6c9
permissions
-rw-r--r--

backlight: search for card in Linux

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
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
19 #define _POSIX_C_SOURCE 200809L
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
20
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <errno.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 #include <stdarg.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include <stdio.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 #include <stdlib.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include <string.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
26
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 enum action {
9
bd47bd0c977a backlight: fix for FreeBSD and pet a warning
David Demelier <markand@malikania.fr>
parents: 7
diff changeset
28 None,
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 Up,
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 Down
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 };
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
32
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 static void
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 usage(const char *name)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 fprintf(stderr, "usage: %s down\n", name);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 fprintf(stderr, " %s up\n", name);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 exit(1);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
40
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 static void
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
42 die(const char *fmt, ...)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
43 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 va_list ap;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
45
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 va_start(ap, fmt);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 fprintf(stderr, "abort: ");
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 vfprintf(stderr, fmt, ap);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 va_end(ap);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
50
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 exit(1);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
53
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 /* {{{ Support for FreeBSD */
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 #if defined(__FreeBSD__)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
57
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 #include <sys/types.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 #include <sys/sysctl.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
60
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
61 static int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 set(enum action type)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
64 int current, next = 0, i;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 int msg[100];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
66 size_t len, nextlen;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
67
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 /* First, get all values availables */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 len = sizeof (msg);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
70
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
71 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
72 die("sysctl: %s\n", strerror(errno));
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 /* Get the current value */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 len = sizeof (current);
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 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
78 die("sysctl: %s\n", strerror(errno));
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 /* First find the index of 0 */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 for (i = 0; msg[i] != 0; ++i)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 continue;
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 /* Find the current value index in msg */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
85 for (; msg[i] != current; ++i)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 continue;
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 if (type == Up) {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 if (msg[i] >= 100)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 return msg[i];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
91
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 next = msg[i + 1];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 } else {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 if (msg[i] == 0)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 return 0;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
96
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 next = msg[i - 1];
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 }
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 nextlen = sizeof (next);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
101
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 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
103 die("sysctl: %s\n", strerror(errno));
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
104
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
105 return next;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
106 }
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 /* }}} */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
109
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
110 /* {{{ Support for Linux */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
111
9
bd47bd0c977a backlight: fix for FreeBSD and pet a warning
David Demelier <markand@malikania.fr>
parents: 7
diff changeset
112 #elif defined(__linux__)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
113
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
114 #include <sys/types.h>
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
115 #include <sys/stat.h>
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
116 #include <fcntl.h>
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
117 #include <dirent.h>
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
118 #include <unistd.h>
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
119
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
120 #define SYS_PATH "/sys/class/backlight"
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
121
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
122 static int
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
123 read_int(int dfd, const char *file)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
124 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
125 char buf[32] = {0};
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
126 int fd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
127
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
128 if ((fd = openat(dfd, file, O_RDONLY)) < 0)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
129 die("open: %s\n", strerror(errno));
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
130 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
131 die("read: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
132
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
133 close(fd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
134
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
135 return atoi(buf);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
136 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
137
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
138 static int
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
139 write_int(int dfd, const char *file, int value)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
140 {
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 length;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
143 int fd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
144
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
145 if ((fd = openat(dfd, file, O_WRONLY)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
146 die("open: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
147 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
148 die("snprintf: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
149 if (write(fd, buf, length) != length)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
150 die("write: %s\n", strerror(errno));
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
151
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
152 /* Re-read in case kernel change the value. */
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
153 return read_int(dfd, "brightness");
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 static int
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
157 try_adaptor(int parent_fd, const char *path)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
158 {
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
159 int dfd;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
160 struct stat st;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
161
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
162 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
163 return -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
164 if (fstatat(dfd, "brightness", &st, 0) < 0) {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
165 close(dfd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
166 return -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
167 }
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
168
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
169 return dfd;
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
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
172 static int
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
173 find_adaptor(void)
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, adaptorfd = -1;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
176 DIR *dirp;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
177 struct dirent *dirent;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
178
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
179 if ((dfd = open(SYS_PATH, O_RDONLY)) < 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
180 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
181 if (!(dirp = fdopendir(dfd)))
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
182 die("fdopendir: %s: %s\n", SYS_PATH, strerror(errno));
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
183
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
184 while ((dirent = readdir(dirp))) {
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
185 if (strcmp(dirent->d_name, ".") == 0 ||
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
186 strcmp(dirent->d_name, "..") == 0)
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
187 continue;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
188
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
189 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
190 break;
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
191 }
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 closedir(dirp);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
194 close(dfd);
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
195
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
196 return adaptorfd;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
197 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
198
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
199 static int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
200 set(int type)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
201 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
202 int current, max;
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
203 int dfd;
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 ((dfd = find_adaptor()) < 0)
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
206 die("could not find card adaptor\n");
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
207
10
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
208 current = read_int(dfd, "actual_brightness");
2b12d8bee90d backlight: search for card in Linux
David Demelier <markand@malikania.fr>
parents: 9
diff changeset
209 max = read_int(dfd, "max_brightness");
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
210
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
211 if (type == Up) {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
212 current += max / 100;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
213
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
214 if (current > max)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
215 return max;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
216 } else {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
217 current -= max / 100;
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 if (current < 0)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
220 return 0;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
221 }
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 return write_int(dfd, "brightness", current);
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
224 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
225
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
226 #else
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
227
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
228 /* Not supported */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
229 static int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
230 set(int type)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
231 {
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
232 (void)type;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
233
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
234 die("backlight is not supported on this system");
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
235 }
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
236
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
237 #endif
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
238
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
239 /* }}} */
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
240
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
241 int
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
242 main(int argc, char *argv[])
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
243 {
9
bd47bd0c977a backlight: fix for FreeBSD and pet a warning
David Demelier <markand@malikania.fr>
parents: 7
diff changeset
244 int st, type = None;
7
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
245
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
246 if (argc < 2)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
247 usage(argv[0]);
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 if (strcmp(argv[1], "up") == 0)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
250 type = Up;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
251 else if (strcmp(argv[1], "down") == 0)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
252 type = Down;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
253 else
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
254 usage(argv[0]);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
255
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
256 if ((st = set(type)) < 0)
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
257 return 1;
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
258
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
259 printf("switching to %d\n", st);
45a06b02a61f misc: add missing files
David Demelier <markand@malikania.fr>
parents:
diff changeset
260 }

mercurial