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