1 /* $NetBSD: usage.c,v 1.8 2000/10/10 19:23:58 is Exp $ */
4 * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
40 #define _PATH_HIDTABLE "/usr/share/misc/usb_hid_usages"
42 struct usage_in_page {
47 static struct usage_page {
50 struct usage_in_page *page_contents;
51 int pagesize, pagesizemax;
53 static int npages, npagesmax;
61 for (i = 0; i < npages; i++) {
62 printf("%d\t%s\n", pages[i].usage, pages[i].name);
63 for (j = 0; j < pages[i].pagesize; j++) {
64 printf("\t%d\t%s\n", pages[i].page_contents[j].usage,
65 pages[i].page_contents[j].name);
72 hid_init(const char *hidname)
75 char line[100], name[100], *p, *n;
78 struct usage_page *curpage = 0;
81 hidname = _PATH_HIDTABLE;
83 f = fopen(hidname, "r");
85 err(1, "%s", hidname);
86 for (lineno = 1; ; lineno++) {
87 if (fgets(line, sizeof line, f) == NULL)
91 for (p = line; *p && isspace(*p); p++)
95 if (sscanf(line, " * %[^\n]", name) == 1)
97 else if (sscanf(line, " 0x%x %[^\n]", &no, name) != 2 &&
98 sscanf(line, " %d %[^\n]", &no, name) != 2)
99 errx(1, "file %s, line %d, syntax error\n",
101 for (p = name; *p; p++)
102 if (isspace(*p) || *p == '.')
107 if (isspace(line[0])) {
109 errx(1, "file %s, line %d, syntax error\n",
111 if (curpage->pagesize >= curpage->pagesizemax) {
112 curpage->pagesizemax += 10;
113 curpage->page_contents =
114 realloc(curpage->page_contents,
115 curpage->pagesizemax *
116 sizeof (struct usage_in_page));
117 if (!curpage->page_contents)
120 curpage->page_contents[curpage->pagesize].name = n;
121 curpage->page_contents[curpage->pagesize].usage = no;
124 if (npages >= npagesmax) {
127 pages = malloc(npagesmax *
128 sizeof (struct usage_page));
131 pages = realloc(pages,
133 sizeof (struct usage_page));
138 curpage = &pages[npages++];
141 curpage->pagesize = 0;
142 curpage->pagesizemax = 10;
143 curpage->page_contents =
144 malloc(curpage->pagesizemax *
145 sizeof (struct usage_in_page));
146 if (!curpage->page_contents)
157 hid_usage_page(int i)
163 errx(1, "no hid table\n");
165 for (k = 0; k < npages; k++)
166 if (pages[k].usage == i)
167 return pages[k].name;
168 sprintf(b, "0x%04x", i);
173 hid_usage_in_page(unsigned int u)
175 int page = HID_PAGE(u);
176 int i = HID_USAGE(u);
180 for (k = 0; k < npages; k++)
181 if (pages[k].usage == page)
185 for (j = 0; j < pages[k].pagesize; j++) {
186 us = pages[k].page_contents[j].usage;
189 pages[k].page_contents[j].name, i);
193 return pages[k].page_contents[j].name;
196 sprintf(b, "0x%04x", i);
201 hid_parse_usage_page(const char *name)
206 errx(1, "no hid table\n");
208 for (k = 0; k < npages; k++)
209 if (strcmp(pages[k].name, name) == 0)
210 return pages[k].usage;
216 hid_parse_usage_in_page(const char *name)
218 const char *sep = strchr(name, ':');
225 for (k = 0; k < npages; k++)
226 if (strncmp(pages[k].name, name, l) == 0)
231 for (j = 0; j < pages[k].pagesize; j++)
232 if (strcmp(pages[k].page_contents[j].name, sep) == 0)
233 return (pages[k].usage << 16) | pages[k].page_contents[j].usage;