blob: aa95b9bb5415382b16a2e73260637df2816ac53c [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 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080024 */
25
26#include "config.h"
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31
32#include "shell.h"
33#include "desktop-shell-server-protocol.h"
34#include "input-method-server-protocol.h"
35
36struct input_panel_surface {
37 struct wl_resource *resource;
38 struct wl_signal destroy_signal;
39
40 struct desktop_shell *shell;
41
42 struct wl_list link;
43 struct weston_surface *surface;
44 struct weston_view *view;
45 struct wl_listener surface_destroy_listener;
46
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030047 struct weston_view_animation *anim;
48
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080049 struct weston_output *output;
50 uint32_t panel;
51};
52
53static void
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030054input_panel_slide_done(struct weston_view_animation *animation, void *data)
55{
56 struct input_panel_surface *ipsurf = data;
57
58 ipsurf->anim = NULL;
59}
60
61static void
62show_input_panel_surface(struct input_panel_surface *ipsurf)
63{
64 struct desktop_shell *shell = ipsurf->shell;
Manuel Bachmann5082ad62014-04-17 14:04:32 +020065 struct weston_seat *seat;
66 struct weston_surface *focus;
67 float x, y;
68
69 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Pekka Paalanena4bac9e2014-11-20 10:15:07 +020070 if (!seat->keyboard || !seat->keyboard->focus)
Manuel Bachmann5082ad62014-04-17 14:04:32 +020071 continue;
72 focus = weston_surface_get_main_surface(seat->keyboard->focus);
73 ipsurf->output = focus->output;
74 x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2;
75 y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height;
76 weston_view_set_position(ipsurf->view, x, y);
77 }
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030078
Giulio Camuffo412e6a52014-07-09 22:12:56 +030079 weston_layer_entry_insert(&shell->input_panel_layer.view_list,
80 &ipsurf->view->layer_link);
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030081 weston_view_geometry_dirty(ipsurf->view);
82 weston_view_update_transform(ipsurf->view);
83 weston_surface_damage(ipsurf->surface);
84
85 if (ipsurf->anim)
86 weston_view_animation_destroy(ipsurf->anim);
87
88 ipsurf->anim =
89 weston_slide_run(ipsurf->view,
90 ipsurf->surface->height * 0.9, 0,
91 input_panel_slide_done, ipsurf);
92}
93
94static void
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080095show_input_panels(struct wl_listener *listener, void *data)
96{
97 struct desktop_shell *shell =
98 container_of(listener, struct desktop_shell,
99 show_input_panel_listener);
100 struct input_panel_surface *ipsurf, *next;
101
102 shell->text_input.surface = (struct weston_surface*)data;
103
104 if (shell->showing_input_panels)
105 return;
106
107 shell->showing_input_panels = true;
108
109 if (!shell->locked)
Manuel Bachmann805d2f52014-03-05 12:21:34 +0100110 wl_list_insert(&shell->compositor->cursor_layer.link,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800111 &shell->input_panel_layer.link);
112
113 wl_list_for_each_safe(ipsurf, next,
114 &shell->input_panel.surfaces, link) {
Kristian Høgsberg2eebcd32014-01-02 01:27:06 -0800115 if (ipsurf->surface->width == 0)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800116 continue;
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300117
118 show_input_panel_surface(ipsurf);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800119 }
120}
121
122static void
123hide_input_panels(struct wl_listener *listener, void *data)
124{
125 struct desktop_shell *shell =
126 container_of(listener, struct desktop_shell,
127 hide_input_panel_listener);
128 struct weston_view *view, *next;
129
130 if (!shell->showing_input_panels)
131 return;
132
133 shell->showing_input_panels = false;
134
135 if (!shell->locked)
136 wl_list_remove(&shell->input_panel_layer.link);
137
138 wl_list_for_each_safe(view, next,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300139 &shell->input_panel_layer.view_list.link,
140 layer_link.link)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800141 weston_view_unmap(view);
142}
143
144static void
145update_input_panels(struct wl_listener *listener, void *data)
146{
147 struct desktop_shell *shell =
148 container_of(listener, struct desktop_shell,
149 update_input_panel_listener);
150
151 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
152}
153
Pekka Paalanen8274d902014-08-06 19:36:51 +0300154static int
155input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
156{
157 return snprintf(buf, len, "input panel");
158}
159
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800160static 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 desktop_shell *shell = ip_surface->shell;
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800165 struct weston_view *view;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800166 float x, y;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800167
168 if (surface->width == 0)
169 return;
170
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800171 if (ip_surface->panel) {
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800172 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;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800177 } 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
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300184 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
185 show_input_panel_surface(ip_surface);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800186}
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;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300197 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800198 weston_view_destroy(input_panel_surface->view);
199
200 free(input_panel_surface);
201}
202
203static struct input_panel_surface *
204get_input_panel_surface(struct weston_surface *surface)
205{
206 if (surface->configure == input_panel_configure) {
207 return surface->configure_private;
208 } else {
209 return NULL;
210 }
211}
212
213static void
214input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
215{
216 struct input_panel_surface *ipsurface = container_of(listener,
217 struct input_panel_surface,
218 surface_destroy_listener);
219
220 if (ipsurface->resource) {
221 wl_resource_destroy(ipsurface->resource);
222 } else {
223 destroy_input_panel_surface(ipsurface);
224 }
225}
226
227static struct input_panel_surface *
228create_input_panel_surface(struct desktop_shell *shell,
229 struct weston_surface *surface)
230{
231 struct input_panel_surface *input_panel_surface;
232
233 input_panel_surface = calloc(1, sizeof *input_panel_surface);
234 if (!input_panel_surface)
235 return NULL;
236
237 surface->configure = input_panel_configure;
238 surface->configure_private = input_panel_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300239 weston_surface_set_label_func(surface, input_panel_get_label);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800240
241 input_panel_surface->shell = shell;
242
243 input_panel_surface->surface = surface;
244 input_panel_surface->view = weston_view_create(surface);
245
246 wl_signal_init(&input_panel_surface->destroy_signal);
247 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
248 wl_signal_add(&surface->destroy_signal,
249 &input_panel_surface->surface_destroy_listener);
250
251 wl_list_init(&input_panel_surface->link);
252
253 return input_panel_surface;
254}
255
256static void
257input_panel_surface_set_toplevel(struct wl_client *client,
258 struct wl_resource *resource,
259 struct wl_resource *output_resource,
260 uint32_t position)
261{
262 struct input_panel_surface *input_panel_surface =
263 wl_resource_get_user_data(resource);
264 struct desktop_shell *shell = input_panel_surface->shell;
265
266 wl_list_insert(&shell->input_panel.surfaces,
267 &input_panel_surface->link);
268
269 input_panel_surface->output = wl_resource_get_user_data(output_resource);
270 input_panel_surface->panel = 0;
271}
272
273static void
274input_panel_surface_set_overlay_panel(struct wl_client *client,
275 struct wl_resource *resource)
276{
277 struct input_panel_surface *input_panel_surface =
278 wl_resource_get_user_data(resource);
279 struct desktop_shell *shell = input_panel_surface->shell;
280
281 wl_list_insert(&shell->input_panel.surfaces,
282 &input_panel_surface->link);
283
284 input_panel_surface->panel = 1;
285}
286
287static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
288 input_panel_surface_set_toplevel,
289 input_panel_surface_set_overlay_panel
290};
291
292static void
293destroy_input_panel_surface_resource(struct wl_resource *resource)
294{
295 struct input_panel_surface *ipsurf =
296 wl_resource_get_user_data(resource);
297
298 destroy_input_panel_surface(ipsurf);
299}
300
301static void
302input_panel_get_input_panel_surface(struct wl_client *client,
303 struct wl_resource *resource,
304 uint32_t id,
305 struct wl_resource *surface_resource)
306{
307 struct weston_surface *surface =
308 wl_resource_get_user_data(surface_resource);
309 struct desktop_shell *shell = wl_resource_get_user_data(resource);
310 struct input_panel_surface *ipsurf;
311
312 if (get_input_panel_surface(surface)) {
313 wl_resource_post_error(surface_resource,
314 WL_DISPLAY_ERROR_INVALID_OBJECT,
315 "wl_input_panel::get_input_panel_surface already requested");
316 return;
317 }
318
319 ipsurf = create_input_panel_surface(shell, surface);
320 if (!ipsurf) {
321 wl_resource_post_error(surface_resource,
322 WL_DISPLAY_ERROR_INVALID_OBJECT,
323 "surface->configure already set");
324 return;
325 }
326
327 ipsurf->resource =
328 wl_resource_create(client,
329 &wl_input_panel_surface_interface, 1, id);
330 wl_resource_set_implementation(ipsurf->resource,
331 &input_panel_surface_implementation,
332 ipsurf,
333 destroy_input_panel_surface_resource);
334}
335
336static const struct wl_input_panel_interface input_panel_implementation = {
337 input_panel_get_input_panel_surface
338};
339
340static void
341unbind_input_panel(struct wl_resource *resource)
342{
343 struct desktop_shell *shell = wl_resource_get_user_data(resource);
344
345 shell->input_panel.binding = NULL;
346}
347
348static void
349bind_input_panel(struct wl_client *client,
350 void *data, uint32_t version, uint32_t id)
351{
352 struct desktop_shell *shell = data;
353 struct wl_resource *resource;
354
355 resource = wl_resource_create(client,
356 &wl_input_panel_interface, 1, id);
357
358 if (shell->input_panel.binding == NULL) {
359 wl_resource_set_implementation(resource,
360 &input_panel_implementation,
361 shell, unbind_input_panel);
362 shell->input_panel.binding = resource;
363 return;
364 }
365
366 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
367 "interface object already bound");
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800368}
369
370void
371input_panel_destroy(struct desktop_shell *shell)
372{
373 wl_list_remove(&shell->show_input_panel_listener.link);
374 wl_list_remove(&shell->hide_input_panel_listener.link);
375}
376
377int
378input_panel_setup(struct desktop_shell *shell)
379{
380 struct weston_compositor *ec = shell->compositor;
381
382 shell->show_input_panel_listener.notify = show_input_panels;
383 wl_signal_add(&ec->show_input_panel_signal,
384 &shell->show_input_panel_listener);
385 shell->hide_input_panel_listener.notify = hide_input_panels;
386 wl_signal_add(&ec->hide_input_panel_signal,
387 &shell->hide_input_panel_listener);
388 shell->update_input_panel_listener.notify = update_input_panels;
389 wl_signal_add(&ec->update_input_panel_signal,
390 &shell->update_input_panel_listener);
391
392 wl_list_init(&shell->input_panel.surfaces);
393
394 if (wl_global_create(shell->compositor->wl_display,
395 &wl_input_panel_interface, 1,
396 shell, bind_input_panel) == NULL)
397 return -1;
398
399 return 0;
400}