blob: 7623f6cb06d146a2d0a23cf83c2514916ba75333 [file] [log] [blame]
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08001/*
2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
25#include "config.h"
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30
31#include "shell.h"
32#include "desktop-shell-server-protocol.h"
33#include "input-method-server-protocol.h"
34
35struct input_panel_surface {
36 struct wl_resource *resource;
37 struct wl_signal destroy_signal;
38
39 struct desktop_shell *shell;
40
41 struct wl_list link;
42 struct weston_surface *surface;
43 struct weston_view *view;
44 struct wl_listener surface_destroy_listener;
45
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030046 struct weston_view_animation *anim;
47
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080048 struct weston_output *output;
49 uint32_t panel;
50};
51
52static void
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030053input_panel_slide_done(struct weston_view_animation *animation, void *data)
54{
55 struct input_panel_surface *ipsurf = data;
56
57 ipsurf->anim = NULL;
58}
59
60static void
61show_input_panel_surface(struct input_panel_surface *ipsurf)
62{
63 struct desktop_shell *shell = ipsurf->shell;
Manuel Bachmann5082ad62014-04-17 14:04:32 +020064 struct weston_seat *seat;
65 struct weston_surface *focus;
66 float x, y;
67
68 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
69 if (!seat->keyboard)
70 continue;
71 focus = weston_surface_get_main_surface(seat->keyboard->focus);
72 ipsurf->output = focus->output;
73 x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2;
74 y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height;
75 weston_view_set_position(ipsurf->view, x, y);
76 }
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030077
78 wl_list_insert(&shell->input_panel_layer.view_list,
79 &ipsurf->view->layer_link);
80 weston_view_geometry_dirty(ipsurf->view);
81 weston_view_update_transform(ipsurf->view);
82 weston_surface_damage(ipsurf->surface);
83
84 if (ipsurf->anim)
85 weston_view_animation_destroy(ipsurf->anim);
86
87 ipsurf->anim =
88 weston_slide_run(ipsurf->view,
89 ipsurf->surface->height * 0.9, 0,
90 input_panel_slide_done, ipsurf);
91}
92
93static void
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080094show_input_panels(struct wl_listener *listener, void *data)
95{
96 struct desktop_shell *shell =
97 container_of(listener, struct desktop_shell,
98 show_input_panel_listener);
99 struct input_panel_surface *ipsurf, *next;
100
101 shell->text_input.surface = (struct weston_surface*)data;
102
103 if (shell->showing_input_panels)
104 return;
105
106 shell->showing_input_panels = true;
107
108 if (!shell->locked)
Manuel Bachmann805d2f52014-03-05 12:21:34 +0100109 wl_list_insert(&shell->compositor->cursor_layer.link,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800110 &shell->input_panel_layer.link);
111
112 wl_list_for_each_safe(ipsurf, next,
113 &shell->input_panel.surfaces, link) {
Kristian Høgsberg2eebcd32014-01-02 01:27:06 -0800114 if (ipsurf->surface->width == 0)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800115 continue;
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300116
117 show_input_panel_surface(ipsurf);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800118 }
119}
120
121static void
122hide_input_panels(struct wl_listener *listener, void *data)
123{
124 struct desktop_shell *shell =
125 container_of(listener, struct desktop_shell,
126 hide_input_panel_listener);
127 struct weston_view *view, *next;
128
129 if (!shell->showing_input_panels)
130 return;
131
132 shell->showing_input_panels = false;
133
134 if (!shell->locked)
135 wl_list_remove(&shell->input_panel_layer.link);
136
137 wl_list_for_each_safe(view, next,
138 &shell->input_panel_layer.view_list, layer_link)
139 weston_view_unmap(view);
140}
141
142static void
143update_input_panels(struct wl_listener *listener, void *data)
144{
145 struct desktop_shell *shell =
146 container_of(listener, struct desktop_shell,
147 update_input_panel_listener);
148
149 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
150}
151
152static void
153input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
154{
155 struct input_panel_surface *ip_surface = surface->configure_private;
156 struct desktop_shell *shell = ip_surface->shell;
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800157 struct weston_view *view;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800158 float x, y;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800159
160 if (surface->width == 0)
161 return;
162
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800163 if (ip_surface->panel) {
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800164 view = get_default_view(shell->text_input.surface);
165 if (view == NULL)
166 return;
167 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
168 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800169 } else {
170 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
171 y = ip_surface->output->y + ip_surface->output->height - surface->height;
172 }
173
174 weston_view_set_position(ip_surface->view, x, y);
175
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300176 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
177 show_input_panel_surface(ip_surface);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800178}
179
180static void
181destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
182{
183 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
184
185 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
186 wl_list_remove(&input_panel_surface->link);
187
188 input_panel_surface->surface->configure = NULL;
189 weston_view_destroy(input_panel_surface->view);
190
191 free(input_panel_surface);
192}
193
194static struct input_panel_surface *
195get_input_panel_surface(struct weston_surface *surface)
196{
197 if (surface->configure == input_panel_configure) {
198 return surface->configure_private;
199 } else {
200 return NULL;
201 }
202}
203
204static void
205input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
206{
207 struct input_panel_surface *ipsurface = container_of(listener,
208 struct input_panel_surface,
209 surface_destroy_listener);
210
211 if (ipsurface->resource) {
212 wl_resource_destroy(ipsurface->resource);
213 } else {
214 destroy_input_panel_surface(ipsurface);
215 }
216}
217
218static struct input_panel_surface *
219create_input_panel_surface(struct desktop_shell *shell,
220 struct weston_surface *surface)
221{
222 struct input_panel_surface *input_panel_surface;
223
224 input_panel_surface = calloc(1, sizeof *input_panel_surface);
225 if (!input_panel_surface)
226 return NULL;
227
228 surface->configure = input_panel_configure;
229 surface->configure_private = input_panel_surface;
230
231 input_panel_surface->shell = shell;
232
233 input_panel_surface->surface = surface;
234 input_panel_surface->view = weston_view_create(surface);
235
236 wl_signal_init(&input_panel_surface->destroy_signal);
237 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
238 wl_signal_add(&surface->destroy_signal,
239 &input_panel_surface->surface_destroy_listener);
240
241 wl_list_init(&input_panel_surface->link);
242
243 return input_panel_surface;
244}
245
246static void
247input_panel_surface_set_toplevel(struct wl_client *client,
248 struct wl_resource *resource,
249 struct wl_resource *output_resource,
250 uint32_t position)
251{
252 struct input_panel_surface *input_panel_surface =
253 wl_resource_get_user_data(resource);
254 struct desktop_shell *shell = input_panel_surface->shell;
255
256 wl_list_insert(&shell->input_panel.surfaces,
257 &input_panel_surface->link);
258
259 input_panel_surface->output = wl_resource_get_user_data(output_resource);
260 input_panel_surface->panel = 0;
261}
262
263static void
264input_panel_surface_set_overlay_panel(struct wl_client *client,
265 struct wl_resource *resource)
266{
267 struct input_panel_surface *input_panel_surface =
268 wl_resource_get_user_data(resource);
269 struct desktop_shell *shell = input_panel_surface->shell;
270
271 wl_list_insert(&shell->input_panel.surfaces,
272 &input_panel_surface->link);
273
274 input_panel_surface->panel = 1;
275}
276
277static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
278 input_panel_surface_set_toplevel,
279 input_panel_surface_set_overlay_panel
280};
281
282static void
283destroy_input_panel_surface_resource(struct wl_resource *resource)
284{
285 struct input_panel_surface *ipsurf =
286 wl_resource_get_user_data(resource);
287
288 destroy_input_panel_surface(ipsurf);
289}
290
291static void
292input_panel_get_input_panel_surface(struct wl_client *client,
293 struct wl_resource *resource,
294 uint32_t id,
295 struct wl_resource *surface_resource)
296{
297 struct weston_surface *surface =
298 wl_resource_get_user_data(surface_resource);
299 struct desktop_shell *shell = wl_resource_get_user_data(resource);
300 struct input_panel_surface *ipsurf;
301
302 if (get_input_panel_surface(surface)) {
303 wl_resource_post_error(surface_resource,
304 WL_DISPLAY_ERROR_INVALID_OBJECT,
305 "wl_input_panel::get_input_panel_surface already requested");
306 return;
307 }
308
309 ipsurf = create_input_panel_surface(shell, surface);
310 if (!ipsurf) {
311 wl_resource_post_error(surface_resource,
312 WL_DISPLAY_ERROR_INVALID_OBJECT,
313 "surface->configure already set");
314 return;
315 }
316
317 ipsurf->resource =
318 wl_resource_create(client,
319 &wl_input_panel_surface_interface, 1, id);
320 wl_resource_set_implementation(ipsurf->resource,
321 &input_panel_surface_implementation,
322 ipsurf,
323 destroy_input_panel_surface_resource);
324}
325
326static const struct wl_input_panel_interface input_panel_implementation = {
327 input_panel_get_input_panel_surface
328};
329
330static void
331unbind_input_panel(struct wl_resource *resource)
332{
333 struct desktop_shell *shell = wl_resource_get_user_data(resource);
334
335 shell->input_panel.binding = NULL;
336}
337
338static void
339bind_input_panel(struct wl_client *client,
340 void *data, uint32_t version, uint32_t id)
341{
342 struct desktop_shell *shell = data;
343 struct wl_resource *resource;
344
345 resource = wl_resource_create(client,
346 &wl_input_panel_interface, 1, id);
347
348 if (shell->input_panel.binding == NULL) {
349 wl_resource_set_implementation(resource,
350 &input_panel_implementation,
351 shell, unbind_input_panel);
352 shell->input_panel.binding = resource;
353 return;
354 }
355
356 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
357 "interface object already bound");
358 wl_resource_destroy(resource);
359}
360
361void
362input_panel_destroy(struct desktop_shell *shell)
363{
364 wl_list_remove(&shell->show_input_panel_listener.link);
365 wl_list_remove(&shell->hide_input_panel_listener.link);
366}
367
368int
369input_panel_setup(struct desktop_shell *shell)
370{
371 struct weston_compositor *ec = shell->compositor;
372
373 shell->show_input_panel_listener.notify = show_input_panels;
374 wl_signal_add(&ec->show_input_panel_signal,
375 &shell->show_input_panel_listener);
376 shell->hide_input_panel_listener.notify = hide_input_panels;
377 wl_signal_add(&ec->hide_input_panel_signal,
378 &shell->hide_input_panel_listener);
379 shell->update_input_panel_listener.notify = update_input_panels;
380 wl_signal_add(&ec->update_input_panel_signal,
381 &shell->update_input_panel_listener);
382
383 wl_list_init(&shell->input_panel.surfaces);
384
385 if (wl_global_create(shell->compositor->wl_display,
386 &wl_input_panel_interface, 1,
387 shell, bind_input_panel) == NULL)
388 return -1;
389
390 return 0;
391}