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