2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
33 #include <sys/capsicum.h>
34 #include <sys/backlight.h>
44 #include <capsicum_helpers.h>
46 #define BACKLIGHT_QUERY 0x0001
47 #define BACKLIGHT_SET_BRIGHTNESS 0x0002
48 #define BACKLIGHT_INCR 0x0004
49 #define BACKLIGHT_DECR 0x0008
50 #define BACKLIGHT_INFO 0x0010
52 static char device_name[PATH_MAX] = "/dev/backlight/backlight0";
55 set_device_name(const char *name)
59 strlcpy(device_name, name, sizeof(device_name));
61 snprintf(device_name, sizeof(device_name), "/dev/backlight/%s", name);
67 fprintf(stderr, "Usage:\n");
68 fprintf(stderr, "\tbacklight [-f dev]\n");
69 fprintf(stderr, "\tbacklight [-f dev] -i\n");
70 fprintf(stderr, "\tbacklight [-f dev] <percent>\n");
71 fprintf(stderr, "\tbacklight [-f dev] incr|+ <percent>\n");
72 fprintf(stderr, "\tbacklight [-f dev] decr|- <percent>\n");
77 backlight_type_to_string(enum backlight_info_type type)
80 case BACKLIGHT_TYPE_PANEL:
82 case BACKLIGHT_TYPE_KEYBOARD:
90 main(int argc, char *argv[])
92 struct backlight_props props;
93 struct backlight_info info;
96 cap_rights_t right_ioctl;
97 const unsigned long backlight_ioctls[] = {
99 BACKLIGHTUPDATESTATUS,
102 const char *percent_error;
107 action = BACKLIGHT_QUERY;
111 while ((ch = getopt(argc, argv, "f:qhi")) != -1) {
118 set_device_name(optarg);
121 action = BACKLIGHT_INFO;
132 if (strcmp("incr", argv[0]) == 0 ||
133 strcmp("+", argv[0]) == 0) {
134 action = BACKLIGHT_INCR;
138 else if (strcmp("decr", argv[0]) == 0 ||
139 strcmp("-", argv[0]) == 0) {
140 action = BACKLIGHT_DECR;
144 action = BACKLIGHT_SET_BRIGHTNESS;
147 /* ignore a trailing % for user friendlyness */
148 if (strlen(argv[0]) > 0 &&
149 argv[0][strlen(argv[0]) - 1] == '%')
150 argv[0][strlen(argv[0]) - 1] = '\0';
151 percent = strtonum(argv[0], 0, 100, &percent_error);
153 errx(1, "Cannot parse brightness level %s: %s",
159 if ((fd = open(device_name, O_RDWR)) == -1)
160 errx(1, "cannot open %s: %s",
161 device_name, strerror(errno));
163 if (caph_limit_stdio() < 0)
164 errx(1, "can't limit stdio rights");
165 caph_cache_catpages();
166 cap_rights_init(&right_ioctl, CAP_IOCTL);
167 if (caph_rights_limit(fd, &right_ioctl) < 0)
168 errx(1, "cap_right_limit() failed");
169 if (caph_ioctls_limit(fd, backlight_ioctls, nitems(backlight_ioctls)) < 0)
170 errx(1, "caph_ioctls_limit() failed");
171 if (caph_enter() < 0)
172 errx(1, "failed to enter capability mode");
175 case BACKLIGHT_QUERY:
176 if (ioctl(fd, BACKLIGHTGETSTATUS, &props) == -1)
177 errx(1, "Cannot query the backlight device");
179 printf("%u\n", props.brightness);
181 printf("brightness: %d\n", props.brightness);
182 if (props.nlevels != 0) {
184 for (i = 0; i < props.nlevels; i++)
185 printf(" %d", props.levels[i]);
190 case BACKLIGHT_SET_BRIGHTNESS:
191 props.brightness = percent;
192 if (ioctl(fd, BACKLIGHTUPDATESTATUS, &props) == -1)
193 errx(1, "Cannot update the backlight device");
197 if (ioctl(fd, BACKLIGHTGETSTATUS, &props) == -1)
198 errx(1, "Cannot query the backlight device");
199 percent = percent == 0 ? 10 : percent;
200 percent = action == BACKLIGHT_INCR ? percent : -percent;
201 props.brightness += percent;
202 if ((int)props.brightness < 0)
203 props.brightness = 0;
204 if (props.brightness > 100)
205 props.brightness = 100;
206 if (ioctl(fd, BACKLIGHTUPDATESTATUS, &props) == -1)
207 errx(1, "Cannot update the backlight device");
210 if (ioctl(fd, BACKLIGHTGETINFO, &info) == -1)
211 errx(1, "Cannot query the backlight device");
212 if (quiet == false) {
213 printf("Backlight name: %s\n", info.name);
214 printf("Backlight hardware type: %s\n", backlight_type_to_string(info.type));
216 printf("%s\n", info.name);
217 printf("%s\n", backlight_type_to_string(info.type));