1706f2543Smrg/*
2706f2543Smrg *
3706f2543Smrg * Copyright (c) 1997  Metro Link Incorporated
4706f2543Smrg *
5706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6706f2543Smrg * copy of this software and associated documentation files (the "Software"),
7706f2543Smrg * to deal in the Software without restriction, including without limitation
8706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
10706f2543Smrg * Software is furnished to do so, subject to the following conditions:
11706f2543Smrg *
12706f2543Smrg * The above copyright notice and this permission notice shall be included in
13706f2543Smrg * all copies or substantial portions of the Software.
14706f2543Smrg *
15706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18706f2543Smrg * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19706f2543Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20706f2543Smrg * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21706f2543Smrg * SOFTWARE.
22706f2543Smrg *
23706f2543Smrg * Except as contained in this notice, the name of the Metro Link shall not be
24706f2543Smrg * used in advertising or otherwise to promote the sale, use or other dealings
25706f2543Smrg * in this Software without prior written authorization from Metro Link.
26706f2543Smrg *
27706f2543Smrg */
28706f2543Smrg/*
29706f2543Smrg * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
30706f2543Smrg *
31706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
32706f2543Smrg * copy of this software and associated documentation files (the "Software"),
33706f2543Smrg * to deal in the Software without restriction, including without limitation
34706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
36706f2543Smrg * Software is furnished to do so, subject to the following conditions:
37706f2543Smrg *
38706f2543Smrg * The above copyright notice and this permission notice shall be included in
39706f2543Smrg * all copies or substantial portions of the Software.
40706f2543Smrg *
41706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
44706f2543Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
45706f2543Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
46706f2543Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
47706f2543Smrg * OTHER DEALINGS IN THE SOFTWARE.
48706f2543Smrg *
49706f2543Smrg * Except as contained in this notice, the name of the copyright holder(s)
50706f2543Smrg * and author(s) shall not be used in advertising or otherwise to promote
51706f2543Smrg * the sale, use or other dealings in this Software without prior written
52706f2543Smrg * authorization from the copyright holder(s) and author(s).
53706f2543Smrg */
54706f2543Smrg
55706f2543Smrg
56706f2543Smrg/* View/edit this file with tab stops set to 4 */
57706f2543Smrg
58706f2543Smrg#ifdef HAVE_XORG_CONFIG_H
59706f2543Smrg#include <xorg-config.h>
60706f2543Smrg#endif
61706f2543Smrg
62706f2543Smrg#include "os.h"
63706f2543Smrg#include "xf86Parser.h"
64706f2543Smrg#include "xf86tokens.h"
65706f2543Smrg#include "Configint.h"
66706f2543Smrg
67706f2543Smrgextern LexRec val;
68706f2543Smrg
69706f2543Smrgstatic
70706f2543Smrgxf86ConfigSymTabRec InputTab[] =
71706f2543Smrg{
72706f2543Smrg	{ENDSECTION, "endsection"},
73706f2543Smrg	{IDENTIFIER, "identifier"},
74706f2543Smrg	{OPTION, "option"},
75706f2543Smrg	{DRIVER, "driver"},
76706f2543Smrg	{-1, ""},
77706f2543Smrg};
78706f2543Smrg
79706f2543Smrg#define CLEANUP xf86freeInputList
80706f2543Smrg
81706f2543SmrgXF86ConfInputPtr
82706f2543Smrgxf86parseInputSection (void)
83706f2543Smrg{
84706f2543Smrg	int has_ident = FALSE;
85706f2543Smrg	int token;
86706f2543Smrg	parsePrologue (XF86ConfInputPtr, XF86ConfInputRec)
87706f2543Smrg
88706f2543Smrg	while ((token = xf86getToken (InputTab)) != ENDSECTION)
89706f2543Smrg	{
90706f2543Smrg		switch (token)
91706f2543Smrg		{
92706f2543Smrg		case COMMENT:
93706f2543Smrg			ptr->inp_comment = xf86addComment(ptr->inp_comment, val.str);
94706f2543Smrg			break;
95706f2543Smrg		case IDENTIFIER:
96706f2543Smrg			if (xf86getSubToken (&(ptr->inp_comment)) != STRING)
97706f2543Smrg				Error (QUOTE_MSG, "Identifier");
98706f2543Smrg			if (has_ident == TRUE)
99706f2543Smrg				Error (MULTIPLE_MSG, "Identifier");
100706f2543Smrg			ptr->inp_identifier = val.str;
101706f2543Smrg			has_ident = TRUE;
102706f2543Smrg			break;
103706f2543Smrg		case DRIVER:
104706f2543Smrg			if (xf86getSubToken (&(ptr->inp_comment)) != STRING)
105706f2543Smrg				Error (QUOTE_MSG, "Driver");
106706f2543Smrg                        if (strcmp(val.str, "keyboard") == 0) {
107706f2543Smrg                            ptr->inp_driver = strdup("kbd");
108706f2543Smrg                            free(val.str);
109706f2543Smrg                        }
110706f2543Smrg                        else
111706f2543Smrg			    ptr->inp_driver = val.str;
112706f2543Smrg			break;
113706f2543Smrg		case OPTION:
114706f2543Smrg			ptr->inp_option_lst = xf86parseOption(ptr->inp_option_lst);
115706f2543Smrg			break;
116706f2543Smrg		case EOF_TOKEN:
117706f2543Smrg			Error (UNEXPECTED_EOF_MSG, NULL);
118706f2543Smrg			break;
119706f2543Smrg		default:
120706f2543Smrg			Error (INVALID_KEYWORD_MSG, xf86tokenString ());
121706f2543Smrg			break;
122706f2543Smrg		}
123706f2543Smrg	}
124706f2543Smrg
125706f2543Smrg	if (!has_ident)
126706f2543Smrg		Error (NO_IDENT_MSG, NULL);
127706f2543Smrg
128706f2543Smrg#ifdef DEBUG
129706f2543Smrg	printf ("InputDevice section parsed\n");
130706f2543Smrg#endif
131706f2543Smrg
132706f2543Smrg	return ptr;
133706f2543Smrg}
134706f2543Smrg
135706f2543Smrg#undef CLEANUP
136706f2543Smrg
137706f2543Smrgvoid
138706f2543Smrgxf86printInputSection (FILE * cf, XF86ConfInputPtr ptr)
139706f2543Smrg{
140706f2543Smrg	while (ptr)
141706f2543Smrg	{
142706f2543Smrg		fprintf (cf, "Section \"InputDevice\"\n");
143706f2543Smrg		if (ptr->inp_comment)
144706f2543Smrg			fprintf (cf, "%s", ptr->inp_comment);
145706f2543Smrg		if (ptr->inp_identifier)
146706f2543Smrg			fprintf (cf, "\tIdentifier  \"%s\"\n", ptr->inp_identifier);
147706f2543Smrg		if (ptr->inp_driver)
148706f2543Smrg			fprintf (cf, "\tDriver      \"%s\"\n", ptr->inp_driver);
149706f2543Smrg		xf86printOptionList(cf, ptr->inp_option_lst, 1);
150706f2543Smrg		fprintf (cf, "EndSection\n\n");
151706f2543Smrg		ptr = ptr->list.next;
152706f2543Smrg	}
153706f2543Smrg}
154706f2543Smrg
155706f2543Smrgvoid
156706f2543Smrgxf86freeInputList (XF86ConfInputPtr ptr)
157706f2543Smrg{
158706f2543Smrg	XF86ConfInputPtr prev;
159706f2543Smrg
160706f2543Smrg	while (ptr)
161706f2543Smrg	{
162706f2543Smrg		TestFree (ptr->inp_identifier);
163706f2543Smrg		TestFree (ptr->inp_driver);
164706f2543Smrg		TestFree (ptr->inp_comment);
165706f2543Smrg		xf86optionListFree (ptr->inp_option_lst);
166706f2543Smrg
167706f2543Smrg		prev = ptr;
168706f2543Smrg		ptr = ptr->list.next;
169706f2543Smrg		free (prev);
170706f2543Smrg	}
171706f2543Smrg}
172706f2543Smrg
173706f2543Smrgint
174706f2543Smrgxf86validateInput (XF86ConfigPtr p)
175706f2543Smrg{
176706f2543Smrg	XF86ConfInputPtr input = p->conf_input_lst;
177706f2543Smrg
178706f2543Smrg	while (input) {
179706f2543Smrg		if (!input->inp_driver) {
180706f2543Smrg			xf86validationError (UNDEFINED_INPUTDRIVER_MSG, input->inp_identifier);
181706f2543Smrg			return FALSE;
182706f2543Smrg		}
183706f2543Smrg		input = input->list.next;
184706f2543Smrg	}
185706f2543Smrg	return TRUE;
186706f2543Smrg}
187706f2543Smrg
188706f2543SmrgXF86ConfInputPtr
189706f2543Smrgxf86findInput (const char *ident, XF86ConfInputPtr p)
190706f2543Smrg{
191706f2543Smrg	while (p)
192706f2543Smrg	{
193706f2543Smrg		if (xf86nameCompare (ident, p->inp_identifier) == 0)
194706f2543Smrg			return p;
195706f2543Smrg
196706f2543Smrg		p = p->list.next;
197706f2543Smrg	}
198706f2543Smrg	return NULL;
199706f2543Smrg}
200706f2543Smrg
201706f2543SmrgXF86ConfInputPtr
202706f2543Smrgxf86findInputByDriver (const char *driver, XF86ConfInputPtr p)
203706f2543Smrg{
204706f2543Smrg	while (p)
205706f2543Smrg	{
206706f2543Smrg		if (xf86nameCompare (driver, p->inp_driver) == 0)
207706f2543Smrg			return p;
208706f2543Smrg
209706f2543Smrg		p = p->list.next;
210706f2543Smrg	}
211706f2543Smrg	return NULL;
212706f2543Smrg}
213706f2543Smrg
214