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