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>
60 #define VMOD_HIDE_VALUE 0
61 #define VMOD_SHOW_VALUE 1
62 #define VMOD_COMMENT_VALUE 2
64 #define BUF_CHUNK_SIZE 4096
73 do_realloc(struct buf *buf, size_t at_least)
77 buf->alloc += BUF_CHUNK_SIZE;
78 if (at_least >= BUF_CHUNK_SIZE)
79 buf->alloc += at_least;
81 new = realloc(buf->buf, buf->alloc);
89 ATTR_PRINTF(2, 3) static bool
90 check_write_buf(struct buf *buf, const char *fmt, ...)
96 available = buf->alloc - buf->size;
98 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
104 if (printed >= available)
105 if (!do_realloc(buf, printed))
108 /* The buffer has enough space now. */
110 available = buf->alloc - buf->size;
112 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
115 if (printed >= available || printed < 0)
118 buf->size += printed;
127 #define write_buf(buf, ...) do { \
128 if (!check_write_buf(buf, __VA_ARGS__)) \
133 write_vmods(struct xkb_keymap *keymap, struct buf *buf)
138 for (i = 0; i < XkbNumVirtualMods; i++) {
139 if (!keymap->vmod_names[i])
142 write_buf(buf, "\t\tvirtual_modifiers ");
145 write_buf(buf, "%s", keymap->vmod_names[i]);
150 write_buf(buf, ";\n\n");
155 #define GET_TEXT_BUF_SIZE 512
157 #define append_get_text(...) do { \
158 int _size = snprintf(ret, GET_TEXT_BUF_SIZE, __VA_ARGS__); \
159 if (_size >= GET_TEXT_BUF_SIZE) \
164 get_mod_mask_text(struct xkb_keymap *keymap, uint8_t real_mods,
167 static char ret[GET_TEXT_BUF_SIZE], ret2[GET_TEXT_BUF_SIZE];
170 memset(ret, 0, GET_TEXT_BUF_SIZE);
172 if (real_mods == 0 && vmods == 0) {
177 /* This is so broken. If we have a real modmask of 0xff and a
178 * vmodmask, we'll get, e.g., all+RightControl. But, it's what xkbfile
180 if (real_mods == 0xff) {
183 else if (real_mods) {
184 for (i = 0; i < XkbNumModifiers; i++) {
185 if (!(real_mods & (1 << i)))
187 if (ret[0] != '\0') {
189 append_get_text("%s+%s", ret2, ModIndexToName(i));
192 append_get_text("%s", ModIndexToName(i));
200 for (i = 0; i < XkbNumVirtualMods; i++) {
201 if (!(vmods & (1 << i)))
203 if (ret[0] != '\0') {
205 append_get_text("%s+%s", ret2, keymap->vmod_names[i]);
208 append_get_text("%s", keymap->vmod_names[i]);
216 get_indicator_state_text(uint8_t which)
219 static char ret[GET_TEXT_BUF_SIZE];
220 /* FIXME: Merge with ... something ... in xkbcomp? */
221 static const char *state_names[] = {
229 memset(ret, 0, GET_TEXT_BUF_SIZE);
231 which &= XkbIM_UseAnyMods;
238 for (i = 0; which != 0; i++) {
239 if (!(which & (1 << i)))
244 append_get_text("%s+%s", ret, state_names[i]);
246 append_get_text("%s", state_names[i]);
253 get_control_mask_text(uint32_t control_mask)
256 static char ret[GET_TEXT_BUF_SIZE];
257 /* FIXME: Merge with ... something ... in xkbcomp. */
258 static const char *ctrl_names[] = {
274 memset(ret, 0, GET_TEXT_BUF_SIZE);
276 control_mask &= XkbAllBooleanCtrlsMask;
278 if (control_mask == 0) {
282 else if (control_mask == XkbAllBooleanCtrlsMask) {
287 for (i = 0; control_mask; i++) {
288 if (!(control_mask & (1 << i)))
290 control_mask &= ~(1 << i);
293 append_get_text("%s+%s", ret, ctrl_names[i]);
295 append_get_text("%s", ctrl_names[i]);
302 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
305 struct xkb_key_alias *alias;
308 if (keymap->keycodes_section_name)
309 write_buf(buf, "\txkb_keycodes \"%s\" {\n",
310 keymap->keycodes_section_name);
312 write_buf(buf, "\txkb_keycodes {\n");
314 write_buf(buf, "\t\tminimum = %d;\n",
315 keymap->min_key_code);
316 write_buf(buf, "\t\tmaximum = %d;\n",
317 keymap->max_key_code);
319 xkb_foreach_key(key, keymap) {
320 if (key->name[0] == '\0')
323 write_buf(buf, "\t\t%6s = %d;\n",
324 KeyNameText(key->name), XkbKeyGetKeycode(keymap, key));
327 for (i = 0; i < XkbNumIndicators; i++) {
328 if (!keymap->indicator_names[i])
330 write_buf(buf, "\t\tindicator %d = \"%s\";\n",
331 i + 1, keymap->indicator_names[i]);
335 darray_foreach(alias, keymap->key_aliases)
336 write_buf(buf, "\t\talias %6s = %6s;\n",
337 KeyNameText(alias->alias),
338 KeyNameText(alias->real));
340 write_buf(buf, "\t};\n\n");
345 write_types(struct xkb_keymap *keymap, struct buf *buf)
349 struct xkb_key_type *type;
350 struct xkb_kt_map_entry *entry;
352 if (keymap->types_section_name)
353 write_buf(buf, "\txkb_types \"%s\" {\n\n",
354 keymap->types_section_name);
356 write_buf(buf, "\txkb_types {\n\n");
358 write_vmods(keymap, buf);
360 for (i = 0; i < keymap->num_types; i++) {
361 type = &keymap->types[i];
363 write_buf(buf, "\t\ttype \"%s\" {\n",
365 write_buf(buf, "\t\t\tmodifiers= %s;\n",
366 get_mod_mask_text(keymap, type->mods.real_mods,
369 darray_foreach(entry, type->map) {
373 * Printing level 1 entries is redundant, it's the default,
374 * unless there's preserve info.
376 if (entry->level == 0 && entry->preserve.mask == 0)
379 str = get_mod_mask_text(keymap, entry->mods.real_mods,
381 write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
382 str, entry->level + 1);
384 if (!entry->preserve.real_mods && !entry->preserve.vmods)
387 write_buf(buf, "\t\t\tpreserve[%s]= ", str);
388 write_buf(buf, "%s;\n",
389 get_mod_mask_text(keymap, entry->preserve.real_mods,
390 entry->preserve.vmods));
393 if (type->level_names) {
394 for (n = 0; n < type->num_levels; n++) {
395 if (!type->level_names[n])
397 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
398 type->level_names[n]);
401 write_buf(buf, "\t\t};\n");
404 write_buf(buf, "\t};\n\n");
409 write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
411 struct xkb_indicator_map *led = &keymap->indicators[num];
413 write_buf(buf, "\t\tindicator \"%s\" {\n",
414 keymap->indicator_names[num]);
416 if (led->which_groups) {
417 if (led->which_groups != XkbIM_UseEffective) {
418 write_buf(buf, "\t\t\twhichGroupState= %s;\n",
419 get_indicator_state_text(led->which_groups));
421 write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
425 if (led->which_mods) {
426 if (led->which_mods != XkbIM_UseEffective) {
427 write_buf(buf, "\t\t\twhichModState= %s;\n",
428 get_indicator_state_text(led->which_mods));
430 write_buf(buf, "\t\t\tmodifiers= %s;\n",
431 get_mod_mask_text(keymap, led->mods.real_mods,
436 write_buf(buf, "\t\t\tcontrols= %s;\n",
437 get_control_mask_text(led->ctrls));
440 write_buf(buf, "\t\t};\n");
445 write_action(struct xkb_keymap *keymap, struct buf *buf,
446 union xkb_action *action, const char *prefix, const char *suffix)
449 const char *args = NULL;
456 type = ActionTypeText(action->any.type);
458 switch (action->any.type) {
460 case XkbSA_LatchMods:
462 if (action->mods.flags & XkbSA_UseModMapMods)
465 args = get_mod_mask_text(keymap, action->mods.real_mods,
467 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
468 (action->any.type != XkbSA_LockGroup &&
469 (action->mods.flags & XkbSA_ClearLocks)) ?
471 (action->any.type != XkbSA_LockGroup &&
472 (action->mods.flags & XkbSA_LatchToLock)) ?
478 case XkbSA_LatchGroup:
479 case XkbSA_LockGroup:
480 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
481 (!(action->group.flags & XkbSA_GroupAbsolute) &&
482 action->group.group > 0) ? "+" : "",
483 (action->group.flags & XkbSA_GroupAbsolute) ?
484 action->group.group + 1 : action->group.group,
485 (action->any.type != XkbSA_LockGroup &&
486 (action->group.flags & XkbSA_ClearLocks)) ?
488 (action->any.type != XkbSA_LockGroup &&
489 (action->group.flags & XkbSA_LatchToLock)) ?
494 case XkbSA_Terminate:
495 write_buf(buf, "%s%s()%s", prefix, type, suffix);
499 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
500 (!(action->ptr.flags & XkbSA_MoveAbsoluteX) &&
501 action->ptr.x >= 0) ? "+" : "",
503 (!(action->ptr.flags & XkbSA_MoveAbsoluteY) &&
504 action->ptr.y >= 0) ? "+" : "",
506 (action->ptr.flags & XkbSA_NoAcceleration) ? ",!accel" : "",
510 case XkbSA_LockPtrBtn:
511 switch (action->btn.flags & (XkbSA_LockNoUnlock | XkbSA_LockNoLock)) {
512 case XkbSA_LockNoUnlock:
513 args = ",affect=lock";
516 case XkbSA_LockNoLock:
517 args = ",affect=unlock";
520 case XkbSA_LockNoLock | XkbSA_LockNoUnlock:
521 args = ",affect=neither";
525 args = ",affect=both";
529 write_buf(buf, "%s%s(button=", prefix, type);
530 if (action->btn.button > 0 && action->btn.button <= 5)
531 write_buf(buf, "%d", action->btn.button);
533 write_buf(buf, "default");
534 if (action->btn.count)
535 write_buf(buf, ",count=%d", action->btn.count);
537 write_buf(buf, "%s", args);
538 write_buf(buf, ")%s", suffix);
541 case XkbSA_SetPtrDflt:
542 write_buf(buf, "%s%s(", prefix, type);
543 if (action->dflt.affect == XkbSA_AffectDfltBtn)
544 write_buf(buf, "affect=button,button=%s%d",
545 (!(action->dflt.flags & XkbSA_DfltBtnAbsolute) &&
546 action->dflt.value >= 0) ? "+" : "",
548 write_buf(buf, ")%s", suffix);
551 case XkbSA_SwitchScreen:
552 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
553 (!(action->screen.flags & XkbSA_SwitchAbsolute) &&
554 action->screen.screen >= 0) ? "+" : "",
555 action->screen.screen,
556 (action->screen.flags & XkbSA_SwitchApplication) ? "!" : "",
560 /* Deprecated actions below here */
561 case XkbSA_SetControls:
562 case XkbSA_LockControls:
563 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
564 get_control_mask_text(action->ctrls.ctrls), suffix);
568 case XkbSA_ActionMessage:
569 case XkbSA_RedirectKey:
570 case XkbSA_DeviceBtn:
571 case XkbSA_LockDeviceBtn:
574 write_buf(buf, "%sNoAction()%s", prefix, suffix);
577 case XkbSA_XFree86Private:
580 "%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",
581 prefix, type, action->any.type, action->any.data[0],
582 action->any.data[1], action->any.data[2],
583 action->any.data[3], action->any.data[4],
584 action->any.data[5], action->any.data[6],
593 write_compat(struct xkb_keymap *keymap, struct buf *buf)
596 struct xkb_sym_interpret *interp;
598 if (keymap->compat_section_name)
599 write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
600 keymap->compat_section_name);
602 write_buf(buf, "\txkb_compatibility {\n\n");
604 write_vmods(keymap, buf);
606 write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
607 write_buf(buf, "\t\tinterpret.repeat= False;\n");
608 write_buf(buf, "\t\tinterpret.locking= False;\n");
610 darray_foreach(interp, keymap->sym_interpret) {
611 char keysym_name[64];
613 if (interp->sym == XKB_KEY_NoSymbol)
614 sprintf(keysym_name, "Any");
616 xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
618 write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
620 SIMatchText(interp->match),
621 get_mod_mask_text(keymap, interp->mods, 0));
623 if (interp->virtual_mod != XkbNoModifier) {
624 write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
625 keymap->vmod_names[interp->virtual_mod]);
628 if (interp->match & XkbSI_LevelOneOnly)
630 "\t\t\tuseModMapMods=level1;\n");
631 if (interp->flags & XkbSI_LockingKey)
632 write_buf(buf, "\t\t\tlocking= True;\n");
633 if (interp->flags & XkbSI_AutoRepeat)
634 write_buf(buf, "\t\t\trepeat= True;\n");
636 write_action(keymap, buf, &interp->act, "\t\t\taction= ", ";\n");
637 write_buf(buf, "\t\t};\n");
640 for (i = 0; i < XkbNumKbdGroups; i++) {
643 gc = &keymap->groups[i];
644 if (gc->real_mods == 0 && gc->vmods == 0)
646 write_buf(buf, "\t\tgroup %d = %s;\n", i + 1,
647 get_mod_mask_text(keymap, gc->real_mods, gc->vmods));
650 for (i = 0; i < XkbNumIndicators; i++) {
651 struct xkb_indicator_map *map = &keymap->indicators[i];
652 if (map->flags == 0 && map->which_groups == 0 &&
653 map->groups == 0 && map->which_mods == 0 &&
654 map->mods.real_mods == 0 && map->mods.vmods == 0 &&
657 write_indicator_map(keymap, buf, i);
660 write_buf(buf, "\t};\n\n");
666 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
667 struct xkb_key *key, xkb_group_index_t group)
669 const xkb_keysym_t *syms;
671 xkb_level_index_t level;
672 #define OUT_BUF_LEN 128
673 char out_buf[OUT_BUF_LEN];
675 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group); level++) {
677 write_buf(buf, ", ");
678 num_syms = xkb_key_get_syms_by_level(keymap, key, group, level,
681 write_buf(buf, "%15s", "NoSymbol");
683 else if (num_syms == 1) {
684 xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
685 write_buf(buf, "%15s", out_buf);
689 write_buf(buf, "{ ");
690 for (s = 0; s < num_syms; s++) {
692 write_buf(buf, ", ");
693 xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
694 write_buf(buf, "%15s", out_buf);
696 write_buf(buf, " }");
705 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
708 xkb_group_index_t group, tmp;
711 if (keymap->symbols_section_name)
712 write_buf(buf, "\txkb_symbols \"%s\" {\n\n",
713 keymap->symbols_section_name);
715 write_buf(buf, "\txkb_symbols {\n\n");
717 for (tmp = group = 0; group < XkbNumKbdGroups; group++) {
718 if (!keymap->group_names[group])
721 "\t\tname[group%d]=\"%s\";\n", group + 1,
722 keymap->group_names[group]);
726 write_buf(buf, "\n");
728 xkb_foreach_key(key, keymap) {
731 if (key->num_groups == 0)
734 write_buf(buf, "\t\tkey %6s {", KeyNameText(key->name));
736 if (key->explicit & XkbExplicitKeyTypesMask) {
737 bool multi_type = false;
738 int type = XkbKeyTypeIndex(key, 0);
742 for (group = 0; group < key->num_groups; group++) {
743 if (XkbKeyTypeIndex(key, group) != type) {
750 for (group = 0; group < key->num_groups; group++) {
751 if (!(key->explicit & (1 << group)))
753 type = XkbKeyTypeIndex(key, group);
754 write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
755 group + 1, keymap->types[type].name);
759 write_buf(buf, "\n\t\t\ttype= \"%s\",",
760 keymap->types[type].name);
764 if (key->explicit & XkbExplicitAutoRepeatMask) {
766 write_buf(buf, "\n\t\t\trepeat= Yes,");
768 write_buf(buf, "\n\t\t\trepeat= No,");
772 if (key->vmodmap && (key->explicit & XkbExplicitVModMapMask)) {
773 write_buf(buf, "\n\t\t\tvirtualMods= %s,",
774 get_mod_mask_text(keymap, 0, key->vmodmap));
777 switch (key->out_of_range_group_action) {
778 case XkbClampIntoRange:
779 write_buf(buf, "\n\t\t\tgroupsClamp,");
782 case XkbRedirectIntoRange:
783 write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
784 key->out_of_range_group_number + 1);
788 if (key->explicit & XkbExplicitInterpretMask)
789 showActions = XkbKeyHasActions(key);
793 if (key->num_groups > 1 || showActions)
797 write_buf(buf, "\t[ ");
798 if (!write_keysyms(keymap, buf, key, 0))
800 write_buf(buf, " ] };\n");
803 union xkb_action *acts;
806 acts = XkbKeyActionsPtr(keymap, key);
807 for (group = 0; group < key->num_groups; group++) {
810 write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
811 if (!write_keysyms(keymap, buf, key, group))
813 write_buf(buf, " ]");
815 write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
818 level < XkbKeyGroupWidth(keymap, key, group);
821 write_buf(buf, ", ");
822 write_action(keymap, buf, &acts[level], NULL, NULL);
824 write_buf(buf, " ]");
828 write_buf(buf, "\n\t\t};\n");
832 xkb_foreach_key(key, keymap) {
835 if (key->modmap == 0)
838 for (mod = 0; mod < XkbNumModifiers; mod++) {
839 if (!(key->modmap & (1 << mod)))
842 write_buf(buf, "\t\tmodifier_map %s { %s };\n",
843 ModIndexToName(mod), KeyNameText(key->name));
847 write_buf(buf, "\t};\n\n");
852 xkb_map_get_as_string(struct xkb_keymap *keymap)
855 struct buf buf = { NULL, 0, 0 };
857 ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
858 write_keycodes(keymap, &buf) &&
859 write_types(keymap, &buf) &&
860 write_compat(keymap, &buf) &&
861 write_symbols(keymap, &buf) &&
862 check_write_buf(&buf, "};\n"));
864 return (ok ? buf.buf : NULL);