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