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