Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 10 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
Kristian Høgsberg | c7d2c4c | 2013-08-26 14:43:17 -0700 | [diff] [blame] | 24 | #include <config.h> |
| 25 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Kristian Høgsberg | 1199b16 | 2012-11-27 13:41:48 -0500 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <sys/mman.h> |
| 31 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 32 | |
| 33 | #include <linux/input.h> |
| 34 | |
| 35 | #include "window.h" |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 36 | #include "input-method-unstable-v1-client-protocol.h" |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 37 | |
| 38 | enum compose_state { |
| 39 | state_normal, |
| 40 | state_compose |
| 41 | }; |
| 42 | |
| 43 | struct compose_seq { |
| 44 | uint32_t keys[4]; |
| 45 | |
| 46 | const char *text; |
| 47 | }; |
| 48 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 49 | struct simple_im; |
| 50 | |
| 51 | typedef void (*keyboard_input_key_handler_t)(struct simple_im *keyboard, |
| 52 | uint32_t serial, |
| 53 | uint32_t time, uint32_t key, uint32_t unicode, |
| 54 | enum wl_keyboard_key_state state); |
| 55 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 56 | struct simple_im { |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 57 | struct zwp_input_method_v1 *input_method; |
| 58 | struct zwp_input_method_context_v1 *context; |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 59 | struct wl_display *display; |
| 60 | struct wl_registry *registry; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 61 | struct wl_keyboard *keyboard; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 62 | enum compose_state compose_state; |
| 63 | struct compose_seq compose_seq; |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 64 | |
| 65 | struct xkb_context *xkb_context; |
| 66 | |
| 67 | uint32_t modifiers; |
| 68 | |
| 69 | struct xkb_keymap *keymap; |
| 70 | struct xkb_state *state; |
| 71 | xkb_mod_mask_t control_mask; |
| 72 | xkb_mod_mask_t alt_mask; |
| 73 | xkb_mod_mask_t shift_mask; |
| 74 | |
| 75 | keyboard_input_key_handler_t key_handler; |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 76 | |
| 77 | uint32_t serial; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static const struct compose_seq compose_seqs[] = { |
| 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_quotedbl, XKB_KEY_a, 0 }, "ä" }, |
| 85 | { { XKB_KEY_quotedbl, XKB_KEY_o, 0 }, "ö" }, |
| 86 | { { XKB_KEY_quotedbl, XKB_KEY_u, 0 }, "ü" }, |
| 87 | { { XKB_KEY_apostrophe, XKB_KEY_A, 0 }, "Á" }, |
| 88 | { { XKB_KEY_apostrophe, XKB_KEY_a, 0 }, "á" }, |
Kristian Høgsberg | b88b68f | 2012-11-27 15:11:04 -0500 | [diff] [blame] | 89 | { { XKB_KEY_slash, XKB_KEY_O, 0 }, "Ø" }, |
| 90 | { { XKB_KEY_slash, XKB_KEY_o, 0 }, "ø" }, |
| 91 | { { XKB_KEY_less, XKB_KEY_3, 0 }, "♥" }, |
| 92 | { { XKB_KEY_A, XKB_KEY_A, 0 }, "Å" }, |
| 93 | { { XKB_KEY_A, XKB_KEY_E, 0 }, "Æ" }, |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 94 | { { XKB_KEY_O, XKB_KEY_C, 0 }, "©" }, |
| 95 | { { XKB_KEY_O, XKB_KEY_R, 0 }, "®" }, |
| 96 | { { XKB_KEY_s, XKB_KEY_s, 0 }, "ß" }, |
Kristian Høgsberg | b88b68f | 2012-11-27 15:11:04 -0500 | [diff] [blame] | 97 | { { XKB_KEY_a, XKB_KEY_e, 0 }, "æ" }, |
| 98 | { { XKB_KEY_a, XKB_KEY_a, 0 }, "å" }, |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static const uint32_t ignore_keys_on_compose[] = { |
| 102 | XKB_KEY_Shift_L, |
| 103 | XKB_KEY_Shift_R |
| 104 | }; |
| 105 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 106 | static void |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 107 | handle_surrounding_text(void *data, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 108 | struct zwp_input_method_context_v1 *context, |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 109 | const char *text, |
| 110 | uint32_t cursor, |
| 111 | uint32_t anchor) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 112 | { |
| 113 | fprintf(stderr, "Surrounding text updated: %s\n", text); |
| 114 | } |
| 115 | |
| 116 | static void |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 117 | handle_reset(void *data, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 118 | struct zwp_input_method_context_v1 *context) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 119 | { |
| 120 | struct simple_im *keyboard = data; |
| 121 | |
| 122 | fprintf(stderr, "Reset pre-edit buffer\n"); |
| 123 | |
| 124 | keyboard->compose_state = state_normal; |
| 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, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 129 | struct zwp_input_method_context_v1 *context, |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 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, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 137 | struct zwp_input_method_context_v1 *context, |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 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 | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 144 | handle_commit_state(void *data, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 145 | struct zwp_input_method_context_v1 *context, |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 146 | uint32_t serial) |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 147 | { |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 148 | struct simple_im *keyboard = data; |
| 149 | |
| 150 | keyboard->serial = serial; |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void |
| 154 | handle_preferred_language(void *data, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 155 | struct zwp_input_method_context_v1 *context, |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 156 | const char *language) |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 157 | { |
| 158 | } |
| 159 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 160 | static const struct zwp_input_method_context_v1_listener input_method_context_listener = { |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 161 | handle_surrounding_text, |
| 162 | handle_reset, |
| 163 | handle_content_type, |
| 164 | handle_invoke_action, |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 165 | handle_commit_state, |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 166 | handle_preferred_language |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | static void |
| 170 | input_method_keyboard_keymap(void *data, |
| 171 | struct wl_keyboard *wl_keyboard, |
| 172 | uint32_t format, |
| 173 | int32_t fd, |
| 174 | uint32_t size) |
| 175 | { |
| 176 | struct simple_im *keyboard = data; |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 177 | char *map_str; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 178 | |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 179 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 180 | close(fd); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 185 | if (map_str == MAP_FAILED) { |
| 186 | close(fd); |
| 187 | return; |
| 188 | } |
| 189 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 190 | keyboard->keymap = |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 191 | xkb_keymap_new_from_string(keyboard->xkb_context, |
| 192 | map_str, |
| 193 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 194 | 0); |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 195 | |
| 196 | munmap(map_str, size); |
| 197 | close(fd); |
| 198 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 199 | if (!keyboard->keymap) { |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 200 | fprintf(stderr, "failed to compile keymap\n"); |
| 201 | return; |
| 202 | } |
| 203 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 204 | keyboard->state = xkb_state_new(keyboard->keymap); |
| 205 | if (!keyboard->state) { |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 206 | fprintf(stderr, "failed to create XKB state\n"); |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 207 | xkb_keymap_unref(keyboard->keymap); |
Kristian Høgsberg | aec12b8 | 2012-11-27 14:21:34 -0500 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 211 | keyboard->control_mask = |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 212 | 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Control"); |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 213 | keyboard->alt_mask = |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 214 | 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Mod1"); |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 215 | keyboard->shift_mask = |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 216 | 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Shift"); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void |
| 220 | input_method_keyboard_key(void *data, |
| 221 | struct wl_keyboard *wl_keyboard, |
| 222 | uint32_t serial, |
| 223 | uint32_t time, |
| 224 | uint32_t key, |
| 225 | uint32_t state_w) |
| 226 | { |
| 227 | struct simple_im *keyboard = data; |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 228 | uint32_t code; |
| 229 | uint32_t num_syms; |
| 230 | const xkb_keysym_t *syms; |
| 231 | xkb_keysym_t sym; |
| 232 | enum wl_keyboard_key_state state = state_w; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 233 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 234 | if (!keyboard->state) |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 235 | return; |
| 236 | |
| 237 | code = key + 8; |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 238 | num_syms = xkb_state_key_get_syms(keyboard->state, code, &syms); |
Kristian Høgsberg | 6aae614 | 2012-11-27 14:20:31 -0500 | [diff] [blame] | 239 | |
| 240 | sym = XKB_KEY_NoSymbol; |
| 241 | if (num_syms == 1) |
| 242 | sym = syms[0]; |
| 243 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 244 | if (keyboard->key_handler) |
| 245 | (*keyboard->key_handler)(keyboard, serial, time, key, sym, |
| 246 | state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void |
| 250 | input_method_keyboard_modifiers(void *data, |
| 251 | struct wl_keyboard *wl_keyboard, |
| 252 | uint32_t serial, |
| 253 | uint32_t mods_depressed, |
| 254 | uint32_t mods_latched, |
| 255 | uint32_t mods_locked, |
| 256 | uint32_t group) |
| 257 | { |
| 258 | struct simple_im *keyboard = data; |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 259 | struct zwp_input_method_context_v1 *context = keyboard->context; |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 260 | xkb_mod_mask_t mask; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 261 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 262 | xkb_state_update_mask(keyboard->state, mods_depressed, |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 263 | mods_latched, mods_locked, 0, 0, group); |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 264 | mask = xkb_state_serialize_mods(keyboard->state, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 265 | XKB_STATE_MODS_DEPRESSED | |
| 266 | XKB_STATE_MODS_LATCHED); |
Kristian Høgsberg | 3a3704d | 2012-11-27 14:18:40 -0500 | [diff] [blame] | 267 | |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 268 | keyboard->modifiers = 0; |
| 269 | if (mask & keyboard->control_mask) |
| 270 | keyboard->modifiers |= MOD_CONTROL_MASK; |
| 271 | if (mask & keyboard->alt_mask) |
| 272 | keyboard->modifiers |= MOD_ALT_MASK; |
| 273 | if (mask & keyboard->shift_mask) |
| 274 | keyboard->modifiers |= MOD_SHIFT_MASK; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 275 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 276 | zwp_input_method_context_v1_modifiers(context, serial, |
| 277 | mods_depressed, mods_depressed, |
| 278 | mods_latched, group); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static const struct wl_keyboard_listener input_method_keyboard_listener = { |
| 282 | input_method_keyboard_keymap, |
| 283 | NULL, /* enter */ |
| 284 | NULL, /* leave */ |
| 285 | input_method_keyboard_key, |
| 286 | input_method_keyboard_modifiers |
| 287 | }; |
| 288 | |
| 289 | static void |
| 290 | input_method_activate(void *data, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 291 | struct zwp_input_method_v1 *input_method, |
| 292 | struct zwp_input_method_context_v1 *context) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 293 | { |
| 294 | struct simple_im *keyboard = data; |
| 295 | |
| 296 | if (keyboard->context) |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 297 | zwp_input_method_context_v1_destroy(keyboard->context); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 298 | |
| 299 | keyboard->compose_state = state_normal; |
| 300 | |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 301 | keyboard->serial = 0; |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 302 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 303 | keyboard->context = context; |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 304 | zwp_input_method_context_v1_add_listener(context, |
| 305 | &input_method_context_listener, |
| 306 | keyboard); |
| 307 | keyboard->keyboard = zwp_input_method_context_v1_grab_keyboard(context); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 308 | wl_keyboard_add_listener(keyboard->keyboard, |
| 309 | &input_method_keyboard_listener, |
| 310 | keyboard); |
| 311 | } |
| 312 | |
| 313 | static void |
| 314 | input_method_deactivate(void *data, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 315 | struct zwp_input_method_v1 *input_method, |
| 316 | struct zwp_input_method_context_v1 *context) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 317 | { |
| 318 | struct simple_im *keyboard = data; |
| 319 | |
| 320 | if (!keyboard->context) |
| 321 | return; |
| 322 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 323 | zwp_input_method_context_v1_destroy(keyboard->context); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 324 | keyboard->context = NULL; |
| 325 | } |
| 326 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 327 | static const struct zwp_input_method_v1_listener input_method_listener = { |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 328 | input_method_activate, |
| 329 | input_method_deactivate |
| 330 | }; |
| 331 | |
| 332 | static void |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 333 | registry_handle_global(void *data, struct wl_registry *registry, |
| 334 | uint32_t name, const char *interface, uint32_t version) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 335 | { |
| 336 | struct simple_im *keyboard = data; |
| 337 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 338 | if (!strcmp(interface, "zwp_input_method_v1")) { |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 339 | keyboard->input_method = |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 340 | wl_registry_bind(registry, name, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 341 | &zwp_input_method_v1_interface, 1); |
| 342 | zwp_input_method_v1_add_listener(keyboard->input_method, |
| 343 | &input_method_listener, keyboard); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 347 | static void |
| 348 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 349 | uint32_t name) |
| 350 | { |
| 351 | } |
| 352 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 353 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 354 | registry_handle_global, |
| 355 | registry_handle_global_remove |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 356 | }; |
| 357 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 358 | static int |
| 359 | compare_compose_keys(const void *c1, const void *c2) |
| 360 | { |
| 361 | const struct compose_seq *cs1 = c1; |
| 362 | const struct compose_seq *cs2 = c2; |
| 363 | int i; |
| 364 | |
| 365 | for (i = 0; cs1->keys[i] != 0 && cs2->keys[i] != 0; i++) { |
| 366 | if (cs1->keys[i] != cs2->keys[i]) |
| 367 | return cs1->keys[i] - cs2->keys[i]; |
| 368 | } |
| 369 | |
| 370 | if (cs1->keys[i] == cs2->keys[i] |
| 371 | || cs1->keys[i] == 0) |
| 372 | return 0; |
| 373 | |
| 374 | return cs1->keys[i] - cs2->keys[i]; |
| 375 | } |
| 376 | |
| 377 | static void |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 378 | simple_im_key_handler(struct simple_im *keyboard, |
Kristian Høgsberg | 79bfde2 | 2012-11-27 13:57:27 -0500 | [diff] [blame] | 379 | 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] | 380 | enum wl_keyboard_key_state state) |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 381 | { |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 382 | struct zwp_input_method_context_v1 *context = keyboard->context; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 383 | char text[64]; |
| 384 | |
| 385 | if (sym == XKB_KEY_Multi_key && |
| 386 | state == WL_KEYBOARD_KEY_STATE_RELEASED && |
| 387 | keyboard->compose_state == state_normal) { |
| 388 | keyboard->compose_state = state_compose; |
| 389 | memset(&keyboard->compose_seq, 0, sizeof(struct compose_seq)); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | if (keyboard->compose_state == state_compose) { |
| 394 | uint32_t i = 0; |
| 395 | struct compose_seq *cs; |
| 396 | |
| 397 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 398 | return; |
| 399 | |
| 400 | for (i = 0; i < sizeof(ignore_keys_on_compose) / sizeof(ignore_keys_on_compose[0]); i++) { |
| 401 | if (sym == ignore_keys_on_compose[i]) { |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 402 | zwp_input_method_context_v1_key(context, |
| 403 | keyboard->serial, |
| 404 | time, |
| 405 | key, |
| 406 | state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | for (i = 0; keyboard->compose_seq.keys[i] != 0; i++); |
| 412 | |
| 413 | keyboard->compose_seq.keys[i] = sym; |
| 414 | |
| 415 | cs = bsearch (&keyboard->compose_seq, compose_seqs, |
| 416 | sizeof(compose_seqs) / sizeof(compose_seqs[0]), |
| 417 | sizeof(compose_seqs[0]), compare_compose_keys); |
| 418 | |
| 419 | if (cs) { |
| 420 | if (cs->keys[i + 1] == 0) { |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 421 | zwp_input_method_context_v1_preedit_cursor(keyboard->context, |
| 422 | 0); |
| 423 | zwp_input_method_context_v1_preedit_string(keyboard->context, |
| 424 | keyboard->serial, |
| 425 | "", ""); |
| 426 | zwp_input_method_context_v1_cursor_position(keyboard->context, |
| 427 | 0, 0); |
| 428 | zwp_input_method_context_v1_commit_string(keyboard->context, |
| 429 | keyboard->serial, |
| 430 | cs->text); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 431 | keyboard->compose_state = state_normal; |
| 432 | } else { |
| 433 | uint32_t j = 0, idx = 0; |
| 434 | |
| 435 | for (; j <= i; j++) { |
| 436 | idx += xkb_keysym_to_utf8(cs->keys[j], text + idx, sizeof(text) - idx); |
| 437 | } |
| 438 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 439 | zwp_input_method_context_v1_preedit_cursor(keyboard->context, |
| 440 | strlen(text)); |
| 441 | zwp_input_method_context_v1_preedit_string(keyboard->context, |
| 442 | keyboard->serial, |
| 443 | text, |
| 444 | text); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 445 | } |
| 446 | } else { |
| 447 | uint32_t j = 0, idx = 0; |
| 448 | |
| 449 | for (; j <= i; j++) { |
| 450 | idx += xkb_keysym_to_utf8(keyboard->compose_seq.keys[j], text + idx, sizeof(text) - idx); |
| 451 | } |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 452 | zwp_input_method_context_v1_preedit_cursor(keyboard->context, |
| 453 | 0); |
| 454 | zwp_input_method_context_v1_preedit_string(keyboard->context, |
| 455 | keyboard->serial, |
| 456 | "", ""); |
| 457 | zwp_input_method_context_v1_cursor_position(keyboard->context, |
| 458 | 0, 0); |
| 459 | zwp_input_method_context_v1_commit_string(keyboard->context, |
| 460 | keyboard->serial, |
| 461 | text); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 462 | keyboard->compose_state = state_normal; |
| 463 | } |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) { |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 468 | zwp_input_method_context_v1_key(context, serial, time, key, state); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
| 472 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 473 | return; |
| 474 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 475 | zwp_input_method_context_v1_cursor_position(keyboard->context, |
| 476 | 0, 0); |
| 477 | zwp_input_method_context_v1_commit_string(keyboard->context, |
| 478 | keyboard->serial, |
| 479 | text); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | int |
| 483 | main(int argc, char *argv[]) |
| 484 | { |
| 485 | struct simple_im simple_im; |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 486 | int ret = 0; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 487 | |
| 488 | memset(&simple_im, 0, sizeof(simple_im)); |
| 489 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 490 | simple_im.display = wl_display_connect(NULL); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 491 | if (simple_im.display == NULL) { |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 492 | fprintf(stderr, "failed to connect to server: %m\n"); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 493 | return -1; |
| 494 | } |
| 495 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 496 | simple_im.registry = wl_display_get_registry(simple_im.display); |
| 497 | wl_registry_add_listener(simple_im.registry, |
| 498 | ®istry_listener, &simple_im); |
| 499 | wl_display_roundtrip(simple_im.display); |
| 500 | if (simple_im.input_method == NULL) { |
| 501 | fprintf(stderr, "No input_method global\n"); |
| 502 | exit(1); |
| 503 | } |
| 504 | |
Kristian Høgsberg | 8c03616 | 2012-11-27 13:47:16 -0500 | [diff] [blame] | 505 | simple_im.xkb_context = xkb_context_new(0); |
| 506 | if (simple_im.xkb_context == NULL) { |
| 507 | fprintf(stderr, "Failed to create XKB context\n"); |
| 508 | return -1; |
| 509 | } |
| 510 | |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 511 | simple_im.context = NULL; |
Kristian Høgsberg | 504e8f1 | 2012-11-27 14:28:19 -0500 | [diff] [blame] | 512 | simple_im.key_handler = simple_im_key_handler; |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 513 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 514 | while (ret != -1) |
| 515 | ret = wl_display_dispatch(simple_im.display); |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 516 | |
Kristian Høgsberg | de318ab | 2012-11-27 14:07:22 -0500 | [diff] [blame] | 517 | if (ret == -1) { |
| 518 | fprintf(stderr, "Dispatch error: %m\n"); |
| 519 | exit(1); |
| 520 | } |
Jan Arne Petersen | e9fba2b | 2012-11-18 19:06:50 +0100 | [diff] [blame] | 521 | |
| 522 | return 0; |
| 523 | } |