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