1 /* $NetBSD: usbhidaction.c,v 1.8 2002/06/11 06:06:21 itojun Exp $ */
5 * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson <lennart@augustsson.net>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
48 #include <sys/types.h>
49 #include <dev/usb/usbhid.h>
56 static int verbose = 0;
57 static int isdemon = 0;
58 static int reparse = 1;
59 static const char * pidfile = "/var/run/usbaction.pid";
74 struct command *commands;
79 struct command *parse_conf(const char *, report_desc_t, int, int);
80 void docmd(struct command *, int, const char *, int, char **);
81 void freecommands(struct command *);
84 sighup(int sig __unused)
90 main(int argc, char **argv)
92 const char *conf = NULL;
93 const char *dev = NULL;
94 const char *table = NULL;
95 int fd, fp, ch, n, val, i;
97 int demon, ignore, dieearly;
100 char devnamebuf[PATH_MAX];
107 while ((ch = getopt(argc, argv, "c:def:ip:r:t:v")) != -1) {
128 reportid = atoi(optarg);
145 if (conf == NULL || dev == NULL)
151 snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
152 isdigit(dev[0]) ? "uhid" : "", dev);
156 fd = open(dev, O_RDWR);
159 repd = hid_get_report_desc(fd);
161 err(1, "hid_get_report_desc() failed");
163 commands = parse_conf(conf, repd, reportid, ignore);
165 sz = (size_t)hid_report_size(repd, hid_input, -1);
168 printf("report size %zu\n", sz);
170 errx(1, "report too large");
172 (void)signal(SIGHUP, sighup);
175 fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
177 sz1 = snprintf(buf, sizeof buf, "%d\n", getpid());
178 if (sz1 > sizeof buf)
183 err(1, "%s", pidfile);
184 if (daemon(0, 0) < 0)
190 n = read(fd, buf, sz);
192 printf("read %d bytes:", n);
193 for (i = 0; i < n; i++)
194 printf(" %02x", buf[i]);
208 for (cmd = commands; cmd; cmd = cmd->next) {
209 if (cmd->item.report_ID != 0 &&
210 buf[0] != cmd->item.report_ID)
212 if (cmd->item.flags & HIO_VARIABLE)
213 val = hid_get_data(buf, &cmd->item);
215 uint32_t pos = cmd->item.pos;
216 for (i = 0; i < cmd->item.report_count; i++) {
217 val = hid_get_data(buf, &cmd->item);
218 if (val == cmd->value)
220 cmd->item.pos += cmd->item.report_size;
223 val = (i < cmd->item.report_count) ?
226 if (cmd->value != val && cmd->anyvalue == 0)
228 if ((cmd->debounce == 0) ||
229 ((cmd->debounce == 1) && ((cmd->lastseen == -1) ||
230 (cmd->lastseen != val)))) {
231 docmd(cmd, val, dev, argc, argv);
234 if ((cmd->debounce > 1) &&
235 ((cmd->lastused == -1) ||
236 (abs(cmd->lastused - val) >= cmd->debounce))) {
237 docmd(cmd, val, dev, argc, argv);
249 struct command *cmds =
250 parse_conf(conf, repd, reportid, ignore);
252 freecommands(commands);
266 fprintf(stderr, "Usage: %s [-deiv] -c config_file -f hid_dev "
267 "[-p pidfile] [-t tablefile]\n", getprogname());
283 parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
288 char buf[SIZE], name[SIZE], value[SIZE], debounce[SIZE], action[SIZE];
289 char usbuf[SIZE], coll[SIZE], *tmp;
290 struct command *cmd, *cmds;
293 int inst, cinst, u, lo, hi, range, t;
295 f = fopen(conf, "r");
300 for (line = 1; ; line++) {
301 if (fgets(buf, sizeof buf, f) == NULL)
303 if (buf[0] == '#' || buf[0] == '\n')
305 p = strchr(buf, '\n');
306 while (p && isspace(peek(f))) {
307 if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
309 p = strchr(buf, '\n');
313 if (sscanf(buf, "%s %s %s %[^\n]",
314 name, value, debounce, action) != 4) {
316 syslog(LOG_WARNING, "config file `%s', line %d"
317 ", syntax error: %s", conf, line, buf);
321 errx(1, "config file `%s', line %d,"
322 ", syntax error: %s", conf, line, buf);
325 tmp = strchr(name, '#');
328 inst = atoi(tmp + 1);
332 cmd = malloc(sizeof *cmd);
334 err(1, "malloc failed");
339 if (strcmp(value, "*") == 0) {
343 if (sscanf(value, "%d", &cmd->value) != 1) {
346 "config file `%s', line %d, "
347 "bad value: %s (should be * or a number)\n",
352 errx(1, "config file `%s', line %d, "
353 "bad value: %s (should be * or a number)\n",
359 if (sscanf(debounce, "%d", &cmd->debounce) != 1) {
362 "config file `%s', line %d, "
363 "bad value: %s (should be a number >= 0)\n",
364 conf, line, debounce);
368 errx(1, "config file `%s', line %d, "
369 "bad value: %s (should be a number >= 0)\n",
370 conf, line, debounce);
376 for (d = hid_start_parse(repd, 1 << hid_input, reportid);
377 hid_get_item(d, &h); ) {
379 printf("kind=%d usage=%x\n", h.kind, h.usage);
380 if (h.flags & HIO_CONST)
384 if (h.usage_minimum != 0 ||
385 h.usage_maximum != 0) {
386 lo = h.usage_minimum;
387 hi = h.usage_maximum;
394 for (u = lo; u <= hi; u++) {
396 snprintf(usbuf, sizeof usbuf,
398 hid_usage_page(HID_PAGE(u)),
399 hid_usage_in_page(u));
401 snprintf(usbuf, sizeof usbuf,
403 hid_usage_page(HID_PAGE(u)),
404 hid_usage_in_page(u));
407 printf("usage %s\n", usbuf);
408 t = strlen(usbuf) - strlen(name);
410 if (strcmp(usbuf + t, name))
412 if (usbuf[t - 1] != '.')
414 } else if (strcmp(usbuf, name))
421 snprintf(coll + strlen(coll),
422 sizeof coll - strlen(coll), ".%s:%s",
423 hid_usage_page(HID_PAGE(h.usage)),
424 hid_usage_in_page(h.usage));
426 case hid_endcollection:
428 *strrchr(coll, '.') = 0;
436 warnx("ignore item '%s'", name);
440 syslog(LOG_WARNING, "config file `%s', line %d, HID "
441 "item not found: `%s'\n", conf, line, name);
445 errx(1, "config file `%s', line %d, HID item "
446 "not found: `%s'\n", conf, line, name);
454 cmd->name = strdup(name);
455 cmd->action = strdup(action);
464 printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
465 cmd->value, cmd->action);
472 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
474 char cmdbuf[SIZE], *p, *q;
478 for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
481 len = &cmdbuf[SIZE-1] - q;
483 n = strtol(p, &p, 10) - 1;
484 if (n >= 0 && n < argc) {
485 strncpy(q, argv[n], len);
488 } else if (*p == 'V') {
490 snprintf(q, len, "%d", value);
492 } else if (*p == 'N') {
494 strncpy(q, cmd->name, len);
496 } else if (*p == 'H') {
498 strncpy(q, hid, len);
510 printf("system '%s'\n", cmdbuf);
512 if (verbose > 1 && r)
513 printf("return code = 0x%x\n", r);
517 freecommands(struct command *cmd)
519 struct command *next;