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