blob: 954d4ef802fd0e5a9fe2a8b1ee3b4d04d81e0e2a [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);
Armin Krezović50ff4bf2016-06-30 06:04:31 +020089 ipsurf->surface->is_mapped = true;
90 ipsurf->view->is_mapped = true;
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090091 weston_surface_damage(ipsurf->surface);
92
93 if (ipsurf->anim)
94 weston_view_animation_destroy(ipsurf->anim);
95
96 ipsurf->anim =
97 weston_slide_run(ipsurf->view,
98 ipsurf->surface->height * 0.9, 0,
99 input_panel_slide_done, ipsurf);
100}
101
102static void
103show_input_panels(struct wl_listener *listener, void *data)
104{
105 struct ivi_shell *shell =
106 container_of(listener, struct ivi_shell,
107 show_input_panel_listener);
108 struct input_panel_surface *ipsurf, *next;
109
110 shell->text_input.surface = (struct weston_surface*)data;
111
112 if (shell->showing_input_panels)
113 return;
114
115 shell->showing_input_panels = true;
116
117 if (!shell->locked)
118 wl_list_insert(&shell->compositor->cursor_layer.link,
119 &shell->input_panel_layer.link);
120
121 wl_list_for_each_safe(ipsurf, next,
122 &shell->input_panel.surfaces, link) {
123 if (ipsurf->surface->width == 0)
124 continue;
125
126 show_input_panel_surface(ipsurf);
127 }
128}
129
130static void
131hide_input_panels(struct wl_listener *listener, void *data)
132{
133 struct ivi_shell *shell =
134 container_of(listener, struct ivi_shell,
135 hide_input_panel_listener);
136 struct weston_view *view, *next;
137
138 if (!shell->showing_input_panels)
139 return;
140
141 shell->showing_input_panels = false;
142
143 if (!shell->locked)
144 wl_list_remove(&shell->input_panel_layer.link);
145
146 wl_list_for_each_safe(view, next,
147 &shell->input_panel_layer.view_list.link,
148 layer_link.link)
149 weston_view_unmap(view);
150}
151
152static void
153update_input_panels(struct wl_listener *listener, void *data)
154{
155 struct ivi_shell *shell =
156 container_of(listener, struct ivi_shell,
157 update_input_panel_listener);
158
159 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
160}
161
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200162static int
163input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
164{
165 return snprintf(buf, len, "input panel");
166}
167
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900168static void
169input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
170{
171 struct input_panel_surface *ip_surface = surface->configure_private;
172 struct ivi_shell *shell = ip_surface->shell;
173 struct weston_view *view;
174 float x, y;
175
176 if (surface->width == 0)
177 return;
178
179 if (ip_surface->panel) {
180 view = get_default_view(shell->text_input.surface);
181 if (view == NULL)
182 return;
183 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
184 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
185 } else {
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;
188 }
189
190 weston_view_set_position(ip_surface->view, x, y);
191
192 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
193 show_input_panel_surface(ip_surface);
194}
195
196static void
197destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
198{
199 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
200
201 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
202 wl_list_remove(&input_panel_surface->link);
203
204 input_panel_surface->surface->configure = NULL;
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200205 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900206 weston_view_destroy(input_panel_surface->view);
207
208 free(input_panel_surface);
209}
210
211static struct input_panel_surface *
212get_input_panel_surface(struct weston_surface *surface)
213{
214 if (surface->configure == input_panel_configure) {
215 return surface->configure_private;
216 } else {
217 return NULL;
218 }
219}
220
221static void
222input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
223{
224 struct input_panel_surface *ipsurface = container_of(listener,
225 struct input_panel_surface,
226 surface_destroy_listener);
227
228 if (ipsurface->resource) {
229 wl_resource_destroy(ipsurface->resource);
230 } else {
231 destroy_input_panel_surface(ipsurface);
232 }
233}
234
235static struct input_panel_surface *
236create_input_panel_surface(struct ivi_shell *shell,
237 struct weston_surface *surface)
238{
239 struct input_panel_surface *input_panel_surface;
240
241 input_panel_surface = calloc(1, sizeof *input_panel_surface);
242 if (!input_panel_surface)
243 return NULL;
244
245 surface->configure = input_panel_configure;
246 surface->configure_private = input_panel_surface;
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200247 weston_surface_set_label_func(surface, input_panel_get_label);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900248
249 input_panel_surface->shell = shell;
250
251 input_panel_surface->surface = surface;
252 input_panel_surface->view = weston_view_create(surface);
253
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);
258
259 wl_list_init(&input_panel_surface->link);
260
261 return input_panel_surface;
262}
263
264static void
265input_panel_surface_set_toplevel(struct wl_client *client,
266 struct wl_resource *resource,
267 struct wl_resource *output_resource,
268 uint32_t position)
269{
270 struct input_panel_surface *input_panel_surface =
271 wl_resource_get_user_data(resource);
272 struct ivi_shell *shell = input_panel_surface->shell;
273
274 wl_list_insert(&shell->input_panel.surfaces,
275 &input_panel_surface->link);
276
277 input_panel_surface->output = wl_resource_get_user_data(output_resource);
278 input_panel_surface->panel = 0;
279}
280
281static void
282input_panel_surface_set_overlay_panel(struct wl_client *client,
283 struct wl_resource *resource)
284{
285 struct input_panel_surface *input_panel_surface =
286 wl_resource_get_user_data(resource);
287 struct ivi_shell *shell = input_panel_surface->shell;
288
289 wl_list_insert(&shell->input_panel.surfaces,
290 &input_panel_surface->link);
291
292 input_panel_surface->panel = 1;
293}
294
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800295static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = {
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900296 input_panel_surface_set_toplevel,
297 input_panel_surface_set_overlay_panel
298};
299
300static void
301destroy_input_panel_surface_resource(struct wl_resource *resource)
302{
303 struct input_panel_surface *ipsurf =
304 wl_resource_get_user_data(resource);
305
306 destroy_input_panel_surface(ipsurf);
307}
308
309static void
310input_panel_get_input_panel_surface(struct wl_client *client,
311 struct wl_resource *resource,
312 uint32_t id,
313 struct wl_resource *surface_resource)
314{
315 struct weston_surface *surface =
316 wl_resource_get_user_data(surface_resource);
317 struct ivi_shell *shell = wl_resource_get_user_data(resource);
318 struct input_panel_surface *ipsurf;
319
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");
324 return;
325 }
326
327 ipsurf = create_input_panel_surface(shell, surface);
328 if (!ipsurf) {
329 wl_resource_post_error(surface_resource,
330 WL_DISPLAY_ERROR_INVALID_OBJECT,
331 "surface->configure already set");
332 return;
333 }
334
335 ipsurf->resource =
336 wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800337 &zwp_input_panel_surface_v1_interface,
338 1,
339 id);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900340 wl_resource_set_implementation(ipsurf->resource,
341 &input_panel_surface_implementation,
342 ipsurf,
343 destroy_input_panel_surface_resource);
344}
345
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800346static const struct zwp_input_panel_v1_interface input_panel_implementation = {
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900347 input_panel_get_input_panel_surface
348};
349
350static void
351unbind_input_panel(struct wl_resource *resource)
352{
353 struct ivi_shell *shell = wl_resource_get_user_data(resource);
354
355 shell->input_panel.binding = NULL;
356}
357
358static void
359bind_input_panel(struct wl_client *client,
360 void *data, uint32_t version, uint32_t id)
361{
362 struct ivi_shell *shell = data;
363 struct wl_resource *resource;
364
365 resource = wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800366 &zwp_input_panel_v1_interface, 1, id);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900367
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;
373 return;
374 }
375
376 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
377 "interface object already bound");
378}
379
380void
381input_panel_destroy(struct ivi_shell *shell)
382{
383 wl_list_remove(&shell->show_input_panel_listener.link);
384 wl_list_remove(&shell->hide_input_panel_listener.link);
385}
386
387int
388input_panel_setup(struct ivi_shell *shell)
389{
390 struct weston_compositor *ec = shell->compositor;
391
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);
401
402 wl_list_init(&shell->input_panel.surfaces);
403
404 if (wl_global_create(shell->compositor->wl_display,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800405 &zwp_input_panel_v1_interface, 1,
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900406 shell, bind_input_panel) == NULL)
407 return -1;
408
409 return 0;
410}