1 /* $NetBSD: usage.c,v 1.8 2000/10/10 19:23:58 is Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
44 #define _PATH_HIDTABLE "/usr/share/misc/usb_hid_usages"
46 struct usage_in_page {
51 static struct usage_page {
54 struct usage_in_page *page_contents;
55 int pagesize, pagesizemax;
57 static int npages, npagesmax;
65 for (i = 0; i < npages; i++) {
66 printf("%d\t%s\n", pages[i].usage, pages[i].name);
67 for (j = 0; j < pages[i].pagesize; j++) {
68 printf("\t%d\t%s\n", pages[i].page_contents[j].usage,
69 pages[i].page_contents[j].name);
76 hid_init(const char *hidname)
79 char line[100], name[100], *p, *n;
82 struct usage_page *curpage = NULL;
85 hidname = _PATH_HIDTABLE;
87 f = fopen(hidname, "r");
89 err(1, "%s", hidname);
90 for (lineno = 1; ; lineno++) {
91 if (fgets(line, sizeof line, f) == NULL)
95 for (p = line; *p && isspace(*p); p++)
99 if (sscanf(line, " * %[^\n]", name) == 1)
101 else if (sscanf(line, " 0x%x %[^\n]", &no, name) != 2 &&
102 sscanf(line, " %d %[^\n]", &no, name) != 2)
103 errx(1, "file %s, line %d, syntax error",
105 for (p = name; *p; p++)
106 if (isspace(*p) || *p == '.')
111 if (isspace(line[0])) {
113 errx(1, "file %s, line %d, syntax error",
115 if (curpage->pagesize >= curpage->pagesizemax) {
116 curpage->pagesizemax += 10;
117 curpage->page_contents =
118 realloc(curpage->page_contents,
119 curpage->pagesizemax *
120 sizeof (struct usage_in_page));
121 if (!curpage->page_contents)
124 curpage->page_contents[curpage->pagesize].name = n;
125 curpage->page_contents[curpage->pagesize].usage = no;
128 if (npages >= npagesmax) {
131 pages = malloc(npagesmax *
132 sizeof (struct usage_page));
135 pages = realloc(pages,
137 sizeof (struct usage_page));
142 curpage = &pages[npages++];
145 curpage->pagesize = 0;
146 curpage->pagesizemax = 10;
147 curpage->page_contents =
148 malloc(curpage->pagesizemax *
149 sizeof (struct usage_in_page));
150 if (!curpage->page_contents)
161 hid_usage_page(int i)
167 errx(1, "no hid table");
169 for (k = 0; k < npages; k++)
170 if (pages[k].usage == i)
171 return pages[k].name;
172 sprintf(b, "0x%04x", i);
177 hid_usage_in_page(unsigned int u)
179 int page = HID_PAGE(u);
180 int i = HID_USAGE(u);
184 for (k = 0; k < npages; k++)
185 if (pages[k].usage == page)
189 for (j = 0; j < pages[k].pagesize; j++) {
190 us = pages[k].page_contents[j].usage;
193 fmtcheck(pages[k].page_contents[j].name, "%d"),
198 return pages[k].page_contents[j].name;
201 sprintf(b, "0x%04x", i);
206 hid_parse_usage_page(const char *name)
211 errx(1, "no hid table");
213 for (k = 0; k < npages; k++)
214 if (strcmp(pages[k].name, name) == 0)
215 return pages[k].usage;
221 hid_parse_usage_in_page(const char *name)
227 sep = strchr(name, ':');
231 for (k = 0; k < npages; k++)
232 if (strncmp(pages[k].name, name, l) == 0)
237 for (j = 0; j < pages[k].pagesize; j++)
238 if (strcmp(pages[k].page_contents[j].name, sep) == 0)
239 return (pages[k].usage << 16) | pages[k].page_contents[j].usage;