-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkbd_backlight.c
103 lines (90 loc) · 2.16 KB
/
kbd_backlight.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2016 Jeff Spaulding <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
void
print_help()
{
printf("Usage:\n");
printf("kbd_backlight [-e | -d | -l<level>]\n");
printf(" where <level> is a number between 0 and 100.\n");
}
int
main(int argc, char *argv[])
{
int ch;
int eflg, dflg, lflg;
int level = -1;
char level_str[4] = "";
int fd;
const char *kbd_file = "/sys/class/leds/kbd_backlight/brightness";
eflg = dflg = lflg = 0;
while ((ch = getopt(argc, argv, "edhl:")) != -1) {
switch (ch) {
case 'e':
eflg = 1;
if (dflg || lflg) {
print_help();
return EXIT_FAILURE;
}
level = 100;
break;
case 'd':
dflg = 1;
if (eflg || lflg) {
print_help();
return EXIT_FAILURE;
}
level = 0;
break;
case 'l':
lflg = 1;
if (dflg || eflg) {
print_help();
return EXIT_FAILURE;
}
level = atoi(optarg);
if (level < 0 || level > 100) {
print_help();
return EXIT_FAILURE;
}
break;
case '?':
case 'h':
default:
print_help();
return EXIT_FAILURE;
}
}
if (level == -1) {
print_help();
return EXIT_FAILURE;
} else {
snprintf(level_str, sizeof level_str, "%d", level);
}
fd = open(kbd_file, O_TRUNC | O_WRONLY);
if (fd == -1) {
err(1, "%s", kbd_file);
}
write(fd, level_str, strlen(level_str));
close(fd);
}