blob: ecd497e01158396f75db8948ca277a9d050abcb7 [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) {
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
160static void
161input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
162{
163 struct input_panel_surface *ip_surface = surface->configure_private;
164 struct ivi_shell *shell = ip_surface->shell;
165 struct weston_view *view;
166 float x, y;
167
168 if (surface->width == 0)
169 return;
170
171 if (ip_surface->panel) {
172 view = get_default_view(shell->text_input.surface);
173 if (view == NULL)
174 return;
175 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
176 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
177 } else {
178 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
179 y = ip_surface->output->y + ip_surface->output->height - surface->height;
180 }
181
182 weston_view_set_position(ip_surface->view, x, y);
183
184 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
185 show_input_panel_surface(ip_surface);
186}
187
188static void
189destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
190{
191 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
192
193 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
194 wl_list_remove(&input_panel_surface->link);
195
196 input_panel_surface->surface->configure = NULL;
197 weston_view_destroy(input_panel_surface->view);
198
199 free(input_panel_surface);
200}
201
202static struct input_panel_surface *
203get_input_panel_surface(struct weston_surface *surface)
204{
205 if (surface->configure == input_panel_configure) {
206 return surface->configure_private;
207 } else {
208 return NULL;
209 }
210}
211
212static void
213input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
214{
215 struct input_panel_surface *ipsurface = container_of(listener,
216 struct input_panel_surface,
217 surface_destroy_listener);
218
219 if (ipsurface->resource) {
220 wl_resource_destroy(ipsurface->resource);
221 } else {
222 destroy_input_panel_surface(ipsurface);
223 }
224}
225
226static struct input_panel_surface *
227create_input_panel_surface(struct ivi_shell *shell,
228 struct weston_surface *surface)
229{
230 struct input_panel_surface *input_panel_surface;
231
232 input_panel_surface = calloc(1, sizeof *input_panel_surface);
233 if (!input_panel_surface)
234 return NULL;
235
236 surface->configure = input_panel_configure;
237 surface->configure_private = input_panel_surface;
238
239 input_panel_surface->shell = shell;
240
241 input_panel_surface->surface = surface;
242 input_panel_surface->view = weston_view_create(surface);
243
244 wl_signal_init(&input_panel_surface->destroy_signal);
245 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
246 wl_signal_add(&surface->destroy_signal,
247 &input_panel_surface->surface_destroy_listener);
248
249 wl_list_init(&input_panel_surface->link);
250
251 return input_panel_surface;
252}
253
254static void
255input_panel_surface_set_toplevel(struct wl_client *client,
256 struct wl_resource *resource,
257 struct wl_resource *output_resource,
258 uint32_t position)
259{
260 struct input_panel_surface *input_panel_surface =
261 wl_resource_get_user_data(resource);
262 struct ivi_shell *shell = input_panel_surface->shell;
263
264 wl_list_insert(&shell->input_panel.surfaces,
265 &input_panel_surface->link);
266
267 input_panel_surface->output = wl_resource_get_user_data(output_resource);
268 input_panel_surface->panel = 0;
269}
270
271static void
272input_panel_surface_set_overlay_panel(struct wl_client *client,
273 struct wl_resource *resource)
274{
275 struct input_panel_surface *input_panel_surface =
276 wl_resource_get_user_data(resource);
277 struct ivi_shell *shell = input_panel_surface->shell;
278
279 wl_list_insert(&shell->input_panel.surfaces,
280 &input_panel_surface->link);
281
282 input_panel_surface->panel = 1;
283}
284
285static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
286 input_panel_surface_set_toplevel,
287 input_panel_surface_set_overlay_panel
288};
289
290static void
291destroy_input_panel_surface_resource(struct wl_resource *resource)
292{
293 struct input_panel_surface *ipsurf =
294 wl_resource_get_user_data(resource);
295
296 destroy_input_panel_surface(ipsurf);
297}
298
299static void
300input_panel_get_input_panel_surface(struct wl_client *client,
301 struct wl_resource *resource,
302 uint32_t id,
303 struct wl_resource *surface_resource)
304{
305 struct weston_surface *surface =
306 wl_resource_get_user_data(surface_resource);
307 struct ivi_shell *shell = wl_resource_get_user_data(resource);
308 struct input_panel_surface *ipsurf;
309
310 if (get_input_panel_surface(surface)) {
311 wl_resource_post_error(surface_resource,
312 WL_DISPLAY_ERROR_INVALID_OBJECT,
313 "wl_input_panel::get_input_panel_surface already requested");
314 return;
315 }
316
317 ipsurf = create_input_panel_surface(shell, surface);
318 if (!ipsurf) {
319 wl_resource_post_error(surface_resource,
320 WL_DISPLAY_ERROR_INVALID_OBJECT,
321 "surface->configure already set");
322 return;
323 }
324
325 ipsurf->resource =
326 wl_resource_create(client,
327 &wl_input_panel_surface_interface, 1, id);
328 wl_resource_set_implementation(ipsurf->resource,
329 &input_panel_surface_implementation,
330 ipsurf,
331 destroy_input_panel_surface_resource);
332}
333
334static const struct wl_input_panel_interface input_panel_implementation = {
335 input_panel_get_input_panel_surface
336};
337
338static void
339unbind_input_panel(struct wl_resource *resource)
340{
341 struct ivi_shell *shell = wl_resource_get_user_data(resource);
342
343 shell->input_panel.binding = NULL;
344}
345
346static void
347bind_input_panel(struct wl_client *client,
348 void *data, uint32_t version, uint32_t id)
349{
350 struct ivi_shell *shell = data;
351 struct wl_resource *resource;
352
353 resource = wl_resource_create(client,
354 &wl_input_panel_interface, 1, id);
355
356 if (shell->input_panel.binding == NULL) {
357 wl_resource_set_implementation(resource,
358 &input_panel_implementation,
359 shell, unbind_input_panel);
360 shell->input_panel.binding = resource;
361 return;
362 }
363
364 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
365 "interface object already bound");
366}
367
368void
369input_panel_destroy(struct ivi_shell *shell)
370{
371 wl_list_remove(&shell->show_input_panel_listener.link);
372 wl_list_remove(&shell->hide_input_panel_listener.link);
373}
374
375int
376input_panel_setup(struct ivi_shell *shell)
377{
378 struct weston_compositor *ec = shell->compositor;
379
380 shell->show_input_panel_listener.notify = show_input_panels;
381 wl_signal_add(&ec->show_input_panel_signal,
382 &shell->show_input_panel_listener);
383 shell->hide_input_panel_listener.notify = hide_input_panels;
384 wl_signal_add(&ec->hide_input_panel_signal,
385 &shell->hide_input_panel_listener);
386 shell->update_input_panel_listener.notify = update_input_panels;
387 wl_signal_add(&ec->update_input_panel_signal,
388 &shell->update_input_panel_listener);
389
390 wl_list_init(&shell->input_panel.surfaces);
391
392 if (wl_global_create(shell->compositor->wl_display,
393 &wl_input_panel_interface, 1,
394 shell, bind_input_panel) == NULL)
395 return -1;
396
397 return 0;
398}