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