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