keymap: be more careful in xkb_keymap_unref
[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 static struct xkb_keymap *
57 xkb_keymap_new(struct xkb_context *ctx,
58                enum xkb_keymap_format format,
59                enum xkb_keymap_compile_flags flags)
60 {
61     struct xkb_keymap *keymap;
62
63     keymap = calloc(1, sizeof(*keymap));
64     if (!keymap)
65         return NULL;
66
67     keymap->refcnt = 1;
68     keymap->ctx = xkb_context_ref(ctx);
69
70     keymap->format = format;
71     keymap->flags = flags;
72
73     return keymap;
74 }
75
76 XKB_EXPORT struct xkb_keymap *
77 xkb_keymap_ref(struct xkb_keymap *keymap)
78 {
79     keymap->refcnt++;
80     return keymap;
81 }
82
83 XKB_EXPORT void
84 xkb_keymap_unref(struct xkb_keymap *keymap)
85 {
86     unsigned int i, j;
87     struct xkb_key *key;
88
89     if (!keymap || --keymap->refcnt > 0)
90         return;
91
92     if (keymap->keys) {
93         xkb_foreach_key(key, keymap) {
94             if (key->groups) {
95                 for (i = 0; i < key->num_groups; i++) {
96                     if (key->groups[i].levels) {
97                         for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
98                             if (key->groups[i].levels[j].num_syms > 1)
99                                 free(key->groups[i].levels[j].u.syms);
100                         free(key->groups[i].levels);
101                     }
102                 }
103                 free(key->groups);
104             }
105         }
106         free(keymap->keys);
107     }
108     if (keymap->types) {
109         for (i = 0; i < keymap->num_types; i++) {
110             free(keymap->types[i].entries);
111             free(keymap->types[i].level_names);
112         }
113         free(keymap->types);
114     }
115     free(keymap->sym_interprets);
116     free(keymap->key_aliases);
117     free(keymap->group_names);
118     darray_free(keymap->mods);
119     darray_free(keymap->leds);
120     free(keymap->keycodes_section_name);
121     free(keymap->symbols_section_name);
122     free(keymap->types_section_name);
123     free(keymap->compat_section_name);
124     xkb_context_unref(keymap->ctx);
125     free(keymap);
126 }
127
128 static const struct xkb_keymap_format_ops *
129 get_keymap_format_ops(enum xkb_keymap_format format)
130 {
131     static const struct xkb_keymap_format_ops *keymap_format_ops[] = {
132         [XKB_KEYMAP_FORMAT_TEXT_V1] = &text_v1_keymap_format_ops,
133     };
134
135     if ((int) format < 0 || (int) format >= ARRAY_SIZE(keymap_format_ops))
136         return NULL;
137
138     return keymap_format_ops[format];
139 }
140
141 XKB_EXPORT struct xkb_keymap *
142 xkb_keymap_new_from_names(struct xkb_context *ctx,
143                           const struct xkb_rule_names *rmlvo_in,
144                           enum xkb_keymap_compile_flags flags)
145 {
146     struct xkb_keymap *keymap;
147     struct xkb_rule_names rmlvo;
148     const enum xkb_keymap_format format = XKB_KEYMAP_FORMAT_TEXT_V1;
149     const struct xkb_keymap_format_ops *ops;
150
151     ops = get_keymap_format_ops(format);
152     if (!ops || !ops->keymap_new_from_names) {
153         log_err_func(ctx, "unsupported keymap format: %d\n", format);
154         return NULL;
155     }
156
157     if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
158         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
159         return NULL;
160     }
161
162     if (rmlvo_in)
163         rmlvo = *rmlvo_in;
164     else
165         memset(&rmlvo, 0, sizeof(rmlvo));
166
167     if (isempty(rmlvo.rules))
168         rmlvo.rules = xkb_context_get_default_rules(ctx);
169     if (isempty(rmlvo.model))
170         rmlvo.model = xkb_context_get_default_model(ctx);
171     /* Layout and variant are tied together, so don't try to use one from
172      * the caller and one from the environment. */
173     if (isempty(rmlvo.layout)) {
174         rmlvo.layout = xkb_context_get_default_layout(ctx);
175         rmlvo.variant = xkb_context_get_default_variant(ctx);
176     }
177     /* Options can be empty, so respect that if passed in. */
178     if (rmlvo.options == NULL)
179         rmlvo.options = xkb_context_get_default_options(ctx);
180
181     keymap = xkb_keymap_new(ctx, format, flags);
182     if (!keymap)
183         return NULL;
184
185     if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
186         xkb_keymap_unref(keymap);
187         return NULL;
188     }
189
190     return keymap;
191 }
192
193 XKB_EXPORT struct xkb_keymap *
194 xkb_keymap_new_from_string(struct xkb_context *ctx,
195                            const char *string,
196                            enum xkb_keymap_format format,
197                            enum xkb_keymap_compile_flags flags)
198 {
199     return xkb_keymap_new_from_buffer(ctx, string, SIZE_MAX, format, flags);
200 }
201
202 XKB_EXPORT struct xkb_keymap *
203 xkb_keymap_new_from_buffer(struct xkb_context *ctx,
204                            const char *buffer, size_t length,
205                            enum xkb_keymap_format format,
206                            enum xkb_keymap_compile_flags flags)
207 {
208     struct xkb_keymap *keymap;
209     const struct xkb_keymap_format_ops *ops;
210
211     ops = get_keymap_format_ops(format);
212     if (!ops || !ops->keymap_new_from_string) {
213         log_err_func(ctx, "unsupported keymap format: %d\n", format);
214         return NULL;
215     }
216
217     if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
218         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
219         return NULL;
220     }
221
222     if (!buffer) {
223         log_err_func1(ctx, "no buffer specified\n");
224         return NULL;
225     }
226
227     keymap = xkb_keymap_new(ctx, format, flags);
228     if (!keymap)
229         return NULL;
230
231     if (!ops->keymap_new_from_string(keymap, buffer, length)) {
232         xkb_keymap_unref(keymap);
233         return NULL;
234     }
235
236     return keymap;
237 }
238
239 XKB_EXPORT struct xkb_keymap *
240 xkb_keymap_new_from_file(struct xkb_context *ctx,
241                          FILE *file,
242                          enum xkb_keymap_format format,
243                          enum xkb_keymap_compile_flags flags)
244 {
245     struct xkb_keymap *keymap;
246     const struct xkb_keymap_format_ops *ops;
247
248     ops = get_keymap_format_ops(format);
249     if (!ops || !ops->keymap_new_from_file) {
250         log_err_func(ctx, "unsupported keymap format: %d\n", format);
251         return NULL;
252     }
253
254     if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
255         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
256         return NULL;
257     }
258
259     if (!file) {
260         log_err_func1(ctx, "no file specified\n");
261         return NULL;
262     }
263
264     keymap = xkb_keymap_new(ctx, format, flags);
265     if (!keymap)
266         return NULL;
267
268     if (!ops->keymap_new_from_file(keymap, file)) {
269         xkb_keymap_unref(keymap);
270         return NULL;
271     }
272
273     return keymap;
274 }
275
276 XKB_EXPORT char *
277 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
278                          enum xkb_keymap_format format)
279 {
280     const struct xkb_keymap_format_ops *ops;
281
282     if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
283         format = keymap->format;
284
285     ops = get_keymap_format_ops(format);
286     if (!ops || !ops->keymap_get_as_string) {
287         log_err_func(keymap->ctx, "unsupported keymap format: %d\n", format);
288         return NULL;
289     }
290
291     return ops->keymap_get_as_string(keymap);
292 }
293
294 /**
295  * Returns the total number of modifiers active in the keymap.
296  */
297 XKB_EXPORT xkb_mod_index_t
298 xkb_keymap_num_mods(struct xkb_keymap *keymap)
299 {
300     return darray_size(keymap->mods);
301 }
302
303 /**
304  * Return the name for a given modifier.
305  */
306 XKB_EXPORT const char *
307 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
308 {
309     if (idx >= darray_size(keymap->mods))
310         return NULL;
311
312     return xkb_atom_text(keymap->ctx, darray_item(keymap->mods, idx).name);
313 }
314
315 /**
316  * Returns the index for a named modifier.
317  */
318 XKB_EXPORT xkb_mod_index_t
319 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
320 {
321     xkb_mod_index_t i;
322     xkb_atom_t atom;
323     const struct xkb_mod *mod;
324
325     atom = xkb_atom_lookup(keymap->ctx, name);
326     if (atom == XKB_ATOM_NONE)
327         return XKB_MOD_INVALID;
328
329     darray_enumerate(i, mod, keymap->mods)
330         if (mod->name == atom)
331             return i;
332
333     return XKB_MOD_INVALID;
334 }
335
336 /**
337  * Return the total number of active groups in the keymap.
338  */
339 XKB_EXPORT xkb_layout_index_t
340 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
341 {
342     return keymap->num_groups;
343 }
344
345 /**
346  * Returns the name for a given group.
347  */
348 XKB_EXPORT const char *
349 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
350 {
351     if (idx >= keymap->num_group_names)
352         return NULL;
353
354     return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
355 }
356
357 /**
358  * Returns the index for a named layout.
359  */
360 XKB_EXPORT xkb_layout_index_t
361 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
362 {
363     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
364     xkb_layout_index_t i;
365
366     if (atom == XKB_ATOM_NONE)
367         return XKB_LAYOUT_INVALID;
368
369     for (i = 0; i < keymap->num_group_names; i++)
370         if (keymap->group_names[i] == atom)
371             return i;
372
373     return XKB_LAYOUT_INVALID;
374 }
375
376 /**
377  * Returns the number of layouts active for a particular key.
378  */
379 XKB_EXPORT xkb_layout_index_t
380 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
381 {
382     const struct xkb_key *key = XkbKey(keymap, kc);
383
384     if (!key)
385         return 0;
386
387     return key->num_groups;
388 }
389
390 /**
391  * Returns the number of levels active for a particular key and layout.
392  */
393 XKB_EXPORT xkb_level_index_t
394 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
395                               xkb_layout_index_t layout)
396 {
397     const struct xkb_key *key = XkbKey(keymap, kc);
398
399     if (!key)
400         return 0;
401
402     layout = wrap_group_into_range(layout, key->num_groups,
403                                    key->out_of_range_group_action,
404                                    key->out_of_range_group_number);
405     if (layout == XKB_LAYOUT_INVALID)
406         return 0;
407
408     return XkbKeyGroupWidth(key, layout);
409 }
410
411 /**
412  * Return the total number of LEDs in the keymap.
413  */
414 XKB_EXPORT xkb_led_index_t
415 xkb_keymap_num_leds(struct xkb_keymap *keymap)
416 {
417     return darray_size(keymap->leds);
418 }
419
420 /**
421  * Returns the name for a given LED.
422  */
423 XKB_EXPORT const char *
424 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
425 {
426     if (idx >= darray_size(keymap->leds))
427         return NULL;
428
429     return xkb_atom_text(keymap->ctx, darray_item(keymap->leds, idx).name);
430 }
431
432 /**
433  * Returns the index for a named LED.
434  */
435 XKB_EXPORT xkb_led_index_t
436 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
437 {
438     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
439     xkb_led_index_t i;
440     const struct xkb_led *led;
441
442     if (atom == XKB_ATOM_NONE)
443         return XKB_LED_INVALID;
444
445     darray_enumerate(i, led, keymap->leds)
446         if (led->name == atom)
447             return i;
448
449     return XKB_LED_INVALID;
450 }
451
452 /**
453  * As below, but takes an explicit layout/level rather than state.
454  */
455 XKB_EXPORT int
456 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
457                                  xkb_keycode_t kc,
458                                  xkb_layout_index_t layout,
459                                  xkb_level_index_t level,
460                                  const xkb_keysym_t **syms_out)
461 {
462     const struct xkb_key *key = XkbKey(keymap, kc);
463     int num_syms;
464
465     if (!key)
466         goto err;
467
468     layout = wrap_group_into_range(layout, key->num_groups,
469                                    key->out_of_range_group_action,
470                                    key->out_of_range_group_number);
471     if (layout == XKB_LAYOUT_INVALID)
472         goto err;
473
474     if (level >= XkbKeyGroupWidth(key, layout))
475         goto err;
476
477     num_syms = key->groups[layout].levels[level].num_syms;
478     if (num_syms == 0)
479         goto err;
480
481     if (num_syms == 1)
482         *syms_out = &key->groups[layout].levels[level].u.sym;
483     else
484         *syms_out = key->groups[layout].levels[level].u.syms;
485
486     return num_syms;
487
488 err:
489     *syms_out = NULL;
490     return 0;
491 }
492
493 XKB_EXPORT xkb_keycode_t
494 xkb_keymap_min_keycode(struct xkb_keymap *keymap)
495 {
496     return keymap->min_key_code;
497 }
498
499 XKB_EXPORT xkb_keycode_t
500 xkb_keymap_max_keycode(struct xkb_keymap *keymap)
501 {
502     return keymap->max_key_code;
503 }
504
505 XKB_EXPORT void
506 xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
507                         void *data)
508 {
509     struct xkb_key *key;
510
511     xkb_foreach_key(key, keymap)
512         iter(keymap, key->keycode, data);
513 }
514
515 /**
516  * Simple boolean specifying whether or not the key should repeat.
517  */
518 XKB_EXPORT int
519 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
520 {
521     const struct xkb_key *key = XkbKey(keymap, kc);
522
523     if (!key)
524         return 0;
525
526     return key->repeats;
527 }
528
529 struct xkb_key *
530 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
531 {
532     struct xkb_key *key;
533
534     xkb_foreach_key(key, keymap)
535         if (key->name == name)
536             return key;
537
538     if (use_aliases) {
539         xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
540         if (new_name != XKB_ATOM_NONE)
541             return XkbKeyByName(keymap, new_name, false);
542     }
543
544     return NULL;
545 }
546
547 xkb_atom_t
548 XkbResolveKeyAlias(struct xkb_keymap *keymap, xkb_atom_t name)
549 {
550     for (unsigned i = 0; i < keymap->num_key_aliases; i++)
551         if (keymap->key_aliases[i].alias == name)
552             return keymap->key_aliases[i].real;
553
554     return XKB_ATOM_NONE;
555 }