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> |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Eduardo Lima (Etrunko) | 2733700 | 2013-05-14 13:09:31 -0300 | [diff] [blame] | 27 | #include <time.h> |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 28 | |
| 29 | #include "compositor.h" |
| 30 | #include "text-server-protocol.h" |
Jan Arne Petersen | 30b66ef | 2012-09-09 23:08:41 +0200 | [diff] [blame] | 31 | #include "input-method-server-protocol.h" |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 32 | |
| 33 | struct input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 34 | struct input_method_context; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 35 | struct text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 36 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 37 | struct text_input { |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 38 | struct wl_resource resource; |
| 39 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 40 | struct weston_compositor *ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 41 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 42 | struct wl_list input_methods; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 43 | |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 44 | struct weston_surface *surface; |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 45 | |
Jan Arne Petersen | 14da96b | 2013-04-18 16:47:28 +0200 | [diff] [blame] | 46 | pixman_box32_t cursor_rectangle; |
| 47 | |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 48 | uint32_t input_panel_visible; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 51 | struct text_input_manager { |
| 52 | struct wl_global *text_input_manager_global; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 53 | struct wl_listener destroy_listener; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 54 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 55 | struct weston_compositor *ec; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct input_method { |
| 59 | struct wl_resource *input_method_binding; |
| 60 | struct wl_global *input_method_global; |
| 61 | struct wl_listener destroy_listener; |
| 62 | |
| 63 | struct weston_seat *seat; |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 64 | struct text_input *model; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 65 | |
| 66 | struct wl_list link; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 67 | |
| 68 | struct wl_listener keyboard_focus_listener; |
| 69 | |
| 70 | int focus_listener_initialized; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 71 | |
| 72 | struct input_method_context *context; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 73 | |
| 74 | struct text_backend *text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 77 | struct input_method_context { |
| 78 | struct wl_resource resource; |
| 79 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 80 | struct text_input *model; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 81 | struct input_method *input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 82 | |
| 83 | struct wl_list link; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 84 | |
| 85 | struct wl_resource *keyboard; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 88 | struct text_backend { |
| 89 | struct weston_compositor *compositor; |
| 90 | |
| 91 | struct { |
| 92 | char *path; |
| 93 | struct wl_resource *binding; |
| 94 | struct weston_process process; |
| 95 | struct wl_client *client; |
Eduardo Lima (Etrunko) | 2733700 | 2013-05-14 13:09:31 -0300 | [diff] [blame] | 96 | |
| 97 | unsigned deathcount; |
| 98 | uint32_t deathstamp; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 99 | } input_method; |
| 100 | |
| 101 | struct wl_listener seat_created_listener; |
| 102 | struct wl_listener destroy_listener; |
| 103 | }; |
| 104 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 105 | static void input_method_context_create(struct text_input *model, |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 106 | struct input_method *input_method); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 107 | 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] | 108 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 109 | static void input_method_init_seat(struct weston_seat *seat); |
| 110 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 111 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 112 | deactivate_text_input(struct text_input *text_input, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 113 | struct input_method *input_method) |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 114 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 115 | struct weston_compositor *ec = text_input->ec; |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 116 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 117 | if (input_method->model == text_input) { |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 118 | if (input_method->context && input_method->input_method_binding) { |
| 119 | input_method_context_end_keyboard_grab(input_method->context); |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 120 | wl_input_method_send_deactivate(input_method->input_method_binding, |
| 121 | &input_method->context->resource); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 124 | wl_list_remove(&input_method->link); |
| 125 | input_method->model = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 126 | input_method->context = NULL; |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 127 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 128 | wl_text_input_send_leave(&text_input->resource); |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 133 | destroy_text_input(struct wl_resource *resource) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 134 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 135 | struct text_input *text_input = |
| 136 | container_of(resource, struct text_input, resource); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 137 | struct input_method *input_method, *next; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 138 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 139 | wl_list_for_each_safe(input_method, next, &text_input->input_methods, link) |
| 140 | deactivate_text_input(text_input, input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 141 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 142 | free(text_input); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 146 | text_input_set_surrounding_text(struct wl_client *client, |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 147 | struct wl_resource *resource, |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 148 | const char *text, |
| 149 | uint32_t cursor, |
| 150 | uint32_t anchor) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 151 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 152 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 153 | struct input_method *input_method, *next; |
| 154 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 155 | wl_list_for_each_safe(input_method, next, &text_input->input_methods, link) { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 156 | if (!input_method->context) |
| 157 | continue; |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 158 | wl_input_method_context_send_surrounding_text(&input_method->context->resource, |
| 159 | text, |
| 160 | cursor, |
| 161 | anchor); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 162 | } |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 166 | text_input_activate(struct wl_client *client, |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 167 | struct wl_resource *resource, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 168 | struct wl_resource *seat, |
| 169 | struct wl_resource *surface) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 170 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 171 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 172 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 173 | struct input_method *input_method = weston_seat->input_method; |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 174 | struct text_input *old = weston_seat->input_method->model; |
| 175 | struct weston_compositor *ec = text_input->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 176 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 177 | if (old == text_input) |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 178 | return; |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 179 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 180 | if (old) { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 181 | deactivate_text_input(old, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 182 | weston_seat->input_method); |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 185 | input_method->model = text_input; |
| 186 | wl_list_insert(&text_input->input_methods, &input_method->link); |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 187 | input_method_init_seat(weston_seat); |
| 188 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 189 | text_input->surface = surface->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 190 | |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 191 | input_method_context_create(text_input, input_method); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 192 | |
Jan Arne Petersen | 14da96b | 2013-04-18 16:47:28 +0200 | [diff] [blame] | 193 | if (text_input->input_panel_visible) { |
| 194 | wl_signal_emit(&ec->show_input_panel_signal, text_input->surface); |
| 195 | wl_signal_emit(&ec->update_input_panel_signal, &text_input->cursor_rectangle); |
| 196 | } |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 197 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 198 | wl_text_input_send_enter(&text_input->resource, &text_input->surface->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 202 | text_input_deactivate(struct wl_client *client, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 203 | struct wl_resource *resource, |
| 204 | struct wl_resource *seat) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 205 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 206 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 207 | struct weston_seat *weston_seat = seat->data; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 208 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 209 | deactivate_text_input(text_input, |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 210 | weston_seat->input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 214 | text_input_reset(struct wl_client *client, |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 215 | struct wl_resource *resource) |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 216 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 217 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 218 | struct input_method *input_method, *next; |
| 219 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 220 | wl_list_for_each_safe(input_method, next, &text_input->input_methods, link) { |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 221 | if (!input_method->context) |
| 222 | continue; |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 223 | wl_input_method_context_send_reset(&input_method->context->resource); |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 228 | text_input_set_cursor_rectangle(struct wl_client *client, |
Jan Arne Petersen | 7ef8eff | 2013-04-18 16:47:23 +0200 | [diff] [blame] | 229 | struct wl_resource *resource, |
| 230 | int32_t x, |
| 231 | int32_t y, |
| 232 | int32_t width, |
| 233 | int32_t height) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 234 | { |
Jan Arne Petersen | 14da96b | 2013-04-18 16:47:28 +0200 | [diff] [blame] | 235 | struct text_input *text_input = resource->data; |
| 236 | struct weston_compositor *ec = text_input->ec; |
| 237 | |
| 238 | text_input->cursor_rectangle.x1 = x; |
| 239 | text_input->cursor_rectangle.y1 = y; |
| 240 | text_input->cursor_rectangle.x2 = x + width; |
| 241 | text_input->cursor_rectangle.y2 = y + height; |
| 242 | |
| 243 | wl_signal_emit(&ec->update_input_panel_signal, &text_input->cursor_rectangle); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 247 | text_input_set_content_type(struct wl_client *client, |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 248 | struct wl_resource *resource, |
| 249 | uint32_t hint, |
| 250 | uint32_t purpose) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 251 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 252 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 253 | struct input_method *input_method, *next; |
| 254 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 255 | wl_list_for_each_safe(input_method, next, &text_input->input_methods, link) { |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 256 | if (!input_method->context) |
| 257 | continue; |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 258 | wl_input_method_context_send_content_type(&input_method->context->resource, hint, purpose); |
Jan Arne Petersen | 26ffa81 | 2013-01-16 21:26:43 +0100 | [diff] [blame] | 259 | } |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame] | 262 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 263 | text_input_invoke_action(struct wl_client *client, |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame] | 264 | struct wl_resource *resource, |
| 265 | uint32_t button, |
| 266 | uint32_t index) |
| 267 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 268 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame] | 269 | struct input_method *input_method, *next; |
| 270 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 271 | wl_list_for_each_safe(input_method, next, &text_input->input_methods, link) { |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame] | 272 | if (!input_method->context) |
| 273 | continue; |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 274 | wl_input_method_context_send_invoke_action(&input_method->context->resource, button, index); |
Jan Arne Petersen | adfedc1 | 2013-01-16 21:26:46 +0100 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 278 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 279 | text_input_commit_state(struct wl_client *client, |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 280 | struct wl_resource *resource, |
| 281 | uint32_t serial) |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 282 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 283 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 284 | struct input_method *input_method, *next; |
| 285 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 286 | wl_list_for_each_safe(input_method, next, &text_input->input_methods, link) { |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 287 | if (!input_method->context) |
| 288 | continue; |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 289 | wl_input_method_context_send_commit_state(&input_method->context->resource, serial); |
Jan Arne Petersen | 0eabcaa | 2013-01-31 15:52:20 +0100 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 293 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 294 | text_input_show_input_panel(struct wl_client *client, |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 295 | struct wl_resource *resource) |
| 296 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 297 | struct text_input *text_input = resource->data; |
| 298 | struct weston_compositor *ec = text_input->ec; |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 299 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 300 | text_input->input_panel_visible = 1; |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 301 | |
Jan Arne Petersen | 14da96b | 2013-04-18 16:47:28 +0200 | [diff] [blame] | 302 | if (!wl_list_empty(&text_input->input_methods)) { |
| 303 | wl_signal_emit(&ec->show_input_panel_signal, text_input->surface); |
| 304 | wl_signal_emit(&ec->update_input_panel_signal, &text_input->cursor_rectangle); |
| 305 | } |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 309 | text_input_hide_input_panel(struct wl_client *client, |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 310 | struct wl_resource *resource) |
| 311 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 312 | struct text_input *text_input = resource->data; |
| 313 | struct weston_compositor *ec = text_input->ec; |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 314 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 315 | text_input->input_panel_visible = 0; |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 316 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 317 | if (!wl_list_empty(&text_input->input_methods)) |
Jan Arne Petersen | 6138197 | 2013-01-31 15:52:21 +0100 | [diff] [blame] | 318 | wl_signal_emit(&ec->hide_input_panel_signal, ec); |
| 319 | } |
| 320 | |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 321 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 322 | text_input_set_preferred_language(struct wl_client *client, |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 323 | struct wl_resource *resource, |
| 324 | const char *language) |
| 325 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 326 | struct text_input *text_input = resource->data; |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 327 | struct input_method *input_method, *next; |
| 328 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 329 | wl_list_for_each_safe(input_method, next, &text_input->input_methods, link) { |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 330 | if (!input_method->context) |
| 331 | continue; |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 332 | wl_input_method_context_send_preferred_language(&input_method->context->resource, |
| 333 | language); |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 337 | static const struct wl_text_input_interface text_input_implementation = { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 338 | text_input_activate, |
| 339 | text_input_deactivate, |
| 340 | text_input_show_input_panel, |
| 341 | text_input_hide_input_panel, |
| 342 | text_input_reset, |
| 343 | text_input_set_surrounding_text, |
| 344 | text_input_set_content_type, |
| 345 | text_input_set_cursor_rectangle, |
| 346 | text_input_set_preferred_language, |
| 347 | text_input_commit_state, |
| 348 | text_input_invoke_action |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 349 | }; |
| 350 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 351 | static void text_input_manager_create_text_input(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 352 | struct wl_resource *resource, |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 353 | uint32_t id) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 354 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 355 | struct text_input_manager *text_input_manager = resource->data; |
| 356 | struct text_input *text_input; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 357 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 358 | text_input = calloc(1, sizeof *text_input); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 359 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 360 | text_input->resource.object.id = id; |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 361 | text_input->resource.object.interface = &wl_text_input_interface; |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 362 | text_input->resource.object.implementation = |
| 363 | (void (**)(void)) &text_input_implementation; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 364 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 365 | text_input->resource.data = text_input; |
| 366 | text_input->resource.destroy = destroy_text_input; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 367 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 368 | text_input->ec = text_input_manager->ec; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 369 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 370 | wl_list_init(&text_input->input_methods); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 371 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 372 | wl_client_add_resource(client, &text_input->resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 373 | }; |
| 374 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 375 | static const struct wl_text_input_manager_interface text_input_manager_implementation = { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 376 | text_input_manager_create_text_input |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 380 | bind_text_input_manager(struct wl_client *client, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 381 | void *data, |
| 382 | uint32_t version, |
| 383 | uint32_t id) |
| 384 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 385 | struct text_input_manager *text_input_manager = data; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 386 | |
| 387 | /* No checking for duplicate binding necessary. |
| 388 | * No events have to be sent, so we don't need the return value. */ |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 389 | wl_client_add_object(client, &wl_text_input_manager_interface, |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 390 | &text_input_manager_implementation, |
| 391 | id, text_input_manager); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 395 | text_input_manager_notifier_destroy(struct wl_listener *listener, void *data) |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 396 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 397 | struct text_input_manager *text_input_manager = |
| 398 | container_of(listener, struct text_input_manager, destroy_listener); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 399 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 400 | wl_display_remove_global(text_input_manager->ec->wl_display, |
| 401 | text_input_manager->text_input_manager_global); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 402 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 403 | free(text_input_manager); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 406 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 407 | text_input_manager_create(struct weston_compositor *ec) |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 408 | { |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 409 | struct text_input_manager *text_input_manager; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 410 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 411 | text_input_manager = calloc(1, sizeof *text_input_manager); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 412 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 413 | text_input_manager->ec = ec; |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 414 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 415 | text_input_manager->text_input_manager_global = |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 416 | wl_display_add_global(ec->wl_display, |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 417 | &wl_text_input_manager_interface, |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 418 | text_input_manager, bind_text_input_manager); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 419 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 420 | text_input_manager->destroy_listener.notify = text_input_manager_notifier_destroy; |
| 421 | wl_signal_add(&ec->destroy_signal, &text_input_manager->destroy_listener); |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 424 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 425 | input_method_context_destroy(struct wl_client *client, |
| 426 | struct wl_resource *resource) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 427 | { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 428 | wl_resource_destroy(resource); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 431 | static void |
| 432 | input_method_context_commit_string(struct wl_client *client, |
| 433 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 434 | uint32_t serial, |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame] | 435 | const char *text) |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 436 | { |
| 437 | struct input_method_context *context = resource->data; |
| 438 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 439 | wl_text_input_send_commit_string(&context->model->resource, serial, text); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 442 | static void |
| 443 | input_method_context_preedit_string(struct wl_client *client, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 444 | struct wl_resource *resource, |
Jan Arne Petersen | c7d2a98 | 2013-01-16 21:26:39 +0100 | [diff] [blame] | 445 | uint32_t serial, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 446 | const char *text, |
| 447 | const char *commit) |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 448 | { |
| 449 | struct input_method_context *context = resource->data; |
| 450 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 451 | wl_text_input_send_preedit_string(&context->model->resource, serial, text, commit); |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void |
| 455 | input_method_context_preedit_styling(struct wl_client *client, |
| 456 | struct wl_resource *resource, |
| 457 | uint32_t index, |
| 458 | uint32_t length, |
| 459 | uint32_t style) |
| 460 | { |
| 461 | struct input_method_context *context = resource->data; |
| 462 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 463 | wl_text_input_send_preedit_styling(&context->model->resource, index, length, style); |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void |
| 467 | input_method_context_preedit_cursor(struct wl_client *client, |
| 468 | struct wl_resource *resource, |
| 469 | int32_t cursor) |
| 470 | { |
| 471 | struct input_method_context *context = resource->data; |
| 472 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 473 | wl_text_input_send_preedit_cursor(&context->model->resource, cursor); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 476 | static void |
| 477 | input_method_context_delete_surrounding_text(struct wl_client *client, |
| 478 | struct wl_resource *resource, |
| 479 | int32_t index, |
| 480 | uint32_t length) |
| 481 | { |
| 482 | struct input_method_context *context = resource->data; |
| 483 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 484 | wl_text_input_send_delete_surrounding_text(&context->model->resource, index, length); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 487 | static void |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame] | 488 | input_method_context_cursor_position(struct wl_client *client, |
| 489 | struct wl_resource *resource, |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame] | 490 | int32_t index, |
| 491 | int32_t anchor) |
| 492 | { |
| 493 | struct input_method_context *context = resource->data; |
| 494 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 495 | wl_text_input_send_cursor_position(&context->model->resource, index, anchor); |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static void |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 499 | input_method_context_modifiers_map(struct wl_client *client, |
| 500 | struct wl_resource *resource, |
| 501 | struct wl_array *map) |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 502 | { |
| 503 | struct input_method_context *context = resource->data; |
| 504 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 505 | wl_text_input_send_modifiers_map(&context->model->resource, map); |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void |
| 509 | input_method_context_keysym(struct wl_client *client, |
| 510 | struct wl_resource *resource, |
| 511 | uint32_t serial, |
| 512 | uint32_t time, |
| 513 | uint32_t sym, |
| 514 | uint32_t state, |
| 515 | uint32_t modifiers) |
| 516 | { |
| 517 | struct input_method_context *context = resource->data; |
| 518 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 519 | wl_text_input_send_keysym(&context->model->resource, serial, time, |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 520 | sym, state, modifiers); |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 523 | static void |
| 524 | unbind_keyboard(struct wl_resource *resource) |
| 525 | { |
| 526 | struct input_method_context *context = resource->data; |
| 527 | |
| 528 | input_method_context_end_keyboard_grab(context); |
| 529 | context->keyboard = NULL; |
| 530 | |
| 531 | free(resource); |
| 532 | } |
| 533 | |
| 534 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 535 | input_method_context_grab_key(struct weston_keyboard_grab *grab, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 536 | uint32_t time, uint32_t key, uint32_t state_w) |
| 537 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 538 | struct weston_keyboard *keyboard = grab->keyboard; |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 539 | struct wl_display *display; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 540 | uint32_t serial; |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 541 | |
| 542 | if (!keyboard->input_method_resource) |
| 543 | return; |
| 544 | |
| 545 | display = wl_client_get_display(keyboard->input_method_resource->client); |
| 546 | serial = wl_display_next_serial(display); |
| 547 | wl_keyboard_send_key(keyboard->input_method_resource, |
| 548 | serial, time, key, state_w); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 552 | input_method_context_grab_modifier(struct weston_keyboard_grab *grab, uint32_t serial, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 553 | uint32_t mods_depressed, uint32_t mods_latched, |
| 554 | uint32_t mods_locked, uint32_t group) |
| 555 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 556 | struct weston_keyboard *keyboard = grab->keyboard; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 557 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 558 | if (!keyboard->input_method_resource) |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 559 | return; |
| 560 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 561 | wl_keyboard_send_modifiers(keyboard->input_method_resource, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 562 | serial, mods_depressed, mods_latched, |
| 563 | mods_locked, group); |
| 564 | } |
| 565 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 566 | static const struct weston_keyboard_grab_interface input_method_context_grab = { |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 567 | input_method_context_grab_key, |
| 568 | input_method_context_grab_modifier, |
| 569 | }; |
| 570 | |
| 571 | static void |
| 572 | input_method_context_grab_keyboard(struct wl_client *client, |
| 573 | struct wl_resource *resource, |
| 574 | uint32_t id) |
| 575 | { |
| 576 | struct input_method_context *context = resource->data; |
| 577 | struct wl_resource *cr; |
| 578 | struct weston_seat *seat = context->input_method->seat; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 579 | struct weston_keyboard *keyboard = seat->keyboard; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 580 | |
| 581 | cr = wl_client_add_object(client, &wl_keyboard_interface, |
| 582 | NULL, id, context); |
| 583 | cr->destroy = unbind_keyboard; |
| 584 | |
| 585 | context->keyboard = cr; |
| 586 | |
| 587 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 588 | seat->xkb_info.keymap_fd, |
| 589 | seat->xkb_info.keymap_size); |
| 590 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 591 | if (keyboard->grab != &keyboard->default_grab) { |
| 592 | weston_keyboard_end_grab(keyboard); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 593 | } |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 594 | weston_keyboard_start_grab(keyboard, &keyboard->input_method_grab); |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 595 | keyboard->input_method_resource = cr; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 598 | static void |
| 599 | input_method_context_key(struct wl_client *client, |
| 600 | struct wl_resource *resource, |
| 601 | uint32_t serial, |
| 602 | uint32_t time, |
| 603 | uint32_t key, |
| 604 | uint32_t state_w) |
| 605 | { |
| 606 | struct input_method_context *context = resource->data; |
| 607 | struct weston_seat *seat = context->input_method->seat; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 608 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 609 | struct weston_keyboard_grab *default_grab = &keyboard->default_grab; |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 610 | |
| 611 | default_grab->interface->key(default_grab, time, key, state_w); |
| 612 | } |
| 613 | |
| 614 | static void |
| 615 | input_method_context_modifiers(struct wl_client *client, |
| 616 | struct wl_resource *resource, |
| 617 | uint32_t serial, |
| 618 | uint32_t mods_depressed, |
| 619 | uint32_t mods_latched, |
| 620 | uint32_t mods_locked, |
| 621 | uint32_t group) |
| 622 | { |
| 623 | struct input_method_context *context = resource->data; |
| 624 | |
| 625 | struct weston_seat *seat = context->input_method->seat; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 626 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 627 | struct weston_keyboard_grab *default_grab = &keyboard->default_grab; |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 628 | |
| 629 | default_grab->interface->modifiers(default_grab, |
| 630 | serial, mods_depressed, |
| 631 | mods_latched, mods_locked, |
| 632 | group); |
| 633 | } |
| 634 | |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 635 | static void |
| 636 | input_method_context_language(struct wl_client *client, |
| 637 | struct wl_resource *resource, |
| 638 | uint32_t serial, |
| 639 | const char *language) |
| 640 | { |
| 641 | struct input_method_context *context = resource->data; |
| 642 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 643 | wl_text_input_send_language(&context->model->resource, serial, language); |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static void |
| 647 | input_method_context_text_direction(struct wl_client *client, |
| 648 | struct wl_resource *resource, |
| 649 | uint32_t serial, |
| 650 | uint32_t direction) |
| 651 | { |
| 652 | struct input_method_context *context = resource->data; |
| 653 | |
Jan Arne Petersen | 62ece76 | 2013-04-18 16:47:36 +0200 | [diff] [blame] | 654 | wl_text_input_send_text_direction(&context->model->resource, serial, direction); |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 658 | static const struct wl_input_method_context_interface input_method_context_implementation = { |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 659 | input_method_context_destroy, |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 660 | input_method_context_commit_string, |
| 661 | input_method_context_preedit_string, |
Jan Arne Petersen | 4653531 | 2013-01-16 21:26:38 +0100 | [diff] [blame] | 662 | input_method_context_preedit_styling, |
| 663 | input_method_context_preedit_cursor, |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 664 | input_method_context_delete_surrounding_text, |
Jan Arne Petersen | 1cc9e08 | 2013-01-31 15:52:23 +0100 | [diff] [blame] | 665 | input_method_context_cursor_position, |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 666 | input_method_context_modifiers_map, |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 667 | input_method_context_keysym, |
Jan Arne Petersen | 337df95 | 2012-11-18 19:06:46 +0100 | [diff] [blame] | 668 | input_method_context_grab_keyboard, |
| 669 | input_method_context_key, |
Jan Arne Petersen | ece6b5a | 2013-04-18 16:47:15 +0200 | [diff] [blame] | 670 | input_method_context_modifiers, |
| 671 | input_method_context_language, |
| 672 | input_method_context_text_direction |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 673 | }; |
| 674 | |
| 675 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 676 | destroy_input_method_context(struct wl_resource *resource) |
| 677 | { |
| 678 | struct input_method_context *context = resource->data; |
| 679 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 680 | if (context->keyboard) { |
| 681 | wl_resource_destroy(context->keyboard); |
| 682 | } |
| 683 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 684 | free(context); |
| 685 | } |
| 686 | |
| 687 | static void |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 688 | input_method_context_create(struct text_input *model, |
Jan Arne Petersen | 00191c7 | 2013-04-18 16:47:33 +0200 | [diff] [blame] | 689 | struct input_method *input_method) |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 690 | { |
| 691 | struct input_method_context *context; |
| 692 | |
| 693 | if (!input_method->input_method_binding) |
| 694 | return; |
| 695 | |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 696 | context = calloc(1, sizeof *context); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 697 | if (context == NULL) |
| 698 | return; |
| 699 | |
| 700 | context->resource.destroy = destroy_input_method_context; |
| 701 | context->resource.object.id = 0; |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 702 | context->resource.object.interface = &wl_input_method_context_interface; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 703 | context->resource.object.implementation = |
| 704 | (void (**)(void)) &input_method_context_implementation; |
| 705 | context->resource.data = context; |
| 706 | wl_signal_init(&context->resource.destroy_signal); |
| 707 | |
| 708 | context->model = model; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 709 | context->input_method = input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 710 | input_method->context = context; |
| 711 | |
| 712 | wl_client_add_resource(input_method->input_method_binding->client, &context->resource); |
| 713 | |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 714 | wl_input_method_send_activate(input_method->input_method_binding, &context->resource); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static void |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 718 | input_method_context_end_keyboard_grab(struct input_method_context *context) |
| 719 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 720 | struct weston_keyboard_grab *grab = |
| 721 | &context->input_method->seat->keyboard->input_method_grab; |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 722 | struct weston_keyboard *keyboard = grab->keyboard; |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 723 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 724 | if (!grab->keyboard) |
| 725 | return; |
| 726 | |
| 727 | if (grab->keyboard->grab == grab) |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 728 | weston_keyboard_end_grab(grab->keyboard); |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 729 | |
Jan Arne Petersen | a75a789 | 2013-01-16 21:26:50 +0100 | [diff] [blame] | 730 | keyboard->input_method_resource = NULL; |
| 731 | } |
Jan Arne Petersen | 466b9c1 | 2012-11-18 19:06:45 +0100 | [diff] [blame] | 732 | |
| 733 | static void |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 734 | unbind_input_method(struct wl_resource *resource) |
| 735 | { |
| 736 | struct input_method *input_method = resource->data; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 737 | struct text_backend *text_backend = input_method->text_backend; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 738 | |
| 739 | input_method->input_method_binding = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 740 | input_method->context = NULL; |
| 741 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 742 | text_backend->input_method.binding = NULL; |
| 743 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 744 | free(resource); |
| 745 | } |
| 746 | |
| 747 | static void |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 748 | bind_input_method(struct wl_client *client, |
| 749 | void *data, |
| 750 | uint32_t version, |
| 751 | uint32_t id) |
| 752 | { |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 753 | struct input_method *input_method = data; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 754 | struct text_backend *text_backend = input_method->text_backend; |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 755 | struct wl_resource *resource; |
| 756 | |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 757 | resource = wl_client_add_object(client, &wl_input_method_interface, |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 758 | NULL, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 759 | id, input_method); |
| 760 | |
Jan Arne Petersen | b41531a | 2013-04-18 16:47:31 +0200 | [diff] [blame] | 761 | if (input_method->input_method_binding != NULL) { |
| 762 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 763 | "interface object already bound"); |
| 764 | wl_resource_destroy(resource); |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | |
Jan Arne Petersen | b41531a | 2013-04-18 16:47:31 +0200 | [diff] [blame] | 768 | if (text_backend->input_method.client != client) { |
| 769 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 770 | "permission to bind desktop_shell denied"); |
| 771 | wl_resource_destroy(resource); |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | resource->destroy = unbind_input_method; |
| 776 | input_method->input_method_binding = resource; |
| 777 | |
| 778 | text_backend->input_method.binding = resource; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static void |
| 782 | input_method_notifier_destroy(struct wl_listener *listener, void *data) |
| 783 | { |
Kristian Høgsberg | f97f379 | 2012-07-22 11:51:42 -0400 | [diff] [blame] | 784 | struct input_method *input_method = |
| 785 | container_of(listener, struct input_method, destroy_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 786 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 787 | if (input_method->model) |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 788 | deactivate_text_input(input_method->model, input_method); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 789 | |
| 790 | wl_display_remove_global(input_method->seat->compositor->wl_display, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 791 | input_method->input_method_global); |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 792 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 793 | free(input_method); |
| 794 | } |
| 795 | |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 796 | static void |
| 797 | handle_keyboard_focus(struct wl_listener *listener, void *data) |
| 798 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 799 | struct weston_keyboard *keyboard = data; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 800 | struct input_method *input_method = |
| 801 | container_of(listener, struct input_method, keyboard_focus_listener); |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 802 | struct weston_surface *surface = keyboard->focus; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 803 | |
| 804 | if (!input_method->model) |
| 805 | return; |
| 806 | |
| 807 | if (!surface || input_method->model->surface != surface) |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 808 | deactivate_text_input(input_method->model, |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 809 | input_method); |
| 810 | } |
| 811 | |
| 812 | static void |
| 813 | input_method_init_seat(struct weston_seat *seat) |
| 814 | { |
| 815 | if (seat->input_method->focus_listener_initialized) |
| 816 | return; |
| 817 | |
Kristian Høgsberg | 2bf8762 | 2013-05-07 23:17:41 -0400 | [diff] [blame] | 818 | if (seat->keyboard) { |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 819 | seat->input_method->keyboard_focus_listener.notify = handle_keyboard_focus; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 820 | wl_signal_add(&seat->keyboard->focus_signal, &seat->input_method->keyboard_focus_listener); |
| 821 | seat->keyboard->input_method_grab.interface = &input_method_context_grab; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | seat->input_method->focus_listener_initialized = 1; |
| 825 | } |
| 826 | |
Eduardo Lima (Etrunko) | 2733700 | 2013-05-14 13:09:31 -0300 | [diff] [blame] | 827 | static void launch_input_method(struct text_backend *text_backend); |
| 828 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 829 | static void |
| 830 | handle_input_method_sigchld(struct weston_process *process, int status) |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 831 | { |
Eduardo Lima (Etrunko) | 2733700 | 2013-05-14 13:09:31 -0300 | [diff] [blame] | 832 | uint32_t time; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 833 | struct text_backend *text_backend = |
| 834 | container_of(process, struct text_backend, input_method.process); |
| 835 | |
| 836 | text_backend->input_method.process.pid = 0; |
| 837 | text_backend->input_method.client = NULL; |
Eduardo Lima (Etrunko) | 2733700 | 2013-05-14 13:09:31 -0300 | [diff] [blame] | 838 | |
| 839 | /* if input_method dies more than 5 times in 10 seconds, give up */ |
| 840 | time = weston_compositor_get_time(); |
| 841 | if (time - text_backend->input_method.deathstamp > 10000) { |
| 842 | text_backend->input_method.deathstamp = time; |
| 843 | text_backend->input_method.deathcount = 0; |
| 844 | } |
| 845 | |
| 846 | text_backend->input_method.deathcount++; |
| 847 | if (text_backend->input_method.deathcount > 5) { |
| 848 | weston_log("input_method died, giving up.\n"); |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | weston_log("input_method died, respawning...\n"); |
| 853 | launch_input_method(text_backend); |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static void |
| 857 | launch_input_method(struct text_backend *text_backend) |
| 858 | { |
| 859 | if (text_backend->input_method.binding) |
| 860 | return; |
| 861 | |
| 862 | if (!text_backend->input_method.path) |
| 863 | return; |
| 864 | |
| 865 | if (text_backend->input_method.process.pid != 0) |
| 866 | return; |
| 867 | |
| 868 | text_backend->input_method.client = weston_client_launch(text_backend->compositor, |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 869 | &text_backend->input_method.process, |
| 870 | text_backend->input_method.path, |
| 871 | handle_input_method_sigchld); |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 872 | |
| 873 | if (!text_backend->input_method.client) |
| 874 | weston_log("not able to start %s\n", text_backend->input_method.path); |
| 875 | } |
| 876 | |
| 877 | static void |
| 878 | handle_seat_created(struct wl_listener *listener, |
| 879 | void *data) |
| 880 | { |
| 881 | struct weston_seat *seat = data; |
| 882 | struct text_backend *text_backend = |
| 883 | container_of(listener, struct text_backend, |
| 884 | seat_created_listener); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 885 | struct input_method *input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 886 | struct weston_compositor *ec = seat->compositor; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 887 | |
Philipp Brüschweiler | 1746781 | 2012-07-11 22:25:30 +0200 | [diff] [blame] | 888 | input_method = calloc(1, sizeof *input_method); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 889 | |
Philipp Brüschweiler | b13b9ff | 2012-09-09 23:08:31 +0200 | [diff] [blame] | 890 | input_method->seat = seat; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 891 | input_method->model = NULL; |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 892 | input_method->focus_listener_initialized = 0; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 893 | input_method->context = NULL; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 894 | input_method->text_backend = text_backend; |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 895 | |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 896 | input_method->input_method_global = |
| 897 | wl_display_add_global(ec->wl_display, |
Jan Arne Petersen | cc75ec1 | 2013-04-18 16:47:39 +0200 | [diff] [blame] | 898 | &wl_input_method_interface, |
Philipp Brüschweiler | f25602b | 2012-07-11 22:25:31 +0200 | [diff] [blame] | 899 | input_method, bind_input_method); |
| 900 | |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 901 | input_method->destroy_listener.notify = input_method_notifier_destroy; |
Kristian Høgsberg | 4912454 | 2013-05-06 22:27:40 -0400 | [diff] [blame] | 902 | wl_signal_add(&seat->destroy_signal, &input_method->destroy_listener); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 903 | |
| 904 | seat->input_method = input_method; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 905 | |
| 906 | launch_input_method(text_backend); |
Jan Arne Petersen | 1f17be4 | 2012-06-21 21:52:18 +0200 | [diff] [blame] | 907 | } |
Jan Arne Petersen | cd8cdcc | 2012-08-10 16:47:23 +0200 | [diff] [blame] | 908 | |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 909 | static void |
| 910 | text_backend_configuration(struct text_backend *text_backend) |
| 911 | { |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 912 | int config_fd; |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 913 | char *path = NULL; |
| 914 | |
| 915 | struct config_key input_method_keys[] = { |
| 916 | { "path", CONFIG_KEY_STRING, &path } |
| 917 | }; |
| 918 | |
| 919 | struct config_section cs[] = { |
| 920 | { "input-method", input_method_keys, ARRAY_LENGTH(input_method_keys), NULL } |
| 921 | }; |
| 922 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 923 | config_fd = open_config_file("weston.ini"); |
| 924 | parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), text_backend); |
| 925 | close(config_fd); |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 926 | |
| 927 | if (path) |
| 928 | text_backend->input_method.path = path; |
| 929 | else |
| 930 | text_backend->input_method.path = strdup(LIBEXECDIR "/weston-keyboard"); |
| 931 | } |
| 932 | |
| 933 | static void |
| 934 | text_backend_notifier_destroy(struct wl_listener *listener, void *data) |
| 935 | { |
| 936 | struct text_backend *text_backend = |
| 937 | container_of(listener, struct text_backend, destroy_listener); |
| 938 | |
| 939 | if (text_backend->input_method.client) |
| 940 | wl_client_destroy(text_backend->input_method.client); |
| 941 | |
| 942 | free(text_backend->input_method.path); |
| 943 | |
| 944 | free(text_backend); |
| 945 | } |
| 946 | |
| 947 | |
| 948 | WL_EXPORT int |
| 949 | text_backend_init(struct weston_compositor *ec) |
| 950 | { |
| 951 | struct text_backend *text_backend; |
| 952 | |
| 953 | text_backend = calloc(1, sizeof(*text_backend)); |
| 954 | |
| 955 | text_backend->compositor = ec; |
| 956 | |
| 957 | text_backend->seat_created_listener.notify = handle_seat_created; |
| 958 | wl_signal_add(&ec->seat_created_signal, |
| 959 | &text_backend->seat_created_listener); |
| 960 | |
| 961 | text_backend->destroy_listener.notify = text_backend_notifier_destroy; |
| 962 | wl_signal_add(&ec->destroy_signal, &text_backend->destroy_listener); |
| 963 | |
| 964 | text_backend_configuration(text_backend); |
| 965 | |
Jan Arne Petersen | 78d00e4 | 2013-04-18 16:47:24 +0200 | [diff] [blame] | 966 | text_input_manager_create(ec); |
Jan Arne Petersen | 674fd1d | 2012-11-18 19:06:42 +0100 | [diff] [blame] | 967 | |
| 968 | return 0; |
| 969 | } |