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>
52 #include "xkbcomp-priv.h"
55 #define BUF_CHUNK_SIZE 4096
64 do_realloc(struct buf *buf, size_t at_least)
68 buf->alloc += BUF_CHUNK_SIZE;
69 if (at_least >= BUF_CHUNK_SIZE)
70 buf->alloc += at_least;
72 new = realloc(buf->buf, buf->alloc);
80 ATTR_PRINTF(2, 3) static bool
81 check_write_buf(struct buf *buf, const char *fmt, ...)
87 available = buf->alloc - buf->size;
89 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
95 if (printed >= available)
96 if (!do_realloc(buf, printed))
99 /* The buffer has enough space now. */
101 available = buf->alloc - buf->size;
103 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
106 if (printed >= available || printed < 0)
109 buf->size += printed;
118 #define write_buf(buf, ...) do { \
119 if (!check_write_buf(buf, __VA_ARGS__)) \
124 write_vmods(struct xkb_keymap *keymap, struct buf *buf)
126 const struct xkb_mod *mod;
127 xkb_mod_index_t num_vmods = 0;
129 darray_foreach(mod, keymap->mods) {
130 if (mod->type != MOD_VIRT)
134 write_buf(buf, "\tvirtual_modifiers ");
137 write_buf(buf, "%s", xkb_atom_text(keymap->ctx, mod->name));
142 write_buf(buf, ";\n\n");
148 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
150 const struct xkb_key *key;
152 const struct xkb_led *led;
154 if (keymap->keycodes_section_name)
155 write_buf(buf, "xkb_keycodes \"%s\" {\n",
156 keymap->keycodes_section_name);
158 write_buf(buf, "xkb_keycodes {\n");
160 xkb_foreach_key(key, keymap) {
161 if (key->name == XKB_ATOM_NONE)
164 write_buf(buf, "\t%-20s = %d;\n",
165 KeyNameText(keymap->ctx, key->name), key->keycode);
168 darray_enumerate(idx, led, keymap->leds)
169 if (led->name != XKB_ATOM_NONE)
170 write_buf(buf, "\tindicator %d = \"%s\";\n",
171 idx + 1, xkb_atom_text(keymap->ctx, led->name));
174 for (unsigned i = 0; i < keymap->num_key_aliases; i++)
175 write_buf(buf, "\talias %-14s = %s;\n",
176 KeyNameText(keymap->ctx, keymap->key_aliases[i].alias),
177 KeyNameText(keymap->ctx, keymap->key_aliases[i].real));
179 write_buf(buf, "};\n\n");
184 write_types(struct xkb_keymap *keymap, struct buf *buf)
186 if (keymap->types_section_name)
187 write_buf(buf, "xkb_types \"%s\" {\n",
188 keymap->types_section_name);
190 write_buf(buf, "xkb_types {\n");
192 write_vmods(keymap, buf);
194 for (unsigned i = 0; i < keymap->num_types; i++) {
195 const struct xkb_key_type *type = &keymap->types[i];
197 write_buf(buf, "\ttype \"%s\" {\n",
198 xkb_atom_text(keymap->ctx, type->name));
200 write_buf(buf, "\t\tmodifiers= %s;\n",
201 ModMaskText(keymap, type->mods.mods));
203 for (unsigned j = 0; j < type->num_entries; j++) {
205 const struct xkb_key_type_entry *entry = &type->entries[j];
208 * Printing level 1 entries is redundant, it's the default,
209 * unless there's preserve info.
211 if (entry->level == 0 && entry->preserve.mods == 0)
214 str = ModMaskText(keymap, entry->mods.mods);
215 write_buf(buf, "\t\tmap[%s]= Level%d;\n",
216 str, entry->level + 1);
218 if (entry->preserve.mods)
219 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
220 str, ModMaskText(keymap, entry->preserve.mods));
223 for (xkb_level_index_t n = 0; n < type->num_levels; n++)
224 if (type->level_names[n])
225 write_buf(buf, "\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
226 xkb_atom_text(keymap->ctx, type->level_names[n]));
228 write_buf(buf, "\t};\n");
231 write_buf(buf, "};\n\n");
236 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
237 const struct xkb_led *led)
239 write_buf(buf, "\tindicator \"%s\" {\n",
240 xkb_atom_text(keymap->ctx, led->name));
242 if (led->which_groups) {
243 if (led->which_groups != XKB_STATE_LAYOUT_EFFECTIVE) {
244 write_buf(buf, "\t\twhichGroupState= %s;\n",
245 LedStateMaskText(keymap->ctx, led->which_groups));
247 write_buf(buf, "\t\tgroups= 0x%02x;\n",
251 if (led->which_mods) {
252 if (led->which_mods != XKB_STATE_MODS_EFFECTIVE) {
253 write_buf(buf, "\t\twhichModState= %s;\n",
254 LedStateMaskText(keymap->ctx, led->which_mods));
256 write_buf(buf, "\t\tmodifiers= %s;\n",
257 ModMaskText(keymap, led->mods.mods));
261 write_buf(buf, "\t\tcontrols= %s;\n",
262 ControlMaskText(keymap->ctx, led->ctrls));
265 write_buf(buf, "\t};\n");
270 write_action(struct xkb_keymap *keymap, struct buf *buf,
271 const union xkb_action *action,
272 const char *prefix, const char *suffix)
275 const char *args = NULL;
282 type = ActionTypeText(action->type);
284 switch (action->type) {
285 case ACTION_TYPE_MOD_SET:
286 case ACTION_TYPE_MOD_LATCH:
287 case ACTION_TYPE_MOD_LOCK:
288 if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
291 args = ModMaskText(keymap, action->mods.mods.mods);
292 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
293 (action->type != ACTION_TYPE_MOD_LOCK &&
294 (action->mods.flags & ACTION_LOCK_CLEAR)) ?
296 (action->type != ACTION_TYPE_MOD_LOCK &&
297 (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
302 case ACTION_TYPE_GROUP_SET:
303 case ACTION_TYPE_GROUP_LATCH:
304 case ACTION_TYPE_GROUP_LOCK:
305 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
306 (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
307 action->group.group > 0) ? "+" : "",
308 (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
309 action->group.group + 1 : action->group.group,
310 (action->type != ACTION_TYPE_GROUP_LOCK &&
311 (action->group.flags & ACTION_LOCK_CLEAR)) ?
313 (action->type != ACTION_TYPE_GROUP_LOCK &&
314 (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
319 case ACTION_TYPE_TERMINATE:
320 write_buf(buf, "%s%s()%s", prefix, type, suffix);
323 case ACTION_TYPE_PTR_MOVE:
324 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
325 (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
326 action->ptr.x >= 0) ? "+" : "",
328 (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
329 action->ptr.y >= 0) ? "+" : "",
331 (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
335 case ACTION_TYPE_PTR_LOCK:
336 switch (action->btn.flags &
337 (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
338 case ACTION_LOCK_NO_UNLOCK:
339 args = ",affect=lock";
342 case ACTION_LOCK_NO_LOCK:
343 args = ",affect=unlock";
346 case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
347 args = ",affect=neither";
351 args = ",affect=both";
354 case ACTION_TYPE_PTR_BUTTON:
355 write_buf(buf, "%s%s(button=", prefix, type);
356 if (action->btn.button > 0 && action->btn.button <= 5)
357 write_buf(buf, "%d", action->btn.button);
359 write_buf(buf, "default");
360 if (action->btn.count)
361 write_buf(buf, ",count=%d", action->btn.count);
363 write_buf(buf, "%s", args);
364 write_buf(buf, ")%s", suffix);
367 case ACTION_TYPE_PTR_DEFAULT:
368 write_buf(buf, "%s%s(", prefix, type);
369 write_buf(buf, "affect=button,button=%s%d",
370 (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
371 action->dflt.value >= 0) ? "+" : "",
373 write_buf(buf, ")%s", suffix);
376 case ACTION_TYPE_SWITCH_VT:
377 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
378 (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
379 action->screen.screen >= 0) ? "+" : "",
380 action->screen.screen,
381 (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
385 case ACTION_TYPE_CTRL_SET:
386 case ACTION_TYPE_CTRL_LOCK:
387 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
388 ControlMaskText(keymap->ctx, action->ctrls.ctrls), suffix);
391 case ACTION_TYPE_NONE:
392 write_buf(buf, "%sNoAction()%s", prefix, suffix);
397 "%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",
398 prefix, type, action->type, action->priv.data[0],
399 action->priv.data[1], action->priv.data[2],
400 action->priv.data[3], action->priv.data[4],
401 action->priv.data[5], action->priv.data[6],
410 write_compat(struct xkb_keymap *keymap, struct buf *buf)
412 const struct xkb_sym_interpret *si;
413 const struct xkb_led *led;
415 if (keymap->compat_section_name)
416 write_buf(buf, "xkb_compatibility \"%s\" {\n",
417 keymap->compat_section_name);
419 write_buf(buf, "xkb_compatibility {\n");
421 write_vmods(keymap, buf);
423 write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
424 write_buf(buf, "\tinterpret.repeat= False;\n");
426 darray_foreach(si, keymap->sym_interprets) {
427 write_buf(buf, "\tinterpret %s+%s(%s) {\n",
428 si->sym ? KeysymText(keymap->ctx, si->sym) : "Any",
429 SIMatchText(si->match),
430 ModMaskText(keymap, si->mods));
432 if (si->virtual_mod != XKB_MOD_INVALID)
433 write_buf(buf, "\t\tvirtualModifier= %s;\n",
434 ModIndexText(keymap, si->virtual_mod));
436 if (si->level_one_only)
437 write_buf(buf, "\t\tuseModMapMods=level1;\n");
440 write_buf(buf, "\t\trepeat= True;\n");
442 write_action(keymap, buf, &si->action, "\t\taction= ", ";\n");
443 write_buf(buf, "\t};\n");
446 darray_foreach(led, keymap->leds)
447 if (led->which_groups || led->groups || led->which_mods ||
448 led->mods.mods || led->ctrls)
449 write_led_map(keymap, buf, led);
451 write_buf(buf, "};\n\n");
457 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
458 const struct xkb_key *key, xkb_layout_index_t group)
460 for (xkb_level_index_t level = 0; level < XkbKeyGroupWidth(key, group);
462 const xkb_keysym_t *syms;
466 write_buf(buf, ", ");
468 num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
469 group, level, &syms);
471 write_buf(buf, "%15s", "NoSymbol");
473 else if (num_syms == 1) {
474 write_buf(buf, "%15s", KeysymText(keymap->ctx, syms[0]));
477 write_buf(buf, "{ ");
478 for (int s = 0; s < num_syms; s++) {
480 write_buf(buf, ", ");
481 write_buf(buf, "%s", KeysymText(keymap->ctx, syms[s]));
483 write_buf(buf, " }");
491 write_key(struct xkb_keymap *keymap, struct buf *buf,
492 const struct xkb_key *key)
494 xkb_layout_index_t group;
496 bool explicit_types = false;
497 bool multi_type = false;
500 write_buf(buf, "\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
502 for (group = 0; group < key->num_groups; group++) {
503 if (key->groups[group].explicit_type)
504 explicit_types = true;
506 if (group != 0 && key->groups[group].type != key->groups[0].type)
510 if (explicit_types) {
511 const struct xkb_key_type *type;
515 for (group = 0; group < key->num_groups; group++) {
516 if (!key->groups[group].explicit_type)
519 type = key->groups[group].type;
520 write_buf(buf, "\n\t\ttype[group%u]= \"%s\",",
522 xkb_atom_text(keymap->ctx, type->name));
526 type = key->groups[0].type;
527 write_buf(buf, "\n\t\ttype= \"%s\",",
528 xkb_atom_text(keymap->ctx, type->name));
532 if (key->explicit & EXPLICIT_REPEAT) {
534 write_buf(buf, "\n\t\trepeat= Yes,");
536 write_buf(buf, "\n\t\trepeat= No,");
540 if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
541 write_buf(buf, "\n\t\tvirtualMods= %s,",
542 ModMaskText(keymap, key->vmodmap));
544 switch (key->out_of_range_group_action) {
546 write_buf(buf, "\n\t\tgroupsClamp,");
550 write_buf(buf, "\n\t\tgroupsRedirect= Group%u,",
551 key->out_of_range_group_number + 1);
558 show_actions = !!(key->explicit & EXPLICIT_INTERP);
560 if (key->num_groups > 1 || show_actions)
564 write_buf(buf, "\t[ ");
565 if (!write_keysyms(keymap, buf, key, 0))
567 write_buf(buf, " ] };\n");
570 xkb_level_index_t level;
572 for (group = 0; group < key->num_groups; group++) {
575 write_buf(buf, "\n\t\tsymbols[Group%u]= [ ", group + 1);
576 if (!write_keysyms(keymap, buf, key, group))
578 write_buf(buf, " ]");
580 write_buf(buf, ",\n\t\tactions[Group%u]= [ ", group + 1);
582 level < XkbKeyGroupWidth(key, group); level++) {
584 write_buf(buf, ", ");
585 write_action(keymap, buf,
586 &key->groups[group].levels[level].action,
589 write_buf(buf, " ]");
592 write_buf(buf, "\n\t};\n");
599 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
601 const struct xkb_key *key;
602 xkb_layout_index_t group;
604 if (keymap->symbols_section_name)
605 write_buf(buf, "xkb_symbols \"%s\" {\n",
606 keymap->symbols_section_name);
608 write_buf(buf, "xkb_symbols {\n");
610 for (group = 0; group < keymap->num_group_names; group++)
611 if (keymap->group_names[group])
613 "\tname[group%d]=\"%s\";\n", group + 1,
614 xkb_atom_text(keymap->ctx, keymap->group_names[group]));
616 write_buf(buf, "\n");
618 xkb_foreach_key(key, keymap)
619 if (key->num_groups > 0)
620 write_key(keymap, buf, key);
622 xkb_foreach_key(key, keymap) {
624 const struct xkb_mod *mod;
626 if (key->modmap == 0)
629 darray_enumerate(i, mod, keymap->mods)
630 if (key->modmap & (1 << i))
631 write_buf(buf, "\tmodifier_map %s { %s };\n",
632 xkb_atom_text(keymap->ctx, mod->name),
633 KeyNameText(keymap->ctx, key->name));
636 write_buf(buf, "};\n\n");
641 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
643 return (check_write_buf(buf, "xkb_keymap {\n") &&
644 write_keycodes(keymap, buf) &&
645 write_types(keymap, buf) &&
646 write_compat(keymap, buf) &&
647 write_symbols(keymap, buf) &&
648 check_write_buf(buf, "};\n"));
652 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
654 struct buf buf = { NULL, 0, 0 };
656 if (!write_keymap(keymap, &buf)) {