keymap: abstract a bit over the keymap format
[platform/upstream/libxkbcommon.git] / src / xkbcomp / keymap-dump.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
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.
15  *
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.
24  *
25  ********************************************************/
26
27 /*
28  * Copyright © 2012 Intel Corporation
29  *
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:
36  *
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
39  * Software.
40  *
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.
48  *
49  * Author: Daniel Stone <daniel@fooishbar.org>
50  */
51
52 #include "xkbcomp-priv.h"
53 #include "text.h"
54
55 #define BUF_CHUNK_SIZE 4096
56
57 struct buf {
58     char *buf;
59     size_t size;
60     size_t alloc;
61 };
62
63 static bool
64 do_realloc(struct buf *buf, size_t at_least)
65 {
66     char *new;
67
68     buf->alloc += BUF_CHUNK_SIZE;
69     if (at_least >= BUF_CHUNK_SIZE)
70         buf->alloc += at_least;
71
72     new = realloc(buf->buf, buf->alloc);
73     if (!new)
74         return false;
75
76     buf->buf = new;
77     return true;
78 }
79
80 ATTR_PRINTF(2, 3) static bool
81 check_write_buf(struct buf *buf, const char *fmt, ...)
82 {
83     va_list args;
84     int printed;
85     size_t available;
86
87     available = buf->alloc - buf->size;
88     va_start(args, fmt);
89     printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
90     va_end(args);
91
92     if (printed < 0)
93         goto err;
94
95     if (printed >= available)
96         if (!do_realloc(buf, printed))
97             goto err;
98
99     /* The buffer has enough space now. */
100
101     available = buf->alloc - buf->size;
102     va_start(args, fmt);
103     printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
104     va_end(args);
105
106     if (printed >= available || printed < 0)
107         goto err;
108
109     buf->size += printed;
110     return true;
111
112 err:
113     free(buf->buf);
114     buf->buf = NULL;
115     return false;
116 }
117
118 #define write_buf(buf, ...) do { \
119     if (!check_write_buf(buf, __VA_ARGS__)) \
120         return false; \
121 } while (0)
122
123 static bool
124 write_vmods(struct xkb_keymap *keymap, struct buf *buf)
125 {
126     const struct xkb_mod *mod;
127     xkb_mod_index_t num_vmods = 0;
128
129     darray_foreach(mod, keymap->mods) {
130         if (mod->type != MOD_VIRT)
131             continue;
132
133         if (num_vmods == 0)
134             write_buf(buf, "\tvirtual_modifiers ");
135         else
136             write_buf(buf, ",");
137         write_buf(buf, "%s", xkb_atom_text(keymap->ctx, mod->name));
138         num_vmods++;
139     }
140
141     if (num_vmods > 0)
142         write_buf(buf, ";\n\n");
143
144     return true;
145 }
146
147 static bool
148 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
149 {
150     const struct xkb_key *key;
151     const struct xkb_key_alias *alias;
152     xkb_led_index_t i;
153     const struct xkb_led *led;
154
155     if (keymap->keycodes_section_name)
156         write_buf(buf, "xkb_keycodes \"%s\" {\n",
157                   keymap->keycodes_section_name);
158     else
159         write_buf(buf, "xkb_keycodes {\n");
160
161     xkb_foreach_key(key, keymap) {
162         if (key->name == XKB_ATOM_NONE)
163             continue;
164
165         write_buf(buf, "\t%-20s = %d;\n",
166                   KeyNameText(keymap->ctx, key->name), key->keycode);
167     }
168
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));
173
174
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));
179
180     write_buf(buf, "};\n\n");
181     return true;
182 }
183
184 static bool
185 write_types(struct xkb_keymap *keymap, struct buf *buf)
186 {
187     if (keymap->types_section_name)
188         write_buf(buf, "xkb_types \"%s\" {\n",
189                   keymap->types_section_name);
190     else
191         write_buf(buf, "xkb_types {\n");
192
193     write_vmods(keymap, buf);
194
195     for (unsigned i = 0; i < keymap->num_types; i++) {
196         const struct xkb_key_type *type = &keymap->types[i];
197
198         write_buf(buf, "\ttype \"%s\" {\n",
199                   xkb_atom_text(keymap->ctx, type->name));
200
201         write_buf(buf, "\t\tmodifiers= %s;\n",
202                   ModMaskText(keymap, type->mods.mods));
203
204         for (unsigned j = 0; j < type->num_entries; j++) {
205             const char *str;
206             const struct xkb_kt_map_entry *entry = &type->map[j];
207
208             /*
209              * Printing level 1 entries is redundant, it's the default,
210              * unless there's preserve info.
211              */
212             if (entry->level == 0 && entry->preserve.mods == 0)
213                 continue;
214
215             str = ModMaskText(keymap, entry->mods.mods);
216             write_buf(buf, "\t\tmap[%s]= Level%d;\n",
217                       str, entry->level + 1);
218
219             if (entry->preserve.mods)
220                 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
221                           str, ModMaskText(keymap, entry->preserve.mods));
222         }
223
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]));
228
229         write_buf(buf, "\t};\n");
230     }
231
232     write_buf(buf, "};\n\n");
233     return true;
234 }
235
236 static bool
237 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
238               const struct xkb_led *led)
239 {
240     write_buf(buf, "\tindicator \"%s\" {\n",
241               xkb_atom_text(keymap->ctx, led->name));
242
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));
247         }
248         write_buf(buf, "\t\tgroups= 0x%02x;\n",
249                   led->groups);
250     }
251
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));
256         }
257         write_buf(buf, "\t\tmodifiers= %s;\n",
258                   ModMaskText(keymap, led->mods.mods));
259     }
260
261     if (led->ctrls) {
262         write_buf(buf, "\t\tcontrols= %s;\n",
263                   ControlMaskText(keymap->ctx, led->ctrls));
264     }
265
266     write_buf(buf, "\t};\n");
267     return true;
268 }
269
270 static bool
271 write_action(struct xkb_keymap *keymap, struct buf *buf,
272              const union xkb_action *action,
273              const char *prefix, const char *suffix)
274 {
275     const char *type;
276     const char *args = NULL;
277
278     if (!prefix)
279         prefix = "";
280     if (!suffix)
281         suffix = "";
282
283     type = ActionTypeText(action->type);
284
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)
290             args = "modMapMods";
291         else
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)) ?
296                    ",clearLocks" : "",
297                   (action->type != ACTION_TYPE_MOD_LOCK &&
298                    (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
299                    ",latchToLock" : "",
300                   suffix);
301         break;
302
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)) ?
313                   ",clearLocks" : "",
314                   (action->type != ACTION_TYPE_GROUP_LOCK &&
315                    (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
316                   ",latchToLock" : "",
317                   suffix);
318         break;
319
320     case ACTION_TYPE_TERMINATE:
321         write_buf(buf, "%s%s()%s", prefix, type, suffix);
322         break;
323
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) ? "+" : "",
328                   action->ptr.x,
329                   (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
330                    action->ptr.y >= 0) ? "+" : "",
331                   action->ptr.y,
332                   (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
333                   suffix);
334         break;
335
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";
341             break;
342
343         case ACTION_LOCK_NO_LOCK:
344             args = ",affect=unlock";
345             break;
346
347         case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
348             args = ",affect=neither";
349             break;
350
351         default:
352             args = ",affect=both";
353             break;
354         }
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);
359         else
360             write_buf(buf, "default");
361         if (action->btn.count)
362             write_buf(buf, ",count=%d", action->btn.count);
363         if (args)
364             write_buf(buf, "%s", args);
365         write_buf(buf, ")%s", suffix);
366         break;
367
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) ? "+" : "",
373                   action->dflt.value);
374         write_buf(buf, ")%s", suffix);
375         break;
376
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) ? "!" : "",
383                   suffix);
384         break;
385
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);
390         break;
391
392     case ACTION_TYPE_NONE:
393         write_buf(buf, "%sNoAction()%s", prefix, suffix);
394         break;
395
396     default:
397         write_buf(buf,
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],
403                   suffix);
404         break;
405     }
406
407     return true;
408 }
409
410 static bool
411 write_compat(struct xkb_keymap *keymap, struct buf *buf)
412 {
413     const struct xkb_sym_interpret *si;
414     const struct xkb_led *led;
415
416     if (keymap->compat_section_name)
417         write_buf(buf, "xkb_compatibility \"%s\" {\n",
418                   keymap->compat_section_name);
419     else
420         write_buf(buf, "xkb_compatibility {\n");
421
422     write_vmods(keymap, buf);
423
424     write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
425     write_buf(buf, "\tinterpret.repeat= False;\n");
426
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));
432
433         if (si->virtual_mod != XKB_MOD_INVALID)
434             write_buf(buf, "\t\tvirtualModifier= %s;\n",
435                       ModIndexText(keymap, si->virtual_mod));
436
437         if (si->level_one_only)
438             write_buf(buf, "\t\tuseModMapMods=level1;\n");
439
440         if (si->repeat)
441             write_buf(buf, "\t\trepeat= True;\n");
442
443         write_action(keymap, buf, &si->action, "\t\taction= ", ";\n");
444         write_buf(buf, "\t};\n");
445     }
446
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);
451
452     write_buf(buf, "};\n\n");
453
454     return true;
455 }
456
457 static bool
458 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
459               const struct xkb_key *key, xkb_layout_index_t group)
460 {
461     for (xkb_level_index_t level = 0; level < XkbKeyGroupWidth(key, group);
462          level++) {
463         const xkb_keysym_t *syms;
464         int num_syms;
465
466         if (level != 0)
467             write_buf(buf, ", ");
468
469         num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
470                                                     group, level, &syms);
471         if (num_syms == 0) {
472             write_buf(buf, "%15s", "NoSymbol");
473         }
474         else if (num_syms == 1) {
475             write_buf(buf, "%15s", KeysymText(keymap->ctx, syms[0]));
476         }
477         else {
478             write_buf(buf, "{ ");
479             for (int s = 0; s < num_syms; s++) {
480                 if (s != 0)
481                     write_buf(buf, ", ");
482                 write_buf(buf, "%s", KeysymText(keymap->ctx, syms[s]));
483             }
484             write_buf(buf, " }");
485         }
486     }
487
488     return true;
489 }
490
491 static bool
492 write_key(struct xkb_keymap *keymap, struct buf *buf,
493           const struct xkb_key *key)
494 {
495     xkb_layout_index_t group;
496     bool simple = true;
497     bool explicit_types = false;
498     bool multi_type = false;
499     bool show_actions;
500
501     write_buf(buf, "\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
502
503     for (group = 0; group < key->num_groups; group++) {
504         if (key->groups[group].explicit_type)
505             explicit_types = true;
506
507         if (group != 0 && key->groups[group].type != key->groups[0].type)
508             multi_type = true;
509     }
510
511     if (explicit_types) {
512         const struct xkb_key_type *type;
513         simple = false;
514
515         if (multi_type) {
516             for (group = 0; group < key->num_groups; group++) {
517                 if (!key->groups[group].explicit_type)
518                     continue;
519
520                 type = key->groups[group].type;
521                 write_buf(buf, "\n\t\ttype[group%u]= \"%s\",",
522                             group + 1,
523                             xkb_atom_text(keymap->ctx, type->name));
524             }
525         }
526         else {
527             type = key->groups[0].type;
528             write_buf(buf, "\n\t\ttype= \"%s\",",
529                         xkb_atom_text(keymap->ctx, type->name));
530         }
531     }
532
533     if (key->explicit & EXPLICIT_REPEAT) {
534         if (key->repeats)
535             write_buf(buf, "\n\t\trepeat= Yes,");
536         else
537             write_buf(buf, "\n\t\trepeat= No,");
538         simple = false;
539     }
540
541     if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
542         write_buf(buf, "\n\t\tvirtualMods= %s,",
543                     ModMaskText(keymap, key->vmodmap));
544
545     switch (key->out_of_range_group_action) {
546     case RANGE_SATURATE:
547         write_buf(buf, "\n\t\tgroupsClamp,");
548         break;
549
550     case RANGE_REDIRECT:
551         write_buf(buf, "\n\t\tgroupsRedirect= Group%u,",
552                     key->out_of_range_group_number + 1);
553         break;
554
555     default:
556         break;
557     }
558
559     show_actions = !!(key->explicit & EXPLICIT_INTERP);
560
561     if (key->num_groups > 1 || show_actions)
562         simple = false;
563
564     if (simple) {
565         write_buf(buf, "\t[ ");
566         if (!write_keysyms(keymap, buf, key, 0))
567             return false;
568         write_buf(buf, " ] };\n");
569     }
570     else {
571         xkb_level_index_t level;
572
573         for (group = 0; group < key->num_groups; group++) {
574             if (group != 0)
575                 write_buf(buf, ",");
576             write_buf(buf, "\n\t\tsymbols[Group%u]= [ ", group + 1);
577             if (!write_keysyms(keymap, buf, key, group))
578                 return false;
579             write_buf(buf, " ]");
580             if (show_actions) {
581                 write_buf(buf, ",\n\t\tactions[Group%u]= [ ", group + 1);
582                 for (level = 0;
583                         level < XkbKeyGroupWidth(key, group); level++) {
584                     if (level != 0)
585                         write_buf(buf, ", ");
586                     write_action(keymap, buf,
587                                     &key->groups[group].levels[level].action,
588                                     NULL, NULL);
589                 }
590                 write_buf(buf, " ]");
591             }
592         }
593         write_buf(buf, "\n\t};\n");
594     }
595
596     return true;
597 }
598
599 static bool
600 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
601 {
602     const struct xkb_key *key;
603     xkb_layout_index_t group;
604
605     if (keymap->symbols_section_name)
606         write_buf(buf, "xkb_symbols \"%s\" {\n",
607                   keymap->symbols_section_name);
608     else
609         write_buf(buf, "xkb_symbols {\n");
610
611     for (group = 0; group < keymap->num_group_names; group++)
612         if (keymap->group_names[group])
613             write_buf(buf,
614                       "\tname[group%d]=\"%s\";\n", group + 1,
615                       xkb_atom_text(keymap->ctx, keymap->group_names[group]));
616     if (group > 0)
617         write_buf(buf, "\n");
618
619     xkb_foreach_key(key, keymap)
620         if (key->num_groups > 0)
621             write_key(keymap, buf, key);
622
623     xkb_foreach_key(key, keymap) {
624         xkb_mod_index_t i;
625         const struct xkb_mod *mod;
626
627         if (key->modmap == 0)
628             continue;
629
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));
635     }
636
637     write_buf(buf, "};\n\n");
638     return true;
639 }
640
641 static bool
642 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
643 {
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"));
650 }
651
652 char *
653 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
654 {
655     struct buf buf = { NULL, 0, 0 };
656
657     if (!write_keymap(keymap, &buf)) {
658         free(buf.buf);
659         return NULL;
660     }
661
662     return buf.buf;
663 }