From: Ran Benita Date: Sun, 23 Sep 2012 15:52:51 +0000 (+0200) Subject: Add format argument to xkb_keymap_get_as_string X-Git-Tag: xkbcommon-0.2.0~97 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9a18b87251d1111987e98926a9aba9e72afc0259;p=platform%2Fupstream%2Flibxkbcommon.git Add format argument to xkb_keymap_get_as_string This function really needs a format argument, for symmetry with the keymap creation functions. If we add new formats, we will almost certainly want to add support for serializing it into a string. It would also allow to convert from one format to another, etc. The in the common case, the user would just want to use the format she used to create the keymap; for that we add a special XKB_KEYMAP_USE_ORIGINAL_FORMAT value, which will do that (it is defined to -1 outside of the enum because I have a feeling we might want to use 0 for something else). To support this we need to keep the format inside the keymap. While we're at it we also initialize keymap flags properly. This changes the API, but the old xkb_map_get_as_string name works as expected so this is the best time to do this. Signed-off-by: Ran Benita --- diff --git a/src/compat.c b/src/compat.c index 0b0adb7..244a723 100644 --- a/src/compat.c +++ b/src/compat.c @@ -60,7 +60,7 @@ xkb_map_new_from_string(struct xkb_context *context, const char *string, XKB_EXPORT char * xkb_map_get_as_string(struct xkb_keymap *keymap) { - return xkb_keymap_get_as_string(keymap); + return xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1); } XKB_EXPORT struct xkb_keymap * diff --git a/src/keymap-dump.c b/src/keymap-dump.c index 4ae5395..701441c 100644 --- a/src/keymap-dump.c +++ b/src/keymap-dump.c @@ -746,11 +746,22 @@ write_symbols(struct xkb_keymap *keymap, struct buf *buf) } XKB_EXPORT char * -xkb_keymap_get_as_string(struct xkb_keymap *keymap) +xkb_keymap_get_as_string(struct xkb_keymap *keymap, + enum xkb_keymap_format format) { bool ok; struct buf buf = { NULL, 0, 0 }; + if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT) + format = keymap->format; + + if (format != XKB_KEYMAP_FORMAT_TEXT_V1) { + log_err(keymap->ctx, + "Trying to get a keymap as a string in an unsupported format (%d)\n", + format); + return NULL; + } + ok = (check_write_buf(&buf, "xkb_keymap {\n") && write_keycodes(keymap, &buf) && write_types(keymap, &buf) && diff --git a/src/keymap.c b/src/keymap.c index 442dfff..13ad829 100644 --- a/src/keymap.c +++ b/src/keymap.c @@ -54,7 +54,9 @@ #include "text.h" struct xkb_keymap * -xkb_keymap_new(struct xkb_context *ctx) +xkb_keymap_new(struct xkb_context *ctx, + enum xkb_keymap_format format, + enum xkb_keymap_compile_flags flags) { struct xkb_keymap *keymap; @@ -65,6 +67,9 @@ xkb_keymap_new(struct xkb_context *ctx) keymap->refcnt = 1; keymap->ctx = xkb_context_ref(ctx); + keymap->format = format; + keymap->flags = flags; + return keymap; } diff --git a/src/keymap.h b/src/keymap.h index be22124..2f1e42e 100644 --- a/src/keymap.h +++ b/src/keymap.h @@ -342,6 +342,7 @@ struct xkb_keymap { int refcnt; enum xkb_keymap_compile_flags flags; + enum xkb_keymap_format format; unsigned int enabled_ctrls; @@ -421,6 +422,8 @@ XkbKeyActionEntry(const struct xkb_key *key, xkb_layout_index_t layout, } struct xkb_keymap * -xkb_keymap_new(struct xkb_context *ctx); +xkb_keymap_new(struct xkb_context *ctx, + enum xkb_keymap_format format, + enum xkb_keymap_compile_flags); #endif diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c index 884ab2f..426f202 100644 --- a/src/xkbcomp/xkbcomp.c +++ b/src/xkbcomp/xkbcomp.c @@ -31,11 +31,13 @@ #include "rules.h" static struct xkb_keymap * -compile_keymap_file(struct xkb_context *ctx, XkbFile *file) +compile_keymap_file(struct xkb_context *ctx, XkbFile *file, + enum xkb_keymap_format format, + enum xkb_keymap_compile_flags flags) { struct xkb_keymap *keymap; - keymap = xkb_keymap_new(ctx); + keymap = xkb_keymap_new(ctx, format, flags); if (!keymap) goto err; @@ -111,7 +113,7 @@ xkb_keymap_new_from_names(struct xkb_context *ctx, return NULL; } - keymap = compile_keymap_file(ctx, file); + keymap = compile_keymap_file(ctx, file, XKB_KEYMAP_FORMAT_TEXT_V1, flags); FreeXkbFile(file); return keymap; } @@ -142,7 +144,7 @@ xkb_keymap_new_from_string(struct xkb_context *ctx, return NULL; } - keymap = compile_keymap_file(ctx, file); + keymap = compile_keymap_file(ctx, file, format, flags); FreeXkbFile(file); return keymap; } @@ -173,7 +175,7 @@ xkb_keymap_new_from_file(struct xkb_context *ctx, return NULL; } - keymap = compile_keymap_file(ctx, xkb_file); + keymap = compile_keymap_file(ctx, xkb_file, format, flags); FreeXkbFile(xkb_file); return keymap; } diff --git a/test/print-compiled-keymap.c b/test/print-compiled-keymap.c index 95bd85a..0e14205 100644 --- a/test/print-compiled-keymap.c +++ b/test/print-compiled-keymap.c @@ -87,7 +87,7 @@ main(int argc, char *argv[]) goto err_ctx; } - dump = xkb_keymap_get_as_string(keymap); + dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1); if (!dump) { fprintf(stderr, "Couldn't get the keymap string\n"); goto err_map; diff --git a/test/stringcomp.c b/test/stringcomp.c index e00e92f..e95a3c4 100644 --- a/test/stringcomp.c +++ b/test/stringcomp.c @@ -47,7 +47,7 @@ main(int argc, char *argv[]) keymap = test_compile_string(ctx, original); assert(keymap); - dump = xkb_keymap_get_as_string(keymap); + dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT); assert(dump); if (!streq(original, dump)) { diff --git a/xkbcommon/xkbcommon-compat.h b/xkbcommon/xkbcommon-compat.h index 2cd764f..0142bca 100644 --- a/xkbcommon/xkbcommon-compat.h +++ b/xkbcommon/xkbcommon-compat.h @@ -40,7 +40,8 @@ xkb_keymap_new_from_file(context, file, format, flags) #define xkb_map_new_from_string(context, string, format, flags) \ xkb_keymap_new_from_string(context, string, format, flags) -#define xkb_map_get_as_string(keymap) xkb_keymap_get_as_string(keymap) +#define xkb_map_get_as_string(keymap) \ + xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1) #define xkb_map_ref(keymap) xkb_keymap_ref(keymap) #define xkb_map_unref(keymap) xkb_keymap_unref(keymap) diff --git a/xkbcommon/xkbcommon.h b/xkbcommon/xkbcommon.h index a26744c..1049ca5 100644 --- a/xkbcommon/xkbcommon.h +++ b/xkbcommon/xkbcommon.h @@ -558,11 +558,18 @@ xkb_keymap_ref(struct xkb_keymap *keymap); void xkb_keymap_unref(struct xkb_keymap *keymap); +/* See xkb_keymap_get_as_string(). */ +#define XKB_KEYMAP_USE_ORIGINAL_FORMAT ((enum xkb_keymap_format) -1) + /** * @brief Get the compiled keymap as a string. * - * @returns The compiled keymap as a NUL-terminated string, or NULL if - * unsuccessful. + * @param keymap The keymap to get as a string. + * @param format The keymap format to use for the string. You can pass + * in the special value XKB_KEYMAP_USE_ORIGINAL_FORMAT to use the format + * from which the keymap was originally created. + * + * @returns The keymap as a NUL-terminated string, or NULL if unsuccessful. * * The returned string may be fed back into xkb_map_new_from_string() to get * the exact same keymap (possibly in another process, etc.). @@ -571,7 +578,8 @@ xkb_keymap_unref(struct xkb_keymap *keymap); * by the caller. */ char * -xkb_keymap_get_as_string(struct xkb_keymap *keymap); +xkb_keymap_get_as_string(struct xkb_keymap *keymap, + enum xkb_keymap_format format); /** @} */