1ce5b9347SMatthew N. Dodd /* $NetBSD: usbhidaction.c,v 1.8 2002/06/11 06:06:21 itojun Exp $ */
2ce5b9347SMatthew N. Dodd
31de7b4b8SPedro F. Giffuni /*-
4*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni *
6ce5b9347SMatthew N. Dodd * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
7ce5b9347SMatthew N. Dodd * All rights reserved.
8ce5b9347SMatthew N. Dodd *
9ce5b9347SMatthew N. Dodd * This code is derived from software contributed to The NetBSD Foundation
10ce5b9347SMatthew N. Dodd * by Lennart Augustsson <lennart@augustsson.net>.
11ce5b9347SMatthew N. Dodd *
12ce5b9347SMatthew N. Dodd * Redistribution and use in source and binary forms, with or without
13ce5b9347SMatthew N. Dodd * modification, are permitted provided that the following conditions
14ce5b9347SMatthew N. Dodd * are met:
15ce5b9347SMatthew N. Dodd * 1. Redistributions of source code must retain the above copyright
16ce5b9347SMatthew N. Dodd * notice, this list of conditions and the following disclaimer.
17ce5b9347SMatthew N. Dodd * 2. Redistributions in binary form must reproduce the above copyright
18ce5b9347SMatthew N. Dodd * notice, this list of conditions and the following disclaimer in the
19ce5b9347SMatthew N. Dodd * documentation and/or other materials provided with the distribution.
20ce5b9347SMatthew N. Dodd *
21ce5b9347SMatthew N. Dodd * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22ce5b9347SMatthew N. Dodd * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23ce5b9347SMatthew N. Dodd * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24ce5b9347SMatthew N. Dodd * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25ce5b9347SMatthew N. Dodd * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26ce5b9347SMatthew N. Dodd * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27ce5b9347SMatthew N. Dodd * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28ce5b9347SMatthew N. Dodd * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29ce5b9347SMatthew N. Dodd * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30ce5b9347SMatthew N. Dodd * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31ce5b9347SMatthew N. Dodd * POSSIBILITY OF SUCH DAMAGE.
32ce5b9347SMatthew N. Dodd */
33ce5b9347SMatthew N. Dodd
34ce5b9347SMatthew N. Dodd #include <stdio.h>
35ce5b9347SMatthew N. Dodd #include <stdlib.h>
36ce5b9347SMatthew N. Dodd #include <string.h>
37ce5b9347SMatthew N. Dodd #include <ctype.h>
38ce5b9347SMatthew N. Dodd #include <err.h>
39ce5b9347SMatthew N. Dodd #include <fcntl.h>
40ce5b9347SMatthew N. Dodd #include <limits.h>
41ce5b9347SMatthew N. Dodd #include <unistd.h>
42ce5b9347SMatthew N. Dodd #include <sys/types.h>
43bf41796cSAndrew Thompson #include <dev/usb/usbhid.h>
44ce5b9347SMatthew N. Dodd #include <usbhid.h>
45ce5b9347SMatthew N. Dodd #include <syslog.h>
46ce5b9347SMatthew N. Dodd #include <signal.h>
47821df508SXin LI #include <errno.h>
48ce5b9347SMatthew N. Dodd #include <sys/stat.h>
49ce5b9347SMatthew N. Dodd
50ce5b9347SMatthew N. Dodd static int verbose = 0;
51ce5b9347SMatthew N. Dodd static int isdemon = 0;
52ce5b9347SMatthew N. Dodd static int reparse = 1;
53a77c5ad8SDavid Malone static const char * pidfile = "/var/run/usbaction.pid";
54ce5b9347SMatthew N. Dodd
55ce5b9347SMatthew N. Dodd struct command {
56ce5b9347SMatthew N. Dodd struct command *next;
57ce5b9347SMatthew N. Dodd int line;
58ce5b9347SMatthew N. Dodd
59ce5b9347SMatthew N. Dodd struct hid_item item;
60ce5b9347SMatthew N. Dodd int value;
61f5a4d3f0SMatthew N. Dodd int lastseen;
62f5a4d3f0SMatthew N. Dodd int lastused;
63f5a4d3f0SMatthew N. Dodd int debounce;
64ce5b9347SMatthew N. Dodd char anyvalue;
65ce5b9347SMatthew N. Dodd char *name;
66ce5b9347SMatthew N. Dodd char *action;
67ce5b9347SMatthew N. Dodd };
68793cdaceSEd Schouten static struct command *commands;
69ce5b9347SMatthew N. Dodd
70ce5b9347SMatthew N. Dodd #define SIZE 4000
71ce5b9347SMatthew N. Dodd
72ce5b9347SMatthew N. Dodd void usage(void);
73ce5b9347SMatthew N. Dodd struct command *parse_conf(const char *, report_desc_t, int, int);
74ce5b9347SMatthew N. Dodd void docmd(struct command *, int, const char *, int, char **);
75ce5b9347SMatthew N. Dodd void freecommands(struct command *);
76ce5b9347SMatthew N. Dodd
77ce5b9347SMatthew N. Dodd static void
sighup(int sig __unused)78a77c5ad8SDavid Malone sighup(int sig __unused)
79ce5b9347SMatthew N. Dodd {
80ce5b9347SMatthew N. Dodd reparse = 1;
81ce5b9347SMatthew N. Dodd }
82ce5b9347SMatthew N. Dodd
83ce5b9347SMatthew N. Dodd int
main(int argc,char ** argv)84ce5b9347SMatthew N. Dodd main(int argc, char **argv)
85ce5b9347SMatthew N. Dodd {
86ce5b9347SMatthew N. Dodd const char *conf = NULL;
87ce5b9347SMatthew N. Dodd const char *dev = NULL;
88986bd841SWarner Losh const char *table = NULL;
89a77c5ad8SDavid Malone int fd, fp, ch, n, val, i;
90a77c5ad8SDavid Malone size_t sz, sz1;
91f5a4d3f0SMatthew N. Dodd int demon, ignore, dieearly;
92ce5b9347SMatthew N. Dodd report_desc_t repd;
93ce5b9347SMatthew N. Dodd char buf[100];
94ce5b9347SMatthew N. Dodd char devnamebuf[PATH_MAX];
95ce5b9347SMatthew N. Dodd struct command *cmd;
961bee2ec7SAlexander Motin int reportid = -1;
97ce5b9347SMatthew N. Dodd
98ce5b9347SMatthew N. Dodd demon = 1;
99ce5b9347SMatthew N. Dodd ignore = 0;
100f5a4d3f0SMatthew N. Dodd dieearly = 0;
1011bee2ec7SAlexander Motin while ((ch = getopt(argc, argv, "c:def:ip:r:t:v")) != -1) {
102ce5b9347SMatthew N. Dodd switch(ch) {
103ce5b9347SMatthew N. Dodd case 'c':
104ce5b9347SMatthew N. Dodd conf = optarg;
105ce5b9347SMatthew N. Dodd break;
106ce5b9347SMatthew N. Dodd case 'd':
107ce5b9347SMatthew N. Dodd demon ^= 1;
108ce5b9347SMatthew N. Dodd break;
109f5a4d3f0SMatthew N. Dodd case 'e':
110f5a4d3f0SMatthew N. Dodd dieearly = 1;
111f5a4d3f0SMatthew N. Dodd break;
112ce5b9347SMatthew N. Dodd case 'i':
113ce5b9347SMatthew N. Dodd ignore++;
114ce5b9347SMatthew N. Dodd break;
115ce5b9347SMatthew N. Dodd case 'f':
116ce5b9347SMatthew N. Dodd dev = optarg;
117ce5b9347SMatthew N. Dodd break;
118ce5b9347SMatthew N. Dodd case 'p':
119ce5b9347SMatthew N. Dodd pidfile = optarg;
120ce5b9347SMatthew N. Dodd break;
1211bee2ec7SAlexander Motin case 'r':
1221bee2ec7SAlexander Motin reportid = atoi(optarg);
1231bee2ec7SAlexander Motin break;
124986bd841SWarner Losh case 't':
125986bd841SWarner Losh table = optarg;
126986bd841SWarner Losh break;
127ce5b9347SMatthew N. Dodd case 'v':
128ce5b9347SMatthew N. Dodd demon = 0;
129ce5b9347SMatthew N. Dodd verbose++;
130ce5b9347SMatthew N. Dodd break;
131ce5b9347SMatthew N. Dodd case '?':
132ce5b9347SMatthew N. Dodd default:
133ce5b9347SMatthew N. Dodd usage();
134ce5b9347SMatthew N. Dodd }
135ce5b9347SMatthew N. Dodd }
136ce5b9347SMatthew N. Dodd argc -= optind;
137ce5b9347SMatthew N. Dodd argv += optind;
138ce5b9347SMatthew N. Dodd
139ce5b9347SMatthew N. Dodd if (conf == NULL || dev == NULL)
140ce5b9347SMatthew N. Dodd usage();
141ce5b9347SMatthew N. Dodd
142986bd841SWarner Losh hid_init(table);
143ce5b9347SMatthew N. Dodd
144ce5b9347SMatthew N. Dodd if (dev[0] != '/') {
145ce5b9347SMatthew N. Dodd snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
146ce5b9347SMatthew N. Dodd isdigit(dev[0]) ? "uhid" : "", dev);
147ce5b9347SMatthew N. Dodd dev = devnamebuf;
148ce5b9347SMatthew N. Dodd }
149ce5b9347SMatthew N. Dodd
150ce5b9347SMatthew N. Dodd fd = open(dev, O_RDWR);
151ce5b9347SMatthew N. Dodd if (fd < 0)
152ce5b9347SMatthew N. Dodd err(1, "%s", dev);
153ce5b9347SMatthew N. Dodd repd = hid_get_report_desc(fd);
154ce5b9347SMatthew N. Dodd if (repd == NULL)
155ce5b9347SMatthew N. Dodd err(1, "hid_get_report_desc() failed");
156ce5b9347SMatthew N. Dodd
157ce5b9347SMatthew N. Dodd commands = parse_conf(conf, repd, reportid, ignore);
158ce5b9347SMatthew N. Dodd
1591bee2ec7SAlexander Motin sz = (size_t)hid_report_size(repd, hid_input, -1);
160ce5b9347SMatthew N. Dodd
161ce5b9347SMatthew N. Dodd if (verbose)
162a77c5ad8SDavid Malone printf("report size %zu\n", sz);
163ce5b9347SMatthew N. Dodd if (sz > sizeof buf)
164ce5b9347SMatthew N. Dodd errx(1, "report too large");
165ce5b9347SMatthew N. Dodd
166ce5b9347SMatthew N. Dodd (void)signal(SIGHUP, sighup);
167ce5b9347SMatthew N. Dodd
168ce5b9347SMatthew N. Dodd if (demon) {
169ce5b9347SMatthew N. Dodd fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
170b27480c6SHans Petter Selasky if (fp < 0)
171ce5b9347SMatthew N. Dodd err(1, "%s", pidfile);
172f5a4d3f0SMatthew N. Dodd if (daemon(0, 0) < 0)
173f5a4d3f0SMatthew N. Dodd err(1, "daemon()");
174b27480c6SHans Petter Selasky snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
175b27480c6SHans Petter Selasky sz1 = strlen(buf);
176b27480c6SHans Petter Selasky if (write(fp, buf, sz1) < 0)
177b27480c6SHans Petter Selasky err(1, "%s", pidfile);
178b27480c6SHans Petter Selasky close(fp);
179ce5b9347SMatthew N. Dodd isdemon = 1;
180ce5b9347SMatthew N. Dodd }
181ce5b9347SMatthew N. Dodd
182ce5b9347SMatthew N. Dodd for(;;) {
183ce5b9347SMatthew N. Dodd n = read(fd, buf, sz);
184ce5b9347SMatthew N. Dodd if (verbose > 2) {
185ce5b9347SMatthew N. Dodd printf("read %d bytes:", n);
186ce5b9347SMatthew N. Dodd for (i = 0; i < n; i++)
187ce5b9347SMatthew N. Dodd printf(" %02x", buf[i]);
188ce5b9347SMatthew N. Dodd printf("\n");
189ce5b9347SMatthew N. Dodd }
190ce5b9347SMatthew N. Dodd if (n < 0) {
191ce5b9347SMatthew N. Dodd if (verbose)
192ce5b9347SMatthew N. Dodd err(1, "read");
193ce5b9347SMatthew N. Dodd else
194ce5b9347SMatthew N. Dodd exit(1);
195ce5b9347SMatthew N. Dodd }
196ce5b9347SMatthew N. Dodd #if 0
197ce5b9347SMatthew N. Dodd if (n != sz) {
198ce5b9347SMatthew N. Dodd err(2, "read size");
199ce5b9347SMatthew N. Dodd }
200ce5b9347SMatthew N. Dodd #endif
201ce5b9347SMatthew N. Dodd for (cmd = commands; cmd; cmd = cmd->next) {
2021bee2ec7SAlexander Motin if (cmd->item.report_ID != 0 &&
2031bee2ec7SAlexander Motin buf[0] != cmd->item.report_ID)
2041bee2ec7SAlexander Motin continue;
2051bee2ec7SAlexander Motin if (cmd->item.flags & HIO_VARIABLE)
206ce5b9347SMatthew N. Dodd val = hid_get_data(buf, &cmd->item);
2071bee2ec7SAlexander Motin else {
2081bee2ec7SAlexander Motin uint32_t pos = cmd->item.pos;
2091bee2ec7SAlexander Motin for (i = 0; i < cmd->item.report_count; i++) {
2101bee2ec7SAlexander Motin val = hid_get_data(buf, &cmd->item);
2111bee2ec7SAlexander Motin if (val == cmd->value)
2121bee2ec7SAlexander Motin break;
2131bee2ec7SAlexander Motin cmd->item.pos += cmd->item.report_size;
2141bee2ec7SAlexander Motin }
2151bee2ec7SAlexander Motin cmd->item.pos = pos;
2161bee2ec7SAlexander Motin val = (i < cmd->item.report_count) ?
2171bee2ec7SAlexander Motin cmd->value : -1;
2181bee2ec7SAlexander Motin }
219f5a4d3f0SMatthew N. Dodd if (cmd->value != val && cmd->anyvalue == 0)
220f5a4d3f0SMatthew N. Dodd goto next;
221f5a4d3f0SMatthew N. Dodd if ((cmd->debounce == 0) ||
2226ec73e0eSMatthew N. Dodd ((cmd->debounce == 1) && ((cmd->lastseen == -1) ||
223f5a4d3f0SMatthew N. Dodd (cmd->lastseen != val)))) {
224ce5b9347SMatthew N. Dodd docmd(cmd, val, dev, argc, argv);
225f5a4d3f0SMatthew N. Dodd goto next;
226ce5b9347SMatthew N. Dodd }
227f5a4d3f0SMatthew N. Dodd if ((cmd->debounce > 1) &&
228f5a4d3f0SMatthew N. Dodd ((cmd->lastused == -1) ||
229f5a4d3f0SMatthew N. Dodd (abs(cmd->lastused - val) >= cmd->debounce))) {
230f5a4d3f0SMatthew N. Dodd docmd(cmd, val, dev, argc, argv);
231f5a4d3f0SMatthew N. Dodd cmd->lastused = val;
232f5a4d3f0SMatthew N. Dodd goto next;
233f5a4d3f0SMatthew N. Dodd }
234f5a4d3f0SMatthew N. Dodd next:
235f5a4d3f0SMatthew N. Dodd cmd->lastseen = val;
236f5a4d3f0SMatthew N. Dodd }
237f5a4d3f0SMatthew N. Dodd
238f5a4d3f0SMatthew N. Dodd if (dieearly)
239f5a4d3f0SMatthew N. Dodd exit(0);
240f5a4d3f0SMatthew N. Dodd
241ce5b9347SMatthew N. Dodd if (reparse) {
242ce5b9347SMatthew N. Dodd struct command *cmds =
243ce5b9347SMatthew N. Dodd parse_conf(conf, repd, reportid, ignore);
244ce5b9347SMatthew N. Dodd if (cmds) {
245ce5b9347SMatthew N. Dodd freecommands(commands);
246ce5b9347SMatthew N. Dodd commands = cmds;
247ce5b9347SMatthew N. Dodd }
248ce5b9347SMatthew N. Dodd reparse = 0;
249ce5b9347SMatthew N. Dodd }
250ce5b9347SMatthew N. Dodd }
251ce5b9347SMatthew N. Dodd
252ce5b9347SMatthew N. Dodd exit(0);
253ce5b9347SMatthew N. Dodd }
254ce5b9347SMatthew N. Dodd
255ce5b9347SMatthew N. Dodd void
usage(void)256ce5b9347SMatthew N. Dodd usage(void)
257ce5b9347SMatthew N. Dodd {
258ce5b9347SMatthew N. Dodd
259f5a4d3f0SMatthew N. Dodd fprintf(stderr, "Usage: %s [-deiv] -c config_file -f hid_dev "
260986bd841SWarner Losh "[-p pidfile] [-t tablefile]\n", getprogname());
261ce5b9347SMatthew N. Dodd exit(1);
262ce5b9347SMatthew N. Dodd }
263ce5b9347SMatthew N. Dodd
264ce5b9347SMatthew N. Dodd static int
peek(FILE * f)265ce5b9347SMatthew N. Dodd peek(FILE *f)
266ce5b9347SMatthew N. Dodd {
267ce5b9347SMatthew N. Dodd int c;
268ce5b9347SMatthew N. Dodd
269ce5b9347SMatthew N. Dodd c = getc(f);
270ce5b9347SMatthew N. Dodd if (c != EOF)
271ce5b9347SMatthew N. Dodd ungetc(c, f);
272ce5b9347SMatthew N. Dodd return c;
273ce5b9347SMatthew N. Dodd }
274ce5b9347SMatthew N. Dodd
275ce5b9347SMatthew N. Dodd struct command *
parse_conf(const char * conf,report_desc_t repd,int reportid,int ignore)276ce5b9347SMatthew N. Dodd parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
277ce5b9347SMatthew N. Dodd {
278ce5b9347SMatthew N. Dodd FILE *f;
279ce5b9347SMatthew N. Dodd char *p;
280ce5b9347SMatthew N. Dodd int line;
281f5a4d3f0SMatthew N. Dodd char buf[SIZE], name[SIZE], value[SIZE], debounce[SIZE], action[SIZE];
2827778ab7eSAlexander Motin char usbuf[SIZE], coll[SIZE], *tmp;
283ce5b9347SMatthew N. Dodd struct command *cmd, *cmds;
284ce5b9347SMatthew N. Dodd struct hid_data *d;
285ce5b9347SMatthew N. Dodd struct hid_item h;
2867778ab7eSAlexander Motin int inst, cinst, u, lo, hi, range, t;
287ce5b9347SMatthew N. Dodd
288ce5b9347SMatthew N. Dodd f = fopen(conf, "r");
289ce5b9347SMatthew N. Dodd if (f == NULL)
290ce5b9347SMatthew N. Dodd err(1, "%s", conf);
291ce5b9347SMatthew N. Dodd
292ce5b9347SMatthew N. Dodd cmds = NULL;
293ce5b9347SMatthew N. Dodd for (line = 1; ; line++) {
294ce5b9347SMatthew N. Dodd if (fgets(buf, sizeof buf, f) == NULL)
295ce5b9347SMatthew N. Dodd break;
296ce5b9347SMatthew N. Dodd if (buf[0] == '#' || buf[0] == '\n')
297ce5b9347SMatthew N. Dodd continue;
298ce5b9347SMatthew N. Dodd p = strchr(buf, '\n');
299ce5b9347SMatthew N. Dodd while (p && isspace(peek(f))) {
300ce5b9347SMatthew N. Dodd if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
301ce5b9347SMatthew N. Dodd break;
302ce5b9347SMatthew N. Dodd p = strchr(buf, '\n');
303ce5b9347SMatthew N. Dodd }
304ce5b9347SMatthew N. Dodd if (p)
305ce5b9347SMatthew N. Dodd *p = 0;
306f5a4d3f0SMatthew N. Dodd if (sscanf(buf, "%s %s %s %[^\n]",
307f5a4d3f0SMatthew N. Dodd name, value, debounce, action) != 4) {
308ce5b9347SMatthew N. Dodd if (isdemon) {
309ce5b9347SMatthew N. Dodd syslog(LOG_WARNING, "config file `%s', line %d"
310ce5b9347SMatthew N. Dodd ", syntax error: %s", conf, line, buf);
311ce5b9347SMatthew N. Dodd freecommands(cmds);
312ce5b9347SMatthew N. Dodd return (NULL);
313ce5b9347SMatthew N. Dodd } else {
314ce5b9347SMatthew N. Dodd errx(1, "config file `%s', line %d,"
315ce5b9347SMatthew N. Dodd ", syntax error: %s", conf, line, buf);
316ce5b9347SMatthew N. Dodd }
317ce5b9347SMatthew N. Dodd }
3187778ab7eSAlexander Motin tmp = strchr(name, '#');
3197778ab7eSAlexander Motin if (tmp != NULL) {
3207778ab7eSAlexander Motin *tmp = 0;
3217778ab7eSAlexander Motin inst = atoi(tmp + 1);
3227778ab7eSAlexander Motin } else
3237778ab7eSAlexander Motin inst = 0;
324ce5b9347SMatthew N. Dodd
325ce5b9347SMatthew N. Dodd cmd = malloc(sizeof *cmd);
326ce5b9347SMatthew N. Dodd if (cmd == NULL)
327ce5b9347SMatthew N. Dodd err(1, "malloc failed");
328ce5b9347SMatthew N. Dodd cmd->next = cmds;
329ce5b9347SMatthew N. Dodd cmds = cmd;
330ce5b9347SMatthew N. Dodd cmd->line = line;
331ce5b9347SMatthew N. Dodd
332ce5b9347SMatthew N. Dodd if (strcmp(value, "*") == 0) {
333ce5b9347SMatthew N. Dodd cmd->anyvalue = 1;
334ce5b9347SMatthew N. Dodd } else {
335ce5b9347SMatthew N. Dodd cmd->anyvalue = 0;
336ce5b9347SMatthew N. Dodd if (sscanf(value, "%d", &cmd->value) != 1) {
337ce5b9347SMatthew N. Dodd if (isdemon) {
338ce5b9347SMatthew N. Dodd syslog(LOG_WARNING,
339ce5b9347SMatthew N. Dodd "config file `%s', line %d, "
340f5a4d3f0SMatthew N. Dodd "bad value: %s (should be * or a number)\n",
341ce5b9347SMatthew N. Dodd conf, line, value);
342ce5b9347SMatthew N. Dodd freecommands(cmds);
343ce5b9347SMatthew N. Dodd return (NULL);
344ce5b9347SMatthew N. Dodd } else {
345ce5b9347SMatthew N. Dodd errx(1, "config file `%s', line %d, "
346f5a4d3f0SMatthew N. Dodd "bad value: %s (should be * or a number)\n",
347ce5b9347SMatthew N. Dodd conf, line, value);
348ce5b9347SMatthew N. Dodd }
349ce5b9347SMatthew N. Dodd }
350ce5b9347SMatthew N. Dodd }
351ce5b9347SMatthew N. Dodd
352f5a4d3f0SMatthew N. Dodd if (sscanf(debounce, "%d", &cmd->debounce) != 1) {
353f5a4d3f0SMatthew N. Dodd if (isdemon) {
354f5a4d3f0SMatthew N. Dodd syslog(LOG_WARNING,
355f5a4d3f0SMatthew N. Dodd "config file `%s', line %d, "
356f5a4d3f0SMatthew N. Dodd "bad value: %s (should be a number >= 0)\n",
357f5a4d3f0SMatthew N. Dodd conf, line, debounce);
358f5a4d3f0SMatthew N. Dodd freecommands(cmds);
359f5a4d3f0SMatthew N. Dodd return (NULL);
360f5a4d3f0SMatthew N. Dodd } else {
361f5a4d3f0SMatthew N. Dodd errx(1, "config file `%s', line %d, "
362f5a4d3f0SMatthew N. Dodd "bad value: %s (should be a number >= 0)\n",
363f5a4d3f0SMatthew N. Dodd conf, line, debounce);
364f5a4d3f0SMatthew N. Dodd }
365f5a4d3f0SMatthew N. Dodd }
366f5a4d3f0SMatthew N. Dodd
367ce5b9347SMatthew N. Dodd coll[0] = 0;
3687778ab7eSAlexander Motin cinst = 0;
369ce5b9347SMatthew N. Dodd for (d = hid_start_parse(repd, 1 << hid_input, reportid);
370ce5b9347SMatthew N. Dodd hid_get_item(d, &h); ) {
371ce5b9347SMatthew N. Dodd if (verbose > 2)
372ce5b9347SMatthew N. Dodd printf("kind=%d usage=%x\n", h.kind, h.usage);
373ce5b9347SMatthew N. Dodd if (h.flags & HIO_CONST)
374ce5b9347SMatthew N. Dodd continue;
375ce5b9347SMatthew N. Dodd switch (h.kind) {
376ce5b9347SMatthew N. Dodd case hid_input:
377ce5b9347SMatthew N. Dodd if (h.usage_minimum != 0 ||
378ce5b9347SMatthew N. Dodd h.usage_maximum != 0) {
379ce5b9347SMatthew N. Dodd lo = h.usage_minimum;
380ce5b9347SMatthew N. Dodd hi = h.usage_maximum;
381ce5b9347SMatthew N. Dodd range = 1;
382ce5b9347SMatthew N. Dodd } else {
383ce5b9347SMatthew N. Dodd lo = h.usage;
384ce5b9347SMatthew N. Dodd hi = h.usage;
385ce5b9347SMatthew N. Dodd range = 0;
386ce5b9347SMatthew N. Dodd }
387ce5b9347SMatthew N. Dodd for (u = lo; u <= hi; u++) {
388ce5b9347SMatthew N. Dodd if (coll[0]) {
389a77c5ad8SDavid Malone snprintf(usbuf, sizeof usbuf,
390ce5b9347SMatthew N. Dodd "%s.%s:%s", coll+1,
391ce5b9347SMatthew N. Dodd hid_usage_page(HID_PAGE(u)),
392ce5b9347SMatthew N. Dodd hid_usage_in_page(u));
3937778ab7eSAlexander Motin } else {
3947778ab7eSAlexander Motin snprintf(usbuf, sizeof usbuf,
3957778ab7eSAlexander Motin "%s:%s",
3967778ab7eSAlexander Motin hid_usage_page(HID_PAGE(u)),
3977778ab7eSAlexander Motin hid_usage_in_page(u));
398ce5b9347SMatthew N. Dodd }
3997778ab7eSAlexander Motin if (verbose > 2)
4007778ab7eSAlexander Motin printf("usage %s\n", usbuf);
4017778ab7eSAlexander Motin t = strlen(usbuf) - strlen(name);
4027778ab7eSAlexander Motin if (t > 0) {
4037778ab7eSAlexander Motin if (strcmp(usbuf + t, name))
4047778ab7eSAlexander Motin continue;
4057778ab7eSAlexander Motin if (usbuf[t - 1] != '.')
4067778ab7eSAlexander Motin continue;
4077778ab7eSAlexander Motin } else if (strcmp(usbuf, name))
4087778ab7eSAlexander Motin continue;
4097778ab7eSAlexander Motin if (inst == cinst++)
4107778ab7eSAlexander Motin goto foundhid;
411ce5b9347SMatthew N. Dodd }
412ce5b9347SMatthew N. Dodd break;
413ce5b9347SMatthew N. Dodd case hid_collection:
414ce5b9347SMatthew N. Dodd snprintf(coll + strlen(coll),
415ce5b9347SMatthew N. Dodd sizeof coll - strlen(coll), ".%s:%s",
416ce5b9347SMatthew N. Dodd hid_usage_page(HID_PAGE(h.usage)),
417ce5b9347SMatthew N. Dodd hid_usage_in_page(h.usage));
418ce5b9347SMatthew N. Dodd break;
419ce5b9347SMatthew N. Dodd case hid_endcollection:
420ce5b9347SMatthew N. Dodd if (coll[0])
421ce5b9347SMatthew N. Dodd *strrchr(coll, '.') = 0;
422ce5b9347SMatthew N. Dodd break;
423ce5b9347SMatthew N. Dodd default:
424ce5b9347SMatthew N. Dodd break;
425ce5b9347SMatthew N. Dodd }
426ce5b9347SMatthew N. Dodd }
427ce5b9347SMatthew N. Dodd if (ignore) {
428ce5b9347SMatthew N. Dodd if (verbose)
429ce5b9347SMatthew N. Dodd warnx("ignore item '%s'", name);
430ce5b9347SMatthew N. Dodd continue;
431ce5b9347SMatthew N. Dodd }
432ce5b9347SMatthew N. Dodd if (isdemon) {
433ce5b9347SMatthew N. Dodd syslog(LOG_WARNING, "config file `%s', line %d, HID "
434ce5b9347SMatthew N. Dodd "item not found: `%s'\n", conf, line, name);
435ce5b9347SMatthew N. Dodd freecommands(cmds);
436ce5b9347SMatthew N. Dodd return (NULL);
437ce5b9347SMatthew N. Dodd } else {
438ce5b9347SMatthew N. Dodd errx(1, "config file `%s', line %d, HID item "
439ce5b9347SMatthew N. Dodd "not found: `%s'\n", conf, line, name);
440ce5b9347SMatthew N. Dodd }
441ce5b9347SMatthew N. Dodd
442ce5b9347SMatthew N. Dodd foundhid:
443ce5b9347SMatthew N. Dodd hid_end_parse(d);
4446ec73e0eSMatthew N. Dodd cmd->lastseen = -1;
4456ec73e0eSMatthew N. Dodd cmd->lastused = -1;
446ce5b9347SMatthew N. Dodd cmd->item = h;
447ce5b9347SMatthew N. Dodd cmd->name = strdup(name);
448ce5b9347SMatthew N. Dodd cmd->action = strdup(action);
449ce5b9347SMatthew N. Dodd if (range) {
450ce5b9347SMatthew N. Dodd if (cmd->value == 1)
451ce5b9347SMatthew N. Dodd cmd->value = u - lo;
452ce5b9347SMatthew N. Dodd else
453ce5b9347SMatthew N. Dodd cmd->value = -1;
454ce5b9347SMatthew N. Dodd }
455ce5b9347SMatthew N. Dodd
456ce5b9347SMatthew N. Dodd if (verbose)
457ce5b9347SMatthew N. Dodd printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
458ce5b9347SMatthew N. Dodd cmd->value, cmd->action);
459ce5b9347SMatthew N. Dodd }
460ce5b9347SMatthew N. Dodd fclose(f);
461ce5b9347SMatthew N. Dodd return (cmds);
462ce5b9347SMatthew N. Dodd }
463ce5b9347SMatthew N. Dodd
464ce5b9347SMatthew N. Dodd void
docmd(struct command * cmd,int value,const char * hid,int argc,char ** argv)465ce5b9347SMatthew N. Dodd docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
466ce5b9347SMatthew N. Dodd {
467ce5b9347SMatthew N. Dodd char cmdbuf[SIZE], *p, *q;
468ce5b9347SMatthew N. Dodd size_t len;
469ce5b9347SMatthew N. Dodd int n, r;
470ce5b9347SMatthew N. Dodd
471ce5b9347SMatthew N. Dodd for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
472ce5b9347SMatthew N. Dodd if (*p == '$') {
473ce5b9347SMatthew N. Dodd p++;
474ce5b9347SMatthew N. Dodd len = &cmdbuf[SIZE-1] - q;
475ce5b9347SMatthew N. Dodd if (isdigit(*p)) {
476ce5b9347SMatthew N. Dodd n = strtol(p, &p, 10) - 1;
477ce5b9347SMatthew N. Dodd if (n >= 0 && n < argc) {
478ce5b9347SMatthew N. Dodd strncpy(q, argv[n], len);
479ce5b9347SMatthew N. Dodd q += strlen(q);
480ce5b9347SMatthew N. Dodd }
481ce5b9347SMatthew N. Dodd } else if (*p == 'V') {
482ce5b9347SMatthew N. Dodd p++;
483ce5b9347SMatthew N. Dodd snprintf(q, len, "%d", value);
484ce5b9347SMatthew N. Dodd q += strlen(q);
485ce5b9347SMatthew N. Dodd } else if (*p == 'N') {
486ce5b9347SMatthew N. Dodd p++;
487ce5b9347SMatthew N. Dodd strncpy(q, cmd->name, len);
488ce5b9347SMatthew N. Dodd q += strlen(q);
489ce5b9347SMatthew N. Dodd } else if (*p == 'H') {
490ce5b9347SMatthew N. Dodd p++;
491ce5b9347SMatthew N. Dodd strncpy(q, hid, len);
492ce5b9347SMatthew N. Dodd q += strlen(q);
493ce5b9347SMatthew N. Dodd } else if (*p) {
494ce5b9347SMatthew N. Dodd *q++ = *p++;
495ce5b9347SMatthew N. Dodd }
496ce5b9347SMatthew N. Dodd } else {
497ce5b9347SMatthew N. Dodd *q++ = *p++;
498ce5b9347SMatthew N. Dodd }
499ce5b9347SMatthew N. Dodd }
500ce5b9347SMatthew N. Dodd *q = 0;
501ce5b9347SMatthew N. Dodd
502ce5b9347SMatthew N. Dodd if (verbose)
503ce5b9347SMatthew N. Dodd printf("system '%s'\n", cmdbuf);
504ce5b9347SMatthew N. Dodd r = system(cmdbuf);
505ce5b9347SMatthew N. Dodd if (verbose > 1 && r)
506ce5b9347SMatthew N. Dodd printf("return code = 0x%x\n", r);
507ce5b9347SMatthew N. Dodd }
508ce5b9347SMatthew N. Dodd
509ce5b9347SMatthew N. Dodd void
freecommands(struct command * cmd)510ce5b9347SMatthew N. Dodd freecommands(struct command *cmd)
511ce5b9347SMatthew N. Dodd {
512ce5b9347SMatthew N. Dodd struct command *next;
513ce5b9347SMatthew N. Dodd
514ce5b9347SMatthew N. Dodd while (cmd) {
515ce5b9347SMatthew N. Dodd next = cmd->next;
516ce5b9347SMatthew N. Dodd free(cmd);
517ce5b9347SMatthew N. Dodd cmd = next;
518ce5b9347SMatthew N. Dodd }
519ce5b9347SMatthew N. Dodd }
520