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