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 | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 251 | static const struct text_model_interface text_model_implementation = { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 252 | text_model_set_surrounding_text, |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 253 | text_model_activate, |
| 254 | text_model_deactivate, |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 255 | text_model_reset, |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 256 | text_model_set_micro_focus, |
| 257 | text_model_set_preedit, |
| 258 | text_model_set_content_type |
| 259 | }; |
| 260 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 261 | 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] | 262 | struct wl_resource *resource, |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 263 | uint32_t id) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 264 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 265 | struct text_model_factory *text_model_factory = resource->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 266 | struct text_model *text_model; |
| 267 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 268 | text_model = calloc(1, sizeof *text_model); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 269 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 270 | text_model->resource.object.id = id; |
| 271 | text_model->resource.object.interface = &text_model_interface; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 272 | text_model->resource.object.implementation = |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 273 | (void (**)(void)) &text_model_implementation; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 274 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 275 | text_model->resource.data = text_model; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 276 | text_model->resource.destroy = destroy_text_model; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 277 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 278 | text_model->ec = text_model_factory->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 279 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 280 | wl_list_init(&text_model->input_methods); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 281 | |
| 282 | wl_client_add_resource(client, &text_model->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 283 | }; |
| 284 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 285 | static const struct text_model_factory_interface text_model_factory_implementation = { |
| 286 | text_model_factory_create_text_model |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | static void |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 290 | bind_text_model_factory(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 291 | void *data, |
| 292 | uint32_t version, |
| 293 | uint32_t id) |
| 294 | { |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 295 | struct text_model_factory *text_model_factory = data; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 296 | |
| 297 | /* No checking for duplicate binding necessary. |
| 298 | * 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] | 299 | wl_client_add_object(client, &text_model_factory_interface, |
| 300 | &text_model_factory_implementation, |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 301 | id, text_model_factory); |
| 302 | } |
| 303 | |
| 304 | static void |
| 305 | text_model_factory_notifier_destroy(struct wl_listener *listener, void *data) |
| 306 | { |
| 307 | struct text_model_factory *text_model_factory = |
| 308 | container_of(listener, struct text_model_factory, destroy_listener); |
| 309 | |
| 310 | wl_display_remove_global(text_model_factory->ec->wl_display, |
| 311 | text_model_factory->text_model_factory_global); |
| 312 | |
| 313 | free(text_model_factory); |
| 314 | } |
| 315 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 316 | static void |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 317 | text_model_factory_create(struct weston_compositor *ec) |
| 318 | { |
| 319 | struct text_model_factory *text_model_factory; |
| 320 | |
| 321 | text_model_factory = calloc(1, sizeof *text_model_factory); |
| 322 | |
| 323 | text_model_factory->ec = ec; |
| 324 | |
| 325 | text_model_factory->text_model_factory_global = |
| 326 | wl_display_add_global(ec->wl_display, |
| 327 | &text_model_factory_interface, |
| 328 | text_model_factory, bind_text_model_factory); |
| 329 | |
| 330 | text_model_factory->destroy_listener.notify = text_model_factory_notifier_destroy; |
| 331 | wl_signal_add(&ec->destroy_signal, &text_model_factory->destroy_listener); |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 334 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 335 | input_method_context_destroy(struct wl_client *client, |
| 336 | struct wl_resource *resource) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 337 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 338 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 341 | static void |
| 342 | input_method_context_commit_string(struct wl_client *client, |
| 343 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 344 | uint32_t serial, |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 345 | const char *text, |
| 346 | uint32_t index) |
| 347 | { |
| 348 | struct input_method_context *context = resource->data; |
| 349 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 350 | text_model_send_commit_string(&context->model->resource, serial, text, index); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 353 | static void |
| 354 | input_method_context_preedit_string(struct wl_client *client, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 355 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 356 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 357 | const char *text, |
| 358 | const char *commit) |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 359 | { |
| 360 | struct input_method_context *context = resource->data; |
| 361 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 362 | text_model_send_preedit_string(&context->model->resource, serial, text, commit); |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void |
| 366 | input_method_context_preedit_styling(struct wl_client *client, |
| 367 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 368 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 369 | uint32_t index, |
| 370 | uint32_t length, |
| 371 | uint32_t style) |
| 372 | { |
| 373 | struct input_method_context *context = resource->data; |
| 374 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 375 | 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] | 376 | } |
| 377 | |
| 378 | static void |
| 379 | input_method_context_preedit_cursor(struct wl_client *client, |
| 380 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 381 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 382 | int32_t cursor) |
| 383 | { |
| 384 | struct input_method_context *context = resource->data; |
| 385 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 386 | text_model_send_preedit_cursor(&context->model->resource, serial, cursor); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 389 | static void |
| 390 | input_method_context_delete_surrounding_text(struct wl_client *client, |
| 391 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 392 | uint32_t serial, |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 393 | int32_t index, |
| 394 | uint32_t length) |
| 395 | { |
| 396 | struct input_method_context *context = resource->data; |
| 397 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 398 | 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] | 399 | } |
| 400 | |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 401 | static void |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 402 | input_method_context_modifiers_map(struct wl_client *client, |
| 403 | struct wl_resource *resource, |
| 404 | struct wl_array *map) |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 405 | { |
| 406 | struct input_method_context *context = resource->data; |
| 407 | |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 408 | text_model_send_modifiers_map(&context->model->resource, map); |
| 409 | } |
| 410 | |
| 411 | static void |
| 412 | input_method_context_keysym(struct wl_client *client, |
| 413 | struct wl_resource *resource, |
| 414 | uint32_t serial, |
| 415 | uint32_t time, |
| 416 | uint32_t sym, |
| 417 | uint32_t state, |
| 418 | uint32_t modifiers) |
| 419 | { |
| 420 | struct input_method_context *context = resource->data; |
| 421 | |
| 422 | text_model_send_keysym(&context->model->resource, serial, time, |
| 423 | sym, state, modifiers); |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 426 | static void |
| 427 | unbind_keyboard(struct wl_resource *resource) |
| 428 | { |
| 429 | struct input_method_context *context = resource->data; |
| 430 | |
| 431 | input_method_context_end_keyboard_grab(context); |
| 432 | context->keyboard = NULL; |
| 433 | |
| 434 | free(resource); |
| 435 | } |
| 436 | |
| 437 | static void |
| 438 | input_method_context_grab_key(struct wl_keyboard_grab *grab, |
| 439 | uint32_t time, uint32_t key, uint32_t state_w) |
| 440 | { |
| 441 | struct input_method_context *input_method_context = container_of(grab, struct input_method_context, grab); |
| 442 | uint32_t serial; |
| 443 | |
| 444 | if (input_method_context->keyboard) { |
| 445 | serial = wl_display_next_serial(input_method_context->model->ec->wl_display); |
| 446 | wl_keyboard_send_key(input_method_context->keyboard, |
| 447 | serial, time, key, state_w); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static void |
| 452 | input_method_context_grab_modifier(struct wl_keyboard_grab *grab, uint32_t serial, |
| 453 | uint32_t mods_depressed, uint32_t mods_latched, |
| 454 | uint32_t mods_locked, uint32_t group) |
| 455 | { |
| 456 | struct input_method_context *input_method_context = container_of(grab, struct input_method_context, grab); |
| 457 | |
| 458 | if (!input_method_context->keyboard) |
| 459 | return; |
| 460 | |
| 461 | wl_keyboard_send_modifiers(input_method_context->keyboard, |
| 462 | serial, mods_depressed, mods_latched, |
| 463 | mods_locked, group); |
| 464 | } |
| 465 | |
| 466 | static const struct wl_keyboard_grab_interface input_method_context_grab = { |
| 467 | input_method_context_grab_key, |
| 468 | input_method_context_grab_modifier, |
| 469 | }; |
| 470 | |
| 471 | static void |
| 472 | input_method_context_grab_keyboard(struct wl_client *client, |
| 473 | struct wl_resource *resource, |
| 474 | uint32_t id) |
| 475 | { |
| 476 | struct input_method_context *context = resource->data; |
| 477 | struct wl_resource *cr; |
| 478 | struct weston_seat *seat = context->input_method->seat; |
| 479 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 480 | |
| 481 | cr = wl_client_add_object(client, &wl_keyboard_interface, |
| 482 | NULL, id, context); |
| 483 | cr->destroy = unbind_keyboard; |
| 484 | |
| 485 | context->keyboard = cr; |
| 486 | |
| 487 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 488 | seat->xkb_info.keymap_fd, |
| 489 | seat->xkb_info.keymap_size); |
| 490 | |
| 491 | if (keyboard->grab != &keyboard->default_grab) { |
| 492 | wl_keyboard_end_grab(keyboard); |
| 493 | } |
| 494 | wl_keyboard_start_grab(keyboard, &context->grab); |
| 495 | } |
| 496 | |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 497 | static void |
| 498 | input_method_context_key(struct wl_client *client, |
| 499 | struct wl_resource *resource, |
| 500 | uint32_t serial, |
| 501 | uint32_t time, |
| 502 | uint32_t key, |
| 503 | uint32_t state_w) |
| 504 | { |
| 505 | struct input_method_context *context = resource->data; |
| 506 | struct weston_seat *seat = context->input_method->seat; |
| 507 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 508 | struct wl_keyboard_grab *default_grab = &keyboard->default_grab; |
| 509 | |
| 510 | default_grab->interface->key(default_grab, time, key, state_w); |
| 511 | } |
| 512 | |
| 513 | static void |
| 514 | input_method_context_modifiers(struct wl_client *client, |
| 515 | struct wl_resource *resource, |
| 516 | uint32_t serial, |
| 517 | uint32_t mods_depressed, |
| 518 | uint32_t mods_latched, |
| 519 | uint32_t mods_locked, |
| 520 | uint32_t group) |
| 521 | { |
| 522 | struct input_method_context *context = resource->data; |
| 523 | |
| 524 | struct weston_seat *seat = context->input_method->seat; |
| 525 | struct wl_keyboard *keyboard = seat->seat.keyboard; |
| 526 | struct wl_keyboard_grab *default_grab = &keyboard->default_grab; |
| 527 | |
| 528 | default_grab->interface->modifiers(default_grab, |
| 529 | serial, mods_depressed, |
| 530 | mods_latched, mods_locked, |
| 531 | group); |
| 532 | } |
| 533 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 534 | static const struct input_method_context_interface input_method_context_implementation = { |
| 535 | input_method_context_destroy, |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 536 | input_method_context_commit_string, |
| 537 | input_method_context_preedit_string, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 538 | input_method_context_preedit_styling, |
| 539 | input_method_context_preedit_cursor, |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 540 | input_method_context_delete_surrounding_text, |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 541 | input_method_context_modifiers_map, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 542 | input_method_context_keysym, |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 543 | input_method_context_grab_keyboard, |
| 544 | input_method_context_key, |
| 545 | input_method_context_modifiers |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 549 | destroy_input_method_context(struct wl_resource *resource) |
| 550 | { |
| 551 | struct input_method_context *context = resource->data; |
| 552 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 553 | if (context->keyboard) { |
| 554 | wl_resource_destroy(context->keyboard); |
| 555 | } |
| 556 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 557 | free(context); |
| 558 | } |
| 559 | |
| 560 | static void |
| 561 | input_method_context_create(struct text_model *model, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 562 | struct input_method *input_method, |
| 563 | uint32_t serial) |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 564 | { |
| 565 | struct input_method_context *context; |
| 566 | |
| 567 | if (!input_method->input_method_binding) |
| 568 | return; |
| 569 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 570 | context = calloc(1, sizeof *context); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 571 | if (context == NULL) |
| 572 | return; |
| 573 | |
| 574 | context->resource.destroy = destroy_input_method_context; |
| 575 | context->resource.object.id = 0; |
| 576 | context->resource.object.interface = &input_method_context_interface; |
| 577 | context->resource.object.implementation = |
| 578 | (void (**)(void)) &input_method_context_implementation; |
| 579 | context->resource.data = context; |
| 580 | wl_signal_init(&context->resource.destroy_signal); |
| 581 | |
| 582 | context->model = model; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 583 | context->input_method = input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 584 | input_method->context = context; |
| 585 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 586 | context->grab.interface = &input_method_context_grab; |
| 587 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 588 | wl_client_add_resource(input_method->input_method_binding->client, &context->resource); |
| 589 | |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 590 | 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] | 591 | } |
| 592 | |
| 593 | static void |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 594 | input_method_context_end_keyboard_grab(struct input_method_context *context) |
| 595 | { |
| 596 | struct wl_keyboard_grab *grab = &context->grab; |
| 597 | |
| 598 | if (grab->keyboard && (grab->keyboard->grab == grab)) { |
| 599 | wl_keyboard_end_grab(grab->keyboard); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | |
| 604 | static void |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 605 | unbind_input_method(struct wl_resource *resource) |
| 606 | { |
| 607 | struct input_method *input_method = resource->data; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 608 | struct text_backend *text_backend = input_method->text_backend; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 609 | |
| 610 | input_method->input_method_binding = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 611 | input_method->context = NULL; |
| 612 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 613 | text_backend->input_method.binding = NULL; |
| 614 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 615 | free(resource); |
| 616 | } |
| 617 | |
| 618 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 619 | bind_input_method(struct wl_client *client, |
| 620 | void *data, |
| 621 | uint32_t version, |
| 622 | uint32_t id) |
| 623 | { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 624 | struct input_method *input_method = 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 | struct wl_resource *resource; |
| 627 | |
| 628 | resource = wl_client_add_object(client, &input_method_interface, |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 629 | NULL, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 630 | id, input_method); |
| 631 | |
| 632 | if (input_method->input_method_binding == NULL) { |
| 633 | resource->destroy = unbind_input_method; |
| 634 | input_method->input_method_binding = resource; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 635 | |
| 636 | text_backend->input_method.binding = resource; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 637 | return; |
| 638 | } |
| 639 | |
| 640 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 641 | "interface object already bound"); |
| 642 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static void |
| 646 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 647 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 648 | struct input_method *input_method = |
| 649 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 650 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 651 | if (input_method->model) |
| 652 | deactivate_text_model(input_method->model, input_method); |
| 653 | |
| 654 | wl_display_remove_global(input_method->seat->compositor->wl_display, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 655 | input_method->input_method_global); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 656 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 657 | free(input_method); |
| 658 | } |
| 659 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 660 | static void |
| 661 | handle_keyboard_focus(struct wl_listener *listener, void *data) |
| 662 | { |
| 663 | struct wl_keyboard *keyboard = data; |
| 664 | struct input_method *input_method = |
| 665 | container_of(listener, struct input_method, keyboard_focus_listener); |
| 666 | struct wl_surface *surface = keyboard->focus; |
| 667 | |
| 668 | if (!input_method->model) |
| 669 | return; |
| 670 | |
| 671 | if (!surface || input_method->model->surface != surface) |
| 672 | deactivate_text_model(input_method->model, |
| 673 | input_method); |
| 674 | } |
| 675 | |
| 676 | static void |
| 677 | input_method_init_seat(struct weston_seat *seat) |
| 678 | { |
| 679 | if (seat->input_method->focus_listener_initialized) |
| 680 | return; |
| 681 | |
| 682 | if (seat->has_keyboard) { |
| 683 | seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus; |
| 684 | wl_signal_add(&seat->seat.keyboard->focus_signal, &seat->input_method->keyboard_focus_listener); |
| 685 | } |
| 686 | |
| 687 | seat->input_method->focus_listener_initialized = 1; |
| 688 | } |
| 689 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 690 | static void |
| 691 | handle_input_method_sigchld(struct weston_process *process, int status) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 692 | { |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 693 | struct text_backend *text_backend = |
| 694 | container_of(process, struct text_backend, input_method.process); |
| 695 | |
| 696 | text_backend->input_method.process.pid = 0; |
| 697 | text_backend->input_method.client = NULL; |
| 698 | } |
| 699 | |
| 700 | static void |
| 701 | launch_input_method(struct text_backend *text_backend) |
| 702 | { |
| 703 | if (text_backend->input_method.binding) |
| 704 | return; |
| 705 | |
| 706 | if (!text_backend->input_method.path) |
| 707 | return; |
| 708 | |
| 709 | if (text_backend->input_method.process.pid != 0) |
| 710 | return; |
| 711 | |
| 712 | text_backend->input_method.client = weston_client_launch(text_backend->compositor, |
| 713 | &text_backend->input_method.process, |
| 714 | text_backend->input_method.path, |
| 715 | handle_input_method_sigchld); |
| 716 | |
| 717 | if (!text_backend->input_method.client) |
| 718 | weston_log("not able to start %s\n", text_backend->input_method.path); |
| 719 | } |
| 720 | |
| 721 | static void |
| 722 | handle_seat_created(struct wl_listener *listener, |
| 723 | void *data) |
| 724 | { |
| 725 | struct weston_seat *seat = data; |
| 726 | struct text_backend *text_backend = |
| 727 | container_of(listener, struct text_backend, |
| 728 | seat_created_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 729 | struct input_method *input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 730 | struct weston_compositor *ec = seat->compositor; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 731 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 732 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 733 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 734 | input_method->seat = seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 735 | input_method->model = NULL; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 736 | input_method->focus_listener_initialized = 0; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 737 | input_method->context = NULL; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 738 | input_method->text_backend = text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 739 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 740 | input_method->input_method_global = |
| 741 | wl_display_add_global(ec->wl_display, |
| 742 | &input_method_interface, |
| 743 | input_method, bind_input_method); |
| 744 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 745 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 746 | wl_signal_add(&seat->seat.destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 747 | |
| 748 | seat->input_method = input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 749 | |
| 750 | launch_input_method(text_backend); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 751 | } |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 752 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 753 | static void |
| 754 | text_backend_configuration(struct text_backend *text_backend) |
| 755 | { |
| 756 | char *config_file; |
| 757 | char *path = NULL; |
| 758 | |
| 759 | struct config_key input_method_keys[] = { |
| 760 | { "path", CONFIG_KEY_STRING, &path } |
| 761 | }; |
| 762 | |
| 763 | struct config_section cs[] = { |
| 764 | { "input-method", input_method_keys, ARRAY_LENGTH(input_method_keys), NULL } |
| 765 | }; |
| 766 | |
| 767 | config_file = config_file_path("weston.ini"); |
| 768 | parse_config_file(config_file, cs, ARRAY_LENGTH(cs), text_backend); |
| 769 | free(config_file); |
| 770 | |
| 771 | if (path) |
| 772 | text_backend->input_method.path = path; |
| 773 | else |
| 774 | text_backend->input_method.path = strdup(LIBEXECDIR "/weston-keyboard"); |
| 775 | } |
| 776 | |
| 777 | static void |
| 778 | text_backend_notifier_destroy(struct wl_listener *listener, void *data) |
| 779 | { |
| 780 | struct text_backend *text_backend = |
| 781 | container_of(listener, struct text_backend, destroy_listener); |
| 782 | |
| 783 | if (text_backend->input_method.client) |
| 784 | wl_client_destroy(text_backend->input_method.client); |
| 785 | |
| 786 | free(text_backend->input_method.path); |
| 787 | |
| 788 | free(text_backend); |
| 789 | } |
| 790 | |
| 791 | |
| 792 | WL_EXPORT int |
| 793 | text_backend_init(struct weston_compositor *ec) |
| 794 | { |
| 795 | struct text_backend *text_backend; |
| 796 | |
| 797 | text_backend = calloc(1, sizeof(*text_backend)); |
| 798 | |
| 799 | text_backend->compositor = ec; |
| 800 | |
| 801 | text_backend->seat_created_listener.notify = handle_seat_created; |
| 802 | wl_signal_add(&ec->seat_created_signal, |
| 803 | &text_backend->seat_created_listener); |
| 804 | |
| 805 | text_backend->destroy_listener.notify = text_backend_notifier_destroy; |
| 806 | wl_signal_add(&ec->destroy_signal, &text_backend->destroy_listener); |
| 807 | |
| 808 | text_backend_configuration(text_backend); |
| 809 | |
| 810 | text_model_factory_create(ec); |
| 811 | |
| 812 | return 0; |
| 813 | } |