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