Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Openismus GmbH |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Intel Corporation |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 25 | #include <string.h> |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 26 | |
| 27 | #include "compositor.h" |
| 28 | #include "text-server-protocol.h" |
Jan Arne Petersen | 30b66ef | 2012-09-09 23:08:41 +0200 | [diff] [blame] | 29 | #include "input-method-server-protocol.h" |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 30 | |
| 31 | struct input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 32 | struct input_method_context; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 33 | struct text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 34 | |
| 35 | struct text_model { |
| 36 | struct wl_resource resource; |
| 37 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 38 | struct weston_compositor *ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 39 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 40 | struct wl_list input_methods; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 41 | |
| 42 | struct wl_surface *surface; |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 43 | |
| 44 | uint32_t input_panel_visible; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 47 | struct text_model_factory { |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 48 | struct wl_global *text_model_factory_global; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 49 | struct wl_listener destroy_listener; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 50 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 51 | struct weston_compositor *ec; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | struct input_method { |
| 55 | struct wl_resource *input_method_binding; |
| 56 | struct wl_global *input_method_global; |
| 57 | struct wl_listener destroy_listener; |
| 58 | |
| 59 | struct weston_seat *seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 60 | struct text_model *model; |
| 61 | |
| 62 | struct wl_list link; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 63 | |
| 64 | struct wl_listener keyboard_focus_listener; |
| 65 | |
| 66 | int focus_listener_initialized; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 67 | |
| 68 | struct input_method_context *context; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 69 | |
| 70 | struct text_backend *text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 73 | struct input_method_context { |
| 74 | struct wl_resource resource; |
| 75 | |
| 76 | struct text_model *model; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 77 | struct input_method *input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 78 | |
| 79 | struct wl_list link; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 80 | |
| 81 | struct wl_resource *keyboard; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 84 | struct text_backend { |
| 85 | struct weston_compositor *compositor; |
| 86 | |
| 87 | struct { |
| 88 | char *path; |
| 89 | struct wl_resource *binding; |
| 90 | struct weston_process process; |
| 91 | struct wl_client *client; |
| 92 | } input_method; |
| 93 | |
| 94 | struct wl_listener seat_created_listener; |
| 95 | struct wl_listener destroy_listener; |
| 96 | }; |
| 97 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 98 | static void input_method_context_create(struct text_model *model, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 99 | struct input_method *input_method, |
| 100 | uint32_t serial); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 101 | static void input_method_context_end_keyboard_grab(struct input_method_context *context); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 102 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 103 | static void input_method_init_seat(struct weston_seat *seat); |
| 104 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 105 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 106 | deactivate_text_model(struct text_model *text_model, |
| 107 | struct input_method *input_method) |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 108 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 109 | struct weston_compositor *ec = text_model->ec; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 110 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 111 | if (input_method->model == text_model) { |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 112 | if (input_method->context && input_method->input_method_binding) { |
| 113 | input_method_context_end_keyboard_grab(input_method->context); |
| 114 | input_method_send_deactivate(input_method->input_method_binding, |
| 115 | &input_method->context->resource); |
| 116 | } |
| 117 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 118 | wl_list_remove(&input_method->link); |
| 119 | input_method->model = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 120 | input_method->context = NULL; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 121 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 122 | text_model_send_leave(&text_model->resource); |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 127 | destroy_text_model(struct wl_resource *resource) |
| 128 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 129 | struct text_model *text_model = |
| 130 | container_of(resource, struct text_model, resource); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 131 | struct input_method *input_method, *next; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 132 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 133 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) |
| 134 | deactivate_text_model(text_model, input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 135 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 136 | free(text_model); |
| 137 | } |
| 138 | |
| 139 | static void |
| 140 | text_model_set_surrounding_text(struct wl_client *client, |
| 141 | struct wl_resource *resource, |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 142 | const char *text, |
| 143 | uint32_t cursor, |
| 144 | uint32_t anchor) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 145 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 146 | struct text_model *text_model = resource->data; |
| 147 | struct input_method *input_method, *next; |
| 148 | |
| 149 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 150 | if (!input_method->context) |
| 151 | continue; |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 152 | input_method_context_send_surrounding_text(&input_method->context->resource, |
| 153 | text, |
| 154 | cursor, |
| 155 | anchor); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 156 | } |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 160 | text_model_activate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 161 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 162 | uint32_t serial, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 163 | struct wl_resource *seat, |
| 164 | struct wl_resource *surface) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 165 | { |
| 166 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 167 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 168 | struct input_method *input_method = weston_seat->input_method; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 169 | struct text_model *old = weston_seat->input_method->model; |
| 170 | struct weston_compositor *ec = text_model->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 171 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 172 | if (old == text_model) |
| 173 | return; |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 174 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 175 | if (old) { |
| 176 | deactivate_text_model(old, |
| 177 | weston_seat->input_method); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 180 | input_method->model = text_model; |
| 181 | wl_list_insert(&text_model->input_methods, &input_method->link); |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 182 | input_method_init_seat(weston_seat); |
| 183 | |
| 184 | text_model->surface = surface->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 185 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 186 | input_method_context_create(text_model, input_method, serial); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 187 | |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 188 | if (text_model->input_panel_visible) |
| 189 | wl_signal_emit(&ec->show_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 190 | |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 191 | text_model_send_enter(&text_model->resource, &text_model->surface->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void |
| 195 | text_model_deactivate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 196 | struct wl_resource *resource, |
| 197 | struct wl_resource *seat) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 198 | { |
| 199 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 200 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 201 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 202 | deactivate_text_model(text_model, |
| 203 | weston_seat->input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 207 | text_model_reset(struct wl_client *client, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 208 | struct wl_resource *resource, |
| 209 | uint32_t serial) |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 210 | { |
| 211 | struct text_model *text_model = resource->data; |
| 212 | struct input_method *input_method, *next; |
| 213 | |
| 214 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 215 | if (!input_method->context) |
| 216 | continue; |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 217 | input_method_context_send_reset(&input_method->context->resource, serial); |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 222 | text_model_set_micro_focus(struct wl_client *client, |
| 223 | struct wl_resource *resource, |
| 224 | int32_t x, |
| 225 | int32_t y, |
| 226 | int32_t width, |
| 227 | int32_t height) |
| 228 | { |
| 229 | } |
| 230 | |
| 231 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 232 | text_model_set_content_type(struct wl_client *client, |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 233 | struct wl_resource *resource, |
| 234 | uint32_t hint, |
| 235 | uint32_t purpose) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 236 | { |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 237 | struct text_model *text_model = resource->data; |
| 238 | struct input_method *input_method, *next; |
| 239 | |
| 240 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 241 | if (!input_method->context) |
| 242 | continue; |
| 243 | input_method_context_send_content_type(&input_method->context->resource, hint, purpose); |
| 244 | } |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame] | 247 | static void |
| 248 | text_model_invoke_action(struct wl_client *client, |
| 249 | struct wl_resource *resource, |
| 250 | uint32_t button, |
| 251 | uint32_t index) |
| 252 | { |
| 253 | struct text_model *text_model = resource->data; |
| 254 | struct input_method *input_method, *next; |
| 255 | |
| 256 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 257 | if (!input_method->context) |
| 258 | continue; |
| 259 | input_method_context_send_invoke_action(&input_method->context->resource, button, index); |
| 260 | } |
| 261 | } |
| 262 | |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 263 | static void |
| 264 | text_model_commit(struct wl_client *client, |
| 265 | struct wl_resource *resource) |
| 266 | { |
| 267 | struct text_model *text_model = resource->data; |
| 268 | struct input_method *input_method, *next; |
| 269 | |
| 270 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 271 | if (!input_method->context) |
| 272 | continue; |
| 273 | input_method_context_send_commit(&input_method->context->resource); |
| 274 | } |
| 275 | } |
| 276 | |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 277 | static void |
| 278 | text_model_show_input_panel(struct wl_client *client, |
| 279 | struct wl_resource *resource) |
| 280 | { |
| 281 | struct text_model *text_model = resource->data; |
| 282 | struct weston_compositor *ec = text_model->ec; |
| 283 | |
| 284 | text_model->input_panel_visible = 1; |
| 285 | |
| 286 | if (!wl_list_empty(&text_model->input_methods)) |
| 287 | wl_signal_emit(&ec->show_input_panel_signal, ec); |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | text_model_hide_input_panel(struct wl_client *client, |
| 292 | struct wl_resource *resource) |
| 293 | { |
| 294 | struct text_model *text_model = resource->data; |
| 295 | struct weston_compositor *ec = text_model->ec; |
| 296 | |
| 297 | text_model->input_panel_visible = 0; |
| 298 | |
| 299 | if (!wl_list_empty(&text_model->input_methods)) |
| 300 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
| 301 | } |
| 302 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 303 | static const struct text_model_interface text_model_implementation = { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 304 | text_model_set_surrounding_text, |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 305 | text_model_activate, |
| 306 | text_model_deactivate, |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 307 | text_model_reset, |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 308 | text_model_set_micro_focus, |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame] | 309 | text_model_set_content_type, |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 310 | text_model_invoke_action, |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 311 | text_model_commit, |
| 312 | text_model_show_input_panel, |
| 313 | text_model_hide_input_panel |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 314 | }; |
| 315 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 316 | static void text_model_factory_create_text_model(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 317 | struct wl_resource *resource, |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 318 | uint32_t id) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 319 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 320 | struct text_model_factory *text_model_factory = resource->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 321 | struct text_model *text_model; |
| 322 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 323 | text_model = calloc(1, sizeof *text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 324 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 325 | text_model->resource.object.id = id; |
| 326 | text_model->resource.object.interface = &text_model_interface; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 327 | text_model->resource.object.implementation = |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 328 | (void (**)(void)) &text_model_implementation; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 329 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 330 | text_model->resource.data = text_model; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 331 | text_model->resource.destroy = destroy_text_model; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 332 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 333 | text_model->ec = text_model_factory->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 334 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 335 | wl_list_init(&text_model->input_methods); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 336 | |
| 337 | wl_client_add_resource(client, &text_model->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 338 | }; |
| 339 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 340 | static const struct text_model_factory_interface text_model_factory_implementation = { |
| 341 | text_model_factory_create_text_model |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | static void |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 345 | bind_text_model_factory(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 346 | void *data, |
| 347 | uint32_t version, |
| 348 | uint32_t id) |
| 349 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 350 | struct text_model_factory *text_model_factory = data; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 351 | |
| 352 | /* No checking for duplicate binding necessary. |
| 353 | * No events have to be sent, so we don't need the return value. */ |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 354 | wl_client_add_object(client, &text_model_factory_interface, |
| 355 | &text_model_factory_implementation, |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 356 | id, text_model_factory); |
| 357 | } |
| 358 | |
| 359 | static void |
| 360 | text_model_factory_notifier_destroy(struct wl_listener *listener, void *data) |
| 361 | { |
| 362 | struct text_model_factory *text_model_factory = |
| 363 | container_of(listener, struct text_model_factory, destroy_listener); |
| 364 | |
| 365 | wl_display_remove_global(text_model_factory->ec->wl_display, |
| 366 | text_model_factory->text_model_factory_global); |
| 367 | |
| 368 | free(text_model_factory); |
| 369 | } |
| 370 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 371 | static void |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 372 | text_model_factory_create(struct weston_compositor *ec) |
| 373 | { |
| 374 | struct text_model_factory *text_model_factory; |
| 375 | |
| 376 | text_model_factory = calloc(1, sizeof *text_model_factory); |
| 377 | |
| 378 | text_model_factory->ec = ec; |
| 379 | |
| 380 | text_model_factory->text_model_factory_global = |
| 381 | wl_display_add_global(ec->wl_display, |
| 382 | &text_model_factory_interface, |
| 383 | text_model_factory, bind_text_model_factory); |
| 384 | |
| 385 | text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy; |
| 386 | wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener); |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 389 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 390 | input_method_context_destroy(struct wl_client *client, |
| 391 | struct wl_resource *resource) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 392 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 393 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 396 | static void |
| 397 | input_method_context_commit_string(struct wl_client *client, |
| 398 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 399 | uint32_t serial, |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame^] | 400 | const char *text) |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 401 | { |
| 402 | struct input_method_context *context = resource->data; |
| 403 | |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame^] | 404 | text_model_send_commit_string(&context->model->resource, serial, text); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 407 | static void |
| 408 | input_method_context_preedit_string(struct wl_client *client, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 409 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 410 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 411 | const char *text, |
| 412 | const char *commit) |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 413 | { |
| 414 | struct input_method_context *context = resource->data; |
| 415 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 416 | text_model_send_preedit_string(&context->model->resource, serial, text, commit); |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static void |
| 420 | input_method_context_preedit_styling(struct wl_client *client, |
| 421 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 422 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 423 | uint32_t index, |
| 424 | uint32_t length, |
| 425 | uint32_t style) |
| 426 | { |
| 427 | struct input_method_context *context = resource->data; |
| 428 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 429 | text_model_send_preedit_styling(&context->model->resource, serial, index, length, style); |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static void |
| 433 | input_method_context_preedit_cursor(struct wl_client *client, |
| 434 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 435 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 436 | int32_t cursor) |
| 437 | { |
| 438 | struct input_method_context *context = resource->data; |
| 439 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 440 | text_model_send_preedit_cursor(&context->model->resource, serial, cursor); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 443 | static void |
| 444 | input_method_context_delete_surrounding_text(struct wl_client *client, |
| 445 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 446 | uint32_t serial, |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 447 | int32_t index, |
| 448 | uint32_t length) |
| 449 | { |
| 450 | struct input_method_context *context = resource->data; |
| 451 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 452 | text_model_send_delete_surrounding_text(&context->model->resource, serial, index, length); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 455 | static void |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame^] | 456 | input_method_context_cursor_position(struct wl_client *client, |
| 457 | struct wl_resource *resource, |
| 458 | uint32_t serial, |
| 459 | int32_t index, |
| 460 | int32_t anchor) |
| 461 | { |
| 462 | struct input_method_context *context = resource->data; |
| 463 | |
| 464 | text_model_send_cursor_position(&context->model->resource, serial, index, anchor); |
| 465 | } |
| 466 | |
| 467 | static void |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 468 | input_method_context_modifiers_map(struct wl_client *client, |
| 469 | struct wl_resource *resource, |
| 470 | struct wl_array *map) |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 471 | { |
| 472 | struct input_method_context *context = resource->data; |
| 473 | |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 474 | text_model_send_modifiers_map(&context->model->resource, map); |
| 475 | } |
| 476 | |
| 477 | static void |
| 478 | input_method_context_keysym(struct wl_client *client, |
| 479 | struct wl_resource *resource, |
| 480 | uint32_t serial, |
| 481 | uint32_t time, |
| 482 | uint32_t sym, |
| 483 | uint32_t state, |
| 484 | uint32_t modifiers) |
| 485 | { |
| 486 | struct input_method_context *context = resource->data; |
| 487 | |
| 488 | text_model_send_keysym(&context->model->resource, serial, time, |
| 489 | sym, state, modifiers); |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 492 | static void |
| 493 | unbind_keyboard(struct wl_resource *resource) |
| 494 | { |
| 495 | struct input_method_context *context = resource->data; |
| 496 | |
| 497 | input_method_context_end_keyboard_grab(context); |
| 498 | context->keyboard = NULL; |
| 499 | |
| 500 | free(resource); |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | input_method_context_grab_key(struct wl_keyboard_grab *grab, |
| 505 | uint32_t time, uint32_t key, uint32_t state_w) |
| 506 | { |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 507 | struct weston_keyboard *keyboard = (struct weston_keyboard *)grab->keyboard; |
| 508 | struct wl_display *display; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 509 | uint32_t serial; |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 510 | |
| 511 | if (!keyboard->input_method_resource) |
| 512 | return; |
| 513 | |
| 514 | display = wl_client_get_display(keyboard->input_method_resource->client); |
| 515 | serial = wl_display_next_serial(display); |
| 516 | wl_keyboard_send_key(keyboard->input_method_resource, |
| 517 | serial, time, key, state_w); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void |
| 521 | input_method_context_grab_modifier(struct wl_keyboard_grab *grab, uint32_t serial, |
| 522 | uint32_t mods_depressed, uint32_t mods_latched, |
| 523 | uint32_t mods_locked, uint32_t group) |
| 524 | { |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 525 | struct weston_keyboard *keyboard = (struct weston_keyboard *)grab->keyboard; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 526 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 527 | if (!keyboard->input_method_resource) |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 528 | return; |
| 529 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 530 | wl_keyboard_send_modifiers(keyboard->input_method_resource, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 531 | serial, mods_depressed, mods_latched, |
| 532 | mods_locked, group); |
| 533 | } |
| 534 | |
| 535 | static const struct wl_keyboard_grab_interface input_method_context_grab = { |
| 536 | input_method_context_grab_key, |
| 537 | input_method_context_grab_modifier, |
| 538 | }; |
| 539 | |
| 540 | static void |
| 541 | input_method_context_grab_keyboard(struct wl_client *client, |
| 542 | struct wl_resource *resource, |
| 543 | uint32_t id) |
| 544 | { |
| 545 | struct input_method_context *context = resource->data; |
| 546 | struct wl_resource *cr; |
| 547 | struct weston_seat *seat = context->input_method->seat; |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 548 | struct weston_keyboard *keyboard = &seat->keyboard; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 549 | |
| 550 | cr = wl_client_add_object(client, &wl_keyboard_interface, |
| 551 | NULL, id, context); |
| 552 | cr->destroy = unbind_keyboard; |
| 553 | |
| 554 | context->keyboard = cr; |
| 555 | |
| 556 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 557 | seat->xkb_info.keymap_fd, |
| 558 | seat->xkb_info.keymap_size); |
| 559 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 560 | if (keyboard->keyboard.grab != &keyboard->keyboard.default_grab) { |
| 561 | wl_keyboard_end_grab(&keyboard->keyboard); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 562 | } |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 563 | wl_keyboard_start_grab(&keyboard->keyboard, &keyboard->input_method_grab); |
| 564 | keyboard->input_method_resource = cr; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 567 | static void |
| 568 | input_method_context_key(struct wl_client *client, |
| 569 | struct wl_resource *resource, |
| 570 | uint32_t serial, |
| 571 | uint32_t time, |
| 572 | uint32_t key, |
| 573 | uint32_t state_w) |
| 574 | { |
| 575 | struct input_method_context *context = resource->data; |
| 576 | struct weston_seat *seat = context->input_method->seat; |
| 577 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 578 | struct wl_keyboard_grab *default_grab = &keyboard->default_grab; |
| 579 | |
| 580 | default_grab->interface->key(default_grab, time, key, state_w); |
| 581 | } |
| 582 | |
| 583 | static void |
| 584 | input_method_context_modifiers(struct wl_client *client, |
| 585 | struct wl_resource *resource, |
| 586 | uint32_t serial, |
| 587 | uint32_t mods_depressed, |
| 588 | uint32_t mods_latched, |
| 589 | uint32_t mods_locked, |
| 590 | uint32_t group) |
| 591 | { |
| 592 | struct input_method_context *context = resource->data; |
| 593 | |
| 594 | struct weston_seat *seat = context->input_method->seat; |
| 595 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 596 | struct wl_keyboard_grab *default_grab = &keyboard->default_grab; |
| 597 | |
| 598 | default_grab->interface->modifiers(default_grab, |
| 599 | serial, mods_depressed, |
| 600 | mods_latched, mods_locked, |
| 601 | group); |
| 602 | } |
| 603 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 604 | static const struct input_method_context_interface input_method_context_implementation = { |
| 605 | input_method_context_destroy, |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 606 | input_method_context_commit_string, |
| 607 | input_method_context_preedit_string, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 608 | input_method_context_preedit_styling, |
| 609 | input_method_context_preedit_cursor, |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 610 | input_method_context_delete_surrounding_text, |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame^] | 611 | input_method_context_cursor_position, |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 612 | input_method_context_modifiers_map, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 613 | input_method_context_keysym, |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 614 | input_method_context_grab_keyboard, |
| 615 | input_method_context_key, |
| 616 | input_method_context_modifiers |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 617 | }; |
| 618 | |
| 619 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 620 | destroy_input_method_context(struct wl_resource *resource) |
| 621 | { |
| 622 | struct input_method_context *context = resource->data; |
| 623 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 624 | if (context->keyboard) { |
| 625 | wl_resource_destroy(context->keyboard); |
| 626 | } |
| 627 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 628 | free(context); |
| 629 | } |
| 630 | |
| 631 | static void |
| 632 | input_method_context_create(struct text_model *model, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 633 | struct input_method *input_method, |
| 634 | uint32_t serial) |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 635 | { |
| 636 | struct input_method_context *context; |
| 637 | |
| 638 | if (!input_method->input_method_binding) |
| 639 | return; |
| 640 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 641 | context = calloc(1, sizeof *context); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 642 | if (context == NULL) |
| 643 | return; |
| 644 | |
| 645 | context->resource.destroy = destroy_input_method_context; |
| 646 | context->resource.object.id = 0; |
| 647 | context->resource.object.interface = &input_method_context_interface; |
| 648 | context->resource.object.implementation = |
| 649 | (void (**)(void)) &input_method_context_implementation; |
| 650 | context->resource.data = context; |
| 651 | wl_signal_init(&context->resource.destroy_signal); |
| 652 | |
| 653 | context->model = model; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 654 | context->input_method = input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 655 | input_method->context = context; |
| 656 | |
| 657 | wl_client_add_resource(input_method->input_method_binding->client, &context->resource); |
| 658 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 659 | input_method_send_activate(input_method->input_method_binding, &context->resource, serial); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 663 | input_method_context_end_keyboard_grab(struct input_method_context *context) |
| 664 | { |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 665 | struct wl_keyboard_grab *grab = &context->input_method->seat->keyboard.input_method_grab; |
| 666 | struct weston_keyboard *keyboard = (struct weston_keyboard *)grab->keyboard; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 667 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 668 | if (!grab->keyboard) |
| 669 | return; |
| 670 | |
| 671 | if (grab->keyboard->grab == grab) |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 672 | wl_keyboard_end_grab(grab->keyboard); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 673 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 674 | keyboard->input_method_resource = NULL; |
| 675 | } |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 676 | |
| 677 | static void |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 678 | unbind_input_method(struct wl_resource *resource) |
| 679 | { |
| 680 | struct input_method *input_method = resource->data; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 681 | struct text_backend *text_backend = input_method->text_backend; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 682 | |
| 683 | input_method->input_method_binding = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 684 | input_method->context = NULL; |
| 685 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 686 | text_backend->input_method.binding = NULL; |
| 687 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 688 | free(resource); |
| 689 | } |
| 690 | |
| 691 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 692 | bind_input_method(struct wl_client *client, |
| 693 | void *data, |
| 694 | uint32_t version, |
| 695 | uint32_t id) |
| 696 | { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 697 | struct input_method *input_method = data; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 698 | struct text_backend *text_backend = input_method->text_backend; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 699 | struct wl_resource *resource; |
| 700 | |
| 701 | resource = wl_client_add_object(client, &input_method_interface, |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 702 | NULL, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 703 | id, input_method); |
| 704 | |
| 705 | if (input_method->input_method_binding == NULL) { |
| 706 | resource->destroy = unbind_input_method; |
| 707 | input_method->input_method_binding = resource; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 708 | |
| 709 | text_backend->input_method.binding = resource; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 710 | return; |
| 711 | } |
| 712 | |
| 713 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 714 | "interface object already bound"); |
| 715 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static void |
| 719 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 720 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 721 | struct input_method *input_method = |
| 722 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 723 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 724 | if (input_method->model) |
| 725 | deactivate_text_model(input_method->model, input_method); |
| 726 | |
| 727 | wl_display_remove_global(input_method->seat->compositor->wl_display, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 728 | input_method->input_method_global); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 729 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 730 | free(input_method); |
| 731 | } |
| 732 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 733 | static void |
| 734 | handle_keyboard_focus(struct wl_listener *listener, void *data) |
| 735 | { |
| 736 | struct wl_keyboard *keyboard = data; |
| 737 | struct input_method *input_method = |
| 738 | container_of(listener, struct input_method, keyboard_focus_listener); |
| 739 | struct wl_surface *surface = keyboard->focus; |
| 740 | |
| 741 | if (!input_method->model) |
| 742 | return; |
| 743 | |
| 744 | if (!surface || input_method->model->surface != surface) |
| 745 | deactivate_text_model(input_method->model, |
| 746 | input_method); |
| 747 | } |
| 748 | |
| 749 | static void |
| 750 | input_method_init_seat(struct weston_seat *seat) |
| 751 | { |
| 752 | if (seat->input_method->focus_listener_initialized) |
| 753 | return; |
| 754 | |
| 755 | if (seat->has_keyboard) { |
| 756 | seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus; |
| 757 | wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener); |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 758 | seat->keyboard.input_method_grab.interface = &input_method_context_grab; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | seat->input_method->focus_listener_initialized = 1; |
| 762 | } |
| 763 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 764 | static void |
| 765 | handle_input_method_sigchld(struct weston_process *process, int status) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 766 | { |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 767 | struct text_backend *text_backend = |
| 768 | container_of(process, struct text_backend, input_method.process); |
| 769 | |
| 770 | text_backend->input_method.process.pid = 0; |
| 771 | text_backend->input_method.client = NULL; |
| 772 | } |
| 773 | |
| 774 | static void |
| 775 | launch_input_method(struct text_backend *text_backend) |
| 776 | { |
| 777 | if (text_backend->input_method.binding) |
| 778 | return; |
| 779 | |
| 780 | if (!text_backend->input_method.path) |
| 781 | return; |
| 782 | |
| 783 | if (text_backend->input_method.process.pid != 0) |
| 784 | return; |
| 785 | |
| 786 | text_backend->input_method.client = weston_client_launch(text_backend->compositor, |
| 787 | &text_backend->input_method.process, |
| 788 | text_backend->input_method.path, |
| 789 | handle_input_method_sigchld); |
| 790 | |
| 791 | if (!text_backend->input_method.client) |
| 792 | weston_log("not able to start %s\n", text_backend->input_method.path); |
| 793 | } |
| 794 | |
| 795 | static void |
| 796 | handle_seat_created(struct wl_listener *listener, |
| 797 | void *data) |
| 798 | { |
| 799 | struct weston_seat *seat = data; |
| 800 | struct text_backend *text_backend = |
| 801 | container_of(listener, struct text_backend, |
| 802 | seat_created_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 803 | struct input_method *input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 804 | struct weston_compositor *ec = seat->compositor; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 805 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 806 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 807 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 808 | input_method->seat = seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 809 | input_method->model = NULL; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 810 | input_method->focus_listener_initialized = 0; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 811 | input_method->context = NULL; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 812 | input_method->text_backend = text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 813 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 814 | input_method->input_method_global = |
| 815 | wl_display_add_global(ec->wl_display, |
| 816 | &input_method_interface, |
| 817 | input_method, bind_input_method); |
| 818 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 819 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 820 | wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 821 | |
| 822 | seat->input_method = input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 823 | |
| 824 | launch_input_method(text_backend); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 825 | } |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 826 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 827 | static void |
| 828 | text_backend_configuration(struct text_backend *text_backend) |
| 829 | { |
| 830 | char *config_file; |
| 831 | char *path = NULL; |
| 832 | |
| 833 | struct config_key input_method_keys[] = { |
| 834 | { "path", CONFIG_KEY_STRING, &path } |
| 835 | }; |
| 836 | |
| 837 | struct config_section cs[] = { |
| 838 | { "input-method", input_method_keys, ARRAY_LENGTH(input_method_keys), NULL } |
| 839 | }; |
| 840 | |
| 841 | config_file = config_file_path("weston.ini"); |
| 842 | parse_config_file(config_file, cs, ARRAY_LENGTH(cs), text_backend); |
| 843 | free(config_file); |
| 844 | |
| 845 | if (path) |
| 846 | text_backend->input_method.path = path; |
| 847 | else |
| 848 | text_backend->input_method.path = strdup(LIBEXECDIR "/weston-keyboard"); |
| 849 | } |
| 850 | |
| 851 | static void |
| 852 | text_backend_notifier_destroy(struct wl_listener *listener, void *data) |
| 853 | { |
| 854 | struct text_backend *text_backend = |
| 855 | container_of(listener, struct text_backend, destroy_listener); |
| 856 | |
| 857 | if (text_backend->input_method.client) |
| 858 | wl_client_destroy(text_backend->input_method.client); |
| 859 | |
| 860 | free(text_backend->input_method.path); |
| 861 | |
| 862 | free(text_backend); |
| 863 | } |
| 864 | |
| 865 | |
| 866 | WL_EXPORT int |
| 867 | text_backend_init(struct weston_compositor *ec) |
| 868 | { |
| 869 | struct text_backend *text_backend; |
| 870 | |
| 871 | text_backend = calloc(1, sizeof(*text_backend)); |
| 872 | |
| 873 | text_backend->compositor = ec; |
| 874 | |
| 875 | text_backend->seat_created_listener.notify = handle_seat_created; |
| 876 | wl_signal_add(&ec->seat_created_signal, |
| 877 | &text_backend->seat_created_listener); |
| 878 | |
| 879 | text_backend->destroy_listener.notify = text_backend_notifier_destroy; |
| 880 | wl_signal_add(&ec->destroy_signal, &text_backend->destroy_listener); |
| 881 | |
| 882 | text_backend_configuration(text_backend); |
| 883 | |
| 884 | text_model_factory_create(ec); |
| 885 | |
| 886 | return 0; |
| 887 | } |