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) \
163 /* FIXME: Merge with src/xkbcomp/expr.c::modIndexNames. */
164 static const char *core_mod_names[] = {
176 get_mod_index_text(uint8_t real_mod)
178 return core_mod_names[real_mod];
182 get_mod_mask_text(struct xkb_keymap *keymap, uint8_t real_mods,
185 static char ret[GET_TEXT_BUF_SIZE], ret2[GET_TEXT_BUF_SIZE];
188 memset(ret, 0, GET_TEXT_BUF_SIZE);
190 if (real_mods == 0 && vmods == 0) {
195 /* This is so broken. If we have a real modmask of 0xff and a
196 * vmodmask, we'll get, e.g., all+RightControl. But, it's what xkbfile
198 if (real_mods == 0xff) {
201 else if (real_mods) {
202 for (i = 0; i < XkbNumModifiers; i++) {
203 if (!(real_mods & (1 << i)))
205 if (ret[0] != '\0') {
207 append_get_text("%s+%s", ret2, core_mod_names[i]);
210 append_get_text("%s", core_mod_names[i]);
218 for (i = 0; i < XkbNumVirtualMods; i++) {
219 if (!(vmods & (1 << i)))
221 if (ret[0] != '\0') {
223 append_get_text("%s+%s", ret2, keymap->vmod_names[i]);
226 append_get_text("%s", keymap->vmod_names[i]);
234 get_indicator_state_text(uint8_t which)
237 static char ret[GET_TEXT_BUF_SIZE];
238 /* FIXME: Merge with ... something ... in xkbcomp? */
239 static const char *state_names[] = {
247 memset(ret, 0, GET_TEXT_BUF_SIZE);
249 which &= XkbIM_UseAnyMods;
256 for (i = 0; which != 0; i++) {
257 if (!(which & (1 << i)))
262 append_get_text("%s+%s", ret, state_names[i]);
264 append_get_text("%s", state_names[i]);
271 get_control_mask_text(uint32_t control_mask)
274 static char ret[GET_TEXT_BUF_SIZE];
275 /* FIXME: Merge with ... something ... in xkbcomp. */
276 static const char *ctrl_names[] = {
292 memset(ret, 0, GET_TEXT_BUF_SIZE);
294 control_mask &= XkbAllBooleanCtrlsMask;
296 if (control_mask == 0) {
300 else if (control_mask == XkbAllBooleanCtrlsMask) {
305 for (i = 0; control_mask; i++) {
306 if (!(control_mask & (1 << i)))
308 control_mask &= ~(1 << i);
311 append_get_text("%s+%s", ret, ctrl_names[i]);
313 append_get_text("%s", ctrl_names[i]);
320 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
323 struct xkb_key_alias *alias;
326 if (keymap->keycodes_section_name)
327 write_buf(buf, "\txkb_keycodes \"%s\" {\n",
328 keymap->keycodes_section_name);
330 write_buf(buf, "\txkb_keycodes {\n");
332 write_buf(buf, "\t\tminimum = %d;\n",
333 keymap->min_key_code);
334 write_buf(buf, "\t\tmaximum = %d;\n",
335 keymap->max_key_code);
337 xkb_foreach_key(key, keymap) {
338 if (key->name[0] == '\0')
341 write_buf(buf, "\t\t%6s = %d;\n",
342 KeyNameText(key->name), XkbKeyGetKeycode(keymap, key));
345 for (i = 0; i < XkbNumIndicators; i++) {
346 if (!keymap->indicator_names[i])
348 write_buf(buf, "\t\tindicator %d = \"%s\";\n",
349 i + 1, keymap->indicator_names[i]);
353 darray_foreach(alias, keymap->key_aliases)
354 write_buf(buf, "\t\talias %6s = %6s;\n",
355 KeyNameText(alias->alias),
356 KeyNameText(alias->real));
358 write_buf(buf, "\t};\n\n");
363 write_types(struct xkb_keymap *keymap, struct buf *buf)
366 struct xkb_key_type *type;
368 if (keymap->types_section_name)
369 write_buf(buf, "\txkb_types \"%s\" {\n\n",
370 keymap->types_section_name);
372 write_buf(buf, "\txkb_types {\n\n");
374 write_vmods(keymap, buf);
376 darray_foreach(type, keymap->types) {
377 write_buf(buf, "\t\ttype \"%s\" {\n",
379 write_buf(buf, "\t\t\tmodifiers= %s;\n",
380 get_mod_mask_text(keymap, type->mods.real_mods,
383 for (n = 0; n < darray_size(type->map); n++) {
384 struct xkb_kt_map_entry *entry = &darray_item(type->map, n);
387 str = get_mod_mask_text(keymap, entry->mods.real_mods,
389 write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
390 str, entry->level + 1);
392 if (!type->preserve || (!type->preserve[n].real_mods &&
393 !type->preserve[n].vmods))
395 write_buf(buf, "\t\t\tpreserve[%s]= ", str);
396 write_buf(buf, "%s;\n",
397 get_mod_mask_text(keymap, type->preserve[n].real_mods,
398 type->preserve[n].vmods));
401 if (type->level_names) {
402 for (n = 0; n < type->num_levels; n++) {
403 if (!type->level_names[n])
405 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
406 type->level_names[n]);
409 write_buf(buf, "\t\t};\n");
412 write_buf(buf, "\t};\n\n");
417 write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
419 struct xkb_indicator_map *led = &keymap->indicators[num];
421 write_buf(buf, "\t\tindicator \"%s\" {\n",
422 keymap->indicator_names[num]);
424 if (led->which_groups) {
425 if (led->which_groups != XkbIM_UseEffective) {
426 write_buf(buf, "\t\t\twhichGroupState= %s;\n",
427 get_indicator_state_text(led->which_groups));
429 write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
433 if (led->which_mods) {
434 if (led->which_mods != XkbIM_UseEffective) {
435 write_buf(buf, "\t\t\twhichModState= %s;\n",
436 get_indicator_state_text(led->which_mods));
438 write_buf(buf, "\t\t\tmodifiers= %s;\n",
439 get_mod_mask_text(keymap, led->mods.real_mods,
444 write_buf(buf, "\t\t\tcontrols= %s;\n",
445 get_control_mask_text(led->ctrls));
448 write_buf(buf, "\t\t};\n");
453 write_action(struct xkb_keymap *keymap, struct buf *buf,
454 union xkb_action *action, const char *prefix, const char *suffix)
457 const char *args = NULL;
464 type = ActionTypeText(action->any.type);
466 switch (action->any.type) {
468 case XkbSA_LatchMods:
470 if (action->mods.flags & XkbSA_UseModMapMods)
473 args = get_mod_mask_text(keymap, action->mods.real_mods,
475 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
476 (action->any.type != XkbSA_LockGroup &&
477 (action->mods.flags & XkbSA_ClearLocks)) ?
479 (action->any.type != XkbSA_LockGroup &&
480 (action->mods.flags & XkbSA_LatchToLock)) ?
486 case XkbSA_LatchGroup:
487 case XkbSA_LockGroup:
488 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
489 (!(action->group.flags & XkbSA_GroupAbsolute) &&
490 action->group.group > 0) ? "+" : "",
491 (action->group.flags & XkbSA_GroupAbsolute) ?
492 action->group.group + 1 : action->group.group,
493 (action->any.type != XkbSA_LockGroup &&
494 (action->group.flags & XkbSA_ClearLocks)) ?
496 (action->any.type != XkbSA_LockGroup &&
497 (action->group.flags & XkbSA_LatchToLock)) ?
502 case XkbSA_Terminate:
503 write_buf(buf, "%s%s()%s", prefix, type, suffix);
507 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
508 (!(action->ptr.flags & XkbSA_MoveAbsoluteX) &&
509 action->ptr.x >= 0) ? "+" : "",
511 (!(action->ptr.flags & XkbSA_MoveAbsoluteY) &&
512 action->ptr.y >= 0) ? "+" : "",
514 (action->ptr.flags & XkbSA_NoAcceleration) ? ",!accel" : "",
518 case XkbSA_LockPtrBtn:
519 switch (action->btn.flags & (XkbSA_LockNoUnlock | XkbSA_LockNoLock)) {
520 case XkbSA_LockNoUnlock:
521 args = ",affect=lock";
524 case XkbSA_LockNoLock:
525 args = ",affect=unlock";
528 case XkbSA_LockNoLock | XkbSA_LockNoUnlock:
529 args = ",affect=neither";
533 args = ",affect=both";
537 write_buf(buf, "%s%s(button=", prefix, type);
538 if (action->btn.button > 0 && action->btn.button <= 5)
539 write_buf(buf, "%d", action->btn.button);
541 write_buf(buf, "default");
542 if (action->btn.count)
543 write_buf(buf, ",count=%d", action->btn.count);
545 write_buf(buf, "%s", args);
546 write_buf(buf, ")%s", suffix);
549 case XkbSA_SetPtrDflt:
550 write_buf(buf, "%s%s(", prefix, type);
551 if (action->dflt.affect == XkbSA_AffectDfltBtn)
552 write_buf(buf, "affect=button,button=%s%d",
553 (!(action->dflt.flags & XkbSA_DfltBtnAbsolute) &&
554 action->dflt.value >= 0) ? "+" : "",
556 write_buf(buf, ")%s", suffix);
559 case XkbSA_SwitchScreen:
560 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
561 (!(action->screen.flags & XkbSA_SwitchAbsolute) &&
562 action->screen.screen >= 0) ? "+" : "",
563 action->screen.screen,
564 (action->screen.flags & XkbSA_SwitchApplication) ? "!" : "",
568 /* Deprecated actions below here */
569 case XkbSA_SetControls:
570 case XkbSA_LockControls:
571 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
572 get_control_mask_text(action->ctrls.ctrls), suffix);
576 case XkbSA_ActionMessage:
577 case XkbSA_RedirectKey:
578 case XkbSA_DeviceBtn:
579 case XkbSA_LockDeviceBtn:
582 write_buf(buf, "%sNoAction()%s", prefix, suffix);
585 case XkbSA_XFree86Private:
588 "%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",
589 prefix, type, action->any.type, action->any.data[0],
590 action->any.data[1], action->any.data[2],
591 action->any.data[3], action->any.data[4],
592 action->any.data[5], action->any.data[6],
601 write_compat(struct xkb_keymap *keymap, struct buf *buf)
604 struct xkb_sym_interpret *interp;
606 if (keymap->compat_section_name)
607 write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
608 keymap->compat_section_name);
610 write_buf(buf, "\txkb_compatibility {\n\n");
612 write_vmods(keymap, buf);
614 write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
615 write_buf(buf, "\t\tinterpret.repeat= False;\n");
616 write_buf(buf, "\t\tinterpret.locking= False;\n");
618 darray_foreach(interp, keymap->sym_interpret) {
619 char keysym_name[64];
621 if (interp->sym == XKB_KEY_NoSymbol)
622 sprintf(keysym_name, "Any");
624 xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
626 write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
628 SIMatchText(interp->match),
629 get_mod_mask_text(keymap, interp->mods, 0));
631 if (interp->virtual_mod != XkbNoModifier) {
632 write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
633 keymap->vmod_names[interp->virtual_mod]);
636 if (interp->match & XkbSI_LevelOneOnly)
638 "\t\t\tuseModMapMods=level1;\n");
639 if (interp->flags & XkbSI_LockingKey)
640 write_buf(buf, "\t\t\tlocking= True;\n");
641 if (interp->flags & XkbSI_AutoRepeat)
642 write_buf(buf, "\t\t\trepeat= True;\n");
644 write_action(keymap, buf, &interp->act, "\t\t\taction= ", ";\n");
645 write_buf(buf, "\t\t};\n");
648 for (i = 0; i < XkbNumKbdGroups; i++) {
651 gc = &keymap->groups[i];
652 if (gc->real_mods == 0 && gc->vmods == 0)
654 write_buf(buf, "\t\tgroup %d = %s;\n", i + 1,
655 get_mod_mask_text(keymap, gc->real_mods, gc->vmods));
658 for (i = 0; i < XkbNumIndicators; i++) {
659 struct xkb_indicator_map *map = &keymap->indicators[i];
660 if (map->flags == 0 && map->which_groups == 0 &&
661 map->groups == 0 && map->which_mods == 0 &&
662 map->mods.real_mods == 0 && map->mods.vmods == 0 &&
665 write_indicator_map(keymap, buf, i);
668 write_buf(buf, "\t};\n\n");
674 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
675 struct xkb_key *key, xkb_group_index_t group)
677 const xkb_keysym_t *syms;
679 #define OUT_BUF_LEN 128
680 char out_buf[OUT_BUF_LEN];
682 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group); level++) {
684 write_buf(buf, ", ");
685 num_syms = xkb_key_get_syms_by_level(keymap, key, group, level,
688 write_buf(buf, "%15s", "NoSymbol");
690 else if (num_syms == 1) {
691 xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
692 write_buf(buf, "%15s", out_buf);
696 write_buf(buf, "{ ");
697 for (s = 0; s < num_syms; s++) {
699 write_buf(buf, ", ");
700 xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
701 write_buf(buf, "%15s", out_buf);
703 write_buf(buf, " }");
712 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
715 xkb_group_index_t group, tmp;
718 if (keymap->symbols_section_name)
719 write_buf(buf, "\txkb_symbols \"%s\" {\n\n",
720 keymap->symbols_section_name);
722 write_buf(buf, "\txkb_symbols {\n\n");
724 for (tmp = group = 0; group < XkbNumKbdGroups; group++) {
725 if (!keymap->group_names[group])
728 "\t\tname[group%d]=\"%s\";\n", group + 1,
729 keymap->group_names[group]);
733 write_buf(buf, "\n");
735 xkb_foreach_key(key, keymap) {
738 if (key->num_groups == 0)
741 write_buf(buf, "\t\tkey %6s {", KeyNameText(key->name));
743 if (key->explicit & XkbExplicitKeyTypesMask) {
744 bool multi_type = false;
745 int type = XkbKeyTypeIndex(key, 0);
749 for (group = 0; group < key->num_groups; group++) {
750 if (XkbKeyTypeIndex(key, group) != type) {
757 for (group = 0; group < key->num_groups; group++) {
758 if (!(key->explicit & (1 << group)))
760 type = XkbKeyTypeIndex(key, group);
761 write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
763 darray_item(keymap->types, type).name);
767 write_buf(buf, "\n\t\t\ttype= \"%s\",",
768 darray_item(keymap->types, type).name);
772 if (key->explicit & XkbExplicitAutoRepeatMask) {
774 write_buf(buf, "\n\t\t\trepeat= Yes,");
776 write_buf(buf, "\n\t\t\trepeat= No,");
780 if (key->vmodmap && (key->explicit & XkbExplicitVModMapMask)) {
781 write_buf(buf, "\n\t\t\tvirtualMods= %s,",
782 get_mod_mask_text(keymap, 0, key->vmodmap));
785 switch (key->out_of_range_group_action) {
786 case XkbClampIntoRange:
787 write_buf(buf, "\n\t\t\tgroupsClamp,");
790 case XkbRedirectIntoRange:
791 write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
792 key->out_of_range_group_number + 1);
796 if (key->explicit & XkbExplicitInterpretMask)
797 showActions = XkbKeyHasActions(key);
801 if (key->num_groups > 1 || showActions)
805 write_buf(buf, "\t[ ");
806 if (!write_keysyms(keymap, buf, key, 0))
808 write_buf(buf, " ] };\n");
811 union xkb_action *acts;
814 acts = XkbKeyActionsPtr(keymap, key);
815 for (group = 0; group < key->num_groups; group++) {
818 write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
819 if (!write_keysyms(keymap, buf, key, group))
821 write_buf(buf, " ]");
823 write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
826 level < XkbKeyGroupWidth(keymap, key, group);
829 write_buf(buf, ", ");
830 write_action(keymap, buf, &acts[level], NULL, NULL);
832 write_buf(buf, " ]");
836 write_buf(buf, "\n\t\t};\n");
840 xkb_foreach_key(key, keymap) {
843 if (key->modmap == 0)
846 for (mod = 0; mod < XkbNumModifiers; mod++) {
847 if (!(key->modmap & (1 << mod)))
850 write_buf(buf, "\t\tmodifier_map %s { %s };\n",
851 get_mod_index_text(mod), KeyNameText(key->name));
855 write_buf(buf, "\t};\n\n");
860 xkb_map_get_as_string(struct xkb_keymap *keymap)
863 struct buf buf = { NULL, 0, 0 };
865 ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
866 write_keycodes(keymap, &buf) &&
867 write_types(keymap, &buf) &&
868 write_compat(keymap, &buf) &&
869 write_symbols(keymap, &buf) &&
870 check_write_buf(&buf, "};\n"));
872 return (ok ? buf.buf : NULL);