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