1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
28 * Copyright © 2012 Intel Corporation
30 * Permission is hereby granted, free of charge, to any person obtaining a
31 * copy of this software and associated documentation files (the "Software"),
32 * to deal in the Software without restriction, including without limitation
33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34 * and/or sell copies of the Software, and to permit persons to whom the
35 * Software is furnished to do so, subject to the following conditions:
37 * The above copyright notice and this permission notice (including the next
38 * paragraph) shall be included in all copies or substantial portions of the
41 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
44 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47 * DEALINGS IN THE SOFTWARE.
49 * Author: Daniel Stone <daniel@fooishbar.org>
55 #define VMOD_HIDE_VALUE 0
56 #define VMOD_SHOW_VALUE 1
57 #define VMOD_COMMENT_VALUE 2
59 #define BUF_CHUNK_SIZE 4096
68 do_realloc(struct buf *buf, size_t at_least)
72 buf->alloc += BUF_CHUNK_SIZE;
73 if (at_least >= BUF_CHUNK_SIZE)
74 buf->alloc += at_least;
76 new = realloc(buf->buf, buf->alloc);
84 ATTR_PRINTF(2, 3) static bool
85 check_write_buf(struct buf *buf, const char *fmt, ...)
91 available = buf->alloc - buf->size;
93 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
99 if (printed >= available)
100 if (!do_realloc(buf, printed))
103 /* The buffer has enough space now. */
105 available = buf->alloc - buf->size;
107 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
110 if (printed >= available || printed < 0)
113 buf->size += printed;
122 #define write_buf(buf, ...) do { \
123 if (!check_write_buf(buf, __VA_ARGS__)) \
128 write_vmods(struct xkb_keymap *keymap, struct buf *buf)
130 const struct xkb_mod *mod;
131 xkb_mod_index_t num_vmods = 0;
133 darray_foreach(mod, keymap->mods) {
134 if (mod->type != MOD_VIRT)
138 write_buf(buf, "\t\tvirtual_modifiers ");
141 write_buf(buf, "%s", xkb_atom_text(keymap->ctx, mod->name));
146 write_buf(buf, ";\n\n");
152 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
155 struct xkb_key_alias *alias;
157 const struct xkb_led *led;
159 if (keymap->keycodes_section_name)
160 write_buf(buf, "\txkb_keycodes \"%s\" {\n",
161 keymap->keycodes_section_name);
163 write_buf(buf, "\txkb_keycodes {\n");
165 xkb_foreach_key(key, keymap) {
166 if (key->name == XKB_ATOM_NONE)
169 write_buf(buf, "\t\t%-20s = %d;\n",
170 KeyNameText(keymap->ctx, key->name), key->keycode);
173 darray_enumerate(i, led, keymap->leds)
174 if (led->name != XKB_ATOM_NONE)
175 write_buf(buf, "\t\tindicator %d = \"%s\";\n",
176 i + 1, xkb_atom_text(keymap->ctx, led->name));
179 darray_foreach(alias, keymap->key_aliases)
180 write_buf(buf, "\t\talias %-14s = %s;\n",
181 KeyNameText(keymap->ctx, alias->alias),
182 KeyNameText(keymap->ctx, alias->real));
184 write_buf(buf, "\t};\n\n");
189 write_types(struct xkb_keymap *keymap, struct buf *buf)
193 struct xkb_key_type *type;
194 struct xkb_kt_map_entry *entry;
196 if (keymap->types_section_name)
197 write_buf(buf, "\txkb_types \"%s\" {\n\n",
198 keymap->types_section_name);
200 write_buf(buf, "\txkb_types {\n\n");
202 write_vmods(keymap, buf);
204 for (i = 0; i < keymap->num_types; i++) {
205 type = &keymap->types[i];
207 write_buf(buf, "\t\ttype \"%s\" {\n",
208 xkb_atom_text(keymap->ctx, type->name));
209 write_buf(buf, "\t\t\tmodifiers= %s;\n",
210 ModMaskText(keymap, type->mods.mods));
212 for (j = 0; j < type->num_entries; j++) {
214 entry = &type->map[j];
217 * Printing level 1 entries is redundant, it's the default,
218 * unless there's preserve info.
220 if (entry->level == 0 && entry->preserve.mods == 0)
223 str = ModMaskText(keymap, entry->mods.mods);
224 write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
225 str, entry->level + 1);
227 if (entry->preserve.mods == 0)
230 write_buf(buf, "\t\t\tpreserve[%s]= ", str);
231 write_buf(buf, "%s;\n",
232 ModMaskText(keymap, entry->preserve.mods));
235 if (type->level_names) {
236 for (n = 0; n < type->num_levels; n++) {
237 if (!type->level_names[n])
239 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
240 xkb_atom_text(keymap->ctx, type->level_names[n]));
243 write_buf(buf, "\t\t};\n");
246 write_buf(buf, "\t};\n\n");
251 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
252 const struct xkb_led *led)
254 write_buf(buf, "\t\tindicator \"%s\" {\n",
255 xkb_atom_text(keymap->ctx, led->name));
257 if (led->which_groups) {
258 if (led->which_groups != XKB_STATE_LAYOUT_EFFECTIVE) {
259 write_buf(buf, "\t\t\twhichGroupState= %s;\n",
260 LedStateText(keymap->ctx, led->which_groups));
262 write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
266 if (led->which_mods) {
267 if (led->which_mods != XKB_STATE_MODS_EFFECTIVE) {
268 write_buf(buf, "\t\t\twhichModState= %s;\n",
269 LedStateText(keymap->ctx, led->which_mods));
271 write_buf(buf, "\t\t\tmodifiers= %s;\n",
272 ModMaskText(keymap, led->mods.mods));
276 write_buf(buf, "\t\t\tcontrols= %s;\n",
277 ControlMaskText(keymap->ctx, led->ctrls));
280 write_buf(buf, "\t\t};\n");
285 write_action(struct xkb_keymap *keymap, struct buf *buf,
286 const union xkb_action *action,
287 const char *prefix, const char *suffix)
290 const char *args = NULL;
297 type = ActionTypeText(action->type);
299 switch (action->type) {
300 case ACTION_TYPE_MOD_SET:
301 case ACTION_TYPE_MOD_LATCH:
302 case ACTION_TYPE_MOD_LOCK:
303 if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
306 args = ModMaskText(keymap, action->mods.mods.mods);
307 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
308 (action->type != ACTION_TYPE_MOD_LOCK &&
309 (action->mods.flags & ACTION_LOCK_CLEAR)) ?
311 (action->type != ACTION_TYPE_MOD_LOCK &&
312 (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
317 case ACTION_TYPE_GROUP_SET:
318 case ACTION_TYPE_GROUP_LATCH:
319 case ACTION_TYPE_GROUP_LOCK:
320 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
321 (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
322 action->group.group > 0) ? "+" : "",
323 (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
324 action->group.group + 1 : action->group.group,
325 (action->type != ACTION_TYPE_GROUP_LOCK &&
326 (action->group.flags & ACTION_LOCK_CLEAR)) ?
328 (action->type != ACTION_TYPE_GROUP_LOCK &&
329 (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
334 case ACTION_TYPE_TERMINATE:
335 write_buf(buf, "%s%s()%s", prefix, type, suffix);
338 case ACTION_TYPE_PTR_MOVE:
339 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
340 (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
341 action->ptr.x >= 0) ? "+" : "",
343 (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
344 action->ptr.y >= 0) ? "+" : "",
346 (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
350 case ACTION_TYPE_PTR_LOCK:
351 switch (action->btn.flags &
352 (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
353 case ACTION_LOCK_NO_UNLOCK:
354 args = ",affect=lock";
357 case ACTION_LOCK_NO_LOCK:
358 args = ",affect=unlock";
361 case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
362 args = ",affect=neither";
366 args = ",affect=both";
369 case ACTION_TYPE_PTR_BUTTON:
370 write_buf(buf, "%s%s(button=", prefix, type);
371 if (action->btn.button > 0 && action->btn.button <= 5)
372 write_buf(buf, "%d", action->btn.button);
374 write_buf(buf, "default");
375 if (action->btn.count)
376 write_buf(buf, ",count=%d", action->btn.count);
378 write_buf(buf, "%s", args);
379 write_buf(buf, ")%s", suffix);
382 case ACTION_TYPE_PTR_DEFAULT:
383 write_buf(buf, "%s%s(", prefix, type);
384 write_buf(buf, "affect=button,button=%s%d",
385 (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
386 action->dflt.value >= 0) ? "+" : "",
388 write_buf(buf, ")%s", suffix);
391 case ACTION_TYPE_SWITCH_VT:
392 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
393 (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
394 action->screen.screen >= 0) ? "+" : "",
395 action->screen.screen,
396 (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
400 case ACTION_TYPE_CTRL_SET:
401 case ACTION_TYPE_CTRL_LOCK:
402 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
403 ControlMaskText(keymap->ctx, action->ctrls.ctrls), suffix);
406 case ACTION_TYPE_NONE:
407 write_buf(buf, "%sNoAction()%s", prefix, suffix);
412 "%s%s(type=0x%02x,data[0]=0x%02x,data[1]=0x%02x,data[2]=0x%02x,data[3]=0x%02x,data[4]=0x%02x,data[5]=0x%02x,data[6]=0x%02x)%s",
413 prefix, type, action->type, action->priv.data[0],
414 action->priv.data[1], action->priv.data[2],
415 action->priv.data[3], action->priv.data[4],
416 action->priv.data[5], action->priv.data[6],
425 write_compat(struct xkb_keymap *keymap, struct buf *buf)
427 struct xkb_sym_interpret *interp;
428 const struct xkb_led *led;
430 if (keymap->compat_section_name)
431 write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
432 keymap->compat_section_name);
434 write_buf(buf, "\txkb_compatibility {\n\n");
436 write_vmods(keymap, buf);
438 write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
439 write_buf(buf, "\t\tinterpret.repeat= False;\n");
441 darray_foreach(interp, keymap->sym_interprets) {
442 char keysym_name[64];
444 if (interp->sym == XKB_KEY_NoSymbol)
445 sprintf(keysym_name, "Any");
447 xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
449 write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
451 SIMatchText(interp->match),
452 ModMaskText(keymap, interp->mods));
454 if (interp->virtual_mod != XKB_MOD_INVALID)
455 write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
456 ModIndexText(keymap, interp->virtual_mod));
458 if (interp->level_one_only)
459 write_buf(buf, "\t\t\tuseModMapMods=level1;\n");
461 write_buf(buf, "\t\t\trepeat= True;\n");
463 write_action(keymap, buf, &interp->action, "\t\t\taction= ", ";\n");
464 write_buf(buf, "\t\t};\n");
467 darray_foreach(led, keymap->leds)
468 if (led->which_groups || led->groups || led->which_mods ||
469 led->mods.mods || led->ctrls)
470 write_led_map(keymap, buf, led);
472 write_buf(buf, "\t};\n\n");
478 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
479 struct xkb_key *key, xkb_layout_index_t group)
481 const xkb_keysym_t *syms;
483 xkb_level_index_t level;
484 #define OUT_BUF_LEN 128
485 char out_buf[OUT_BUF_LEN];
487 for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
489 write_buf(buf, ", ");
490 num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
491 group, level, &syms);
493 write_buf(buf, "%15s", "NoSymbol");
495 else if (num_syms == 1) {
496 xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
497 write_buf(buf, "%15s", out_buf);
501 write_buf(buf, "{ ");
502 for (s = 0; s < num_syms; s++) {
504 write_buf(buf, ", ");
505 xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
506 write_buf(buf, "%s", out_buf);
508 write_buf(buf, " }");
517 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
520 xkb_layout_index_t group;
523 if (keymap->symbols_section_name)
524 write_buf(buf, "\txkb_symbols \"%s\" {\n\n",
525 keymap->symbols_section_name);
527 write_buf(buf, "\txkb_symbols {\n\n");
529 for (group = 0; group < keymap->num_group_names; group++)
530 if (keymap->group_names[group])
532 "\t\tname[group%d]=\"%s\";\n", group + 1,
533 xkb_atom_text(keymap->ctx, keymap->group_names[group]));
535 write_buf(buf, "\n");
537 xkb_foreach_key(key, keymap) {
539 bool explicit_types = false;
540 bool multi_type = false;
542 if (key->num_groups == 0)
545 write_buf(buf, "\t\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
547 for (group = 0; group < key->num_groups; group++) {
548 if (key->groups[group].explicit_type)
549 explicit_types = true;
551 if (group != 0 && key->groups[group].type != key->groups[0].type)
555 if (explicit_types) {
556 const struct xkb_key_type *type;
560 for (group = 0; group < key->num_groups; group++) {
561 if (!key->groups[group].explicit_type)
564 type = key->groups[group].type;
565 write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
567 xkb_atom_text(keymap->ctx, type->name));
571 type = key->groups[0].type;
572 write_buf(buf, "\n\t\t\ttype= \"%s\",",
573 xkb_atom_text(keymap->ctx, type->name));
577 if (key->explicit & EXPLICIT_REPEAT) {
579 write_buf(buf, "\n\t\t\trepeat= Yes,");
581 write_buf(buf, "\n\t\t\trepeat= No,");
585 if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
586 write_buf(buf, "\n\t\t\tvirtualMods= %s,",
587 ModMaskText(keymap, key->vmodmap));
589 switch (key->out_of_range_group_action) {
591 write_buf(buf, "\n\t\t\tgroupsClamp,");
595 write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
596 key->out_of_range_group_number + 1);
603 showActions = !!(key->explicit & EXPLICIT_INTERP);
605 if (key->num_groups > 1 || showActions)
609 write_buf(buf, "\t[ ");
610 if (!write_keysyms(keymap, buf, key, 0))
612 write_buf(buf, " ] };\n");
615 xkb_level_index_t level;
617 for (group = 0; group < key->num_groups; group++) {
620 write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
621 if (!write_keysyms(keymap, buf, key, group))
623 write_buf(buf, " ]");
625 write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
628 level < XkbKeyGroupWidth(key, group); level++) {
630 write_buf(buf, ", ");
631 write_action(keymap, buf,
632 &key->groups[group].levels[level].action,
635 write_buf(buf, " ]");
638 write_buf(buf, "\n\t\t};\n");
642 xkb_foreach_key(key, keymap) {
644 const struct xkb_mod *mod;
646 if (key->modmap == 0)
649 darray_enumerate(i, mod, keymap->mods) {
650 if (!(key->modmap & (1 << i)))
653 write_buf(buf, "\t\tmodifier_map %s { %s };\n",
654 xkb_atom_text(keymap->ctx, mod->name),
655 KeyNameText(keymap->ctx, key->name));
659 write_buf(buf, "\t};\n\n");
664 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
665 enum xkb_keymap_format format)
668 struct buf buf = { NULL, 0, 0 };
670 if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
671 format = keymap->format;
673 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
675 "Trying to get a keymap as a string in an unsupported format (%d)\n",
680 ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
681 write_keycodes(keymap, &buf) &&
682 write_types(keymap, &buf) &&
683 write_compat(keymap, &buf) &&
684 write_symbols(keymap, &buf) &&
685 check_write_buf(&buf, "};\n"));
687 return (ok ? buf.buf : NULL);