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