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 ((size_t) 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 < 0 || (size_t) printed >= available)
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 /* xkbcomp and X11 really want to see keymaps with a minimum of 8, and
161 * a maximum of at least 255, else XWayland really starts hating life.
162 * If this is a problem and people really need strictly bounded keymaps,
163 * we should probably control this with a flag. */
164 write_buf(buf, "\tminimum = %u;\n", min(keymap->min_key_code, 8));
165 write_buf(buf, "\tmaximum = %u;\n", max(keymap->max_key_code, 255));
167 xkb_foreach_key(key, keymap) {
168 if (key->name == XKB_ATOM_NONE)
171 write_buf(buf, "\t%-20s = %u;\n",
172 KeyNameText(keymap->ctx, key->name), key->keycode);
175 darray_enumerate(idx, led, keymap->leds)
176 if (led->name != XKB_ATOM_NONE)
177 write_buf(buf, "\tindicator %u = \"%s\";\n",
178 idx + 1, xkb_atom_text(keymap->ctx, led->name));
181 for (unsigned i = 0; i < keymap->num_key_aliases; i++)
182 write_buf(buf, "\talias %-14s = %s;\n",
183 KeyNameText(keymap->ctx, keymap->key_aliases[i].alias),
184 KeyNameText(keymap->ctx, keymap->key_aliases[i].real));
186 write_buf(buf, "};\n\n");
191 write_types(struct xkb_keymap *keymap, struct buf *buf)
193 if (keymap->types_section_name)
194 write_buf(buf, "xkb_types \"%s\" {\n",
195 keymap->types_section_name);
197 write_buf(buf, "xkb_types {\n");
199 write_vmods(keymap, buf);
201 for (unsigned i = 0; i < keymap->num_types; i++) {
202 const struct xkb_key_type *type = &keymap->types[i];
204 write_buf(buf, "\ttype \"%s\" {\n",
205 xkb_atom_text(keymap->ctx, type->name));
207 write_buf(buf, "\t\tmodifiers= %s;\n",
208 ModMaskText(keymap, type->mods.mods));
210 for (unsigned j = 0; j < type->num_entries; j++) {
212 const struct xkb_key_type_entry *entry = &type->entries[j];
215 * Printing level 1 entries is redundant, it's the default,
216 * unless there's preserve info.
218 if (entry->level == 0 && entry->preserve.mods == 0)
221 str = ModMaskText(keymap, entry->mods.mods);
222 write_buf(buf, "\t\tmap[%s]= Level%u;\n",
223 str, entry->level + 1);
225 if (entry->preserve.mods)
226 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
227 str, ModMaskText(keymap, entry->preserve.mods));
230 for (xkb_level_index_t n = 0; n < type->num_levels; n++)
231 if (type->level_names[n])
232 write_buf(buf, "\t\tlevel_name[Level%u]= \"%s\";\n", n + 1,
233 xkb_atom_text(keymap->ctx, type->level_names[n]));
235 write_buf(buf, "\t};\n");
238 write_buf(buf, "};\n\n");
243 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
244 const struct xkb_led *led)
246 write_buf(buf, "\tindicator \"%s\" {\n",
247 xkb_atom_text(keymap->ctx, led->name));
249 if (led->which_groups) {
250 if (led->which_groups != XKB_STATE_LAYOUT_EFFECTIVE) {
251 write_buf(buf, "\t\twhichGroupState= %s;\n",
252 LedStateMaskText(keymap->ctx, led->which_groups));
254 write_buf(buf, "\t\tgroups= 0x%02x;\n",
258 if (led->which_mods) {
259 if (led->which_mods != XKB_STATE_MODS_EFFECTIVE) {
260 write_buf(buf, "\t\twhichModState= %s;\n",
261 LedStateMaskText(keymap->ctx, led->which_mods));
263 write_buf(buf, "\t\tmodifiers= %s;\n",
264 ModMaskText(keymap, led->mods.mods));
268 write_buf(buf, "\t\tcontrols= %s;\n",
269 ControlMaskText(keymap->ctx, led->ctrls));
272 write_buf(buf, "\t};\n");
277 write_action(struct xkb_keymap *keymap, struct buf *buf,
278 const union xkb_action *action,
279 const char *prefix, const char *suffix)
282 const char *args = NULL;
289 type = ActionTypeText(action->type);
291 switch (action->type) {
292 case ACTION_TYPE_MOD_SET:
293 case ACTION_TYPE_MOD_LATCH:
294 case ACTION_TYPE_MOD_LOCK:
295 if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
298 args = ModMaskText(keymap, action->mods.mods.mods);
299 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
300 (action->type != ACTION_TYPE_MOD_LOCK &&
301 (action->mods.flags & ACTION_LOCK_CLEAR)) ?
303 (action->type != ACTION_TYPE_MOD_LOCK &&
304 (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
309 case ACTION_TYPE_GROUP_SET:
310 case ACTION_TYPE_GROUP_LATCH:
311 case ACTION_TYPE_GROUP_LOCK:
312 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
313 (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
314 action->group.group > 0) ? "+" : "",
315 (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
316 action->group.group + 1 : action->group.group,
317 (action->type != ACTION_TYPE_GROUP_LOCK &&
318 (action->group.flags & ACTION_LOCK_CLEAR)) ?
320 (action->type != ACTION_TYPE_GROUP_LOCK &&
321 (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
326 case ACTION_TYPE_TERMINATE:
327 write_buf(buf, "%s%s()%s", prefix, type, suffix);
330 case ACTION_TYPE_PTR_MOVE:
331 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
332 (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
333 action->ptr.x >= 0) ? "+" : "",
335 (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
336 action->ptr.y >= 0) ? "+" : "",
338 (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
342 case ACTION_TYPE_PTR_LOCK:
343 switch (action->btn.flags &
344 (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
345 case ACTION_LOCK_NO_UNLOCK:
346 args = ",affect=lock";
349 case ACTION_LOCK_NO_LOCK:
350 args = ",affect=unlock";
353 case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
354 args = ",affect=neither";
358 args = ",affect=both";
361 case ACTION_TYPE_PTR_BUTTON:
362 write_buf(buf, "%s%s(button=", prefix, type);
363 if (action->btn.button > 0 && action->btn.button <= 5)
364 write_buf(buf, "%d", action->btn.button);
366 write_buf(buf, "default");
367 if (action->btn.count)
368 write_buf(buf, ",count=%d", action->btn.count);
370 write_buf(buf, "%s", args);
371 write_buf(buf, ")%s", suffix);
374 case ACTION_TYPE_PTR_DEFAULT:
375 write_buf(buf, "%s%s(", prefix, type);
376 write_buf(buf, "affect=button,button=%s%d",
377 (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
378 action->dflt.value >= 0) ? "+" : "",
380 write_buf(buf, ")%s", suffix);
383 case ACTION_TYPE_SWITCH_VT:
384 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
385 (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
386 action->screen.screen >= 0) ? "+" : "",
387 action->screen.screen,
388 (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
392 case ACTION_TYPE_CTRL_SET:
393 case ACTION_TYPE_CTRL_LOCK:
394 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
395 ControlMaskText(keymap->ctx, action->ctrls.ctrls), suffix);
398 case ACTION_TYPE_NONE:
399 write_buf(buf, "%sNoAction()%s", prefix, suffix);
404 "%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",
405 prefix, type, action->type, action->priv.data[0],
406 action->priv.data[1], action->priv.data[2],
407 action->priv.data[3], action->priv.data[4],
408 action->priv.data[5], action->priv.data[6],
417 write_compat(struct xkb_keymap *keymap, struct buf *buf)
419 const struct xkb_led *led;
421 if (keymap->compat_section_name)
422 write_buf(buf, "xkb_compatibility \"%s\" {\n",
423 keymap->compat_section_name);
425 write_buf(buf, "xkb_compatibility {\n");
427 write_vmods(keymap, buf);
429 write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
430 write_buf(buf, "\tinterpret.repeat= False;\n");
432 for (unsigned i = 0; i < keymap->num_sym_interprets; i++) {
433 const struct xkb_sym_interpret *si = &keymap->sym_interprets[i];
435 write_buf(buf, "\tinterpret %s+%s(%s) {\n",
436 si->sym ? KeysymText(keymap->ctx, si->sym) : "Any",
437 SIMatchText(si->match),
438 ModMaskText(keymap, si->mods));
440 if (si->virtual_mod != XKB_MOD_INVALID)
441 write_buf(buf, "\t\tvirtualModifier= %s;\n",
442 ModIndexText(keymap, si->virtual_mod));
444 if (si->level_one_only)
445 write_buf(buf, "\t\tuseModMapMods=level1;\n");
448 write_buf(buf, "\t\trepeat= True;\n");
450 write_action(keymap, buf, &si->action, "\t\taction= ", ";\n");
451 write_buf(buf, "\t};\n");
454 darray_foreach(led, keymap->leds)
455 if (led->which_groups || led->groups || led->which_mods ||
456 led->mods.mods || led->ctrls)
457 write_led_map(keymap, buf, led);
459 write_buf(buf, "};\n\n");
465 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
466 const struct xkb_key *key, xkb_layout_index_t group)
468 for (xkb_level_index_t level = 0; level < XkbKeyGroupWidth(key, group);
470 const xkb_keysym_t *syms;
474 write_buf(buf, ", ");
476 num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
477 group, level, &syms);
479 write_buf(buf, "%15s", "NoSymbol");
481 else if (num_syms == 1) {
482 write_buf(buf, "%15s", KeysymText(keymap->ctx, syms[0]));
485 write_buf(buf, "{ ");
486 for (int s = 0; s < num_syms; s++) {
488 write_buf(buf, ", ");
489 write_buf(buf, "%s", KeysymText(keymap->ctx, syms[s]));
491 write_buf(buf, " }");
499 write_key(struct xkb_keymap *keymap, struct buf *buf,
500 const struct xkb_key *key)
502 xkb_layout_index_t group;
504 bool explicit_types = false;
505 bool multi_type = false;
508 write_buf(buf, "\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
510 for (group = 0; group < key->num_groups; group++) {
511 if (key->groups[group].explicit_type)
512 explicit_types = true;
514 if (group != 0 && key->groups[group].type != key->groups[0].type)
518 if (explicit_types) {
519 const struct xkb_key_type *type;
523 for (group = 0; group < key->num_groups; group++) {
524 if (!key->groups[group].explicit_type)
527 type = key->groups[group].type;
528 write_buf(buf, "\n\t\ttype[group%u]= \"%s\",",
530 xkb_atom_text(keymap->ctx, type->name));
534 type = key->groups[0].type;
535 write_buf(buf, "\n\t\ttype= \"%s\",",
536 xkb_atom_text(keymap->ctx, type->name));
540 if (key->explicit & EXPLICIT_REPEAT) {
542 write_buf(buf, "\n\t\trepeat= Yes,");
544 write_buf(buf, "\n\t\trepeat= No,");
548 if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
549 write_buf(buf, "\n\t\tvirtualMods= %s,",
550 ModMaskText(keymap, key->vmodmap));
552 switch (key->out_of_range_group_action) {
554 write_buf(buf, "\n\t\tgroupsClamp,");
558 write_buf(buf, "\n\t\tgroupsRedirect= Group%u,",
559 key->out_of_range_group_number + 1);
566 show_actions = !!(key->explicit & EXPLICIT_INTERP);
568 if (key->num_groups > 1 || show_actions)
572 write_buf(buf, "\t[ ");
573 if (!write_keysyms(keymap, buf, key, 0))
575 write_buf(buf, " ] };\n");
578 xkb_level_index_t level;
580 for (group = 0; group < key->num_groups; group++) {
583 write_buf(buf, "\n\t\tsymbols[Group%u]= [ ", group + 1);
584 if (!write_keysyms(keymap, buf, key, group))
586 write_buf(buf, " ]");
588 write_buf(buf, ",\n\t\tactions[Group%u]= [ ", group + 1);
590 level < XkbKeyGroupWidth(key, group); level++) {
592 write_buf(buf, ", ");
593 write_action(keymap, buf,
594 &key->groups[group].levels[level].action,
597 write_buf(buf, " ]");
600 write_buf(buf, "\n\t};\n");
607 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
609 const struct xkb_key *key;
610 xkb_layout_index_t group;
612 if (keymap->symbols_section_name)
613 write_buf(buf, "xkb_symbols \"%s\" {\n",
614 keymap->symbols_section_name);
616 write_buf(buf, "xkb_symbols {\n");
618 for (group = 0; group < keymap->num_group_names; group++)
619 if (keymap->group_names[group])
621 "\tname[group%u]=\"%s\";\n", group + 1,
622 xkb_atom_text(keymap->ctx, keymap->group_names[group]));
624 write_buf(buf, "\n");
626 xkb_foreach_key(key, keymap)
627 if (key->num_groups > 0)
628 write_key(keymap, buf, key);
630 xkb_foreach_key(key, keymap) {
632 const struct xkb_mod *mod;
634 if (key->modmap == 0)
637 darray_enumerate(i, mod, keymap->mods)
638 if (key->modmap & (1 << i))
639 write_buf(buf, "\tmodifier_map %s { %s };\n",
640 xkb_atom_text(keymap->ctx, mod->name),
641 KeyNameText(keymap->ctx, key->name));
644 write_buf(buf, "};\n\n");
649 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
651 return (check_write_buf(buf, "xkb_keymap {\n") &&
652 write_keycodes(keymap, buf) &&
653 write_types(keymap, buf) &&
654 write_compat(keymap, buf) &&
655 write_symbols(keymap, buf) &&
656 check_write_buf(buf, "};\n"));
660 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
662 struct buf buf = { NULL, 0, 0 };
664 if (!write_keymap(keymap, &buf)) {