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