blob: 581b56bc634c78c5c67a24e6aff214539fd40df0 [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 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * 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:
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090013 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070014 * 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.
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090026 */
27
28#include "config.h"
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33
34#include "ivi-shell.h"
Jonas Ådahlb57f4722015-11-17 16:00:30 +080035#include "input-method-unstable-v1-server-protocol.h"
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090036#include "ivi-layout-private.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070037#include "shared/helpers.h"
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090038
39struct input_panel_surface {
40 struct wl_resource *resource;
41 struct wl_signal destroy_signal;
42
43 struct ivi_shell *shell;
44
45 struct wl_list link;
46 struct weston_surface *surface;
47 struct weston_view *view;
48 struct wl_listener surface_destroy_listener;
49
50 struct weston_view_animation *anim;
51
52 struct weston_output *output;
53 uint32_t panel;
54};
55
56static void
57input_panel_slide_done(struct weston_view_animation *animation, void *data)
58{
59 struct input_panel_surface *ipsurf = data;
60
61 ipsurf->anim = NULL;
62}
63
64static void
65show_input_panel_surface(struct input_panel_surface *ipsurf)
66{
67 struct ivi_shell *shell = ipsurf->shell;
68 struct weston_seat *seat;
69 struct weston_surface *focus;
70 float x, y;
71
72 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050073 struct weston_keyboard *keyboard =
74 weston_seat_get_keyboard(seat);
75
76 if (!keyboard || !keyboard->focus)
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090077 continue;
Derek Foreman1281a362015-07-31 16:55:32 -050078 focus = weston_surface_get_main_surface(keyboard->focus);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090079 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);
83 }
84
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 weston_surface_damage(ipsurf->surface);
90
91 if (ipsurf->anim)
92 weston_view_animation_destroy(ipsurf->anim);
93
94 ipsurf->anim =
95 weston_slide_run(ipsurf->view,
96 ipsurf->surface->height * 0.9, 0,
97 input_panel_slide_done, ipsurf);
98}
99
100static void
101show_input_panels(struct wl_listener *listener, void *data)
102{
103 struct ivi_shell *shell =
104 container_of(listener, struct ivi_shell,
105 show_input_panel_listener);
106 struct input_panel_surface *ipsurf, *next;
107
108 shell->text_input.surface = (struct weston_surface*)data;
109
110 if (shell->showing_input_panels)
111 return;
112
113 shell->showing_input_panels = true;
114
115 if (!shell->locked)
116 wl_list_insert(&shell->compositor->cursor_layer.link,
117 &shell->input_panel_layer.link);
118
119 wl_list_for_each_safe(ipsurf, next,
120 &shell->input_panel.surfaces, link) {
121 if (ipsurf->surface->width == 0)
122 continue;
123
124 show_input_panel_surface(ipsurf);
125 }
126}
127
128static void
129hide_input_panels(struct wl_listener *listener, void *data)
130{
131 struct ivi_shell *shell =
132 container_of(listener, struct ivi_shell,
133 hide_input_panel_listener);
134 struct weston_view *view, *next;
135
136 if (!shell->showing_input_panels)
137 return;
138
139 shell->showing_input_panels = false;
140
141 if (!shell->locked)
142 wl_list_remove(&shell->input_panel_layer.link);
143
144 wl_list_for_each_safe(view, next,
145 &shell->input_panel_layer.view_list.link,
146 layer_link.link)
147 weston_view_unmap(view);
148}
149
150static void
151update_input_panels(struct wl_listener *listener, void *data)
152{
153 struct ivi_shell *shell =
154 container_of(listener, struct ivi_shell,
155 update_input_panel_listener);
156
157 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
158}
159
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200160static int
161input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
162{
163 return snprintf(buf, len, "input panel");
164}
165
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900166static void
167input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
168{
169 struct input_panel_surface *ip_surface = surface->configure_private;
170 struct ivi_shell *shell = ip_surface->shell;
171 struct weston_view *view;
172 float x, y;
173
174 if (surface->width == 0)
175 return;
176
177 if (ip_surface->panel) {
178 view = get_default_view(shell->text_input.surface);
179 if (view == NULL)
180 return;
181 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
182 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
183 } else {
184 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
185 y = ip_surface->output->y + ip_surface->output->height - surface->height;
186 }
187
188 weston_view_set_position(ip_surface->view, x, y);
189
190 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
191 show_input_panel_surface(ip_surface);
192}
193
194static void
195destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
196{
197 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
198
199 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
200 wl_list_remove(&input_panel_surface->link);
201
202 input_panel_surface->surface->configure = NULL;
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200203 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900204 weston_view_destroy(input_panel_surface->view);
205
206 free(input_panel_surface);
207}
208
209static struct input_panel_surface *
210get_input_panel_surface(struct weston_surface *surface)
211{
212 if (surface->configure == input_panel_configure) {
213 return surface->configure_private;
214 } else {
215 return NULL;
216 }
217}
218
219static void
220input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
221{
222 struct input_panel_surface *ipsurface = container_of(listener,
223 struct input_panel_surface,
224 surface_destroy_listener);
225
226 if (ipsurface->resource) {
227 wl_resource_destroy(ipsurface->resource);
228 } else {
229 destroy_input_panel_surface(ipsurface);
230 }
231}
232
233static struct input_panel_surface *
234create_input_panel_surface(struct ivi_shell *shell,
235 struct weston_surface *surface)
236{
237 struct input_panel_surface *input_panel_surface;
238
239 input_panel_surface = calloc(1, sizeof *input_panel_surface);
240 if (!input_panel_surface)
241 return NULL;
242
243 surface->configure = input_panel_configure;
244 surface->configure_private = input_panel_surface;
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200245 weston_surface_set_label_func(surface, input_panel_get_label);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900246
247 input_panel_surface->shell = shell;
248
249 input_panel_surface->surface = surface;
250 input_panel_surface->view = weston_view_create(surface);
251
252 wl_signal_init(&input_panel_surface->destroy_signal);
253 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
254 wl_signal_add(&surface->destroy_signal,
255 &input_panel_surface->surface_destroy_listener);
256
257 wl_list_init(&input_panel_surface->link);
258
259 return input_panel_surface;
260}
261
262static void
263input_panel_surface_set_toplevel(struct wl_client *client,
264 struct wl_resource *resource,
265 struct wl_resource *output_resource,
266 uint32_t position)
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->output = wl_resource_get_user_data(output_resource);
276 input_panel_surface->panel = 0;
277}
278
279static void
280input_panel_surface_set_overlay_panel(struct wl_client *client,
281 struct wl_resource *resource)
282{
283 struct input_panel_surface *input_panel_surface =
284 wl_resource_get_user_data(resource);
285 struct ivi_shell *shell = input_panel_surface->shell;
286
287 wl_list_insert(&shell->input_panel.surfaces,
288 &input_panel_surface->link);
289
290 input_panel_surface->panel = 1;
291}
292
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800293static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = {
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900294 input_panel_surface_set_toplevel,
295 input_panel_surface_set_overlay_panel
296};
297
298static void
299destroy_input_panel_surface_resource(struct wl_resource *resource)
300{
301 struct input_panel_surface *ipsurf =
302 wl_resource_get_user_data(resource);
303
304 destroy_input_panel_surface(ipsurf);
305}
306
307static void
308input_panel_get_input_panel_surface(struct wl_client *client,
309 struct wl_resource *resource,
310 uint32_t id,
311 struct wl_resource *surface_resource)
312{
313 struct weston_surface *surface =
314 wl_resource_get_user_data(surface_resource);
315 struct ivi_shell *shell = wl_resource_get_user_data(resource);
316 struct input_panel_surface *ipsurf;
317
318 if (get_input_panel_surface(surface)) {
319 wl_resource_post_error(surface_resource,
320 WL_DISPLAY_ERROR_INVALID_OBJECT,
321 "wl_input_panel::get_input_panel_surface already requested");
322 return;
323 }
324
325 ipsurf = create_input_panel_surface(shell, surface);
326 if (!ipsurf) {
327 wl_resource_post_error(surface_resource,
328 WL_DISPLAY_ERROR_INVALID_OBJECT,
329 "surface->configure already set");
330 return;
331 }
332
333 ipsurf->resource =
334 wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800335 &zwp_input_panel_surface_v1_interface,
336 1,
337 id);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900338 wl_resource_set_implementation(ipsurf->resource,
339 &input_panel_surface_implementation,
340 ipsurf,
341 destroy_input_panel_surface_resource);
342}
343
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800344static const struct zwp_input_panel_v1_interface input_panel_implementation = {
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900345 input_panel_get_input_panel_surface
346};
347
348static void
349unbind_input_panel(struct wl_resource *resource)
350{
351 struct ivi_shell *shell = wl_resource_get_user_data(resource);
352
353 shell->input_panel.binding = NULL;
354}
355
356static void
357bind_input_panel(struct wl_client *client,
358 void *data, uint32_t version, uint32_t id)
359{
360 struct ivi_shell *shell = data;
361 struct wl_resource *resource;
362
363 resource = wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800364 &zwp_input_panel_v1_interface, 1, id);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900365
366 if (shell->input_panel.binding == NULL) {
367 wl_resource_set_implementation(resource,
368 &input_panel_implementation,
369 shell, unbind_input_panel);
370 shell->input_panel.binding = resource;
371 return;
372 }
373
374 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
375 "interface object already bound");
376}
377
378void
379input_panel_destroy(struct ivi_shell *shell)
380{
381 wl_list_remove(&shell->show_input_panel_listener.link);
382 wl_list_remove(&shell->hide_input_panel_listener.link);
383}
384
385int
386input_panel_setup(struct ivi_shell *shell)
387{
388 struct weston_compositor *ec = shell->compositor;
389
390 shell->show_input_panel_listener.notify = show_input_panels;
391 wl_signal_add(&ec->show_input_panel_signal,
392 &shell->show_input_panel_listener);
393 shell->hide_input_panel_listener.notify = hide_input_panels;
394 wl_signal_add(&ec->hide_input_panel_signal,
395 &shell->hide_input_panel_listener);
396 shell->update_input_panel_listener.notify = update_input_panels;
397 wl_signal_add(&ec->update_input_panel_signal,
398 &shell->update_input_panel_listener);
399
400 wl_list_init(&shell->input_panel.surfaces);
401
402 if (wl_global_create(shell->compositor->wl_display,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800403 &zwp_input_panel_v1_interface, 1,
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900404 shell, bind_input_panel) == NULL)
405 return -1;
406
407 return 0;
408}