keymap: rename xkb_kt_map_entry to xkb_key_type_entry
[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     xkb_led_index_t idx;
152     const struct xkb_led *led;
153
154     if (keymap->keycodes_section_name)
155         write_buf(buf, "xkb_keycodes \"%s\" {\n",
156                   keymap->keycodes_section_name);
157     else
158         write_buf(buf, "xkb_keycodes {\n");
159
160     xkb_foreach_key(key, keymap) {
161         if (key->name == XKB_ATOM_NONE)
162             continue;
163
164         write_buf(buf, "\t%-20s = %d;\n",
165                   KeyNameText(keymap->ctx, key->name), key->keycode);
166     }
167
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));
172
173
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));
178
179     write_buf(buf, "};\n\n");
180     return true;
181 }
182
183 static bool
184 write_types(struct xkb_keymap *keymap, struct buf *buf)
185 {
186     if (keymap->types_section_name)
187         write_buf(buf, "xkb_types \"%s\" {\n",
188                   keymap->types_section_name);
189     else
190         write_buf(buf, "xkb_types {\n");
191
192     write_vmods(keymap, buf);
193
194     for (unsigned i = 0; i < keymap->num_types; i++) {
195         const struct xkb_key_type *type = &keymap->types[i];
196
197         write_buf(buf, "\ttype \"%s\" {\n",
198                   xkb_atom_text(keymap->ctx, type->name));
199
200         write_buf(buf, "\t\tmodifiers= %s;\n",
201                   ModMaskText(keymap, type->mods.mods));
202
203         for (unsigned j = 0; j < type->num_entries; j++) {
204             const char *str;
205             const struct xkb_key_type_entry *entry = &type->entries[j];
206
207             /*
208              * Printing level 1 entries is redundant, it's the default,
209              * unless there's preserve info.
210              */
211             if (entry->level == 0 && entry->preserve.mods == 0)
212                 continue;
213
214             str = ModMaskText(keymap, entry->mods.mods);
215             write_buf(buf, "\t\tmap[%s]= Level%d;\n",
216                       str, entry->level + 1);
217
218             if (entry->preserve.mods)
219                 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
220                           str, ModMaskText(keymap, entry->preserve.mods));
221         }
222
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]));
227
228         write_buf(buf, "\t};\n");
229     }
230
231     write_buf(buf, "};\n\n");
232     return true;
233 }
234
235 static bool
236 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
237               const struct xkb_led *led)
238 {
239     write_buf(buf, "\tindicator \"%s\" {\n",
240               xkb_atom_text(keymap->ctx, led->name));
241
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));
246         }
247         write_buf(buf, "\t\tgroups= 0x%02x;\n",
248                   led->groups);
249     }
250
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));
255         }
256         write_buf(buf, "\t\tmodifiers= %s;\n",
257                   ModMaskText(keymap, led->mods.mods));
258     }
259
260     if (led->ctrls) {
261         write_buf(buf, "\t\tcontrols= %s;\n",
262                   ControlMaskText(keymap->ctx, led->ctrls));
263     }
264
265     write_buf(buf, "\t};\n");
266     return true;
267 }
268
269 static bool
270 write_action(struct xkb_keymap *keymap, struct buf *buf,
271              const union xkb_action *action,
272              const char *prefix, const char *suffix)
273 {
274     const char *type;
275     const char *args = NULL;
276
277     if (!prefix)
278         prefix = "";
279     if (!suffix)
280         suffix = "";
281
282     type = ActionTypeText(action->type);
283
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)
289             args = "modMapMods";
290         else
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)) ?
295                    ",clearLocks" : "",
296                   (action->type != ACTION_TYPE_MOD_LOCK &&
297                    (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
298                    ",latchToLock" : "",
299                   suffix);
300         break;
301
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)) ?
312                   ",clearLocks" : "",
313                   (action->type != ACTION_TYPE_GROUP_LOCK &&
314                    (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
315                   ",latchToLock" : "",
316                   suffix);
317         break;
318
319     case ACTION_TYPE_TERMINATE:
320         write_buf(buf, "%s%s()%s", prefix, type, suffix);
321         break;
322
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) ? "+" : "",
327                   action->ptr.x,
328                   (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
329                    action->ptr.y >= 0) ? "+" : "",
330                   action->ptr.y,
331                   (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
332                   suffix);
333         break;
334
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";
340             break;
341
342         case ACTION_LOCK_NO_LOCK:
343             args = ",affect=unlock";
344             break;
345
346         case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
347             args = ",affect=neither";
348             break;
349
350         default:
351             args = ",affect=both";
352             break;
353         }
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);
358         else
359             write_buf(buf, "default");
360         if (action->btn.count)
361             write_buf(buf, ",count=%d", action->btn.count);
362         if (args)
363             write_buf(buf, "%s", args);
364         write_buf(buf, ")%s", suffix);
365         break;
366
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) ? "+" : "",
372                   action->dflt.value);
373         write_buf(buf, ")%s", suffix);
374         break;
375
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) ? "!" : "",
382                   suffix);
383         break;
384
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);
389         break;
390
391     case ACTION_TYPE_NONE:
392         write_buf(buf, "%sNoAction()%s", prefix, suffix);
393         break;
394
395     default:
396         write_buf(buf,
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],
402                   suffix);
403         break;
404     }
405
406     return true;
407 }
408
409 static bool
410 write_compat(struct xkb_keymap *keymap, struct buf *buf)
411 {
412     const struct xkb_sym_interpret *si;
413     const struct xkb_led *led;
414
415     if (keymap->compat_section_name)
416         write_buf(buf, "xkb_compatibility \"%s\" {\n",
417                   keymap->compat_section_name);
418     else
419         write_buf(buf, "xkb_compatibility {\n");
420
421     write_vmods(keymap, buf);
422
423     write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
424     write_buf(buf, "\tinterpret.repeat= False;\n");
425
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));
431
432         if (si->virtual_mod != XKB_MOD_INVALID)
433             write_buf(buf, "\t\tvirtualModifier= %s;\n",
434                       ModIndexText(keymap, si->virtual_mod));
435
436         if (si->level_one_only)
437             write_buf(buf, "\t\tuseModMapMods=level1;\n");
438
439         if (si->repeat)
440             write_buf(buf, "\t\trepeat= True;\n");
441
442         write_action(keymap, buf, &si->action, "\t\taction= ", ";\n");
443         write_buf(buf, "\t};\n");
444     }
445
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);
450
451     write_buf(buf, "};\n\n");
452
453     return true;
454 }
455
456 static bool
457 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
458               const struct xkb_key *key, xkb_layout_index_t group)
459 {
460     for (xkb_level_index_t level = 0; level < XkbKeyGroupWidth(key, group);
461          level++) {
462         const xkb_keysym_t *syms;
463         int num_syms;
464
465         if (level != 0)
466             write_buf(buf, ", ");
467
468         num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
469                                                     group, level, &syms);
470         if (num_syms == 0) {
471             write_buf(buf, "%15s", "NoSymbol");
472         }
473         else if (num_syms == 1) {
474             write_buf(buf, "%15s", KeysymText(keymap->ctx, syms[0]));
475         }
476         else {
477             write_buf(buf, "{ ");
478             for (int s = 0; s < num_syms; s++) {
479                 if (s != 0)
480                     write_buf(buf, ", ");
481                 write_buf(buf, "%s", KeysymText(keymap->ctx, syms[s]));
482             }
483             write_buf(buf, " }");
484         }
485     }
486
487     return true;
488 }
489
490 static bool
491 write_key(struct xkb_keymap *keymap, struct buf *buf,
492           const struct xkb_key *key)
493 {
494     xkb_layout_index_t group;
495     bool simple = true;
496     bool explicit_types = false;
497     bool multi_type = false;
498     bool show_actions;
499
500     write_buf(buf, "\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
501
502     for (group = 0; group < key->num_groups; group++) {
503         if (key->groups[group].explicit_type)
504             explicit_types = true;
505
506         if (group != 0 && key->groups[group].type != key->groups[0].type)
507             multi_type = true;
508     }
509
510     if (explicit_types) {
511         const struct xkb_key_type *type;
512         simple = false;
513
514         if (multi_type) {
515             for (group = 0; group < key->num_groups; group++) {
516                 if (!key->groups[group].explicit_type)
517                     continue;
518
519                 type = key->groups[group].type;
520                 write_buf(buf, "\n\t\ttype[group%u]= \"%s\",",
521                             group + 1,
522                             xkb_atom_text(keymap->ctx, type->name));
523             }
524         }
525         else {
526             type = key->groups[0].type;
527             write_buf(buf, "\n\t\ttype= \"%s\",",
528                         xkb_atom_text(keymap->ctx, type->name));
529         }
530     }
531
532     if (key->explicit & EXPLICIT_REPEAT) {
533         if (key->repeats)
534             write_buf(buf, "\n\t\trepeat= Yes,");
535         else
536             write_buf(buf, "\n\t\trepeat= No,");
537         simple = false;
538     }
539
540     if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
541         write_buf(buf, "\n\t\tvirtualMods= %s,",
542                     ModMaskText(keymap, key->vmodmap));
543
544     switch (key->out_of_range_group_action) {
545     case RANGE_SATURATE:
546         write_buf(buf, "\n\t\tgroupsClamp,");
547         break;
548
549     case RANGE_REDIRECT:
550         write_buf(buf, "\n\t\tgroupsRedirect= Group%u,",
551                     key->out_of_range_group_number + 1);
552         break;
553
554     default:
555         break;
556     }
557
558     show_actions = !!(key->explicit & EXPLICIT_INTERP);
559
560     if (key->num_groups > 1 || show_actions)
561         simple = false;
562
563     if (simple) {
564         write_buf(buf, "\t[ ");
565         if (!write_keysyms(keymap, buf, key, 0))
566             return false;
567         write_buf(buf, " ] };\n");
568     }
569     else {
570         xkb_level_index_t level;
571
572         for (group = 0; group < key->num_groups; group++) {
573             if (group != 0)
574                 write_buf(buf, ",");
575             write_buf(buf, "\n\t\tsymbols[Group%u]= [ ", group + 1);
576             if (!write_keysyms(keymap, buf, key, group))
577                 return false;
578             write_buf(buf, " ]");
579             if (show_actions) {
580                 write_buf(buf, ",\n\t\tactions[Group%u]= [ ", group + 1);
581                 for (level = 0;
582                         level < XkbKeyGroupWidth(key, group); level++) {
583                     if (level != 0)
584                         write_buf(buf, ", ");
585                     write_action(keymap, buf,
586                                     &key->groups[group].levels[level].action,
587                                     NULL, NULL);
588                 }
589                 write_buf(buf, " ]");
590             }
591         }
592         write_buf(buf, "\n\t};\n");
593     }
594
595     return true;
596 }
597
598 static bool
599 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
600 {
601     const struct xkb_key *key;
602     xkb_layout_index_t group;
603
604     if (keymap->symbols_section_name)
605         write_buf(buf, "xkb_symbols \"%s\" {\n",
606                   keymap->symbols_section_name);
607     else
608         write_buf(buf, "xkb_symbols {\n");
609
610     for (group = 0; group < keymap->num_group_names; group++)
611         if (keymap->group_names[group])
612             write_buf(buf,
613                       "\tname[group%d]=\"%s\";\n", group + 1,
614                       xkb_atom_text(keymap->ctx, keymap->group_names[group]));
615     if (group > 0)
616         write_buf(buf, "\n");
617
618     xkb_foreach_key(key, keymap)
619         if (key->num_groups > 0)
620             write_key(keymap, buf, key);
621
622     xkb_foreach_key(key, keymap) {
623         xkb_mod_index_t i;
624         const struct xkb_mod *mod;
625
626         if (key->modmap == 0)
627             continue;
628
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));
634     }
635
636     write_buf(buf, "};\n\n");
637     return true;
638 }
639
640 static bool
641 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
642 {
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"));
649 }
650
651 char *
652 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
653 {
654     struct buf buf = { NULL, 0, 0 };
655
656     if (!write_keymap(keymap, &buf)) {
657         free(buf.buf);
658         return NULL;
659     }
660
661     return buf.buf;
662 }