2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
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:
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
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.
34 #include "input-method-unstable-v1-server-protocol.h"
35 #include "shared/helpers.h"
37 struct input_panel_surface {
38 struct wl_resource *resource;
39 struct wl_signal destroy_signal;
41 struct desktop_shell *shell;
44 struct weston_surface *surface;
45 struct weston_view *view;
46 struct wl_listener surface_destroy_listener;
48 struct weston_view_animation *anim;
50 struct weston_output *output;
55 input_panel_slide_done(struct weston_view_animation *animation, void *data)
57 struct input_panel_surface *ipsurf = data;
63 show_input_panel_surface(struct input_panel_surface *ipsurf)
65 struct desktop_shell *shell = ipsurf->shell;
66 struct weston_seat *seat;
67 struct weston_surface *focus;
70 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
71 struct weston_keyboard *keyboard =
72 weston_seat_get_keyboard(seat);
74 if (!keyboard || !keyboard->focus)
76 focus = weston_surface_get_main_surface(keyboard->focus);
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);
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 ipsurf->surface->is_mapped = true;
90 ipsurf->view->is_mapped = true;
91 weston_surface_damage(ipsurf->surface);
94 weston_view_animation_destroy(ipsurf->anim);
97 weston_slide_run(ipsurf->view,
98 ipsurf->surface->height * 0.9, 0,
99 input_panel_slide_done, ipsurf);
103 show_input_panels(struct wl_listener *listener, void *data)
105 struct desktop_shell *shell =
106 container_of(listener, struct desktop_shell,
107 show_input_panel_listener);
108 struct input_panel_surface *ipsurf, *next;
110 shell->text_input.surface = (struct weston_surface*)data;
112 if (shell->showing_input_panels)
115 shell->showing_input_panels = true;
118 wl_list_insert(&shell->compositor->cursor_layer.link,
119 &shell->input_panel_layer.link);
121 wl_list_for_each_safe(ipsurf, next,
122 &shell->input_panel.surfaces, link) {
123 if (ipsurf->surface->width == 0)
126 show_input_panel_surface(ipsurf);
131 hide_input_panels(struct wl_listener *listener, void *data)
133 struct desktop_shell *shell =
134 container_of(listener, struct desktop_shell,
135 hide_input_panel_listener);
136 struct weston_view *view, *next;
138 if (!shell->showing_input_panels)
141 shell->showing_input_panels = false;
144 wl_list_remove(&shell->input_panel_layer.link);
146 wl_list_for_each_safe(view, next,
147 &shell->input_panel_layer.view_list.link,
149 weston_view_unmap(view);
153 update_input_panels(struct wl_listener *listener, void *data)
155 struct desktop_shell *shell =
156 container_of(listener, struct desktop_shell,
157 update_input_panel_listener);
159 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
163 input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
165 return snprintf(buf, len, "input panel");
169 input_panel_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
171 struct input_panel_surface *ip_surface = surface->committed_private;
172 struct desktop_shell *shell = ip_surface->shell;
173 struct weston_view *view;
176 if (surface->width == 0)
179 if (ip_surface->panel) {
180 view = get_default_view(shell->text_input.surface);
183 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
184 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
186 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
187 y = ip_surface->output->y + ip_surface->output->height - surface->height;
190 weston_view_set_position(ip_surface->view, x, y);
192 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
193 show_input_panel_surface(ip_surface);
197 destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
199 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
201 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
202 wl_list_remove(&input_panel_surface->link);
204 input_panel_surface->surface->committed = NULL;
205 weston_surface_set_label_func(input_panel_surface->surface, NULL);
206 weston_view_destroy(input_panel_surface->view);
208 free(input_panel_surface);
211 static struct input_panel_surface *
212 get_input_panel_surface(struct weston_surface *surface)
214 if (surface->committed == input_panel_committed) {
215 return surface->committed_private;
222 input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
224 struct input_panel_surface *ipsurface = container_of(listener,
225 struct input_panel_surface,
226 surface_destroy_listener);
228 if (ipsurface->resource) {
229 wl_resource_destroy(ipsurface->resource);
231 destroy_input_panel_surface(ipsurface);
235 static struct input_panel_surface *
236 create_input_panel_surface(struct desktop_shell *shell,
237 struct weston_surface *surface)
239 struct input_panel_surface *input_panel_surface;
241 input_panel_surface = calloc(1, sizeof *input_panel_surface);
242 if (!input_panel_surface)
245 surface->committed = input_panel_committed;
246 surface->committed_private = input_panel_surface;
247 weston_surface_set_label_func(surface, input_panel_get_label);
249 input_panel_surface->shell = shell;
251 input_panel_surface->surface = surface;
252 input_panel_surface->view = weston_view_create(surface);
254 wl_signal_init(&input_panel_surface->destroy_signal);
255 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
256 wl_signal_add(&surface->destroy_signal,
257 &input_panel_surface->surface_destroy_listener);
259 wl_list_init(&input_panel_surface->link);
261 return input_panel_surface;
265 input_panel_surface_set_toplevel(struct wl_client *client,
266 struct wl_resource *resource,
267 struct wl_resource *output_resource,
270 struct input_panel_surface *input_panel_surface =
271 wl_resource_get_user_data(resource);
272 struct desktop_shell *shell = input_panel_surface->shell;
274 wl_list_insert(&shell->input_panel.surfaces,
275 &input_panel_surface->link);
277 input_panel_surface->output = wl_resource_get_user_data(output_resource);
278 input_panel_surface->panel = 0;
282 input_panel_surface_set_overlay_panel(struct wl_client *client,
283 struct wl_resource *resource)
285 struct input_panel_surface *input_panel_surface =
286 wl_resource_get_user_data(resource);
287 struct desktop_shell *shell = input_panel_surface->shell;
289 wl_list_insert(&shell->input_panel.surfaces,
290 &input_panel_surface->link);
292 input_panel_surface->panel = 1;
295 static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = {
296 input_panel_surface_set_toplevel,
297 input_panel_surface_set_overlay_panel
301 destroy_input_panel_surface_resource(struct wl_resource *resource)
303 struct input_panel_surface *ipsurf =
304 wl_resource_get_user_data(resource);
306 destroy_input_panel_surface(ipsurf);
310 input_panel_get_input_panel_surface(struct wl_client *client,
311 struct wl_resource *resource,
313 struct wl_resource *surface_resource)
315 struct weston_surface *surface =
316 wl_resource_get_user_data(surface_resource);
317 struct desktop_shell *shell = wl_resource_get_user_data(resource);
318 struct input_panel_surface *ipsurf;
320 if (get_input_panel_surface(surface)) {
321 wl_resource_post_error(surface_resource,
322 WL_DISPLAY_ERROR_INVALID_OBJECT,
323 "wl_input_panel::get_input_panel_surface already requested");
327 ipsurf = create_input_panel_surface(shell, surface);
329 wl_resource_post_error(surface_resource,
330 WL_DISPLAY_ERROR_INVALID_OBJECT,
331 "surface->committed already set");
336 wl_resource_create(client,
337 &zwp_input_panel_surface_v1_interface,
340 wl_resource_set_implementation(ipsurf->resource,
341 &input_panel_surface_implementation,
343 destroy_input_panel_surface_resource);
346 static const struct zwp_input_panel_v1_interface input_panel_implementation = {
347 input_panel_get_input_panel_surface
351 unbind_input_panel(struct wl_resource *resource)
353 struct desktop_shell *shell = wl_resource_get_user_data(resource);
355 shell->input_panel.binding = NULL;
359 bind_input_panel(struct wl_client *client,
360 void *data, uint32_t version, uint32_t id)
362 struct desktop_shell *shell = data;
363 struct wl_resource *resource;
365 resource = wl_resource_create(client,
366 &zwp_input_panel_v1_interface, 1, id);
368 if (shell->input_panel.binding == NULL) {
369 wl_resource_set_implementation(resource,
370 &input_panel_implementation,
371 shell, unbind_input_panel);
372 shell->input_panel.binding = resource;
376 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
377 "interface object already bound");
381 input_panel_destroy(struct desktop_shell *shell)
383 wl_list_remove(&shell->show_input_panel_listener.link);
384 wl_list_remove(&shell->hide_input_panel_listener.link);
388 input_panel_setup(struct desktop_shell *shell)
390 struct weston_compositor *ec = shell->compositor;
392 shell->show_input_panel_listener.notify = show_input_panels;
393 wl_signal_add(&ec->show_input_panel_signal,
394 &shell->show_input_panel_listener);
395 shell->hide_input_panel_listener.notify = hide_input_panels;
396 wl_signal_add(&ec->hide_input_panel_signal,
397 &shell->hide_input_panel_listener);
398 shell->update_input_panel_listener.notify = update_input_panels;
399 wl_signal_add(&ec->update_input_panel_signal,
400 &shell->update_input_panel_listener);
402 wl_list_init(&shell->input_panel.surfaces);
404 if (wl_global_create(shell->compositor->wl_display,
405 &zwp_input_panel_v1_interface, 1,
406 shell, bind_input_panel) == NULL)