-
Notifications
You must be signed in to change notification settings - Fork 1
/
ledturner.c
144 lines (107 loc) · 2.92 KB
/
ledturner.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sys/types.h>
#include <termios.h>
#include <signal.h>
void handler (int sig)
{
printf ("Exiting...(%d)n", sig);
exit (0);
}
void perror_exit (char *error)
{
perror(error);
handler (9);
}
void usage()
{
printf("LEDTURNER\n");
printf("\tTurns on|off keyboards LEDs writing directly to device.\n\n");
printf("USAGE:\n");
printf("\t./ledturner --input /dev/input/event* [OPTIONS]\n\n");
printf("OPTIONS:\n");
printf("\t--num, -n [0|1] Turn OFF (0) , ON (1) Num Lock LED\n");
printf("\t--caps, -c [0|1] Turn OFF (0) , ON (1) Caps Lock LED\n");
printf("\t--scroll, -s [0|1] Turn OFF (0) , ON (1) Scroll Lock LED\n");
printf("\n");
}
void processValue(int candidateValue, int *holder)
{
if (candidateValue == 1 || candidateValue == 0) {
*holder = candidateValue;
}
}
void makeEvent(struct input_event* event, int led, int action)
{
event->type = EV_LED;
if (action == 1) {
// Turn LED on
event->value = MSC_PULSELED;
} else {
// Turn LED off
event->value = !MSC_PULSELED;
}
event->code = led;
}
int main (int argc, char *argv[])
{
char *device = NULL;
int fd;
int c;
struct input_event event;
int num = -1;
int caps = -1;
int scroll = -1;
int tempValue;
const struct option long_opts[] = {
{ "input", required_argument, NULL, 'i' },
{ "num", required_argument, NULL, 'n' },
{ "caps" , required_argument, NULL, 'c' },
{ "scroll" , required_argument, NULL, 's' },
{ NULL, 0, NULL, 0 }
};
while ( (c = getopt_long(argc, argv, "i:n:c:s:", long_opts, NULL)) != EOF) {
switch (c) {
case 'i':
device = optarg;
break;
case 'n':
processValue(atoi(optarg),&num);
break;
case 'c':
processValue(atoi(optarg),&caps);
break;
case 's':
processValue(atoi(optarg),&scroll);
break;
case '?':
usage();
return 0;
}
}
if (device == NULL) {
perror_exit("Not a valid input event device found");
}
if ((fd = open (device, O_RDWR | O_NONBLOCK)) == -1) {
perror_exit("Unable to open input event device");
}
if (num != -1) {
makeEvent(&event, LED_NUML, num);
write(fd, &event, sizeof(struct input_event));
}
if (caps != -1) {
makeEvent(&event, LED_CAPSL, caps);
write(fd, &event, sizeof(struct input_event));
}
if (scroll != -1) {
makeEvent(&event, LED_SCROLLL, scroll);
write(fd, &event, sizeof(struct input_event));
}
return 0;
}