text: explicitly take mod_type in mod functions
[platform/upstream/libxkbcommon.git] / src / 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 "keymap.h"
53 #include "text.h"
54
55 #define VMOD_HIDE_VALUE    0
56 #define VMOD_SHOW_VALUE    1
57 #define VMOD_COMMENT_VALUE 2
58
59 #define BUF_CHUNK_SIZE     4096
60
61 struct buf {
62     char *buf;
63     size_t size;
64     size_t alloc;
65 };
66
67 static bool
68 do_realloc(struct buf *buf, size_t at_least)
69 {
70     char *new;
71
72     buf->alloc += BUF_CHUNK_SIZE;
73     if (at_least >= BUF_CHUNK_SIZE)
74         buf->alloc += at_least;
75
76     new = realloc(buf->buf, buf->alloc);
77     if (!new)
78         return false;
79
80     buf->buf = new;
81     return true;
82 }
83
84 ATTR_PRINTF(2, 3) static bool
85 check_write_buf(struct buf *buf, const char *fmt, ...)
86 {
87     va_list args;
88     int printed;
89     size_t available;
90
91     available = buf->alloc - buf->size;
92     va_start(args, fmt);
93     printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
94     va_end(args);
95
96     if (printed < 0)
97         goto err;
98
99     if (printed >= available)
100         if (!do_realloc(buf, printed))
101             goto err;
102
103     /* The buffer has enough space now. */
104
105     available = buf->alloc - buf->size;
106     va_start(args, fmt);
107     printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
108     va_end(args);
109
110     if (printed >= available || printed < 0)
111         goto err;
112
113     buf->size += printed;
114     return true;
115
116 err:
117     free(buf->buf);
118     buf->buf = NULL;
119     return false;
120 }
121
122 #define write_buf(buf, ...) do { \
123     if (!check_write_buf(buf, __VA_ARGS__)) \
124         return false; \
125 } while (0)
126
127 static bool
128 write_vmods(struct xkb_keymap *keymap, struct buf *buf)
129 {
130     const struct xkb_mod *mod;
131     xkb_mod_index_t num_vmods = 0;
132
133     darray_foreach(mod, keymap->mods) {
134         if (mod->type != MOD_VIRT)
135             continue;
136
137         if (num_vmods == 0)
138             write_buf(buf, "\t\tvirtual_modifiers ");
139         else
140             write_buf(buf, ",");
141         write_buf(buf, "%s", xkb_atom_text(keymap->ctx, mod->name));
142         num_vmods++;
143     }
144
145     if (num_vmods > 0)
146         write_buf(buf, ";\n\n");
147
148     return true;
149 }
150
151 #define GET_TEXT_BUF_SIZE 512
152
153 #define append_get_text(...) do { \
154         int _size = snprintf(ret, GET_TEXT_BUF_SIZE, __VA_ARGS__); \
155         if (_size >= GET_TEXT_BUF_SIZE) \
156             return NULL; \
157 } while (0)
158
159 static char *
160 get_indicator_state_text(enum xkb_state_component which)
161 {
162     unsigned int i;
163     static char ret[GET_TEXT_BUF_SIZE];
164
165     memset(ret, 0, GET_TEXT_BUF_SIZE);
166
167     if (which == 0) {
168         strcpy(ret, "0");
169         return NULL;
170     }
171
172     for (i = 0; which != 0; i++) {
173         const char *name;
174
175         if (!(which & (1 << i)))
176             continue;
177
178         which &= ~(1 << i);
179         name = LookupValue(modComponentMaskNames, (1 << i));
180
181         if (ret[0] != '\0')
182             append_get_text("%s+%s", ret, name);
183         else
184             append_get_text("%s", name);
185     }
186
187     return ret;
188 }
189
190 static char *
191 get_control_mask_text(enum xkb_action_controls control_mask)
192 {
193     int i;
194     static char ret[GET_TEXT_BUF_SIZE];
195     const char *control_name;
196
197     memset(ret, 0, GET_TEXT_BUF_SIZE);
198
199     if (control_mask == 0) {
200         strcpy(ret, "none");
201         return ret;
202     }
203     else if (control_mask == CONTROL_ALL) {
204         strcpy(ret, "all");
205         return ret;
206     }
207
208     for (i = 0; control_mask; i++) {
209         if (!(control_mask & (1 << i)))
210             continue;
211
212         control_mask &= ~(1 << i);
213         control_name = LookupValue(ctrlMaskNames, (1 << i));
214
215         if (ret[0] != '\0')
216             append_get_text("%s+%s", ret, control_name);
217         else
218             append_get_text("%s", control_name);
219     }
220
221     return ret;
222 }
223
224 static bool
225 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
226 {
227     struct xkb_key *key;
228     struct xkb_key_alias *alias;
229     xkb_led_index_t i;
230
231     if (keymap->keycodes_section_name)
232         write_buf(buf, "\txkb_keycodes \"%s\" {\n",
233                   keymap->keycodes_section_name);
234     else
235         write_buf(buf, "\txkb_keycodes {\n");
236
237     xkb_foreach_key(key, keymap) {
238         if (key->name == XKB_ATOM_NONE)
239             continue;
240
241         write_buf(buf, "\t\t%-20s = %d;\n",
242                   KeyNameText(keymap->ctx, key->name), key->keycode);
243     }
244
245     for (i = 0; i < XKB_NUM_INDICATORS; i++) {
246         if (keymap->indicators[i].name == XKB_ATOM_NONE)
247             continue;
248         write_buf(buf, "\t\tindicator %d = \"%s\";\n", i + 1,
249                   xkb_atom_text(keymap->ctx, keymap->indicators[i].name));
250     }
251
252
253     darray_foreach(alias, keymap->key_aliases)
254         write_buf(buf, "\t\talias %-14s = %s;\n",
255                   KeyNameText(keymap->ctx, alias->alias),
256                   KeyNameText(keymap->ctx, alias->real));
257
258     write_buf(buf, "\t};\n\n");
259     return true;
260 }
261
262 static bool
263 write_types(struct xkb_keymap *keymap, struct buf *buf)
264 {
265     unsigned int i, j;
266     xkb_level_index_t n;
267     struct xkb_key_type *type;
268     struct xkb_kt_map_entry *entry;
269
270     if (keymap->types_section_name)
271         write_buf(buf, "\txkb_types \"%s\" {\n\n",
272                   keymap->types_section_name);
273     else
274         write_buf(buf, "\txkb_types {\n\n");
275
276     write_vmods(keymap, buf);
277
278     for (i = 0; i < keymap->num_types; i++) {
279         type = &keymap->types[i];
280
281         write_buf(buf, "\t\ttype \"%s\" {\n",
282                   xkb_atom_text(keymap->ctx, type->name));
283         write_buf(buf, "\t\t\tmodifiers= %s;\n",
284                   ModMaskText(keymap, type->mods.mods, MOD_BOTH));
285
286         for (j = 0; j < type->num_entries; j++) {
287             const char *str;
288             entry = &type->map[j];
289
290             /*
291              * Printing level 1 entries is redundant, it's the default,
292              * unless there's preserve info.
293              */
294             if (entry->level == 0 && entry->preserve.mods == 0)
295                 continue;
296
297             str = ModMaskText(keymap, entry->mods.mods, MOD_BOTH);
298             write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
299                       str, entry->level + 1);
300
301             if (entry->preserve.mods == 0)
302                 continue;
303
304             write_buf(buf, "\t\t\tpreserve[%s]= ", str);
305             write_buf(buf, "%s;\n",
306                       ModMaskText(keymap, entry->preserve.mods, MOD_BOTH));
307         }
308
309         if (type->level_names) {
310             for (n = 0; n < type->num_levels; n++) {
311                 if (!type->level_names[n])
312                     continue;
313                 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
314                           xkb_atom_text(keymap->ctx, type->level_names[n]));
315             }
316         }
317         write_buf(buf, "\t\t};\n");
318     }
319
320     write_buf(buf, "\t};\n\n");
321     return true;
322 }
323
324 static bool
325 write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
326 {
327     struct xkb_indicator_map *led = &keymap->indicators[num];
328
329     write_buf(buf, "\t\tindicator \"%s\" {\n",
330               xkb_atom_text(keymap->ctx, keymap->indicators[num].name));
331
332     if (led->which_groups) {
333         if (led->which_groups != XKB_STATE_EFFECTIVE) {
334             write_buf(buf, "\t\t\twhichGroupState= %s;\n",
335                       get_indicator_state_text(led->which_groups));
336         }
337         write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
338                   led->groups);
339     }
340
341     if (led->which_mods) {
342         if (led->which_mods != XKB_STATE_EFFECTIVE) {
343             write_buf(buf, "\t\t\twhichModState= %s;\n",
344                       get_indicator_state_text(led->which_mods));
345         }
346         write_buf(buf, "\t\t\tmodifiers= %s;\n",
347                   ModMaskText(keymap, led->mods.mods, MOD_BOTH));
348     }
349
350     if (led->ctrls) {
351         write_buf(buf, "\t\t\tcontrols= %s;\n",
352                   get_control_mask_text(led->ctrls));
353     }
354
355     write_buf(buf, "\t\t};\n");
356     return true;
357 }
358
359 static bool
360 write_action(struct xkb_keymap *keymap, struct buf *buf,
361              const union xkb_action *action,
362              const char *prefix, const char *suffix)
363 {
364     const char *type;
365     const char *args = NULL;
366
367     if (!prefix)
368         prefix = "";
369     if (!suffix)
370         suffix = "";
371
372     type = ActionTypeText(action->type);
373
374     switch (action->type) {
375     case ACTION_TYPE_MOD_SET:
376     case ACTION_TYPE_MOD_LATCH:
377     case ACTION_TYPE_MOD_LOCK:
378         if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
379             args = "modMapMods";
380         else
381             args = ModMaskText(keymap, action->mods.mods.mods, MOD_BOTH);
382         write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
383                   (action->type != ACTION_TYPE_MOD_LOCK &&
384                    (action->mods.flags & ACTION_LOCK_CLEAR)) ?
385                    ",clearLocks" : "",
386                   (action->type != ACTION_TYPE_MOD_LOCK &&
387                    (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
388                    ",latchToLock" : "",
389                   suffix);
390         break;
391
392     case ACTION_TYPE_GROUP_SET:
393     case ACTION_TYPE_GROUP_LATCH:
394     case ACTION_TYPE_GROUP_LOCK:
395         write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
396                   (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
397                    action->group.group > 0) ? "+" : "",
398                   (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
399                   action->group.group + 1 : action->group.group,
400                   (action->type != ACTION_TYPE_GROUP_LOCK &&
401                    (action->group.flags & ACTION_LOCK_CLEAR)) ?
402                   ",clearLocks" : "",
403                   (action->type != ACTION_TYPE_GROUP_LOCK &&
404                    (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
405                   ",latchToLock" : "",
406                   suffix);
407         break;
408
409     case ACTION_TYPE_TERMINATE:
410         write_buf(buf, "%s%s()%s", prefix, type, suffix);
411         break;
412
413     case ACTION_TYPE_PTR_MOVE:
414         write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
415                   (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
416                    action->ptr.x >= 0) ? "+" : "",
417                   action->ptr.x,
418                   (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
419                    action->ptr.y >= 0) ? "+" : "",
420                   action->ptr.y,
421                   (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
422                   suffix);
423         break;
424
425     case ACTION_TYPE_PTR_LOCK:
426         switch (action->btn.flags &
427                  (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
428         case ACTION_LOCK_NO_UNLOCK:
429             args = ",affect=lock";
430             break;
431
432         case ACTION_LOCK_NO_LOCK:
433             args = ",affect=unlock";
434             break;
435
436         case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
437             args = ",affect=neither";
438             break;
439
440         default:
441             args = ",affect=both";
442             break;
443         }
444     case ACTION_TYPE_PTR_BUTTON:
445         write_buf(buf, "%s%s(button=", prefix, type);
446         if (action->btn.button > 0 && action->btn.button <= 5)
447             write_buf(buf, "%d", action->btn.button);
448         else
449             write_buf(buf, "default");
450         if (action->btn.count)
451             write_buf(buf, ",count=%d", action->btn.count);
452         if (args)
453             write_buf(buf, "%s", args);
454         write_buf(buf, ")%s", suffix);
455         break;
456
457     case ACTION_TYPE_PTR_DEFAULT:
458         write_buf(buf, "%s%s(", prefix, type);
459         write_buf(buf, "affect=button,button=%s%d",
460                   (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
461                    action->dflt.value >= 0) ? "+" : "",
462                   action->dflt.value);
463         write_buf(buf, ")%s", suffix);
464         break;
465
466     case ACTION_TYPE_SWITCH_VT:
467         write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
468                   (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
469                    action->screen.screen >= 0) ? "+" : "",
470                   action->screen.screen,
471                   (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
472                   suffix);
473         break;
474
475     case ACTION_TYPE_CTRL_SET:
476     case ACTION_TYPE_CTRL_LOCK:
477         write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
478                   get_control_mask_text(action->ctrls.ctrls), suffix);
479         break;
480
481     case ACTION_TYPE_NONE:
482         write_buf(buf, "%sNoAction()%s", prefix, suffix);
483         break;
484
485     default:
486         write_buf(buf,
487                   "%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",
488                   prefix, type, action->type, action->priv.data[0],
489                   action->priv.data[1], action->priv.data[2],
490                   action->priv.data[3], action->priv.data[4],
491                   action->priv.data[5], action->priv.data[6],
492                   suffix);
493         break;
494     }
495
496     return true;
497 }
498
499 static bool
500 write_compat(struct xkb_keymap *keymap, struct buf *buf)
501 {
502     int i;
503     struct xkb_sym_interpret *interp;
504
505     if (keymap->compat_section_name)
506         write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
507                   keymap->compat_section_name);
508     else
509         write_buf(buf, "\txkb_compatibility {\n\n");
510
511     write_vmods(keymap, buf);
512
513     write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
514     write_buf(buf, "\t\tinterpret.repeat= False;\n");
515
516     darray_foreach(interp, keymap->sym_interpret) {
517         char keysym_name[64];
518
519         if (interp->sym == XKB_KEY_NoSymbol)
520             sprintf(keysym_name, "Any");
521         else
522             xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
523
524         write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
525                   keysym_name,
526                   SIMatchText(interp->match),
527                   ModMaskText(keymap, interp->mods, MOD_REAL));
528
529         if (interp->virtual_mod != XKB_MOD_INVALID)
530             write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
531                       ModIndexText(keymap, interp->virtual_mod, MOD_VIRT));
532
533         if (interp->match & MATCH_LEVEL_ONE_ONLY)
534             write_buf(buf, "\t\t\tuseModMapMods=level1;\n");
535         if (interp->repeat)
536             write_buf(buf, "\t\t\trepeat= True;\n");
537
538         write_action(keymap, buf, &interp->act, "\t\t\taction= ", ";\n");
539         write_buf(buf, "\t\t};\n");
540     }
541
542     for (i = 0; i < XKB_NUM_INDICATORS; i++) {
543         struct xkb_indicator_map *map = &keymap->indicators[i];
544         if (map->which_groups == 0 && map->groups == 0 &&
545             map->which_mods == 0 && map->mods.mods == 0 &&
546             map->ctrls == 0)
547             continue;
548         write_indicator_map(keymap, buf, i);
549     }
550
551     write_buf(buf, "\t};\n\n");
552
553     return true;
554 }
555
556 static bool
557 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
558               struct xkb_key *key, xkb_layout_index_t group)
559 {
560     const xkb_keysym_t *syms;
561     int num_syms;
562     xkb_level_index_t level;
563 #define OUT_BUF_LEN 128
564     char out_buf[OUT_BUF_LEN];
565
566     for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
567         if (level != 0)
568             write_buf(buf, ", ");
569         num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
570                                                     group, level, &syms);
571         if (num_syms == 0) {
572             write_buf(buf, "%15s", "NoSymbol");
573         }
574         else if (num_syms == 1) {
575             xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
576             write_buf(buf, "%15s", out_buf);
577         }
578         else {
579             int s;
580             write_buf(buf, "{ ");
581             for (s = 0; s < num_syms; s++) {
582                 if (s != 0)
583                     write_buf(buf, ", ");
584                 xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
585                 write_buf(buf, "%s", out_buf);
586             }
587             write_buf(buf, " }");
588         }
589     }
590 #undef OUT_BUF_LEN
591
592     return true;
593 }
594
595 static bool
596 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
597 {
598     struct xkb_key *key;
599     xkb_layout_index_t group, tmp;
600     xkb_atom_t *group_name;
601     bool showActions;
602
603     if (keymap->symbols_section_name)
604         write_buf(buf, "\txkb_symbols \"%s\" {\n\n",
605                   keymap->symbols_section_name);
606     else
607         write_buf(buf, "\txkb_symbols {\n\n");
608
609     tmp = 0;
610     darray_enumerate(group, group_name, keymap->group_names) {
611         if (!*group_name)
612             continue;
613         write_buf(buf,
614                   "\t\tname[group%d]=\"%s\";\n", group + 1,
615                   xkb_atom_text(keymap->ctx, *group_name));
616         tmp++;
617     }
618     if (tmp > 0)
619         write_buf(buf, "\n");
620
621     xkb_foreach_key(key, keymap) {
622         bool simple = true;
623         bool explicit_types = false;
624         bool multi_type = false;
625
626         if (key->num_groups == 0)
627             continue;
628
629         write_buf(buf, "\t\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
630
631         for (group = 0; group < key->num_groups; group++) {
632             if (key->groups[group].explicit_type)
633                 explicit_types = true;
634
635             if (group != 0 && key->groups[group].type != key->groups[0].type)
636                 multi_type = true;
637         }
638
639         if (explicit_types) {
640             const struct xkb_key_type *type;
641             simple = false;
642
643             if (multi_type) {
644                 for (group = 0; group < key->num_groups; group++) {
645                     if (!key->groups[group].explicit_type)
646                         continue;
647
648                     type = key->groups[group].type;
649                     write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
650                               group + 1,
651                               xkb_atom_text(keymap->ctx, type->name));
652                 }
653             }
654             else {
655                 type = key->groups[0].type;
656                 write_buf(buf, "\n\t\t\ttype= \"%s\",",
657                           xkb_atom_text(keymap->ctx, type->name));
658             }
659         }
660
661         if (key->explicit & EXPLICIT_REPEAT) {
662             if (key->repeats)
663                 write_buf(buf, "\n\t\t\trepeat= Yes,");
664             else
665                 write_buf(buf, "\n\t\t\trepeat= No,");
666             simple = false;
667         }
668
669         if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
670             write_buf(buf, "\n\t\t\tvirtualMods= %s,",
671                       ModMaskText(keymap, key->vmodmap, MOD_VIRT));
672
673         switch (key->out_of_range_group_action) {
674         case RANGE_SATURATE:
675             write_buf(buf, "\n\t\t\tgroupsClamp,");
676             break;
677
678         case RANGE_REDIRECT:
679             write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
680                       key->out_of_range_group_number + 1);
681             break;
682
683         default:
684             break;
685         }
686
687         showActions = !!(key->explicit & EXPLICIT_INTERP);
688
689         if (key->num_groups > 1 || showActions)
690             simple = false;
691
692         if (simple) {
693             write_buf(buf, "\t[ ");
694             if (!write_keysyms(keymap, buf, key, 0))
695                 return false;
696             write_buf(buf, " ] };\n");
697         }
698         else {
699             xkb_level_index_t level;
700
701             for (group = 0; group < key->num_groups; group++) {
702                 if (group != 0)
703                     write_buf(buf, ",");
704                 write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
705                 if (!write_keysyms(keymap, buf, key, group))
706                     return false;
707                 write_buf(buf, " ]");
708                 if (showActions) {
709                     write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
710                               group + 1);
711                     for (level = 0;
712                          level < XkbKeyGroupWidth(key, group); level++) {
713                         if (level != 0)
714                             write_buf(buf, ", ");
715                         write_action(keymap, buf,
716                                      &key->groups[group].levels[level].action,
717                                      NULL, NULL);
718                     }
719                     write_buf(buf, " ]");
720                 }
721             }
722             write_buf(buf, "\n\t\t};\n");
723         }
724     }
725
726     xkb_foreach_key(key, keymap) {
727         xkb_mod_index_t i;
728         const struct xkb_mod *mod;
729
730         if (key->modmap == 0)
731             continue;
732
733         darray_enumerate(i, mod, keymap->mods) {
734             if (!(key->modmap & (1 << i)))
735                 continue;
736
737             write_buf(buf, "\t\tmodifier_map %s { %s };\n",
738                       xkb_atom_text(keymap->ctx, mod->name),
739                       KeyNameText(keymap->ctx, key->name));
740         }
741     }
742
743     write_buf(buf, "\t};\n\n");
744     return true;
745 }
746
747 XKB_EXPORT char *
748 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
749                          enum xkb_keymap_format format)
750 {
751     bool ok;
752     struct buf buf = { NULL, 0, 0 };
753
754     if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
755         format = keymap->format;
756
757     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
758         log_err(keymap->ctx,
759                 "Trying to get a keymap as a string in an unsupported format (%d)\n",
760                 format);
761         return NULL;
762     }
763
764     ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
765           write_keycodes(keymap, &buf) &&
766           write_types(keymap, &buf) &&
767           write_compat(keymap, &buf) &&
768           write_symbols(keymap, &buf) &&
769           check_write_buf(&buf, "};\n"));
770
771     return (ok ? buf.buf : NULL);
772 }