Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010-2012 Intel Corporation |
| 3 | * Copyright © 2011-2012 Collabora, Ltd. |
| 4 | * Copyright © 2013 Raspberry Pi Foundation |
| 5 | * |
Bryce Harrington | af637c2 | 2015-06-11 12:55:55 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 12 | * |
Bryce Harrington | af637c2 | 2015-06-11 12:55:55 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | * DEALINGS IN THE SOFTWARE. |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include "shell.h" |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame] | 33 | #include "input-method-unstable-v1-server-protocol.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 34 | #include "shared/helpers.h" |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 35 | |
| 36 | struct input_panel_surface { |
| 37 | struct wl_resource *resource; |
| 38 | struct wl_signal destroy_signal; |
| 39 | |
| 40 | struct desktop_shell *shell; |
| 41 | |
| 42 | struct wl_list link; |
| 43 | struct weston_surface *surface; |
| 44 | struct weston_view *view; |
| 45 | struct wl_listener surface_destroy_listener; |
| 46 | |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 47 | struct weston_view_animation *anim; |
| 48 | |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 49 | struct weston_output *output; |
| 50 | uint32_t panel; |
| 51 | }; |
| 52 | |
| 53 | static void |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 54 | input_panel_slide_done(struct weston_view_animation *animation, void *data) |
| 55 | { |
| 56 | struct input_panel_surface *ipsurf = data; |
| 57 | |
| 58 | ipsurf->anim = NULL; |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | show_input_panel_surface(struct input_panel_surface *ipsurf) |
| 63 | { |
| 64 | struct desktop_shell *shell = ipsurf->shell; |
Manuel Bachmann | 5082ad6 | 2014-04-17 14:04:32 +0200 | [diff] [blame] | 65 | struct weston_seat *seat; |
| 66 | struct weston_surface *focus; |
| 67 | float x, y; |
| 68 | |
| 69 | wl_list_for_each(seat, &shell->compositor->seat_list, link) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 70 | struct weston_keyboard *keyboard = |
| 71 | weston_seat_get_keyboard(seat); |
| 72 | |
| 73 | if (!keyboard || !keyboard->focus) |
Manuel Bachmann | 5082ad6 | 2014-04-17 14:04:32 +0200 | [diff] [blame] | 74 | continue; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 75 | focus = weston_surface_get_main_surface(keyboard->focus); |
Nicolas Guyomard | 4845354 | 2015-10-06 10:40:28 -0500 | [diff] [blame] | 76 | if (!focus) |
| 77 | continue; |
Manuel Bachmann | 5082ad6 | 2014-04-17 14:04:32 +0200 | [diff] [blame] | 78 | ipsurf->output = focus->output; |
| 79 | x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2; |
| 80 | y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height; |
| 81 | weston_view_set_position(ipsurf->view, x, y); |
| 82 | } |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 83 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 84 | weston_layer_entry_insert(&shell->input_panel_layer.view_list, |
| 85 | &ipsurf->view->layer_link); |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 86 | weston_view_geometry_dirty(ipsurf->view); |
| 87 | weston_view_update_transform(ipsurf->view); |
| 88 | weston_surface_damage(ipsurf->surface); |
| 89 | |
| 90 | if (ipsurf->anim) |
| 91 | weston_view_animation_destroy(ipsurf->anim); |
| 92 | |
| 93 | ipsurf->anim = |
| 94 | weston_slide_run(ipsurf->view, |
| 95 | ipsurf->surface->height * 0.9, 0, |
| 96 | input_panel_slide_done, ipsurf); |
| 97 | } |
| 98 | |
| 99 | static void |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 100 | show_input_panels(struct wl_listener *listener, void *data) |
| 101 | { |
| 102 | struct desktop_shell *shell = |
| 103 | container_of(listener, struct desktop_shell, |
| 104 | show_input_panel_listener); |
| 105 | struct input_panel_surface *ipsurf, *next; |
| 106 | |
| 107 | shell->text_input.surface = (struct weston_surface*)data; |
| 108 | |
| 109 | if (shell->showing_input_panels) |
| 110 | return; |
| 111 | |
| 112 | shell->showing_input_panels = true; |
| 113 | |
| 114 | if (!shell->locked) |
Manuel Bachmann | 805d2f5 | 2014-03-05 12:21:34 +0100 | [diff] [blame] | 115 | wl_list_insert(&shell->compositor->cursor_layer.link, |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 116 | &shell->input_panel_layer.link); |
| 117 | |
| 118 | wl_list_for_each_safe(ipsurf, next, |
| 119 | &shell->input_panel.surfaces, link) { |
Kristian Høgsberg | 2eebcd3 | 2014-01-02 01:27:06 -0800 | [diff] [blame] | 120 | if (ipsurf->surface->width == 0) |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 121 | continue; |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 122 | |
| 123 | show_input_panel_surface(ipsurf); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | hide_input_panels(struct wl_listener *listener, void *data) |
| 129 | { |
| 130 | struct desktop_shell *shell = |
| 131 | container_of(listener, struct desktop_shell, |
| 132 | hide_input_panel_listener); |
| 133 | struct weston_view *view, *next; |
| 134 | |
| 135 | if (!shell->showing_input_panels) |
| 136 | return; |
| 137 | |
| 138 | shell->showing_input_panels = false; |
| 139 | |
| 140 | if (!shell->locked) |
| 141 | wl_list_remove(&shell->input_panel_layer.link); |
| 142 | |
| 143 | wl_list_for_each_safe(view, next, |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 144 | &shell->input_panel_layer.view_list.link, |
| 145 | layer_link.link) |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 146 | weston_view_unmap(view); |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | update_input_panels(struct wl_listener *listener, void *data) |
| 151 | { |
| 152 | struct desktop_shell *shell = |
| 153 | container_of(listener, struct desktop_shell, |
| 154 | update_input_panel_listener); |
| 155 | |
| 156 | memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t)); |
| 157 | } |
| 158 | |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 159 | static int |
| 160 | input_panel_get_label(struct weston_surface *surface, char *buf, size_t len) |
| 161 | { |
| 162 | return snprintf(buf, len, "input panel"); |
| 163 | } |
| 164 | |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 165 | static void |
| 166 | input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy) |
| 167 | { |
| 168 | struct input_panel_surface *ip_surface = surface->configure_private; |
| 169 | struct desktop_shell *shell = ip_surface->shell; |
U. Artie Eoff | c4c7a4f | 2014-01-15 14:03:56 -0800 | [diff] [blame] | 170 | struct weston_view *view; |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 171 | float x, y; |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 172 | |
| 173 | if (surface->width == 0) |
| 174 | return; |
| 175 | |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 176 | if (ip_surface->panel) { |
U. Artie Eoff | c4c7a4f | 2014-01-15 14:03:56 -0800 | [diff] [blame] | 177 | view = get_default_view(shell->text_input.surface); |
| 178 | if (view == NULL) |
| 179 | return; |
| 180 | x = view->geometry.x + shell->text_input.cursor_rectangle.x2; |
| 181 | y = view->geometry.y + shell->text_input.cursor_rectangle.y2; |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 182 | } else { |
| 183 | x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2; |
| 184 | y = ip_surface->output->y + ip_surface->output->height - surface->height; |
| 185 | } |
| 186 | |
| 187 | weston_view_set_position(ip_surface->view, x, y); |
| 188 | |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 189 | if (!weston_surface_is_mapped(surface) && shell->showing_input_panels) |
| 190 | show_input_panel_surface(ip_surface); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void |
| 194 | destroy_input_panel_surface(struct input_panel_surface *input_panel_surface) |
| 195 | { |
| 196 | wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface); |
| 197 | |
| 198 | wl_list_remove(&input_panel_surface->surface_destroy_listener.link); |
| 199 | wl_list_remove(&input_panel_surface->link); |
| 200 | |
| 201 | input_panel_surface->surface->configure = NULL; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 202 | weston_surface_set_label_func(input_panel_surface->surface, NULL); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 203 | weston_view_destroy(input_panel_surface->view); |
| 204 | |
| 205 | free(input_panel_surface); |
| 206 | } |
| 207 | |
| 208 | static struct input_panel_surface * |
| 209 | get_input_panel_surface(struct weston_surface *surface) |
| 210 | { |
| 211 | if (surface->configure == input_panel_configure) { |
| 212 | return surface->configure_private; |
| 213 | } else { |
| 214 | return NULL; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | input_panel_handle_surface_destroy(struct wl_listener *listener, void *data) |
| 220 | { |
| 221 | struct input_panel_surface *ipsurface = container_of(listener, |
| 222 | struct input_panel_surface, |
| 223 | surface_destroy_listener); |
| 224 | |
| 225 | if (ipsurface->resource) { |
| 226 | wl_resource_destroy(ipsurface->resource); |
| 227 | } else { |
| 228 | destroy_input_panel_surface(ipsurface); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static struct input_panel_surface * |
| 233 | create_input_panel_surface(struct desktop_shell *shell, |
| 234 | struct weston_surface *surface) |
| 235 | { |
| 236 | struct input_panel_surface *input_panel_surface; |
| 237 | |
| 238 | input_panel_surface = calloc(1, sizeof *input_panel_surface); |
| 239 | if (!input_panel_surface) |
| 240 | return NULL; |
| 241 | |
| 242 | surface->configure = input_panel_configure; |
| 243 | surface->configure_private = input_panel_surface; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 244 | weston_surface_set_label_func(surface, input_panel_get_label); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 245 | |
| 246 | input_panel_surface->shell = shell; |
| 247 | |
| 248 | input_panel_surface->surface = surface; |
| 249 | input_panel_surface->view = weston_view_create(surface); |
| 250 | |
| 251 | wl_signal_init(&input_panel_surface->destroy_signal); |
| 252 | input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy; |
| 253 | wl_signal_add(&surface->destroy_signal, |
| 254 | &input_panel_surface->surface_destroy_listener); |
| 255 | |
| 256 | wl_list_init(&input_panel_surface->link); |
| 257 | |
| 258 | return input_panel_surface; |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | input_panel_surface_set_toplevel(struct wl_client *client, |
| 263 | struct wl_resource *resource, |
| 264 | struct wl_resource *output_resource, |
| 265 | uint32_t position) |
| 266 | { |
| 267 | struct input_panel_surface *input_panel_surface = |
| 268 | wl_resource_get_user_data(resource); |
| 269 | struct desktop_shell *shell = input_panel_surface->shell; |
| 270 | |
| 271 | wl_list_insert(&shell->input_panel.surfaces, |
| 272 | &input_panel_surface->link); |
| 273 | |
| 274 | input_panel_surface->output = wl_resource_get_user_data(output_resource); |
| 275 | input_panel_surface->panel = 0; |
| 276 | } |
| 277 | |
| 278 | static void |
| 279 | input_panel_surface_set_overlay_panel(struct wl_client *client, |
| 280 | struct wl_resource *resource) |
| 281 | { |
| 282 | struct input_panel_surface *input_panel_surface = |
| 283 | wl_resource_get_user_data(resource); |
| 284 | struct desktop_shell *shell = input_panel_surface->shell; |
| 285 | |
| 286 | wl_list_insert(&shell->input_panel.surfaces, |
| 287 | &input_panel_surface->link); |
| 288 | |
| 289 | input_panel_surface->panel = 1; |
| 290 | } |
| 291 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame] | 292 | static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = { |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 293 | input_panel_surface_set_toplevel, |
| 294 | input_panel_surface_set_overlay_panel |
| 295 | }; |
| 296 | |
| 297 | static void |
| 298 | destroy_input_panel_surface_resource(struct wl_resource *resource) |
| 299 | { |
| 300 | struct input_panel_surface *ipsurf = |
| 301 | wl_resource_get_user_data(resource); |
| 302 | |
| 303 | destroy_input_panel_surface(ipsurf); |
| 304 | } |
| 305 | |
| 306 | static void |
| 307 | input_panel_get_input_panel_surface(struct wl_client *client, |
| 308 | struct wl_resource *resource, |
| 309 | uint32_t id, |
| 310 | struct wl_resource *surface_resource) |
| 311 | { |
| 312 | struct weston_surface *surface = |
| 313 | wl_resource_get_user_data(surface_resource); |
| 314 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 315 | struct input_panel_surface *ipsurf; |
| 316 | |
| 317 | if (get_input_panel_surface(surface)) { |
| 318 | wl_resource_post_error(surface_resource, |
| 319 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 320 | "wl_input_panel::get_input_panel_surface already requested"); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | ipsurf = create_input_panel_surface(shell, surface); |
| 325 | if (!ipsurf) { |
| 326 | wl_resource_post_error(surface_resource, |
| 327 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 328 | "surface->configure already set"); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | ipsurf->resource = |
| 333 | wl_resource_create(client, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame] | 334 | &zwp_input_panel_surface_v1_interface, |
| 335 | 1, |
| 336 | id); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 337 | wl_resource_set_implementation(ipsurf->resource, |
| 338 | &input_panel_surface_implementation, |
| 339 | ipsurf, |
| 340 | destroy_input_panel_surface_resource); |
| 341 | } |
| 342 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame] | 343 | static const struct zwp_input_panel_v1_interface input_panel_implementation = { |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 344 | input_panel_get_input_panel_surface |
| 345 | }; |
| 346 | |
| 347 | static void |
| 348 | unbind_input_panel(struct wl_resource *resource) |
| 349 | { |
| 350 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 351 | |
| 352 | shell->input_panel.binding = NULL; |
| 353 | } |
| 354 | |
| 355 | static void |
| 356 | bind_input_panel(struct wl_client *client, |
| 357 | void *data, uint32_t version, uint32_t id) |
| 358 | { |
| 359 | struct desktop_shell *shell = data; |
| 360 | struct wl_resource *resource; |
| 361 | |
| 362 | resource = wl_resource_create(client, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame] | 363 | &zwp_input_panel_v1_interface, 1, id); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 364 | |
| 365 | if (shell->input_panel.binding == NULL) { |
| 366 | wl_resource_set_implementation(resource, |
| 367 | &input_panel_implementation, |
| 368 | shell, unbind_input_panel); |
| 369 | shell->input_panel.binding = resource; |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 374 | "interface object already bound"); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void |
| 378 | input_panel_destroy(struct desktop_shell *shell) |
| 379 | { |
| 380 | wl_list_remove(&shell->show_input_panel_listener.link); |
| 381 | wl_list_remove(&shell->hide_input_panel_listener.link); |
| 382 | } |
| 383 | |
| 384 | int |
| 385 | input_panel_setup(struct desktop_shell *shell) |
| 386 | { |
| 387 | struct weston_compositor *ec = shell->compositor; |
| 388 | |
| 389 | shell->show_input_panel_listener.notify = show_input_panels; |
| 390 | wl_signal_add(&ec->show_input_panel_signal, |
| 391 | &shell->show_input_panel_listener); |
| 392 | shell->hide_input_panel_listener.notify = hide_input_panels; |
| 393 | wl_signal_add(&ec->hide_input_panel_signal, |
| 394 | &shell->hide_input_panel_listener); |
| 395 | shell->update_input_panel_listener.notify = update_input_panels; |
| 396 | wl_signal_add(&ec->update_input_panel_signal, |
| 397 | &shell->update_input_panel_listener); |
| 398 | |
| 399 | wl_list_init(&shell->input_panel.surfaces); |
| 400 | |
| 401 | if (wl_global_create(shell->compositor->wl_display, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame] | 402 | &zwp_input_panel_v1_interface, 1, |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 403 | shell, bind_input_panel) == NULL) |
| 404 | return -1; |
| 405 | |
| 406 | return 0; |
| 407 | } |