blob: c624ef378fc1f96f92c3b8beeddd61668e634d5f [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"
35#include "input-method-server-protocol.h"
36#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) {
73 if (!seat->keyboard || !seat->keyboard->focus)
74 continue;
75 focus = weston_surface_get_main_surface(seat->keyboard->focus);
76 ipsurf->output = focus->output;
77 x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2;
78 y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height;
79 weston_view_set_position(ipsurf->view, x, y);
80 }
81
82 weston_layer_entry_insert(&shell->input_panel_layer.view_list,
83 &ipsurf->view->layer_link);
84 weston_view_geometry_dirty(ipsurf->view);
85 weston_view_update_transform(ipsurf->view);
86 weston_surface_damage(ipsurf->surface);
87
88 if (ipsurf->anim)
89 weston_view_animation_destroy(ipsurf->anim);
90
91 ipsurf->anim =
92 weston_slide_run(ipsurf->view,
93 ipsurf->surface->height * 0.9, 0,
94 input_panel_slide_done, ipsurf);
95}
96
97static void
98show_input_panels(struct wl_listener *listener, void *data)
99{
100 struct ivi_shell *shell =
101 container_of(listener, struct ivi_shell,
102 show_input_panel_listener);
103 struct input_panel_surface *ipsurf, *next;
104
105 shell->text_input.surface = (struct weston_surface*)data;
106
107 if (shell->showing_input_panels)
108 return;
109
110 shell->showing_input_panels = true;
111
112 if (!shell->locked)
113 wl_list_insert(&shell->compositor->cursor_layer.link,
114 &shell->input_panel_layer.link);
115
116 wl_list_for_each_safe(ipsurf, next,
117 &shell->input_panel.surfaces, link) {
118 if (ipsurf->surface->width == 0)
119 continue;
120
121 show_input_panel_surface(ipsurf);
122 }
123}
124
125static void
126hide_input_panels(struct wl_listener *listener, void *data)
127{
128 struct ivi_shell *shell =
129 container_of(listener, struct ivi_shell,
130 hide_input_panel_listener);
131 struct weston_view *view, *next;
132
133 if (!shell->showing_input_panels)
134 return;
135
136 shell->showing_input_panels = false;
137
138 if (!shell->locked)
139 wl_list_remove(&shell->input_panel_layer.link);
140
141 wl_list_for_each_safe(view, next,
142 &shell->input_panel_layer.view_list.link,
143 layer_link.link)
144 weston_view_unmap(view);
145}
146
147static void
148update_input_panels(struct wl_listener *listener, void *data)
149{
150 struct ivi_shell *shell =
151 container_of(listener, struct ivi_shell,
152 update_input_panel_listener);
153
154 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
155}
156
157static void
158input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
159{
160 struct input_panel_surface *ip_surface = surface->configure_private;
161 struct ivi_shell *shell = ip_surface->shell;
162 struct weston_view *view;
163 float x, y;
164
165 if (surface->width == 0)
166 return;
167
168 if (ip_surface->panel) {
169 view = get_default_view(shell->text_input.surface);
170 if (view == NULL)
171 return;
172 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
173 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
174 } else {
175 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
176 y = ip_surface->output->y + ip_surface->output->height - surface->height;
177 }
178
179 weston_view_set_position(ip_surface->view, x, y);
180
181 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
182 show_input_panel_surface(ip_surface);
183}
184
185static void
186destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
187{
188 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
189
190 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
191 wl_list_remove(&input_panel_surface->link);
192
193 input_panel_surface->surface->configure = NULL;
194 weston_view_destroy(input_panel_surface->view);
195
196 free(input_panel_surface);
197}
198
199static struct input_panel_surface *
200get_input_panel_surface(struct weston_surface *surface)
201{
202 if (surface->configure == input_panel_configure) {
203 return surface->configure_private;
204 } else {
205 return NULL;
206 }
207}
208
209static void
210input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
211{
212 struct input_panel_surface *ipsurface = container_of(listener,
213 struct input_panel_surface,
214 surface_destroy_listener);
215
216 if (ipsurface->resource) {
217 wl_resource_destroy(ipsurface->resource);
218 } else {
219 destroy_input_panel_surface(ipsurface);
220 }
221}
222
223static struct input_panel_surface *
224create_input_panel_surface(struct ivi_shell *shell,
225 struct weston_surface *surface)
226{
227 struct input_panel_surface *input_panel_surface;
228
229 input_panel_surface = calloc(1, sizeof *input_panel_surface);
230 if (!input_panel_surface)
231 return NULL;
232
233 surface->configure = input_panel_configure;
234 surface->configure_private = input_panel_surface;
235
236 input_panel_surface->shell = shell;
237
238 input_panel_surface->surface = surface;
239 input_panel_surface->view = weston_view_create(surface);
240
241 wl_signal_init(&input_panel_surface->destroy_signal);
242 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
243 wl_signal_add(&surface->destroy_signal,
244 &input_panel_surface->surface_destroy_listener);
245
246 wl_list_init(&input_panel_surface->link);
247
248 return input_panel_surface;
249}
250
251static void
252input_panel_surface_set_toplevel(struct wl_client *client,
253 struct wl_resource *resource,
254 struct wl_resource *output_resource,
255 uint32_t position)
256{
257 struct input_panel_surface *input_panel_surface =
258 wl_resource_get_user_data(resource);
259 struct ivi_shell *shell = input_panel_surface->shell;
260
261 wl_list_insert(&shell->input_panel.surfaces,
262 &input_panel_surface->link);
263
264 input_panel_surface->output = wl_resource_get_user_data(output_resource);
265 input_panel_surface->panel = 0;
266}
267
268static void
269input_panel_surface_set_overlay_panel(struct wl_client *client,
270 struct wl_resource *resource)
271{
272 struct input_panel_surface *input_panel_surface =
273 wl_resource_get_user_data(resource);
274 struct ivi_shell *shell = input_panel_surface->shell;
275
276 wl_list_insert(&shell->input_panel.surfaces,
277 &input_panel_surface->link);
278
279 input_panel_surface->panel = 1;
280}
281
282static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
283 input_panel_surface_set_toplevel,
284 input_panel_surface_set_overlay_panel
285};
286
287static void
288destroy_input_panel_surface_resource(struct wl_resource *resource)
289{
290 struct input_panel_surface *ipsurf =
291 wl_resource_get_user_data(resource);
292
293 destroy_input_panel_surface(ipsurf);
294}
295
296static void
297input_panel_get_input_panel_surface(struct wl_client *client,
298 struct wl_resource *resource,
299 uint32_t id,
300 struct wl_resource *surface_resource)
301{
302 struct weston_surface *surface =
303 wl_resource_get_user_data(surface_resource);
304 struct ivi_shell *shell = wl_resource_get_user_data(resource);
305 struct input_panel_surface *ipsurf;
306
307 if (get_input_panel_surface(surface)) {
308 wl_resource_post_error(surface_resource,
309 WL_DISPLAY_ERROR_INVALID_OBJECT,
310 "wl_input_panel::get_input_panel_surface already requested");
311 return;
312 }
313
314 ipsurf = create_input_panel_surface(shell, surface);
315 if (!ipsurf) {
316 wl_resource_post_error(surface_resource,
317 WL_DISPLAY_ERROR_INVALID_OBJECT,
318 "surface->configure already set");
319 return;
320 }
321
322 ipsurf->resource =
323 wl_resource_create(client,
324 &wl_input_panel_surface_interface, 1, id);
325 wl_resource_set_implementation(ipsurf->resource,
326 &input_panel_surface_implementation,
327 ipsurf,
328 destroy_input_panel_surface_resource);
329}
330
331static const struct wl_input_panel_interface input_panel_implementation = {
332 input_panel_get_input_panel_surface
333};
334
335static void
336unbind_input_panel(struct wl_resource *resource)
337{
338 struct ivi_shell *shell = wl_resource_get_user_data(resource);
339
340 shell->input_panel.binding = NULL;
341}
342
343static void
344bind_input_panel(struct wl_client *client,
345 void *data, uint32_t version, uint32_t id)
346{
347 struct ivi_shell *shell = data;
348 struct wl_resource *resource;
349
350 resource = wl_resource_create(client,
351 &wl_input_panel_interface, 1, id);
352
353 if (shell->input_panel.binding == NULL) {
354 wl_resource_set_implementation(resource,
355 &input_panel_implementation,
356 shell, unbind_input_panel);
357 shell->input_panel.binding = resource;
358 return;
359 }
360
361 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
362 "interface object already bound");
363}
364
365void
366input_panel_destroy(struct ivi_shell *shell)
367{
368 wl_list_remove(&shell->show_input_panel_listener.link);
369 wl_list_remove(&shell->hide_input_panel_listener.link);
370}
371
372int
373input_panel_setup(struct ivi_shell *shell)
374{
375 struct weston_compositor *ec = shell->compositor;
376
377 shell->show_input_panel_listener.notify = show_input_panels;
378 wl_signal_add(&ec->show_input_panel_signal,
379 &shell->show_input_panel_listener);
380 shell->hide_input_panel_listener.notify = hide_input_panels;
381 wl_signal_add(&ec->hide_input_panel_signal,
382 &shell->hide_input_panel_listener);
383 shell->update_input_panel_listener.notify = update_input_panels;
384 wl_signal_add(&ec->update_input_panel_signal,
385 &shell->update_input_panel_listener);
386
387 wl_list_init(&shell->input_panel.surfaces);
388
389 if (wl_global_create(shell->compositor->wl_display,
390 &wl_input_panel_interface, 1,
391 shell, bind_input_panel) == NULL)
392 return -1;
393
394 return 0;
395}