compat: move some unclear code where it belongs
[platform/upstream/libxkbcommon.git] / src / xkbcomp / keymap.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-priv.h"
28 #include "indicators.h"
29
30 /**
31  * Compile the given file and store the output in xkb.
32  * @param file A list of XkbFiles, each denoting one type (e.g.
33  * FILE_TYPE_KEYCODES, etc.)
34  */
35 struct xkb_keymap *
36 CompileKeymap(struct xkb_context *ctx, XkbFile *file)
37 {
38     unsigned have = 0;
39     bool ok;
40     enum xkb_file_type mainType;
41     const char *mainName;
42     struct xkb_keymap *keymap = XkbcAllocKeyboard(ctx);
43     struct {
44         XkbFile *keycodes;
45         XkbFile *types;
46         XkbFile *compat;
47         XkbFile *symbols;
48     } sections = { NULL };
49
50     if (!keymap)
51         return NULL;
52
53     mainType = file->type;
54     mainName = file->name ? file->name : "(unnamed)";
55
56     /*
57      * Other aggregate file types are converted to FILE_TYPE_KEYMAP
58      * in the parser.
59      */
60     if (mainType != FILE_TYPE_KEYMAP) {
61         ERROR("Cannot compile a %s file alone into a keymap\n",
62               XkbcFileTypeText(mainType));
63         goto err;
64     }
65
66     /* Check for duplicate entries in the input file */
67     for (file = (XkbFile *) file->defs; file; file = (XkbFile *) file->common.next)
68     {
69         if (have & file->type) {
70             ERROR("More than one %s section in a %s file\n",
71                    XkbcFileTypeText(file->type), XkbcFileTypeText(mainType));
72             ACTION("All sections after the first ignored\n");
73             continue;
74         }
75         else if (!(file->type & LEGAL_FILE_TYPES)) {
76             ERROR("Cannot define %s in a %s file\n",
77                    XkbcFileTypeText(file->type), XkbcFileTypeText(mainType));
78             continue;
79         }
80
81         switch (file->type) {
82         case FILE_TYPE_KEYCODES:
83             sections.keycodes = file;
84             break;
85         case FILE_TYPE_TYPES:
86             sections.types = file;
87             break;
88         case FILE_TYPE_SYMBOLS:
89             sections.symbols = file;
90             break;
91         case FILE_TYPE_COMPAT:
92             sections.compat = file;
93             break;
94         case FILE_TYPE_GEOMETRY:
95             continue;
96         default:
97             WSGO("Unknown file type %d\n", file->type);
98             ACTION("Ignored\n");
99             continue;
100         case FILE_TYPE_KEYMAP:
101             WSGO("Illegal %s configuration in a %s file\n",
102                   XkbcFileTypeText(file->type), XkbcFileTypeText(mainType));
103             ACTION("Ignored\n");
104             continue;
105         }
106
107         if (!file->topName || strcmp(file->topName, mainName) != 0) {
108             free(file->topName);
109             file->topName = strdup(mainName);
110         }
111
112         have |= file->type;
113     }
114
115     if (REQUIRED_FILE_TYPES & (~have)) {
116         enum xkb_file_type bit;
117         enum xkb_file_type missing;
118
119         missing = REQUIRED_FILE_TYPES & (~have);
120
121         for (bit = 1; missing != 0; bit <<= 1) {
122             if (missing & bit) {
123                 ERROR("Required section %s missing from keymap\n",
124                       XkbcFileTypeText(bit));
125                 missing &= ~bit;
126             }
127         }
128
129         goto err;
130     }
131
132     /* compile the sections we have in the file one-by-one, or fail. */
133     if (sections.keycodes == NULL ||
134         !CompileKeycodes(sections.keycodes, keymap, MERGE_OVERRIDE))
135     {
136         ERROR("Failed to compile keycodes\n");
137         goto err;
138     }
139     if (sections.types == NULL ||
140         !CompileKeyTypes(sections.types, keymap, MERGE_OVERRIDE))
141     {
142         ERROR("Failed to compile key types\n");
143         goto err;
144     }
145     if (sections.compat == NULL ||
146         !CompileCompatMap(sections.compat, keymap, MERGE_OVERRIDE))
147     {
148         ERROR("Failed to compile compat map\n");
149         goto err;
150     }
151     if (sections.symbols == NULL ||
152         !CompileSymbols(sections.symbols, keymap, MERGE_OVERRIDE))
153     {
154         ERROR("Failed to compile symbols\n");
155         goto err;
156     }
157
158     ok = UpdateModifiersFromCompat(keymap);
159     if (!ok)
160         goto err;
161
162     return keymap;
163
164 err:
165     ACTION("Failed to compile keymap\n");
166     xkb_map_unref(keymap);
167     return NULL;
168 }