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