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