File: config.c

package info (click to toggle)
keytouch 2.2.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,212 kB
  • ctags: 1,399
  • sloc: ansic: 9,195; sh: 3,630; makefile: 399
file content (289 lines) | stat: -rw-r--r-- 9,761 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*---------------------------------------------------------------------------------
Name               : config.c
Author             : Marvin Raaijmakers
Description        : Reads and writes the configuration files
Date of last change: 19-Mar-2006
History            : 19-Mar-2006 get_current_keyboard() first checks if
                                 current_keyboard.xml exists
                     26-Jan-2006 Modified read_keyboard_file() so that a keyboard
                                 file without a scancode element is still valid
                     11-Dec-2005 read_key_settings() now skips acpi-hotkeys

    Copyright (C) 2005-2006 Marvin Raaijmakers

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    You can contact me at: marvinr(at)users(dot)sf(dot)net
    (replace (at) by @ and (dot) by .)
-----------------------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mxml.h>

#include <string_to_keycode.h>
#include <keytouch-init.h> 

static KTKeySettings *read_key_settings (	XmlContent		*keylist_element,
						int			*index,
						XmlElementType		*key_type,
						XmlElementType		*scancode_type,
						XmlElementType		*keycode_type,
						XmlAttributeName	*key_type_attrname );


KTKeySettings
*read_key_settings (	XmlContent		*keylist_element,
			int			*index,
			XmlElementType		*key_type,
			XmlElementType		*scancode_type,
			XmlElementType		*keycode_type,
			XmlAttributeName	*key_type_attrname )
/*
Input:
	keylist_element		- The element to read from.
	index			- Read the index'th key element
	key_type		- The XmlElementType for the key element
	scancode_type		- The XmlElementType for the scancode element
	keycode_type		- The XmlElementType for the keycode element
	key_type_attrname	- The XmlAttributeName for the key-type attribute
				  name
Output:
	index			- If the value of "key-type" attribute of the
				  *index'th element is "acpi-hotkey". The integer
				  pointed to by index will be the first key element
				  not of key-type "acpi-hotkey" after the *index'th
				  key element.
Returns:
	A pointer to the new KTKeySettings if it was read, otherwise NULL.
Desciption:
	This function reads the key_name and keycode member from the *index'th key
	element that is a child of keylist_element.
*/
{
	XmlContent	*key_element,
			*scancode_element,
			*keycode_element;
	char		*keycode_string,
			*scancode_string,
			*end_pointer,
			*key_type_string;
	KTKeySettings	*key_settings;
	
	(*index)--;
	do {
		(*index)++;
		key_element = XmlGetElementOfType(XmlElementContentList(keylist_element), key_type, *index);
		if (key_element == NULL)
		{
			return (NULL);
		}
		key_type_string = XmlGetAttributeValue(key_type_attrname, key_element);
	} while (key_type_string != NULL && strcmp(key_type_string, "acpi-hotkey") == EQUAL);
	
	if ((scancode_element =
	       XmlGetElementOfType(XmlElementContentList(key_element), scancode_type, 0)) == NULL ||
	    (keycode_element =
	       XmlGetElementOfType(XmlElementContentList(key_element), keycode_type, 0)) == NULL)
	{
		KTError ("The keyboard file contains an incomplete key element.", "");
		exit (EXIT_FAILURE);
	}
	key_settings = keytouch_malloc(sizeof(KTKeySettings));
	/* Read the scancode */
	scancode_string = XmlGetElementString(scancode_element, "");
	/* Convert the string of the scancode to a integer */
	key_settings->scancode = strtol(scancode_string, &end_pointer, 0);
	/* If the scancode string is not a vallid scancode */
	if (*end_pointer != '\0')
	{
		KTError ("%s is an invalid scancode", scancode_string);
		exit (EXIT_FAILURE);
	}
	XmlFree (scancode_string);
	/* Read the string of the keycode */
	keycode_string = XmlGetElementString(keycode_element, "");
	/* Convert the keycode */
	key_settings->keycode = string_to_kernel_keycode(keycode_string);
	/* If the string is not a string for a keycode */
	if (key_settings->keycode == 0)
	{
		KTError ("The keyboard file contains an invalid keycode.", "");
		exit (EXIT_FAILURE);
	}
	XmlFree (keycode_string);
	
	key_settings->next = NULL;
	
	return (key_settings);
}


void
read_keyboard_file (	KTKeySettingsList	*key_list,
			KTKeyboardName		*keyboard_name  )
