Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Openismus GmbH |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include "compositor.h" |
| 26 | #include "text-server-protocol.h" |
| 27 | |
| 28 | struct input_method; |
| 29 | |
| 30 | struct text_model { |
| 31 | struct wl_resource resource; |
| 32 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 33 | struct weston_compositor *ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 34 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 35 | struct wl_list input_methods; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct input_method { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 39 | struct wl_resource *input_method_binding; |
| 40 | struct wl_global *input_method_global; |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 41 | struct wl_global *text_model_factory_global; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 42 | struct wl_listener destroy_listener; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 43 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 44 | struct weston_compositor *ec; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 45 | struct text_model *model; |
| 46 | |
| 47 | struct wl_list link; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 51 | deactivate_text_model(struct text_model *text_model, |
| 52 | struct input_method *input_method) |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 53 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 54 | struct weston_compositor *ec = text_model->ec; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 55 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 56 | if (input_method->model == text_model) { |
| 57 | wl_list_remove(&input_method->link); |
| 58 | input_method->model = NULL; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 59 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 60 | text_model_send_deactivated(&text_model->resource); |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 65 | destroy_text_model(struct wl_resource *resource) |
| 66 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 67 | struct text_model *text_model = |
| 68 | container_of(resource, struct text_model, resource); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 69 | struct input_method *input_method, *next; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 70 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 71 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) |
| 72 | deactivate_text_model(text_model, input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 73 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 74 | free(text_model); |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | text_model_set_surrounding_text(struct wl_client *client, |
| 79 | struct wl_resource *resource, |
| 80 | const char *text) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | text_model_set_cursor_index(struct wl_client *client, |
| 86 | struct wl_resource *resource, |
| 87 | uint32_t index) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | text_model_activate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 93 | struct wl_resource *resource, |
| 94 | struct wl_resource *seat, |
| 95 | struct wl_resource *surface) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 96 | { |
| 97 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 98 | struct weston_seat *weston_seat = seat->data; |
| 99 | struct text_model *old = weston_seat->input_method->model; |
| 100 | struct weston_compositor *ec = text_model->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 101 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 102 | if (old == text_model) |
| 103 | return; |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 104 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 105 | if (old) { |
| 106 | deactivate_text_model(old, |
| 107 | weston_seat->input_method); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 110 | weston_seat->input_method->model = text_model; |
| 111 | wl_list_insert(&text_model->input_methods, &weston_seat->input_method->link); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 112 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 113 | wl_signal_emit(&ec->show_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 114 | |
| 115 | text_model_send_activated(&text_model->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void |
| 119 | text_model_deactivate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 120 | struct wl_resource *resource, |
| 121 | struct wl_resource *seat) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 122 | { |
| 123 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 124 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 125 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 126 | deactivate_text_model(text_model, |
| 127 | weston_seat->input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void |
| 131 | text_model_set_selected_text(struct wl_client *client, |
| 132 | struct wl_resource *resource, |
| 133 | const char *text, |
| 134 | int32_t index) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | text_model_set_micro_focus(struct wl_client *client, |
| 140 | struct wl_resource *resource, |
| 141 | int32_t x, |
| 142 | int32_t y, |
| 143 | int32_t width, |
| 144 | int32_t height) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | text_model_set_preedit(struct wl_client *client, |
| 150 | struct wl_resource *resource) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | text_model_set_content_type(struct wl_client *client, |
| 156 | struct wl_resource *resource) |
| 157 | { |
| 158 | } |
| 159 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 160 | static const struct text_model_interface text_model_implementation = { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 161 | text_model_set_surrounding_text, |
| 162 | text_model_set_cursor_index, |
| 163 | text_model_activate, |
| 164 | text_model_deactivate, |
| 165 | text_model_set_selected_text, |
| 166 | text_model_set_micro_focus, |
| 167 | text_model_set_preedit, |
| 168 | text_model_set_content_type |
| 169 | }; |
| 170 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 171 | 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] | 172 | struct wl_resource *resource, |
| 173 | uint32_t id, |
| 174 | struct wl_resource *surface) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 175 | { |
| 176 | struct input_method *input_method = resource->data; |
| 177 | struct text_model *text_model; |
| 178 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 179 | text_model = calloc(1, sizeof *text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 180 | |
| 181 | text_model->resource.destroy = destroy_text_model; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 182 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 183 | text_model->resource.object.id = id; |
| 184 | text_model->resource.object.interface = &text_model_interface; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 185 | text_model->resource.object.implementation = |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 186 | (void (**)(void)) &text_model_implementation; |
| 187 | text_model->resource.data = text_model; |
| 188 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 189 | text_model->ec = input_method->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 190 | |
| 191 | wl_client_add_resource(client, &text_model->resource); |
| 192 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 193 | wl_list_init(&text_model->input_methods); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 194 | }; |
| 195 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 196 | static const struct text_model_factory_interface text_model_factory_implementation = { |
| 197 | text_model_factory_create_text_model |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static void |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 201 | bind_text_model_factory(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 202 | void *data, |
| 203 | uint32_t version, |
| 204 | uint32_t id) |
| 205 | { |
| 206 | struct input_method *input_method = data; |
| 207 | |
| 208 | /* No checking for duplicate binding necessary. |
| 209 | * 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] | 210 | wl_client_add_object(client, &text_model_factory_interface, |
| 211 | &text_model_factory_implementation, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 212 | id, input_method); |
| 213 | } |
| 214 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 215 | static void |
| 216 | input_method_commit_string(struct wl_client *client, |
| 217 | struct wl_resource *resource, |
| 218 | const char *text, |
| 219 | uint32_t index) |
| 220 | { |
| 221 | struct input_method *input_method = resource->data; |
| 222 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 223 | if (input_method->model) { |
| 224 | text_model_send_commit_string(&input_method->model->resource, text, index); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 228 | static const struct input_method_interface input_method_implementation = { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 229 | input_method_commit_string |
| 230 | }; |
| 231 | |
| 232 | static void |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 233 | unbind_input_method(struct wl_resource *resource) |
| 234 | { |
| 235 | struct input_method *input_method = resource->data; |
| 236 | |
| 237 | input_method->input_method_binding = NULL; |
| 238 | free(resource); |
| 239 | } |
| 240 | |
| 241 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 242 | bind_input_method(struct wl_client *client, |
| 243 | void *data, |
| 244 | uint32_t version, |
| 245 | uint32_t id) |
| 246 | { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 247 | struct input_method *input_method = data; |
| 248 | struct wl_resource *resource; |
| 249 | |
| 250 | resource = wl_client_add_object(client, &input_method_interface, |
| 251 | &input_method_implementation, |
| 252 | id, input_method); |
| 253 | |
| 254 | if (input_method->input_method_binding == NULL) { |
| 255 | resource->destroy = unbind_input_method; |
| 256 | input_method->input_method_binding = resource; |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 261 | "interface object already bound"); |
| 262 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static void |
| 266 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 267 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 268 | struct input_method *input_method = |
| 269 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 270 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 271 | wl_display_remove_global(input_method->ec->wl_display, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 272 | input_method->input_method_global); |
| 273 | wl_display_remove_global(input_method->ec->wl_display, |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 274 | input_method->text_model_factory_global); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 275 | free(input_method); |
| 276 | } |
| 277 | |
| 278 | void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 279 | input_method_create(struct weston_compositor *ec, |
| 280 | struct weston_seat *seat) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 281 | { |
| 282 | struct input_method *input_method; |
| 283 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 284 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 285 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 286 | input_method->ec = ec; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 287 | input_method->model = NULL; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 288 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 289 | input_method->input_method_global = |
| 290 | wl_display_add_global(ec->wl_display, |
| 291 | &input_method_interface, |
| 292 | input_method, bind_input_method); |
| 293 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 294 | input_method->text_model_factory_global = |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 295 | wl_display_add_global(ec->wl_display, |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 296 | &text_model_factory_interface, |
| 297 | input_method, bind_text_model_factory); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 298 | |
| 299 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 300 | wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame^] | 301 | |
| 302 | seat->input_method = input_method; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 303 | } |