blob: 0008a52d337179dabea7df13ff7bdae2a7697605 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030031#include <stdint.h>
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090032#include <stdio.h>
33#include <string.h>
34
35#include "ivi-shell.h"
Jonas Ådahlb57f4722015-11-17 16:00:30 +080036#include "input-method-unstable-v1-server-protocol.h"
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090037#include "ivi-layout-private.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070038#include "shared/helpers.h"
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090039
40struct 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
57static void
58input_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
65static void
66show_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) {
Derek Foreman1281a362015-07-31 16:55:32 -050074 struct weston_keyboard *keyboard =
75 weston_seat_get_keyboard(seat);
76
77 if (!keyboard || !keyboard->focus)
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090078 continue;
Derek Foreman1281a362015-07-31 16:55:32 -050079 focus = weston_surface_get_main_surface(keyboard->focus);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090080 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);
Armin Krezović50ff4bf2016-06-30 06:04:31 +020090 ipsurf->surface->is_mapped = true;
91 ipsurf->view->is_mapped = true;
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +090092 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
103static void
104show_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)
Quentin Glidic82681572016-12-17 13:40:51 +0100119 weston_layer_set_position(&shell->input_panel_layer,
120 WESTON_LAYER_POSITION_TOP_UI);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900121
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
131static void
132hide_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)
Quentin Glidic82681572016-12-17 13:40:51 +0100145 weston_layer_unset_position(&shell->input_panel_layer);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900146
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
153static void
154update_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
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200163static int
164input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
165{
166 return snprintf(buf, len, "input panel");
167}
168
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900169static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200170input_panel_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900171{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200172 struct input_panel_surface *ip_surface = surface->committed_private;
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900173 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
197static void
198destroy_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
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200205 input_panel_surface->surface->committed = NULL;
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200206 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900207 weston_view_destroy(input_panel_surface->view);
208
209 free(input_panel_surface);
210}
211
212static struct input_panel_surface *
213get_input_panel_surface(struct weston_surface *surface)
214{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200215 if (surface->committed == input_panel_committed) {
216 return surface->committed_private;
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900217 } else {
218 return NULL;
219 }
220}
221
222static void
223input_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
236static struct input_panel_surface *
237create_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
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200246 surface->committed = input_panel_committed;
247 surface->committed_private = input_panel_surface;
Pekka Paalanencfb053f2016-03-07 16:25:33 +0200248 weston_surface_set_label_func(surface, input_panel_get_label);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900249
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
265static void
266input_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
Pekka Paalanen9ffb2502017-03-27 15:14:32 +0300278 input_panel_surface->output = weston_output_from_resource(output_resource);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900279 input_panel_surface->panel = 0;
280}
281
282static void
283input_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
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800296static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = {
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900297 input_panel_surface_set_toplevel,
298 input_panel_surface_set_overlay_panel
299};
300
301static void
302destroy_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
310static void
311input_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,
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200332 "surface->committed already set");
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900333 return;
334 }
335
336 ipsurf->resource =
337 wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800338 &zwp_input_panel_surface_v1_interface,
339 1,
340 id);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900341 wl_resource_set_implementation(ipsurf->resource,
342 &input_panel_surface_implementation,
343 ipsurf,
344 destroy_input_panel_surface_resource);
345}
346
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800347static const struct zwp_input_panel_v1_interface input_panel_implementation = {
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900348 input_panel_get_input_panel_surface
349};
350
351static void
352unbind_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
359static void
360bind_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,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800367 &zwp_input_panel_v1_interface, 1, id);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900368
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
381void
382input_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
388int
389input_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,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800406 &zwp_input_panel_v1_interface, 1,
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900407 shell, bind_input_panel) == NULL)
408 return -1;
409
410 return 0;
411}