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; |
| 120 | uint32_t show_surface = 0; |
| 121 | |
| 122 | if (surface->width == 0) |
| 123 | return; |
| 124 | |
| 125 | if (!weston_surface_is_mapped(surface)) { |
| 126 | if (!shell->showing_input_panels) |
| 127 | return; |
| 128 | |
| 129 | show_surface = 1; |
| 130 | } |
| 131 | |
| 132 | fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output); |
| 133 | |
| 134 | if (ip_surface->panel) { |
| 135 | x = get_default_view(shell->text_input.surface)->geometry.x + shell->text_input.cursor_rectangle.x2; |
| 136 | y = get_default_view(shell->text_input.surface)->geometry.y + shell->text_input.cursor_rectangle.y2; |
| 137 | } else { |
| 138 | x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2; |
| 139 | y = ip_surface->output->y + ip_surface->output->height - surface->height; |
| 140 | } |
| 141 | |
| 142 | weston_view_set_position(ip_surface->view, x, y); |
| 143 | |
| 144 | if (show_surface) { |
| 145 | wl_list_insert(&shell->input_panel_layer.view_list, |
| 146 | &ip_surface->view->layer_link); |
| 147 | weston_view_update_transform(ip_surface->view); |
| 148 | weston_surface_damage(surface); |
| 149 | weston_slide_run(ip_surface->view, ip_surface->view->surface->height, 0, NULL, NULL); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | destroy_input_panel_surface(struct input_panel_surface *input_panel_surface) |
| 155 | { |
| 156 | wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface); |
| 157 | |
| 158 | wl_list_remove(&input_panel_surface->surface_destroy_listener.link); |
| 159 | wl_list_remove(&input_panel_surface->link); |
| 160 | |
| 161 | input_panel_surface->surface->configure = NULL; |
| 162 | weston_view_destroy(input_panel_surface->view); |
| 163 | |
| 164 | free(input_panel_surface); |
| 165 | } |
| 166 | |
| 167 | static struct input_panel_surface * |
| 168 | get_input_panel_surface(struct weston_surface *surface) |
| 169 | { |
| 170 | if (surface->configure == input_panel_configure) { |
| 171 | return surface->configure_private; |
| 172 | } else { |
| 173 | return NULL; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void |
| 178 | input_panel_handle_surface_destroy(struct wl_listener *listener, void *data) |
| 179 | { |
| 180 | struct input_panel_surface *ipsurface = container_of(listener, |
| 181 | struct input_panel_surface, |
| 182 | surface_destroy_listener); |
| 183 | |
| 184 | if (ipsurface->resource) { |
| 185 | wl_resource_destroy(ipsurface->resource); |
| 186 | } else { |
| 187 | destroy_input_panel_surface(ipsurface); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static struct input_panel_surface * |
| 192 | create_input_panel_surface(struct desktop_shell *shell, |
| 193 | struct weston_surface *surface) |
| 194 | { |
| 195 | struct input_panel_surface *input_panel_surface; |
| 196 | |
| 197 | input_panel_surface = calloc(1, sizeof *input_panel_surface); |
| 198 | if (!input_panel_surface) |
| 199 | return NULL; |
| 200 | |
| 201 | surface->configure = input_panel_configure; |
| 202 | surface->configure_private = input_panel_surface; |
| 203 | |
| 204 | input_panel_surface->shell = shell; |
| 205 | |
| 206 | input_panel_surface->surface = surface; |
| 207 | input_panel_surface->view = weston_view_create(surface); |
| 208 | |
| 209 | wl_signal_init(&input_panel_surface->destroy_signal); |
| 210 | input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy; |
| 211 | wl_signal_add(&surface->destroy_signal, |
| 212 | &input_panel_surface->surface_destroy_listener); |
| 213 | |
| 214 | wl_list_init(&input_panel_surface->link); |
| 215 | |
| 216 | return input_panel_surface; |
| 217 | } |
| 218 | |
| 219 | static void |
| 220 | input_panel_surface_set_toplevel(struct wl_client *client, |
| 221 | struct wl_resource *resource, |
| 222 | struct wl_resource *output_resource, |
| 223 | uint32_t position) |
| 224 | { |
| 225 | struct input_panel_surface *input_panel_surface = |
| 226 | wl_resource_get_user_data(resource); |
| 227 | struct desktop_shell *shell = input_panel_surface->shell; |
| 228 | |
| 229 | wl_list_insert(&shell->input_panel.surfaces, |
| 230 | &input_panel_surface->link); |
| 231 | |
| 232 | input_panel_surface->output = wl_resource_get_user_data(output_resource); |
| 233 | input_panel_surface->panel = 0; |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | input_panel_surface_set_overlay_panel(struct wl_client *client, |
| 238 | struct wl_resource *resource) |
| 239 | { |
| 240 | struct input_panel_surface *input_panel_surface = |
| 241 | wl_resource_get_user_data(resource); |
| 242 | struct desktop_shell *shell = input_panel_surface->shell; |
| 243 | |
| 244 | wl_list_insert(&shell->input_panel.surfaces, |
| 245 | &input_panel_surface->link); |
| 246 | |
| 247 | input_panel_surface->panel = 1; |
| 248 | } |
| 249 | |
| 250 | static const struct wl_input_panel_surface_interface input_panel_surface_implementation = { |
| 251 | input_panel_surface_set_toplevel, |
| 252 | input_panel_surface_set_overlay_panel |
| 253 | }; |
| 254 | |
| 255 | static void |
| 256 | destroy_input_panel_surface_resource(struct wl_resource *resource) |
| 257 | { |
| 258 | struct input_panel_surface *ipsurf = |
| 259 | wl_resource_get_user_data(resource); |
| 260 | |
| 261 | destroy_input_panel_surface(ipsurf); |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | input_panel_get_input_panel_surface(struct wl_client *client, |
| 266 | struct wl_resource *resource, |
| 267 | uint32_t id, |
| 268 | struct wl_resource *surface_resource) |
| 269 | { |
| 270 | struct weston_surface *surface = |
| 271 | wl_resource_get_user_data(surface_resource); |
| 272 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 273 | struct input_panel_surface *ipsurf; |
| 274 | |
| 275 | if (get_input_panel_surface(surface)) { |
| 276 | wl_resource_post_error(surface_resource, |
| 277 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 278 | "wl_input_panel::get_input_panel_surface already requested"); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | ipsurf = create_input_panel_surface(shell, surface); |
| 283 | if (!ipsurf) { |
| 284 | wl_resource_post_error(surface_resource, |
| 285 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 286 | "surface->configure already set"); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | ipsurf->resource = |
| 291 | wl_resource_create(client, |
| 292 | &wl_input_panel_surface_interface, 1, id); |
| 293 | wl_resource_set_implementation(ipsurf->resource, |
| 294 | &input_panel_surface_implementation, |
| 295 | ipsurf, |
| 296 | destroy_input_panel_surface_resource); |
| 297 | } |
| 298 | |
| 299 | static const struct wl_input_panel_interface input_panel_implementation = { |
| 300 | input_panel_get_input_panel_surface |
| 301 | }; |
| 302 | |
| 303 | static void |
| 304 | unbind_input_panel(struct wl_resource *resource) |
| 305 | { |
| 306 | struct desktop_shell *shell = wl_resource_get_user_data(resource); |
| 307 | |
| 308 | shell->input_panel.binding = NULL; |
| 309 | } |
| 310 | |
| 311 | static void |
| 312 | bind_input_panel(struct wl_client *client, |
| 313 | void *data, uint32_t version, uint32_t id) |
| 314 | { |
| 315 | struct desktop_shell *shell = data; |
| 316 | struct wl_resource *resource; |
| 317 | |
| 318 | resource = wl_resource_create(client, |
| 319 | &wl_input_panel_interface, 1, id); |
| 320 | |
| 321 | if (shell->input_panel.binding == NULL) { |
| 322 | wl_resource_set_implementation(resource, |
| 323 | &input_panel_implementation, |
| 324 | shell, unbind_input_panel); |
| 325 | shell->input_panel.binding = resource; |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 330 | "interface object already bound"); |
| 331 | wl_resource_destroy(resource); |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | input_panel_destroy(struct desktop_shell *shell) |
| 336 | { |
| 337 | wl_list_remove(&shell->show_input_panel_listener.link); |
| 338 | wl_list_remove(&shell->hide_input_panel_listener.link); |
| 339 | } |
| 340 | |
| 341 | int |
| 342 | input_panel_setup(struct desktop_shell *shell) |
| 343 | { |
| 344 | struct weston_compositor *ec = shell->compositor; |
| 345 | |
| 346 | shell->show_input_panel_listener.notify = show_input_panels; |
| 347 | wl_signal_add(&ec->show_input_panel_signal, |
| 348 | &shell->show_input_panel_listener); |
| 349 | shell->hide_input_panel_listener.notify = hide_input_panels; |
| 350 | wl_signal_add(&ec->hide_input_panel_signal, |
| 351 | &shell->hide_input_panel_listener); |
| 352 | shell->update_input_panel_listener.notify = update_input_panels; |
| 353 | wl_signal_add(&ec->update_input_panel_signal, |
| 354 | &shell->update_input_panel_listener); |
| 355 | |
| 356 | wl_list_init(&shell->input_panel.surfaces); |
| 357 | |
| 358 | if (wl_global_create(shell->compositor->wl_display, |
| 359 | &wl_input_panel_interface, 1, |
| 360 | shell, bind_input_panel) == NULL) |
| 361 | return -1; |
| 362 | |
| 363 | return 0; |
| 364 | } |