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