Move include path from X11/extensions/ to xkbcommon/
[platform/upstream/libxkbcommon.git] / src / xkb.c
1 /************************************************************
2 Copyright (c) 1993 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 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include "xkbmisc.h"
31 #include "xkbcommon/xkbcommon.h"
32 #include "XKBcommonint.h"
33
34 static char *
35 XkbcCanonicaliseComponent(char *name, const char *old)
36 {
37     char *tmp;
38     int i;
39
40     if (!name)
41         return NULL;
42
43     /* Treachery. */
44     if (old && strchr(old, '%'))
45         return NULL;
46
47     if (name[0] == '+' || name[0] == '|') {
48         if (old) {
49             tmp = malloc(strlen(name) + strlen(old) + 1);
50             if (!tmp)
51                 return NULL;
52             sprintf(tmp, "%s%s", old, name);
53             free(name);
54             name = tmp;
55         }
56         else {
57             memmove(name, &name[1], strlen(&name[1]) + 1);
58         }
59     }
60
61     for (i = 0; name[i]; i++) {
62         if (name[i] == '%') {
63             if (old) {
64                 tmp = malloc(strlen(name) + strlen(old));
65                 if (!tmp)
66                     return NULL;
67                 strncpy(tmp, name, i);
68                 strcat(tmp + i, old);
69                 strcat(tmp + i + strlen(old), &name[i + 1]);
70                 free(name);
71                 name = tmp;
72                 i--;
73             }
74             else {
75                 memmove(&name[i - 1], &name[i + 1], strlen(&name[i + 1]) + 1);
76                 i -= 2;
77             }
78         }
79     }
80
81     return name;
82 }
83
84 void
85 xkb_canonicalise_components(struct xkb_component_names * names,
86                            const struct xkb_component_names * old)
87 {
88     names->keycodes = XkbcCanonicaliseComponent(names->keycodes,
89                                                 old ? old->keycodes : NULL);
90     names->compat = XkbcCanonicaliseComponent(names->compat,
91                                               old ? old->compat : NULL);
92     names->geometry = XkbcCanonicaliseComponent(names->geometry,
93                                                 old ? old->geometry : NULL);
94     names->symbols = XkbcCanonicaliseComponent(names->symbols,
95                                                old ? old->symbols : NULL);
96     names->types = XkbcCanonicaliseComponent(names->types,
97                                              old ? old->types : NULL);
98 }
99
100 Bool
101 XkbcComputeEffectiveMap(struct xkb_desc * xkb, struct xkb_key_type * type,
102                         unsigned char *map_rtrn)
103 {
104     int i;
105     unsigned tmp;
106     struct xkb_kt_map_entry * entry = NULL;
107
108     if (!xkb || !type || !xkb->server)
109         return False;
110
111     if (type->mods.vmods != 0) {
112         if (!XkbcVirtualModsToReal(xkb, type->mods.vmods, &tmp))
113             return False;
114
115         type->mods.mask = tmp | type->mods.real_mods;
116         entry = type->map;
117         for (i = 0; i < type->map_count; i++, entry++) {
118             tmp = 0;
119             if (entry->mods.vmods != 0) {
120                 if (!XkbcVirtualModsToReal(xkb, entry->mods.vmods, &tmp))
121                     return False;
122                 if (tmp == 0) {
123                     entry->active = False;
124                     continue;
125                 }
126             }
127             entry->active = True;
128             entry->mods.mask = (entry->mods.real_mods | tmp) & type->mods.mask;
129         }
130     }
131     else
132         type->mods.mask = type->mods.real_mods;
133
134     if (map_rtrn) {
135         bzero(map_rtrn, type->mods.mask + 1);
136         for (i = 0; i < type->map_count; i++) {
137             if (entry->active)
138                 map_rtrn[type->map[i].mods.mask] = type->map[i].level;
139         }
140     }
141
142     return True;
143 }