keymap: remove XkbKeyGetKeycode
[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     int num_vmods = 0;
131     int i;
132
133     for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
134         if (!keymap->vmod_names[i])
135             continue;
136         if (num_vmods == 0)
137             write_buf(buf, "\t\tvirtual_modifiers ");
138         else
139             write_buf(buf, ",");
140         write_buf(buf, "%s",
141                   xkb_atom_text(keymap->ctx, keymap->vmod_names[i]));
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(uint8_t which)
161 {
162     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     int 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[0] == '\0')
239             continue;
240
241         write_buf(buf, "\t\t%6s = %d;\n",
242                   KeyNameText(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 %6s = %6s;\n",
255                   KeyNameText(alias->alias),
256                   KeyNameText(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                   VModMaskText(keymap, type->mods.mods));
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 = VModMaskText(keymap, entry->mods.mods);
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", VModMaskText(keymap, entry->preserve.mods));
306         }
307
308         if (type->level_names) {
309             for (n = 0; n < type->num_levels; n++) {
310                 if (!type->level_names[n])
311                     continue;
312                 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
313                           xkb_atom_text(keymap->ctx, type->level_names[n]));
314             }
315         }
316         write_buf(buf, "\t\t};\n");
317     }
318
319     write_buf(buf, "\t};\n\n");
320     return true;
321 }
322
323 static bool
324 write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
325 {
326     struct xkb_indicator_map *led = &keymap->indicators[num];
327
328     write_buf(buf, "\t\tindicator \"%s\" {\n",
329               xkb_atom_text(keymap->ctx, keymap->indicators[num].name));
330
331     if (led->which_groups) {
332         if (led->which_groups != XKB_STATE_EFFECTIVE) {
333             write_buf(buf, "\t\t\twhichGroupState= %s;\n",
334                       get_indicator_state_text(led->which_groups));
335         }
336         write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
337                   led->groups);
338     }
339
340     if (led->which_mods) {
341         if (led->which_mods != XKB_STATE_EFFECTIVE) {
342             write_buf(buf, "\t\t\twhichModState= %s;\n",
343                       get_indicator_state_text(led->which_mods));
344         }
345         write_buf(buf, "\t\t\tmodifiers= %s;\n",
346                   VModMaskText(keymap, led->mods.mods));
347     }
348
349     if (led->ctrls) {
350         write_buf(buf, "\t\t\tcontrols= %s;\n",
351                   get_control_mask_text(led->ctrls));
352     }
353
354     write_buf(buf, "\t\t};\n");
355     return true;
356 }
357
358 static bool
359 write_action(struct xkb_keymap *keymap, struct buf *buf,
360              const union xkb_action *action,
361              const char *prefix, const char *suffix)
362 {
363     const char *type;
364     const char *args = NULL;
365
366     if (!prefix)
367         prefix = "";
368     if (!suffix)
369         suffix = "";
370
371     type = ActionTypeText(action->type);
372
373     switch (action->type) {
374     case ACTION_TYPE_MOD_SET:
375     case ACTION_TYPE_MOD_LATCH:
376     case ACTION_TYPE_MOD_LOCK:
377         if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
378             args = "modMapMods";
379         else
380             args = VModMaskText(keymap, action->mods.mods.mods);
381         write_buf(buf, "%s%s(modifiers=%s%s%s)%s", prefix, type, args,
382                   (action->type != ACTION_TYPE_MOD_LOCK &&
383                    (action->mods.flags & ACTION_LOCK_CLEAR)) ?
384                    ",clearLocks" : "",
385                   (action->type != ACTION_TYPE_MOD_LOCK &&
386                    (action->mods.flags & ACTION_LATCH_TO_LOCK)) ?
387                    ",latchToLock" : "",
388                   suffix);
389         break;
390
391     case ACTION_TYPE_GROUP_SET:
392     case ACTION_TYPE_GROUP_LATCH:
393     case ACTION_TYPE_GROUP_LOCK:
394         write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
395                   (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) &&
396                    action->group.group > 0) ? "+" : "",
397                   (action->group.flags & ACTION_ABSOLUTE_SWITCH) ?
398                   action->group.group + 1 : action->group.group,
399                   (action->type != ACTION_TYPE_GROUP_LOCK &&
400                    (action->group.flags & ACTION_LOCK_CLEAR)) ?
401                   ",clearLocks" : "",
402                   (action->type != ACTION_TYPE_GROUP_LOCK &&
403                    (action->group.flags & ACTION_LATCH_TO_LOCK)) ?
404                   ",latchToLock" : "",
405                   suffix);
406         break;
407
408     case ACTION_TYPE_TERMINATE:
409         write_buf(buf, "%s%s()%s", prefix, type, suffix);
410         break;
411
412     case ACTION_TYPE_PTR_MOVE:
413         write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
414                   (!(action->ptr.flags & ACTION_ABSOLUTE_X) &&
415                    action->ptr.x >= 0) ? "+" : "",
416                   action->ptr.x,
417                   (!(action->ptr.flags & ACTION_ABSOLUTE_Y) &&
418                    action->ptr.y >= 0) ? "+" : "",
419                   action->ptr.y,
420                   (action->ptr.flags & ACTION_NO_ACCEL) ? ",!accel" : "",
421                   suffix);
422         break;
423
424     case ACTION_TYPE_PTR_LOCK:
425         switch (action->btn.flags &
426                  (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
427         case ACTION_LOCK_NO_UNLOCK:
428             args = ",affect=lock";
429             break;
430
431         case ACTION_LOCK_NO_LOCK:
432             args = ",affect=unlock";
433             break;
434
435         case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
436             args = ",affect=neither";
437             break;
438
439         default:
440             args = ",affect=both";
441             break;
442         }
443     case ACTION_TYPE_PTR_BUTTON:
444         write_buf(buf, "%s%s(button=", prefix, type);
445         if (action->btn.button > 0 && action->btn.button <= 5)
446             write_buf(buf, "%d", action->btn.button);
447         else
448             write_buf(buf, "default");
449         if (action->btn.count)
450             write_buf(buf, ",count=%d", action->btn.count);
451         if (args)
452             write_buf(buf, "%s", args);
453         write_buf(buf, ")%s", suffix);
454         break;
455
456     case ACTION_TYPE_PTR_DEFAULT:
457         write_buf(buf, "%s%s(", prefix, type);
458         write_buf(buf, "affect=button,button=%s%d",
459                   (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) &&
460                    action->dflt.value >= 0) ? "+" : "",
461                   action->dflt.value);
462         write_buf(buf, ")%s", suffix);
463         break;
464
465     case ACTION_TYPE_SWITCH_VT:
466         write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
467                   (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) &&
468                    action->screen.screen >= 0) ? "+" : "",
469                   action->screen.screen,
470                   (action->screen.flags & ACTION_SAME_SCREEN) ? "!" : "",
471                   suffix);
472         break;
473
474     case ACTION_TYPE_CTRL_SET:
475     case ACTION_TYPE_CTRL_LOCK:
476         write_buf(buf, "%s%s(controls=%s)%s", prefix, type,
477                   get_control_mask_text(action->ctrls.ctrls), suffix);
478         break;
479
480     case ACTION_TYPE_NONE:
481         write_buf(buf, "%sNoAction()%s", prefix, suffix);
482         break;
483
484     default:
485         write_buf(buf,
486                   "%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",
487                   prefix, type, action->type, action->priv.data[0],
488                   action->priv.data[1], action->priv.data[2],
489                   action->priv.data[3], action->priv.data[4],
490                   action->priv.data[5], action->priv.data[6],
491                   suffix);
492         break;
493     }
494
495     return true;
496 }
497
498 static bool
499 write_compat(struct xkb_keymap *keymap, struct buf *buf)
500 {
501     int i;
502     struct xkb_sym_interpret *interp;
503
504     if (keymap->compat_section_name)
505         write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
506                   keymap->compat_section_name);
507     else
508         write_buf(buf, "\txkb_compatibility {\n\n");
509
510     write_vmods(keymap, buf);
511
512     write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
513     write_buf(buf, "\t\tinterpret.repeat= False;\n");
514
515     darray_foreach(interp, keymap->sym_interpret) {
516         char keysym_name[64];
517
518         if (interp->sym == XKB_KEY_NoSymbol)
519             sprintf(keysym_name, "Any");
520         else
521             xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
522
523         write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
524                   keysym_name,
525                   SIMatchText(interp->match),
526                   VModMaskText(keymap, interp->mods));
527
528         if (interp->virtual_mod != XKB_MOD_INVALID) {
529             write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
530                       xkb_atom_text(keymap->ctx,
531                                     keymap->vmod_names[interp->virtual_mod]));
532         }
533
534         if (interp->match & MATCH_LEVEL_ONE_ONLY)
535             write_buf(buf,
536                       "\t\t\tuseModMapMods=level1;\n");
537         if (interp->repeat)
538             write_buf(buf, "\t\t\trepeat= True;\n");
539
540         write_action(keymap, buf, &interp->act, "\t\t\taction= ", ";\n");
541         write_buf(buf, "\t\t};\n");
542     }
543
544     for (i = 0; i < XKB_NUM_INDICATORS; i++) {
545         struct xkb_indicator_map *map = &keymap->indicators[i];
546         if (map->which_groups == 0 && map->groups == 0 &&
547             map->which_mods == 0 && map->mods.mods == 0 &&
548             map->ctrls == 0)
549             continue;
550         write_indicator_map(keymap, buf, i);
551     }
552
553     write_buf(buf, "\t};\n\n");
554
555     return true;
556 }
557
558 static bool
559 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
560               struct xkb_key *key, xkb_layout_index_t group)
561 {
562     const xkb_keysym_t *syms;
563     int num_syms;
564     xkb_level_index_t level;
565 #define OUT_BUF_LEN 128
566     char out_buf[OUT_BUF_LEN];
567
568     for (level = 0; level < XkbKeyGroupWidth(keymap, key, group); level++) {
569         if (level != 0)
570             write_buf(buf, ", ");
571         num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
572                                                     group, level, &syms);
573         if (num_syms == 0) {
574             write_buf(buf, "%15s", "NoSymbol");
575         }
576         else if (num_syms == 1) {
577             xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
578             write_buf(buf, "%15s", out_buf);
579         }
580         else {
581             int s;
582             write_buf(buf, "{ ");
583             for (s = 0; s < num_syms; s++) {
584                 if (s != 0)
585                     write_buf(buf, ", ");
586                 xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
587                 write_buf(buf, "%s", out_buf);
588             }
589             write_buf(buf, " }");
590         }
591     }
592 #undef OUT_BUF_LEN
593
594     return true;
595 }
596
597 static bool
598 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
599 {
600     struct xkb_key *key;
601     xkb_layout_index_t group, tmp;
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     for (tmp = group = 0; group < XKB_NUM_GROUPS; group++) {
611         if (!keymap->group_names[group])
612             continue;
613         write_buf(buf,
614                   "\t\tname[group%d]=\"%s\";\n", group + 1,
615                   xkb_atom_text(keymap->ctx, keymap->group_names[group]));
616         tmp++;
617     }
618     if (tmp > 0)
619         write_buf(buf, "\n");
620
621     xkb_foreach_key(key, keymap) {
622         bool simple = true;
623
624         if (key->num_groups == 0)
625             continue;
626
627         write_buf(buf, "\t\tkey %6s {", KeyNameText(key->name));
628
629         if (key->explicit_groups) {
630             bool multi_type = false;
631             struct xkb_key_type *type = XkbKeyType(keymap, key, 0);
632
633             simple = false;
634
635             for (group = 1; group < key->num_groups; group++) {
636                 if (XkbKeyType(keymap, key, group) != type) {
637                     multi_type = true;
638                     break;
639                 }
640             }
641
642             if (multi_type) {
643                 for (group = 0; group < key->num_groups; group++) {
644                     if (!(key->explicit_groups & (1 << group)))
645                         continue;
646                     type = XkbKeyType(keymap, key, group);
647                     write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
648                               group + 1,
649                               xkb_atom_text(keymap->ctx, type->name));
650                 }
651             }
652             else {
653                 write_buf(buf, "\n\t\t\ttype= \"%s\",",
654                           xkb_atom_text(keymap->ctx, type->name));
655             }
656         }
657
658         if (key->explicit & EXPLICIT_REPEAT) {
659             if (key->repeats)
660                 write_buf(buf, "\n\t\t\trepeat= Yes,");
661             else
662                 write_buf(buf, "\n\t\t\trepeat= No,");
663             simple = false;
664         }
665
666         if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP)) {
667             /* XXX: vmodmap cmask? */
668             write_buf(buf, "\n\t\t\tvirtualMods= %s,",
669                       VModMaskText(keymap, key->vmodmap << XKB_NUM_CORE_MODS));
670         }
671
672         switch (key->out_of_range_group_action) {
673         case RANGE_SATURATE:
674             write_buf(buf, "\n\t\t\tgroupsClamp,");
675             break;
676
677         case RANGE_REDIRECT:
678             write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
679                       key->out_of_range_group_number + 1);
680             break;
681
682         default:
683             break;
684         }
685
686         if (key->explicit & EXPLICIT_INTERP)
687             showActions = (key->actions != NULL);
688         else
689             showActions = false;
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(keymap, key, group);
715                          level++) {
716                         if (level != 0)
717                             write_buf(buf, ", ");
718                         write_action(keymap, buf,
719                                      XkbKeyActionEntry(key, group, level),
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         int 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), KeyNameText(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 {
751     bool ok;
752     struct buf buf = { NULL, 0, 0 };
753
754     ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
755           write_keycodes(keymap, &buf) &&
756           write_types(keymap, &buf) &&
757           write_compat(keymap, &buf) &&
758           write_symbols(keymap, &buf) &&
759           check_write_buf(&buf, "};\n"));
760
761     return (ok ? buf.buf : NULL);
762 }