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