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 <ran234@gmail.com>
master
Ran Benita 2012-09-23 17:52:51 +02:00 committed by Daniel Stone
parent 5d31b9e3e7
commit 9a18b87251
9 changed files with 45 additions and 15 deletions

View File

@ -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 *

View File

@ -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) &&

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -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)) {

View File

@ -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)

View File

@ -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);
/** @} */