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 int fd, fp, ch, n, val, i;
98 int demon, ignore, dieearly;
101 char devnamebuf[PATH_MAX];
108 while ((ch = getopt(argc, argv, "c:def:ip:v")) != -1) {
140 if (conf == NULL || dev == NULL)
146 snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
147 isdigit(dev[0]) ? "uhid" : "", dev);
151 fd = open(dev, O_RDWR);
154 if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
156 repd = hid_get_report_desc(fd);
158 err(1, "hid_get_report_desc() failed");
160 commands = parse_conf(conf, repd, reportid, ignore);
162 sz = (size_t)hid_report_size(repd, hid_input, reportid);
165 printf("report size %zu\n", sz);
167 errx(1, "report too large");
169 (void)signal(SIGHUP, sighup);
172 fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
174 sz1 = snprintf(buf, sizeof buf, "%d\n", getpid());
175 if (sz1 > sizeof buf)
180 err(1, "%s", pidfile);
181 if (daemon(0, 0) < 0)
187 n = read(fd, buf, sz);
189 printf("read %d bytes:", n);
190 for (i = 0; i < n; i++)
191 printf(" %02x", buf[i]);
205 for (cmd = commands; cmd; cmd = cmd->next) {
206 val = hid_get_data(buf, &cmd->item);
207 if (cmd->value != val && cmd->anyvalue == 0)
209 if ((cmd->debounce == 0) ||
210 ((cmd->debounce == 1) && ((cmd->lastseen == -1) ||
211 (cmd->lastseen != val)))) {
212 docmd(cmd, val, dev, argc, argv);
215 if ((cmd->debounce > 1) &&
216 ((cmd->lastused == -1) ||
217 (abs(cmd->lastused - val) >= cmd->debounce))) {
218 docmd(cmd, val, dev, argc, argv);
230 struct command *cmds =
231 parse_conf(conf, repd, reportid, ignore);
233 freecommands(commands);
247 fprintf(stderr, "Usage: %s [-deiv] -c config_file -f hid_dev "
248 "[-p pidfile]\n", getprogname());
264 parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
269 char buf[SIZE], name[SIZE], value[SIZE], debounce[SIZE], action[SIZE];
270 char usbuf[SIZE], coll[SIZE];
271 struct command *cmd, *cmds;
274 int u, lo, hi, range;
277 f = fopen(conf, "r");
282 for (line = 1; ; line++) {
283 if (fgets(buf, sizeof buf, f) == NULL)
285 if (buf[0] == '#' || buf[0] == '\n')
287 p = strchr(buf, '\n');
288 while (p && isspace(peek(f))) {
289 if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
291 p = strchr(buf, '\n');
295 if (sscanf(buf, "%s %s %s %[^\n]",
296 name, value, debounce, action) != 4) {
298 syslog(LOG_WARNING, "config file `%s', line %d"
299 ", syntax error: %s", conf, line, buf);
303 errx(1, "config file `%s', line %d,"
304 ", syntax error: %s", conf, line, buf);
308 cmd = malloc(sizeof *cmd);
310 err(1, "malloc failed");
315 if (strcmp(value, "*") == 0) {
319 if (sscanf(value, "%d", &cmd->value) != 1) {
322 "config file `%s', line %d, "
323 "bad value: %s (should be * or a number)\n",
328 errx(1, "config file `%s', line %d, "
329 "bad value: %s (should be * or a number)\n",
335 if (sscanf(debounce, "%d", &cmd->debounce) != 1) {
338 "config file `%s', line %d, "
339 "bad value: %s (should be a number >= 0)\n",
340 conf, line, debounce);
344 errx(1, "config file `%s', line %d, "
345 "bad value: %s (should be a number >= 0)\n",
346 conf, line, debounce);
351 for (d = hid_start_parse(repd, 1 << hid_input, reportid);
352 hid_get_item(d, &h); ) {
354 printf("kind=%d usage=%x\n", h.kind, h.usage);
355 if (h.flags & HIO_CONST)
359 if (h.usage_minimum != 0 ||
360 h.usage_maximum != 0) {
361 lo = h.usage_minimum;
362 hi = h.usage_maximum;
369 for (u = lo; u <= hi; u++) {
370 snprintf(usbuf, sizeof usbuf, "%s:%s",
371 hid_usage_page(HID_PAGE(u)),
372 hid_usage_in_page(u));
374 printf("usage %s\n", usbuf);
375 if (!strcasecmp(usbuf, name))
378 snprintf(usbuf, sizeof usbuf,
380 hid_usage_page(HID_PAGE(u)),
381 hid_usage_in_page(u));
385 if (!strcasecmp(usbuf, name))
391 snprintf(coll + strlen(coll),
392 sizeof coll - strlen(coll), ".%s:%s",
393 hid_usage_page(HID_PAGE(h.usage)),
394 hid_usage_in_page(h.usage));
396 case hid_endcollection:
398 *strrchr(coll, '.') = 0;
406 warnx("ignore item '%s'", name);
410 syslog(LOG_WARNING, "config file `%s', line %d, HID "
411 "item not found: `%s'\n", conf, line, name);
415 errx(1, "config file `%s', line %d, HID item "
416 "not found: `%s'\n", conf, line, name);
424 cmd->name = strdup(name);
425 cmd->action = strdup(action);
434 printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
435 cmd->value, cmd->action);
442 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
444 char cmdbuf[SIZE], *p, *q;
448 for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
451 len = &cmdbuf[SIZE-1] - q;
453 n = strtol(p, &p, 10) - 1;
454 if (n >= 0 && n < argc) {
455 strncpy(q, argv[n], len);
458 } else if (*p == 'V') {
460 snprintf(q, len, "%d", value);
462 } else if (*p == 'N') {
464 strncpy(q, cmd->name, len);
466 } else if (*p == 'H') {
468 strncpy(q, hid, len);
480 printf("system '%s'\n", cmdbuf);
482 if (verbose > 1 && r)
483 printf("return code = 0x%x\n", r);
487 freecommands(struct command *cmd)
489 struct command *next;