b2222a7fbb3c0c319000fb0420d22de18b0c5642
[platform/upstream/libxkbcommon.git] / src / xkbcomp / misc.c
1 /************************************************************
2  Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3
4  Permission to use, copy, modify, and distribute this
5  software and its documentation for any purpose and without
6  fee is hereby granted, provided that the above copyright
7  notice appear in all copies and that both that copyright
8  notice and this permission notice appear in supporting
9  documentation, and that the name of Silicon Graphics not be
10  used in advertising or publicity pertaining to distribution
11  of the software without specific prior written permission.
12  Silicon Graphics makes no representation about the suitability
13  of this software for any purpose. It is provided "as is"
14  without any express or implied warranty.
15
16  SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25  ********************************************************/
26
27 #include "xkbcomp.h"
28 #include "xkballoc.h"
29 #include "xkbmisc.h"
30 #include "xkbpath.h"
31 #include "keycodes.h"
32 #include "misc.h"
33 #include "parseutils.h"
34
35 /***====================================================================***/
36
37 /**
38  * Open the file given in the include statement and parse it's content.
39  * If the statement defines a specific map to use, this map is returned in
40  * file_rtrn. Otherwise, the default map is returned.
41  *
42  * @param context The context containing include paths
43  * @param stmt The include statement, specifying the file name to look for.
44  * @param file_type Type of file (XkmKeyNamesIdx, etc.)
45  * @param file_rtrn Returns the key map to be used.
46  * @param merge_rtrn Always returns stmt->merge.
47  *
48  * @return true on success or false otherwise.
49  */
50 bool
51 ProcessIncludeFile(struct xkb_context *context,
52                    IncludeStmt * stmt,
53                    unsigned file_type,
54                    XkbFile ** file_rtrn, unsigned *merge_rtrn)
55 {
56     FILE *file;
57     XkbFile *rtrn, *mapToUse, *next;
58
59     file = XkbFindFileInPath(context, stmt->file, file_type, &stmt->path);
60     if (file == NULL)
61     {
62         ERROR("Can't find file \"%s\" for %s include\n", stmt->file,
63                 XkbDirectoryForInclude(file_type));
64         return false;
65     }
66     /* parse the file */
67     if ((XKBParseFile(file, stmt->file, &rtrn) == 0) || (rtrn == NULL))
68     {
69         ERROR("Error interpreting include file \"%s\"\n", stmt->file);
70         fclose(file);
71         return false;
72     }
73     fclose(file);
74
75     mapToUse = rtrn;
76     if (stmt->map != NULL)
77     {
78         while (mapToUse)
79         {
80             next = (XkbFile *)mapToUse->common.next;
81             mapToUse->common.next = NULL;
82             if (strcmp(mapToUse->name, stmt->map) == 0 &&
83                 mapToUse->type == file_type)
84             {
85                     FreeXKBFile(next);
86                     break;
87             }
88             else
89             {
90                 FreeXKBFile(mapToUse);
91             }
92             mapToUse = next;
93         }
94         if (!mapToUse)
95         {
96             ERROR("No %s named \"%s\" in the include file \"%s\"\n",
97                    XkbcConfigText(file_type), stmt->map, stmt->file);
98             return false;
99         }
100     }
101     else if ((rtrn->common.next != NULL) && (warningLevel > 5))
102     {
103         WARN("No map in include statement, but \"%s\" contains several\n",
104               stmt->file);
105         ACTION("Using first defined map, \"%s\"\n", rtrn->name);
106     }
107     if (mapToUse->type != file_type)
108     {
109         ERROR("Include file wrong type (expected %s, got %s)\n",
110                XkbcConfigText(file_type), XkbcConfigText(mapToUse->type));
111         ACTION("Include file \"%s\" ignored\n", stmt->file);
112         return false;
113     }
114     /* FIXME: we have to check recursive includes here (or somewhere) */
115
116     *file_rtrn = mapToUse;
117     *merge_rtrn = stmt->merge;
118     return true;
119 }
120
121 /***====================================================================***/
122
123 int
124 ReportNotArray(const char *type, const char *field, const char *name)
125 {
126     ERROR("The %s %s field is not an array\n", type, field);
127     ACTION("Ignoring illegal assignment in %s\n", name);
128     return false;
129 }
130
131 int
132 ReportShouldBeArray(const char *type, const char *field, const char *name)
133 {
134     ERROR("Missing subscript for %s %s\n", type, field);
135     ACTION("Ignoring illegal assignment in %s\n", name);
136     return false;
137 }
138
139 int
140 ReportBadType(const char *type, const char *field,
141               const char *name, const char *wanted)
142 {
143     ERROR("The %s %s field must be a %s\n", type, field, wanted);
144     ACTION("Ignoring illegal assignment in %s\n", name);
145     return false;
146 }
147
148 int
149 ReportBadField(const char *type, const char *field, const char *name)
150 {
151     ERROR("Unknown %s field %s in %s\n", type, field, name);
152     ACTION("Ignoring assignment to unknown field in %s\n", name);
153     return false;
154 }
155
156 /***====================================================================***/
157
158 bool
159 UseNewField(unsigned field,
160             CommonInfo * oldDefs, CommonInfo * newDefs, unsigned *pCollide)
161 {
162     bool useNew;
163
164     useNew = false;
165     if (oldDefs->defined & field)
166     {
167         if (newDefs->defined & field)
168         {
169             if (((oldDefs->fileID == newDefs->fileID)
170                  && (warningLevel > 0)) || (warningLevel > 9))
171             {
172                 *pCollide |= field;
173             }
174             if (newDefs->merge != MergeAugment)
175                 useNew = true;
176         }
177     }
178     else if (newDefs->defined & field)
179         useNew = true;
180     return useNew;
181 }
182
183 char *
184 ClearCommonInfo(CommonInfo * cmn)
185 {
186     if (cmn != NULL)
187     {
188         CommonInfo *this, *next;
189         for (this = cmn; this != NULL; this = next)
190         {
191             next = this->next;
192             free(this);
193         }
194     }
195     return NULL;
196 }
197
198 char *
199 AddCommonInfo(CommonInfo * old, CommonInfo * new)
200 {
201     CommonInfo *first;
202
203     first = old;
204     while (old && old->next)
205     {
206         old = old->next;
207     }
208     new->next = NULL;
209     if (old)
210     {
211         old->next = new;
212         return (char *) first;
213     }
214     return (char *) new;
215 }
216
217 /***====================================================================***/
218
219 /**
220  * Find the key with the given name and return its keycode in kc_rtrn.
221  *
222  * @param name The 4-letter name of the key as a long.
223  * @param kc_rtrn Set to the keycode if the key was found, otherwise 0.
224  * @param use_aliases true if the key aliases should be searched too.
225  * @param create If true and the key is not found, it is added to the
226  *        xkb->names at the first free keycode.
227  * @param start_from Keycode to start searching from.
228  *
229  * @return true if found, false otherwise.
230  */
231 bool
232 FindNamedKey(struct xkb_keymap * xkb,
233              unsigned long name,
234              xkb_keycode_t *kc_rtrn,
235              bool use_aliases, bool create, xkb_keycode_t start_from)
236 {
237     unsigned n;
238
239     if (start_from < xkb->min_key_code)
240     {
241         start_from = xkb->min_key_code;
242     }
243     else if (start_from > xkb->max_key_code)
244     {
245         return false;
246     }
247
248     *kc_rtrn = 0;               /* some callers rely on this */
249     if (xkb && xkb->names && xkb->names->keys)
250     {
251         for (n = start_from; n <= xkb->max_key_code; n++)
252         {
253             unsigned long tmp;
254             tmp = KeyNameToLong(xkb->names->keys[n].name);
255             if (tmp == name)
256             {
257                 *kc_rtrn = n;
258                 return true;
259             }
260         }
261         if (use_aliases)
262         {
263             unsigned long new_name;
264             if (FindKeyNameForAlias(xkb, name, &new_name))
265                 return FindNamedKey(xkb, new_name, kc_rtrn, false, create, 0);
266         }
267     }
268     if (create)
269     {
270         if ((!xkb->names) || (!xkb->names->keys))
271         {
272             if (XkbcAllocNames(xkb, XkbKeyNamesMask, 0) != Success)
273             {
274                 if (warningLevel > 0)
275                 {
276                     WARN("Couldn't allocate key names in FindNamedKey\n");
277                     ACTION("Key \"%s\" not automatically created\n",
278                             longText(name));
279                 }
280                 return false;
281             }
282         }
283         /* Find first unused keycode and store our key here */
284         for (n = xkb->min_key_code; n <= xkb->max_key_code; n++)
285         {
286             if (xkb->names->keys[n].name[0] == '\0')
287             {
288                 char buf[XkbKeyNameLength + 1];
289                 LongToKeyName(name, buf);
290                 memcpy(xkb->names->keys[n].name, buf, XkbKeyNameLength);
291                 *kc_rtrn = n;
292                 return true;
293             }
294         }
295     }
296     return false;
297 }
298
299 bool
300 FindKeyNameForAlias(struct xkb_keymap * xkb, unsigned long lname,
301                     unsigned long *real_name)
302 {
303     unsigned int i;
304     char name[XkbKeyNameLength + 1];
305
306     if (xkb && xkb->names && xkb->names->key_aliases)
307     {
308         struct xkb_key_alias * a;
309         a = xkb->names->key_aliases;
310         LongToKeyName(lname, name);
311         name[XkbKeyNameLength] = '\0';
312         for (i = 0; i < xkb->names->num_key_aliases; i++, a++)
313         {
314             if (strncmp(name, a->alias, XkbKeyNameLength) == 0)
315             {
316                 *real_name = KeyNameToLong(a->real);
317                 return true;
318             }
319         }
320     }
321     return false;
322 }