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" |
| 33 | #include "desktop-shell-server-protocol.h" |
| 34 | #include "input-method-server-protocol.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame^] | 35 | #include "shared/helpers.h" |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 36 | |
| 37 | struct input_panel_surface { |
| 38 | struct wl_resource *resource; |
| 39 | struct wl_signal destroy_signal; |
| 40 | |
| 41 | struct desktop_shell *shell; |
| 42 | |
| 43 | struct wl_list link; |
| 44 | struct weston_surface *surface; |
| 45 | struct weston_view *view; |
| 46 | struct wl_listener surface_destroy_listener; |
| 47 | |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 48 | struct weston_view_animation *anim; |
| 49 | |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 50 | struct weston_output *output; |
| 51 | uint32_t panel; |
| 52 | }; |
| 53 | |
| 54 | static void |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 55 | input_panel_slide_done(struct weston_view_animation *animation, void *data) |
| 56 | { |
| 57 | struct input_panel_surface *ipsurf = data; |
| 58 | |
| 59 | ipsurf->anim = NULL; |
| 60 | } |
| 61 | |
| 62 | static void |
| 63 | show_input_panel_surface(struct input_panel_surface *ipsurf) |
| 64 | { |
| 65 | struct desktop_shell *shell = ipsurf->shell; |
Manuel Bachmann | 5082ad6 | 2014-04-17 14:04:32 +0200 | [diff] [blame] | 66 | struct weston_seat *seat; |
| 67 | struct weston_surface *focus; |
| 68 | float x, y; |
| 69 | |
| 70 | wl_list_for_each(seat, &shell->compositor->seat_list, link) { |
Pekka Paalanen | a4bac9e | 2014-11-20 10:15:07 +0200 | [diff] [blame] | 71 | if (!seat->keyboard || !seat->keyboard->focus) |
Manuel Bachmann | 5082ad6 | 2014-04-17 14:04:32 +0200 | [diff] [blame] | 72 | continue; |
| 73 | focus = weston_surface_get_main_surface(seat->keyboard->focus); |
| 74 | ipsurf->output = focus->output; |
| 75 | x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2; |
| 76 | y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height; |
| 77 | weston_view_set_position(ipsurf->view, x, y); |
| 78 | } |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 79 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 80 | weston_layer_entry_insert(&shell->input_panel_layer.view_list, |
| 81 | &ipsurf->view->layer_link); |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 82 | weston_view_geometry_dirty(ipsurf->view); |
| 83 | weston_view_update_transform(ipsurf->view); |
| 84 | weston_surface_damage(ipsurf->surface); |
| 85 | |
| 86 | if (ipsurf->anim) |
| 87 | weston_view_animation_destroy(ipsurf->anim); |
| 88 | |
| 89 | ipsurf->anim = |
| 90 | weston_slide_run(ipsurf->view, |
| 91 | ipsurf->surface->height * 0.9, 0, |
| 92 | input_panel_slide_done, ipsurf); |
| 93 | } |
| 94 | |
| 95 | static void |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 96 | show_input_panels(struct wl_listener *listener, void *data) |
| 97 | { |
| 98 | struct desktop_shell *shell = |
| 99 | container_of(listener, struct desktop_shell, |
| 100 | show_input_panel_listener); |
| 101 | struct input_panel_surface *ipsurf, *next; |
| 102 | |
| 103 | shell->text_input.surface = (struct weston_surface*)data; |
| 104 | |
| 105 | if (shell->showing_input_panels) |
| 106 | return; |
| 107 | |
| 108 | shell->showing_input_panels = true; |
| 109 | |
| 110 | if (!shell->locked) |
Manuel Bachmann | 805d2f5 | 2014-03-05 12:21:34 +0100 | [diff] [blame] | 111 | wl_list_insert(&shell->compositor->cursor_layer.link, |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 112 | &shell->input_panel_layer.link); |
| 113 | |
| 114 | wl_list_for_each_safe(ipsurf, next, |
| 115 | &shell->input_panel.surfaces, link) { |
Kristian Høgsberg | 2eebcd3 | 2014-01-02 01:27:06 -0800 | [diff] [blame] | 116 | if (ipsurf->surface->width == 0) |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 117 | continue; |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 118 | |
| 119 | show_input_panel_surface(ipsurf); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | hide_input_panels(struct wl_listener *listener, void *data) |
| 125 | { |
| 126 | struct desktop_shell *shell = |
| 127 | container_of(listener, struct desktop_shell, |
| 128 | hide_input_panel_listener); |
| 129 | struct weston_view *view, *next; |
| 130 | |
| 131 | if (!shell->showing_input_panels) |
| 132 | return; |
| 133 | |
| 134 | shell->showing_input_panels = false; |
| 135 | |
| 136 | if (!shell->locked) |
| 137 | wl_list_remove(&shell->input_panel_layer.link); |
| 138 | |
| 139 | wl_list_for_each_safe(view, next, |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 140 | &shell->input_panel_layer.view_list.link, |
| 141 | layer_link.link) |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 142 | weston_view_unmap(view); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | update_input_panels(struct wl_listener *listener, void *data) |
| 147 | { |
| 148 | struct desktop_shell *shell = |
| 149 | container_of(listener, struct desktop_shell, |
| 150 | update_input_panel_listener); |
| 151 | |
| 152 | memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t)); |
| 153 | } |
| 154 | |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 155 | static int |
| 156 | input_panel_get_label(struct weston_surface *surface, char *buf, size_t len) |
| 157 | { |
| 158 | return snprintf(buf, len, "input panel"); |
| 159 | } |
| 160 | |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 161 | static void |
| 162 | input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy) |
| 163 | { |
| 164 | struct input_panel_surface *ip_surface = surface->configure_private; |
| 165 | struct desktop_shell *shell = ip_surface->shell; |
U. Artie Eoff | c4c7a4f | 2014-01-15 14:03:56 -0800 | [diff] [blame] | 166 | struct weston_view *view; |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 167 | float x, y; |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 168 | |
| 169 | if (surface->width == 0) |
| 170 | return; |
| 171 | |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 172 | if (ip_surface->panel) { |
U. Artie Eoff | c4c7a4f | 2014-01-15 14:03:56 -0800 | [diff] [blame] | 173 | view = get_default_view(shell->text_input.surface); |
| 174 | if (view == NULL) |
| 175 | return; |
| 176 | x = view->geometry.x + shell->text_input.cursor_rectangle.x2; |
| 177 | y = view->geometry.y + shell->text_input.cursor_rectangle.y2; |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 178 | } else { |
| 179 | x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2; |
| 180 | y = ip_surface->output->y + ip_surface->output->height - surface->height; |
| 181 | } |
| 182 | |
| 183 | weston_view_set_position(ip_surface->view, x, y); |
| 184 | |
Ander Conselvan de Oliveira | 75c373c | 2014-04-14 15:48:07 +0300 | [diff] [blame] | 185 | if (!weston_surface_is_mapped(surface) && shell->showing_input_panels) |
| 186 | show_input_panel_surface(ip_surface); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static void |
| 190 | destroy_input_panel_surface(struct input_panel_surface *input_panel_surface) |
| 191 | { |
| 192 | wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface); |
| 193 | |
| 194 | wl_list_remove(&input_panel_surface->surface_destroy_listener.link); |
| 195 | wl_list_remove(&input_panel_surface->link); |
| 196 | |
| 197 | input_panel_surface->surface->configure = NULL; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 198 | weston_surface_set_label_func(input_panel_surface->surface, NULL); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 199 | weston_view_destroy(input_panel_surface->view); |
| 200 | |
| 201 | free(input_panel_surface); |
| 202 | } |
| 203 | |
| 204 | static struct input_panel_surface * |
| 205 | get_input_panel_surface(struct weston_surface *surface) |
| 206 | { |
| 207 | if (surface->configure == input_panel_configure) { |
| 208 | return surface->configure_private; |
| 209 | } else { |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | input_panel_handle_surface_destroy(struct wl_listener *listener, void *data) |
| 216 | { |
| 217 | struct input_panel_surface *ipsurface = container_of(listener, |
| 218 | struct input_panel_surface, |
| 219 | surface_destroy_listener); |
| 220 | |
| 221 | if (ipsurface->resource) { |
| 222 | wl_resource_destroy(ipsurface->resource); |
| 223 | } else { |
| 224 | destroy_input_panel_surface(ipsurface); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static struct input_panel_surface * |
| 229 | create_input_panel_surface(struct desktop_shell *shell, |
| 230 | struct weston_surface *surface) |
| 231 | { |
| 232 | struct input_panel_surface *input_panel_surface; |
| 233 | |
| 234 | input_panel_surface = calloc(1, sizeof *input_panel_surface); |
| 235 | if (!input_panel_surface) |
| 236 | return NULL; |
| 237 | |
| 238 | surface->configure = input_panel_configure; |
| 239 | surface->configure_private = input_panel_surface; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 240 | weston_surface_set_label_func(surface, input_panel_get_label); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 241 | |
| 242 | input_panel_surface->shell = shell; |
| 243 | |
| 244 | input_panel_surface->surface = surface; |
| 245 | input_panel_surface->view = weston_view_create(surface); |
| 246 | |
| 247 | wl_signal_init(&input_panel_surface->destroy_signal); |
| 248 | input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy; |
| 249 | wl_signal_add(&surface->destroy_signal, |
| 250 | &input_panel_surface->surface_destroy_listener); |
| 251 | |
| 252 | wl_list_init(&input_panel_surface->link); |
| 253 | |
| 254 | return input_panel_surface; |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | input_panel_surface_set_toplevel(struct wl_client *client, |
| 259 | struct wl_resource *resource, |
| 260 | struct wl_resource *output_resource, |
| 261 | uint32_t position) |
| 262 | { |
| 263 | struct input_panel_surface *input_panel_surface = |
| 264 | wl_resource_get_user_data(resource); |
| 265 | struct desktop_shell *shell = input_panel_surface->shell; |
| 266 | |
| 267 | wl_list_insert(&shell->input_panel.surfaces, |
| 268 | &input_panel_surface->link); |
| 269 | |
| 270 | input_panel_surface->output = wl_resource_get_user_data(output_resource); |
| 271 | input_panel_surface->panel = 0; |
| 272 | } |
| 273 | |
| 274 | static void |
| 275 | input_panel_surface_set_overlay_panel(struct wl_client *client, |
| 276 | struct wl_resource *resource) |
| 277 | { |
| 278 | struct input_panel_surface *input_panel_surface = |
| 279 | wl_resource_get_user_data(resource); |
| 280 | struct desktop_shell *shell = input_panel_surface->shell; |
| 281 | |
| 282 | wl_list_insert(&shell->input_panel.surfaces, |
| 283 | &input_panel_surface->link); |
| 284 | |
| 285 | input_panel_surface->panel = 1; |
| 286 | } |
| 287 | |
| 288 | static const struct wl_input_panel_surface_interface input_panel_surface_implementation = { |
| 289 | input_panel_surface_set_toplevel, |
| 290 | input_panel_surface_set_overlay_panel |
| 291 | }; |
| 292 | |
| 293 | static void |
| 294 | destroy_input_panel_surface_resource(struct wl_resource *resource) |
| 295 | { |
| 296 | struct input_panel_surface *ipsurf = |
| 297 | wl_resource_get_user_data(resource); |
| 298 | |
| 299 | destroy_input_panel_surface(ipsurf); |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | input_panel_get_input_panel_surface(struct wl_client *client, |
| 304 | struct wl_resource *resource, |
| 305 | uint32_t id, |
| 306 | struct wl_resource *surface_resource) |
| 307 | { |
| 308 | struct weston_surface *surface = |
| 309 | wl_resource_get_user_data(surface_resource); |
| 310 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 311 | struct input_panel_surface *ipsurf; |
| 312 | |
| 313 | if (get_input_panel_surface(surface)) { |
| 314 | wl_resource_post_error(surface_resource, |
| 315 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 316 | "wl_input_panel::get_input_panel_surface already requested"); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | ipsurf = create_input_panel_surface(shell, surface); |
| 321 | if (!ipsurf) { |
| 322 | wl_resource_post_error(surface_resource, |
| 323 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 324 | "surface->configure already set"); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | ipsurf->resource = |
| 329 | wl_resource_create(client, |
| 330 | &wl_input_panel_surface_interface, 1, id); |
| 331 | wl_resource_set_implementation(ipsurf->resource, |
| 332 | &input_panel_surface_implementation, |
| 333 | ipsurf, |
| 334 | destroy_input_panel_surface_resource); |
| 335 | } |
| 336 | |
| 337 | static const struct wl_input_panel_interface input_panel_implementation = { |
| 338 | input_panel_get_input_panel_surface |
| 339 | }; |
| 340 | |
| 341 | static void |
| 342 | unbind_input_panel(struct wl_resource *resource) |
| 343 | { |
| 344 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 345 | |
| 346 | shell->input_panel.binding = NULL; |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | bind_input_panel(struct wl_client *client, |
| 351 | void *data, uint32_t version, uint32_t id) |
| 352 | { |
| 353 | struct desktop_shell *shell = data; |
| 354 | struct wl_resource *resource; |
| 355 | |
| 356 | resource = wl_resource_create(client, |
| 357 | &wl_input_panel_interface, 1, id); |
| 358 | |
| 359 | if (shell->input_panel.binding == NULL) { |
| 360 | wl_resource_set_implementation(resource, |
| 361 | &input_panel_implementation, |
| 362 | shell, unbind_input_panel); |
| 363 | shell->input_panel.binding = resource; |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 368 | "interface object already bound"); |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void |
| 372 | input_panel_destroy(struct desktop_shell *shell) |
| 373 | { |
| 374 | wl_list_remove(&shell->show_input_panel_listener.link); |
| 375 | wl_list_remove(&shell->hide_input_panel_listener.link); |
| 376 | } |
| 377 | |
| 378 | int |
| 379 | input_panel_setup(struct desktop_shell *shell) |
| 380 | { |
| 381 | struct weston_compositor *ec = shell->compositor; |
| 382 | |
| 383 | shell->show_input_panel_listener.notify = show_input_panels; |
| 384 | wl_signal_add(&ec->show_input_panel_signal, |
| 385 | &shell->show_input_panel_listener); |
| 386 | shell->hide_input_panel_listener.notify = hide_input_panels; |
| 387 | wl_signal_add(&ec->hide_input_panel_signal, |
| 388 | &shell->hide_input_panel_listener); |
| 389 | shell->update_input_panel_listener.notify = update_input_panels; |
| 390 | wl_signal_add(&ec->update_input_panel_signal, |
| 391 | &shell->update_input_panel_listener); |
| 392 | |
| 393 | wl_list_init(&shell->input_panel.surfaces); |
| 394 | |
| 395 | if (wl_global_create(shell->compositor->wl_display, |
| 396 | &wl_input_panel_interface, 1, |
| 397 | shell, bind_input_panel) == NULL) |
| 398 | return -1; |
| 399 | |
| 400 | return 0; |
| 401 | } |