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 xkb_mod_index_t i, num_vmods = 0;
132 for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
133 if (!keymap->vmod_names[i])
136 write_buf(buf, "\t\tvirtual_modifiers ");
140 xkb_atom_text(keymap->ctx, keymap->vmod_names[i]));
145 write_buf(buf, ";\n\n");
150 #define GET_TEXT_BUF_SIZE 512
152 #define append_get_text(...) do { \
153 int _size = snprintf(ret, GET_TEXT_BUF_SIZE, __VA_ARGS__); \
154 if (_size >= GET_TEXT_BUF_SIZE) \
159 get_indicator_state_text(enum xkb_state_component which)
162 static char ret[GET_TEXT_BUF_SIZE];
164 memset(ret, 0, GET_TEXT_BUF_SIZE);
171 for (i = 0; which != 0; i++) {
174 if (!(which & (1 << i)))
178 name = LookupValue(modComponentMaskNames, (1 << i));
181 append_get_text("%s+%s", ret, name);
183 append_get_text("%s", name);
190 get_control_mask_text(enum xkb_action_controls control_mask)
193 static char ret[GET_TEXT_BUF_SIZE];
194 const char *control_name;
196 memset(ret, 0, GET_TEXT_BUF_SIZE);
198 if (control_mask == 0) {
202 else if (control_mask == CONTROL_ALL) {
207 for (i = 0; control_mask; i++) {
208 if (!(control_mask & (1 << i)))
211 control_mask &= ~(1 << i);
212 control_name = LookupValue(ctrlMaskNames, (1 << i));
215 append_get_text("%s+%s", ret, control_name);
217 append_get_text("%s", control_name);
224 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
227 struct xkb_key_alias *alias;
230 if (keymap->keycodes_section_name)
231 write_buf(buf, "\txkb_keycodes \"%s\" {\n",
232 keymap->keycodes_section_name);
234 write_buf(buf, "\txkb_keycodes {\n");
236 xkb_foreach_key(key, keymap) {
237 if (key->name == XKB_ATOM_NONE)
240 write_buf(buf, "\t\t%-20s = %d;\n",
241 KeyNameText(keymap->ctx, key->name), key->keycode);
244 for (i = 0; i < XKB_NUM_INDICATORS; i++) {
245 if (keymap->indicators[i].name == XKB_ATOM_NONE)
247 write_buf(buf, "\t\tindicator %d = \"%s\";\n", i + 1,
248 xkb_atom_text(keymap->ctx, keymap->indicators[i].name));
252 darray_foreach(alias, keymap->key_aliases)
253 write_buf(buf, "\t\talias %-14s = %s;\n",
254 KeyNameText(keymap->ctx, alias->alias),
255 KeyNameText(keymap->ctx, alias->real));
257 write_buf(buf, "\t};\n\n");
262 write_types(struct xkb_keymap *keymap, struct buf *buf)
266 struct xkb_key_type *type;
267 struct xkb_kt_map_entry *entry;
269 if (keymap->types_section_name)
270 write_buf(buf, "\txkb_types \"%s\" {\n\n",
271 keymap->types_section_name);
273 write_buf(buf, "\txkb_types {\n\n");
275 write_vmods(keymap, buf);
277 for (i = 0; i < keymap->num_types; i++) {
278 type = &keymap->types[i];
280 write_buf(buf, "\t\ttype \"%s\" {\n",
281 xkb_atom_text(keymap->ctx, type->name));
282 write_buf(buf, "\t\t\tmodifiers= %s;\n",
283 VModMaskText(keymap, type->mods.mods));
285 for (j = 0; j < type->num_entries; j++) {
287 entry = &type->map[j];
290 * Printing level 1 entries is redundant, it's the default,
291 * unless there's preserve info.
293 if (entry->level == 0 && entry->preserve.mods == 0)
296 str = VModMaskText(keymap, entry->mods.mods);
297 write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
298 str, entry->level + 1);
300 if (entry->preserve.mods == 0)
303 write_buf(buf, "\t\t\tpreserve[%s]= ", str);
304 write_buf(buf, "%s;\n", VModMaskText(keymap, entry->preserve.mods));
307 if (type->level_names) {
308 for (n = 0; n < type->num_levels; n++) {
309 if (!type->level_names[n])
311 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
312 xkb_atom_text(keymap->ctx, type->level_names[n]));
315 write_buf(buf, "\t\t};\n");
318 write_buf(buf, "\t};\n\n");
323 write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
325 struct xkb_indicator_map *led = &keymap->indicators[num];
327 write_buf(buf, "\t\tindicator \"%s\" {\n",
328 xkb_atom_text(keymap->ctx, keymap->indicators[num].name));
330 if (led->which_groups) {
331 if (led->which_groups != XKB_STATE_EFFECTIVE) {
332 write_buf(buf, "\t\t\twhichGroupState= %s;\n",
333 get_indicator_state_text(led->which_groups));
335 write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
339 if (led->which_mods) {
340 if (led->which_mods != XKB_STATE_EFFECTIVE) {
341 write_buf(buf, "\t\t\twhichModState= %s;\n",
342 get_indicator_state_text(led->which_mods));
344 write_buf(buf, "\t\t\tmodifiers= %s;\n",
345 VModMaskText(keymap, led->mods.mods));
349 write_buf(buf, "\t\t\tcontrols= %s;\n",
350 get_control_mask_text(led->ctrls));
353 write_buf(buf, "\t\t};\n");
358 write_action(struct xkb_keymap *keymap, struct buf *buf,
359 const union xkb_action *action,
360 const char *prefix, const char *suffix)
363 const char *args = NULL;
370 type = ActionTypeText(action->type);
372 switch (action->type) {
373 case ACTION_TYPE_MOD_SET:
374 case ACTION_TYPE_MOD_LATCH:
375 case ACTION_TYPE_MOD_LOCK:
376 if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
379 args = VModMaskText(keymap, action->mods.mods.mods);
380 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
381 (action->type != ACTION_TYPE_MOD_LOCK &&
382 (action->mods.flags & ACTION_LOCK_CLEAR)) ?
384 (action->type != ACTION_TYPE_MOD_LOCK &&
385 (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
390 case ACTION_TYPE_GROUP_SET:
391 case ACTION_TYPE_GROUP_LATCH:
392 case ACTION_TYPE_GROUP_LOCK:
393 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
394 (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
395 action->group.group > 0) ? "+" : "",
396 (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
397 action->group.group + 1 : action->group.group,
398 (action->type != ACTION_TYPE_GROUP_LOCK &&
399 (action->group.flags & ACTION_LOCK_CLEAR)) ?
401 (action->type != ACTION_TYPE_GROUP_LOCK &&
402 (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
407 case ACTION_TYPE_TERMINATE:
408 write_buf(buf, "%s%s()%s", prefix, type, suffix);
411 case ACTION_TYPE_PTR_MOVE:
412 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
413 (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
414 action->ptr.x >= 0) ? "+" : "",
416 (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
417 action->ptr.y >= 0) ? "+" : "",
419 (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
423 case ACTION_TYPE_PTR_LOCK:
424 switch (action->btn.flags &
425 (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
426 case ACTION_LOCK_NO_UNLOCK:
427 args = ",affect=lock";
430 case ACTION_LOCK_NO_LOCK:
431 args = ",affect=unlock";
434 case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
435 args = ",affect=neither";
439 args = ",affect=both";
442 case ACTION_TYPE_PTR_BUTTON:
443 write_buf(buf, "%s%s(button=", prefix, type);
444 if (action->btn.button > 0 && action->btn.button <= 5)
445 write_buf(buf, "%d", action->btn.button);
447 write_buf(buf, "default");
448 if (action->btn.count)
449 write_buf(buf, ",count=%d", action->btn.count);
451 write_buf(buf, "%s", args);
452 write_buf(buf, ")%s", suffix);
455 case ACTION_TYPE_PTR_DEFAULT:
456 write_buf(buf, "%s%s(", prefix, type);
457 write_buf(buf, "affect=button,button=%s%d",
458 (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
459 action->dflt.value >= 0) ? "+" : "",
461 write_buf(buf, ")%s", suffix);
464 case ACTION_TYPE_SWITCH_VT:
465 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
466 (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
467 action->screen.screen >= 0) ? "+" : "",
468 action->screen.screen,
469 (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
473 case ACTION_TYPE_CTRL_SET:
474 case ACTION_TYPE_CTRL_LOCK:
475 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
476 get_control_mask_text(action->ctrls.ctrls), suffix);
479 case ACTION_TYPE_NONE:
480 write_buf(buf, "%sNoAction()%s", prefix, suffix);
485 "%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",
486 prefix, type, action->type, action->priv.data[0],
487 action->priv.data[1], action->priv.data[2],
488 action->priv.data[3], action->priv.data[4],
489 action->priv.data[5], action->priv.data[6],
498 write_compat(struct xkb_keymap *keymap, struct buf *buf)
501 struct xkb_sym_interpret *interp;
503 if (keymap->compat_section_name)
504 write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
505 keymap->compat_section_name);
507 write_buf(buf, "\txkb_compatibility {\n\n");
509 write_vmods(keymap, buf);
511 write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
512 write_buf(buf, "\t\tinterpret.repeat= False;\n");
514 darray_foreach(interp, keymap->sym_interpret) {
515 char keysym_name[64];
517 if (interp->sym == XKB_KEY_NoSymbol)
518 sprintf(keysym_name, "Any");
520 xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
522 write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
524 SIMatchText(interp->match),
525 VModMaskText(keymap, interp->mods));
527 if (interp->virtual_mod != XKB_MOD_INVALID) {
528 write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
529 xkb_atom_text(keymap->ctx,
530 keymap->vmod_names[interp->virtual_mod]));
533 if (interp->match & MATCH_LEVEL_ONE_ONLY)
535 "\t\t\tuseModMapMods=level1;\n");
537 write_buf(buf, "\t\t\trepeat= True;\n");
539 write_action(keymap, buf, &interp->act, "\t\t\taction= ", ";\n");
540 write_buf(buf, "\t\t};\n");
543 for (i = 0; i < XKB_NUM_INDICATORS; i++) {
544 struct xkb_indicator_map *map = &keymap->indicators[i];
545 if (map->which_groups == 0 && map->groups == 0 &&
546 map->which_mods == 0 && map->mods.mods == 0 &&
549 write_indicator_map(keymap, buf, i);
552 write_buf(buf, "\t};\n\n");
558 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
559 struct xkb_key *key, xkb_layout_index_t group)
561 const xkb_keysym_t *syms;
563 xkb_level_index_t level;
564 #define OUT_BUF_LEN 128
565 char out_buf[OUT_BUF_LEN];
567 for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
569 write_buf(buf, ", ");
570 num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
571 group, level, &syms);
573 write_buf(buf, "%15s", "NoSymbol");
575 else if (num_syms == 1) {
576 xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
577 write_buf(buf, "%15s", out_buf);
581 write_buf(buf, "{ ");
582 for (s = 0; s < num_syms; s++) {
584 write_buf(buf, ", ");
585 xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
586 write_buf(buf, "%s", out_buf);
588 write_buf(buf, " }");
597 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
600 xkb_layout_index_t group, tmp;
601 xkb_atom_t *group_name;
604 if (keymap->symbols_section_name)
605 write_buf(buf, "\txkb_symbols \"%s\" {\n\n",
606 keymap->symbols_section_name);
608 write_buf(buf, "\txkb_symbols {\n\n");
611 darray_enumerate(group, group_name, keymap->group_names) {
615 "\t\tname[group%d]=\"%s\";\n", group + 1,
616 xkb_atom_text(keymap->ctx, *group_name));
620 write_buf(buf, "\n");
622 xkb_foreach_key(key, keymap) {
624 bool explicit_types = false;
625 bool multi_type = false;
627 if (key->num_groups == 0)
630 write_buf(buf, "\t\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
632 for (group = 0; group < key->num_groups; group++) {
633 if (key->groups[group].explicit_type)
634 explicit_types = true;
636 if (group != 0 && key->groups[group].type != key->groups[0].type)
640 if (explicit_types) {
641 const struct xkb_key_type *type;
645 for (group = 0; group < key->num_groups; group++) {
646 if (!key->groups[group].explicit_type)
649 type = key->groups[group].type;
650 write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
652 xkb_atom_text(keymap->ctx, type->name));
656 type = key->groups[0].type;
657 write_buf(buf, "\n\t\t\ttype= \"%s\",",
658 xkb_atom_text(keymap->ctx, type->name));
662 if (key->explicit & EXPLICIT_REPEAT) {
664 write_buf(buf, "\n\t\t\trepeat= Yes,");
666 write_buf(buf, "\n\t\t\trepeat= No,");
670 if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP)) {
671 /* XXX: vmodmap cmask? */
672 write_buf(buf, "\n\t\t\tvirtualMods= %s,",
673 VModMaskText(keymap, key->vmodmap << XKB_NUM_CORE_MODS));
676 switch (key->out_of_range_group_action) {
678 write_buf(buf, "\n\t\t\tgroupsClamp,");
682 write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
683 key->out_of_range_group_number + 1);
690 showActions = !!(key->explicit & EXPLICIT_INTERP);
692 if (key->num_groups > 1 || showActions)
696 write_buf(buf, "\t[ ");
697 if (!write_keysyms(keymap, buf, key, 0))
699 write_buf(buf, " ] };\n");
702 xkb_level_index_t level;
704 for (group = 0; group < key->num_groups; group++) {
707 write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
708 if (!write_keysyms(keymap, buf, key, group))
710 write_buf(buf, " ]");
712 write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
715 level < XkbKeyGroupWidth(key, group); level++) {
717 write_buf(buf, ", ");
718 write_action(keymap, buf,
719 &key->groups[group].levels[level].action,
722 write_buf(buf, " ]");
725 write_buf(buf, "\n\t\t};\n");
729 xkb_foreach_key(key, keymap) {
732 if (key->modmap == 0)
735 for (mod = 0; mod < XKB_NUM_CORE_MODS; mod++) {
736 if (!(key->modmap & (1 << mod)))
739 write_buf(buf, "\t\tmodifier_map %s { %s };\n",
741 KeyNameText(keymap->ctx, key->name));
745 write_buf(buf, "\t};\n\n");
750 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
751 enum xkb_keymap_format format)
754 struct buf buf = { NULL, 0, 0 };
756 if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
757 format = keymap->format;
759 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
761 "Trying to get a keymap as a string in an unsupported format (%d)\n",
766 ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
767 write_keycodes(keymap, &buf) &&
768 write_types(keymap, &buf) &&
769 write_compat(keymap, &buf) &&
770 write_symbols(keymap, &buf) &&
771 check_write_buf(&buf, "};\n"));
773 return (ok ? buf.buf : NULL);