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