context: add xkb_context_sanitize_rule_names()
[platform/upstream/libxkbcommon.git] / src / keymap.c
1 /**
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2012 Ran Benita <ran234@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Author: Daniel Stone <daniel@fooishbar.org>
25  */
26
27 /************************************************************
28  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
29  *
30  * Permission to use, copy, modify, and distribute this
31  * software and its documentation for any purpose and without
32  * fee is hereby granted, provided that the above copyright
33  * notice appear in all copies and that both that copyright
34  * notice and this permission notice appear in supporting
35  * documentation, and that the name of Silicon Graphics not be
36  * used in advertising or publicity pertaining to distribution
37  * of the software without specific prior written permission.
38  * Silicon Graphics makes no representation about the suitability
39  * of this software for any purpose. It is provided "as is"
40  * without any express or implied warranty.
41  *
42  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
43  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
44  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
45  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
46  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
47  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
48  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
49  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
50  *
51  * ********************************************************/
52
53 #include "keymap.h"
54 #include "text.h"
55
56 XKB_EXPORT struct xkb_keymap *
57 xkb_keymap_ref(struct xkb_keymap *keymap)
58 {
59     keymap->refcnt++;
60     return keymap;
61 }
62
63 XKB_EXPORT void
64 xkb_keymap_unref(struct xkb_keymap *keymap)
65 {
66     if (!keymap || --keymap->refcnt > 0)
67         return;
68
69     if (keymap->keys) {
70         struct xkb_key *key;
71         xkb_foreach_key(key, keymap) {
72             if (key->groups) {
73                 for (unsigned i = 0; i < key->num_groups; i++) {
74                     if (key->groups[i].levels) {
75                         for (unsigned j = 0; j < XkbKeyGroupWidth(key, i); j++)
76                             if (key->groups[i].levels[j].num_syms > 1)
77                                 free(key->groups[i].levels[j].u.syms);
78                         free(key->groups[i].levels);
79                     }
80                 }
81                 free(key->groups);
82             }
83         }
84         free(keymap->keys);
85     }
86     if (keymap->types) {
87         for (unsigned i = 0; i < keymap->num_types; i++) {
88             free(keymap->types[i].entries);
89             free(keymap->types[i].level_names);
90         }
91         free(keymap->types);
92     }
93     free(keymap->sym_interprets);
94     free(keymap->key_aliases);
95     free(keymap->group_names);
96     darray_free(keymap->mods);
97     darray_free(keymap->leds);
98     free(keymap->keycodes_section_name);
99     free(keymap->symbols_section_name);
100     free(keymap->types_section_name);
101     free(keymap->compat_section_name);
102     xkb_context_unref(keymap->ctx);
103     free(keymap);
104 }
105
106 static const struct xkb_keymap_format_ops *
107 get_keymap_format_ops(enum xkb_keymap_format format)
108 {
109     static const struct xkb_keymap_format_ops *keymap_format_ops[] = {
110         [XKB_KEYMAP_FORMAT_TEXT_V1] = &text_v1_keymap_format_ops,
111     };
112
113     if ((int) format < 0 || (int) format >= (int) ARRAY_SIZE(keymap_format_ops))
114         return NULL;
115
116     return keymap_format_ops[(int) format];
117 }
118
119 XKB_EXPORT struct xkb_keymap *
120 xkb_keymap_new_from_names(struct xkb_context *ctx,
121                           const struct xkb_rule_names *rmlvo_in,
122                           enum xkb_keymap_compile_flags flags)
123 {
124     struct xkb_keymap *keymap;
125     struct xkb_rule_names rmlvo;
126     const enum xkb_keymap_format format = XKB_KEYMAP_FORMAT_TEXT_V1;
127     const struct xkb_keymap_format_ops *ops;
128
129     ops = get_keymap_format_ops(format);
130     if (!ops || !ops->keymap_new_from_names) {
131         log_err_func(ctx, "unsupported keymap format: %d\n", format);
132         return NULL;
133     }
134
135     if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
136         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
137         return NULL;
138     }
139
140     keymap = xkb_keymap_new(ctx, format, flags);
141     if (!keymap)
142         return NULL;
143
144     if (rmlvo_in)
145         rmlvo = *rmlvo_in;
146     else
147         memset(&rmlvo, 0, sizeof(rmlvo));
148     xkb_context_sanitize_rule_names(ctx, &rmlvo);
149
150     if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
151         xkb_keymap_unref(keymap);
152         return NULL;
153     }
154
155     return keymap;
156 }
157
158 XKB_EXPORT struct xkb_keymap *
159 xkb_keymap_new_from_string(struct xkb_context *ctx,
160                            const char *string,
161                            enum xkb_keymap_format format,
162                            enum xkb_keymap_compile_flags flags)
163 {
164     return xkb_keymap_new_from_buffer(ctx, string, strlen(string),
165                                       format, flags);
166 }
167
168 XKB_EXPORT struct xkb_keymap *
169 xkb_keymap_new_from_buffer(struct xkb_context *ctx,
170                            const char *buffer, size_t length,
171                            enum xkb_keymap_format format,
172                            enum xkb_keymap_compile_flags flags)
173 {
174     struct xkb_keymap *keymap;
175     const struct xkb_keymap_format_ops *ops;
176
177     ops = get_keymap_format_ops(format);
178     if (!ops || !ops->keymap_new_from_string) {
179         log_err_func(ctx, "unsupported keymap format: %d\n", format);
180         return NULL;
181     }
182
183     if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
184         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
185         return NULL;
186     }
187
188     if (!buffer) {
189         log_err_func1(ctx, "no buffer specified\n");
190         return NULL;
191     }
192
193     keymap = xkb_keymap_new(ctx, format, flags);
194     if (!keymap)
195         return NULL;
196
197     if (!ops->keymap_new_from_string(keymap, buffer, length)) {
198         xkb_keymap_unref(keymap);
199         return NULL;
200     }
201
202     return keymap;
203 }
204
205 XKB_EXPORT struct xkb_keymap *
206 xkb_keymap_new_from_file(struct xkb_context *ctx,
207                          FILE *file,
208                          enum xkb_keymap_format format,
209                          enum xkb_keymap_compile_flags flags)
210 {
211     struct xkb_keymap *keymap;
212     const struct xkb_keymap_format_ops *ops;
213
214     ops = get_keymap_format_ops(format);
215     if (!ops || !ops->keymap_new_from_file) {
216         log_err_func(ctx, "unsupported keymap format: %d\n", format);
217         return NULL;
218     }
219
220     if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
221         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
222         return NULL;
223     }
224
225     if (!file) {
226         log_err_func1(ctx, "no file specified\n");
227         return NULL;
228     }
229
230     keymap = xkb_keymap_new(ctx, format, flags);
231     if (!keymap)
232         return NULL;
233
234     if (!ops->keymap_new_from_file(keymap, file)) {
235         xkb_keymap_unref(keymap);
236         return NULL;
237     }
238
239     return keymap;
240 }
241
242 XKB_EXPORT char *
243 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
244                          enum xkb_keymap_format format)
245 {
246     const struct xkb_keymap_format_ops *ops;
247
248     if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
249         format = keymap->format;
250
251     ops = get_keymap_format_ops(format);
252     if (!ops || !ops->keymap_get_as_string) {
253         log_err_func(keymap->ctx, "unsupported keymap format: %d\n", format);
254         return NULL;
255     }
256
257     return ops->keymap_get_as_string(keymap);
258 }
259
260 /**
261  * Returns the total number of modifiers active in the keymap.
262  */
263 XKB_EXPORT xkb_mod_index_t
264 xkb_keymap_num_mods(struct xkb_keymap *keymap)
265 {
266     return darray_size(keymap->mods);
267 }
268
269 /**
270  * Return the name for a given modifier.
271  */
272 XKB_EXPORT const char *
273 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
274 {
275     if (idx >= darray_size(keymap->mods))
276         return NULL;
277
278     return xkb_atom_text(keymap->ctx, darray_item(keymap->mods, idx).name);
279 }
280
281 /**
282  * Returns the index for a named modifier.
283  */
284 XKB_EXPORT xkb_mod_index_t
285 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
286 {
287     xkb_mod_index_t i;
288     xkb_atom_t atom;
289     const struct xkb_mod *mod;
290
291     atom = xkb_atom_lookup(keymap->ctx, name);
292     if (atom == XKB_ATOM_NONE)
293         return XKB_MOD_INVALID;
294
295     darray_enumerate(i, mod, keymap->mods)
296         if (mod->name == atom)
297             return i;
298
299     return XKB_MOD_INVALID;
300 }
301
302 /**
303  * Return the total number of active groups in the keymap.
304  */
305 XKB_EXPORT xkb_layout_index_t
306 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
307 {
308     return keymap->num_groups;
309 }
310
311 /**
312  * Returns the name for a given group.
313  */
314 XKB_EXPORT const char *
315 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
316 {
317     if (idx >= keymap->num_group_names)
318         return NULL;
319
320     return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
321 }
322
323 /**
324  * Returns the index for a named layout.
325  */
326 XKB_EXPORT xkb_layout_index_t
327 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
328 {
329     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
330     xkb_layout_index_t i;
331
332     if (atom == XKB_ATOM_NONE)
333         return XKB_LAYOUT_INVALID;
334
335     for (i = 0; i < keymap->num_group_names; i++)
336         if (keymap->group_names[i] == atom)
337             return i;
338
339     return XKB_LAYOUT_INVALID;
340 }
341
342 /**
343  * Returns the number of layouts active for a particular key.
344  */
345 XKB_EXPORT xkb_layout_index_t
346 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
347 {
348     const struct xkb_key *key = XkbKey(keymap, kc);
349
350     if (!key)
351         return 0;
352
353     return key->num_groups;
354 }
355
356 /**
357  * Returns the number of levels active for a particular key and layout.
358  */
359 XKB_EXPORT xkb_level_index_t
360 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
361                               xkb_layout_index_t layout)
362 {
363     const struct xkb_key *key = XkbKey(keymap, kc);
364
365     if (!key)
366         return 0;
367
368     layout = wrap_group_into_range(layout, key->num_groups,
369                                    key->out_of_range_group_action,
370                                    key->out_of_range_group_number);
371     if (layout == XKB_LAYOUT_INVALID)
372         return 0;
373
374     return XkbKeyGroupWidth(key, layout);
375 }
376
377 /**
378  * Return the total number of LEDs in the keymap.
379  */
380 XKB_EXPORT xkb_led_index_t
381 xkb_keymap_num_leds(struct xkb_keymap *keymap)
382 {
383     return darray_size(keymap->leds);
384 }
385
386 /**
387  * Returns the name for a given LED.
388  */
389 XKB_EXPORT const char *
390 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
391 {
392     if (idx >= darray_size(keymap->leds))
393         return NULL;
394
395     return xkb_atom_text(keymap->ctx, darray_item(keymap->leds, idx).name);
396 }
397
398 /**
399  * Returns the index for a named LED.
400  */
401 XKB_EXPORT xkb_led_index_t
402 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
403 {
404     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
405     xkb_led_index_t i;
406     const struct xkb_led *led;
407
408     if (atom == XKB_ATOM_NONE)
409         return XKB_LED_INVALID;
410
411     darray_enumerate(i, led, keymap->leds)
412         if (led->name == atom)
413             return i;
414
415     return XKB_LED_INVALID;
416 }
417
418 /**
419  * As below, but takes an explicit layout/level rather than state.
420  */
421 XKB_EXPORT int
422 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
423                                  xkb_keycode_t kc,
424                                  xkb_layout_index_t layout,
425                                  xkb_level_index_t level,
426                                  const xkb_keysym_t **syms_out)
427 {
428     const struct xkb_key *key = XkbKey(keymap, kc);
429     int num_syms;
430
431     if (!key)
432         goto err;
433
434     layout = wrap_group_into_range(layout, key->num_groups,
435                                    key->out_of_range_group_action,
436                                    key->out_of_range_group_number);
437     if (layout == XKB_LAYOUT_INVALID)
438         goto err;
439
440     if (level >= XkbKeyGroupWidth(key, layout))
441         goto err;
442
443     num_syms = key->groups[layout].levels[level].num_syms;
444     if (num_syms == 0)
445         goto err;
446
447     if (num_syms == 1)
448         *syms_out = &key->groups[layout].levels[level].u.sym;
449     else
450         *syms_out = key->groups[layout].levels[level].u.syms;
451
452     return num_syms;
453
454 err:
455     *syms_out = NULL;
456     return 0;
457 }
458
459 XKB_EXPORT xkb_keycode_t
460 xkb_keymap_min_keycode(struct xkb_keymap *keymap)
461 {
462     return keymap->min_key_code;
463 }
464
465 XKB_EXPORT xkb_keycode_t
466 xkb_keymap_max_keycode(struct xkb_keymap *keymap)
467 {
468     return keymap->max_key_code;
469 }
470
471 XKB_EXPORT void
472 xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
473                         void *data)
474 {
475     struct xkb_key *key;
476
477     xkb_foreach_key(key, keymap)
478         iter(keymap, key->keycode, data);
479 }
480
481 /**
482  * Simple boolean specifying whether or not the key should repeat.
483  */
484 XKB_EXPORT int
485 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
486 {
487     const struct xkb_key *key = XkbKey(keymap, kc);
488
489     if (!key)
490         return 0;
491
492     return key->repeats;
493 }