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" |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 35 | #include "input-method-unstable-v1-server-protocol.h" |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 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) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 73 | struct weston_keyboard *keyboard = |
| 74 | weston_seat_get_keyboard(seat); |
| 75 | |
| 76 | if (!keyboard || !keyboard->focus) |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 77 | continue; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 78 | focus = weston_surface_get_main_surface(keyboard->focus); |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 79 | ipsurf->output = focus->output; |
| 80 | x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2; |
| 81 | y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height; |
| 82 | weston_view_set_position(ipsurf->view, x, y); |
| 83 | } |
| 84 | |
| 85 | weston_layer_entry_insert(&shell->input_panel_layer.view_list, |
| 86 | &ipsurf->view->layer_link); |
| 87 | weston_view_geometry_dirty(ipsurf->view); |
| 88 | weston_view_update_transform(ipsurf->view); |
| 89 | weston_surface_damage(ipsurf->surface); |
| 90 | |
| 91 | if (ipsurf->anim) |
| 92 | weston_view_animation_destroy(ipsurf->anim); |
| 93 | |
| 94 | ipsurf->anim = |
| 95 | weston_slide_run(ipsurf->view, |
| 96 | ipsurf->surface->height * 0.9, 0, |
| 97 | input_panel_slide_done, ipsurf); |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | show_input_panels(struct wl_listener *listener, void *data) |
| 102 | { |
| 103 | struct ivi_shell *shell = |
| 104 | container_of(listener, struct ivi_shell, |
| 105 | show_input_panel_listener); |
| 106 | struct input_panel_surface *ipsurf, *next; |
| 107 | |
| 108 | shell->text_input.surface = (struct weston_surface*)data; |
| 109 | |
| 110 | if (shell->showing_input_panels) |
| 111 | return; |
| 112 | |
| 113 | shell->showing_input_panels = true; |
| 114 | |
| 115 | if (!shell->locked) |
| 116 | wl_list_insert(&shell->compositor->cursor_layer.link, |
| 117 | &shell->input_panel_layer.link); |
| 118 | |
| 119 | wl_list_for_each_safe(ipsurf, next, |
| 120 | &shell->input_panel.surfaces, link) { |
| 121 | if (ipsurf->surface->width == 0) |
| 122 | continue; |
| 123 | |
| 124 | show_input_panel_surface(ipsurf); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | hide_input_panels(struct wl_listener *listener, void *data) |
| 130 | { |
| 131 | struct ivi_shell *shell = |
| 132 | container_of(listener, struct ivi_shell, |
| 133 | hide_input_panel_listener); |
| 134 | struct weston_view *view, *next; |
| 135 | |
| 136 | if (!shell->showing_input_panels) |
| 137 | return; |
| 138 | |
| 139 | shell->showing_input_panels = false; |
| 140 | |
| 141 | if (!shell->locked) |
| 142 | wl_list_remove(&shell->input_panel_layer.link); |
| 143 | |
| 144 | wl_list_for_each_safe(view, next, |
| 145 | &shell->input_panel_layer.view_list.link, |
| 146 | layer_link.link) |
| 147 | weston_view_unmap(view); |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | update_input_panels(struct wl_listener *listener, void *data) |
| 152 | { |
| 153 | struct ivi_shell *shell = |
| 154 | container_of(listener, struct ivi_shell, |
| 155 | update_input_panel_listener); |
| 156 | |
| 157 | memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t)); |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy) |
| 162 | { |
| 163 | struct input_panel_surface *ip_surface = surface->configure_private; |
| 164 | struct ivi_shell *shell = ip_surface->shell; |
| 165 | struct weston_view *view; |
| 166 | float x, y; |
| 167 | |
| 168 | if (surface->width == 0) |
| 169 | return; |
| 170 | |
| 171 | if (ip_surface->panel) { |
| 172 | view = get_default_view(shell->text_input.surface); |
| 173 | if (view == NULL) |
| 174 | return; |
| 175 | x = view->geometry.x + shell->text_input.cursor_rectangle.x2; |
| 176 | y = view->geometry.y + shell->text_input.cursor_rectangle.y2; |
| 177 | } else { |
| 178 | x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2; |
| 179 | y = ip_surface->output->y + ip_surface->output->height - surface->height; |
| 180 | } |
| 181 | |
| 182 | weston_view_set_position(ip_surface->view, x, y); |
| 183 | |
| 184 | if (!weston_surface_is_mapped(surface) && shell->showing_input_panels) |
| 185 | show_input_panel_surface(ip_surface); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | destroy_input_panel_surface(struct input_panel_surface *input_panel_surface) |
| 190 | { |
| 191 | wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface); |
| 192 | |
| 193 | wl_list_remove(&input_panel_surface->surface_destroy_listener.link); |
| 194 | wl_list_remove(&input_panel_surface->link); |
| 195 | |
| 196 | input_panel_surface->surface->configure = NULL; |
| 197 | weston_view_destroy(input_panel_surface->view); |
| 198 | |
| 199 | free(input_panel_surface); |
| 200 | } |
| 201 | |
| 202 | static struct input_panel_surface * |
| 203 | get_input_panel_surface(struct weston_surface *surface) |
| 204 | { |
| 205 | if (surface->configure == input_panel_configure) { |
| 206 | return surface->configure_private; |
| 207 | } else { |
| 208 | return NULL; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void |
| 213 | input_panel_handle_surface_destroy(struct wl_listener *listener, void *data) |
| 214 | { |
| 215 | struct input_panel_surface *ipsurface = container_of(listener, |
| 216 | struct input_panel_surface, |
| 217 | surface_destroy_listener); |
| 218 | |
| 219 | if (ipsurface->resource) { |
| 220 | wl_resource_destroy(ipsurface->resource); |
| 221 | } else { |
| 222 | destroy_input_panel_surface(ipsurface); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static struct input_panel_surface * |
| 227 | create_input_panel_surface(struct ivi_shell *shell, |
| 228 | struct weston_surface *surface) |
| 229 | { |
| 230 | struct input_panel_surface *input_panel_surface; |
| 231 | |
| 232 | input_panel_surface = calloc(1, sizeof *input_panel_surface); |
| 233 | if (!input_panel_surface) |
| 234 | return NULL; |
| 235 | |
| 236 | surface->configure = input_panel_configure; |
| 237 | surface->configure_private = input_panel_surface; |
| 238 | |
| 239 | input_panel_surface->shell = shell; |
| 240 | |
| 241 | input_panel_surface->surface = surface; |
| 242 | input_panel_surface->view = weston_view_create(surface); |
| 243 | |
| 244 | wl_signal_init(&input_panel_surface->destroy_signal); |
| 245 | input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy; |
| 246 | wl_signal_add(&surface->destroy_signal, |
| 247 | &input_panel_surface->surface_destroy_listener); |
| 248 | |
| 249 | wl_list_init(&input_panel_surface->link); |
| 250 | |
| 251 | return input_panel_surface; |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | input_panel_surface_set_toplevel(struct wl_client *client, |
| 256 | struct wl_resource *resource, |
| 257 | struct wl_resource *output_resource, |
| 258 | uint32_t position) |
| 259 | { |
| 260 | struct input_panel_surface *input_panel_surface = |
| 261 | wl_resource_get_user_data(resource); |
| 262 | struct ivi_shell *shell = input_panel_surface->shell; |
| 263 | |
| 264 | wl_list_insert(&shell->input_panel.surfaces, |
| 265 | &input_panel_surface->link); |
| 266 | |
| 267 | input_panel_surface->output = wl_resource_get_user_data(output_resource); |
| 268 | input_panel_surface->panel = 0; |
| 269 | } |
| 270 | |
| 271 | static void |
| 272 | input_panel_surface_set_overlay_panel(struct wl_client *client, |
| 273 | struct wl_resource *resource) |
| 274 | { |
| 275 | struct input_panel_surface *input_panel_surface = |
| 276 | wl_resource_get_user_data(resource); |
| 277 | struct ivi_shell *shell = input_panel_surface->shell; |
| 278 | |
| 279 | wl_list_insert(&shell->input_panel.surfaces, |
| 280 | &input_panel_surface->link); |
| 281 | |
| 282 | input_panel_surface->panel = 1; |
| 283 | } |
| 284 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 285 | static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = { |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 286 | input_panel_surface_set_toplevel, |
| 287 | input_panel_surface_set_overlay_panel |
| 288 | }; |
| 289 | |
| 290 | static void |
| 291 | destroy_input_panel_surface_resource(struct wl_resource *resource) |
| 292 | { |
| 293 | struct input_panel_surface *ipsurf = |
| 294 | wl_resource_get_user_data(resource); |
| 295 | |
| 296 | destroy_input_panel_surface(ipsurf); |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | input_panel_get_input_panel_surface(struct wl_client *client, |
| 301 | struct wl_resource *resource, |
| 302 | uint32_t id, |
| 303 | struct wl_resource *surface_resource) |
| 304 | { |
| 305 | struct weston_surface *surface = |
| 306 | wl_resource_get_user_data(surface_resource); |
| 307 | struct ivi_shell *shell = wl_resource_get_user_data(resource); |
| 308 | struct input_panel_surface *ipsurf; |
| 309 | |
| 310 | if (get_input_panel_surface(surface)) { |
| 311 | wl_resource_post_error(surface_resource, |
| 312 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 313 | "wl_input_panel::get_input_panel_surface already requested"); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | ipsurf = create_input_panel_surface(shell, surface); |
| 318 | if (!ipsurf) { |
| 319 | wl_resource_post_error(surface_resource, |
| 320 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 321 | "surface->configure already set"); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | ipsurf->resource = |
| 326 | wl_resource_create(client, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 327 | &zwp_input_panel_surface_v1_interface, |
| 328 | 1, |
| 329 | id); |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 330 | wl_resource_set_implementation(ipsurf->resource, |
| 331 | &input_panel_surface_implementation, |
| 332 | ipsurf, |
| 333 | destroy_input_panel_surface_resource); |
| 334 | } |
| 335 | |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 336 | static const struct zwp_input_panel_v1_interface input_panel_implementation = { |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 337 | input_panel_get_input_panel_surface |
| 338 | }; |
| 339 | |
| 340 | static void |
| 341 | unbind_input_panel(struct wl_resource *resource) |
| 342 | { |
| 343 | struct ivi_shell *shell = wl_resource_get_user_data(resource); |
| 344 | |
| 345 | shell->input_panel.binding = NULL; |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | bind_input_panel(struct wl_client *client, |
| 350 | void *data, uint32_t version, uint32_t id) |
| 351 | { |
| 352 | struct ivi_shell *shell = data; |
| 353 | struct wl_resource *resource; |
| 354 | |
| 355 | resource = wl_resource_create(client, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 356 | &zwp_input_panel_v1_interface, 1, id); |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 357 | |
| 358 | if (shell->input_panel.binding == NULL) { |
| 359 | wl_resource_set_implementation(resource, |
| 360 | &input_panel_implementation, |
| 361 | shell, unbind_input_panel); |
| 362 | shell->input_panel.binding = resource; |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 367 | "interface object already bound"); |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | input_panel_destroy(struct ivi_shell *shell) |
| 372 | { |
| 373 | wl_list_remove(&shell->show_input_panel_listener.link); |
| 374 | wl_list_remove(&shell->hide_input_panel_listener.link); |
| 375 | } |
| 376 | |
| 377 | int |
| 378 | input_panel_setup(struct ivi_shell *shell) |
| 379 | { |
| 380 | struct weston_compositor *ec = shell->compositor; |
| 381 | |
| 382 | shell->show_input_panel_listener.notify = show_input_panels; |
| 383 | wl_signal_add(&ec->show_input_panel_signal, |
| 384 | &shell->show_input_panel_listener); |
| 385 | shell->hide_input_panel_listener.notify = hide_input_panels; |
| 386 | wl_signal_add(&ec->hide_input_panel_signal, |
| 387 | &shell->hide_input_panel_listener); |
| 388 | shell->update_input_panel_listener.notify = update_input_panels; |
| 389 | wl_signal_add(&ec->update_input_panel_signal, |
| 390 | &shell->update_input_panel_listener); |
| 391 | |
| 392 | wl_list_init(&shell->input_panel.surfaces); |
| 393 | |
| 394 | if (wl_global_create(shell->compositor->wl_display, |
Jonas Ådahl | b57f472 | 2015-11-17 16:00:30 +0800 | [diff] [blame^] | 395 | &zwp_input_panel_v1_interface, 1, |
Nobuhiko Tanibata | 0038b73 | 2014-11-27 13:25:34 +0900 | [diff] [blame] | 396 | shell, bind_input_panel) == NULL) |
| 397 | return -1; |
| 398 | |
| 399 | return 0; |
| 400 | } |