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