#include "keymap.h"
#include "keysym.h"
+#include "utf8.h"
struct xkb_filter {
union xkb_action action;
return sym;
}
+XKB_EXPORT int
+xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
+ char *buffer, size_t size)
+{
+ xkb_keysym_t sym;
+ const xkb_keysym_t *syms;
+ int nsyms;
+ int offset;
+ char tmp[7];
+
+ /* Make sure the keysym transformations are applied. */
+ sym = xkb_state_key_get_one_sym(state, kc);
+ if (sym != XKB_KEY_NoSymbol) {
+ nsyms = 1; syms = &sym;
+ }
+ else {
+ nsyms = xkb_state_key_get_syms(state, kc, &syms);
+ }
+
+ /* Make sure not to truncate in the middle of a UTF-8 sequence. */
+ offset = 0;
+ for (int i = 0; i < nsyms; i++) {
+ int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
+ if (ret <= 0)
+ goto err_bad;
+
+ ret--;
+ if ((size_t) (offset + ret) <= size)
+ memcpy(buffer + offset, tmp, ret);
+ offset += ret;
+ }
+
+ if ((size_t) offset >= size)
+ goto err_trunc;
+ buffer[offset] = '\0';
+
+ if (!is_valid_utf8(buffer, offset))
+ goto err_bad;
+
+ return offset;
+
+err_trunc:
+ if (size > 0)
+ buffer[size - 1] = '\0';
+ return offset;
+
+err_bad:
+ if (size > 0)
+ buffer[0] = '\0';
+ return 0;
+}
+
+XKB_EXPORT uint32_t
+xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
+{
+ xkb_keysym_t sym;
+ uint32_t cp;
+
+ sym = xkb_state_key_get_one_sym(state, kc);
+ cp = xkb_keysym_to_utf32(sym);
+
+ return cp;
+}
+
/**
* Serialises the requested modifier state into an xkb_mod_mask_t, with all
* the same disclaimers as in xkb_state_update_mask.
xkb_state_unref(state);
}
+static void
+test_get_utf8_utf32(struct xkb_keymap *keymap)
+{
+ char buf[256];
+ struct xkb_state *state = xkb_state_new(keymap);
+ assert(state);
+
+#define TEST_KEY(key, expected_utf8, expected_utf32) do { \
+ assert(xkb_state_key_get_utf8(state, key + 8, NULL, 0) == strlen(expected_utf8)); \
+ assert(xkb_state_key_get_utf8(state, key + 8, buf, sizeof(buf)) == strlen(expected_utf8)); \
+ assert(memcmp(buf, expected_utf8, sizeof(expected_utf8)) == 0); \
+ assert(xkb_state_key_get_utf32(state, key + 8) == expected_utf32); \
+} while (0)
+
+ /* Simple ASCII. */
+ TEST_KEY(KEY_A, "a", 0x61);
+ TEST_KEY(KEY_ESC, "\x1B", 0x1B);
+ TEST_KEY(KEY_1, "1", 0x31);
+
+ /* Invalid. */
+ TEST_KEY(XKB_KEYCODE_INVALID - 8, "", 0);
+ TEST_KEY(300, "", 0);
+
+ /* No string. */
+ TEST_KEY(KEY_LEFTCTRL, "", 0);
+ TEST_KEY(KEY_NUMLOCK, "", 0);
+
+ /* Multiple keysyms. */
+ TEST_KEY(KEY_6, "HELLO", 0);
+ TEST_KEY(KEY_7, "YES THIS IS DOG", 0);
+
+ /* Check truncation. */
+ memset(buf, 'X', sizeof(buf));
+ assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 0) == strlen("HELLO"));
+ assert(memcmp(buf, "X", 1) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 1) == strlen("HELLO"));
+ assert(memcmp(buf, "", 1) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 2) == strlen("HELLO"));
+ assert(memcmp(buf, "H", 2) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 3) == strlen("HELLO"));
+ assert(memcmp(buf, "HE", 3) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 5) == strlen("HELLO"));
+ assert(memcmp(buf, "HELL", 5) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 6) == strlen("HELLO"));
+ assert(memcmp(buf, "HELLO", 6) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 7) == strlen("HELLO"));
+ assert(memcmp(buf, "HELLO\0X", 7) == 0);
+
+ /* Switch to ru layout */
+ xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
+ xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
+ assert(xkb_state_key_get_layout(state, KEY_A + 8) == 1);
+
+ /* Non ASCII. */
+ TEST_KEY(KEY_ESC, "\x1B", 0x1B);
+ TEST_KEY(KEY_A, "ф", 0x0444);
+ TEST_KEY(KEY_Z, "я", 0x044F);
+
+ /* Switch back to us layout */
+ xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
+ xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
+ assert(xkb_state_key_get_layout(state, KEY_A + 8) == 0);
+
+ xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
+ TEST_KEY(KEY_A, "A", 0x41);
+ TEST_KEY(KEY_ESC, "\x1B", 0x1B);
+ TEST_KEY(KEY_1, "!", 0x21);
+ xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_UP);
+
+ TEST_KEY(KEY_6, "HELLO", 0);
+ TEST_KEY(KEY_7, "YES THIS IS DOG", 0);
+
+ xkb_state_unref(state);
+}
+
int
main(void)
{
test_repeat(keymap);
test_consume(keymap);
test_range(keymap);
+ test_get_utf8_utf32(keymap);
xkb_keymap_unref(keymap);
keymap = test_compile_rules(context, "evdev", NULL, "ch", "fr", NULL);
* @returns The number of bytes written to the buffer (including the
* terminating byte). If the keysym does not have a Unicode
* representation, returns 0. If the buffer is too small, returns -1.
+ *
+ * Prefer not to use this function on keysyms obtained from an
+ * xkb_state. In this case, use xkb_state_key_get_utf8() instead.
+ *
+ * @sa xkb_state_key_get_utf8()
*/
int
xkb_keysym_to_utf8(xkb_keysym_t keysym, char *buffer, size_t size);
* @returns The Unicode/UTF-32 representation of keysym, which is also
* compatible with UCS-4. If the keysym does not have a Unicode
* representation, returns 0.
+ *
+ * Prefer not to use this function on keysyms obtained from an
+ * xkb_state. In this case, use xkb_state_key_get_utf32() instead.
+ *
+ * @sa xkb_state_key_get_utf32()
*/
uint32_t
xkb_keysym_to_utf32(xkb_keysym_t keysym);
const xkb_keysym_t **syms_out);
/**
+ * Get the Unicode/UTF-8 string obtained from pressing a particular key
+ * in a given keyboard state.
+ *
+ * @param[in] state The keyboard state object.
+ * @param[in] key The keycode of the key.
+ * @param[out] buffer A buffer to write the string into.
+ * @param[in] size Size of the buffer.
+ *
+ * @warning If the buffer passed is too small, the string is truncated
+ * (though still NUL-terminated).
+ *
+ * @returns The number of bytes required for the string, excluding the
+ * NUL byte. If there is nothing to write, returns 0.
+ *
+ * You may check if truncation has occurred by comparing the return value
+ * with the size of @p buffer, similarly to the snprintf(3) function.
+ * You may safely pass NULL and 0 to @p buffer and @p size to find the
+ * required size (without the NUL-byte).
+ *
+ * @memberof xkb_state
+ */
+int
+xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t key,
+ char *buffer, size_t size);
+
+/**
+ * Get the Unicode/UTF-32 codepoint obtained from pressing a particular
+ * key in a a given keyboard state.
+ *
+ * @returns The UTF-32 representation for the key, if it consists of only
+ * a single codepoint. Otherwise, returns 0.
+ *
+ * @memberof xkb_state
+ */
+uint32_t
+xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t key);
+
+/**
* Get the single keysym obtained from pressing a particular key in a
* given keyboard state.
*