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