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>
54 #include "xkbcomp-priv.h"
57 #define BUF_CHUNK_SIZE 4096
66 do_realloc(struct buf *buf, size_t at_least)
70 buf->alloc += BUF_CHUNK_SIZE;
71 if (at_least >= BUF_CHUNK_SIZE)
72 buf->alloc += at_least;
74 new = realloc(buf->buf, buf->alloc);
82 ATTR_PRINTF(2, 3) static bool
83 check_write_buf(struct buf *buf, const char *fmt, ...)
89 available = buf->alloc - buf->size;
91 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
97 if ((size_t) printed >= available)
98 if (!do_realloc(buf, printed))
101 /* The buffer has enough space now. */
103 available = buf->alloc - buf->size;
105 printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
108 if (printed < 0 || (size_t) printed >= available)
111 buf->size += printed;
120 #define write_buf(buf, ...) do { \
121 if (!check_write_buf(buf, __VA_ARGS__)) \
126 write_vmods(struct xkb_keymap *keymap, struct buf *buf)
128 const struct xkb_mod *mod;
129 xkb_mod_index_t num_vmods = 0;
131 xkb_mods_foreach(mod, &keymap->mods) {
132 if (mod->type != MOD_VIRT)
136 write_buf(buf, "\tvirtual_modifiers ");
139 write_buf(buf, "%s", xkb_atom_text(keymap->ctx, mod->name));
144 write_buf(buf, ";\n\n");
150 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
152 const struct xkb_key *key;
154 const struct xkb_led *led;
156 if (keymap->keycodes_section_name)
157 write_buf(buf, "xkb_keycodes \"%s\" {\n",
158 keymap->keycodes_section_name);
160 write_buf(buf, "xkb_keycodes {\n");
162 /* xkbcomp and X11 really want to see keymaps with a minimum of 8, and
163 * a maximum of at least 255, else XWayland really starts hating life.
164 * If this is a problem and people really need strictly bounded keymaps,
165 * we should probably control this with a flag. */
166 write_buf(buf, "\tminimum = %u;\n", MIN(keymap->min_key_code, 8));
167 write_buf(buf, "\tmaximum = %u;\n", MAX(keymap->max_key_code, 255));
169 xkb_keys_foreach(key, keymap) {
170 if (key->name == XKB_ATOM_NONE)
173 write_buf(buf, "\t%-20s = %u;\n",
174 KeyNameText(keymap->ctx, key->name), key->keycode);
177 xkb_leds_enumerate(idx, led, keymap)
178 if (led->name != XKB_ATOM_NONE)
179 write_buf(buf, "\tindicator %u = \"%s\";\n",
180 idx + 1, xkb_atom_text(keymap->ctx, led->name));
183 for (unsigned i = 0; i < keymap->num_key_aliases; i++)
184 write_buf(buf, "\talias %-14s = %s;\n",
185 KeyNameText(keymap->ctx, keymap->key_aliases[i].alias),
186 KeyNameText(keymap->ctx, keymap->key_aliases[i].real));
188 write_buf(buf, "};\n\n");
193 write_types(struct xkb_keymap *keymap, struct buf *buf)
195 if (keymap->types_section_name)
196 write_buf(buf, "xkb_types \"%s\" {\n",
197 keymap->types_section_name);
199 write_buf(buf, "xkb_types {\n");
201 write_vmods(keymap, buf);
203 for (unsigned i = 0; i < keymap->num_types; i++) {
204 const struct xkb_key_type *type = &keymap->types[i];
206 write_buf(buf, "\ttype \"%s\" {\n",
207 xkb_atom_text(keymap->ctx, type->name));
209 write_buf(buf, "\t\tmodifiers= %s;\n",
210 ModMaskText(keymap->ctx, &keymap->mods, type->mods.mods));
212 for (unsigned j = 0; j < type->num_entries; j++) {
214 const struct xkb_key_type_entry *entry = &type->entries[j];
217 * Printing level 1 entries is redundant, it's the default,
218 * unless there's preserve info.
220 if (entry->level == 0 && entry->preserve.mods == 0)
223 str = ModMaskText(keymap->ctx, &keymap->mods, entry->mods.mods);
224 write_buf(buf, "\t\tmap[%s]= %u;\n",
225 str, entry->level + 1);
227 if (entry->preserve.mods)
228 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
229 str, ModMaskText(keymap->ctx, &keymap->mods,
230 entry->preserve.mods));
233 for (xkb_level_index_t n = 0; n < type->num_level_names; n++)
234 if (type->level_names[n])
235 write_buf(buf, "\t\tlevel_name[%u]= \"%s\";\n", n + 1,
236 xkb_atom_text(keymap->ctx, type->level_names[n]));
238 write_buf(buf, "\t};\n");
241 write_buf(buf, "};\n\n");
246 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
247 const struct xkb_led *led)
249 write_buf(buf, "\tindicator \"%s\" {\n",
250 xkb_atom_text(keymap->ctx, led->name));
252 if (led->which_groups) {
253 if (led->which_groups != XKB_STATE_LAYOUT_EFFECTIVE) {
254 write_buf(buf, "\t\twhichGroupState= %s;\n",
255 LedStateMaskText(keymap->ctx, led->which_groups));
257 write_buf(buf, "\t\tgroups= 0x%02x;\n",
261 if (led->which_mods) {
262 if (led->which_mods != XKB_STATE_MODS_EFFECTIVE) {
263 write_buf(buf, "\t\twhichModState= %s;\n",
264 LedStateMaskText(keymap->ctx, led->which_mods));
266 write_buf(buf, "\t\tmodifiers= %s;\n",
267 ModMaskText(keymap->ctx, &keymap->mods, led->mods.mods));
271 write_buf(buf, "\t\tcontrols= %s;\n",
272 ControlMaskText(keymap->ctx, led->ctrls));
275 write_buf(buf, "\t};\n");
280 affect_lock_text(enum xkb_action_flags flags)
282 switch (flags & (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
283 case ACTION_LOCK_NO_UNLOCK:
284 return ",affect=lock";
285 case ACTION_LOCK_NO_LOCK:
286 return ",affect=unlock";
287 case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
288 return ",affect=neither";
294 write_action(struct xkb_keymap *keymap, struct buf *buf,
295 const union xkb_action *action,
296 const char *prefix, const char *suffix)
299 const char *args = NULL;
306 type = ActionTypeText(action->type);
308 switch (action->type) {
309 case ACTION_TYPE_MOD_SET:
310 case ACTION_TYPE_MOD_LATCH:
311 case ACTION_TYPE_MOD_LOCK:
312 if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
315 args = ModMaskText(keymap->ctx, &keymap->mods,
316 action->mods.mods.mods);
317 write_buf(buf, "%s%s(modifiers=%s%s%s%s)%s", prefix, type, args,
318 (action->type != ACTION_TYPE_MOD_LOCK && (action->mods.flags & ACTION_LOCK_CLEAR)) ? ",clearLocks" : "",
319 (action->type != ACTION_TYPE_MOD_LOCK && (action->mods.flags & ACTION_LATCH_TO_LOCK)) ? ",latchToLock" : "",
320 (action->type == ACTION_TYPE_MOD_LOCK) ? affect_lock_text(action->mods.flags) : "",
324 case ACTION_TYPE_GROUP_SET:
325 case ACTION_TYPE_GROUP_LATCH:
326 case ACTION_TYPE_GROUP_LOCK:
327 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
328 (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) && action->group.group > 0) ? "+" : "",
329 (action->group.flags & ACTION_ABSOLUTE_SWITCH) ? action->group.group + 1 : action->group.group,
330 (action->type != ACTION_TYPE_GROUP_LOCK && (action->group.flags & ACTION_LOCK_CLEAR)) ? ",clearLocks" : "",
331 (action->type != ACTION_TYPE_GROUP_LOCK && (action->group.flags & ACTION_LATCH_TO_LOCK)) ? ",latchToLock" : "",
335 case ACTION_TYPE_TERMINATE:
336 write_buf(buf, "%s%s()%s", prefix, type, suffix);
339 case ACTION_TYPE_PTR_MOVE:
340 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
341 (!(action->ptr.flags & ACTION_ABSOLUTE_X) && action->ptr.x >= 0) ? "+" : "",
343 (!(action->ptr.flags & ACTION_ABSOLUTE_Y) && action->ptr.y >= 0) ? "+" : "",
345 (action->ptr.flags & ACTION_ACCEL) ? "" : ",!accel",
349 case ACTION_TYPE_PTR_LOCK:
350 args = affect_lock_text(action->btn.flags);
352 case ACTION_TYPE_PTR_BUTTON:
353 write_buf(buf, "%s%s(button=", prefix, type);
354 if (action->btn.button > 0 && action->btn.button <= 5)
355 write_buf(buf, "%d", action->btn.button);
357 write_buf(buf, "default");
358 if (action->btn.count)
359 write_buf(buf, ",count=%d", action->btn.count);
361 write_buf(buf, "%s", args);
362 write_buf(buf, ")%s", suffix);
365 case ACTION_TYPE_PTR_DEFAULT:
366 write_buf(buf, "%s%s(", prefix, type);
367 write_buf(buf, "affect=button,button=%s%d",
368 (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) && action->dflt.value >= 0) ? "+" : "",
370 write_buf(buf, ")%s", suffix);
373 case ACTION_TYPE_SWITCH_VT:
374 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
375 (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) && action->screen.screen >= 0) ? "+" : "",
376 action->screen.screen,
377 (action->screen.flags & ACTION_SAME_SCREEN) ? "" : "!",
381 case ACTION_TYPE_CTRL_SET:
382 case ACTION_TYPE_CTRL_LOCK:
383 write_buf(buf, "%s%s(controls=%s%s)%s", prefix, type,
384 ControlMaskText(keymap->ctx, action->ctrls.ctrls),
385 (action->type == ACTION_TYPE_CTRL_LOCK) ? affect_lock_text(action->ctrls.flags) : "",
389 case ACTION_TYPE_NONE:
390 write_buf(buf, "%sNoAction()%s", prefix, suffix);
395 "%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",
396 prefix, type, action->type, action->priv.data[0],
397 action->priv.data[1], action->priv.data[2],
398 action->priv.data[3], action->priv.data[4],
399 action->priv.data[5], action->priv.data[6],
408 write_compat(struct xkb_keymap *keymap, struct buf *buf)
410 const struct xkb_led *led;
412 if (keymap->compat_section_name)
413 write_buf(buf, "xkb_compatibility \"%s\" {\n",
414 keymap->compat_section_name);
416 write_buf(buf, "xkb_compatibility {\n");
418 write_vmods(keymap, buf);
420 write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
421 write_buf(buf, "\tinterpret.repeat= False;\n");
423 for (unsigned i = 0; i < keymap->num_sym_interprets; i++) {
424 const struct xkb_sym_interpret *si = &keymap->sym_interprets[i];
426 write_buf(buf, "\tinterpret %s+%s(%s) {\n",
427 si->sym ? KeysymText(keymap->ctx, si->sym) : "Any",
428 SIMatchText(si->match),
429 ModMaskText(keymap->ctx, &keymap->mods, si->mods));
431 if (si->virtual_mod != XKB_MOD_INVALID)
432 write_buf(buf, "\t\tvirtualModifier= %s;\n",
433 ModIndexText(keymap->ctx, &keymap->mods,
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 xkb_leds_foreach(led, keymap)
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 < XkbKeyNumLevels(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->ctx, &keymap->mods, 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);
581 for (level = 0; level < XkbKeyNumLevels(key, group); level++) {
583 write_buf(buf, ", ");
584 write_action(keymap, buf,
585 &key->groups[group].levels[level].action,
588 write_buf(buf, " ]");
591 write_buf(buf, "\n\t};\n");
598 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
600 const struct xkb_key *key;
601 xkb_layout_index_t group;
603 const struct xkb_mod *mod;
605 if (keymap->symbols_section_name)
606 write_buf(buf, "xkb_symbols \"%s\" {\n",
607 keymap->symbols_section_name);
609 write_buf(buf, "xkb_symbols {\n");
611 for (group = 0; group < keymap->num_group_names; group++)
612 if (keymap->group_names[group])
614 "\tname[Group%u]=\"%s\";\n", group + 1,
615 xkb_atom_text(keymap->ctx, keymap->group_names[group]));
617 write_buf(buf, "\n");
619 xkb_keys_foreach(key, keymap)
620 if (key->num_groups > 0)
621 write_key(keymap, buf, key);
623 xkb_mods_enumerate(i, mod, &keymap->mods) {
624 bool had_any = false;
625 xkb_keys_foreach(key, keymap) {
626 if (key->modmap & (1u << i)) {
628 write_buf(buf, "\tmodifier_map %s { ",
629 xkb_atom_text(keymap->ctx, mod->name));
630 write_buf(buf, "%s%s",
632 KeyNameText(keymap->ctx, key->name));
637 write_buf(buf, " };\n");
640 write_buf(buf, "};\n\n");
645 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
647 return (check_write_buf(buf, "xkb_keymap {\n") &&
648 write_keycodes(keymap, buf) &&
649 write_types(keymap, buf) &&
650 write_compat(keymap, buf) &&
651 write_symbols(keymap, buf) &&
652 check_write_buf(buf, "};\n"));
656 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
658 struct buf buf = { NULL, 0, 0 };
660 if (!write_keymap(keymap, &buf)) {