rules: reformat ApplyRule
[profile/ivi/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 #include "xkb-priv.h"
28
29 static char *
30 XkbcCanonicaliseComponent(char *name, const char *old)
31 {
32     char *tmp;
33     int i;
34
35     if (!name)
36         return NULL;
37
38     /* Treachery. */
39     if (old && strchr(old, '%'))
40         return NULL;
41
42     if (name[0] == '+' || name[0] == '|') {
43         if (old) {
44             tmp = malloc(strlen(name) + strlen(old) + 1);
45             if (!tmp)
46                 return NULL;
47             sprintf(tmp, "%s%s", old, name);
48             free(name);
49             name = tmp;
50         }
51         else {
52             memmove(name, &name[1], strlen(&name[1]) + 1);
53         }
54     }
55
56     for (i = 0; name[i]; i++) {
57         if (name[i] == '%') {
58             if (old) {
59                 tmp = malloc(strlen(name) + strlen(old));
60                 if (!tmp)
61                     return NULL;
62                 strncpy(tmp, name, i);
63                 strcat(tmp + i, old);
64                 strcat(tmp + i + strlen(old), &name[i + 1]);
65                 free(name);
66                 name = tmp;
67                 i--;
68             }
69             else {
70                 memmove(&name[i - 1], &name[i + 1], strlen(&name[i + 1]) + 1);
71                 i -= 2;
72             }
73         }
74     }
75
76     return name;
77 }
78
79 _X_EXPORT void
80 xkb_canonicalise_components(struct xkb_component_names * names,
81                             const struct xkb_component_names * old)
82 {
83     names->keycodes = XkbcCanonicaliseComponent(names->keycodes,
84                                                 old ? old->keycodes : NULL);
85     names->compat = XkbcCanonicaliseComponent(names->compat,
86                                               old ? old->compat : NULL);
87     names->symbols = XkbcCanonicaliseComponent(names->symbols,
88                                                old ? old->symbols : NULL);
89     names->types = XkbcCanonicaliseComponent(names->types,
90                                              old ? old->types : NULL);
91 }
92
93 static bool
94 VirtualModsToReal(struct xkb_keymap *keymap, unsigned virtual_mask,
95                   unsigned *mask_rtrn)
96 {
97     int i, bit;
98     unsigned mask;
99
100     if (!keymap)
101         return false;
102     if (virtual_mask == 0) {
103         *mask_rtrn = 0;
104         return true;
105     }
106     if (!keymap->server)
107         return false;
108
109     for (i = mask = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
110         if (virtual_mask & bit)
111             mask |= keymap->server->vmods[i];
112     }
113
114     *mask_rtrn = mask;
115     return true;
116 }
117
118 bool
119 XkbcComputeEffectiveMap(struct xkb_keymap *keymap, struct xkb_key_type *type,
120                         unsigned char *map_rtrn)
121 {
122     int i;
123     unsigned tmp;
124     struct xkb_kt_map_entry * entry = NULL;
125
126     if (!keymap || !type || !keymap->server)
127         return false;
128
129     if (type->mods.vmods != 0) {
130         if (!VirtualModsToReal(keymap, type->mods.vmods, &tmp))
131             return false;
132
133         type->mods.mask = tmp | type->mods.real_mods;
134         entry = type->map;
135         for (i = 0; i < type->map_count; i++, entry++) {
136             tmp = 0;
137             if (entry->mods.vmods != 0) {
138                 if (!VirtualModsToReal(keymap, entry->mods.vmods, &tmp))
139                     return false;
140                 if (tmp == 0) {
141                     entry->active = false;
142                     continue;
143                 }
144             }
145             entry->active = true;
146             entry->mods.mask = (entry->mods.real_mods | tmp) & type->mods.mask;
147         }
148     }
149     else
150         type->mods.mask = type->mods.real_mods;
151
152     if (map_rtrn) {
153         memset(map_rtrn, 0, type->mods.mask + 1);
154         if (entry && entry->active)
155             for (i = 0; i < type->map_count; i++)
156                 map_rtrn[type->map[i].mods.mask] = type->map[i].level;
157     }
158
159     return true;
160 }