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 < XKB_NUM_VIRTUAL_MODS; i++) {
139 if (!keymap->vmod_names[i])
142 write_buf(buf, "\t\tvirtual_modifiers ");
146 xkb_atom_text(keymap->ctx, keymap->vmod_names[i]));
151 write_buf(buf, ";\n\n");
156 #define GET_TEXT_BUF_SIZE 512
158 #define append_get_text(...) do { \
159 int _size = snprintf(ret, GET_TEXT_BUF_SIZE, __VA_ARGS__); \
160 if (_size >= GET_TEXT_BUF_SIZE) \
165 get_indicator_state_text(uint8_t which)
168 static char ret[GET_TEXT_BUF_SIZE];
170 memset(ret, 0, GET_TEXT_BUF_SIZE);
177 for (i = 0; which != 0; i++) {
180 if (!(which & (1 << i)))
184 name = LookupValue(modComponentMaskNames, (1 << i));
187 append_get_text("%s+%s", ret, name);
189 append_get_text("%s", name);
196 get_control_mask_text(uint32_t control_mask)
199 static char ret[GET_TEXT_BUF_SIZE];
200 const char *control_name;
202 memset(ret, 0, GET_TEXT_BUF_SIZE);
204 control_mask &= XkbAllBooleanCtrlsMask;
206 if (control_mask == 0) {
210 else if (control_mask == XkbAllBooleanCtrlsMask) {
215 for (i = 0; control_mask; i++) {
216 if (!(control_mask & (1 << i)))
219 control_mask &= ~(1 << i);
220 control_name = LookupValue(ctrlMaskNames, (1 << i));
223 append_get_text("%s+%s", ret, control_name);
225 append_get_text("%s", control_name);
232 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
235 struct xkb_key_alias *alias;
238 if (keymap->keycodes_section_name)
239 write_buf(buf, "\txkb_keycodes \"%s\" {\n",
240 keymap->keycodes_section_name);
242 write_buf(buf, "\txkb_keycodes {\n");
244 write_buf(buf, "\t\tminimum = %d;\n",
245 keymap->min_key_code);
246 write_buf(buf, "\t\tmaximum = %d;\n",
247 keymap->max_key_code);
249 xkb_foreach_key(key, keymap) {
250 if (key->name[0] == '\0')
253 write_buf(buf, "\t\t%6s = %d;\n",
254 KeyNameText(key->name), XkbKeyGetKeycode(keymap, key));
257 for (i = 0; i < XKB_NUM_INDICATORS; i++) {
258 if (keymap->indicators[i].name == XKB_ATOM_NONE)
260 write_buf(buf, "\t\tindicator %d = \"%s\";\n", i + 1,
261 xkb_atom_text(keymap->ctx, keymap->indicators[i].name));
265 darray_foreach(alias, keymap->key_aliases)
266 write_buf(buf, "\t\talias %6s = %6s;\n",
267 KeyNameText(alias->alias),
268 KeyNameText(alias->real));
270 write_buf(buf, "\t};\n\n");
275 write_types(struct xkb_keymap *keymap, struct buf *buf)
279 struct xkb_key_type *type;
280 struct xkb_kt_map_entry *entry;
282 if (keymap->types_section_name)
283 write_buf(buf, "\txkb_types \"%s\" {\n\n",
284 keymap->types_section_name);
286 write_buf(buf, "\txkb_types {\n\n");
288 write_vmods(keymap, buf);
290 for (i = 0; i < keymap->num_types; i++) {
291 type = &keymap->types[i];
293 write_buf(buf, "\t\ttype \"%s\" {\n",
294 xkb_atom_text(keymap->ctx, type->name));
295 write_buf(buf, "\t\t\tmodifiers= %s;\n",
296 VModMaskText(keymap, type->mods.mods));
298 for (j = 0; j < type->num_entries; j++) {
300 entry = &type->map[j];
303 * Printing level 1 entries is redundant, it's the default,
304 * unless there's preserve info.
306 if (entry->level == 0 && entry->preserve.mods == 0)
309 str = VModMaskText(keymap, entry->mods.mods);
310 write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
311 str, entry->level + 1);
313 if (entry->preserve.mods == 0)
316 write_buf(buf, "\t\t\tpreserve[%s]= ", str);
317 write_buf(buf, "%s;\n", VModMaskText(keymap, entry->preserve.mods));
320 if (type->level_names) {
321 for (n = 0; n < type->num_levels; n++) {
322 if (!type->level_names[n])
324 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
325 xkb_atom_text(keymap->ctx, type->level_names[n]));
328 write_buf(buf, "\t\t};\n");
331 write_buf(buf, "\t};\n\n");
336 write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
338 struct xkb_indicator_map *led = &keymap->indicators[num];
340 write_buf(buf, "\t\tindicator \"%s\" {\n",
341 xkb_atom_text(keymap->ctx, keymap->indicators[num].name));
343 if (led->which_groups) {
344 if (led->which_groups != XKB_STATE_EFFECTIVE) {
345 write_buf(buf, "\t\t\twhichGroupState= %s;\n",
346 get_indicator_state_text(led->which_groups));
348 write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
352 if (led->which_mods) {
353 if (led->which_mods != XKB_STATE_EFFECTIVE) {
354 write_buf(buf, "\t\t\twhichModState= %s;\n",
355 get_indicator_state_text(led->which_mods));
357 write_buf(buf, "\t\t\tmodifiers= %s;\n",
358 VModMaskText(keymap, led->mods.mods));
362 write_buf(buf, "\t\t\tcontrols= %s;\n",
363 get_control_mask_text(led->ctrls));
366 write_buf(buf, "\t\t};\n");
371 write_action(struct xkb_keymap *keymap, struct buf *buf,
372 union xkb_action *action, const char *prefix, const char *suffix)
375 const char *args = NULL;
382 type = ActionTypeText(action->type);
384 switch (action->type) {
385 case ACTION_TYPE_MOD_SET:
386 case ACTION_TYPE_MOD_LATCH:
387 case ACTION_TYPE_MOD_LOCK:
388 if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
391 args = VModMaskText(keymap, action->mods.mods.mods);
392 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
393 (action->type != ACTION_TYPE_MOD_LOCK &&
394 (action->mods.flags & ACTION_LOCK_CLEAR)) ?
396 (action->type != ACTION_TYPE_MOD_LOCK &&
397 (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
402 case ACTION_TYPE_GROUP_SET:
403 case ACTION_TYPE_GROUP_LATCH:
404 case ACTION_TYPE_GROUP_LOCK:
405 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
406 (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
407 action->group.group > 0) ? "+" : "",
408 (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
409 action->group.group + 1 : action->group.group,
410 (action->type != ACTION_TYPE_GROUP_LOCK &&
411 (action->group.flags & ACTION_LOCK_CLEAR)) ?
413 (action->type != ACTION_TYPE_GROUP_LOCK &&
414 (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
419 case ACTION_TYPE_TERMINATE:
420 write_buf(buf, "%s%s()%s", prefix, type, suffix);
423 case ACTION_TYPE_PTR_MOVE:
424 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
425 (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
426 action->ptr.x >= 0) ? "+" : "",
428 (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
429 action->ptr.y >= 0) ? "+" : "",
431 (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
435 case ACTION_TYPE_PTR_LOCK:
436 switch (action->btn.flags &
437 (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
438 case ACTION_LOCK_NO_UNLOCK:
439 args = ",affect=lock";
442 case ACTION_LOCK_NO_LOCK:
443 args = ",affect=unlock";
446 case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
447 args = ",affect=neither";
451 args = ",affect=both";
454 case ACTION_TYPE_PTR_BUTTON:
455 write_buf(buf, "%s%s(button=", prefix, type);
456 if (action->btn.button > 0 && action->btn.button <= 5)
457 write_buf(buf, "%d", action->btn.button);
459 write_buf(buf, "default");
460 if (action->btn.count)
461 write_buf(buf, ",count=%d", action->btn.count);
463 write_buf(buf, "%s", args);
464 write_buf(buf, ")%s", suffix);
467 case ACTION_TYPE_PTR_DEFAULT:
468 write_buf(buf, "%s%s(", prefix, type);
469 write_buf(buf, "affect=button,button=%s%d",
470 (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
471 action->dflt.value >= 0) ? "+" : "",
473 write_buf(buf, ")%s", suffix);
476 case ACTION_TYPE_SWITCH_VT:
477 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
478 (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
479 action->screen.screen >= 0) ? "+" : "",
480 action->screen.screen,
481 (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
485 case ACTION_TYPE_CTRL_SET:
486 case ACTION_TYPE_CTRL_LOCK:
487 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
488 get_control_mask_text(action->ctrls.ctrls), suffix);
491 case ACTION_TYPE_NONE:
492 write_buf(buf, "%sNoAction()%s", prefix, suffix);
497 "%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",
498 prefix, type, action->type, action->priv.data[0],
499 action->priv.data[1], action->priv.data[2],
500 action->priv.data[3], action->priv.data[4],
501 action->priv.data[5], action->priv.data[6],
510 write_compat(struct xkb_keymap *keymap, struct buf *buf)
513 struct xkb_sym_interpret *interp;
515 if (keymap->compat_section_name)
516 write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
517 keymap->compat_section_name);
519 write_buf(buf, "\txkb_compatibility {\n\n");
521 write_vmods(keymap, buf);
523 write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
524 write_buf(buf, "\t\tinterpret.repeat= False;\n");
526 darray_foreach(interp, keymap->sym_interpret) {
527 char keysym_name[64];
529 if (interp->sym == XKB_KEY_NoSymbol)
530 sprintf(keysym_name, "Any");
532 xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
534 write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
536 SIMatchText(interp->match),
537 VModMaskText(keymap, interp->mods));
539 if (interp->virtual_mod != XKB_MOD_INVALID) {
540 write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
541 xkb_atom_text(keymap->ctx,
542 keymap->vmod_names[interp->virtual_mod]));
545 if (interp->match & XkbSI_LevelOneOnly)
547 "\t\t\tuseModMapMods=level1;\n");
548 if (interp->flags & XkbSI_AutoRepeat)
549 write_buf(buf, "\t\t\trepeat= True;\n");
551 write_action(keymap, buf, &interp->act, "\t\t\taction= ", ";\n");
552 write_buf(buf, "\t\t};\n");
555 for (i = 0; i < XKB_NUM_INDICATORS; i++) {
556 struct xkb_indicator_map *map = &keymap->indicators[i];
557 if (map->which_groups == 0 && map->groups == 0 &&
558 map->which_mods == 0 && map->mods.mods == 0 &&
561 write_indicator_map(keymap, buf, i);
564 write_buf(buf, "\t};\n\n");
570 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
571 struct xkb_key *key, xkb_group_index_t group)
573 const xkb_keysym_t *syms;
575 xkb_level_index_t level;
576 #define OUT_BUF_LEN 128
577 char out_buf[OUT_BUF_LEN];
579 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group); level++) {
581 write_buf(buf, ", ");
582 num_syms = xkb_key_get_syms_by_level(keymap, key, group, level,
585 write_buf(buf, "%15s", "NoSymbol");
587 else if (num_syms == 1) {
588 xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
589 write_buf(buf, "%15s", out_buf);
593 write_buf(buf, "{ ");
594 for (s = 0; s < num_syms; s++) {
596 write_buf(buf, ", ");
597 xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
598 write_buf(buf, "%15s", out_buf);
600 write_buf(buf, " }");
609 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
612 xkb_group_index_t group, tmp;
615 if (keymap->symbols_section_name)
616 write_buf(buf, "\txkb_symbols \"%s\" {\n\n",
617 keymap->symbols_section_name);
619 write_buf(buf, "\txkb_symbols {\n\n");
621 for (tmp = group = 0; group < XKB_NUM_GROUPS; group++) {
622 if (!keymap->group_names[group])
625 "\t\tname[group%d]=\"%s\";\n", group + 1,
626 xkb_atom_text(keymap->ctx, keymap->group_names[group]));
630 write_buf(buf, "\n");
632 xkb_foreach_key(key, keymap) {
635 if (key->num_groups == 0)
638 write_buf(buf, "\t\tkey %6s {", KeyNameText(key->name));
640 if (key->explicit & XkbExplicitKeyTypesMask) {
641 bool multi_type = false;
642 struct xkb_key_type *type = XkbKeyType(keymap, key, 0);
646 for (group = 1; group < key->num_groups; group++) {
647 if (XkbKeyType(keymap, key, group) != type) {
654 for (group = 0; group < key->num_groups; group++) {
655 if (!(key->explicit & (1 << group)))
657 type = XkbKeyType(keymap, key, group);
658 write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
660 xkb_atom_text(keymap->ctx, type->name));
664 write_buf(buf, "\n\t\t\ttype= \"%s\",",
665 xkb_atom_text(keymap->ctx, type->name));
669 if (key->explicit & XkbExplicitAutoRepeatMask) {
671 write_buf(buf, "\n\t\t\trepeat= Yes,");
673 write_buf(buf, "\n\t\t\trepeat= No,");
677 if (key->vmodmap && (key->explicit & XkbExplicitVModMapMask)) {
678 /* XXX: vmodmap cmask? */
679 write_buf(buf, "\n\t\t\tvirtualMods= %s,",
680 VModMaskText(keymap, key->vmodmap << XKB_NUM_CORE_MODS));
683 switch (key->out_of_range_group_action) {
684 case XkbClampIntoRange:
685 write_buf(buf, "\n\t\t\tgroupsClamp,");
688 case XkbRedirectIntoRange:
689 write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
690 key->out_of_range_group_number + 1);
694 if (key->explicit & XkbExplicitInterpretMask)
695 showActions = (key->actions != NULL);
699 if (key->num_groups > 1 || showActions)
703 write_buf(buf, "\t[ ");
704 if (!write_keysyms(keymap, buf, key, 0))
706 write_buf(buf, " ] };\n");
709 xkb_level_index_t level;
711 for (group = 0; group < key->num_groups; group++) {
714 write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
715 if (!write_keysyms(keymap, buf, key, group))
717 write_buf(buf, " ]");
719 write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
722 level < XkbKeyGroupWidth(keymap, key, group);
725 write_buf(buf, ", ");
726 write_action(keymap, buf,
727 XkbKeyActionEntry(key, group, level),
730 write_buf(buf, " ]");
733 write_buf(buf, "\n\t\t};\n");
737 xkb_foreach_key(key, keymap) {
740 if (key->modmap == 0)
743 for (mod = 0; mod < XKB_NUM_CORE_MODS; mod++) {
744 if (!(key->modmap & (1 << mod)))
747 write_buf(buf, "\t\tmodifier_map %s { %s };\n",
748 ModIndexToName(mod), KeyNameText(key->name));
752 write_buf(buf, "\t};\n\n");
757 xkb_map_get_as_string(struct xkb_keymap *keymap)
760 struct buf buf = { NULL, 0, 0 };
762 ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
763 write_keycodes(keymap, &buf) &&
764 write_types(keymap, &buf) &&
765 write_compat(keymap, &buf) &&
766 write_symbols(keymap, &buf) &&
767 check_write_buf(&buf, "};\n"));
769 return (ok ? buf.buf : NULL);