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