-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyscan.c
135 lines (102 loc) · 2.84 KB
/
keyscan.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
/*
* read volume keys and translate them to alsa commands (from the shell)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strerror() */
#include <errno.h> /* errno */
#include <fcntl.h> /* open() */
#include <unistd.h> /* close() */
#include <sys/ioctl.h> /* ioctl() */
#include <linux/input.h> /* EVIOCGVERSION ++ */
#include <unistd.h>
#include <sys/types.h>
#include "trim.h"
#include "readIni.h"
#define EV_BUF_SIZE 16
#define NOT_FOUND -1
int main(int argc, char *argv[])
{
int fd, sz;
unsigned i;
/* A few examples of information to gather */
unsigned version;
unsigned short id[4]; /* or use struct input_id */
char name[256] = "N/A";
struct input_event ev[EV_BUF_SIZE]; /* Read up to N events ata time */
if (argc < 2) {
fprintf(stderr,
"Usage: %s [configfile]\n",
argv[0]
);
return EINVAL;
}
readIni(argv[1],&initItems);
fprintf(stderr, "Settings: \n");
for (i=0;i<CFG_MAX;i++) {
fprintf(stderr,"%s: %s\n",initItems[i].name,initItems[i].value);
}
if ((fd = open(initItems[CFG_EVENT].value, O_RDONLY)) < 0) {
fprintf(stderr,
"ERR %d:\n"
"Unable to open `%s'\n"
"%s\n",
errno, argv[1], strerror(errno)
);
}
/* Error check here as well. */
ioctl(fd, EVIOCGVERSION, &version);
ioctl(fd, EVIOCGID, id);
ioctl(fd, EVIOCGNAME(sizeof(name)), name);
fprintf(stderr,
"Name : %s\n"
"Version : %d.%d.%d\n"
"ID : Bus=%04x Vendor=%04x Product=%04x Version=%04x\n"
"----------\n"
,
name,
version >> 16,
(version >> 8) & 0xff,
version & 0xff,
id[ID_BUS],
id[ID_VENDOR],
id[ID_PRODUCT],
id[ID_VERSION]
);
/* Loop. Read event file and parse result. */
for (;;) {
sz = read(fd, ev, sizeof(struct input_event) * EV_BUF_SIZE);
if (sz < (int) sizeof(struct input_event)) {
fprintf(stderr,
"ERR %d:\n"
"Reading of `%s' failed\n"
"%s\n",
errno, argv[1], strerror(errno)
);
goto fine;
}
/* Implement code to translate type, code and value */
for (i = 0; i < sz / sizeof(struct input_event); ++i) {
if (ev[i].type == EV_KEY &&
ev[i].value ==1 &&
(ev[i].code ==113 || ev[i].code == 114 || ev[i].code==115 ))
{
switch (ev[i].code)
{
case 113:// mute/unmute
system(initItems[CFG_MUTE].value);
break;
case 114://volme down
system(initItems[CFG_VOLUP].value);
break;
case 115://volume up
system(initItems[CFG_VOLDWN].value);
break;
}
}
}
}
fine:
close(fd);
return errno;
}