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 | * |
| 6 | * Permission to use, copy, modify, distribute, and sell this software and |
| 7 | * its documentation for any purpose is hereby granted without fee, provided |
| 8 | * that the above copyright notice appear in all copies and that both that |
| 9 | * copyright notice and this permission notice appear in supporting |
| 10 | * documentation, and that the name of the copyright holders not be used in |
| 11 | * advertising or publicity pertaining to distribution of the software |
| 12 | * without specific, written prior permission. The copyright holders make |
| 13 | * no representations about the suitability of this software for any |
| 14 | * purpose. It is provided "as is" without express or implied warranty. |
| 15 | * |
| 16 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 17 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 18 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 20 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 21 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 22 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "config.h" |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "shell.h" |
| 32 | #include "desktop-shell-server-protocol.h" |
| 33 | #include "input-method-server-protocol.h" |
| 34 | |
| 35 | struct input_panel_surface { |
| 36 | struct wl_resource *resource; |
| 37 | struct wl_signal destroy_signal; |
| 38 | |
| 39 | struct desktop_shell *shell; |
| 40 | |
| 41 | struct wl_list link; |
| 42 | struct weston_surface *surface; |
| 43 | struct weston_view *view; |
| 44 | struct wl_listener surface_destroy_listener; |
| 45 | |
| 46 | struct weston_output *output; |
| 47 | uint32_t panel; |
| 48 | }; |
| 49 | |
| 50 | static void |
| 51 | show_input_panels(struct wl_listener *listener, void *data) |
| 52 | { |
| 53 | struct desktop_shell *shell = |
| 54 | container_of(listener, struct desktop_shell, |
| 55 | show_input_panel_listener); |
| 56 | struct input_panel_surface *ipsurf, *next; |
| 57 | |
| 58 | shell->text_input.surface = (struct weston_surface*)data; |
| 59 | |
| 60 | if (shell->showing_input_panels) |
| 61 | return; |
| 62 | |
| 63 | shell->showing_input_panels = true; |
| 64 | |
| 65 | if (!shell->locked) |
| 66 | wl_list_insert(&shell->panel_layer.link, |
| 67 | &shell->input_panel_layer.link); |
| 68 | |
| 69 | wl_list_for_each_safe(ipsurf, next, |
| 70 | &shell->input_panel.surfaces, link) { |
| 71 | if (!ipsurf->surface->buffer_ref.buffer) |
| 72 | continue; |
| 73 | wl_list_insert(&shell->input_panel_layer.view_list, |
| 74 | &ipsurf->view->layer_link); |
| 75 | weston_view_geometry_dirty(ipsurf->view); |
| 76 | weston_view_update_transform(ipsurf->view); |
| 77 | weston_surface_damage(ipsurf->surface); |
| 78 | weston_slide_run(ipsurf->view, ipsurf->surface->height, |
| 79 | 0, NULL, NULL); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | hide_input_panels(struct wl_listener *listener, void *data) |
| 85 | { |
| 86 | struct desktop_shell *shell = |
| 87 | container_of(listener, struct desktop_shell, |
| 88 | hide_input_panel_listener); |
| 89 | struct weston_view *view, *next; |
| 90 | |
| 91 | if (!shell->showing_input_panels) |
| 92 | return; |
| 93 | |
| 94 | shell->showing_input_panels = false; |
| 95 | |
| 96 | if (!shell->locked) |
| 97 | wl_list_remove(&shell->input_panel_layer.link); |
| 98 | |
| 99 | wl_list_for_each_safe(view, next, |
| 100 | &shell->input_panel_layer.view_list, layer_link) |
| 101 | weston_view_unmap(view); |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | update_input_panels(struct wl_listener *listener, void *data) |
| 106 | { |
| 107 | struct desktop_shell *shell = |
| 108 | container_of(listener, struct desktop_shell, |
| 109 | update_input_panel_listener); |
| 110 | |
| 111 | memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t)); |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy) |
| 116 | { |
| 117 | struct input_panel_surface *ip_surface = surface->configure_private; |
| 118 | struct desktop_shell *shell = ip_surface->shell; |
| 119 | float x, y; |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 120 | |
| 121 | if (surface->width == 0) |
| 122 | return; |
| 123 | |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 124 | fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output); |
| 125 | |
| 126 | if (ip_surface->panel) { |
| 127 | x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2; |
| 128 | y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2; |
| 129 | } else { |
| 130 | x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2; |
| 131 | y = ip_surface->output->y + ip_surface->output->height - surface->height; |
| 132 | } |
| 133 | |
| 134 | weston_view_set_position(ip_surface->view, x, y); |
| 135 | |
Kristian Høgsberg | c30c8a3 | 2014-01-02 00:45:17 -0800 | [diff] [blame^] | 136 | if (!weston_surface_is_mapped(surface) && shell->showing_input_panels) { |
Kristian Høgsberg | 677a5f5 | 2013-12-04 11:00:19 -0800 | [diff] [blame] | 137 | wl_list_insert(&shell->input_panel_layer.view_list, |
| 138 | &ip_surface->view->layer_link); |
| 139 | weston_view_update_transform(ip_surface->view); |
| 140 | weston_surface_damage(surface); |
| 141 | weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | destroy_input_panel_surface(struct input_panel_surface *input_panel_surface) |
| 147 | { |
| 148 | wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface); |
| 149 | |
| 150 | wl_list_remove(&input_panel_surface->surface_destroy_listener.link); |
| 151 | wl_list_remove(&input_panel_surface->link); |
| 152 | |
| 153 | input_panel_surface->surface->configure = NULL; |
| 154 | weston_view_destroy(input_panel_surface->view); |
| 155 | |
| 156 | free(input_panel_surface); |
| 157 | } |
| 158 | |
| 159 | static struct input_panel_surface * |
| 160 | get_input_panel_surface(struct weston_surface *surface) |
| 161 | { |
| 162 | if (surface->configure == input_panel_configure) { |
| 163 | return surface->configure_private; |
| 164 | } else { |
| 165 | return NULL; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static void |
| 170 | input_panel_handle_surface_destroy(struct wl_listener *listener, void *data) |
| 171 | { |
| 172 | struct input_panel_surface *ipsurface = container_of(listener, |
| 173 | struct input_panel_surface, |
| 174 | surface_destroy_listener); |
| 175 | |
| 176 | if (ipsurface->resource) { |
| 177 | wl_resource_destroy(ipsurface->resource); |
| 178 | } else { |
| 179 | destroy_input_panel_surface(ipsurface); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static struct input_panel_surface * |
| 184 | create_input_panel_surface(struct desktop_shell *shell, |
| 185 | struct weston_surface *surface) |
| 186 | { |
| 187 | struct input_panel_surface *input_panel_surface; |
| 188 | |
| 189 | input_panel_surface = calloc(1, sizeof *input_panel_surface); |
| 190 | if (!input_panel_surface) |
| 191 | return NULL; |
| 192 | |
| 193 | surface->configure = input_panel_configure; |
| 194 | surface->configure_private = input_panel_surface; |
| 195 | |
| 196 | input_panel_surface->shell = shell; |
| 197 | |
| 198 | input_panel_surface->surface = surface; |
| 199 | input_panel_surface->view = weston_view_create(surface); |
| 200 | |
| 201 | wl_signal_init(&input_panel_surface->destroy_signal); |
| 202 | input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy; |
| 203 | wl_signal_add(&surface->destroy_signal, |
| 204 | &input_panel_surface->surface_destroy_listener); |
| 205 | |
| 206 | wl_list_init(&input_panel_surface->link); |
| 207 | |
| 208 | return input_panel_surface; |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | input_panel_surface_set_toplevel(struct wl_client *client, |
| 213 | struct wl_resource *resource, |
| 214 | struct wl_resource *output_resource, |
| 215 | uint32_t position) |
| 216 | { |
| 217 | struct input_panel_surface *input_panel_surface = |
| 218 | wl_resource_get_user_data(resource); |
| 219 | struct desktop_shell *shell = input_panel_surface->shell; |
| 220 | |
| 221 | wl_list_insert(&shell->input_panel.surfaces, |
| 222 | &input_panel_surface->link); |
| 223 | |
| 224 | input_panel_surface->output = wl_resource_get_user_data(output_resource); |
| 225 | input_panel_surface->panel = 0; |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | input_panel_surface_set_overlay_panel(struct wl_client *client, |
| 230 | struct wl_resource *resource) |
| 231 | { |
| 232 | struct input_panel_surface *input_panel_surface = |
| 233 | wl_resource_get_user_data(resource); |
| 234 | struct desktop_shell *shell = input_panel_surface->shell; |
| 235 | |
| 236 | wl_list_insert(&shell->input_panel.surfaces, |
| 237 | &input_panel_surface->link); |
| 238 | |
| 239 | input_panel_surface->panel = 1; |
| 240 | } |
| 241 | |
| 242 | static const struct wl_input_panel_surface_interface input_panel_surface_implementation = { |
| 243 | input_panel_surface_set_toplevel, |
| 244 | input_panel_surface_set_overlay_panel |
| 245 | }; |
| 246 | |
| 247 | static void |
| 248 | destroy_input_panel_surface_resource(struct wl_resource *resource) |
| 249 | { |
| 250 | struct input_panel_surface *ipsurf = |
| 251 | wl_resource_get_user_data(resource); |
| 252 | |
| 253 | destroy_input_panel_surface(ipsurf); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | input_panel_get_input_panel_surface(struct wl_client *client, |
| 258 | struct wl_resource *resource, |
| 259 | uint32_t id, |
| 260 | struct wl_resource *surface_resource) |
| 261 | { |
| 262 | struct weston_surface *surface = |
| 263 | wl_resource_get_user_data(surface_resource); |
| 264 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 265 | struct input_panel_surface *ipsurf; |
| 266 | |
| 267 | if (get_input_panel_surface(surface)) { |
| 268 | wl_resource_post_error(surface_resource, |
| 269 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 270 | "wl_input_panel::get_input_panel_surface already requested"); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | ipsurf = create_input_panel_surface(shell, surface); |
| 275 | if (!ipsurf) { |
| 276 | wl_resource_post_error(surface_resource, |
| 277 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 278 | "surface->configure already set"); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | ipsurf->resource = |
| 283 | wl_resource_create(client, |
| 284 | &wl_input_panel_surface_interface, 1, id); |
| 285 | wl_resource_set_implementation(ipsurf->resource, |
| 286 | &input_panel_surface_implementation, |
| 287 | ipsurf, |
| 288 | destroy_input_panel_surface_resource); |
| 289 | } |
| 290 | |
| 291 | static const struct wl_input_panel_interface input_panel_implementation = { |
| 292 | input_panel_get_input_panel_surface |
| 293 | }; |
| 294 | |
| 295 | static void |
| 296 | unbind_input_panel(struct wl_resource *resource) |
| 297 | { |
| 298 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 299 | |
| 300 | shell->input_panel.binding = NULL; |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | bind_input_panel(struct wl_client *client, |
| 305 | void *data, uint32_t version, uint32_t id) |
| 306 | { |
| 307 | struct desktop_shell *shell = data; |
| 308 | struct wl_resource *resource; |
| 309 | |
| 310 | resource = wl_resource_create(client, |
| 311 | &wl_input_panel_interface, 1, id); |
| 312 | |
| 313 | if (shell->input_panel.binding == NULL) { |
| 314 | wl_resource_set_implementation(resource, |
| 315 | &input_panel_implementation, |
| 316 | shell, unbind_input_panel); |
| 317 | shell->input_panel.binding = resource; |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 322 | "interface object already bound"); |
| 323 | wl_resource_destroy(resource); |
| 324 | } |
| 325 | |
| 326 | void |
| 327 | input_panel_destroy(struct desktop_shell *shell) |
| 328 | { |
| 329 | wl_list_remove(&shell->show_input_panel_listener.link); |
| 330 | wl_list_remove(&shell->hide_input_panel_listener.link); |
| 331 | } |
| 332 | |
| 333 | int |
| 334 | input_panel_setup(struct desktop_shell *shell) |
| 335 | { |
| 336 | struct weston_compositor *ec = shell->compositor; |
| 337 | |
| 338 | shell->show_input_panel_listener.notify = show_input_panels; |
| 339 | wl_signal_add(&ec->show_input_panel_signal, |
| 340 | &shell->show_input_panel_listener); |
| 341 | shell->hide_input_panel_listener.notify = hide_input_panels; |
| 342 | wl_signal_add(&ec->hide_input_panel_signal, |
| 343 | &shell->hide_input_panel_listener); |
| 344 | shell->update_input_panel_listener.notify = update_input_panels; |
| 345 | wl_signal_add(&ec->update_input_panel_signal, |
| 346 | &shell->update_input_panel_listener); |
| 347 | |
| 348 | wl_list_init(&shell->input_panel.surfaces); |
| 349 | |
| 350 | if (wl_global_create(shell->compositor->wl_display, |
| 351 | &wl_input_panel_interface, 1, |
| 352 | shell, bind_input_panel) == NULL) |
| 353 | return -1; |
| 354 | |
| 355 | return 0; |
| 356 | } |