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 <sys/ioctl.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbhid.h>
58 static int verbose = 0;
59 static int isdemon = 0;
60 static int reparse = 1;
61 static const char * pidfile = "/var/run/usbaction.pid";
76 struct command *commands;
81 struct command *parse_conf(const char *, report_desc_t, int, int);
82 void docmd(struct command *, int, const char *, int, char **);
83 void freecommands(struct command *);
86 sighup(int sig __unused)
92 main(int argc, char **argv)
94 const char *conf = NULL;
95 const char *dev = NULL;
96 const char *table = NULL;
97 int fd, fp, ch, n, val, i;
99 int demon, ignore, dieearly;
102 char devnamebuf[PATH_MAX];
109 while ((ch = getopt(argc, argv, "c:def:ip:t:v")) != -1) {
144 if (conf == NULL || dev == NULL)
150 snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
151 isdigit(dev[0]) ? "uhid" : "", dev);
155 fd = open(dev, O_RDWR);
158 if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
160 repd = hid_get_report_desc(fd);
162 err(1, "hid_get_report_desc() failed");
164 commands = parse_conf(conf, repd, reportid, ignore);
166 sz = (size_t)hid_report_size(repd, hid_input, reportid);
169 printf("report size %zu\n", sz);
171 errx(1, "report too large");
173 (void)signal(SIGHUP, sighup);
176 fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
178 sz1 = snprintf(buf, sizeof buf, "%d\n", getpid());
179 if (sz1 > sizeof buf)
184 err(1, "%s", pidfile);
185 if (daemon(0, 0) < 0)
191 n = read(fd, buf, sz);
193 printf("read %d bytes:", n);
194 for (i = 0; i < n; i++)
195 printf(" %02x", buf[i]);
209 for (cmd = commands; cmd; cmd = cmd->next) {
210 val = hid_get_data(buf, &cmd->item);
211 if (cmd->value != val && cmd->anyvalue == 0)
213 if ((cmd->debounce == 0) ||
214 ((cmd->debounce == 1) && ((cmd->lastseen == -1) ||
215 (cmd->lastseen != val)))) {
216 docmd(cmd, val, dev, argc, argv);
219 if ((cmd->debounce > 1) &&
220 ((cmd->lastused == -1) ||
221 (abs(cmd->lastused - val) >= cmd->debounce))) {
222 docmd(cmd, val, dev, argc, argv);
234 struct command *cmds =
235 parse_conf(conf, repd, reportid, ignore);
237 freecommands(commands);
251 fprintf(stderr, "Usage: %s [-deiv] -c config_file -f hid_dev "
252 "[-p pidfile] [-t tablefile]\n", getprogname());
268 parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
273 char buf[SIZE], name[SIZE], value[SIZE], debounce[SIZE], action[SIZE];
274 char usbuf[SIZE], coll[SIZE];
275 struct command *cmd, *cmds;
278 int u, lo, hi, range;
281 f = fopen(conf, "r");
286 for (line = 1; ; line++) {
287 if (fgets(buf, sizeof buf, f) == NULL)
289 if (buf[0] == '#' || buf[0] == '\n')
291 p = strchr(buf, '\n');
292 while (p && isspace(peek(f))) {
293 if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
295 p = strchr(buf, '\n');
299 if (sscanf(buf, "%s %s %s %[^\n]",
300 name, value, debounce, action) != 4) {
302 syslog(LOG_WARNING, "config file `%s', line %d"
303 ", syntax error: %s", conf, line, buf);
307 errx(1, "config file `%s', line %d,"
308 ", syntax error: %s", conf, line, buf);
312 cmd = malloc(sizeof *cmd);
314 err(1, "malloc failed");
319 if (strcmp(value, "*") == 0) {
323 if (sscanf(value, "%d", &cmd->value) != 1) {
326 "config file `%s', line %d, "
327 "bad value: %s (should be * or a number)\n",
332 errx(1, "config file `%s', line %d, "
333 "bad value: %s (should be * or a number)\n",
339 if (sscanf(debounce, "%d", &cmd->debounce) != 1) {
342 "config file `%s', line %d, "
343 "bad value: %s (should be a number >= 0)\n",
344 conf, line, debounce);
348 errx(1, "config file `%s', line %d, "
349 "bad value: %s (should be a number >= 0)\n",
350 conf, line, debounce);
355 for (d = hid_start_parse(repd, 1 << hid_input, reportid);
356 hid_get_item(d, &h); ) {
358 printf("kind=%d usage=%x\n", h.kind, h.usage);
359 if (h.flags & HIO_CONST)
363 if (h.usage_minimum != 0 ||
364 h.usage_maximum != 0) {
365 lo = h.usage_minimum;
366 hi = h.usage_maximum;
373 for (u = lo; u <= hi; u++) {
374 snprintf(usbuf, sizeof usbuf, "%s:%s",
375 hid_usage_page(HID_PAGE(u)),
376 hid_usage_in_page(u));
378 printf("usage %s\n", usbuf);
379 if (!strcasecmp(usbuf, name))
382 snprintf(usbuf, sizeof usbuf,
384 hid_usage_page(HID_PAGE(u)),
385 hid_usage_in_page(u));
389 if (!strcasecmp(usbuf, name))
395 snprintf(coll + strlen(coll),
396 sizeof coll - strlen(coll), ".%s:%s",
397 hid_usage_page(HID_PAGE(h.usage)),
398 hid_usage_in_page(h.usage));
400 case hid_endcollection:
402 *strrchr(coll, '.') = 0;
410 warnx("ignore item '%s'", name);
414 syslog(LOG_WARNING, "config file `%s', line %d, HID "
415 "item not found: `%s'\n", conf, line, name);
419 errx(1, "config file `%s', line %d, HID item "
420 "not found: `%s'\n", conf, line, name);
428 cmd->name = strdup(name);
429 cmd->action = strdup(action);
438 printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
439 cmd->value, cmd->action);
446 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
448 char cmdbuf[SIZE], *p, *q;
452 for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
455 len = &cmdbuf[SIZE-1] - q;
457 n = strtol(p, &p, 10) - 1;
458 if (n >= 0 && n < argc) {
459 strncpy(q, argv[n], len);
462 } else if (*p == 'V') {
464 snprintf(q, len, "%d", value);
466 } else if (*p == 'N') {
468 strncpy(q, cmd->name, len);
470 } else if (*p == 'H') {
472 strncpy(q, hid, len);
484 printf("system '%s'\n", cmdbuf);
486 if (verbose > 1 && r)
487 printf("return code = 0x%x\n", r);
491 freecommands(struct command *cmd)
493 struct command *next;