Remove the XKB_NUM_VIRTUAL_MODIFIERS limit
[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_vmod *vmod;
131     xkb_mod_index_t num_vmods = 0;
132
133     darray_foreach(vmod, keymap->vmods) {
134         if (num_vmods == 0)
135             write_buf(buf, "\t\tvirtual_modifiers ");
136         else
137             write_buf(buf, ",");
138         write_buf(buf, "%s", xkb_atom_text(keymap->ctx, vmod->name));
139         num_vmods++;
140     }
141
142     if (num_vmods > 0)
143         write_buf(buf, ";\n\n");
144
145     return true;
146 }
147
148 #define GET_TEXT_BUF_SIZE 512
149
150 #define append_get_text(...) do { \
151         int _size = snprintf(ret, GET_TEXT_BUF_SIZE, __VA_ARGS__); \
152         if (_size >= GET_TEXT_BUF_SIZE) \
153             return NULL; \
154 } while (0)
155
156 static char *
157 get_indicator_state_text(enum xkb_state_component which)
158 {
159     unsigned int i;
160     static char ret[GET_TEXT_BUF_SIZE];
161
162     memset(ret, 0, GET_TEXT_BUF_SIZE);
163
164     if (which == 0) {
165         strcpy(ret, "0");
166         return NULL;
167     }
168
169     for (i = 0; which != 0; i++) {
170         const char *name;
171
172         if (!(which & (1 << i)))
173             continue;
174
175         which &= ~(1 << i);
176         name = LookupValue(modComponentMaskNames, (1 << i));
177
178         if (ret[0] != '\0')
179             append_get_text("%s+%s", ret, name);
180         else
181             append_get_text("%s", name);
182     }
183
184     return ret;
185 }
186
187 static char *
188 get_control_mask_text(enum xkb_action_controls control_mask)
189 {
190     int i;
191     static char ret[GET_TEXT_BUF_SIZE];
192     const char *control_name;
193
194     memset(ret, 0, GET_TEXT_BUF_SIZE);
195
196     if (control_mask == 0) {
197         strcpy(ret, "none");
198         return ret;
199     }
200     else if (control_mask == CONTROL_ALL) {
201         strcpy(ret, "all");
202         return ret;
203     }
204
205     for (i = 0; control_mask; i++) {
206         if (!(control_mask & (1 << i)))
207             continue;
208
209         control_mask &= ~(1 << i);
210         control_name = LookupValue(ctrlMaskNames, (1 << i));
211
212         if (ret[0] != '\0')
213             append_get_text("%s+%s", ret, control_name);
214         else
215             append_get_text("%s", control_name);
216     }
217
218     return ret;
219 }
220
221 static bool
222 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
223 {
224     struct xkb_key *key;
225     struct xkb_key_alias *alias;
226     xkb_led_index_t i;
227
228     if (keymap->keycodes_section_name)
229         write_buf(buf, "\txkb_keycodes \"%s\" {\n",
230                   keymap->keycodes_section_name);
231     else
232         write_buf(buf, "\txkb_keycodes {\n");
233
234     xkb_foreach_key(key, keymap) {
235         if (key->name == XKB_ATOM_NONE)
236             continue;
237
238         write_buf(buf, "\t\t%-20s = %d;\n",
239                   KeyNameText(keymap->ctx, key->name), key->keycode);
240     }
241
242     for (i = 0; i < XKB_NUM_INDICATORS; i++) {
243         if (keymap->indicators[i].name == XKB_ATOM_NONE)
244             continue;
245         write_buf(buf, "\t\tindicator %d = \"%s\";\n", i + 1,
246                   xkb_atom_text(keymap->ctx, keymap->indicators[i].name));
247     }
248
249
250     darray_foreach(alias, keymap->key_aliases)
251         write_buf(buf, "\t\talias %-14s = %s;\n",
252                   KeyNameText(keymap->ctx, alias->alias),
253                   KeyNameText(keymap->ctx, alias->real));
254
255     write_buf(buf, "\t};\n\n");
256     return true;
257 }
258
259 static bool
260 write_types(struct xkb_keymap *keymap, struct buf *buf)
261 {
262     unsigned int i, j;
263     xkb_level_index_t n;
264     struct xkb_key_type *type;
265     struct xkb_kt_map_entry *entry;
266
267     if (keymap->types_section_name)
268         write_buf(buf, "\txkb_types \"%s\" {\n\n",
269                   keymap->types_section_name);
270     else
271         write_buf(buf, "\txkb_types {\n\n");
272
273     write_vmods(keymap, buf);
274
275     for (i = 0; i < keymap->num_types; i++) {
276         type = &keymap->types[i];
277
278         write_buf(buf, "\t\ttype \"%s\" {\n",
279                   xkb_atom_text(keymap->ctx, type->name));
280         write_buf(buf, "\t\t\tmodifiers= %s;\n",
281                   VModMaskText(keymap, type->mods.mods));
282
283         for (j = 0; j < type->num_entries; j++) {
284             const char *str;
285             entry = &type->map[j];
286
287             /*
288              * Printing level 1 entries is redundant, it's the default,
289              * unless there's preserve info.
290              */
291             if (entry->level == 0 && entry->preserve.mods == 0)
292                 continue;
293
294             str = VModMaskText(keymap, entry->mods.mods);
295             write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
296                       str, entry->level + 1);
297
298             if (entry->preserve.mods == 0)
299                 continue;
300
301             write_buf(buf, "\t\t\tpreserve[%s]= ", str);
302             write_buf(buf, "%s;\n", VModMaskText(keymap, entry->preserve.mods));
303         }
304
305         if (type->level_names) {
306             for (n = 0; n < type->num_levels; n++) {
307                 if (!type->level_names[n])
308                     continue;
309                 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
310                           xkb_atom_text(keymap->ctx, type->level_names[n]));
311             }
312         }
313         write_buf(buf, "\t\t};\n");
314     }
315
316     write_buf(buf, "\t};\n\n");
317     return true;
318 }
319
320 static bool
321 write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
322 {
323     struct xkb_indicator_map *led = &keymap->indicators[num];
324
325     write_buf(buf, "\t\tindicator \"%s\" {\n",
326               xkb_atom_text(keymap->ctx, keymap->indicators[num].name));
327
328     if (led->which_groups) {
329         if (led->which_groups != XKB_STATE_EFFECTIVE) {
330             write_buf(buf, "\t\t\twhichGroupState= %s;\n",
331                       get_indicator_state_text(led->which_groups));
332         }
333         write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
334                   led->groups);
335     }
336
337     if (led->which_mods) {
338         if (led->which_mods != XKB_STATE_EFFECTIVE) {
339             write_buf(buf, "\t\t\twhichModState= %s;\n",
340                       get_indicator_state_text(led->which_mods));
341         }
342         write_buf(buf, "\t\t\tmodifiers= %s;\n",
343                   VModMaskText(keymap, led->mods.mods));
344     }
345
346     if (led->ctrls) {
347         write_buf(buf, "\t\t\tcontrols= %s;\n",
348                   get_control_mask_text(led->ctrls));
349     }
350
351     write_buf(buf, "\t\t};\n");
352     return true;
353 }
354
355 static bool
356 write_action(struct xkb_keymap *keymap, struct buf *buf,
357              const union xkb_action *action,
358              const char *prefix, const char *suffix)
359 {
360     const char *type;
361     const char *args = NULL;
362
363     if (!prefix)
364         prefix = "";
365     if (!suffix)
366         suffix = "";
367
368     type = ActionTypeText(action->type);
369
370     switch (action->type) {
371     case ACTION_TYPE_MOD_SET:
372     case ACTION_TYPE_MOD_LATCH:
373     case ACTION_TYPE_MOD_LOCK:
374         if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
375             args = "modMapMods";
376         else
377             args = VModMaskText(keymap, action->mods.mods.mods);
378         write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
379                   (action->type != ACTION_TYPE_MOD_LOCK &&
380                    (action->mods.flags & ACTION_LOCK_CLEAR)) ?
381                    ",clearLocks" : "",
382                   (action->type != ACTION_TYPE_MOD_LOCK &&
383                    (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
384                    ",latchToLock" : "",
385                   suffix);
386         break;
387
388     case ACTION_TYPE_GROUP_SET:
389     case ACTION_TYPE_GROUP_LATCH:
390     case ACTION_TYPE_GROUP_LOCK:
391         write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
392                   (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
393                    action->group.group > 0) ? "+" : "",
394                   (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
395                   action->group.group + 1 : action->group.group,
396                   (action->type != ACTION_TYPE_GROUP_LOCK &&
397                    (action->group.flags & ACTION_LOCK_CLEAR)) ?
398                   ",clearLocks" : "",
399                   (action->type != ACTION_TYPE_GROUP_LOCK &&
400                    (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
401                   ",latchToLock" : "",
402                   suffix);
403         break;
404
405     case ACTION_TYPE_TERMINATE:
406         write_buf(buf, "%s%s()%s", prefix, type, suffix);
407         break;
408
409     case ACTION_TYPE_PTR_MOVE:
410         write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
411                   (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
412                    action->ptr.x >= 0) ? "+" : "",
413                   action->ptr.x,
414                   (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
415                    action->ptr.y >= 0) ? "+" : "",
416                   action->ptr.y,
417                   (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
418                   suffix);
419         break;
420
421     case ACTION_TYPE_PTR_LOCK:
422         switch (action->btn.flags &
423                  (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
424         case ACTION_LOCK_NO_UNLOCK:
425             args = ",affect=lock";
426             break;
427
428         case ACTION_LOCK_NO_LOCK:
429             args = ",affect=unlock";
430             break;
431
432         case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
433             args = ",affect=neither";
434             break;
435
436         default:
437             args = ",affect=both";
438             break;
439         }
440     case ACTION_TYPE_PTR_BUTTON:
441         write_buf(buf, "%s%s(button=", prefix, type);
442         if (action->btn.button > 0 && action->btn.button <= 5)
443             write_buf(buf, "%d", action->btn.button);
444         else
445             write_buf(buf, "default");
446         if (action->btn.count)
447             write_buf(buf, ",count=%d", action->btn.count);
448         if (args)
449             write_buf(buf, "%s", args);
450         write_buf(buf, ")%s", suffix);
451         break;
452
453     case ACTION_TYPE_PTR_DEFAULT:
454         write_buf(buf, "%s%s(", prefix, type);
455         write_buf(buf, "affect=button,button=%s%d",
456                   (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
457                    action->dflt.value >= 0) ? "+" : "",
458                   action->dflt.value);
459         write_buf(buf, ")%s", suffix);
460         break;
461
462     case ACTION_TYPE_SWITCH_VT:
463         write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
464                   (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
465                    action->screen.screen >= 0) ? "+" : "",
466                   action->screen.screen,
467                   (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
468                   suffix);
469         break;
470
471     case ACTION_TYPE_CTRL_SET:
472     case ACTION_TYPE_CTRL_LOCK:
473         write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
474                   get_control_mask_text(action->ctrls.ctrls), suffix);
475         break;
476
477     case ACTION_TYPE_NONE:
478         write_buf(buf, "%sNoAction()%s", prefix, suffix);
479         break;
480
481     default:
482         write_buf(buf,
483                   "%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",
484                   prefix, type, action->type, action->priv.data[0],
485                   action->priv.data[1], action->priv.data[2],
486                   action->priv.data[3], action->priv.data[4],
487                   action->priv.data[5], action->priv.data[6],
488                   suffix);
489         break;
490     }
491
492     return true;
493 }
494
495 static bool
496 write_compat(struct xkb_keymap *keymap, struct buf *buf)
497 {
498     int i;
499     struct xkb_sym_interpret *interp;
500
501     if (keymap->compat_section_name)
502         write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
503                   keymap->compat_section_name);
504     else
505         write_buf(buf, "\txkb_compatibility {\n\n");
506
507     write_vmods(keymap, buf);
508
509     write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
510     write_buf(buf, "\t\tinterpret.repeat= False;\n");
511
512     darray_foreach(interp, keymap->sym_interpret) {
513         char keysym_name[64];
514
515         if (interp->sym == XKB_KEY_NoSymbol)
516             sprintf(keysym_name, "Any");
517         else
518             xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
519
520         write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
521                   keysym_name,
522                   SIMatchText(interp->match),
523                   VModMaskText(keymap, interp->mods));
524
525         if (interp->virtual_mod != XKB_MOD_INVALID) {
526             write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
527                       xkb_atom_text(keymap->ctx,
528                                     darray_item(keymap->vmods,
529                                                 interp->virtual_mod).name));
530         }
531
532         if (interp->match & MATCH_LEVEL_ONE_ONLY)
533             write_buf(buf,
534                       "\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             /* XXX: vmodmap cmask? */
671             write_buf(buf, "\n\t\t\tvirtualMods= %s,",
672                       VModMaskText(keymap, key->vmodmap << XKB_NUM_CORE_MODS));
673         }
674
675         switch (key->out_of_range_group_action) {
676         case RANGE_SATURATE:
677             write_buf(buf, "\n\t\t\tgroupsClamp,");
678             break;
679
680         case RANGE_REDIRECT:
681             write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
682                       key->out_of_range_group_number + 1);
683             break;
684
685         default:
686             break;
687         }
688
689         showActions = !!(key->explicit & EXPLICIT_INTERP);
690
691         if (key->num_groups > 1 || showActions)
692             simple = false;
693
694         if (simple) {
695             write_buf(buf, "\t[ ");
696             if (!write_keysyms(keymap, buf, key, 0))
697                 return false;
698             write_buf(buf, " ] };\n");
699         }
700         else {
701             xkb_level_index_t level;
702
703             for (group = 0; group < key->num_groups; group++) {
704                 if (group != 0)
705                     write_buf(buf, ",");
706                 write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
707                 if (!write_keysyms(keymap, buf, key, group))
708                     return false;
709                 write_buf(buf, " ]");
710                 if (showActions) {
711                     write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
712                               group + 1);
713                     for (level = 0;
714                          level < XkbKeyGroupWidth(key, group); level++) {
715                         if (level != 0)
716                             write_buf(buf, ", ");
717                         write_action(keymap, buf,
718                                      &key->groups[group].levels[level].action,
719                                      NULL, NULL);
720                     }
721                     write_buf(buf, " ]");
722                 }
723             }
724             write_buf(buf, "\n\t\t};\n");
725         }
726     }
727
728     xkb_foreach_key(key, keymap) {
729         xkb_mod_index_t mod;
730
731         if (key->modmap == 0)
732             continue;
733
734         for (mod = 0; mod < XKB_NUM_CORE_MODS; mod++) {
735             if (!(key->modmap & (1 << mod)))
736                 continue;
737
738             write_buf(buf, "\t\tmodifier_map %s { %s };\n",
739                       ModIndexToName(mod),
740                       KeyNameText(keymap->ctx, key->name));
741         }
742     }
743
744     write_buf(buf, "\t};\n\n");
745     return true;
746 }
747
748 XKB_EXPORT char *
749 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
750                          enum xkb_keymap_format format)
751 {
752     bool ok;
753     struct buf buf = { NULL, 0, 0 };
754
755     if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
756         format = keymap->format;
757
758     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
759         log_err(keymap->ctx,
760                 "Trying to get a keymap as a string in an unsupported format (%d)\n",
761                 format);
762         return NULL;
763     }
764
765     ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
766           write_keycodes(keymap, &buf) &&
767           write_types(keymap, &buf) &&
768           write_compat(keymap, &buf) &&
769           write_symbols(keymap, &buf) &&
770           check_write_buf(&buf, "};\n"));
771
772     return (ok ? buf.buf : NULL);
773 }