UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / libusbhid / usage.c
blob1bc99e7183e81ac13d6b61792996007e80923eba
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.
29 #include <assert.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "apc.h"
37 #include "usbhid.h"
39 #define _PATH_HIDTABLE "/usr/share/misc/usb_hid_usages"
41 struct usage_in_page {
42 const char *name;
43 int usage;
46 static struct usage_page {
47 const char *name;
48 int usage;
49 struct usage_in_page *page_contents;
50 int pagesize, pagesizemax;
51 } *pages;
52 static int npages, npagesmax;
54 #ifdef DEBUG
55 void
56 dump_hid_table(void)
58 int i, j;
60 for (i = 0; i < npages; i++) {
61 printf("%d\t%s\n", pages[i].usage, pages[i].name);
62 for (j = 0; j < pages[i].pagesize; j++) {
63 printf("\t%d\t%s\n", pages[i].page_contents[j].usage,
64 pages[i].page_contents[j].name);
68 #endif
70 void
71 hid_init(const char *hidname)
73 FILE *f;
74 char line[100], name[100], *p, *n;
75 int no;
76 int lineno;
77 struct usage_page *curpage = 0;
79 if (hidname == 0)
80 hidname = _PATH_HIDTABLE;
82 f = fopen(hidname, "r");
83 if (f == NULL)
84 err(1, "%s", hidname);
85 for (lineno = 1; ; lineno++) {
86 if (fgets(line, sizeof line, f) == NULL)
87 break;
88 if (line[0] == '#')
89 continue;
90 for (p = line; *p && isspace(*p); p++)
92 if (!*p)
93 continue;
94 if (sscanf(line, " * %[^\n]", name) == 1)
95 no = -1;
96 else if (sscanf(line, " 0x%x %[^\n]", &no, name) != 2 &&
97 sscanf(line, " %d %[^\n]", &no, name) != 2)
98 errx(1, "file %s, line %d, syntax error",
99 hidname, lineno);
100 for (p = name; *p; p++)
101 if (isspace(*p) || *p == '.')
102 *p = '_';
103 n = strdup(name);
104 if (!n)
105 err(1, "strdup");
106 if (isspace(line[0])) {
107 if (!curpage)
108 errx(1, "file %s, line %d, syntax error",
109 hidname, lineno);
110 if (curpage->pagesize >= curpage->pagesizemax) {
111 curpage->pagesizemax += 10;
112 curpage->page_contents = (usage_in_page *)
113 realloc(curpage->page_contents,
114 curpage->pagesizemax *
115 sizeof (struct usage_in_page));
116 if (!curpage->page_contents)
117 err(1, "realloc");
119 curpage->page_contents[curpage->pagesize].name = n;
120 curpage->page_contents[curpage->pagesize].usage = no;
121 curpage->pagesize++;
122 } else {
123 if (npages >= npagesmax) {
124 if (pages == 0) {
125 npagesmax = 5;
126 pages = (usage_page *)malloc(npagesmax *
127 sizeof (struct usage_page));
128 } else {
129 npagesmax += 5;
130 pages = (usage_page *)realloc(pages,
131 npagesmax *
132 sizeof (struct usage_page));
134 if (!pages)
135 err(1, "alloc");
137 curpage = &pages[npages++];
138 curpage->name = n;
139 curpage->usage = no;
140 curpage->pagesize = 0;
141 curpage->pagesizemax = 10;
142 curpage->page_contents = (usage_in_page *)
143 malloc(curpage->pagesizemax *
144 sizeof (struct usage_in_page));
145 if (!curpage->page_contents)
146 err(1, "malloc");
149 fclose(f);
150 #ifdef DEBUG
151 dump_hid_table();
152 #endif
155 const char *
156 hid_usage_page(int i)
158 static char b[10];
159 int k;
161 if (!pages)
162 errx(1, "no hid table");
164 for (k = 0; k < npages; k++)
165 if (pages[k].usage == i)
166 return pages[k].name;
167 asnprintf(b, sizeof(b), "0x%04x", i);
168 return b;
171 #define fmtcheck(x,y) (x)
173 const char *
174 hid_usage_in_page(unsigned int u)
176 int page = HID_PAGE(u);
177 int i = HID_USAGE(u);
178 static char b[100];
179 int j, k, us;
181 for (k = 0; k < npages; k++)
182 if (pages[k].usage == page)
183 break;
184 if (k >= npages)
185 goto bad;
186 for (j = 0; j < pages[k].pagesize; j++) {
187 us = pages[k].page_contents[j].usage;
188 if (us == -1) {
189 asnprintf(b, sizeof(b),
190 fmtcheck(pages[k].page_contents[j].name, "%d"),
192 return b;
194 if (us == i)
195 return pages[k].page_contents[j].name;
197 bad:
198 asnprintf(b, sizeof(b), "0x%04x", i);
199 return b;
203 hid_parse_usage_page(const char *name)
205 int k;
207 if (!pages)
208 errx(1, "no hid table");
210 for (k = 0; k < npages; k++)
211 if (strcmp(pages[k].name, name) == 0)
212 return pages[k].usage;
213 return -1;
216 /* XXX handle hex */
218 hid_parse_usage_in_page(const char *name)
220 const char *sep;
221 int k, j;
222 unsigned int l;
224 sep = strchr(name, ':');
225 if (sep == NULL)
226 return -1;
227 l = sep - name;
228 for (k = 0; k < npages; k++)
229 if (strncmp(pages[k].name, name, l) == 0)
230 goto found;
231 return -1;
232 found:
233 sep++;
234 for (j = 0; j < pages[k].pagesize; j++)
235 if (strcmp(pages[k].page_contents[j].name, sep) == 0)
236 return (pages[k].usage << 16) | pages[k].page_contents[j].usage;
237 return (-1);