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> |
| 25 | |
| 26 | #include "compositor.h" |
| 27 | #include "text-server-protocol.h" |
| 28 | |
| 29 | struct input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 30 | struct input_method_context; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 31 | |
| 32 | struct text_model { |
| 33 | struct wl_resource resource; |
| 34 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 35 | struct weston_compositor *ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 36 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 37 | struct wl_list input_methods; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 38 | |
| 39 | struct wl_surface *surface; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 42 | struct text_model_factory { |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 43 | struct wl_global *text_model_factory_global; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 44 | struct wl_listener destroy_listener; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 45 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 46 | struct weston_compositor *ec; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct input_method { |
| 50 | struct wl_resource *input_method_binding; |
| 51 | struct wl_global *input_method_global; |
| 52 | struct wl_listener destroy_listener; |
| 53 | |
| 54 | struct weston_seat *seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 55 | struct text_model *model; |
| 56 | |
| 57 | struct wl_list link; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 58 | |
| 59 | struct wl_listener keyboard_focus_listener; |
| 60 | |
| 61 | int focus_listener_initialized; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 62 | |
| 63 | struct input_method_context *context; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 66 | struct input_method_context { |
| 67 | struct wl_resource resource; |
| 68 | |
| 69 | struct text_model *model; |
| 70 | |
| 71 | struct wl_list link; |
| 72 | }; |
| 73 | |
| 74 | static void input_method_context_create(struct text_model *model, |
| 75 | struct input_method *input_method); |
| 76 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 77 | static void input_method_init_seat(struct weston_seat *seat); |
| 78 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 79 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 80 | deactivate_text_model(struct text_model *text_model, |
| 81 | struct input_method *input_method) |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 82 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 83 | struct weston_compositor *ec = text_model->ec; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 84 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 85 | if (input_method->model == text_model) { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 86 | if (input_method->input_method_binding) |
| 87 | input_method_send_deactivate(input_method->input_method_binding, &input_method->context->resource); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 88 | wl_list_remove(&input_method->link); |
| 89 | input_method->model = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 90 | input_method->context = NULL; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 91 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 92 | text_model_send_deactivated(&text_model->resource); |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 97 | destroy_text_model(struct wl_resource *resource) |
| 98 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 99 | struct text_model *text_model = |
| 100 | container_of(resource, struct text_model, resource); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 101 | struct input_method *input_method, *next; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 102 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 103 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) |
| 104 | deactivate_text_model(text_model, input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 105 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 106 | free(text_model); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | text_model_set_surrounding_text(struct wl_client *client, |
| 111 | struct wl_resource *resource, |
| 112 | const char *text) |
| 113 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 114 | struct text_model *text_model = resource->data; |
| 115 | struct input_method *input_method, *next; |
| 116 | |
| 117 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 118 | if (!input_method->context) |
| 119 | continue; |
| 120 | input_method_context_send_set_surrounding_text(&input_method->context->resource, text); |
| 121 | } |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void |
| 125 | text_model_set_cursor_index(struct wl_client *client, |
| 126 | struct wl_resource *resource, |
| 127 | uint32_t index) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | text_model_activate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 133 | struct wl_resource *resource, |
| 134 | struct wl_resource *seat, |
| 135 | struct wl_resource *surface) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 136 | { |
| 137 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 138 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 139 | struct input_method *input_method = weston_seat->input_method; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 140 | struct text_model *old = weston_seat->input_method->model; |
| 141 | struct weston_compositor *ec = text_model->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 142 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 143 | if (old == text_model) |
| 144 | return; |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 145 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 146 | if (old) { |
| 147 | deactivate_text_model(old, |
| 148 | weston_seat->input_method); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 151 | input_method->model = text_model; |
| 152 | wl_list_insert(&text_model->input_methods, &input_method->link); |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 153 | input_method_init_seat(weston_seat); |
| 154 | |
| 155 | text_model->surface = surface->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 156 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 157 | input_method_context_create(text_model, input_method); |
| 158 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 159 | wl_signal_emit(&ec->show_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 160 | |
| 161 | text_model_send_activated(&text_model->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void |
| 165 | text_model_deactivate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 166 | struct wl_resource *resource, |
| 167 | struct wl_resource *seat) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 168 | { |
| 169 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 170 | struct weston_seat *weston_seat = seat->data; |
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 | deactivate_text_model(text_model, |
| 173 | weston_seat->input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void |
| 177 | text_model_set_selected_text(struct wl_client *client, |
| 178 | struct wl_resource *resource, |
| 179 | const char *text, |
| 180 | int32_t index) |
| 181 | { |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | text_model_set_micro_focus(struct wl_client *client, |
| 186 | struct wl_resource *resource, |
| 187 | int32_t x, |
| 188 | int32_t y, |
| 189 | int32_t width, |
| 190 | int32_t height) |
| 191 | { |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | text_model_set_preedit(struct wl_client *client, |
| 196 | struct wl_resource *resource) |
| 197 | { |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | text_model_set_content_type(struct wl_client *client, |
| 202 | struct wl_resource *resource) |
| 203 | { |
| 204 | } |
| 205 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 206 | static const struct text_model_interface text_model_implementation = { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 207 | text_model_set_surrounding_text, |
| 208 | text_model_set_cursor_index, |
| 209 | text_model_activate, |
| 210 | text_model_deactivate, |
| 211 | text_model_set_selected_text, |
| 212 | text_model_set_micro_focus, |
| 213 | text_model_set_preedit, |
| 214 | text_model_set_content_type |
| 215 | }; |
| 216 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 217 | 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] | 218 | struct wl_resource *resource, |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 219 | uint32_t id) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 220 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 221 | struct text_model_factory *text_model_factory = resource->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 222 | struct text_model *text_model; |
| 223 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 224 | text_model = calloc(1, sizeof *text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 225 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 226 | text_model->resource.object.id = id; |
| 227 | text_model->resource.object.interface = &text_model_interface; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 228 | text_model->resource.object.implementation = |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 229 | (void (**)(void)) &text_model_implementation; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 230 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 231 | text_model->resource.data = text_model; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 232 | text_model->resource.destroy = destroy_text_model; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 233 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 234 | text_model->ec = text_model_factory->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 235 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 236 | wl_list_init(&text_model->input_methods); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 237 | |
| 238 | wl_client_add_resource(client, &text_model->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 239 | }; |
| 240 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 241 | static const struct text_model_factory_interface text_model_factory_implementation = { |
| 242 | text_model_factory_create_text_model |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | static void |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 246 | bind_text_model_factory(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 247 | void *data, |
| 248 | uint32_t version, |
| 249 | uint32_t id) |
| 250 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 251 | struct text_model_factory *text_model_factory = data; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 252 | |
| 253 | /* No checking for duplicate binding necessary. |
| 254 | * 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] | 255 | wl_client_add_object(client, &text_model_factory_interface, |
| 256 | &text_model_factory_implementation, |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 257 | id, text_model_factory); |
| 258 | } |
| 259 | |
| 260 | static void |
| 261 | text_model_factory_notifier_destroy(struct wl_listener *listener, void *data) |
| 262 | { |
| 263 | struct text_model_factory *text_model_factory = |
| 264 | container_of(listener, struct text_model_factory, destroy_listener); |
| 265 | |
| 266 | wl_display_remove_global(text_model_factory->ec->wl_display, |
| 267 | text_model_factory->text_model_factory_global); |
| 268 | |
| 269 | free(text_model_factory); |
| 270 | } |
| 271 | |
| 272 | WL_EXPORT void |
| 273 | text_model_factory_create(struct weston_compositor *ec) |
| 274 | { |
| 275 | struct text_model_factory *text_model_factory; |
| 276 | |
| 277 | text_model_factory = calloc(1, sizeof *text_model_factory); |
| 278 | |
| 279 | text_model_factory->ec = ec; |
| 280 | |
| 281 | text_model_factory->text_model_factory_global = |
| 282 | wl_display_add_global(ec->wl_display, |
| 283 | &text_model_factory_interface, |
| 284 | text_model_factory, bind_text_model_factory); |
| 285 | |
| 286 | text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy; |
| 287 | wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener); |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 290 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 291 | input_method_context_destroy(struct wl_client *client, |
| 292 | struct wl_resource *resource) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 293 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 294 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 297 | static void |
| 298 | input_method_context_commit_string(struct wl_client *client, |
| 299 | struct wl_resource *resource, |
| 300 | const char *text, |
| 301 | uint32_t index) |
| 302 | { |
| 303 | struct input_method_context *context = resource->data; |
| 304 | |
| 305 | text_model_send_commit_string(&context->model->resource, text, index); |
| 306 | } |
| 307 | |
| 308 | static const struct input_method_context_interface input_method_context_implementation = { |
| 309 | input_method_context_destroy, |
| 310 | input_method_context_commit_string |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 314 | destroy_input_method_context(struct wl_resource *resource) |
| 315 | { |
| 316 | struct input_method_context *context = resource->data; |
| 317 | |
| 318 | free(context); |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | input_method_context_create(struct text_model *model, |
| 323 | struct input_method *input_method) |
| 324 | { |
| 325 | struct input_method_context *context; |
| 326 | |
| 327 | if (!input_method->input_method_binding) |
| 328 | return; |
| 329 | |
| 330 | context = malloc(sizeof *context); |
| 331 | if (context == NULL) |
| 332 | return; |
| 333 | |
| 334 | context->resource.destroy = destroy_input_method_context; |
| 335 | context->resource.object.id = 0; |
| 336 | context->resource.object.interface = &input_method_context_interface; |
| 337 | context->resource.object.implementation = |
| 338 | (void (**)(void)) &input_method_context_implementation; |
| 339 | context->resource.data = context; |
| 340 | wl_signal_init(&context->resource.destroy_signal); |
| 341 | |
| 342 | context->model = model; |
| 343 | input_method->context = context; |
| 344 | |
| 345 | wl_client_add_resource(input_method->input_method_binding->client, &context->resource); |
| 346 | |
| 347 | input_method_send_activate(input_method->input_method_binding, &context->resource); |
| 348 | } |
| 349 | |
| 350 | static void |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 351 | unbind_input_method(struct wl_resource *resource) |
| 352 | { |
| 353 | struct input_method *input_method = resource->data; |
| 354 | |
| 355 | input_method->input_method_binding = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 356 | input_method->context = NULL; |
| 357 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 358 | free(resource); |
| 359 | } |
| 360 | |
| 361 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 362 | bind_input_method(struct wl_client *client, |
| 363 | void *data, |
| 364 | uint32_t version, |
| 365 | uint32_t id) |
| 366 | { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 367 | struct input_method *input_method = data; |
| 368 | struct wl_resource *resource; |
| 369 | |
| 370 | resource = wl_client_add_object(client, &input_method_interface, |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 371 | NULL, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 372 | id, input_method); |
| 373 | |
| 374 | if (input_method->input_method_binding == NULL) { |
| 375 | resource->destroy = unbind_input_method; |
| 376 | input_method->input_method_binding = resource; |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 381 | "interface object already bound"); |
| 382 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void |
| 386 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 387 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 388 | struct input_method *input_method = |
| 389 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 390 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 391 | if (input_method->model) |
| 392 | deactivate_text_model(input_method->model, input_method); |
| 393 | |
| 394 | wl_display_remove_global(input_method->seat->compositor->wl_display, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 395 | input_method->input_method_global); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 396 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 397 | free(input_method); |
| 398 | } |
| 399 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 400 | static void |
| 401 | handle_keyboard_focus(struct wl_listener *listener, void *data) |
| 402 | { |
| 403 | struct wl_keyboard *keyboard = data; |
| 404 | struct input_method *input_method = |
| 405 | container_of(listener, struct input_method, keyboard_focus_listener); |
| 406 | struct wl_surface *surface = keyboard->focus; |
| 407 | |
| 408 | if (!input_method->model) |
| 409 | return; |
| 410 | |
| 411 | if (!surface || input_method->model->surface != surface) |
| 412 | deactivate_text_model(input_method->model, |
| 413 | input_method); |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | input_method_init_seat(struct weston_seat *seat) |
| 418 | { |
| 419 | if (seat->input_method->focus_listener_initialized) |
| 420 | return; |
| 421 | |
| 422 | if (seat->has_keyboard) { |
| 423 | seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus; |
| 424 | wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener); |
| 425 | } |
| 426 | |
| 427 | seat->input_method->focus_listener_initialized = 1; |
| 428 | } |
| 429 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 430 | WL_EXPORT void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 431 | input_method_create(struct weston_compositor *ec, |
| 432 | struct weston_seat *seat) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 433 | { |
| 434 | struct input_method *input_method; |
| 435 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 436 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 437 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 438 | input_method->seat = seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 439 | input_method->model = NULL; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 440 | input_method->focus_listener_initialized = 0; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame^] | 441 | input_method->context = NULL; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 442 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 443 | input_method->input_method_global = |
| 444 | wl_display_add_global(ec->wl_display, |
| 445 | &input_method_interface, |
| 446 | input_method, bind_input_method); |
| 447 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 448 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 449 | wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 450 | |
| 451 | seat->input_method = input_method; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 452 | } |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 453 | |