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 | |
| 46 | struct simple_im { |
| 47 | struct input_method *input_method; |
| 48 | struct input_method_context *context; |
Kristian Høgsberg | 8c03616 | 2012-11-27 13:47:16 -0500 | [diff] [blame] | 49 | struct xkb_context *xkb_context; |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 50 | struct wl_display *display; |
| 51 | struct wl_registry *registry; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 52 | struct wl_keyboard *keyboard; |
| 53 | struct keyboard_input *keyboard_input; |
| 54 | enum compose_state compose_state; |
| 55 | struct compose_seq compose_seq; |
| 56 | }; |
| 57 | |
| 58 | static const struct compose_seq compose_seqs[] = { |
| 59 | { { XKB_KEY_quotedbl, XKB_KEY_A, 0 }, "Ä" }, |
| 60 | { { XKB_KEY_quotedbl, XKB_KEY_O, 0 }, "Ö" }, |
| 61 | { { XKB_KEY_quotedbl, XKB_KEY_U, 0 }, "Ü" }, |
| 62 | { { XKB_KEY_quotedbl, XKB_KEY_a, 0 }, "ä" }, |
| 63 | { { XKB_KEY_quotedbl, XKB_KEY_o, 0 }, "ö" }, |
| 64 | { { XKB_KEY_quotedbl, XKB_KEY_u, 0 }, "ü" }, |
| 65 | { { XKB_KEY_apostrophe, XKB_KEY_A, 0 }, "Á" }, |
| 66 | { { XKB_KEY_apostrophe, XKB_KEY_a, 0 }, "á" }, |
| 67 | { { XKB_KEY_O, XKB_KEY_C, 0 }, "©" }, |
| 68 | { { XKB_KEY_O, XKB_KEY_R, 0 }, "®" }, |
| 69 | { { XKB_KEY_s, XKB_KEY_s, 0 }, "ß" }, |
| 70 | }; |
| 71 | |
| 72 | static const uint32_t ignore_keys_on_compose[] = { |
| 73 | XKB_KEY_Shift_L, |
| 74 | XKB_KEY_Shift_R |
| 75 | }; |
| 76 | |
Kristian Høgsberg | 1199b16 | 2012-11-27 13:41:48 -0500 | [diff] [blame] | 77 | typedef void (*keyboard_input_key_handler_t)(struct keyboard_input *keyboard_input, |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 78 | uint32_t serial, |
Kristian Høgsberg | 1199b16 | 2012-11-27 13:41:48 -0500 | [diff] [blame] | 79 | uint32_t time, uint32_t key, uint32_t unicode, |
| 80 | enum wl_keyboard_key_state state, void *data); |
| 81 | |
| 82 | struct keyboard_input { |
| 83 | struct xkb_context *xkb_context; |
| 84 | |
| 85 | uint32_t modifiers; |
| 86 | |
| 87 | struct xkb_keymap *keymap; |
| 88 | struct xkb_state *state; |
| 89 | xkb_mod_mask_t control_mask; |
| 90 | xkb_mod_mask_t alt_mask; |
| 91 | xkb_mod_mask_t shift_mask; |
| 92 | |
| 93 | void *user_data; |
| 94 | keyboard_input_key_handler_t key_handler; |
| 95 | }; |
| 96 | |
| 97 | static struct keyboard_input* |
| 98 | keyboard_input_create(struct xkb_context *xkb_context) |
| 99 | { |
| 100 | struct keyboard_input *keyboard_input; |
| 101 | |
| 102 | keyboard_input = calloc(1, sizeof *keyboard_input); |
| 103 | keyboard_input->xkb_context = xkb_context; |
| 104 | |
| 105 | return keyboard_input; |
| 106 | } |
| 107 | |
| 108 | static void |
Kristian Høgsberg | 1199b16 | 2012-11-27 13:41:48 -0500 | [diff] [blame] | 109 | keyboard_input_set_user_data(struct keyboard_input *keyboard_input, void *data) |
| 110 | { |
| 111 | keyboard_input->user_data = data; |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | keyboard_input_set_key_handler(struct keyboard_input *keyboard_input, |
| 116 | keyboard_input_key_handler_t handler) |
| 117 | { |
| 118 | keyboard_input->key_handler = handler; |
| 119 | } |
| 120 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 121 | static void |
| 122 | input_method_context_surrounding_text(void *data, |
| 123 | struct input_method_context *context, |
| 124 | const char *text, |
| 125 | uint32_t cursor, |
| 126 | uint32_t anchor) |
| 127 | { |
| 128 | fprintf(stderr, "Surrounding text updated: %s\n", text); |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | input_method_context_reset(void *data, |
| 133 | struct input_method_context *context) |
| 134 | { |
| 135 | struct simple_im *keyboard = data; |
| 136 | |
| 137 | fprintf(stderr, "Reset pre-edit buffer\n"); |
| 138 | |
| 139 | keyboard->compose_state = state_normal; |
| 140 | } |
| 141 | |
| 142 | static const struct input_method_context_listener input_method_context_listener = { |
| 143 | input_method_context_surrounding_text, |
| 144 | input_method_context_reset |
| 145 | }; |
| 146 | |
| 147 | static void |
| 148 | input_method_keyboard_keymap(void *data, |
| 149 | struct wl_keyboard *wl_keyboard, |
| 150 | uint32_t format, |
| 151 | int32_t fd, |
| 152 | uint32_t size) |
| 153 | { |
| 154 | struct simple_im *keyboard = data; |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame^] | 155 | struct keyboard_input *keyboard_input = keyboard->keyboard_input; |
| 156 | char *map_str; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 157 | |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame^] | 158 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 159 | close(fd); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 164 | if (map_str == MAP_FAILED) { |
| 165 | close(fd); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | keyboard_input->keymap = |
| 170 | xkb_map_new_from_string(keyboard_input->xkb_context, |
| 171 | map_str, |
| 172 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 173 | 0); |
| 174 | |
| 175 | munmap(map_str, size); |
| 176 | close(fd); |
| 177 | |
| 178 | if (!keyboard_input->keymap) { |
| 179 | fprintf(stderr, "failed to compile keymap\n"); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | keyboard_input->state = xkb_state_new(keyboard_input->keymap); |
| 184 | if (!keyboard_input->state) { |
| 185 | fprintf(stderr, "failed to create XKB state\n"); |
| 186 | xkb_map_unref(keyboard_input->keymap); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | keyboard_input->control_mask = |
| 191 | 1 << xkb_map_mod_get_index(keyboard_input->keymap, "Control"); |
| 192 | keyboard_input->alt_mask = |
| 193 | 1 << xkb_map_mod_get_index(keyboard_input->keymap, "Mod1"); |
| 194 | keyboard_input->shift_mask = |
| 195 | 1 << xkb_map_mod_get_index(keyboard_input->keymap, "Shift"); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
| 199 | input_method_keyboard_key(void *data, |
| 200 | struct wl_keyboard *wl_keyboard, |
| 201 | uint32_t serial, |
| 202 | uint32_t time, |
| 203 | uint32_t key, |
| 204 | uint32_t state_w) |
| 205 | { |
| 206 | struct simple_im *keyboard = data; |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 207 | struct keyboard_input *keyboard_input = keyboard->keyboard_input; |
| 208 | uint32_t code; |
| 209 | uint32_t num_syms; |
| 210 | const xkb_keysym_t *syms; |
| 211 | xkb_keysym_t sym; |
| 212 | enum wl_keyboard_key_state state = state_w; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 213 | |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 214 | if (!keyboard_input->state) |
| 215 | return; |
| 216 | |
| 217 | code = key + 8; |
| 218 | num_syms = xkb_key_get_syms(keyboard_input->state, code, &syms); |
| 219 | |
| 220 | sym = XKB_KEY_NoSymbol; |
| 221 | if (num_syms == 1) |
| 222 | sym = syms[0]; |
| 223 | |
| 224 | if (keyboard_input->key_handler) |
| 225 | (*keyboard_input->key_handler)(keyboard_input, serial, time, key, sym, |
| 226 | state, keyboard_input->user_data); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void |
| 230 | input_method_keyboard_modifiers(void *data, |
| 231 | struct wl_keyboard *wl_keyboard, |
| 232 | uint32_t serial, |
| 233 | uint32_t mods_depressed, |
| 234 | uint32_t mods_latched, |
| 235 | uint32_t mods_locked, |
| 236 | uint32_t group) |
| 237 | { |
| 238 | struct simple_im *keyboard = data; |
| 239 | struct input_method_context *context = keyboard->context; |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 240 | struct keyboard_input *keyboard_input = keyboard->keyboard_input; |
| 241 | xkb_mod_mask_t mask; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 242 | |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 243 | xkb_state_update_mask(keyboard_input->state, mods_depressed, |
| 244 | mods_latched, mods_locked, 0, 0, group); |
| 245 | mask = xkb_state_serialize_mods(keyboard_input->state, |
| 246 | XKB_STATE_DEPRESSED | |
| 247 | XKB_STATE_LATCHED); |
| 248 | |
| 249 | keyboard_input->modifiers = 0; |
| 250 | if (mask & keyboard_input->control_mask) |
| 251 | keyboard_input->modifiers |= MOD_CONTROL_MASK; |
| 252 | if (mask & keyboard_input->alt_mask) |
| 253 | keyboard_input->modifiers |= MOD_ALT_MASK; |
| 254 | if (mask & keyboard_input->shift_mask) |
| 255 | keyboard_input->modifiers |= MOD_SHIFT_MASK; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 256 | |
| 257 | input_method_context_modifiers(context, serial, |
| 258 | mods_depressed, mods_depressed, |
| 259 | mods_latched, group); |
| 260 | } |
| 261 | |
| 262 | static const struct wl_keyboard_listener input_method_keyboard_listener = { |
| 263 | input_method_keyboard_keymap, |
| 264 | NULL, /* enter */ |
| 265 | NULL, /* leave */ |
| 266 | input_method_keyboard_key, |
| 267 | input_method_keyboard_modifiers |
| 268 | }; |
| 269 | |
| 270 | static void |
| 271 | input_method_activate(void *data, |
| 272 | struct input_method *input_method, |
| 273 | struct input_method_context *context) |
| 274 | { |
| 275 | struct simple_im *keyboard = data; |
| 276 | |
| 277 | if (keyboard->context) |
| 278 | input_method_context_destroy(keyboard->context); |
| 279 | |
| 280 | keyboard->compose_state = state_normal; |
| 281 | |
| 282 | keyboard->context = context; |
| 283 | input_method_context_add_listener(context, |
| 284 | &input_method_context_listener, |
| 285 | keyboard); |
| 286 | keyboard->keyboard = input_method_context_grab_keyboard(context); |
| 287 | wl_keyboard_add_listener(keyboard->keyboard, |
| 288 | &input_method_keyboard_listener, |
| 289 | keyboard); |
| 290 | } |
| 291 | |
| 292 | static void |
| 293 | input_method_deactivate(void *data, |
| 294 | struct input_method *input_method, |
| 295 | struct input_method_context *context) |
| 296 | { |
| 297 | struct simple_im *keyboard = data; |
| 298 | |
| 299 | if (!keyboard->context) |
| 300 | return; |
| 301 | |
| 302 | input_method_context_destroy(keyboard->context); |
| 303 | keyboard->context = NULL; |
| 304 | } |
| 305 | |
| 306 | static const struct input_method_listener input_method_listener = { |
| 307 | input_method_activate, |
| 308 | input_method_deactivate |
| 309 | }; |
| 310 | |
| 311 | static void |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 312 | registry_handle_global(void *data, struct wl_registry *registry, |
| 313 | uint32_t name, const char *interface, uint32_t version) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 314 | { |
| 315 | struct simple_im *keyboard = data; |
| 316 | |
| 317 | if (!strcmp(interface, "input_method")) { |
| 318 | keyboard->input_method = |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 319 | wl_registry_bind(registry, name, |
| 320 | &input_method_interface, 1); |
| 321 | input_method_add_listener(keyboard->input_method, |
| 322 | &input_method_listener, keyboard); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 326 | static const struct wl_registry_listener registry_listener = { |
| 327 | registry_handle_global |
| 328 | }; |
| 329 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 330 | static int |
| 331 | compare_compose_keys(const void *c1, const void *c2) |
| 332 | { |
| 333 | const struct compose_seq *cs1 = c1; |
| 334 | const struct compose_seq *cs2 = c2; |
| 335 | int i; |
| 336 | |
| 337 | for (i = 0; cs1->keys[i] != 0 && cs2->keys[i] != 0; i++) { |
| 338 | if (cs1->keys[i] != cs2->keys[i]) |
| 339 | return cs1->keys[i] - cs2->keys[i]; |
| 340 | } |
| 341 | |
| 342 | if (cs1->keys[i] == cs2->keys[i] |
| 343 | || cs1->keys[i] == 0) |
| 344 | return 0; |
| 345 | |
| 346 | return cs1->keys[i] - cs2->keys[i]; |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | simple_im_key_handler(struct keyboard_input *keyboard_input, |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 351 | uint32_t serial, uint32_t time, uint32_t key, uint32_t sym, |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 352 | enum wl_keyboard_key_state state, void *data) |
| 353 | { |
| 354 | struct simple_im *keyboard = data; |
| 355 | struct input_method_context *context = keyboard->context; |
| 356 | char text[64]; |
| 357 | |
| 358 | if (sym == XKB_KEY_Multi_key && |
| 359 | state == WL_KEYBOARD_KEY_STATE_RELEASED && |
| 360 | keyboard->compose_state == state_normal) { |
| 361 | keyboard->compose_state = state_compose; |
| 362 | memset(&keyboard->compose_seq, 0, sizeof(struct compose_seq)); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | if (keyboard->compose_state == state_compose) { |
| 367 | uint32_t i = 0; |
| 368 | struct compose_seq *cs; |
| 369 | |
| 370 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 371 | return; |
| 372 | |
| 373 | for (i = 0; i < sizeof(ignore_keys_on_compose) / sizeof(ignore_keys_on_compose[0]); i++) { |
| 374 | if (sym == ignore_keys_on_compose[i]) { |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 375 | input_method_context_key(context, serial, time, key, state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 376 | return; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | for (i = 0; keyboard->compose_seq.keys[i] != 0; i++); |
| 381 | |
| 382 | keyboard->compose_seq.keys[i] = sym; |
| 383 | |
| 384 | cs = bsearch (&keyboard->compose_seq, compose_seqs, |
| 385 | sizeof(compose_seqs) / sizeof(compose_seqs[0]), |
| 386 | sizeof(compose_seqs[0]), compare_compose_keys); |
| 387 | |
| 388 | if (cs) { |
| 389 | if (cs->keys[i + 1] == 0) { |
| 390 | input_method_context_preedit_string(keyboard->context, |
| 391 | "", 0); |
| 392 | input_method_context_commit_string(keyboard->context, |
| 393 | cs->text, |
| 394 | strlen(cs->text)); |
| 395 | keyboard->compose_state = state_normal; |
| 396 | } else { |
| 397 | uint32_t j = 0, idx = 0; |
| 398 | |
| 399 | for (; j <= i; j++) { |
| 400 | idx += xkb_keysym_to_utf8(cs->keys[j], text + idx, sizeof(text) - idx); |
| 401 | } |
| 402 | |
| 403 | input_method_context_preedit_string(keyboard->context, |
| 404 | text, |
| 405 | strlen(text)); |
| 406 | } |
| 407 | } else { |
| 408 | uint32_t j = 0, idx = 0; |
| 409 | |
| 410 | for (; j <= i; j++) { |
| 411 | idx += xkb_keysym_to_utf8(keyboard->compose_seq.keys[j], text + idx, sizeof(text) - idx); |
| 412 | } |
| 413 | input_method_context_preedit_string(keyboard->context, |
| 414 | "", 0); |
| 415 | input_method_context_commit_string(keyboard->context, |
| 416 | text, |
| 417 | strlen(text)); |
| 418 | keyboard->compose_state = state_normal; |
| 419 | } |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) { |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 424 | input_method_context_key(context, serial, time, key, state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
| 428 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 429 | return; |
| 430 | |
| 431 | input_method_context_commit_string(keyboard->context, |
| 432 | text, |
| 433 | strlen(text)); |
| 434 | } |
| 435 | |
| 436 | int |
| 437 | main(int argc, char *argv[]) |
| 438 | { |
| 439 | struct simple_im simple_im; |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 440 | int ret = 0; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 441 | |
| 442 | memset(&simple_im, 0, sizeof(simple_im)); |
| 443 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 444 | simple_im.display = wl_display_connect(NULL); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 445 | if (simple_im.display == NULL) { |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 446 | fprintf(stderr, "failed to connect to server: %m\n"); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 447 | return -1; |
| 448 | } |
| 449 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 450 | simple_im.registry = wl_display_get_registry(simple_im.display); |
| 451 | wl_registry_add_listener(simple_im.registry, |
| 452 | ®istry_listener, &simple_im); |
| 453 | wl_display_roundtrip(simple_im.display); |
| 454 | if (simple_im.input_method == NULL) { |
| 455 | fprintf(stderr, "No input_method global\n"); |
| 456 | exit(1); |
| 457 | } |
| 458 | |
Kristian Høgsberg | 8c03616 | 2012-11-27 13:47:16 -0500 | [diff] [blame] | 459 | simple_im.xkb_context = xkb_context_new(0); |
| 460 | if (simple_im.xkb_context == NULL) { |
| 461 | fprintf(stderr, "Failed to create XKB context\n"); |
| 462 | return -1; |
| 463 | } |
| 464 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 465 | simple_im.context = NULL; |
Kristian Høgsberg | 8c03616 | 2012-11-27 13:47:16 -0500 | [diff] [blame] | 466 | simple_im.keyboard_input = keyboard_input_create(simple_im.xkb_context); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 467 | keyboard_input_set_user_data(simple_im.keyboard_input, &simple_im); |
| 468 | keyboard_input_set_key_handler(simple_im.keyboard_input, simple_im_key_handler); |
| 469 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 470 | while (ret != -1) |
| 471 | ret = wl_display_dispatch(simple_im.display); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 472 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 473 | if (ret == -1) { |
| 474 | fprintf(stderr, "Dispatch error: %m\n"); |
| 475 | exit(1); |
| 476 | } |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 477 | |
| 478 | return 0; |
| 479 | } |