uterm: input: add string_to_keysym() helper

This new helper can be used to convert a string description of a keysym
into a real keysym. This is required to allow users to configure keyboard
shortcuts and more.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
David Herrmann 2012-09-05 14:04:05 +02:00
parent 9754f71c16
commit 7ff4426bc3
5 changed files with 56 additions and 0 deletions

View File

@ -332,6 +332,8 @@ bool uterm_input_is_awake(struct uterm_input *input);
void uterm_input_keysym_to_string(struct uterm_input *input,
uint32_t keysym, char *str, size_t size);
int uterm_input_string_to_keysym(struct uterm_input *input, const char *n,
uint32_t *out);
/*
* System Monitor

View File

@ -488,3 +488,19 @@ void uterm_input_keysym_to_string(struct uterm_input *input,
kbd_desc_keysym_to_string(input->desc, keysym, str, size);
}
int uterm_input_string_to_keysym(struct uterm_input *input, const char *n,
uint32_t *out)
{
if (!n || !out)
return -EINVAL;
if (input)
return kbd_desc_string_to_keysym(input->desc, n, out);
#ifdef UTERM_HAVE_XKBCOMMON
return uxkb_string_to_keysym(n, out);
#endif
return plain_string_to_keysym(n, out);
}

View File

@ -430,12 +430,20 @@ static void plain_keysym_to_string(uint32_t keysym, char *str, size_t size)
snprintf(str, size, "%#x", keysym);
}
int plain_string_to_keysym(const char *n, uint32_t *out)
{
/* TODO: we really need to implement this; maybe use a hashtable similar
* to the Xlib? */
return -EOPNOTSUPP;
}
const struct kbd_desc_ops plain_desc_ops = {
.init = plain_desc_init,
.ref = plain_desc_ref,
.unref = plain_desc_unref,
.alloc = plain_desc_alloc,
.keysym_to_string = plain_keysym_to_string,
.string_to_keysym = plain_string_to_keysym,
};
const struct kbd_dev_ops plain_dev_ops = {

View File

@ -292,12 +292,27 @@ static void uxkb_keysym_to_string(uint32_t keysym, char *str, size_t size)
xkb_keysym_get_name(keysym, str, size);
}
int uxkb_string_to_keysym(const char *n, uint32_t *out)
{
uint32_t keysym;
/* TODO: fix xkbcommon upstream to be case-insensitive if case-sensitive
* match fails. */
sym = xkb_keysym_from_name(n);
if (!sym)
return -EFAULT;
*out = sym;
return 0;
}
const struct kbd_desc_ops uxkb_desc_ops = {
.init = uxkb_desc_init,
.ref = uxkb_desc_ref,
.unref = uxkb_desc_unref,
.alloc = uxkb_desc_alloc,
.keysym_to_string = uxkb_keysym_to_string,
.string_to_keysym = uxkb_string_to_keysym,
};
const struct kbd_dev_ops uxkb_dev_ops = {

View File

@ -418,6 +418,7 @@ struct kbd_desc_ops {
void (*unref) (struct kbd_desc *desc);
int (*alloc) (struct kbd_desc *desc, struct kbd_dev **out);
void (*keysym_to_string) (uint32_t keysym, char *str, size_t size);
int (*string_to_keysym) (const char *n, uint32_t *out);
};
struct kbd_dev_ops {
@ -440,6 +441,8 @@ static const bool plain_available = true;
extern const struct kbd_desc_ops plain_desc_ops;
extern const struct kbd_dev_ops plain_dev_ops;
extern int plain_string_to_keysym(const char *n, uint32_t *out);
#ifdef UTERM_HAVE_XKBCOMMON
struct uxkb_desc {
@ -455,6 +458,8 @@ static const bool uxkb_available = true;
extern const struct kbd_desc_ops uxkb_desc_ops;
extern const struct kbd_dev_ops uxkb_dev_ops;
extern int uxkb_string_to_keysym(const char *n, uint32_t *out);
#else /* !UTERM_HAVE_XKBCOMMON */
struct uxkb_desc {
@ -560,6 +565,16 @@ static inline void kbd_desc_keysym_to_string(struct kbd_desc *desc,
return desc->ops->keysym_to_string(keysym, str, size);
}
static inline int kbd_desc_string_to_keysym(struct kbd_desc *desc,
const char *n,
uint32_t *out)
{
if (!desc)
return -EINVAL;
return desc->ops->string_to_keysym(n, out);
}
static inline void kbd_dev_ref(struct kbd_dev *dev)
{
if (!dev)