libweston: Rename weston_surface::configure to ::committed
[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                 wl_list_insert(&shell->compositor->cursor_layer.link,
120                                &shell->input_panel_layer.link);
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                 wl_list_remove(&shell->input_panel_layer.link);
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
275         wl_list_insert(&shell->input_panel.surfaces,
276                        &input_panel_surface->link);
277
278         input_panel_surface->output = wl_resource_get_user_data(output_resource);
279         input_panel_surface->panel = 0;
280 }
281
282 static void
283 input_panel_surface_set_overlay_panel(struct wl_client *client,
284                                       struct wl_resource *resource)
285 {
286         struct input_panel_surface *input_panel_surface =
287                 wl_resource_get_user_data(resource);
288         struct ivi_shell *shell = input_panel_surface->shell;
289
290         wl_list_insert(&shell->input_panel.surfaces,
291                        &input_panel_surface->link);
292
293         input_panel_surface->panel = 1;
294 }
295
296 static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = {
297         input_panel_surface_set_toplevel,
298         input_panel_surface_set_overlay_panel
299 };
300
301 static void
302 destroy_input_panel_surface_resource(struct wl_resource *resource)
303 {
304         struct input_panel_surface *ipsurf =
305                 wl_resource_get_user_data(resource);
306
307         destroy_input_panel_surface(ipsurf);
308 }
309
310 static void
311 input_panel_get_input_panel_surface(struct wl_client *client,
312                                     struct wl_resource *resource,
313                                     uint32_t id,
314                                     struct wl_resource *surface_resource)
315 {
316         struct weston_surface *surface =
317                 wl_resource_get_user_data(surface_resource);
318         struct ivi_shell *shell = wl_resource_get_user_data(resource);
319         struct input_panel_surface *ipsurf;
320
321         if (get_input_panel_surface(surface)) {
322                 wl_resource_post_error(surface_resource,
323                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
324                                        "wl_input_panel::get_input_panel_surface already requested");
325                 return;
326         }
327
328         ipsurf = create_input_panel_surface(shell, surface);
329         if (!ipsurf) {
330                 wl_resource_post_error(surface_resource,
331                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
332                                        "surface->committed already set");
333                 return;
334         }
335
336         ipsurf->resource =
337                 wl_resource_create(client,
338                                    &zwp_input_panel_surface_v1_interface,
339                                    1,
340                                    id);
341         wl_resource_set_implementation(ipsurf->resource,
342                                        &input_panel_surface_implementation,
343                                        ipsurf,
344                                        destroy_input_panel_surface_resource);
345 }
346
347 static const struct zwp_input_panel_v1_interface input_panel_implementation = {
348         input_panel_get_input_panel_surface
349 };
350
351 static void
352 unbind_input_panel(struct wl_resource *resource)
353 {
354         struct ivi_shell *shell = wl_resource_get_user_data(resource);
355
356         shell->input_panel.binding = NULL;
357 }
358
359 static void
360 bind_input_panel(struct wl_client *client,
361              void *data, uint32_t version, uint32_t id)
362 {
363         struct ivi_shell *shell = data;
364         struct wl_resource *resource;
365
366         resource = wl_resource_create(client,
367                                       &zwp_input_panel_v1_interface, 1, id);
368
369         if (shell->input_panel.binding == NULL) {
370                 wl_resource_set_implementation(resource,
371                                                &input_panel_implementation,
372                                                shell, unbind_input_panel);
373                 shell->input_panel.binding = resource;
374                 return;
375         }
376
377         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
378                                "interface object already bound");
379 }
380
381 void
382 input_panel_destroy(struct ivi_shell *shell)
383 {
384         wl_list_remove(&shell->show_input_panel_listener.link);
385         wl_list_remove(&shell->hide_input_panel_listener.link);
386 }
387
388 int
389 input_panel_setup(struct ivi_shell *shell)
390 {
391         struct weston_compositor *ec = shell->compositor;
392
393         shell->show_input_panel_listener.notify = show_input_panels;
394         wl_signal_add(&ec->show_input_panel_signal,
395                       &shell->show_input_panel_listener);
396         shell->hide_input_panel_listener.notify = hide_input_panels;
397         wl_signal_add(&ec->hide_input_panel_signal,
398                       &shell->hide_input_panel_listener);
399         shell->update_input_panel_listener.notify = update_input_panels;
400         wl_signal_add(&ec->update_input_panel_signal,
401                       &shell->update_input_panel_listener);
402
403         wl_list_init(&shell->input_panel.surfaces);
404
405         if (wl_global_create(shell->compositor->wl_display,
406                              &zwp_input_panel_v1_interface, 1,
407                              shell, bind_input_panel) == NULL)
408                 return -1;
409
410         return 0;
411 }