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