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 "config.h"
54
55 #include "keymap.h"
56 #include "text.h"
57
58 XKB_EXPORT struct xkb_keymap *
xkb_keymap_ref(struct xkb_keymap * keymap)59 xkb_keymap_ref(struct xkb_keymap *keymap)
60 {
61 keymap->refcnt++;
62 return keymap;
63 }
64
65 XKB_EXPORT void
xkb_keymap_unref(struct xkb_keymap * keymap)66 xkb_keymap_unref(struct xkb_keymap *keymap)
67 {
68 if (!keymap || --keymap->refcnt > 0)
69 return;
70
71 if (keymap->keys) {
72 struct xkb_key *key;
73 xkb_keys_foreach(key, keymap) {
74 if (key->groups) {
75 for (unsigned i = 0; i < key->num_groups; i++) {
76 if (key->groups[i].levels) {
77 for (unsigned j = 0; j < XkbKeyNumLevels(key, i); j++)
78 if (key->groups[i].levels[j].num_syms > 1)
79 free(key->groups[i].levels[j].u.syms);
80 free(key->groups[i].levels);
81 }
82 }
83 free(key->groups);
84 }
85 }
86 free(keymap->keys);
87 }
88 if (keymap->types) {
89 for (unsigned i = 0; i < keymap->num_types; i++) {
90 free(keymap->types[i].entries);
91 free(keymap->types[i].level_names);
92 }
93 free(keymap->types);
94 }
95 free(keymap->sym_interprets);
96 free(keymap->key_aliases);
97 free(keymap->group_names);
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 *
get_keymap_format_ops(enum xkb_keymap_format format)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 *
xkb_keymap_new_from_names(struct xkb_context * ctx,const struct xkb_rule_names * rmlvo_in,enum xkb_keymap_compile_flags flags)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 *
xkb_keymap_new_from_string(struct xkb_context * ctx,const char * string,enum xkb_keymap_format format,enum xkb_keymap_compile_flags flags)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 *
xkb_keymap_new_from_buffer(struct xkb_context * ctx,const char * buffer,size_t length,enum xkb_keymap_format format,enum xkb_keymap_compile_flags flags)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 *
xkb_keymap_new_from_file(struct xkb_context * ctx,FILE * file,enum xkb_keymap_format format,enum xkb_keymap_compile_flags flags)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 *
xkb_keymap_get_as_string(struct xkb_keymap * keymap,enum xkb_keymap_format format)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
xkb_keymap_num_mods(struct xkb_keymap * keymap)264 xkb_keymap_num_mods(struct xkb_keymap *keymap)
265 {
266 return keymap->mods.num_mods;
267 }
268
269 /**
270 * Return the name for a given modifier.
271 */
272 XKB_EXPORT const char *
xkb_keymap_mod_get_name(struct xkb_keymap * keymap,xkb_mod_index_t idx)273 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
274 {
275 if (idx >= keymap->mods.num_mods)
276 return NULL;
277
278 return xkb_atom_text(keymap->ctx, keymap->mods.mods[idx].name);
279 }
280
281 /**
282 * Returns the index for a named modifier.
283 */
284 XKB_EXPORT xkb_mod_index_t
xkb_keymap_mod_get_index(struct xkb_keymap * keymap,const char * name)285 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
286 {
287 xkb_atom_t atom;
288
289 atom = xkb_atom_lookup(keymap->ctx, name);
290 if (atom == XKB_ATOM_NONE)
291 return XKB_MOD_INVALID;
292
293 return XkbModNameToIndex(&keymap->mods, atom, MOD_BOTH);
294 }
295
296 /**
297 * Return the total number of active groups in the keymap.
298 */
299 XKB_EXPORT xkb_layout_index_t
xkb_keymap_num_layouts(struct xkb_keymap * keymap)300 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
301 {
302 return keymap->num_groups;
303 }
304
305 /**
306 * Returns the name for a given group.
307 */
308 XKB_EXPORT const char *
xkb_keymap_layout_get_name(struct xkb_keymap * keymap,xkb_layout_index_t idx)309 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
310 {
311 if (idx >= keymap->num_group_names)
312 return NULL;
313
314 return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
315 }
316
317 /**
318 * Returns the index for a named layout.
319 */
320 XKB_EXPORT xkb_layout_index_t
xkb_keymap_layout_get_index(struct xkb_keymap * keymap,const char * name)321 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
322 {
323 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
324 xkb_layout_index_t i;
325
326 if (atom == XKB_ATOM_NONE)
327 return XKB_LAYOUT_INVALID;
328
329 for (i = 0; i < keymap->num_group_names; i++)
330 if (keymap->group_names[i] == atom)
331 return i;
332
333 return XKB_LAYOUT_INVALID;
334 }
335
336 /**
337 * Returns the number of layouts active for a particular key.
338 */
339 XKB_EXPORT xkb_layout_index_t
xkb_keymap_num_layouts_for_key(struct xkb_keymap * keymap,xkb_keycode_t kc)340 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
341 {
342 const struct xkb_key *key = XkbKey(keymap, kc);
343
344 if (!key)
345 return 0;
346
347 return key->num_groups;
348 }
349
350 /**
351 * Returns the number of levels active for a particular key and layout.
352 */
353 XKB_EXPORT xkb_level_index_t
xkb_keymap_num_levels_for_key(struct xkb_keymap * keymap,xkb_keycode_t kc,xkb_layout_index_t layout)354 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
355 xkb_layout_index_t layout)
356 {
357 const struct xkb_key *key = XkbKey(keymap, kc);
358
359 if (!key)
360 return 0;
361
362 layout = XkbWrapGroupIntoRange(layout, key->num_groups,
363 key->out_of_range_group_action,
364 key->out_of_range_group_number);
365 if (layout == XKB_LAYOUT_INVALID)
366 return 0;
367
368 return XkbKeyNumLevels(key, layout);
369 }
370
371 /**
372 * Return the total number of LEDs in the keymap.
373 */
374 XKB_EXPORT xkb_led_index_t
xkb_keymap_num_leds(struct xkb_keymap * keymap)375 xkb_keymap_num_leds(struct xkb_keymap *keymap)
376 {
377 return keymap->num_leds;
378 }
379
380 /**
381 * Returns the name for a given LED.
382 */
383 XKB_EXPORT const char *
xkb_keymap_led_get_name(struct xkb_keymap * keymap,xkb_led_index_t idx)384 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
385 {
386 if (idx >= keymap->num_leds)
387 return NULL;
388
389 return xkb_atom_text(keymap->ctx, keymap->leds[idx].name);
390 }
391
392 /**
393 * Returns the index for a named LED.
394 */
395 XKB_EXPORT xkb_led_index_t
xkb_keymap_led_get_index(struct xkb_keymap * keymap,const char * name)396 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
397 {
398 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
399 xkb_led_index_t i;
400 const struct xkb_led *led;
401
402 if (atom == XKB_ATOM_NONE)
403 return XKB_LED_INVALID;
404
405 xkb_leds_enumerate(i, led, keymap)
406 if (led->name == atom)
407 return i;
408
409 return XKB_LED_INVALID;
410 }
411
412 XKB_EXPORT size_t
xkb_keymap_key_get_mods_for_level(struct xkb_keymap * keymap,xkb_keycode_t kc,xkb_layout_index_t layout,xkb_level_index_t level,xkb_mod_mask_t * masks_out,size_t masks_size)413 xkb_keymap_key_get_mods_for_level(struct xkb_keymap *keymap,
414 xkb_keycode_t kc,
415 xkb_layout_index_t layout,
416 xkb_level_index_t level,
417 xkb_mod_mask_t *masks_out,
418 size_t masks_size)
419 {
420 const struct xkb_key *key = XkbKey(keymap, kc);
421 if (!key)
422 return 0;
423
424 layout = XkbWrapGroupIntoRange(layout, key->num_groups,
425 key->out_of_range_group_action,
426 key->out_of_range_group_number);
427 if (layout == XKB_LAYOUT_INVALID)
428 return 0;
429
430 if (level >= XkbKeyNumLevels(key, layout))
431 return 0;
432
433 const struct xkb_key_type *type = key->groups[layout].type;
434
435 size_t count = 0;
436
437 /*
438 * If the active set of modifiers doesn't match any explicit entry of
439 * the key type, the resulting level is 0 (i.e. Level 1).
440 * So, if we are asked to find the modifiers for level==0, we can offer
441 * an ~infinite supply, which is not very workable.
442 * What we do instead, is special case the empty set of modifiers for
443 * this purpose. If the empty set isn't explicit mapped to a level, we
444 * take it to map to Level 1.
445 * This is almost always what we want. If applicable, given it priority
446 * over other ways to generate the level.
447 */
448 if (level == 0) {
449 bool empty_mapped = false;
450 for (unsigned i = 0; i < type->num_entries && count < masks_size; i++)
451 if (entry_is_active(&type->entries[i]) &&
452 type->entries[i].mods.mask == 0) {
453 empty_mapped = true;
454 break;
455 }
456 if (!empty_mapped && count < masks_size) {
457 masks_out[count++] = 0;
458 }
459 }
460
461 /* Now search explicit mappings. */
462 for (unsigned i = 0; i < type->num_entries && count < masks_size; i++) {
463 if (entry_is_active(&type->entries[i]) &&
464 type->entries[i].level == level) {
465 masks_out[count++] = type->entries[i].mods.mask;
466 }
467 }
468
469 return count;
470 }
471
472 /**
473 * As below, but takes an explicit layout/level rather than state.
474 */
475 XKB_EXPORT int
xkb_keymap_key_get_syms_by_level(struct xkb_keymap * keymap,xkb_keycode_t kc,xkb_layout_index_t layout,xkb_level_index_t level,const xkb_keysym_t ** syms_out)476 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
477 xkb_keycode_t kc,
478 xkb_layout_index_t layout,
479 xkb_level_index_t level,
480 const xkb_keysym_t **syms_out)
481 {
482 const struct xkb_key *key = XkbKey(keymap, kc);
483 int num_syms;
484
485 if (!key)
486 goto err;
487
488 layout = XkbWrapGroupIntoRange(layout, key->num_groups,
489 key->out_of_range_group_action,
490 key->out_of_range_group_number);
491 if (layout == XKB_LAYOUT_INVALID)
492 goto err;
493
494 if (level >= XkbKeyNumLevels(key, layout))
495 goto err;
496
497 num_syms = key->groups[layout].levels[level].num_syms;
498 if (num_syms == 0)
499 goto err;
500
501 if (num_syms == 1)
502 *syms_out = &key->groups[layout].levels[level].u.sym;
503 else
504 *syms_out = key->groups[layout].levels[level].u.syms;
505
506 return num_syms;
507
508 err:
509 *syms_out = NULL;
510 return 0;
511 }
512
513 XKB_EXPORT xkb_keycode_t
xkb_keymap_min_keycode(struct xkb_keymap * keymap)514 xkb_keymap_min_keycode(struct xkb_keymap *keymap)
515 {
516 return keymap->min_key_code;
517 }
518
519 XKB_EXPORT xkb_keycode_t
xkb_keymap_max_keycode(struct xkb_keymap * keymap)520 xkb_keymap_max_keycode(struct xkb_keymap *keymap)
521 {
522 return keymap->max_key_code;
523 }
524
525 XKB_EXPORT void
xkb_keymap_key_for_each(struct xkb_keymap * keymap,xkb_keymap_key_iter_t iter,void * data)526 xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
527 void *data)
528 {
529 struct xkb_key *key;
530
531 xkb_keys_foreach(key, keymap)
532 iter(keymap, key->keycode, data);
533 }
534
535 XKB_EXPORT const char *
xkb_keymap_key_get_name(struct xkb_keymap * keymap,xkb_keycode_t kc)536 xkb_keymap_key_get_name(struct xkb_keymap *keymap, xkb_keycode_t kc)
537 {
538 const struct xkb_key *key = XkbKey(keymap, kc);
539
540 if (!key)
541 return NULL;
542
543 return xkb_atom_text(keymap->ctx, key->name);
544 }
545
546 XKB_EXPORT xkb_keycode_t
xkb_keymap_key_by_name(struct xkb_keymap * keymap,const char * name)547 xkb_keymap_key_by_name(struct xkb_keymap *keymap, const char *name)
548 {
549 struct xkb_key *key;
550 xkb_atom_t atom;
551
552 atom = xkb_atom_lookup(keymap->ctx, name);
553 if (atom) {
554 xkb_atom_t ratom = XkbResolveKeyAlias(keymap, atom);
555 if (ratom)
556 atom = ratom;
557 }
558 if (!atom)
559 return XKB_KEYCODE_INVALID;
560
561 xkb_keys_foreach(key, keymap) {
562 if (key->name == atom)
563 return key->keycode;
564 }
565
566 return XKB_KEYCODE_INVALID;
567 }
568
569 /**
570 * Simple boolean specifying whether or not the key should repeat.
571 */
572 XKB_EXPORT int
xkb_keymap_key_repeats(struct xkb_keymap * keymap,xkb_keycode_t kc)573 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
574 {
575 const struct xkb_key *key = XkbKey(keymap, kc);
576
577 if (!key)
578 return 0;
579
580 return key->repeats;
581 }
582