/*
Input:
	keyboard_name	- The name of the keyboard to read the configuration from.
Output:
	key_list	- List containing information for every key.
Returns:
	-
Desciption:
	This function reads the keyboard file of the keyboard named keyboard_name,
	which means that it for every special function key an entry in key_list and
	fills in the following elements:
	- keyname
	- key_code
*/
{
	XmlDocument		*document;
	XmlContent		*keyboard_element,
				*keylist_element;
	XmlElementType		*keyboard_type,
				*keylist_type,
				*key_type,
				*scancode_type,
				*keycode_type;
	XmlAttributeName	*key_type_attrname;
	KTKeySettings		*key_settings;
	char			*file_name;
	int			count;
	
	file_name = (char *)keytouch_malloc(	strlen(KEYBOARD_FILES_DIR "/.")+
						strlen(keyboard_name->manufacturer)+
						strlen(keyboard_name->model)+1 );
	strcpy (file_name, KEYBOARD_FILES_DIR "/");
	strcat (file_name, keyboard_name->model);
	strcat (file_name, ".");
	strcat (file_name, keyboard_name->manufacturer);
	
	document = XmlParseDocument(file_name);
	if (document == NULL)
	{
		KTError ("'%s' is an invalid keyboard file.", file_name);
		exit (EXIT_FAILURE);
	}
	
	keyboard_type = XmlGetElementType("keyboard", document, FALSE);
	if (keyboard_type == NULL ||
	    (keyboard_element = XmlGetElementOfType(XmlDocumentContentList(document), keyboard_type, 0)) == NULL)
	{
		KTError ("The keyboard file '%s' does not contain a keyboard element.", file_name);
		exit (EXIT_FAILURE);
	}
	keylist_type = XmlGetElementType("key-list", document, FALSE);
	if (keylist_type == NULL ||
	    (keylist_element =
	       XmlGetElementOfType(XmlElementContentList(keyboard_element), keylist_type, 0)) == NULL)
	{
		KTError ("The keyboard file '%s' does not contain a key-list element.", file_name);
		exit (EXIT_FAILURE);
	}
	
	if ((keycode_type = XmlGetElementType("keycode", document, FALSE)) == NULL ||
	    (key_type = XmlGetElementType("key", document, FALSE)) == NULL)
	{
		KTError ("The keyboard file '%s' is invalid.", file_name);
		exit (EXIT_FAILURE);
	}
	scancode_type = XmlGetElementType("scancode", document, TRUE);
	key_type_attrname = XmlGetAttributeName ("key-type", document, TRUE);
	
	count = 0;
	/* Read the first key of the key settings list */
	key_list->head = key_list->tail = read_key_settings(keylist_element, &count, key_type,
	                                                    scancode_type, keycode_type,
	                                                    key_type_attrname);
	key_settings = key_list->head;
	/* Read the rest of the key settings list */
	for (count++; key_settings != NULL; count++)
	{
		key_settings = read_key_settings(keylist_element, &count, key_type,
		                                 scancode_type, keycode_type,
		                                 key_type_attrname);
		key_list->tail->next = key_settings;
		if (key_settings != NULL)
		{
			key_list->tail = key_settings;
		}
	}
	
	free (file_name);
	XmlFreeDocument (document);
}


void
get_current_keyboard (KTKeyboardName *keyboard_name)
/*
Input:
	-
Output:
	keyboard_name	- The readen keyboard name
Returns:
	-
Desciption:
	This function reads the name of the current keyboard from
	CURRENT_KEYBOARD_FILE. If CURRENT_KEYBOARD_FILE this function will exit the
	program quietly.
*/
{
	XmlDocument	*document;
	XmlElementType	*current_keyboard_type,
			*model_type,
			*manufacturer_type;
	XmlContent	*current_keyboard_element,
			*model_element,
			*manufacturer_element;
	
	/* If current_keyboard.xml does not exist */
	if (access( CURRENT_KEYBOARD_FILE, F_OK ) == -1)
	{
		exit (EXIT_SUCCESS);
	}
	document = XmlParseDocument(CURRENT_KEYBOARD_FILE);
	if (document == NULL)
	{
		KTError ("'%s' is invalid", CURRENT_KEYBOARD_FILE);
		exit (EXIT_FAILURE);
	}
	if ((current_keyboard_type = XmlGetElementType("current-keyboard", document, FALSE)) == NULL ||
	    (model_type = XmlGetElementType("model", document, FALSE)) == NULL ||
	    (manufacturer_type = XmlGetElementType("manufacturer", document, FALSE)) == NULL)
	{
		KTError ("'%s' is invalid", CURRENT_KEYBOARD_FILE);
		exit (EXIT_FAILURE);
	}
	if ((current_keyboard_element =
	      XmlGetElementOfType(XmlDocumentContentList(document), current_keyboard_type, 0)) == NULL)
	{
		KTError ("'%s' is invalid because it has no current-keyboard "
		         "element as the root element", CURRENT_KEYBOARD_FILE);
		exit (EXIT_FAILURE);
	}
	if ((model_element =
	      XmlGetElementOfType(XmlElementContentList(current_keyboard_element), model_type, 0)) == NULL ||
	    (manufacturer_element = 
	      XmlGetElementOfType(XmlElementContentList(current_keyboard_element), manufacturer_type, 0)) == NULL)
	{
		KTError ("'%s' is invalid", CURRENT_KEYBOARD_FILE);
		exit (EXIT_FAILURE);
	}
	keyboard_name->model = XmlGetElementString(model_element, "");
	keyboard_name->manufacturer = XmlGetElementString(manufacturer_element, "");
	
	XmlFreeDocument (document);
}