keymap-dump: unbreak some complex lines
[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 ((size_t) 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 < 0 || (size_t) printed >= available)
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     /* 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));
166
167     xkb_foreach_key(key, keymap) {
168         if (key->name == XKB_ATOM_NONE)
169             continue;
170
171         write_buf(buf, "\t%-20s = %u;\n",
172                   KeyNameText(keymap->ctx, key->name), key->keycode);
173     }
174
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));
179
180
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));
185
186     write_buf(buf, "};\n\n");
187     return true;
188 }
189
190 static bool
191 write_types(struct xkb_keymap *keymap, struct buf *buf)
192 {
193     if (keymap->types_section_name)
194         write_buf(buf, "xkb_types \"%s\" {\n",
195                   keymap->types_section_name);
196     else
197         write_buf(buf, "xkb_types {\n");
198
199     write_vmods(keymap, buf);
200
201     for (unsigned i = 0; i < keymap->num_types; i++) {
202         const struct xkb_key_type *type = &keymap->types[i];
203
204         write_buf(buf, "\ttype \"%s\" {\n",
205                   xkb_atom_text(keymap->ctx, type->name));
206
207         write_buf(buf, "\t\tmodifiers= %s;\n",
208                   ModMaskText(keymap, type->mods.mods));
209
210         for (unsigned j = 0; j < type->num_entries; j++) {
211             const char *str;
212             const struct xkb_key_type_entry *entry = &type->entries[j];
213
214             /*
215              * Printing level 1 entries is redundant, it's the default,
216              * unless there's preserve info.
217              */
218             if (entry->level == 0 && entry->preserve.mods == 0)
219                 continue;
220
221             str = ModMaskText(keymap, entry->mods.mods);
222             write_buf(buf, "\t\tmap[%s]= Level%u;\n",
223                       str, entry->level + 1);
224
225             if (entry->preserve.mods)
226                 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
227                           str, ModMaskText(keymap, entry->preserve.mods));
228         }
229
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]));
234
235         write_buf(buf, "\t};\n");
236     }
237
238     write_buf(buf, "};\n\n");
239     return true;
240 }
241
242 static bool
243 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
244               const struct xkb_led *led)
245 {
246     write_buf(buf, "\tindicator \"%s\" {\n",
247               xkb_atom_text(keymap->ctx, led->name));
248
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));
253         }
254         write_buf(buf, "\t\tgroups= 0x%02x;\n",
255                   led->groups);
256     }
257
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));
262         }
263         write_buf(buf, "\t\tmodifiers= %s;\n",
264                   ModMaskText(keymap, led->mods.mods));
265     }
266
267     if (led->ctrls) {
268         write_buf(buf, "\t\tcontrols= %s;\n",
269                   ControlMaskText(keymap->ctx, led->ctrls));
270     }
271
272     write_buf(buf, "\t};\n");
273     return true;
274 }
275
276 static bool
277 write_action(struct xkb_keymap *keymap, struct buf *buf,
278              const union xkb_action *action,
279              const char *prefix, const char *suffix)
280 {
281     const char *type;
282     const char *args = NULL;
283
284     if (!prefix)
285         prefix = "";
286     if (!suffix)
287         suffix = "";
288
289     type = ActionTypeText(action->type);
290
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)
296             args = "modMapMods";
297         else
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 && (action->mods.flags & ACTION_LOCK_CLEAR)) ? ",clearLocks" : "",
301                   (action->type != ACTION_TYPE_MOD_LOCK && (action->mods.flags & ACTION_LATCH_TO_LOCK)) ? ",latchToLock" : "",
302                   suffix);
303         break;
304
305     case ACTION_TYPE_GROUP_SET:
306     case ACTION_TYPE_GROUP_LATCH:
307     case ACTION_TYPE_GROUP_LOCK:
308         write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
309                   (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) && action->group.group > 0) ? "+" : "",
310                   (action->group.flags & ACTION_ABSOLUTE_SWITCH) ? action->group.group + 1 : action->group.group,
311                   (action->type != ACTION_TYPE_GROUP_LOCK && (action->group.flags & ACTION_LOCK_CLEAR)) ? ",clearLocks" : "",
312                   (action->type != ACTION_TYPE_GROUP_LOCK && (action->group.flags & ACTION_LATCH_TO_LOCK)) ? ",latchToLock" : "",
313                   suffix);
314         break;
315
316     case ACTION_TYPE_TERMINATE:
317         write_buf(buf, "%s%s()%s", prefix, type, suffix);
318         break;
319
320     case ACTION_TYPE_PTR_MOVE:
321         write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
322                   (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
323                    action->ptr.x >= 0) ? "+" : "",
324                   action->ptr.x,
325                   (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
326                    action->ptr.y >= 0) ? "+" : "",
327                   action->ptr.y,
328                   (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
329                   suffix);
330         break;
331
332     case ACTION_TYPE_PTR_LOCK:
333         switch (action->btn.flags &
334                  (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
335         case ACTION_LOCK_NO_UNLOCK:
336             args = ",affect=lock";
337             break;
338
339         case ACTION_LOCK_NO_LOCK:
340             args = ",affect=unlock";
341             break;
342
343         case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
344             args = ",affect=neither";
345             break;
346
347         default:
348             args = ",affect=both";
349             break;
350         }
351     case ACTION_TYPE_PTR_BUTTON:
352         write_buf(buf, "%s%s(button=", prefix, type);
353         if (action->btn.button > 0 && action->btn.button <= 5)
354             write_buf(buf, "%d", action->btn.button);
355         else
356             write_buf(buf, "default");
357         if (action->btn.count)
358             write_buf(buf, ",count=%d", action->btn.count);
359         if (args)
360             write_buf(buf, "%s", args);
361         write_buf(buf, ")%s", suffix);
362         break;
363
364     case ACTION_TYPE_PTR_DEFAULT:
365         write_buf(buf, "%s%s(", prefix, type);
366         write_buf(buf, "affect=button,button=%s%d",
367                   (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) && action->dflt.value >= 0) ? "+" : "",
368                   action->dflt.value);
369         write_buf(buf, ")%s", suffix);
370         break;
371
372     case ACTION_TYPE_SWITCH_VT:
373         write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
374                   (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) && action->screen.screen >= 0) ? "+" : "",
375                   action->screen.screen,
376                   (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
377                   suffix);
378         break;
379
380     case ACTION_TYPE_CTRL_SET:
381     case ACTION_TYPE_CTRL_LOCK:
382         write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
383                   ControlMaskText(keymap->ctx, action->ctrls.ctrls), suffix);
384         break;
385
386     case ACTION_TYPE_NONE:
387         write_buf(buf, "%sNoAction()%s", prefix, suffix);
388         break;
389
390     default:
391         write_buf(buf,
392                   "%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",
393                   prefix, type, action->type, action->priv.data[0],
394                   action->priv.data[1], action->priv.data[2],
395                   action->priv.data[3], action->priv.data[4],
396                   action->priv.data[5], action->priv.data[6],
397                   suffix);
398         break;
399     }
400
401     return true;
402 }
403
404 static bool
405 write_compat(struct xkb_keymap *keymap, struct buf *buf)
406 {
407     const struct xkb_led *led;
408
409     if (keymap->compat_section_name)
410         write_buf(buf, "xkb_compatibility \"%s\" {\n",
411                   keymap->compat_section_name);
412     else
413         write_buf(buf, "xkb_compatibility {\n");
414
415     write_vmods(keymap, buf);
416
417     write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
418     write_buf(buf, "\tinterpret.repeat= False;\n");
419
420     for (unsigned i = 0; i < keymap->num_sym_interprets; i++) {
421         const struct xkb_sym_interpret *si = &keymap->sym_interprets[i];
422
423         write_buf(buf, "\tinterpret %s+%s(%s) {\n",
424                   si->sym ? KeysymText(keymap->ctx, si->sym) : "Any",
425                   SIMatchText(si->match),
426                   ModMaskText(keymap, si->mods));
427
428         if (si->virtual_mod != XKB_MOD_INVALID)
429             write_buf(buf, "\t\tvirtualModifier= %s;\n",
430                       ModIndexText(keymap, si->virtual_mod));
431
432         if (si->level_one_only)
433             write_buf(buf, "\t\tuseModMapMods=level1;\n");
434
435         if (si->repeat)
436             write_buf(buf, "\t\trepeat= True;\n");
437
438         write_action(keymap, buf, &si->action, "\t\taction= ", ";\n");
439         write_buf(buf, "\t};\n");
440     }
441
442     darray_foreach(led, keymap->leds)
443         if (led->which_groups || led->groups || led->which_mods ||
444             led->mods.mods || led->ctrls)
445             write_led_map(keymap, buf, led);
446
447     write_buf(buf, "};\n\n");
448
449     return true;
450 }
451
452 static bool
453 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
454               const struct xkb_key *key, xkb_layout_index_t group)
455 {
456     for (xkb_level_index_t level = 0; level < XkbKeyGroupWidth(key, group);
457          level++) {
458         const xkb_keysym_t *syms;
459         int num_syms;
460
461         if (level != 0)
462             write_buf(buf, ", ");
463
464         num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
465                                                     group, level, &syms);
466         if (num_syms == 0) {
467             write_buf(buf, "%15s", "NoSymbol");
468         }
469         else if (num_syms == 1) {
470             write_buf(buf, "%15s", KeysymText(keymap->ctx, syms[0]));
471         }
472         else {
473             write_buf(buf, "{ ");
474             for (int s = 0; s < num_syms; s++) {
475                 if (s != 0)
476                     write_buf(buf, ", ");
477                 write_buf(buf, "%s", KeysymText(keymap->ctx, syms[s]));
478             }
479             write_buf(buf, " }");
480         }
481     }
482
483     return true;
484 }
485
486 static bool
487 write_key(struct xkb_keymap *keymap, struct buf *buf,
488           const struct xkb_key *key)
489 {
490     xkb_layout_index_t group;
491     bool simple = true;
492     bool explicit_types = false;
493     bool multi_type = false;
494     bool show_actions;
495
496     write_buf(buf, "\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
497
498     for (group = 0; group < key->num_groups; group++) {
499         if (key->groups[group].explicit_type)
500             explicit_types = true;
501
502         if (group != 0 && key->groups[group].type != key->groups[0].type)
503             multi_type = true;
504     }
505
506     if (explicit_types) {
507         const struct xkb_key_type *type;
508         simple = false;
509
510         if (multi_type) {
511             for (group = 0; group < key->num_groups; group++) {
512                 if (!key->groups[group].explicit_type)
513                     continue;
514
515                 type = key->groups[group].type;
516                 write_buf(buf, "\n\t\ttype[group%u]= \"%s\",",
517                             group + 1,
518                             xkb_atom_text(keymap->ctx, type->name));
519             }
520         }
521         else {
522             type = key->groups[0].type;
523             write_buf(buf, "\n\t\ttype= \"%s\",",
524                         xkb_atom_text(keymap->ctx, type->name));
525         }
526     }
527
528     if (key->explicit & EXPLICIT_REPEAT) {
529         if (key->repeats)
530             write_buf(buf, "\n\t\trepeat= Yes,");
531         else
532             write_buf(buf, "\n\t\trepeat= No,");
533         simple = false;
534     }
535
536     if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
537         write_buf(buf, "\n\t\tvirtualMods= %s,",
538                     ModMaskText(keymap, key->vmodmap));
539
540     switch (key->out_of_range_group_action) {
541     case RANGE_SATURATE:
542         write_buf(buf, "\n\t\tgroupsClamp,");
543         break;
544
545     case RANGE_REDIRECT:
546         write_buf(buf, "\n\t\tgroupsRedirect= Group%u,",
547                     key->out_of_range_group_number + 1);
548         break;
549
550     default:
551         break;
552     }
553
554     show_actions = !!(key->explicit & EXPLICIT_INTERP);
555
556     if (key->num_groups > 1 || show_actions)
557         simple = false;
558
559     if (simple) {
560         write_buf(buf, "\t[ ");
561         if (!write_keysyms(keymap, buf, key, 0))
562             return false;
563         write_buf(buf, " ] };\n");
564     }
565     else {
566         xkb_level_index_t level;
567
568         for (group = 0; group < key->num_groups; group++) {
569             if (group != 0)
570                 write_buf(buf, ",");
571             write_buf(buf, "\n\t\tsymbols[Group%u]= [ ", group + 1);
572             if (!write_keysyms(keymap, buf, key, group))
573                 return false;
574             write_buf(buf, " ]");
575             if (show_actions) {
576                 write_buf(buf, ",\n\t\tactions[Group%u]= [ ", group + 1);
577                 for (level = 0;
578                         level < XkbKeyGroupWidth(key, group); level++) {
579                     if (level != 0)
580                         write_buf(buf, ", ");
581                     write_action(keymap, buf,
582                                     &key->groups[group].levels[level].action,
583                                     NULL, NULL);
584                 }
585                 write_buf(buf, " ]");
586             }
587         }
588         write_buf(buf, "\n\t};\n");
589     }
590
591     return true;
592 }
593
594 static bool
595 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
596 {
597     const struct xkb_key *key;
598     xkb_layout_index_t group;
599
600     if (keymap->symbols_section_name)
601         write_buf(buf, "xkb_symbols \"%s\" {\n",
602                   keymap->symbols_section_name);
603     else
604         write_buf(buf, "xkb_symbols {\n");
605
606     for (group = 0; group < keymap->num_group_names; group++)
607         if (keymap->group_names[group])
608             write_buf(buf,
609                       "\tname[group%u]=\"%s\";\n", group + 1,
610                       xkb_atom_text(keymap->ctx, keymap->group_names[group]));
611     if (group > 0)
612         write_buf(buf, "\n");
613
614     xkb_foreach_key(key, keymap)
615         if (key->num_groups > 0)
616             write_key(keymap, buf, key);
617
618     xkb_foreach_key(key, keymap) {
619         xkb_mod_index_t i;
620         const struct xkb_mod *mod;
621
622         if (key->modmap == 0)
623             continue;
624
625         darray_enumerate(i, mod, keymap->mods)
626             if (key->modmap & (1u << i))
627                 write_buf(buf, "\tmodifier_map %s { %s };\n",
628                           xkb_atom_text(keymap->ctx, mod->name),
629                           KeyNameText(keymap->ctx, key->name));
630     }
631
632     write_buf(buf, "};\n\n");
633     return true;
634 }
635
636 static bool
637 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
638 {
639     return (check_write_buf(buf, "xkb_keymap {\n") &&
640             write_keycodes(keymap, buf) &&
641             write_types(keymap, buf) &&
642             write_compat(keymap, buf) &&
643             write_symbols(keymap, buf) &&
644             check_write_buf(buf, "};\n"));
645 }
646
647 char *
648 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
649 {
650     struct buf buf = { NULL, 0, 0 };
651
652     if (!write_keymap(keymap, &buf)) {
653         free(buf.buf);
654         return NULL;
655     }
656
657     return buf.buf;
658 }