build: include config.h manually
[platform/upstream/libxkbcommon.git] / src / xkbcomp / 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 "xkbcomp-priv.h"
55 #include "text.h"
56
57 #define BUF_CHUNK_SIZE 4096
58
59 struct buf {
60     char *buf;
61     size_t size;
62     size_t alloc;
63 };
64
65 static bool
66 do_realloc(struct buf *buf, size_t at_least)
67 {
68     char *new;
69
70     buf->alloc += BUF_CHUNK_SIZE;
71     if (at_least >= BUF_CHUNK_SIZE)
72         buf->alloc += at_least;
73
74     new = realloc(buf->buf, buf->alloc);
75     if (!new)
76         return false;
77
78     buf->buf = new;
79     return true;
80 }
81
82 ATTR_PRINTF(2, 3) static bool
83 check_write_buf(struct buf *buf, const char *fmt, ...)
84 {
85     va_list args;
86     int printed;
87     size_t available;
88
89     available = buf->alloc - buf->size;
90     va_start(args, fmt);
91     printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
92     va_end(args);
93
94     if (printed < 0)
95         goto err;
96
97     if ((size_t) printed >= available)
98         if (!do_realloc(buf, printed))
99             goto err;
100
101     /* The buffer has enough space now. */
102
103     available = buf->alloc - buf->size;
104     va_start(args, fmt);
105     printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
106     va_end(args);
107
108     if (printed < 0 || (size_t) printed >= available)
109         goto err;
110
111     buf->size += printed;
112     return true;
113
114 err:
115     free(buf->buf);
116     buf->buf = NULL;
117     return false;
118 }
119
120 #define write_buf(buf, ...) do { \
121     if (!check_write_buf(buf, __VA_ARGS__)) \
122         return false; \
123 } while (0)
124
125 static bool
126 write_vmods(struct xkb_keymap *keymap, struct buf *buf)
127 {
128     const struct xkb_mod *mod;
129     xkb_mod_index_t num_vmods = 0;
130
131     xkb_mods_foreach(mod, &keymap->mods) {
132         if (mod->type != MOD_VIRT)
133             continue;
134
135         if (num_vmods == 0)
136             write_buf(buf, "\tvirtual_modifiers ");
137         else
138             write_buf(buf, ",");
139         write_buf(buf, "%s", xkb_atom_text(keymap->ctx, mod->name));
140         num_vmods++;
141     }
142
143     if (num_vmods > 0)
144         write_buf(buf, ";\n\n");
145
146     return true;
147 }
148
149 static bool
150 write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
151 {
152     const struct xkb_key *key;
153     xkb_led_index_t idx;
154     const struct xkb_led *led;
155
156     if (keymap->keycodes_section_name)
157         write_buf(buf, "xkb_keycodes \"%s\" {\n",
158                   keymap->keycodes_section_name);
159     else
160         write_buf(buf, "xkb_keycodes {\n");
161
162     /* xkbcomp and X11 really want to see keymaps with a minimum of 8, and
163      * a maximum of at least 255, else XWayland really starts hating life.
164      * If this is a problem and people really need strictly bounded keymaps,
165      * we should probably control this with a flag. */
166     write_buf(buf, "\tminimum = %u;\n", MIN(keymap->min_key_code, 8));
167     write_buf(buf, "\tmaximum = %u;\n", MAX(keymap->max_key_code, 255));
168
169     xkb_keys_foreach(key, keymap) {
170         if (key->name == XKB_ATOM_NONE)
171             continue;
172
173         write_buf(buf, "\t%-20s = %u;\n",
174                   KeyNameText(keymap->ctx, key->name), key->keycode);
175     }
176
177     xkb_leds_enumerate(idx, led, keymap)
178         if (led->name != XKB_ATOM_NONE)
179             write_buf(buf, "\tindicator %u = \"%s\";\n",
180                       idx + 1, xkb_atom_text(keymap->ctx, led->name));
181
182
183     for (unsigned i = 0; i < keymap->num_key_aliases; i++)
184         write_buf(buf, "\talias %-14s = %s;\n",
185                   KeyNameText(keymap->ctx, keymap->key_aliases[i].alias),
186                   KeyNameText(keymap->ctx, keymap->key_aliases[i].real));
187
188     write_buf(buf, "};\n\n");
189     return true;
190 }
191
192 static bool
193 write_types(struct xkb_keymap *keymap, struct buf *buf)
194 {
195     if (keymap->types_section_name)
196         write_buf(buf, "xkb_types \"%s\" {\n",
197                   keymap->types_section_name);
198     else
199         write_buf(buf, "xkb_types {\n");
200
201     write_vmods(keymap, buf);
202
203     for (unsigned i = 0; i < keymap->num_types; i++) {
204         const struct xkb_key_type *type = &keymap->types[i];
205
206         write_buf(buf, "\ttype \"%s\" {\n",
207                   xkb_atom_text(keymap->ctx, type->name));
208
209         write_buf(buf, "\t\tmodifiers= %s;\n",
210                   ModMaskText(keymap->ctx, &keymap->mods, type->mods.mods));
211
212         for (unsigned j = 0; j < type->num_entries; j++) {
213             const char *str;
214             const struct xkb_key_type_entry *entry = &type->entries[j];
215
216             /*
217              * Printing level 1 entries is redundant, it's the default,
218              * unless there's preserve info.
219              */
220             if (entry->level == 0 && entry->preserve.mods == 0)
221                 continue;
222
223             str = ModMaskText(keymap->ctx, &keymap->mods, entry->mods.mods);
224             write_buf(buf, "\t\tmap[%s]= %u;\n",
225                       str, entry->level + 1);
226
227             if (entry->preserve.mods)
228                 write_buf(buf, "\t\tpreserve[%s]= %s;\n",
229                           str, ModMaskText(keymap->ctx, &keymap->mods,
230                                            entry->preserve.mods));
231         }
232
233         for (xkb_level_index_t n = 0; n < type->num_level_names; n++)
234             if (type->level_names[n])
235                 write_buf(buf, "\t\tlevel_name[%u]= \"%s\";\n", n + 1,
236                           xkb_atom_text(keymap->ctx, type->level_names[n]));
237
238         write_buf(buf, "\t};\n");
239     }
240
241     write_buf(buf, "};\n\n");
242     return true;
243 }
244
245 static bool
246 write_led_map(struct xkb_keymap *keymap, struct buf *buf,
247               const struct xkb_led *led)
248 {
249     write_buf(buf, "\tindicator \"%s\" {\n",
250               xkb_atom_text(keymap->ctx, led->name));
251
252     if (led->which_groups) {
253         if (led->which_groups != XKB_STATE_LAYOUT_EFFECTIVE) {
254             write_buf(buf, "\t\twhichGroupState= %s;\n",
255                       LedStateMaskText(keymap->ctx, led->which_groups));
256         }
257         write_buf(buf, "\t\tgroups= 0x%02x;\n",
258                   led->groups);
259     }
260
261     if (led->which_mods) {
262         if (led->which_mods != XKB_STATE_MODS_EFFECTIVE) {
263             write_buf(buf, "\t\twhichModState= %s;\n",
264                       LedStateMaskText(keymap->ctx, led->which_mods));
265         }
266         write_buf(buf, "\t\tmodifiers= %s;\n",
267                   ModMaskText(keymap->ctx, &keymap->mods, led->mods.mods));
268     }
269
270     if (led->ctrls) {
271         write_buf(buf, "\t\tcontrols= %s;\n",
272                   ControlMaskText(keymap->ctx, led->ctrls));
273     }
274
275     write_buf(buf, "\t};\n");
276     return true;
277 }
278
279 static const char *
280 affect_lock_text(enum xkb_action_flags flags)
281 {
282     switch (flags & (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK)) {
283     case ACTION_LOCK_NO_UNLOCK:
284         return ",affect=lock";
285     case ACTION_LOCK_NO_LOCK:
286         return ",affect=unlock";
287     case ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK:
288         return ",affect=neither";
289     }
290     return "";
291 }
292
293 static bool
294 write_action(struct xkb_keymap *keymap, struct buf *buf,
295              const union xkb_action *action,
296              const char *prefix, const char *suffix)
297 {
298     const char *type;
299     const char *args = NULL;
300
301     if (!prefix)
302         prefix = "";
303     if (!suffix)
304         suffix = "";
305
306     type = ActionTypeText(action->type);
307
308     switch (action->type) {
309     case ACTION_TYPE_MOD_SET:
310     case ACTION_TYPE_MOD_LATCH:
311     case ACTION_TYPE_MOD_LOCK:
312         if (action->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
313             args = "modMapMods";
314         else
315             args = ModMaskText(keymap->ctx, &keymap->mods,
316                                action->mods.mods.mods);
317         write_buf(buf, "%s%s(modifiers=%s%s%s%s)%s", prefix, type, args,
318                   (action->type != ACTION_TYPE_MOD_LOCK && (action->mods.flags & ACTION_LOCK_CLEAR)) ? ",clearLocks" : "",
319                   (action->type != ACTION_TYPE_MOD_LOCK && (action->mods.flags & ACTION_LATCH_TO_LOCK)) ? ",latchToLock" : "",
320                   (action->type == ACTION_TYPE_MOD_LOCK) ? affect_lock_text(action->mods.flags) : "",
321                   suffix);
322         break;
323
324     case ACTION_TYPE_GROUP_SET:
325     case ACTION_TYPE_GROUP_LATCH:
326     case ACTION_TYPE_GROUP_LOCK:
327         write_buf(buf, "%s%s(group=%s%d%s%s)%s", prefix, type,
328                   (!(action->group.flags & ACTION_ABSOLUTE_SWITCH) && action->group.group > 0) ? "+" : "",
329                   (action->group.flags & ACTION_ABSOLUTE_SWITCH) ? action->group.group + 1 : action->group.group,
330                   (action->type != ACTION_TYPE_GROUP_LOCK && (action->group.flags & ACTION_LOCK_CLEAR)) ? ",clearLocks" : "",
331                   (action->type != ACTION_TYPE_GROUP_LOCK && (action->group.flags & ACTION_LATCH_TO_LOCK)) ? ",latchToLock" : "",
332                   suffix);
333         break;
334
335     case ACTION_TYPE_TERMINATE:
336         write_buf(buf, "%s%s()%s", prefix, type, suffix);
337         break;
338
339     case ACTION_TYPE_PTR_MOVE:
340         write_buf(buf, "%s%s(x=%s%d,y=%s%d%s)%s", prefix, type,
341                   (!(action->ptr.flags & ACTION_ABSOLUTE_X) && action->ptr.x >= 0) ? "+" : "",
342                   action->ptr.x,
343                   (!(action->ptr.flags & ACTION_ABSOLUTE_Y) && action->ptr.y >= 0) ? "+" : "",
344                   action->ptr.y,
345                   (action->ptr.flags & ACTION_ACCEL) ? "" : ",!accel",
346                   suffix);
347         break;
348
349     case ACTION_TYPE_PTR_LOCK:
350         args = affect_lock_text(action->btn.flags);
351         /* fallthrough */
352     case ACTION_TYPE_PTR_BUTTON:
353         write_buf(buf, "%s%s(button=", prefix, type);
354         if (action->btn.button > 0 && action->btn.button <= 5)
355             write_buf(buf, "%d", action->btn.button);
356         else
357             write_buf(buf, "default");
358         if (action->btn.count)
359             write_buf(buf, ",count=%d", action->btn.count);
360         if (args)
361             write_buf(buf, "%s", args);
362         write_buf(buf, ")%s", suffix);
363         break;
364
365     case ACTION_TYPE_PTR_DEFAULT:
366         write_buf(buf, "%s%s(", prefix, type);
367         write_buf(buf, "affect=button,button=%s%d",
368                   (!(action->dflt.flags & ACTION_ABSOLUTE_SWITCH) && action->dflt.value >= 0) ? "+" : "",
369                   action->dflt.value);
370         write_buf(buf, ")%s", suffix);
371         break;
372
373     case ACTION_TYPE_SWITCH_VT:
374         write_buf(buf, "%s%s(screen=%s%d,%ssame)%s", prefix, type,
375                   (!(action->screen.flags & ACTION_ABSOLUTE_SWITCH) && action->screen.screen >= 0) ? "+" : "",
376                   action->screen.screen,
377                   (action->screen.flags & ACTION_SAME_SCREEN) ? "" : "!",
378                   suffix);
379         break;
380
381     case ACTION_TYPE_CTRL_SET:
382     case ACTION_TYPE_CTRL_LOCK:
383         write_buf(buf, "%s%s(controls=%s%s)%s", prefix, type,
384                   ControlMaskText(keymap->ctx, action->ctrls.ctrls),
385                   (action->type == ACTION_TYPE_CTRL_LOCK) ? affect_lock_text(action->ctrls.flags) : "",
386                   suffix);
387         break;
388
389     case ACTION_TYPE_NONE:
390         write_buf(buf, "%sNoAction()%s", prefix, suffix);
391         break;
392
393     default:
394         write_buf(buf,
395                   "%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",
396                   prefix, type, action->type, action->priv.data[0],
397                   action->priv.data[1], action->priv.data[2],
398                   action->priv.data[3], action->priv.data[4],
399                   action->priv.data[5], action->priv.data[6],
400                   suffix);
401         break;
402     }
403
404     return true;
405 }
406
407 static bool
408 write_compat(struct xkb_keymap *keymap, struct buf *buf)
409 {
410     const struct xkb_led *led;
411
412     if (keymap->compat_section_name)
413         write_buf(buf, "xkb_compatibility \"%s\" {\n",
414                   keymap->compat_section_name);
415     else
416         write_buf(buf, "xkb_compatibility {\n");
417
418     write_vmods(keymap, buf);
419
420     write_buf(buf, "\tinterpret.useModMapMods= AnyLevel;\n");
421     write_buf(buf, "\tinterpret.repeat= False;\n");
422
423     for (unsigned i = 0; i < keymap->num_sym_interprets; i++) {
424         const struct xkb_sym_interpret *si = &keymap->sym_interprets[i];
425
426         write_buf(buf, "\tinterpret %s+%s(%s) {\n",
427                   si->sym ? KeysymText(keymap->ctx, si->sym) : "Any",
428                   SIMatchText(si->match),
429                   ModMaskText(keymap->ctx, &keymap->mods, si->mods));
430
431         if (si->virtual_mod != XKB_MOD_INVALID)
432             write_buf(buf, "\t\tvirtualModifier= %s;\n",
433                       ModIndexText(keymap->ctx, &keymap->mods,
434                                    si->virtual_mod));
435
436         if (si->level_one_only)
437             write_buf(buf, "\t\tuseModMapMods=level1;\n");
438
439         if (si->repeat)
440             write_buf(buf, "\t\trepeat= True;\n");
441
442         write_action(keymap, buf, &si->action, "\t\taction= ", ";\n");
443         write_buf(buf, "\t};\n");
444     }
445
446     xkb_leds_foreach(led, keymap)
447         if (led->which_groups || led->groups || led->which_mods ||
448             led->mods.mods || led->ctrls)
449             write_led_map(keymap, buf, led);
450
451     write_buf(buf, "};\n\n");
452
453     return true;
454 }
455
456 static bool
457 write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
458               const struct xkb_key *key, xkb_layout_index_t group)
459 {
460     for (xkb_level_index_t level = 0; level < XkbKeyNumLevels(key, group);
461          level++) {
462         const xkb_keysym_t *syms;
463         int num_syms;
464
465         if (level != 0)
466             write_buf(buf, ", ");
467
468         num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode,
469                                                     group, level, &syms);
470         if (num_syms == 0) {
471             write_buf(buf, "%15s", "NoSymbol");
472         }
473         else if (num_syms == 1) {
474             write_buf(buf, "%15s", KeysymText(keymap->ctx, syms[0]));
475         }
476         else {
477             write_buf(buf, "{ ");
478             for (int s = 0; s < num_syms; s++) {
479                 if (s != 0)
480                     write_buf(buf, ", ");
481                 write_buf(buf, "%s", KeysymText(keymap->ctx, syms[s]));
482             }
483             write_buf(buf, " }");
484         }
485     }
486
487     return true;
488 }
489
490 static bool
491 write_key(struct xkb_keymap *keymap, struct buf *buf,
492           const struct xkb_key *key)
493 {
494     xkb_layout_index_t group;
495     bool simple = true;
496     bool explicit_types = false;
497     bool multi_type = false;
498     bool show_actions;
499
500     write_buf(buf, "\tkey %-20s {", KeyNameText(keymap->ctx, key->name));
501
502     for (group = 0; group < key->num_groups; group++) {
503         if (key->groups[group].explicit_type)
504             explicit_types = true;
505
506         if (group != 0 && key->groups[group].type != key->groups[0].type)
507             multi_type = true;
508     }
509
510     if (explicit_types) {
511         const struct xkb_key_type *type;
512         simple = false;
513
514         if (multi_type) {
515             for (group = 0; group < key->num_groups; group++) {
516                 if (!key->groups[group].explicit_type)
517                     continue;
518
519                 type = key->groups[group].type;
520                 write_buf(buf, "\n\t\ttype[Group%u]= \"%s\",",
521                             group + 1,
522                             xkb_atom_text(keymap->ctx, type->name));
523             }
524         }
525         else {
526             type = key->groups[0].type;
527             write_buf(buf, "\n\t\ttype= \"%s\",",
528                         xkb_atom_text(keymap->ctx, type->name));
529         }
530     }
531
532     if (key->explicit & EXPLICIT_REPEAT) {
533         if (key->repeats)
534             write_buf(buf, "\n\t\trepeat= Yes,");
535         else
536             write_buf(buf, "\n\t\trepeat= No,");
537         simple = false;
538     }
539
540     if (key->vmodmap && (key->explicit & EXPLICIT_VMODMAP))
541         write_buf(buf, "\n\t\tvirtualMods= %s,",
542                   ModMaskText(keymap->ctx, &keymap->mods, key->vmodmap));
543
544     switch (key->out_of_range_group_action) {
545     case RANGE_SATURATE:
546         write_buf(buf, "\n\t\tgroupsClamp,");
547         break;
548
549     case RANGE_REDIRECT:
550         write_buf(buf, "\n\t\tgroupsRedirect= Group%u,",
551                     key->out_of_range_group_number + 1);
552         break;
553
554     default:
555         break;
556     }
557
558     show_actions = (key->explicit & EXPLICIT_INTERP);
559
560     if (key->num_groups > 1 || show_actions)
561         simple = false;
562
563     if (simple) {
564         write_buf(buf, "\t[ ");
565         if (!write_keysyms(keymap, buf, key, 0))
566             return false;
567         write_buf(buf, " ] };\n");
568     }
569     else {
570         xkb_level_index_t level;
571
572         for (group = 0; group < key->num_groups; group++) {
573             if (group != 0)
574                 write_buf(buf, ",");
575             write_buf(buf, "\n\t\tsymbols[Group%u]= [ ", group + 1);
576             if (!write_keysyms(keymap, buf, key, group))
577                 return false;
578             write_buf(buf, " ]");
579             if (show_actions) {
580                 write_buf(buf, ",\n\t\tactions[Group%u]= [ ", group + 1);
581                 for (level = 0; level < XkbKeyNumLevels(key, group); level++) {
582                     if (level != 0)
583                         write_buf(buf, ", ");
584                     write_action(keymap, buf,
585                                     &key->groups[group].levels[level].action,
586                                     NULL, NULL);
587                 }
588                 write_buf(buf, " ]");
589             }
590         }
591         write_buf(buf, "\n\t};\n");
592     }
593
594     return true;
595 }
596
597 static bool
598 write_symbols(struct xkb_keymap *keymap, struct buf *buf)
599 {
600     const struct xkb_key *key;
601     xkb_layout_index_t group;
602     xkb_mod_index_t i;
603     const struct xkb_mod *mod;
604
605     if (keymap->symbols_section_name)
606         write_buf(buf, "xkb_symbols \"%s\" {\n",
607                   keymap->symbols_section_name);
608     else
609         write_buf(buf, "xkb_symbols {\n");
610
611     for (group = 0; group < keymap->num_group_names; group++)
612         if (keymap->group_names[group])
613             write_buf(buf,
614                       "\tname[Group%u]=\"%s\";\n", group + 1,
615                       xkb_atom_text(keymap->ctx, keymap->group_names[group]));
616     if (group > 0)
617         write_buf(buf, "\n");
618
619     xkb_keys_foreach(key, keymap)
620         if (key->num_groups > 0)
621             write_key(keymap, buf, key);
622
623     xkb_mods_enumerate(i, mod, &keymap->mods) {
624         bool had_any = false;
625         xkb_keys_foreach(key, keymap) {
626             if (key->modmap & (1u << i)) {
627                 if (!had_any)
628                     write_buf(buf, "\tmodifier_map %s { ",
629                               xkb_atom_text(keymap->ctx, mod->name));
630                 write_buf(buf, "%s%s",
631                           had_any ? ", " : "",
632                           KeyNameText(keymap->ctx, key->name));
633                 had_any = true;
634             }
635         }
636         if (had_any)
637             write_buf(buf, " };\n");
638     }
639
640     write_buf(buf, "};\n\n");
641     return true;
642 }
643
644 static bool
645 write_keymap(struct xkb_keymap *keymap, struct buf *buf)
646 {
647     return (check_write_buf(buf, "xkb_keymap {\n") &&
648             write_keycodes(keymap, buf) &&
649             write_types(keymap, buf) &&
650             write_compat(keymap, buf) &&
651             write_symbols(keymap, buf) &&
652             check_write_buf(buf, "};\n"));
653 }
654
655 char *
656 text_v1_keymap_get_as_string(struct xkb_keymap *keymap)
657 {
658     struct buf buf = { NULL, 0, 0 };
659
660     if (!write_keymap(keymap, &buf)) {
661         free(buf.buf);
662         return NULL;
663     }
664
665     return buf.buf;
666 }