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