Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Kristian Høgsberg | 1199b16 | 2012-11-27 13:41:48 -0500 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <sys/mman.h> |
| 28 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 29 | |
| 30 | #include <linux/input.h> |
| 31 | |
| 32 | #include "window.h" |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 33 | #include "input-method-client-protocol.h" |
| 34 | |
| 35 | enum compose_state { |
| 36 | state_normal, |
| 37 | state_compose |
| 38 | }; |
| 39 | |
| 40 | struct compose_seq { |
| 41 | uint32_t keys[4]; |
| 42 | |
| 43 | const char *text; |
| 44 | }; |
| 45 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 46 | struct simple_im; |
| 47 | |
| 48 | typedef void (*keyboard_input_key_handler_t)(struct simple_im *keyboard, |
| 49 | uint32_t serial, |
| 50 | uint32_t time, uint32_t key, uint32_t unicode, |
| 51 | enum wl_keyboard_key_state state); |
| 52 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 53 | struct simple_im { |
| 54 | struct input_method *input_method; |
| 55 | struct input_method_context *context; |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 56 | struct wl_display *display; |
| 57 | struct wl_registry *registry; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 58 | struct wl_keyboard *keyboard; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 59 | enum compose_state compose_state; |
| 60 | struct compose_seq compose_seq; |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 61 | |
| 62 | struct xkb_context *xkb_context; |
| 63 | |
| 64 | uint32_t modifiers; |
| 65 | |
| 66 | struct xkb_keymap *keymap; |
| 67 | struct xkb_state *state; |
| 68 | xkb_mod_mask_t control_mask; |
| 69 | xkb_mod_mask_t alt_mask; |
| 70 | xkb_mod_mask_t shift_mask; |
| 71 | |
| 72 | keyboard_input_key_handler_t key_handler; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static const struct compose_seq compose_seqs[] = { |
| 76 | { { XKB_KEY_quotedbl, XKB_KEY_A, 0 }, "Ä" }, |
| 77 | { { XKB_KEY_quotedbl, XKB_KEY_O, 0 }, "Ö" }, |
| 78 | { { XKB_KEY_quotedbl, XKB_KEY_U, 0 }, "Ü" }, |
| 79 | { { XKB_KEY_quotedbl, XKB_KEY_a, 0 }, "ä" }, |
| 80 | { { XKB_KEY_quotedbl, XKB_KEY_o, 0 }, "ö" }, |
| 81 | { { XKB_KEY_quotedbl, XKB_KEY_u, 0 }, "ü" }, |
| 82 | { { XKB_KEY_apostrophe, XKB_KEY_A, 0 }, "Á" }, |
| 83 | { { XKB_KEY_apostrophe, XKB_KEY_a, 0 }, "á" }, |
Kristian Høgsberg | b88b68f | 2012-11-27 15:11:04 -0500 | [diff] [blame] | 84 | { { XKB_KEY_slash, XKB_KEY_O, 0 }, "Ø" }, |
| 85 | { { XKB_KEY_slash, XKB_KEY_o, 0 }, "ø" }, |
| 86 | { { XKB_KEY_less, XKB_KEY_3, 0 }, "♥" }, |
| 87 | { { XKB_KEY_A, XKB_KEY_A, 0 }, "Å" }, |
| 88 | { { XKB_KEY_A, XKB_KEY_E, 0 }, "Æ" }, |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 89 | { { XKB_KEY_O, XKB_KEY_C, 0 }, "©" }, |
| 90 | { { XKB_KEY_O, XKB_KEY_R, 0 }, "®" }, |
| 91 | { { XKB_KEY_s, XKB_KEY_s, 0 }, "ß" }, |
Kristian Høgsberg | b88b68f | 2012-11-27 15:11:04 -0500 | [diff] [blame] | 92 | { { XKB_KEY_a, XKB_KEY_e, 0 }, "æ" }, |
| 93 | { { XKB_KEY_a, XKB_KEY_a, 0 }, "å" }, |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | static const uint32_t ignore_keys_on_compose[] = { |
| 97 | XKB_KEY_Shift_L, |
| 98 | XKB_KEY_Shift_R |
| 99 | }; |
| 100 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 101 | static void |
| 102 | input_method_context_surrounding_text(void *data, |
| 103 | struct input_method_context *context, |
| 104 | const char *text, |
| 105 | uint32_t cursor, |
| 106 | uint32_t anchor) |
| 107 | { |
| 108 | fprintf(stderr, "Surrounding text updated: %s\n", text); |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | input_method_context_reset(void *data, |
| 113 | struct input_method_context *context) |
| 114 | { |
| 115 | struct simple_im *keyboard = data; |
| 116 | |
| 117 | fprintf(stderr, "Reset pre-edit buffer\n"); |
| 118 | |
| 119 | keyboard->compose_state = state_normal; |
| 120 | } |
| 121 | |
| 122 | static const struct input_method_context_listener input_method_context_listener = { |
| 123 | input_method_context_surrounding_text, |
| 124 | input_method_context_reset |
| 125 | }; |
| 126 | |
| 127 | static void |
| 128 | input_method_keyboard_keymap(void *data, |
| 129 | struct wl_keyboard *wl_keyboard, |
| 130 | uint32_t format, |
| 131 | int32_t fd, |
| 132 | uint32_t size) |
| 133 | { |
| 134 | struct simple_im *keyboard = data; |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 135 | char *map_str; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 136 | |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 137 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 138 | close(fd); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 143 | if (map_str == MAP_FAILED) { |
| 144 | close(fd); |
| 145 | return; |
| 146 | } |
| 147 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 148 | keyboard->keymap = |
| 149 | xkb_map_new_from_string(keyboard->xkb_context, |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 150 | map_str, |
| 151 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 152 | 0); |
| 153 | |
| 154 | munmap(map_str, size); |
| 155 | close(fd); |
| 156 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 157 | if (!keyboard->keymap) { |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 158 | fprintf(stderr, "failed to compile keymap\n"); |
| 159 | return; |
| 160 | } |
| 161 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 162 | keyboard->state = xkb_state_new(keyboard->keymap); |
| 163 | if (!keyboard->state) { |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 164 | fprintf(stderr, "failed to create XKB state\n"); |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 165 | xkb_map_unref(keyboard->keymap); |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 169 | keyboard->control_mask = |
| 170 | 1 << xkb_map_mod_get_index(keyboard->keymap, "Control"); |
| 171 | keyboard->alt_mask = |
| 172 | 1 << xkb_map_mod_get_index(keyboard->keymap, "Mod1"); |
| 173 | keyboard->shift_mask = |
| 174 | 1 << xkb_map_mod_get_index(keyboard->keymap, "Shift"); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void |
| 178 | input_method_keyboard_key(void *data, |
| 179 | struct wl_keyboard *wl_keyboard, |
| 180 | uint32_t serial, |
| 181 | uint32_t time, |
| 182 | uint32_t key, |
| 183 | uint32_t state_w) |
| 184 | { |
| 185 | struct simple_im *keyboard = data; |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 186 | uint32_t code; |
| 187 | uint32_t num_syms; |
| 188 | const xkb_keysym_t *syms; |
| 189 | xkb_keysym_t sym; |
| 190 | enum wl_keyboard_key_state state = state_w; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 191 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 192 | if (!keyboard->state) |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 193 | return; |
| 194 | |
| 195 | code = key + 8; |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 196 | num_syms = xkb_key_get_syms(keyboard->state, code, &syms); |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 197 | |
| 198 | sym = XKB_KEY_NoSymbol; |
| 199 | if (num_syms == 1) |
| 200 | sym = syms[0]; |
| 201 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 202 | if (keyboard->key_handler) |
| 203 | (*keyboard->key_handler)(keyboard, serial, time, key, sym, |
| 204 | state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void |
| 208 | input_method_keyboard_modifiers(void *data, |
| 209 | struct wl_keyboard *wl_keyboard, |
| 210 | uint32_t serial, |
| 211 | uint32_t mods_depressed, |
| 212 | uint32_t mods_latched, |
| 213 | uint32_t mods_locked, |
| 214 | uint32_t group) |
| 215 | { |
| 216 | struct simple_im *keyboard = data; |
| 217 | struct input_method_context *context = keyboard->context; |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 218 | xkb_mod_mask_t mask; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 219 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 220 | xkb_state_update_mask(keyboard->state, mods_depressed, |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 221 | mods_latched, mods_locked, 0, 0, group); |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 222 | mask = xkb_state_serialize_mods(keyboard->state, |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 223 | XKB_STATE_DEPRESSED | |
| 224 | XKB_STATE_LATCHED); |
| 225 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 226 | keyboard->modifiers = 0; |
| 227 | if (mask & keyboard->control_mask) |
| 228 | keyboard->modifiers |= MOD_CONTROL_MASK; |
| 229 | if (mask & keyboard->alt_mask) |
| 230 | keyboard->modifiers |= MOD_ALT_MASK; |
| 231 | if (mask & keyboard->shift_mask) |
| 232 | keyboard->modifiers |= MOD_SHIFT_MASK; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 233 | |
| 234 | input_method_context_modifiers(context, serial, |
| 235 | mods_depressed, mods_depressed, |
| 236 | mods_latched, group); |
| 237 | } |
| 238 | |
| 239 | static const struct wl_keyboard_listener input_method_keyboard_listener = { |
| 240 | input_method_keyboard_keymap, |
| 241 | NULL, /* enter */ |
| 242 | NULL, /* leave */ |
| 243 | input_method_keyboard_key, |
| 244 | input_method_keyboard_modifiers |
| 245 | }; |
| 246 | |
| 247 | static void |
| 248 | input_method_activate(void *data, |
| 249 | struct input_method *input_method, |
| 250 | struct input_method_context *context) |
| 251 | { |
| 252 | struct simple_im *keyboard = data; |
| 253 | |
| 254 | if (keyboard->context) |
| 255 | input_method_context_destroy(keyboard->context); |
| 256 | |
| 257 | keyboard->compose_state = state_normal; |
| 258 | |
| 259 | keyboard->context = context; |
| 260 | input_method_context_add_listener(context, |
| 261 | &input_method_context_listener, |
| 262 | keyboard); |
| 263 | keyboard->keyboard = input_method_context_grab_keyboard(context); |
| 264 | wl_keyboard_add_listener(keyboard->keyboard, |
| 265 | &input_method_keyboard_listener, |
| 266 | keyboard); |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | input_method_deactivate(void *data, |
| 271 | struct input_method *input_method, |
| 272 | struct input_method_context *context) |
| 273 | { |
| 274 | struct simple_im *keyboard = data; |
| 275 | |
| 276 | if (!keyboard->context) |
| 277 | return; |
| 278 | |
| 279 | input_method_context_destroy(keyboard->context); |
| 280 | keyboard->context = NULL; |
| 281 | } |
| 282 | |
| 283 | static const struct input_method_listener input_method_listener = { |
| 284 | input_method_activate, |
| 285 | input_method_deactivate |
| 286 | }; |
| 287 | |
| 288 | static void |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 289 | registry_handle_global(void *data, struct wl_registry *registry, |
| 290 | uint32_t name, const char *interface, uint32_t version) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 291 | { |
| 292 | struct simple_im *keyboard = data; |
| 293 | |
| 294 | if (!strcmp(interface, "input_method")) { |
| 295 | keyboard->input_method = |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 296 | wl_registry_bind(registry, name, |
| 297 | &input_method_interface, 1); |
| 298 | input_method_add_listener(keyboard->input_method, |
| 299 | &input_method_listener, keyboard); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame^] | 303 | static void |
| 304 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 305 | uint32_t name) |
| 306 | { |
| 307 | } |
| 308 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 309 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame^] | 310 | registry_handle_global, |
| 311 | registry_handle_global_remove |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 312 | }; |
| 313 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 314 | static int |
| 315 | compare_compose_keys(const void *c1, const void *c2) |
| 316 | { |
| 317 | const struct compose_seq *cs1 = c1; |
| 318 | const struct compose_seq *cs2 = c2; |
| 319 | int i; |
| 320 | |
| 321 | for (i = 0; cs1->keys[i] != 0 && cs2->keys[i] != 0; i++) { |
| 322 | if (cs1->keys[i] != cs2->keys[i]) |
| 323 | return cs1->keys[i] - cs2->keys[i]; |
| 324 | } |
| 325 | |
| 326 | if (cs1->keys[i] == cs2->keys[i] |
| 327 | || cs1->keys[i] == 0) |
| 328 | return 0; |
| 329 | |
| 330 | return cs1->keys[i] - cs2->keys[i]; |
| 331 | } |
| 332 | |
| 333 | static void |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 334 | simple_im_key_handler(struct simple_im *keyboard, |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 335 | uint32_t serial, uint32_t time, uint32_t key, uint32_t sym, |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 336 | enum wl_keyboard_key_state state) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 337 | { |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 338 | struct input_method_context *context = keyboard->context; |
| 339 | char text[64]; |
| 340 | |
| 341 | if (sym == XKB_KEY_Multi_key && |
| 342 | state == WL_KEYBOARD_KEY_STATE_RELEASED && |
| 343 | keyboard->compose_state == state_normal) { |
| 344 | keyboard->compose_state = state_compose; |
| 345 | memset(&keyboard->compose_seq, 0, sizeof(struct compose_seq)); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (keyboard->compose_state == state_compose) { |
| 350 | uint32_t i = 0; |
| 351 | struct compose_seq *cs; |
| 352 | |
| 353 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 354 | return; |
| 355 | |
| 356 | for (i = 0; i < sizeof(ignore_keys_on_compose) / sizeof(ignore_keys_on_compose[0]); i++) { |
| 357 | if (sym == ignore_keys_on_compose[i]) { |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 358 | input_method_context_key(context, serial, time, key, state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | for (i = 0; keyboard->compose_seq.keys[i] != 0; i++); |
| 364 | |
| 365 | keyboard->compose_seq.keys[i] = sym; |
| 366 | |
| 367 | cs = bsearch (&keyboard->compose_seq, compose_seqs, |
| 368 | sizeof(compose_seqs) / sizeof(compose_seqs[0]), |
| 369 | sizeof(compose_seqs[0]), compare_compose_keys); |
| 370 | |
| 371 | if (cs) { |
| 372 | if (cs->keys[i + 1] == 0) { |
| 373 | input_method_context_preedit_string(keyboard->context, |
| 374 | "", 0); |
| 375 | input_method_context_commit_string(keyboard->context, |
| 376 | cs->text, |
| 377 | strlen(cs->text)); |
| 378 | keyboard->compose_state = state_normal; |
| 379 | } else { |
| 380 | uint32_t j = 0, idx = 0; |
| 381 | |
| 382 | for (; j <= i; j++) { |
| 383 | idx += xkb_keysym_to_utf8(cs->keys[j], text + idx, sizeof(text) - idx); |
| 384 | } |
| 385 | |
| 386 | input_method_context_preedit_string(keyboard->context, |
| 387 | text, |
| 388 | strlen(text)); |
| 389 | } |
| 390 | } else { |
| 391 | uint32_t j = 0, idx = 0; |
| 392 | |
| 393 | for (; j <= i; j++) { |
| 394 | idx += xkb_keysym_to_utf8(keyboard->compose_seq.keys[j], text + idx, sizeof(text) - idx); |
| 395 | } |
| 396 | input_method_context_preedit_string(keyboard->context, |
| 397 | "", 0); |
| 398 | input_method_context_commit_string(keyboard->context, |
| 399 | text, |
| 400 | strlen(text)); |
| 401 | keyboard->compose_state = state_normal; |
| 402 | } |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) { |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 407 | input_method_context_key(context, serial, time, key, state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | |
| 411 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 412 | return; |
| 413 | |
| 414 | input_method_context_commit_string(keyboard->context, |
| 415 | text, |
| 416 | strlen(text)); |
| 417 | } |
| 418 | |
| 419 | int |
| 420 | main(int argc, char *argv[]) |
| 421 | { |
| 422 | struct simple_im simple_im; |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 423 | int ret = 0; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 424 | |
| 425 | memset(&simple_im, 0, sizeof(simple_im)); |
| 426 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 427 | simple_im.display = wl_display_connect(NULL); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 428 | if (simple_im.display == NULL) { |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 429 | fprintf(stderr, "failed to connect to server: %m\n"); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 430 | return -1; |
| 431 | } |
| 432 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 433 | simple_im.registry = wl_display_get_registry(simple_im.display); |
| 434 | wl_registry_add_listener(simple_im.registry, |
| 435 | ®istry_listener, &simple_im); |
| 436 | wl_display_roundtrip(simple_im.display); |
| 437 | if (simple_im.input_method == NULL) { |
| 438 | fprintf(stderr, "No input_method global\n"); |
| 439 | exit(1); |
| 440 | } |
| 441 | |
Kristian Høgsberg | 8c03616 | 2012-11-27 13:47:16 -0500 | [diff] [blame] | 442 | simple_im.xkb_context = xkb_context_new(0); |
| 443 | if (simple_im.xkb_context == NULL) { |
| 444 | fprintf(stderr, "Failed to create XKB context\n"); |
| 445 | return -1; |
| 446 | } |
| 447 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 448 | simple_im.context = NULL; |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 449 | simple_im.key_handler = simple_im_key_handler; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 450 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 451 | while (ret != -1) |
| 452 | ret = wl_display_dispatch(simple_im.display); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 453 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 454 | if (ret == -1) { |
| 455 | fprintf(stderr, "Dispatch error: %m\n"); |
| 456 | exit(1); |
| 457 | } |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 458 | |
| 459 | return 0; |
| 460 | } |