keymap-dump: clean up write_buf function

It would have been nice to use open_memstream here if it was portable
enough (maybe someday it will?).

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2012-08-02 20:55:12 +03:00
parent 4c21275301
commit 20bef734d8
1 changed files with 183 additions and 208 deletions

View File

@ -49,8 +49,7 @@
* Author: Daniel Stone <daniel@fooishbar.org> * Author: Daniel Stone <daniel@fooishbar.org>
*/ */
#include <config.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
@ -64,62 +63,74 @@
#define BUF_CHUNK_SIZE 4096 #define BUF_CHUNK_SIZE 4096
struct buf {
char *buf;
size_t size;
size_t alloc;
};
static bool static bool
do_realloc(char **buf, size_t *size, size_t offset, size_t at_least) do_realloc(struct buf *buf, size_t at_least)
{ {
char *new; char *new;
*size += BUF_CHUNK_SIZE; buf->alloc += BUF_CHUNK_SIZE;
if (at_least >= BUF_CHUNK_SIZE) if (at_least >= BUF_CHUNK_SIZE)
*size += at_least; buf->alloc += at_least;
new = realloc(*buf, *size); new = realloc(buf->buf, buf->alloc);
if (!new) if (!new)
return false; return false;
*buf = new;
memset(*buf + offset, 0, *size - offset);
buf->buf = new;
return true; return true;
} }
/* This whole thing should be a function, but you can't call vsnprintf ATTR_PRINTF(2, 3) static bool
* multiple times. */ check_write_buf(struct buf *buf, const char *fmt, ...)
#define check_write_buf(keymap, buf, size, offset, ...) \ {
do { \ va_list args;
size_t _printed; \ int printed;
bool _ret = true; \ size_t available;
\
/* Concatenate the strings, and check whether or not the output was \
* truncated. */ \
while ((_printed = snprintf(*buf + *offset, *size - *offset, \
__VA_ARGS__)) >= \
(*size - *offset)) { \
/* If it was truncated, embiggen the string and roll from the top. */ \
if (!do_realloc(buf, size, *offset, _printed)) { \
fprintf(stderr, \
"xkbcommon: couldn't allocate %zu bytes for keymap\n", \
*size); \
free(*buf); \
*buf = NULL; \
_ret = false; \
break; \
} \
} \
if (_ret == true) \
*offset += _printed; \
} while (0)
#define write_buf(keymap, buf, size, offset, ...) \ available = buf->alloc - buf->size;
do { \ va_start(args, fmt);
check_write_buf(keymap, buf, size, offset, __VA_ARGS__); \ printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
if (*buf == NULL) \ va_end(args);
return false; \
} while (0) if (printed < 0)
goto err;
if (printed >= available)
if (!do_realloc(buf, printed))
goto err;
/* The buffer has enough space now. */
available = buf->alloc - buf->size;
va_start(args, fmt);
printed = vsnprintf(buf->buf + buf->size, available, fmt, args);
va_end(args);
if (printed >= available || printed < 0)
goto err;
buf->size += printed;
return true;
err:
free(buf->buf);
buf->buf = NULL;
return false;
}
#define write_buf(buf, ...) do { \
if (!check_write_buf(buf, __VA_ARGS__)) \
return false; \
} while (0)
static bool static bool
write_vmods(struct xkb_keymap *keymap, char **buf, size_t *size, write_vmods(struct xkb_keymap *keymap, struct buf *buf)
size_t *offset)
{ {
int num_vmods = 0; int num_vmods = 0;
int i; int i;
@ -128,15 +139,15 @@ write_vmods(struct xkb_keymap *keymap, char **buf, size_t *size,
if (!keymap->vmod_names[i]) if (!keymap->vmod_names[i])
continue; continue;
if (num_vmods == 0) if (num_vmods == 0)
write_buf(keymap, buf, size, offset, "\t\tvirtual_modifiers "); write_buf(buf, "\t\tvirtual_modifiers ");
else else
write_buf(keymap, buf, size, offset, ","); write_buf(buf, ",");
write_buf(keymap, buf, size, offset, "%s", keymap->vmod_names[i]); write_buf(buf, "%s", keymap->vmod_names[i]);
num_vmods++; num_vmods++;
} }
if (num_vmods > 0) if (num_vmods > 0)
write_buf(keymap, buf, size, offset, ";\n\n"); write_buf(buf, ";\n\n");
return true; return true;
} }
@ -306,68 +317,66 @@ get_control_mask_text(uint32_t control_mask)
} }
static bool static bool
write_keycodes(struct xkb_keymap *keymap, char **buf, size_t *size, write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
size_t *offset)
{ {
struct xkb_key *key; struct xkb_key *key;
struct xkb_key_alias *alias; struct xkb_key_alias *alias;
int i; int i;
if (keymap->keycodes_section_name) if (keymap->keycodes_section_name)
write_buf(keymap, buf, size, offset, "\txkb_keycodes \"%s\" {\n", write_buf(buf, "\txkb_keycodes \"%s\" {\n",
keymap->keycodes_section_name); keymap->keycodes_section_name);
else else
write_buf(keymap, buf, size, offset, "\txkb_keycodes {\n"); write_buf(buf, "\txkb_keycodes {\n");
write_buf(keymap, buf, size, offset, "\t\tminimum = %d;\n", write_buf(buf, "\t\tminimum = %d;\n",
keymap->min_key_code); keymap->min_key_code);
write_buf(keymap, buf, size, offset, "\t\tmaximum = %d;\n", write_buf(buf, "\t\tmaximum = %d;\n",
keymap->max_key_code); keymap->max_key_code);
xkb_foreach_key(key, keymap) { xkb_foreach_key(key, keymap) {
if (key->name[0] == '\0') if (key->name[0] == '\0')
continue; continue;
write_buf(keymap, buf, size, offset, "\t\t%6s = %d;\n", write_buf(buf, "\t\t%6s = %d;\n",
KeyNameText(key->name), XkbKeyGetKeycode(keymap, key)); KeyNameText(key->name), XkbKeyGetKeycode(keymap, key));
} }
for (i = 0; i < XkbNumIndicators; i++) { for (i = 0; i < XkbNumIndicators; i++) {
if (!keymap->indicator_names[i]) if (!keymap->indicator_names[i])
continue; continue;
write_buf(keymap, buf, size, offset, "\t\tindicator %d = \"%s\";\n", write_buf(buf, "\t\tindicator %d = \"%s\";\n",
i + 1, keymap->indicator_names[i]); i + 1, keymap->indicator_names[i]);
} }
darray_foreach(alias, keymap->key_aliases) darray_foreach(alias, keymap->key_aliases)
write_buf(keymap, buf, size, offset, "\t\talias %6s = %6s;\n", write_buf(buf, "\t\talias %6s = %6s;\n",
KeyNameText(alias->alias), KeyNameText(alias->alias),
KeyNameText(alias->real)); KeyNameText(alias->real));
write_buf(keymap, buf, size, offset, "\t};\n\n"); write_buf(buf, "\t};\n\n");
return true; return true;
} }
static bool static bool
write_types(struct xkb_keymap *keymap, char **buf, size_t *size, write_types(struct xkb_keymap *keymap, struct buf *buf)
size_t *offset)
{ {
int n; int n;
struct xkb_key_type *type; struct xkb_key_type *type;
if (keymap->types_section_name) if (keymap->types_section_name)
write_buf(keymap, buf, size, offset, "\txkb_types \"%s\" {\n\n", write_buf(buf, "\txkb_types \"%s\" {\n\n",
keymap->types_section_name); keymap->types_section_name);
else else
write_buf(keymap, buf, size, offset, "\txkb_types {\n\n"); write_buf(buf, "\txkb_types {\n\n");
write_vmods(keymap, buf, size, offset); write_vmods(keymap, buf);
darray_foreach(type, keymap->types) { darray_foreach(type, keymap->types) {
write_buf(keymap, buf, size, offset, "\t\ttype \"%s\" {\n", write_buf(buf, "\t\ttype \"%s\" {\n",
type->name); type->name);
write_buf(keymap, buf, size, offset, "\t\t\tmodifiers= %s;\n", write_buf(buf, "\t\t\tmodifiers= %s;\n",
get_mod_mask_text(keymap, type->mods.real_mods, get_mod_mask_text(keymap, type->mods.real_mods,
type->mods.vmods)); type->mods.vmods));
@ -377,14 +386,14 @@ write_types(struct xkb_keymap *keymap, char **buf, size_t *size,
str = get_mod_mask_text(keymap, entry->mods.real_mods, str = get_mod_mask_text(keymap, entry->mods.real_mods,
entry->mods.vmods); entry->mods.vmods);
write_buf(keymap, buf, size, offset, "\t\t\tmap[%s]= Level%d;\n", write_buf(buf, "\t\t\tmap[%s]= Level%d;\n",
str, entry->level + 1); str, entry->level + 1);
if (!type->preserve || (!type->preserve[n].real_mods && if (!type->preserve || (!type->preserve[n].real_mods &&
!type->preserve[n].vmods)) !type->preserve[n].vmods))
continue; continue;
write_buf(keymap, buf, size, offset, "\t\t\tpreserve[%s]= ", str); write_buf(buf, "\t\t\tpreserve[%s]= ", str);
write_buf(keymap, buf, size, offset, "%s;\n", write_buf(buf, "%s;\n",
get_mod_mask_text(keymap, type->preserve[n].real_mods, get_mod_mask_text(keymap, type->preserve[n].real_mods,
type->preserve[n].vmods)); type->preserve[n].vmods));
} }
@ -393,54 +402,50 @@ write_types(struct xkb_keymap *keymap, char **buf, size_t *size,
for (n = 0; n < type->num_levels; n++) { for (n = 0; n < type->num_levels; n++) {
if (!type->level_names[n]) if (!type->level_names[n])
continue; continue;
write_buf(keymap, buf, size, offset, write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
"\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
type->level_names[n]); type->level_names[n]);
} }
} }
write_buf(keymap, buf, size, offset, "\t\t};\n"); write_buf(buf, "\t\t};\n");
} }
write_buf(keymap, buf, size, offset, "\t};\n\n"); write_buf(buf, "\t};\n\n");
return true; return true;
} }
static bool static bool
write_indicator_map(struct xkb_keymap *keymap, char **buf, size_t *size, write_indicator_map(struct xkb_keymap *keymap, struct buf *buf, int num)
size_t *offset, int num)
{ {
struct xkb_indicator_map *led = &keymap->indicators[num]; struct xkb_indicator_map *led = &keymap->indicators[num];
write_buf(keymap, buf, size, offset, "\t\tindicator \"%s\" {\n", write_buf(buf, "\t\tindicator \"%s\" {\n",
keymap->indicator_names[num]); keymap->indicator_names[num]);
if (led->which_groups) { if (led->which_groups) {
if (led->which_groups != XkbIM_UseEffective) { if (led->which_groups != XkbIM_UseEffective) {
write_buf(keymap, buf, size, offset, write_buf(buf, "\t\t\twhichGroupState= %s;\n",
"\t\t\twhichGroupState= %s;\n", get_indicator_state_text(led->which_groups));
get_indicator_state_text(
led->which_groups));
} }
write_buf(keymap, buf, size, offset, "\t\t\tgroups= 0x%02x;\n", write_buf(buf, "\t\t\tgroups= 0x%02x;\n",
led->groups); led->groups);
} }
if (led->which_mods) { if (led->which_mods) {
if (led->which_mods != XkbIM_UseEffective) { if (led->which_mods != XkbIM_UseEffective) {
write_buf(keymap, buf, size, offset, "\t\t\twhichModState= %s;\n", write_buf(buf, "\t\t\twhichModState= %s;\n",
get_indicator_state_text(led->which_mods)); get_indicator_state_text(led->which_mods));
} }
write_buf(keymap, buf, size, offset, "\t\t\tmodifiers= %s;\n", write_buf(buf, "\t\t\tmodifiers= %s;\n",
get_mod_mask_text(keymap, led->mods.real_mods, get_mod_mask_text(keymap, led->mods.real_mods,
led->mods.vmods)); led->mods.vmods));
} }
if (led->ctrls) { if (led->ctrls) {
write_buf(keymap, buf, size, offset, "\t\t\tcontrols= %s;\n", write_buf(buf, "\t\t\tcontrols= %s;\n",
get_control_mask_text(led->ctrls)); get_control_mask_text(led->ctrls));
} }
write_buf(keymap, buf, size, offset, "\t\t};\n"); write_buf(buf, "\t\t};\n");
return true; return true;
} }
@ -479,9 +484,8 @@ get_interp_match_text(uint8_t type)
} }
static bool static bool
write_action(struct xkb_keymap *keymap, char **buf, size_t *size, write_action(struct xkb_keymap *keymap, struct buf *buf,
size_t *offset, union xkb_action *action, const char *prefix, union xkb_action *action, const char *prefix, const char *suffix)
const char *suffix)
{ {
const char *type = NULL; const char *type = NULL;
const char *args = NULL; const char *args = NULL;
@ -506,7 +510,7 @@ write_action(struct xkb_keymap *keymap, char **buf, size_t *size,
else else
args = get_mod_mask_text(keymap, action->mods.real_mods, args = get_mod_mask_text(keymap, action->mods.real_mods,
action->mods.vmods); action->mods.vmods);
write_buf(keymap, buf, size, offset, "%s%s(modifiers=%s%s%s)%s", write_buf(buf, "%s%s(modifiers=%s%s%s)%s",
prefix, type, args, prefix, type, args,
(action->any.type != XkbSA_LockGroup && (action->any.type != XkbSA_LockGroup &&
(action->mods.flags & XkbSA_ClearLocks)) ? (action->mods.flags & XkbSA_ClearLocks)) ?
@ -526,29 +530,28 @@ write_action(struct xkb_keymap *keymap, char **buf, size_t *size,
case XkbSA_LockGroup: case XkbSA_LockGroup:
if (!type) if (!type)
type = "LockGroup"; type = "LockGroup";
write_buf( write_buf(buf, "%s%s(group=%s%d%s%s)%s",
keymap, buf, size, offset, "%s%s(group=%s%d%s%s)%s", prefix, type,
prefix, type, (!(action->group.flags & XkbSA_GroupAbsolute) &&
(!(action->group.flags & XkbSA_GroupAbsolute) && action->group.group > 0) ? "+" : "",
action->group.group > 0) ? "+" : "", (action->group.flags & XkbSA_GroupAbsolute) ?
(action->group.flags & XkbSA_GroupAbsolute) ? action->group.group + 1 : action->group.group,
action->group.group + 1 : action->group.group, (action->any.type != XkbSA_LockGroup &&
(action->any.type != XkbSA_LockGroup && (action->group.flags & XkbSA_ClearLocks)) ?
(action->group.flags & XkbSA_ClearLocks)) ? ",clearLocks" : "",
",clearLocks" : "", (action->any.type != XkbSA_LockGroup &&
(action->any.type != XkbSA_LockGroup && (action->group.flags & XkbSA_LatchToLock)) ?
(action->group.flags & XkbSA_LatchToLock)) ? ",latchToLock" : "",
",latchToLock" : "", suffix);
suffix);
break; break;
case XkbSA_Terminate: case XkbSA_Terminate:
write_buf(keymap, buf, size, offset, "%sTerminate()%s", prefix, write_buf(buf, "%sTerminate()%s", prefix,
suffix); suffix);
break; break;
case XkbSA_MovePtr: case XkbSA_MovePtr:
write_buf(keymap, buf, size, offset, "%sMovePtr(x=%s%d,y=%s%d%s)%s", write_buf(buf, "%sMovePtr(x=%s%d,y=%s%d%s)%s",
prefix, prefix,
(!(action->ptr.flags & XkbSA_MoveAbsoluteX) && (!(action->ptr.flags & XkbSA_MoveAbsoluteX) &&
action->ptr.x >= 0) ? "+" : "", action->ptr.x >= 0) ? "+" : "",
@ -588,32 +591,30 @@ write_action(struct xkb_keymap *keymap, char **buf, size_t *size,
else { else {
args = NULL; args = NULL;
} }
write_buf(keymap, buf, size, offset, "%s%s(button=", prefix, type); write_buf(buf, "%s%s(button=", prefix, type);
if (action->btn.button > 0 && action->btn.button <= 5) if (action->btn.button > 0 && action->btn.button <= 5)
write_buf(keymap, buf, size, offset, "%d", action->btn.button); write_buf(buf, "%d", action->btn.button);
else else
write_buf(keymap, buf, size, offset, "default"); write_buf(buf, "default");
if (action->btn.count) if (action->btn.count)
write_buf(keymap, buf, size, offset, ",count=%d", write_buf(buf, ",count=%d", action->btn.count);
action->btn.count);
if (args) if (args)
write_buf(keymap, buf, size, offset, "%s", args); write_buf(buf, "%s", args);
write_buf(keymap, buf, size, offset, ")%s", suffix); write_buf(buf, ")%s", suffix);
break; break;
case XkbSA_SetPtrDflt: case XkbSA_SetPtrDflt:
write_buf(keymap, buf, size, offset, "%sSetPtrDflt(", prefix); write_buf(buf, "%sSetPtrDflt(", prefix);
if (action->dflt.affect == XkbSA_AffectDfltBtn) if (action->dflt.affect == XkbSA_AffectDfltBtn)
write_buf(keymap, buf, size, offset, "affect=button,button=%s%d", write_buf(buf, "affect=button,button=%s%d",
(!(action->dflt.flags & XkbSA_DfltBtnAbsolute) && (!(action->dflt.flags & XkbSA_DfltBtnAbsolute) &&
action->dflt.value >= 0) ? "+" : "", action->dflt.value >= 0) ? "+" : "",
action->dflt.value); action->dflt.value);
write_buf(keymap, buf, size, offset, ")%s", suffix); write_buf(buf, ")%s", suffix);
break; break;
case XkbSA_SwitchScreen: case XkbSA_SwitchScreen:
write_buf(keymap, buf, size, offset, write_buf(buf, "%sSwitchScreen(screen=%s%d,%ssame)%s", prefix,
"%sSwitchScreen(screen=%s%d,%ssame)%s", prefix,
(!(action->screen.flags & XkbSA_SwitchAbsolute) && (!(action->screen.flags & XkbSA_SwitchAbsolute) &&
action->screen.screen >= 0) ? "+" : "", action->screen.screen >= 0) ? "+" : "",
action->screen.screen, action->screen.screen,
@ -628,7 +629,7 @@ write_action(struct xkb_keymap *keymap, char **buf, size_t *size,
case XkbSA_LockControls: case XkbSA_LockControls:
if (!type) if (!type)
type = "LockControls"; type = "LockControls";
write_buf(keymap, buf, size, offset, "%s%s(controls=%s)%s", write_buf(buf, "%s%s(controls=%s)%s",
prefix, type, get_control_mask_text(action->ctrls.ctrls), prefix, type, get_control_mask_text(action->ctrls.ctrls),
suffix); suffix);
break; break;
@ -640,12 +641,12 @@ write_action(struct xkb_keymap *keymap, char **buf, size_t *size,
case XkbSA_LockDeviceBtn: case XkbSA_LockDeviceBtn:
case XkbSA_NoAction: case XkbSA_NoAction:
/* XXX TODO */ /* XXX TODO */
write_buf(keymap, buf, size, offset, "%sNoAction()%s", prefix, suffix); write_buf(buf, "%sNoAction()%s", prefix, suffix);
break; break;
case XkbSA_XFree86Private: case XkbSA_XFree86Private:
default: default:
write_buf(keymap, buf, size, offset, write_buf(buf,
"%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", "%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",
prefix, action->any.type, action->any.data[0], prefix, action->any.type, action->any.data[0],
action->any.data[1], action->any.data[2], action->any.data[1], action->any.data[2],
@ -659,25 +660,22 @@ write_action(struct xkb_keymap *keymap, char **buf, size_t *size,
} }
static bool static bool
write_compat(struct xkb_keymap *keymap, char **buf, size_t *size, write_compat(struct xkb_keymap *keymap, struct buf *buf)
size_t *offset)
{ {
int i; int i;
struct xkb_sym_interpret *interp; struct xkb_sym_interpret *interp;
if (keymap->compat_section_name) if (keymap->compat_section_name)
write_buf(keymap, buf, size, offset, write_buf(buf, "\txkb_compatibility \"%s\" {\n\n",
"\txkb_compatibility \"%s\" {\n\n",
keymap->compat_section_name); keymap->compat_section_name);
else else
write_buf(keymap, buf, size, offset, "\txkb_compatibility {\n\n"); write_buf(buf, "\txkb_compatibility {\n\n");
write_vmods(keymap, buf, size, offset); write_vmods(keymap, buf);
write_buf(keymap, buf, size, offset, write_buf(buf, "\t\tinterpret.useModMapMods= AnyLevel;\n");
"\t\tinterpret.useModMapMods= AnyLevel;\n"); write_buf(buf, "\t\tinterpret.repeat= False;\n");
write_buf(keymap, buf, size, offset, "\t\tinterpret.repeat= False;\n"); write_buf(buf, "\t\tinterpret.locking= False;\n");
write_buf(keymap, buf, size, offset, "\t\tinterpret.locking= False;\n");
darray_foreach(interp, keymap->sym_interpret) { darray_foreach(interp, keymap->sym_interpret) {
char keysym_name[64]; char keysym_name[64];
@ -687,28 +685,26 @@ write_compat(struct xkb_keymap *keymap, char **buf, size_t *size,
else else
xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name)); xkb_keysym_get_name(interp->sym, keysym_name, sizeof(keysym_name));
write_buf(keymap, buf, size, offset, "\t\tinterpret %s+%s(%s) {\n", write_buf(buf, "\t\tinterpret %s+%s(%s) {\n",
keysym_name, keysym_name,
get_interp_match_text(interp->match), get_interp_match_text(interp->match),
get_mod_mask_text(keymap, interp->mods, 0)); get_mod_mask_text(keymap, interp->mods, 0));
if (interp->virtual_mod != XkbNoModifier) { if (interp->virtual_mod != XkbNoModifier) {
write_buf(keymap, buf, size, offset, write_buf(buf, "\t\t\tvirtualModifier= %s;\n",
"\t\t\tvirtualModifier= %s;\n",
keymap->vmod_names[interp->virtual_mod]); keymap->vmod_names[interp->virtual_mod]);
} }
if (interp->match & XkbSI_LevelOneOnly) if (interp->match & XkbSI_LevelOneOnly)
write_buf(keymap, buf, size, offset, write_buf(buf,
"\t\t\tuseModMapMods=level1;\n"); "\t\t\tuseModMapMods=level1;\n");
if (interp->flags & XkbSI_LockingKey) if (interp->flags & XkbSI_LockingKey)
write_buf(keymap, buf, size, offset, "\t\t\tlocking= True;\n"); write_buf(buf, "\t\t\tlocking= True;\n");
if (interp->flags & XkbSI_AutoRepeat) if (interp->flags & XkbSI_AutoRepeat)
write_buf(keymap, buf, size, offset, "\t\t\trepeat= True;\n"); write_buf(buf, "\t\t\trepeat= True;\n");
write_action(keymap, buf, size, offset, &interp->act, write_action(keymap, buf, &interp->act, "\t\t\taction= ", ";\n");
"\t\t\taction= ", ";\n"); write_buf(buf, "\t\t};\n");
write_buf(keymap, buf, size, offset, "\t\t};\n");
} }
for (i = 0; i < XkbNumKbdGroups; i++) { for (i = 0; i < XkbNumKbdGroups; i++) {
@ -717,8 +713,7 @@ write_compat(struct xkb_keymap *keymap, char **buf, size_t *size,
gc = &keymap->groups[i]; gc = &keymap->groups[i];
if (gc->real_mods == 0 && gc->vmods == 0) if (gc->real_mods == 0 && gc->vmods == 0)
continue; continue;
write_buf(keymap, buf, size, offset, write_buf(buf, "\t\tgroup %d = %s;\n", i + 1,
"\t\tgroup %d = %s;\n", i + 1,
get_mod_mask_text(keymap, gc->real_mods, gc->vmods)); get_mod_mask_text(keymap, gc->real_mods, gc->vmods));
} }
@ -729,17 +724,17 @@ write_compat(struct xkb_keymap *keymap, char **buf, size_t *size,
map->mods.real_mods == 0 && map->mods.vmods == 0 && map->mods.real_mods == 0 && map->mods.vmods == 0 &&
map->ctrls == 0) map->ctrls == 0)
continue; continue;
write_indicator_map(keymap, buf, size, offset, i); write_indicator_map(keymap, buf, i);
} }
write_buf(keymap, buf, size, offset, "\t};\n\n"); write_buf(buf, "\t};\n\n");
return true; return true;
} }
static bool static bool
write_keysyms(struct xkb_keymap *keymap, char **buf, size_t *size, write_keysyms(struct xkb_keymap *keymap, struct buf *buf,
size_t *offset, struct xkb_key *key, xkb_group_index_t group) struct xkb_key *key, xkb_group_index_t group)
{ {
const xkb_keysym_t *syms; const xkb_keysym_t *syms;
int num_syms, level; int num_syms, level;
@ -748,26 +743,26 @@ write_keysyms(struct xkb_keymap *keymap, char **buf, size_t *size,
for (level = 0; level < XkbKeyGroupWidth(keymap, key, group); level++) { for (level = 0; level < XkbKeyGroupWidth(keymap, key, group); level++) {
if (level != 0) if (level != 0)
write_buf(keymap, buf, size, offset, ", "); write_buf(buf, ", ");
num_syms = xkb_key_get_syms_by_level(keymap, key, group, level, num_syms = xkb_key_get_syms_by_level(keymap, key, group, level,
&syms); &syms);
if (num_syms == 0) { if (num_syms == 0) {
write_buf(keymap, buf, size, offset, "%15s", "NoSymbol"); write_buf(buf, "%15s", "NoSymbol");
} }
else if (num_syms == 1) { else if (num_syms == 1) {
xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN); xkb_keysym_get_name(syms[0], out_buf, OUT_BUF_LEN);
write_buf(keymap, buf, size, offset, "%15s", out_buf); write_buf(buf, "%15s", out_buf);
} }
else { else {
int s; int s;
write_buf(keymap, buf, size, offset, "{ "); write_buf(buf, "{ ");
for (s = 0; s < num_syms; s++) { for (s = 0; s < num_syms; s++) {
if (s != 0) if (s != 0)
write_buf(keymap, buf, size, offset, ", "); write_buf(buf, ", ");
xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN); xkb_keysym_get_name(syms[s], out_buf, OUT_BUF_LEN);
write_buf(keymap, buf, size, offset, "%15s", out_buf); write_buf(buf, "%15s", out_buf);
} }
write_buf(keymap, buf, size, offset, " }"); write_buf(buf, " }");
} }
} }
#undef OUT_BUF_LEN #undef OUT_BUF_LEN
@ -776,29 +771,28 @@ write_keysyms(struct xkb_keymap *keymap, char **buf, size_t *size,
} }
static bool static bool
write_symbols(struct xkb_keymap *keymap, char **buf, size_t *size, write_symbols(struct xkb_keymap *keymap, struct buf *buf)
size_t *offset)
{ {
struct xkb_key *key; struct xkb_key *key;
xkb_group_index_t group, tmp; xkb_group_index_t group, tmp;
bool showActions; bool showActions;
if (keymap->symbols_section_name) if (keymap->symbols_section_name)
write_buf(keymap, buf, size, offset, "\txkb_symbols \"%s\" {\n\n", write_buf(buf, "\txkb_symbols \"%s\" {\n\n",
keymap->symbols_section_name); keymap->symbols_section_name);
else else
write_buf(keymap, buf, size, offset, "\txkb_symbols {\n\n"); write_buf(buf, "\txkb_symbols {\n\n");
for (tmp = group = 0; group < XkbNumKbdGroups; group++) { for (tmp = group = 0; group < XkbNumKbdGroups; group++) {
if (!keymap->group_names[group]) if (!keymap->group_names[group])
continue; continue;
write_buf(keymap, buf, size, offset, write_buf(buf,
"\t\tname[group%d]=\"%s\";\n", group + 1, "\t\tname[group%d]=\"%s\";\n", group + 1,
keymap->group_names[group]); keymap->group_names[group]);
tmp++; tmp++;
} }
if (tmp > 0) if (tmp > 0)
write_buf(keymap, buf, size, offset, "\n"); write_buf(buf, "\n");
xkb_foreach_key(key, keymap) { xkb_foreach_key(key, keymap) {
bool simple = true; bool simple = true;
@ -806,8 +800,7 @@ write_symbols(struct xkb_keymap *keymap, char **buf, size_t *size,
if (key->num_groups == 0) if (key->num_groups == 0)
continue; continue;
write_buf(keymap, buf, size, offset, "\t\tkey %6s {", write_buf(buf, "\t\tkey %6s {", KeyNameText(key->name));
KeyNameText(key->name));
if (key->explicit & XkbExplicitKeyTypesMask) { if (key->explicit & XkbExplicitKeyTypesMask) {
bool multi_type = false; bool multi_type = false;
@ -827,42 +820,37 @@ write_symbols(struct xkb_keymap *keymap, char **buf, size_t *size,
if (!(key->explicit & (1 << group))) if (!(key->explicit & (1 << group)))
continue; continue;
type = XkbKeyTypeIndex(key, group); type = XkbKeyTypeIndex(key, group);
write_buf(keymap, buf, size, offset, write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
"\n\t\t\ttype[group%u]= \"%s\",",
group + 1, group + 1,
darray_item(keymap->types, type).name); darray_item(keymap->types, type).name);
} }
} }
else { else {
write_buf(keymap, buf, size, offset, write_buf(buf, "\n\t\t\ttype= \"%s\",",
"\n\t\t\ttype= \"%s\",",
darray_item(keymap->types, type).name); darray_item(keymap->types, type).name);
} }
} }
if (key->explicit & XkbExplicitAutoRepeatMask) { if (key->explicit & XkbExplicitAutoRepeatMask) {
if (key->repeats) if (key->repeats)
write_buf(keymap, buf, size, offset, write_buf(buf, "\n\t\t\trepeat= Yes,");
"\n\t\t\trepeat= Yes,");
else else
write_buf(keymap, buf, size, offset, write_buf(buf, "\n\t\t\trepeat= No,");
"\n\t\t\trepeat= No,");
simple = false; simple = false;
} }
if (key->vmodmap && (key->explicit & XkbExplicitVModMapMask)) { if (key->vmodmap && (key->explicit & XkbExplicitVModMapMask)) {
write_buf(keymap, buf, size, offset, "\n\t\t\tvirtualMods= %s,", write_buf(buf, "\n\t\t\tvirtualMods= %s,",
get_mod_mask_text(keymap, 0, key->vmodmap)); get_mod_mask_text(keymap, 0, key->vmodmap));
} }
switch (key->out_of_range_group_action) { switch (key->out_of_range_group_action) {
case XkbClampIntoRange: case XkbClampIntoRange:
write_buf(keymap, buf, size, offset, "\n\t\t\tgroupsClamp,"); write_buf(buf, "\n\t\t\tgroupsClamp,");
break; break;
case XkbRedirectIntoRange: case XkbRedirectIntoRange:
write_buf(keymap, buf, size, offset, write_buf(buf, "\n\t\t\tgroupsRedirect= Group%u,",
"\n\t\t\tgroupsRedirect= Group%u,",
key->out_of_range_group_number + 1); key->out_of_range_group_number + 1);
break; break;
} }
@ -876,10 +864,10 @@ write_symbols(struct xkb_keymap *keymap, char **buf, size_t *size,
simple = false; simple = false;
if (simple) { if (simple) {
write_buf(keymap, buf, size, offset, "\t[ "); write_buf(buf, "\t[ ");
if (!write_keysyms(keymap, buf, size, offset, key, 0)) if (!write_keysyms(keymap, buf, key, 0))
return false; return false;
write_buf(keymap, buf, size, offset, " ] };\n"); write_buf(buf, " ] };\n");
} }
else { else {
union xkb_action *acts; union xkb_action *acts;
@ -888,28 +876,26 @@ write_symbols(struct xkb_keymap *keymap, char **buf, size_t *size,
acts = XkbKeyActionsPtr(keymap, key); acts = XkbKeyActionsPtr(keymap, key);
for (group = 0; group < key->num_groups; group++) { for (group = 0; group < key->num_groups; group++) {
if (group != 0) if (group != 0)
write_buf(keymap, buf, size, offset, ","); write_buf(buf, ",");
write_buf(keymap, buf, size, offset, write_buf(buf, "\n\t\t\tsymbols[Group%u]= [ ", group + 1);
"\n\t\t\tsymbols[Group%u]= [ ", group + 1); if (!write_keysyms(keymap, buf, key, group))
if (!write_keysyms(keymap, buf, size, offset, key, group))
return false; return false;
write_buf(keymap, buf, size, offset, " ]"); write_buf(buf, " ]");
if (showActions) { if (showActions) {
write_buf(keymap, buf, size, offset, write_buf(buf, ",\n\t\t\tactions[Group%u]= [ ",
",\n\t\t\tactions[Group%u]= [ ", group + 1); group + 1);
for (level = 0; for (level = 0;
level < XkbKeyGroupWidth(keymap, key, group); level < XkbKeyGroupWidth(keymap, key, group);
level++) { level++) {
if (level != 0) if (level != 0)
write_buf(keymap, buf, size, offset, ", "); write_buf(buf, ", ");
write_action(keymap, buf, size, offset, &acts[level], write_action(keymap, buf, &acts[level], NULL, NULL);
NULL, NULL);
} }
write_buf(keymap, buf, size, offset, " ]"); write_buf(buf, " ]");
acts += key->width; acts += key->width;
} }
} }
write_buf(keymap, buf, size, offset, "\n\t\t};\n"); write_buf(buf, "\n\t\t};\n");
} }
} }
@ -923,38 +909,27 @@ write_symbols(struct xkb_keymap *keymap, char **buf, size_t *size,
if (!(key->modmap & (1 << mod))) if (!(key->modmap & (1 << mod)))
continue; continue;
write_buf(keymap, buf, size, offset, write_buf(buf, "\t\tmodifier_map %s { %s };\n",
"\t\tmodifier_map %s { %s };\n", get_mod_index_text(mod), KeyNameText(key->name));
get_mod_index_text(mod),
KeyNameText(key->name));
} }
} }
write_buf(keymap, buf, size, offset, "\t};\n\n"); write_buf(buf, "\t};\n\n");
return true; return true;
} }
XKB_EXPORT char * XKB_EXPORT char *
xkb_map_get_as_string(struct xkb_keymap *keymap) xkb_map_get_as_string(struct xkb_keymap *keymap)
{ {
char *ret = NULL; bool ok;
size_t size = 0; struct buf buf = { NULL, 0, 0 };
size_t offset = 0;
check_write_buf(keymap, &ret, &size, &offset, "xkb_keymap {\n"); ok = (check_write_buf(&buf, "xkb_keymap {\n") &&
if (ret == NULL) write_keycodes(keymap, &buf) &&
return NULL; write_types(keymap, &buf) &&
if (!write_keycodes(keymap, &ret, &size, &offset)) write_compat(keymap, &buf) &&
return NULL; write_symbols(keymap, &buf) &&
if (!write_types(keymap, &ret, &size, &offset)) check_write_buf(&buf, "};\n"));
return NULL;
if (!write_compat(keymap, &ret, &size, &offset))
return NULL;
if (!write_symbols(keymap, &ret, &size, &offset))
return NULL;
check_write_buf(keymap, &ret, &size, &offset, "};\n");
if (ret == NULL)
return NULL;
return ret; return (ok ? buf.buf : NULL);
} }