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