1 /* $NetBSD: usage.c,v 1.6 2004/10/28 21:14:52 dsl 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 __RCSID("$NetBSD: usage.c,v 1.6 2004/10/28 21:14:52 dsl Exp $");
41 #define _PATH_HIDTABLE "/usr/share/misc/usb_hid_usages"
43 struct usage_in_page
{
48 static struct usage_page
{
51 struct usage_in_page
*page_contents
;
52 int pagesize
, pagesizemax
;
54 static int npages
, npagesmax
;
57 static void dump_hid_table(void);
63 for (i
= 0; i
< npages
; i
++) {
64 printf("%d\t%s\n", pages
[i
].usage
, pages
[i
].name
);
65 for (j
= 0; j
< pages
[i
].pagesize
; j
++) {
66 printf("\t%d\t%s\n", pages
[i
].page_contents
[j
].usage
,
67 pages
[i
].page_contents
[j
].name
);
74 hid_init(const char *hidname
)
77 char line
[100], name
[100], *p
, *n
;
80 struct usage_page
*curpage
= 0;
83 hidname
= _PATH_HIDTABLE
;
85 f
= fopen(hidname
, "r");
87 err(1, "%s", hidname
);
88 for (lineno
= 1; ; lineno
++) {
89 if (fgets(line
, sizeof line
, f
) == NULL
)
93 for (p
= line
; *p
&& isspace((unsigned char)*p
); p
++)
97 if (sscanf(line
, " * %[^\n]", name
) == 1)
99 else if (sscanf(line
, " 0x%x %[^\n]", &no
, name
) != 2 &&
100 sscanf(line
, " %d %[^\n]", &no
, name
) != 2)
101 errx(1, "file %s, line %d, syntax error",
103 for (p
= name
; *p
; p
++)
104 if (isspace((unsigned char)*p
) || *p
== '.')
109 if (isspace((unsigned char)line
[0])) {
111 errx(1, "file %s, line %d, syntax error",
113 if (curpage
->pagesize
>= curpage
->pagesizemax
) {
114 curpage
->pagesizemax
+= 10;
115 curpage
->page_contents
=
116 realloc(curpage
->page_contents
,
117 curpage
->pagesizemax
*
118 sizeof (struct usage_in_page
));
119 if (!curpage
->page_contents
)
122 curpage
->page_contents
[curpage
->pagesize
].name
= n
;
123 curpage
->page_contents
[curpage
->pagesize
].usage
= no
;
126 if (npages
>= npagesmax
) {
129 pages
= malloc(npagesmax
*
130 sizeof (struct usage_page
));
133 pages
= realloc(pages
,
135 sizeof (struct usage_page
));
140 curpage
= &pages
[npages
++];
143 curpage
->pagesize
= 0;
144 curpage
->pagesizemax
= 10;
145 curpage
->page_contents
=
146 malloc(curpage
->pagesizemax
*
147 sizeof (struct usage_in_page
));
148 if (!curpage
->page_contents
)
159 hid_usage_page(int i
)
165 errx(1, "no hid table");
167 for (k
= 0; k
< npages
; k
++)
168 if (pages
[k
].usage
== i
)
169 return pages
[k
].name
;
170 sprintf(b
, "0x%04x", i
);
175 hid_usage_in_page(unsigned int u
)
177 int page
= HID_PAGE(u
);
178 int i
= HID_USAGE(u
);
182 for (k
= 0; k
< npages
; k
++)
183 if (pages
[k
].usage
== page
)
187 for (j
= 0; j
< pages
[k
].pagesize
; j
++) {
188 us
= pages
[k
].page_contents
[j
].usage
;
191 fmtcheck(pages
[k
].page_contents
[j
].name
, "%d"),
196 return pages
[k
].page_contents
[j
].name
;
199 sprintf(b
, "0x%04x", i
);
204 hid_parse_usage_page(const char *name
)
209 errx(1, "no hid table");
211 for (k
= 0; k
< npages
; k
++)
212 if (strcmp(pages
[k
].name
, name
) == 0)
213 return pages
[k
].usage
;
219 hid_parse_usage_in_page(const char *name
)
225 _DIAGASSERT(name
!= NULL
);
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
;