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;
151 const struct xkb_key_alias *alias;
153 const struct xkb_led *led;
155 if (keymap->keycodes_section_name)
156 write_buf(buf, "xkb_keycodes \"%s\" {\n",
157 keymap->keycodes_section_name);
159 write_buf(buf, "xkb_keycodes {\n");
161 xkb_foreach_key(key, keymap) {
162 if (key->name == XKB_ATOM_NONE)
165 write_buf(buf, "\t%-20s = %d;\n",
166 KeyNameText(keymap->ctx, key->name), key->keycode);
169 darray_enumerate(i, led, keymap->leds)
170 if (led->name != XKB_ATOM_NONE)
171 write_buf(buf, "\tindicator %d = \"%s\";\n",
172 i + 1, xkb_atom_text(keymap->ctx, led->name));
175 darray_foreach(alias, keymap->key_aliases)
176 write_buf(buf, "\talias %-14s = %s;\n",
177 KeyNameText(keymap->ctx, alias->alias),
178 KeyNameText(keymap->ctx, alias->real));
180 write_buf(buf, "};\n\n");
185 write_types(struct xkb_keymap *keymap, struct buf *buf)
187 if (keymap->types_section_name)
188 write_buf(buf, "xkb_types \"%s\" {\n",
189 keymap->types_section_name);
191 write_buf(buf, "xkb_types {\n");
193 write_vmods(keymap, buf);
195 for (unsigned i = 0; i < keymap->num_types; i++) {
196 const struct xkb_key_type *type = &keymap->types[i];
198 write_buf(buf, "\ttype \"%s\" {\n",
199 xkb_atom_text(keymap->ctx, type->name));
201 write_buf(buf, "\t\tmodifiers= %s;\n",
202 ModMaskText(keymap, type->mods.mods));
204 for (unsigned j = 0; j < type->num_entries; j++) {
206 const struct xkb_kt_map_entry *entry = &type->map[j];
209 * Printing level 1 entries is redundant, it's the default,
210 * unless there's preserve info.
212 if (entry->level == 0 && entry->preserve.mods == 0)
215 str = ModMaskText(keymap, entry->mods.mods);
216 write_buf(buf, "\t\tmap[%s]= Level%d;\n",
217 str, entry->level + 1);
219 if (entry->preserve.mods)
220 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
221 str, ModMaskText(keymap, entry->preserve.mods));
224 for (xkb_level_index_t n = 0; n < type->num_levels; n++)
225 if (type->level_names[n])
226 write_buf(buf, "\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
227 xkb_atom_text(keymap->ctx, type->level_names[n]));
229 write_buf(buf, "\t};\n");
232 write_buf(buf, "};\n\n");
237 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
238 const struct xkb_led *led)
240 write_buf(buf, "\tindicator \"%s\" {\n",
241 xkb_atom_text(keymap->ctx, led->name));
243 if (led->which_groups) {
244 if (led->which_groups != XKB_STATE_LAYOUT_EFFECTIVE) {
245 write_buf(buf, "\t\twhichGroupState= %s;\n",
246 LedStateMaskText(keymap->ctx, led->which_groups));
248 write_buf(buf, "\t\tgroups= 0x%02x;\n",
252 if (led->which_mods) {
253 if (led->which_mods != XKB_STATE_MODS_EFFECTIVE) {
254 write_buf(buf, "\t\twhichModState= %s;\n",
255 LedStateMaskText(keymap->ctx, led->which_mods));
257 write_buf(buf, "\t\tmodifiers= %s;\n",
258 ModMaskText(keymap, led->mods.mods));
262 write_buf(buf, "\t\tcontrols= %s;\n",
263 ControlMaskText(keymap->ctx, led->ctrls));
266 write_buf(buf, "\t};\n");
271 write_action(struct xkb_keymap *keymap, struct buf *buf,
272 const union xkb_action *action,
273 const char *prefix, const char *suffix)
276 const char *args = NULL;
283 type = ActionTypeText(action->type);
285 switch (action->type) {
286 case ACTION_TYPE_MOD_SET:
287 case ACTION_TYPE_MOD_LATCH:
288 case ACTION_TYPE_MOD_LOCK:
289 if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
292 args = ModMaskText(keymap, action->mods.mods.mods);
293 write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
294 (action->type != ACTION_TYPE_MOD_LOCK &&
295 (action->mods.flags & ACTION_LOCK_CLEAR)) ?
297 (action->type != ACTION_TYPE_MOD_LOCK &&
298 (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
303 case ACTION_TYPE_GROUP_SET:
304 case ACTION_TYPE_GROUP_LATCH:
305 case ACTION_TYPE_GROUP_LOCK:
306 write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
307 (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
308 action->group.group > 0) ? "+" : "",
309 (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
310 action->group.group + 1 : action->group.group,
311 (action->type != ACTION_TYPE_GROUP_LOCK &&
312 (action->group.flags & ACTION_LOCK_CLEAR)) ?
314 (action->type != ACTION_TYPE_GROUP_LOCK &&
315 (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
320 case ACTION_TYPE_TERMINATE:
321 write_buf(buf, "%s%s()%s", prefix, type, suffix);
324 case ACTION_TYPE_PTR_MOVE:
325 write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
326 (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
327 action->ptr.x >= 0) ? "+" : "",
329 (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
330 action->ptr.y >= 0) ? "+" : "",
332 (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
336 case ACTION_TYPE_PTR_LOCK:
337 switch (action->btn.flags &
338 (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
339 case ACTION_LOCK_NO_UNLOCK:
340 args = ",affect=lock";
343 case ACTION_LOCK_NO_LOCK:
344 args = ",affect=unlock";
347 case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
348 args = ",affect=neither";
352 args = ",affect=both";
355 case ACTION_TYPE_PTR_BUTTON:
356 write_buf(buf, "%s%s(button=", prefix, type);
357 if (action->btn.button > 0 && action->btn.button <= 5)
358 write_buf(buf, "%d", action->btn.button);
360 write_buf(buf, "default");
361 if (action->btn.count)
362 write_buf(buf, ",count=%d", action->btn.count);
364 write_buf(buf, "%s", args);
365 write_buf(buf, ")%s", suffix);
368 case ACTION_TYPE_PTR_DEFAULT:
369 write_buf(buf, "%s%s(", prefix, type);
370 write_buf(buf, "affect=button,button=%s%d",
371 (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
372 action->dflt.value >= 0) ? "+" : "",
374 write_buf(buf, ")%s", suffix);
377 case ACTION_TYPE_SWITCH_VT:
378 write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
379 (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
380 action->screen.screen >= 0) ? "+" : "",
381 action->screen.screen,
382 (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
386 case ACTION_TYPE_CTRL_SET:
387 case ACTION_TYPE_CTRL_LOCK:
388 write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
389 ControlMaskText(keymap->ctx, action->ctrls.ctrls), suffix);
392 case ACTION_TYPE_NONE:
393 write_buf(buf, "%sNoAction()%s", prefix, suffix);
398 "%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",
399 prefix, type, action->type, action->priv.data[0],
400 action->priv.data[1], action->priv.data[2],
401 action->priv.data[3], action->priv.data[4],
402 action->priv.data[5], action->priv.data[6],
411 write_compat(struct xkb_keymap *keymap, struct buf *buf)
413 const struct xkb_sym_interpret *si;
414 const struct xkb_led *led;
416 if (keymap->compat_section_name)
417 write_buf(buf, "xkb_compatibility \"%s\" {\n",
418 keymap->compat_section_name);
420 write_buf(buf, "xkb_compatibility {\n");
422 write_vmods(keymap, buf);
424 write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
425 write_buf(buf, "\tinterpret.repeat= False;\n");
427 darray_foreach(si, keymap->sym_interprets) {
428 write_buf(buf, "\tinterpret %s+%s(%s) {\n",
429 si->sym ? KeysymText(keymap->ctx, si->sym) : "Any",
430 SIMatchText(si->match),
431 ModMaskText(keymap, si->mods));
433 if (si->virtual_mod != XKB_MOD_INVALID)
434 write_buf(buf, "\t\tvirtualModifier= %s;\n",
435 ModIndexText(keymap, si->virtual_mod));
437 if (si->level_one_only)
438 write_buf(buf, "\t\tuseModMapMods=level1;\n");
441 write_buf(buf, "\t\trepeat= True;\n");
443 write_action(keymap, buf, &si->action, "\t\taction= ", ";\n");
444 write_buf(buf, "\t};\n");
447 darray_foreach(led, keymap->leds)
448 if (led->which_groups || led->groups || led->which_mods ||
449 led->mods.mods || led->ctrls)
450 write_led_map(keymap, buf, led);
452 write_buf(buf, "};\n\n");
458 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
459 const struct xkb_key *key, xkb_layout_index_t group)
461 for (xkb_level_index_t level = 0; level < XkbKeyGroupWidth(key, group);
463 const xkb_keysym_t *syms;
467 write_buf(buf, ", ");
469 num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
470 group, level, &syms);
472 write_buf(buf, "%15s", "NoSymbol");
474 else if (num_syms == 1) {
475 write_buf(buf, "%15s", KeysymText(keymap->ctx, syms[0]));
478 write_buf(buf, "{ ");
479 for (int s = 0; s < num_syms; s++) {
481 write_buf(buf, ", ");
482 write_buf(buf, "%s", KeysymText(keymap->ctx, syms[s]));
484 write_buf(buf, " }");
492 write_key(struct xkb_keymap *keymap, struct buf *buf,
493 const struct xkb_key *key)
495 xkb_layout_index_t group;
497 bool explicit_types = false;
498 bool multi_type = false;
501 write_buf(buf, "\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
503 for (group = 0; group < key->num_groups; group++) {
504 if (key->groups[group].explicit_type)
505 explicit_types = true;
507 if (group != 0 && key->groups[group].type != key->groups[0].type)
511 if (explicit_types) {
512 const struct xkb_key_type *type;
516 for (group = 0; group < key->num_groups; group++) {
517 if (!key->groups[group].explicit_type)
520 type = key->groups[group].type;
521 write_buf(buf, "\n\t\ttype[group%u]= \"%s\",",
523 xkb_atom_text(keymap->ctx, type->name));
527 type = key->groups[0].type;
528 write_buf(buf, "\n\t\ttype= \"%s\",",
529 xkb_atom_text(keymap->ctx, type->name));
533 if (key->explicit & EXPLICIT_REPEAT) {
535 write_buf(buf, "\n\t\trepeat= Yes,");
537 write_buf(buf, "\n\t\trepeat= No,");
541 if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
542 write_buf(buf, "\n\t\tvirtualMods= %s,",
543 ModMaskText(keymap, key->vmodmap));
545 switch (key->out_of_range_group_action) {
547 write_buf(buf, "\n\t\tgroupsClamp,");
551 write_buf(buf, "\n\t\tgroupsRedirect= Group%u,",
552 key->out_of_range_group_number + 1);
559 show_actions = !!(key->explicit & EXPLICIT_INTERP);
561 if (key->num_groups > 1 || show_actions)
565 write_buf(buf, "\t[ ");
566 if (!write_keysyms(keymap, buf, key, 0))
568 write_buf(buf, " ] };\n");
571 xkb_level_index_t level;
573 for (group = 0; group < key->num_groups; group++) {
576 write_buf(buf, "\n\t\tsymbols[Group%u]= [ ", group + 1);
577 if (!write_keysyms(keymap, buf, key, group))
579 write_buf(buf, " ]");
581 write_buf(buf, ",\n\t\tactions[Group%u]= [ ", group + 1);
583 level < XkbKeyGroupWidth(key, group); level++) {
585 write_buf(buf, ", ");
586 write_action(keymap, buf,
587 &key->groups[group].levels[level].action,
590 write_buf(buf, " ]");
593 write_buf(buf, "\n\t};\n");
600 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
602 const struct xkb_key *key;
603 xkb_layout_index_t group;
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%d]=\"%s\";\n", group + 1,
615 xkb_atom_text(keymap->ctx, keymap->group_names[group]));
617 write_buf(buf, "\n");
619 xkb_foreach_key(key, keymap)
620 if (key->num_groups > 0)
621 write_key(keymap, buf, key);
623 xkb_foreach_key(key, keymap) {
625 const struct xkb_mod *mod;
627 if (key->modmap == 0)
630 darray_enumerate(i, mod, keymap->mods)
631 if (key->modmap & (1 << i))
632 write_buf(buf, "\tmodifier_map %s { %s };\n",
633 xkb_atom_text(keymap->ctx, mod->name),
634 KeyNameText(keymap->ctx, key->name));
637 write_buf(buf, "};\n\n");
642 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
644 return (check_write_buf(buf, "xkb_keymap {\n") &&
645 write_keycodes(keymap, buf) &&
646 write_types(keymap, buf) &&
647 write_compat(keymap, buf) &&
648 write_symbols(keymap, buf) &&
649 check_write_buf(buf, "};\n"));
653 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
655 struct buf buf = { NULL, 0, 0 };
657 if (!write_keymap(keymap, &buf)) {