Switched back to using pthread_create versus lwp_create.
[dragonfly/vkernel-mp.git] / lib / libusbhid / usage.c
blobf739a2c1a09624dfc0e47dd011e85ba248f59553
1 /* $NetBSD: usage.c,v 1.8 2000/10/10 19:23:58 is Exp $ */
3 /*
4 * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
28 * $FreeBSD: src/lib/libusbhid/usage.c,v 1.6.2.1 2002/04/03 15:54:00 joe Exp $
29 * $DragonFly: src/lib/libusbhid/usage.c,v 1.2 2003/06/17 06:26:51 dillon Exp $
32 #include <ctype.h>
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "libusbhid.h"
40 #define _PATH_HIDTABLE "/usr/share/misc/usb_hid_usages"
42 struct usage_in_page {
43 const char *name;
44 int usage;
47 static struct usage_page {
48 const char *name;
49 int usage;
50 struct usage_in_page *page_contents;
51 int pagesize, pagesizemax;
52 } *pages;
53 static int npages, npagesmax;
55 #ifdef DEBUG
56 void
57 dump_hid_table(void)
59 int i, j;
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);
69 #endif
71 void
72 hid_init(const char *hidname)
74 FILE *f;
75 char line[100], name[100], *p, *n;
76 int no;
77 int lineno;
78 struct usage_page *curpage = 0;
80 if (hidname == 0)
81 hidname = _PATH_HIDTABLE;
83 f = fopen(hidname, "r");
84 if (f == NULL)
85 err(1, "%s", hidname);
86 for (lineno = 1; ; lineno++) {
87 if (fgets(line, sizeof line, f) == NULL)
88 break;
89 if (line[0] == '#')
90 continue;
91 for (p = line; *p && isspace(*p); p++)
93 if (!*p)
94 continue;
95 if (sscanf(line, " * %[^\n]", name) == 1)
96 no = -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",
100 hidname, lineno);
101 for (p = name; *p; p++)
102 if (isspace(*p) || *p == '.')
103 *p = '_';
104 n = strdup(name);
105 if (!n)
106 err(1, "strdup");
107 if (isspace(line[0])) {
108 if (!curpage)
109 errx(1, "file %s, line %d, syntax error\n",
110 hidname, lineno);
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)
118 err(1, "realloc");
120 curpage->page_contents[curpage->pagesize].name = n;
121 curpage->page_contents[curpage->pagesize].usage = no;
122 curpage->pagesize++;
123 } else {
124 if (npages >= npagesmax) {
125 if (pages == 0) {
126 npagesmax = 5;
127 pages = malloc(npagesmax *
128 sizeof (struct usage_page));
129 } else {
130 npagesmax += 5;
131 pages = realloc(pages,
132 npagesmax *
133 sizeof (struct usage_page));
135 if (!pages)
136 err(1, "alloc");
138 curpage = &pages[npages++];
139 curpage->name = n;
140 curpage->usage = no;
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)
147 err(1, "malloc");
150 fclose(f);
151 #ifdef DEBUG
152 dump_hid_table();
153 #endif
156 const char *
157 hid_usage_page(int i)
159 static char b[10];
160 int k;
162 if (!pages)
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);
169 return b;
172 const char *
173 hid_usage_in_page(unsigned int u)
175 int page = HID_PAGE(u);
176 int i = HID_USAGE(u);
177 static char b[100];
178 int j, k, us;
180 for (k = 0; k < npages; k++)
181 if (pages[k].usage == page)
182 break;
183 if (k >= npages)
184 goto bad;
185 for (j = 0; j < pages[k].pagesize; j++) {
186 us = pages[k].page_contents[j].usage;
187 if (us == -1) {
188 sprintf(b, "%s %d",
189 pages[k].page_contents[j].name, i);
190 return b;
192 if (us == i)
193 return pages[k].page_contents[j].name;
195 bad:
196 sprintf(b, "0x%04x", i);
197 return b;
201 hid_parse_usage_page(const char *name)
203 int k;
205 if (!pages)
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;
211 return -1;
214 /* XXX handle hex */
216 hid_parse_usage_in_page(const char *name)
218 const char *sep = strchr(name, ':');
219 int k, j;
220 unsigned int l;
222 if (sep == NULL)
223 return -1;
224 l = sep - name;
225 for (k = 0; k < npages; k++)
226 if (strncmp(pages[k].name, name, l) == 0)
227 goto found;
228 return -1;
229 found:
230 sep++;
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;
234 return (-1);