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