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 | |
| 33 | struct wl_list link; |
| 34 | |
| 35 | struct input_method *input_method; |
| 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; |
| 45 | struct wl_list models; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 46 | struct text_model *active_model; |
| 47 | }; |
| 48 | |
| 49 | static void |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 50 | deactivate_text_model(struct text_model *text_model) |
| 51 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 52 | struct weston_compositor *ec = text_model->input_method->ec; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 53 | |
| 54 | if (text_model->input_method->active_model == text_model) { |
| 55 | text_model->input_method->active_model = NULL; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 56 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame^] | 57 | text_model_send_deactivated(&text_model->resource); |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 62 | destroy_text_model(struct wl_resource *resource) |
| 63 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 64 | struct text_model *text_model = |
| 65 | container_of(resource, struct text_model, resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 66 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 67 | deactivate_text_model(text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 68 | |
| 69 | wl_list_remove(&text_model->link); |
| 70 | free(text_model); |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | text_model_set_surrounding_text(struct wl_client *client, |
| 75 | struct wl_resource *resource, |
| 76 | const char *text) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | text_model_set_cursor_index(struct wl_client *client, |
| 82 | struct wl_resource *resource, |
| 83 | uint32_t index) |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | text_model_activate(struct wl_client *client, |
| 89 | struct wl_resource *resource) |
| 90 | { |
| 91 | struct text_model *text_model = resource->data; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 92 | struct weston_compositor *ec = text_model->input_method->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 93 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame^] | 94 | if (text_model->input_method->active_model) { |
| 95 | if (text_model->input_method->active_model == text_model) |
| 96 | return; |
| 97 | |
| 98 | deactivate_text_model(text_model->input_method->active_model); |
| 99 | } |
| 100 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 101 | text_model->input_method->active_model = text_model; |
| 102 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 103 | wl_signal_emit(&ec->show_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame^] | 104 | |
| 105 | text_model_send_activated(&text_model->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void |
| 109 | text_model_deactivate(struct wl_client *client, |
| 110 | struct wl_resource *resource) |
| 111 | { |
| 112 | struct text_model *text_model = resource->data; |
| 113 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 114 | deactivate_text_model(text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void |
| 118 | text_model_set_selected_text(struct wl_client *client, |
| 119 | struct wl_resource *resource, |
| 120 | const char *text, |
| 121 | int32_t index) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | text_model_set_micro_focus(struct wl_client *client, |
| 127 | struct wl_resource *resource, |
| 128 | int32_t x, |
| 129 | int32_t y, |
| 130 | int32_t width, |
| 131 | int32_t height) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | text_model_set_preedit(struct wl_client *client, |
| 137 | struct wl_resource *resource) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | text_model_set_content_type(struct wl_client *client, |
| 143 | struct wl_resource *resource) |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | struct text_model_interface text_model_implementation = { |
| 148 | text_model_set_surrounding_text, |
| 149 | text_model_set_cursor_index, |
| 150 | text_model_activate, |
| 151 | text_model_deactivate, |
| 152 | text_model_set_selected_text, |
| 153 | text_model_set_micro_focus, |
| 154 | text_model_set_preedit, |
| 155 | text_model_set_content_type |
| 156 | }; |
| 157 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 158 | 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] | 159 | struct wl_resource *resource, |
| 160 | uint32_t id, |
| 161 | struct wl_resource *surface) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 162 | { |
| 163 | struct input_method *input_method = resource->data; |
| 164 | struct text_model *text_model; |
| 165 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 166 | text_model = calloc(1, sizeof *text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 167 | |
| 168 | text_model->resource.destroy = destroy_text_model; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 169 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 170 | text_model->resource.object.id = id; |
| 171 | text_model->resource.object.interface = &text_model_interface; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 172 | text_model->resource.object.implementation = |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 173 | (void (**)(void)) &text_model_implementation; |
| 174 | text_model->resource.data = text_model; |
| 175 | |
| 176 | text_model->input_method = input_method; |
| 177 | |
| 178 | wl_client_add_resource(client, &text_model->resource); |
| 179 | |
| 180 | wl_list_insert(&input_method->models, &text_model->link); |
| 181 | }; |
| 182 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 183 | static const struct text_model_factory_interface text_model_factory_implementation = { |
| 184 | text_model_factory_create_text_model |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | static void |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 188 | bind_text_model_factory(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 189 | void *data, |
| 190 | uint32_t version, |
| 191 | uint32_t id) |
| 192 | { |
| 193 | struct input_method *input_method = data; |
| 194 | |
| 195 | /* No checking for duplicate binding necessary. |
| 196 | * 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] | 197 | wl_client_add_object(client, &text_model_factory_interface, |
| 198 | &text_model_factory_implementation, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 199 | id, input_method); |
| 200 | } |
| 201 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 202 | static void |
| 203 | input_method_commit_string(struct wl_client *client, |
| 204 | struct wl_resource *resource, |
| 205 | const char *text, |
| 206 | uint32_t index) |
| 207 | { |
| 208 | struct input_method *input_method = resource->data; |
| 209 | |
| 210 | if (input_method->active_model) { |
| 211 | text_model_send_commit_string(&input_method->active_model->resource, text, index); |
| 212 | } |
| 213 | } |
| 214 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 215 | static const struct input_method_interface input_method_implementation = { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 216 | input_method_commit_string |
| 217 | }; |
| 218 | |
| 219 | static void |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 220 | unbind_input_method(struct wl_resource *resource) |
| 221 | { |
| 222 | struct input_method *input_method = resource->data; |
| 223 | |
| 224 | input_method->input_method_binding = NULL; |
| 225 | free(resource); |
| 226 | } |
| 227 | |
| 228 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 229 | bind_input_method(struct wl_client *client, |
| 230 | void *data, |
| 231 | uint32_t version, |
| 232 | uint32_t id) |
| 233 | { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 234 | struct input_method *input_method = data; |
| 235 | struct wl_resource *resource; |
| 236 | |
| 237 | resource = wl_client_add_object(client, &input_method_interface, |
| 238 | &input_method_implementation, |
| 239 | id, input_method); |
| 240 | |
| 241 | if (input_method->input_method_binding == NULL) { |
| 242 | resource->destroy = unbind_input_method; |
| 243 | input_method->input_method_binding = resource; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 248 | "interface object already bound"); |
| 249 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void |
| 253 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 254 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 255 | struct input_method *input_method = |
| 256 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 257 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 258 | wl_display_remove_global(input_method->ec->wl_display, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 259 | input_method->input_method_global); |
| 260 | wl_display_remove_global(input_method->ec->wl_display, |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 261 | input_method->text_model_factory_global); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 262 | free(input_method); |
| 263 | } |
| 264 | |
| 265 | void |
| 266 | input_method_create(struct weston_compositor *ec) |
| 267 | { |
| 268 | struct input_method *input_method; |
| 269 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 270 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 271 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 272 | input_method->ec = ec; |
| 273 | input_method->active_model = NULL; |
| 274 | |
| 275 | wl_list_init(&input_method->models); |
| 276 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 277 | input_method->input_method_global = |
| 278 | wl_display_add_global(ec->wl_display, |
| 279 | &input_method_interface, |
| 280 | input_method, bind_input_method); |
| 281 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 282 | input_method->text_model_factory_global = |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 283 | wl_display_add_global(ec->wl_display, |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 284 | &text_model_factory_interface, |
| 285 | input_method, bind_text_model_factory); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 286 | |
| 287 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 288 | wl_signal_add(&ec->destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 289 | } |