Add a command to perform commands when USB HID items take on certain

values.
Some sample uses: controlling USB speakers with the HID buttons on them,
	invoking the proper programs from the HID buttons on modern USB
	keyboards.
This commit is contained in:
augustss 2000-12-30 13:07:48 +00:00
parent c6b9ad5aeb
commit 6a55a964fc
4 changed files with 447 additions and 2 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.114 2000/12/22 01:31:48 itojun Exp $
# $NetBSD: Makefile,v 1.115 2000/12/30 13:07:48 augustss Exp $
# from: @(#)Makefile 8.3 (Berkeley) 1/7/94
.include <bsd.own.mk>
@ -21,7 +21,7 @@ SUBDIR= apply apropos asa at audio banner basename biff \
showmount shuffle skey skeyinfo skeyinit soelim split \
su systat tail talk tcopy tee telnet tftp time \
tip tn3270 top touch tput tr true tset tsort tty ul \
uname unexpand unifdef uniq units unvis usbhidctl users \
uname unexpand unifdef uniq units unvis usbhidaction usbhidctl users \
uudecode uuencode vacation vgrind vi vis vmstat w \
wall wc what whatis whereis which who whois window \
write xargs xinstall xlint xstr yacc yes ypcat \

View File

@ -0,0 +1,12 @@
# $NetBSD: Makefile,v 1.1 2000/12/30 13:07:48 augustss Exp $
.include <bsd.own.mk>
PROG= usbhidaction
SRCS= usbhidaction.c
LDADD+= -lusb -lutil
DPADD+= ${LIBUSB} ${LIBUTIL}
.include <bsd.prog.mk>
.include <bsd.subdir.mk>

View File

@ -0,0 +1,134 @@
.\" $NetBSD: usbhidaction.1,v 1.1 2000/12/30 13:07:49 augustss Exp $
.\"
.\" Copyright (c) 2000 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Lennart Augustsson (lennart@augustsson.net).
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd December 29, 2000
.Dt USBHIDACTION 1
.Os
.Sh NAME
.Nm usbhidaction
.Nd perform actions according to USB HID controls
.Sh SYNOPSIS
.Nm ""
.Fl c Ar config-file
.Op Fl d
.Op Fl i
.Fl f Ar device
.Op Fl v
.Ar arg ...
.Sh DESCRIPTION
.Nm
can be used to execute commands when certain values appear on HID controls.
The normal operation for this program is to read the configuration file
and then become a daemon and execute commands as the HID items specify.
If a read from the HID device fails the program dies; this will make it
die when the USB device is unplugged.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl c Ar config-file
Specify a path name for the config file.
.It Fl d
Toggle the daemon flag.
.It Fl i
Ignore HID items in the config file that does not exist in the device.
.It Fl f Ar device
Specify a path name for the device to operate on. If
.Ar device
is numeric, it is taken to be the USB HID device number. If it is a relative
path, it is taken to be the name of the device under
.Pa /dev .
An absolute path is taken to be the literal device pathname.
.It Fl v
Be verbose, and do not become a daemon.
.El
.Sh CONFIGURATION
The configuration file has a very simple format. Each line describes an
action; if a line begins with a whitespace it is considered a continuation
of the previous line. Lines beginning with `#' are considered as comments.
.Pp
Each line has three parts: a name of a USB HID item, a value for that item,
and an action. There must be whitespace between the parts.
.Pp
The item names are similar to those used by
.Xr usbhidctl 1 ,
but each part must be prefixed by its page name.
.Pp
The value is simply a numeric value. When the item reports this value
the action will be performed.
If the value is `*' it will match any value.
.Pp
The action is a normal command that is executed with
.Xr system 3 .
Before it is executed some substitution will occur:
an occurence of `$n' will be replaced by the nth argument on the
command line, an occurence of `$V' will be replaced by the numeric value
of the HID item, an occurence of `$N' will be replaced by the name
of the control, and an occurence of `$H' will be replaced by the name
of the HID device.
.Sh EXAMPLES
The following configuration file can be used to control a pair
of Philips USB speakers with the HID controls on the speakers.
.Bd -literal -offset indent
# Configuration for various Philips USB speakers
Consumer:Consumer_Control.Consumer:Volume_Up 1
mixerctl -f $1 -n -w fea8-i7-master++
Consumer:Consumer_Control.Consumer:Volume_Down 1
mixerctl -f $1 -n -w fea8-i7-master--
Consumer:Consumer_Control.Consumer:Mute 1
mixerctl -f $1 -n -w fea8-i7-mute++
Consumer:Consumer_Control.Consumer:Channel_Top.Microsoft:Base_Up 1
mixerctl -f $1 -n -w fea8-i7-bass++
Consumer:Consumer_Control.Consumer:Channel_Top.Microsoft:Base_Down 1
mixerctl -f $1 -n -w fea8-i7-bass--
.Ed
.Pp
A sample invokation using this configuration would be
.Bd -literal -offset indent
usbhidaction -f /dev/uhid1 -c conf /dev/mixer1
.Ed
.Sh FILES
.Pa /usr/share/misc/usb_hid_usages
The HID usage table.
.Sh SEE ALSO
.Xr usbhidctl 1 ,
.Xr usb 3 ,
.Xr uhid 4 ,
.Xr usb 4
.Sh HISTORY
The
.Nm
command first appeared in
.Nx 1.6 .

