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> |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 25 | #include <string.h> |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 26 | |
| 27 | #include "compositor.h" |
| 28 | #include "text-server-protocol.h" |
Jan Arne Petersen | 30b66ef | 2012-09-09 23:08:41 +0200 | [diff] [blame] | 29 | #include "input-method-server-protocol.h" |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 30 | |
| 31 | struct input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 32 | struct input_method_context; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 33 | struct text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 34 | |
| 35 | struct text_model { |
| 36 | struct wl_resource resource; |
| 37 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 38 | struct weston_compositor *ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 39 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 40 | struct wl_list input_methods; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 41 | |
| 42 | struct wl_surface *surface; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 45 | struct text_model_factory { |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 46 | struct wl_global *text_model_factory_global; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 47 | struct wl_listener destroy_listener; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 48 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 49 | struct weston_compositor *ec; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct input_method { |
| 53 | struct wl_resource *input_method_binding; |
| 54 | struct wl_global *input_method_global; |
| 55 | struct wl_listener destroy_listener; |
| 56 | |
| 57 | struct weston_seat *seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 58 | struct text_model *model; |
| 59 | |
| 60 | struct wl_list link; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 61 | |
| 62 | struct wl_listener keyboard_focus_listener; |
| 63 | |
| 64 | int focus_listener_initialized; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 65 | |
| 66 | struct input_method_context *context; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 67 | |
| 68 | struct text_backend *text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 71 | struct input_method_context { |
| 72 | struct wl_resource resource; |
| 73 | |
| 74 | struct text_model *model; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 75 | struct input_method *input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 76 | |
| 77 | struct wl_list link; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 78 | |
| 79 | struct wl_resource *keyboard; |
| 80 | struct wl_keyboard_grab grab; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 83 | struct text_backend { |
| 84 | struct weston_compositor *compositor; |
| 85 | |
| 86 | struct { |
| 87 | char *path; |
| 88 | struct wl_resource *binding; |
| 89 | struct weston_process process; |
| 90 | struct wl_client *client; |
| 91 | } input_method; |
| 92 | |
| 93 | struct wl_listener seat_created_listener; |
| 94 | struct wl_listener destroy_listener; |
| 95 | }; |
| 96 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 97 | static void input_method_context_create(struct text_model *model, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 98 | struct input_method *input_method, |
| 99 | uint32_t serial); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 100 | static void input_method_context_end_keyboard_grab(struct input_method_context *context); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 101 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 102 | static void input_method_init_seat(struct weston_seat *seat); |
| 103 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 104 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 105 | deactivate_text_model(struct text_model *text_model, |
| 106 | struct input_method *input_method) |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 107 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 108 | struct weston_compositor *ec = text_model->ec; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 109 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 110 | if (input_method->model == text_model) { |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 111 | if (input_method->context && input_method->input_method_binding) { |
| 112 | input_method_context_end_keyboard_grab(input_method->context); |
| 113 | input_method_send_deactivate(input_method->input_method_binding, |
| 114 | &input_method->context->resource); |
| 115 | } |
| 116 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 117 | wl_list_remove(&input_method->link); |
| 118 | input_method->model = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 119 | input_method->context = NULL; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 120 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 121 | text_model_send_leave(&text_model->resource); |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 126 | destroy_text_model(struct wl_resource *resource) |
| 127 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 128 | struct text_model *text_model = |
| 129 | container_of(resource, struct text_model, resource); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 130 | struct input_method *input_method, *next; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 131 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 132 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) |
| 133 | deactivate_text_model(text_model, input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 134 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 135 | free(text_model); |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | text_model_set_surrounding_text(struct wl_client *client, |
| 140 | struct wl_resource *resource, |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 141 | const char *text, |
| 142 | uint32_t cursor, |
| 143 | uint32_t anchor) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 144 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 145 | struct text_model *text_model = resource->data; |
| 146 | struct input_method *input_method, *next; |
| 147 | |
| 148 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 149 | if (!input_method->context) |
| 150 | continue; |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 151 | input_method_context_send_surrounding_text(&input_method->context->resource, |
| 152 | text, |
| 153 | cursor, |
| 154 | anchor); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 155 | } |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 159 | text_model_activate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 160 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 161 | uint32_t serial, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 162 | struct wl_resource *seat, |
| 163 | struct wl_resource *surface) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 164 | { |
| 165 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 166 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 167 | struct input_method *input_method = weston_seat->input_method; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 168 | struct text_model *old = weston_seat->input_method->model; |
| 169 | struct weston_compositor *ec = text_model->ec; |
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 | if (old == text_model) |
| 172 | return; |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 173 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 174 | if (old) { |
| 175 | deactivate_text_model(old, |
| 176 | weston_seat->input_method); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 179 | input_method->model = text_model; |
| 180 | wl_list_insert(&text_model->input_methods, &input_method->link); |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 181 | input_method_init_seat(weston_seat); |
| 182 | |
| 183 | text_model->surface = surface->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 184 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 185 | input_method_context_create(text_model, input_method, serial); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 186 | |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 187 | wl_signal_emit(&ec->show_input_panel_signal, ec); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 188 | |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 189 | text_model_send_enter(&text_model->resource, &text_model->surface->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void |
| 193 | text_model_deactivate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 194 | struct wl_resource *resource, |
| 195 | struct wl_resource *seat) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 196 | { |
| 197 | struct text_model *text_model = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 198 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 199 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 200 | deactivate_text_model(text_model, |
| 201 | weston_seat->input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 205 | text_model_reset(struct wl_client *client, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 206 | struct wl_resource *resource, |
| 207 | uint32_t serial) |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 208 | { |
| 209 | struct text_model *text_model = resource->data; |
| 210 | struct input_method *input_method, *next; |
| 211 | |
| 212 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 213 | if (!input_method->context) |
| 214 | continue; |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 215 | input_method_context_send_reset(&input_method->context->resource, serial); |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 220 | text_model_set_micro_focus(struct wl_client *client, |
| 221 | struct wl_resource *resource, |
| 222 | int32_t x, |
| 223 | int32_t y, |
| 224 | int32_t width, |
| 225 | int32_t height) |
| 226 | { |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | text_model_set_preedit(struct wl_client *client, |
| 231 | struct wl_resource *resource) |
| 232 | { |
| 233 | } |
| 234 | |
| 235 | static void |
| 236 | text_model_set_content_type(struct wl_client *client, |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 237 | struct wl_resource *resource, |
| 238 | uint32_t hint, |
| 239 | uint32_t purpose) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 240 | { |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 241 | struct text_model *text_model = resource->data; |
| 242 | struct input_method *input_method, *next; |
| 243 | |
| 244 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 245 | if (!input_method->context) |
| 246 | continue; |
| 247 | input_method_context_send_content_type(&input_method->context->resource, hint, purpose); |
| 248 | } |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame^] | 251 | static void |
| 252 | text_model_invoke_action(struct wl_client *client, |
| 253 | struct wl_resource *resource, |
| 254 | uint32_t button, |
| 255 | uint32_t index) |
| 256 | { |
| 257 | struct text_model *text_model = resource->data; |
| 258 | struct input_method *input_method, *next; |
| 259 | |
| 260 | wl_list_for_each_safe(input_method, next, &text_model->input_methods, link) { |
| 261 | if (!input_method->context) |
| 262 | continue; |
| 263 | input_method_context_send_invoke_action(&input_method->context->resource, button, index); |
| 264 | } |
| 265 | } |
| 266 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 267 | static const struct text_model_interface text_model_implementation = { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 268 | text_model_set_surrounding_text, |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 269 | text_model_activate, |
| 270 | text_model_deactivate, |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 271 | text_model_reset, |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 272 | text_model_set_micro_focus, |
| 273 | text_model_set_preedit, |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame^] | 274 | text_model_set_content_type, |
| 275 | text_model_invoke_action |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 276 | }; |
| 277 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 278 | 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] | 279 | struct wl_resource *resource, |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 280 | uint32_t id) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 281 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 282 | struct text_model_factory *text_model_factory = resource->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 283 | struct text_model *text_model; |
| 284 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 285 | text_model = calloc(1, sizeof *text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 286 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 287 | text_model->resource.object.id = id; |
| 288 | text_model->resource.object.interface = &text_model_interface; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 289 | text_model->resource.object.implementation = |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 290 | (void (**)(void)) &text_model_implementation; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 291 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 292 | text_model->resource.data = text_model; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 293 | text_model->resource.destroy = destroy_text_model; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 294 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 295 | text_model->ec = text_model_factory->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 296 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 297 | wl_list_init(&text_model->input_methods); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 298 | |
| 299 | wl_client_add_resource(client, &text_model->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 300 | }; |
| 301 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 302 | static const struct text_model_factory_interface text_model_factory_implementation = { |
| 303 | text_model_factory_create_text_model |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | static void |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 307 | bind_text_model_factory(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 308 | void *data, |
| 309 | uint32_t version, |
| 310 | uint32_t id) |
| 311 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 312 | struct text_model_factory *text_model_factory = data; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 313 | |
| 314 | /* No checking for duplicate binding necessary. |
| 315 | * 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] | 316 | wl_client_add_object(client, &text_model_factory_interface, |
| 317 | &text_model_factory_implementation, |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 318 | id, text_model_factory); |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | text_model_factory_notifier_destroy(struct wl_listener *listener, void *data) |
| 323 | { |
| 324 | struct text_model_factory *text_model_factory = |
| 325 | container_of(listener, struct text_model_factory, destroy_listener); |
| 326 | |
| 327 | wl_display_remove_global(text_model_factory->ec->wl_display, |
| 328 | text_model_factory->text_model_factory_global); |
| 329 | |
| 330 | free(text_model_factory); |
| 331 | } |
| 332 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 333 | static void |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 334 | text_model_factory_create(struct weston_compositor *ec) |
| 335 | { |
| 336 | struct text_model_factory *text_model_factory; |
| 337 | |
| 338 | text_model_factory = calloc(1, sizeof *text_model_factory); |
| 339 | |
| 340 | text_model_factory->ec = ec; |
| 341 | |
| 342 | text_model_factory->text_model_factory_global = |
| 343 | wl_display_add_global(ec->wl_display, |
| 344 | &text_model_factory_interface, |
| 345 | text_model_factory, bind_text_model_factory); |
| 346 | |
| 347 | text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy; |
| 348 | wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener); |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 351 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 352 | input_method_context_destroy(struct wl_client *client, |
| 353 | struct wl_resource *resource) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 354 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 355 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 358 | static void |
| 359 | input_method_context_commit_string(struct wl_client *client, |
| 360 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 361 | uint32_t serial, |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 362 | const char *text, |
| 363 | uint32_t index) |
| 364 | { |
| 365 | struct input_method_context *context = resource->data; |
| 366 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 367 | text_model_send_commit_string(&context->model->resource, serial, text, index); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 370 | static void |
| 371 | input_method_context_preedit_string(struct wl_client *client, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 372 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 373 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 374 | const char *text, |
| 375 | const char *commit) |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 376 | { |
| 377 | struct input_method_context *context = resource->data; |
| 378 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 379 | text_model_send_preedit_string(&context->model->resource, serial, text, commit); |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static void |
| 383 | input_method_context_preedit_styling(struct wl_client *client, |
| 384 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 385 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 386 | uint32_t index, |
| 387 | uint32_t length, |
| 388 | uint32_t style) |
| 389 | { |
| 390 | struct input_method_context *context = resource->data; |
| 391 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 392 | text_model_send_preedit_styling(&context->model->resource, serial, index, length, style); |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void |
| 396 | input_method_context_preedit_cursor(struct wl_client *client, |
| 397 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 398 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 399 | int32_t cursor) |
| 400 | { |
| 401 | struct input_method_context *context = resource->data; |
| 402 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 403 | text_model_send_preedit_cursor(&context->model->resource, serial, cursor); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 406 | static void |
| 407 | input_method_context_delete_surrounding_text(struct wl_client *client, |
| 408 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 409 | uint32_t serial, |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 410 | int32_t index, |
| 411 | uint32_t length) |
| 412 | { |
| 413 | struct input_method_context *context = resource->data; |
| 414 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 415 | text_model_send_delete_surrounding_text(&context->model->resource, serial, index, length); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 418 | static void |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 419 | input_method_context_modifiers_map(struct wl_client *client, |
| 420 | struct wl_resource *resource, |
| 421 | struct wl_array *map) |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 422 | { |
| 423 | struct input_method_context *context = resource->data; |
| 424 | |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 425 | text_model_send_modifiers_map(&context->model->resource, map); |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | input_method_context_keysym(struct wl_client *client, |
| 430 | struct wl_resource *resource, |
| 431 | uint32_t serial, |
| 432 | uint32_t time, |
| 433 | uint32_t sym, |
| 434 | uint32_t state, |
| 435 | uint32_t modifiers) |
| 436 | { |
| 437 | struct input_method_context *context = resource->data; |
| 438 | |
| 439 | text_model_send_keysym(&context->model->resource, serial, time, |
| 440 | sym, state, modifiers); |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 443 | static void |
| 444 | unbind_keyboard(struct wl_resource *resource) |
| 445 | { |
| 446 | struct input_method_context *context = resource->data; |
| 447 | |
| 448 | input_method_context_end_keyboard_grab(context); |
| 449 | context->keyboard = NULL; |
| 450 | |
| 451 | free(resource); |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | input_method_context_grab_key(struct wl_keyboard_grab *grab, |
| 456 | uint32_t time, uint32_t key, uint32_t state_w) |
| 457 | { |
| 458 | struct input_method_context *input_method_context = container_of(grab, struct input_method_context, grab); |
| 459 | uint32_t serial; |
| 460 | |
| 461 | if (input_method_context->keyboard) { |
| 462 | serial = wl_display_next_serial(input_method_context->model->ec->wl_display); |
| 463 | wl_keyboard_send_key(input_method_context->keyboard, |
| 464 | serial, time, key, state_w); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | static void |
| 469 | input_method_context_grab_modifier(struct wl_keyboard_grab *grab, uint32_t serial, |
| 470 | uint32_t mods_depressed, uint32_t mods_latched, |
| 471 | uint32_t mods_locked, uint32_t group) |
| 472 | { |
| 473 | struct input_method_context *input_method_context = container_of(grab, struct input_method_context, grab); |
| 474 | |
| 475 | if (!input_method_context->keyboard) |
| 476 | return; |
| 477 | |
| 478 | wl_keyboard_send_modifiers(input_method_context->keyboard, |
| 479 | serial, mods_depressed, mods_latched, |
| 480 | mods_locked, group); |
| 481 | } |
| 482 | |
| 483 | static const struct wl_keyboard_grab_interface input_method_context_grab = { |
| 484 | input_method_context_grab_key, |
| 485 | input_method_context_grab_modifier, |
| 486 | }; |
| 487 | |
| 488 | static void |
| 489 | input_method_context_grab_keyboard(struct wl_client *client, |
| 490 | struct wl_resource *resource, |
| 491 | uint32_t id) |
| 492 | { |
| 493 | struct input_method_context *context = resource->data; |
| 494 | struct wl_resource *cr; |
| 495 | struct weston_seat *seat = context->input_method->seat; |
| 496 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 497 | |
| 498 | cr = wl_client_add_object(client, &wl_keyboard_interface, |
| 499 | NULL, id, context); |
| 500 | cr->destroy = unbind_keyboard; |
| 501 | |
| 502 | context->keyboard = cr; |
| 503 | |
| 504 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 505 | seat->xkb_info.keymap_fd, |
| 506 | seat->xkb_info.keymap_size); |
| 507 | |
| 508 | if (keyboard->grab != &keyboard->default_grab) { |
| 509 | wl_keyboard_end_grab(keyboard); |
| 510 | } |
| 511 | wl_keyboard_start_grab(keyboard, &context->grab); |
| 512 | } |
| 513 | |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 514 | static void |
| 515 | input_method_context_key(struct wl_client *client, |
| 516 | struct wl_resource *resource, |
| 517 | uint32_t serial, |
| 518 | uint32_t time, |
| 519 | uint32_t key, |
| 520 | uint32_t state_w) |
| 521 | { |
| 522 | struct input_method_context *context = resource->data; |
| 523 | struct weston_seat *seat = context->input_method->seat; |
| 524 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 525 | struct wl_keyboard_grab *default_grab = &keyboard->default_grab; |
| 526 | |
| 527 | default_grab->interface->key(default_grab, time, key, state_w); |
| 528 | } |
| 529 | |
| 530 | static void |
| 531 | input_method_context_modifiers(struct wl_client *client, |
| 532 | struct wl_resource *resource, |
| 533 | uint32_t serial, |
| 534 | uint32_t mods_depressed, |
| 535 | uint32_t mods_latched, |
| 536 | uint32_t mods_locked, |
| 537 | uint32_t group) |
| 538 | { |
| 539 | struct input_method_context *context = resource->data; |
| 540 | |
| 541 | struct weston_seat *seat = context->input_method->seat; |
| 542 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 543 | struct wl_keyboard_grab *default_grab = &keyboard->default_grab; |
| 544 | |
| 545 | default_grab->interface->modifiers(default_grab, |
| 546 | serial, mods_depressed, |
| 547 | mods_latched, mods_locked, |
| 548 | group); |
| 549 | } |
| 550 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 551 | static const struct input_method_context_interface input_method_context_implementation = { |
| 552 | input_method_context_destroy, |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 553 | input_method_context_commit_string, |
| 554 | input_method_context_preedit_string, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 555 | input_method_context_preedit_styling, |
| 556 | input_method_context_preedit_cursor, |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 557 | input_method_context_delete_surrounding_text, |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 558 | input_method_context_modifiers_map, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 559 | input_method_context_keysym, |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 560 | input_method_context_grab_keyboard, |
| 561 | input_method_context_key, |
| 562 | input_method_context_modifiers |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 563 | }; |
| 564 | |
| 565 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 566 | destroy_input_method_context(struct wl_resource *resource) |
| 567 | { |
| 568 | struct input_method_context *context = resource->data; |
| 569 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 570 | if (context->keyboard) { |
| 571 | wl_resource_destroy(context->keyboard); |
| 572 | } |
| 573 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 574 | free(context); |
| 575 | } |
| 576 | |
| 577 | static void |
| 578 | input_method_context_create(struct text_model *model, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 579 | struct input_method *input_method, |
| 580 | uint32_t serial) |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 581 | { |
| 582 | struct input_method_context *context; |
| 583 | |
| 584 | if (!input_method->input_method_binding) |
| 585 | return; |
| 586 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 587 | context = calloc(1, sizeof *context); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 588 | if (context == NULL) |
| 589 | return; |
| 590 | |
| 591 | context->resource.destroy = destroy_input_method_context; |
| 592 | context->resource.object.id = 0; |
| 593 | context->resource.object.interface = &input_method_context_interface; |
| 594 | context->resource.object.implementation = |
| 595 | (void (**)(void)) &input_method_context_implementation; |
| 596 | context->resource.data = context; |
| 597 | wl_signal_init(&context->resource.destroy_signal); |
| 598 | |
| 599 | context->model = model; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 600 | context->input_method = input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 601 | input_method->context = context; |
| 602 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 603 | context->grab.interface = &input_method_context_grab; |
| 604 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 605 | wl_client_add_resource(input_method->input_method_binding->client, &context->resource); |
| 606 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 607 | input_method_send_activate(input_method->input_method_binding, &context->resource, serial); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static void |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 611 | input_method_context_end_keyboard_grab(struct input_method_context *context) |
| 612 | { |
| 613 | struct wl_keyboard_grab *grab = &context->grab; |
| 614 | |
| 615 | if (grab->keyboard && (grab->keyboard->grab == grab)) { |
| 616 | wl_keyboard_end_grab(grab->keyboard); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | |
| 621 | static void |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 622 | unbind_input_method(struct wl_resource *resource) |
| 623 | { |
| 624 | struct input_method *input_method = resource->data; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 625 | struct text_backend *text_backend = input_method->text_backend; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 626 | |
| 627 | input_method->input_method_binding = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 628 | input_method->context = NULL; |
| 629 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 630 | text_backend->input_method.binding = NULL; |
| 631 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 632 | free(resource); |
| 633 | } |
| 634 | |
| 635 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 636 | bind_input_method(struct wl_client *client, |
| 637 | void *data, |
| 638 | uint32_t version, |
| 639 | uint32_t id) |
| 640 | { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 641 | struct input_method *input_method = data; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 642 | struct text_backend *text_backend = input_method->text_backend; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 643 | struct wl_resource *resource; |
| 644 | |
| 645 | resource = wl_client_add_object(client, &input_method_interface, |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 646 | NULL, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 647 | id, input_method); |
| 648 | |
| 649 | if (input_method->input_method_binding == NULL) { |
| 650 | resource->destroy = unbind_input_method; |
| 651 | input_method->input_method_binding = resource; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 652 | |
| 653 | text_backend->input_method.binding = resource; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | |
| 657 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 658 | "interface object already bound"); |
| 659 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void |
| 663 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 664 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 665 | struct input_method *input_method = |
| 666 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 667 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 668 | if (input_method->model) |
| 669 | deactivate_text_model(input_method->model, input_method); |
| 670 | |
| 671 | wl_display_remove_global(input_method->seat->compositor->wl_display, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 672 | input_method->input_method_global); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 673 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 674 | free(input_method); |
| 675 | } |
| 676 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 677 | static void |
| 678 | handle_keyboard_focus(struct wl_listener *listener, void *data) |
| 679 | { |
| 680 | struct wl_keyboard *keyboard = data; |
| 681 | struct input_method *input_method = |
| 682 | container_of(listener, struct input_method, keyboard_focus_listener); |
| 683 | struct wl_surface *surface = keyboard->focus; |
| 684 | |
| 685 | if (!input_method->model) |
| 686 | return; |
| 687 | |
| 688 | if (!surface || input_method->model->surface != surface) |
| 689 | deactivate_text_model(input_method->model, |
| 690 | input_method); |
| 691 | } |
| 692 | |
| 693 | static void |
| 694 | input_method_init_seat(struct weston_seat *seat) |
| 695 | { |
| 696 | if (seat->input_method->focus_listener_initialized) |
| 697 | return; |
| 698 | |
| 699 | if (seat->has_keyboard) { |
| 700 | seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus; |
| 701 | wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener); |
| 702 | } |
| 703 | |
| 704 | seat->input_method->focus_listener_initialized = 1; |
| 705 | } |
| 706 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 707 | static void |
| 708 | handle_input_method_sigchld(struct weston_process *process, int status) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 709 | { |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 710 | struct text_backend *text_backend = |
| 711 | container_of(process, struct text_backend, input_method.process); |
| 712 | |
| 713 | text_backend->input_method.process.pid = 0; |
| 714 | text_backend->input_method.client = NULL; |
| 715 | } |
| 716 | |
| 717 | static void |
| 718 | launch_input_method(struct text_backend *text_backend) |
| 719 | { |
| 720 | if (text_backend->input_method.binding) |
| 721 | return; |
| 722 | |
| 723 | if (!text_backend->input_method.path) |
| 724 | return; |
| 725 | |
| 726 | if (text_backend->input_method.process.pid != 0) |
| 727 | return; |
| 728 | |
| 729 | text_backend->input_method.client = weston_client_launch(text_backend->compositor, |
| 730 | &text_backend->input_method.process, |
| 731 | text_backend->input_method.path, |
| 732 | handle_input_method_sigchld); |
| 733 | |
| 734 | if (!text_backend->input_method.client) |
| 735 | weston_log("not able to start %s\n", text_backend->input_method.path); |
| 736 | } |
| 737 | |
| 738 | static void |
| 739 | handle_seat_created(struct wl_listener *listener, |
| 740 | void *data) |
| 741 | { |
| 742 | struct weston_seat *seat = data; |
| 743 | struct text_backend *text_backend = |
| 744 | container_of(listener, struct text_backend, |
| 745 | seat_created_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 746 | struct input_method *input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 747 | struct weston_compositor *ec = seat->compositor; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 748 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 749 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 750 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 751 | input_method->seat = seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 752 | input_method->model = NULL; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 753 | input_method->focus_listener_initialized = 0; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 754 | input_method->context = NULL; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 755 | input_method->text_backend = text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 756 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 757 | input_method->input_method_global = |
| 758 | wl_display_add_global(ec->wl_display, |
| 759 | &input_method_interface, |
| 760 | input_method, bind_input_method); |
| 761 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 762 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 763 | wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 764 | |
| 765 | seat->input_method = input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 766 | |
| 767 | launch_input_method(text_backend); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 768 | } |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 769 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 770 | static void |
| 771 | text_backend_configuration(struct text_backend *text_backend) |
| 772 | { |
| 773 | char *config_file; |
| 774 | char *path = NULL; |
| 775 | |
| 776 | struct config_key input_method_keys[] = { |
| 777 | { "path", CONFIG_KEY_STRING, &path } |
| 778 | }; |
| 779 | |
| 780 | struct config_section cs[] = { |
| 781 | { "input-method", input_method_keys, ARRAY_LENGTH(input_method_keys), NULL } |
| 782 | }; |
| 783 | |
| 784 | config_file = config_file_path("weston.ini"); |
| 785 | parse_config_file(config_file, cs, ARRAY_LENGTH(cs), text_backend); |
| 786 | free(config_file); |
| 787 | |
| 788 | if (path) |
| 789 | text_backend->input_method.path = path; |
| 790 | else |
| 791 | text_backend->input_method.path = strdup(LIBEXECDIR "/weston-keyboard"); |
| 792 | } |
| 793 | |
| 794 | static void |
| 795 | text_backend_notifier_destroy(struct wl_listener *listener, void *data) |
| 796 | { |
| 797 | struct text_backend *text_backend = |
| 798 | container_of(listener, struct text_backend, destroy_listener); |
| 799 | |
| 800 | if (text_backend->input_method.client) |
| 801 | wl_client_destroy(text_backend->input_method.client); |
| 802 | |
| 803 | free(text_backend->input_method.path); |
| 804 | |
| 805 | free(text_backend); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | WL_EXPORT int |
| 810 | text_backend_init(struct weston_compositor *ec) |
| 811 | { |
| 812 | struct text_backend *text_backend; |
| 813 | |
| 814 | text_backend = calloc(1, sizeof(*text_backend)); |
| 815 | |
| 816 | text_backend->compositor = ec; |
| 817 | |
| 818 | text_backend->seat_created_listener.notify = handle_seat_created; |
| 819 | wl_signal_add(&ec->seat_created_signal, |
| 820 | &text_backend->seat_created_listener); |
| 821 | |
| 822 | text_backend->destroy_listener.notify = text_backend_notifier_destroy; |
| 823 | wl_signal_add(&ec->destroy_signal, &text_backend->destroy_listener); |
| 824 | |
| 825 | text_backend_configuration(text_backend); |
| 826 | |
| 827 | text_model_factory_create(ec); |
| 828 | |
| 829 | return 0; |
| 830 | } |