test/interactive-evdev: add compose support To try, do e.g.: sudo ./test/interactive-evdev -l us -v intl -o compose:ralt -d Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
diff --git a/test/common.c b/test/common.c
index e6cd1c3..0bacba0 100644
--- a/test/common.c
+++ b/test/common.c
@@ -347,14 +347,18 @@ test_compile_rules(struct xkb_context *context, const char *rules,
}
void
-test_print_keycode_state(struct xkb_state *state, xkb_keycode_t keycode)
+test_print_keycode_state(struct xkb_state *state,
+ struct xkb_compose_state *compose_state,
+ xkb_keycode_t keycode)
{
struct xkb_keymap *keymap;
+ xkb_keysym_t sym;
const xkb_keysym_t *syms;
int nsyms;
char s[16];
xkb_layout_index_t layout;
+ enum xkb_compose_status status;
keymap = xkb_state_get_keymap(state);
@@ -363,21 +367,34 @@ test_print_keycode_state(struct xkb_state *state, xkb_keycode_t keycode)
if (nsyms <= 0)
return;
- if (nsyms == 1) {
- xkb_keysym_t sym = xkb_state_key_get_one_sym(state, keycode);
- xkb_keysym_get_name(sym, s, sizeof(s));
- printf("keysym [ %-*s ] ", (int) sizeof(s), s);
+ status = XKB_COMPOSE_NOTHING;
+ if (compose_state)
+ status = xkb_compose_state_get_status(compose_state);
+
+ if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
+ return;
+
+ if (status == XKB_COMPOSE_COMPOSED) {
+ sym = xkb_compose_state_get_one_sym(compose_state);
+ syms = &sym;
+ nsyms = 1;
}
- else {
- printf("keysyms [ ");
- for (int i = 0; i < nsyms; i++) {
- xkb_keysym_get_name(syms[i], s, sizeof(s));
- printf("%-*s ", (int) sizeof(s), s);
- }
- printf("] ");
+ else if (nsyms == 1) {
+ sym = xkb_state_key_get_one_sym(state, keycode);
+ syms = &sym;
}
- xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
+ printf("keysyms [ ");
+ for (int i = 0; i < nsyms; i++) {
+ xkb_keysym_get_name(syms[i], s, sizeof(s));
+ printf("%-*s ", (int) sizeof(s), s);
+ }
+ printf("] ");
+
+ if (status == XKB_COMPOSE_COMPOSED)
+ xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
+ else
+ xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
printf("unicode [ %s ] ", s);
layout = xkb_state_key_get_layout(state, keycode);
diff --git a/test/interactive-evdev.c b/test/interactive-evdev.c
index c3c4965..04a347f 100644
--- a/test/interactive-evdev.c
+++ b/test/interactive-evdev.c
@@ -40,12 +40,14 @@ struct keyboard {
char *path;
int fd;
struct xkb_state *state;
+ struct xkb_compose_state *compose_state;
struct keyboard *next;
};
static bool terminate;
static int evdev_offset = 8;
static bool report_state_changes;
+static bool with_compose;
#define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
@@ -85,12 +87,13 @@ is_keyboard(int fd)
static int
keyboard_new(struct dirent *ent, struct xkb_keymap *keymap,
- struct keyboard **out)
+ struct xkb_compose_table *compose_table, struct keyboard **out)
{
int ret;
char *path;
int fd;
struct xkb_state *state;
+ struct xkb_compose_state *compose_state = NULL;
struct keyboard *kbd;
ret = asprintf(&path, "/dev/input/%s", ent->d_name);
@@ -116,18 +119,31 @@ keyboard_new(struct dirent *ent, struct xkb_keymap *keymap,
goto err_fd;
}
+ if (with_compose) {
+ compose_state = xkb_compose_state_new(compose_table,
+ XKB_COMPOSE_STATE_NO_FLAGS);
+ if (!compose_state) {
+ fprintf(stderr, "Couldn't create compose state for %s\n", path);
+ ret = -EFAULT;
+ goto err_state;
+ }
+ }
+
kbd = calloc(1, sizeof(*kbd));
if (!kbd) {
ret = -ENOMEM;
- goto err_state;
+ goto err_compose_state;
}
kbd->path = path;
kbd->fd = fd;
kbd->state = state;
+ kbd->compose_state = compose_state;
*out = kbd;
return 0;
+err_compose_state:
+ xkb_compose_state_unref(compose_state);
err_state:
xkb_state_unref(state);
err_fd:
@@ -146,6 +162,7 @@ keyboard_free(struct keyboard *kbd)
close(kbd->fd);
free(kbd->path);
xkb_state_unref(kbd->state);
+ xkb_compose_state_unref(kbd->compose_state);
free(kbd);
}
@@ -156,7 +173,8 @@ filter_device_name(const struct dirent *ent)
}
static struct keyboard *
-get_keyboards(struct xkb_keymap *keymap)
+get_keyboards(struct xkb_keymap *keymap,
+ struct xkb_compose_table *compose_table)
{
int ret, i, nents;
struct dirent **ents;
@@ -169,7 +187,7 @@ get_keyboards(struct xkb_keymap *keymap)
}
for (i = 0; i < nents; i++) {
- ret = keyboard_new(ents[i], keymap, &kbd);
+ ret = keyboard_new(ents[i], keymap, compose_table, &kbd);
if (ret) {
if (ret == -EACCES) {
fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
@@ -225,6 +243,7 @@ process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
xkb_keycode_t keycode;
struct xkb_keymap *keymap;
enum xkb_state_component changed;
+ enum xkb_compose_status status;
if (type != EV_KEY)
return;
@@ -235,8 +254,19 @@ process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
return;
+ if (with_compose && value != KEY_STATE_RELEASE) {
+ xkb_keysym_t keysym = xkb_state_key_get_one_sym(kbd->state, keycode);
+ xkb_compose_state_feed(kbd->compose_state, keysym);
+ }
+
if (value != KEY_STATE_RELEASE)
- test_print_keycode_state(kbd->state, keycode);
+ test_print_keycode_state(kbd->state, kbd->compose_state, keycode);
+
+ if (with_compose) {
+ status = xkb_compose_state_get_status(kbd->compose_state);
+ if (status == XKB_COMPOSE_CANCELLED || status == XKB_COMPOSE_COMPOSED)
+ xkb_compose_state_reset(kbd->compose_state);
+ }
if (value == KEY_STATE_RELEASE)
changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
@@ -339,17 +369,19 @@ main(int argc, char *argv[])
struct keyboard *kbds;
struct xkb_context *ctx;
struct xkb_keymap *keymap;
+ struct xkb_compose_table *compose_table = NULL;
const char *rules = NULL;
const char *model = NULL;
const char *layout = NULL;
const char *variant = NULL;
const char *options = NULL;
const char *keymap_path = NULL;
+ const char *locale;
struct sigaction act;
setlocale(LC_ALL, "");
- while ((opt = getopt(argc, argv, "r:m:l:v:o:k:n:c")) != -1) {
+ while ((opt = getopt(argc, argv, "r:m:l:v:o:k:n:cd")) != -1) {
switch (opt) {
case 'r':
rules = optarg;
@@ -380,6 +412,9 @@ main(int argc, char *argv[])
case 'c':
report_state_changes = true;
break;
+ case 'd':
+ with_compose = true;
+ break;
case '?':
fprintf(stderr, " Usage: %s [-r <rules>] [-m <model>] "
"[-l <layout>] [-v <variant>] [-o <options>]\n",
@@ -387,7 +422,8 @@ main(int argc, char *argv[])
fprintf(stderr, " or: %s -k <path to keymap file>\n",
argv[0]);
fprintf(stderr, "For both: -n <evdev keycode offset>\n"
- " -c (to report changes to the state)\n");
+ " -c (to report changes to the state)\n"
+ " -d (to enable compose)\n");
exit(2);
}
}
@@ -422,10 +458,22 @@ main(int argc, char *argv[])
goto err_ctx;
}
- kbds = get_keyboards(keymap);
+ if (with_compose) {
+ locale = setlocale(LC_CTYPE, NULL);
+ compose_table =
+ xkb_compose_table_new_from_locale(ctx, locale,
+ XKB_COMPOSE_COMPILE_NO_FLAGS);
+ if (!compose_table) {
+ ret = -1;
+ fprintf(stderr, "Couldn't create compose from locale\n");
+ goto err_xkb;
+ }
+ }
+
+ kbds = get_keyboards(keymap, compose_table);
if (!kbds) {
ret = -1;
- goto err_xkb;
+ goto err_compose;
}
act.sa_handler = sigintr_handler;
@@ -444,6 +492,8 @@ main(int argc, char *argv[])
err_stty:
system("stty echo");
free_keyboards(kbds);
+err_compose:
+ xkb_compose_table_unref(compose_table);
err_xkb:
xkb_keymap_unref(keymap);
err_ctx:
diff --git a/test/interactive-x11.c b/test/interactive-x11.c
index 8679dda..d720004 100644
--- a/test/interactive-x11.c
+++ b/test/interactive-x11.c
@@ -236,7 +236,7 @@ process_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
xcb_key_press_event_t *event = (xcb_key_press_event_t *) gevent;
xkb_keycode_t keycode = event->detail;
- test_print_keycode_state(kbd->state, keycode);
+ test_print_keycode_state(kbd->state, NULL, keycode);
/* Exit on ESC. */
if (keycode == 9)
diff --git a/test/test.h b/test/test.h
index d0104ce..628ec18 100644
--- a/test/test.h
+++ b/test/test.h
@@ -28,6 +28,7 @@
/* Don't use compat names in internal code. */
#define _XKBCOMMON_COMPAT_H
#include "xkbcommon/xkbcommon.h"
+#include "xkbcommon/xkbcommon-compose.h"
#include "utils.h"
/* Automake test exit code to signify SKIP (à la PASS, FAIL, etc). */
@@ -81,7 +82,9 @@ test_compile_rules(struct xkb_context *context, const char *rules,
const char *options);
void
-test_print_keycode_state(struct xkb_state *state, xkb_keycode_t keycode);
+test_print_keycode_state(struct xkb_state *state,
+ struct xkb_compose_state *compose_state,
+ xkb_keycode_t keycode);
void
test_print_state_changes(enum xkb_state_component changed);