View File

@ -0,0 +1,299 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#include <usb.h>
#include <util.h>
int verbose = 0;
struct command {
struct command *next;
int line;
struct hid_item item;
int value;
char anyvalue;
char *name;
char *action;
};
struct command *commands;
#define SIZE 4000
void usage(void);
void parse_conf(const char *conf, report_desc_t repd, int ignore);
void docmd(struct command *cmd, int value, const char *hid,
int argc, char **argv);
int
main(int argc, char **argv)
{
const char *conf = NULL;
const char *hid = NULL;
int fd, ch, sz, n, val;
int demon, ignore;
report_desc_t repd;
char buf[100];
struct command *cmd;
demon = 1;
ignore = 0;
while ((ch = getopt(argc, argv, "c:dif:v")) != -1) {
switch(ch) {
case 'c':
conf = optarg;
break;
case 'd':
demon ^= 1;
break;
case 'i':
ignore++;
break;
case 'f':
hid = optarg;
break;
case 'v':
demon = 0;
verbose++;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (conf == NULL || hid == NULL)
usage();
hid_init(NULL);
fd = open(hid, O_RDWR);
if (fd < 0)
err(1, "%s", hid);
repd = hid_get_report_desc(fd);
if (repd == NULL)
err(1, "hid_get_report_desc() failed\n");
parse_conf(conf, repd, ignore);
sz = hid_report_size(repd, hid_input, NULL);
hid_dispose_report_desc(repd);
if (verbose)
printf("report size %d\n", sz);
if (sz > sizeof buf)
errx(1, "report too large");
if (demon) {
if (daemon(0, 0) < 0)
err(1, "daemon()");
pidfile(NULL);
}
for(;;) {
n = read(fd, buf, sz);
if (verbose > 2)
printf("read %d bytes [%02x]\n", n, buf[0]);
if (n < 0) {
if (verbose)
err(1, "read");
else
exit(1);
}
for (cmd = commands; cmd; cmd = cmd->next) {
val = hid_get_data(buf, &cmd->item);
if (cmd->value == val || cmd->anyvalue)
docmd(cmd, val, hid, argc, argv);
}
}
exit(0);
}
void
usage(void)
{
extern char *__progname;
fprintf(stderr, "Usage: %s -c config_file [-d] -f hid_dev "
" [-v]\n", __progname);
exit(1);
}
static int
peek(FILE *f)
{
int c;
c = getc(f);
if (c != EOF)
ungetc(c, f);
return c;
}
void
parse_conf(const char *conf, report_desc_t repd, int ignore)
{
FILE *f;
char *p;
int line;
char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
char usage[SIZE], coll[SIZE];
struct command *cmd;
struct hid_data *d;
struct hid_item h;
f = fopen(conf, "r");
if (f == NULL)
err(1, "%s", conf);
for (line = 1; ; line++) {
if (fgets(buf, sizeof buf, f) == NULL)
break;
if (buf[0] == '#' || buf[0] == '\n')
continue;
p = strchr(buf, '\n');
while (p && isspace(peek(f))) {
if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
break;
p = strchr(buf, '\n');
}
if (p)
*p = 0;
if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
errx(1, "config file `%s', line %d, syntax error: %s",
conf, line, buf);
}
cmd = malloc(sizeof *cmd);
if (cmd == NULL)
err(1, "malloc failed");
cmd->next = commands;
commands = cmd;
cmd->line = line;
if (strcmp(value, "*") == 0) {
cmd->anyvalue = 1;
} else {
cmd->anyvalue = 0;
if (sscanf(value, "%d", &cmd->value) != 1)
errx(1, "config file `%s', line %d, "
"bad value: %s\n", conf, line, value);
}
coll[0] = 0;
for (d = hid_start_parse(repd, 1 << hid_input);
hid_get_item(d, &h); ) {
if (verbose > 2)
printf("kind=%d usage=%x\n", h.kind, h.usage);
if (h.flags & HIO_CONST)
continue;
switch (h.kind) {
case hid_input:
snprintf(usage, sizeof usage, "%s:%s",
hid_usage_page(HID_PAGE(h.usage)),
hid_usage_in_page(h.usage));
if (verbose > 2)
printf("usage %s\n", usage);
if (strcasecmp(usage, name) == 0)
goto foundhid;
if (coll[0]) {
snprintf(usage, sizeof usage,
"%s.%s:%s", coll+1,
hid_usage_page(HID_PAGE(h.usage)),
hid_usage_in_page(h.usage));
if (verbose > 2)
printf("usage %s\n", usage);
if (strcasecmp(usage, name) == 0)
goto foundhid;
}
break;
case hid_collection:
snprintf(coll + strlen(coll),
sizeof coll - strlen(coll), ".%s:%s",
hid_usage_page(HID_PAGE(h.usage)),
hid_usage_in_page(h.usage));
break;
case hid_endcollection:
if (coll[0])
*strrchr(coll, '.') = 0;
break;
default:
break;
}
}
if (ignore) {
if (verbose)
warnx("ignore item '%s'\n", name);
continue;
}
errx(1, "config file `%s', line %d, HID item not found: `%s'\n",
conf, line, name);
foundhid:
hid_end_parse(d);
cmd->item = h;
cmd->name = strdup(name);
cmd->action = strdup(action);
if (verbose)
printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
cmd->value, cmd->action);
}
fclose(f);
}
void
docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
{
char cmdbuf[SIZE], *p, *q;
size_t len;
int n, r;
for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
if (*p == '$') {
p++;
len = &cmdbuf[SIZE-1] - q;
if (isdigit(*p)) {
n = strtol(p, &p, 10) - 1;
if (n >= 0 && n < argc) {
strncpy(q, argv[n], len);
q += strlen(q);
}
} else if (*p == 'V') {
p++;
snprintf(q, len, "%d", value);
q += strlen(q);
} else if (*p == 'N') {
p++;
strncpy(q, cmd->name, len);
q += strlen(q);
} else if (*p == 'H') {
p++;
strncpy(q, hid, len);
q += strlen(q);
} else if (*p) {
*q++ = *p++;
}
} else {
*q++ = *p++;
}
}
*q = 0;
if (verbose)
printf("system '%s'\n", cmdbuf);
r = system(cmdbuf);
if (verbose > 1 && r)
printf("return code = 0x%x\n", r);
}