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