blob: 0d003b1a3f07df3e41d2da1ea5fc84b132e51491 [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"
Jonas Ådahlb57f4722015-11-17 16:00:30 +080033#include "input-method-unstable-v1-server-protocol.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070034#include "shared/helpers.h"
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080035
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) {
Derek Foreman1281a362015-07-31 16:55:32 -050070 struct weston_keyboard *keyboard =
71 weston_seat_get_keyboard(seat);
72
73 if (!keyboard || !keyboard->focus)
Manuel Bachmann5082ad62014-04-17 14:04:32 +020074 continue;
Derek Foreman1281a362015-07-31 16:55:32 -050075 focus = weston_surface_get_main_surface(keyboard->focus);
Nicolas Guyomard48453542015-10-06 10:40:28 -050076 if (!focus)
77 continue;
Manuel Bachmann5082ad62014-04-17 14:04:32 +020078 ipsurf->output = focus->output;
79 x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2;
80 y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height;
81 weston_view_set_position(ipsurf->view, x, y);
82 }
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030083
Giulio Camuffo412e6a52014-07-09 22:12:56 +030084 weston_layer_entry_insert(&shell->input_panel_layer.view_list,
85 &ipsurf->view->layer_link);
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030086 weston_view_geometry_dirty(ipsurf->view);
87 weston_view_update_transform(ipsurf->view);
Armin Krezović4663aca2016-06-30 06:04:29 +020088 ipsurf->surface->is_mapped = true;
89 ipsurf->view->is_mapped = true;
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030090 weston_surface_damage(ipsurf->surface);
91
92 if (ipsurf->anim)
93 weston_view_animation_destroy(ipsurf->anim);
94
95 ipsurf->anim =
96 weston_slide_run(ipsurf->view,
97 ipsurf->surface->height * 0.9, 0,
98 input_panel_slide_done, ipsurf);
99}
100
101static void
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800102show_input_panels(struct wl_listener *listener, void *data)
103{
104 struct desktop_shell *shell =
105 container_of(listener, struct desktop_shell,
106 show_input_panel_listener);
107 struct input_panel_surface *ipsurf, *next;
108
109 shell->text_input.surface = (struct weston_surface*)data;
110
111 if (shell->showing_input_panels)
112 return;
113
114 shell->showing_input_panels = true;
115
116 if (!shell->locked)
Manuel Bachmann805d2f52014-03-05 12:21:34 +0100117 wl_list_insert(&shell->compositor->cursor_layer.link,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800118 &shell->input_panel_layer.link);
119
120 wl_list_for_each_safe(ipsurf, next,
121 &shell->input_panel.surfaces, link) {
Kristian Høgsberg2eebcd32014-01-02 01:27:06 -0800122 if (ipsurf->surface->width == 0)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800123 continue;
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300124
125 show_input_panel_surface(ipsurf);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800126 }
127}
128
129static void
130hide_input_panels(struct wl_listener *listener, void *data)
131{
132 struct desktop_shell *shell =
133 container_of(listener, struct desktop_shell,
134 hide_input_panel_listener);
135 struct weston_view *view, *next;
136
137 if (!shell->showing_input_panels)
138 return;
139
140 shell->showing_input_panels = false;
141
142 if (!shell->locked)
143 wl_list_remove(&shell->input_panel_layer.link);
144
145 wl_list_for_each_safe(view, next,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300146 &shell->input_panel_layer.view_list.link,
147 layer_link.link)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800148 weston_view_unmap(view);
149}
150
151static void
152update_input_panels(struct wl_listener *listener, void *data)
153{
154 struct desktop_shell *shell =
155 container_of(listener, struct desktop_shell,
156 update_input_panel_listener);
157
158 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
159}
160
Pekka Paalanen8274d902014-08-06 19:36:51 +0300161static int
162input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
163{
164 return snprintf(buf, len, "input panel");
165}
166
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800167static void
168input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
169{
170 struct input_panel_surface *ip_surface = surface->configure_private;
171 struct desktop_shell *shell = ip_surface->shell;
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800172 struct weston_view *view;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800173 float x, y;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800174
175 if (surface->width == 0)
176 return;
177
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800178 if (ip_surface->panel) {
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800179 view = get_default_view(shell->text_input.surface);
180 if (view == NULL)
181 return;
182 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
183 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800184 } else {
185 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
186 y = ip_surface->output->y + ip_surface->output->height - surface->height;
187 }
188
189 weston_view_set_position(ip_surface->view, x, y);
190
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300191 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
192 show_input_panel_surface(ip_surface);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800193}
194
195static void
196destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
197{
198 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
199
200 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
201 wl_list_remove(&input_panel_surface->link);
202
203 input_panel_surface->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300204 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800205 weston_view_destroy(input_panel_surface->view);
206
207 free(input_panel_surface);
208}
209
210static struct input_panel_surface *
211get_input_panel_surface(struct weston_surface *surface)
212{
213 if (surface->configure == input_panel_configure) {
214 return surface->configure_private;
215 } else {
216 return NULL;
217 }
218}
219
220static void
221input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
222{
223 struct input_panel_surface *ipsurface = container_of(listener,
224 struct input_panel_surface,
225 surface_destroy_listener);
226
227 if (ipsurface->resource) {
228 wl_resource_destroy(ipsurface->resource);
229 } else {
230 destroy_input_panel_surface(ipsurface);
231 }
232}
233
234static struct input_panel_surface *
235create_input_panel_surface(struct desktop_shell *shell,
236 struct weston_surface *surface)
237{
238 struct input_panel_surface *input_panel_surface;
239
240 input_panel_surface = calloc(1, sizeof *input_panel_surface);
241 if (!input_panel_surface)
242 return NULL;
243
244 surface->configure = input_panel_configure;
245 surface->configure_private = input_panel_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300246 weston_surface_set_label_func(surface, input_panel_get_label);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800247
248 input_panel_surface->shell = shell;
249
250 input_panel_surface->surface = surface;
251 input_panel_surface->view = weston_view_create(surface);
252
253 wl_signal_init(&input_panel_surface->destroy_signal);
254 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
255 wl_signal_add(&surface->destroy_signal,
256 &input_panel_surface->surface_destroy_listener);
257
258 wl_list_init(&input_panel_surface->link);
259
260 return input_panel_surface;
261}
262
263static void
264input_panel_surface_set_toplevel(struct wl_client *client,
265 struct wl_resource *resource,
266 struct wl_resource *output_resource,
267 uint32_t position)
268{
269 struct input_panel_surface *input_panel_surface =
270 wl_resource_get_user_data(resource);
271 struct desktop_shell *shell = input_panel_surface->shell;
272
273 wl_list_insert(&shell->input_panel.surfaces,
274 &input_panel_surface->link);
275
276 input_panel_surface->output = wl_resource_get_user_data(output_resource);
277 input_panel_surface->panel = 0;
278}
279
280static void
281input_panel_surface_set_overlay_panel(struct wl_client *client,
282 struct wl_resource *resource)
283{
284 struct input_panel_surface *input_panel_surface =
285 wl_resource_get_user_data(resource);
286 struct desktop_shell *shell = input_panel_surface->shell;
287
288 wl_list_insert(&shell->input_panel.surfaces,
289 &input_panel_surface->link);
290
291 input_panel_surface->panel = 1;
292}
293
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800294static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = {
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800295 input_panel_surface_set_toplevel,
296 input_panel_surface_set_overlay_panel
297};
298
299static void
300destroy_input_panel_surface_resource(struct wl_resource *resource)
301{
302 struct input_panel_surface *ipsurf =
303 wl_resource_get_user_data(resource);
304
305 destroy_input_panel_surface(ipsurf);
306}
307
308static void
309input_panel_get_input_panel_surface(struct wl_client *client,
310 struct wl_resource *resource,
311 uint32_t id,
312 struct wl_resource *surface_resource)
313{
314 struct weston_surface *surface =
315 wl_resource_get_user_data(surface_resource);
316 struct desktop_shell *shell = wl_resource_get_user_data(resource);
317 struct input_panel_surface *ipsurf;
318
319 if (get_input_panel_surface(surface)) {
320 wl_resource_post_error(surface_resource,
321 WL_DISPLAY_ERROR_INVALID_OBJECT,
322 "wl_input_panel::get_input_panel_surface already requested");
323 return;
324 }
325
326 ipsurf = create_input_panel_surface(shell, surface);
327 if (!ipsurf) {
328 wl_resource_post_error(surface_resource,
329 WL_DISPLAY_ERROR_INVALID_OBJECT,
330 "surface->configure already set");
331 return;
332 }
333
334 ipsurf->resource =
335 wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800336 &zwp_input_panel_surface_v1_interface,
337 1,
338 id);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800339 wl_resource_set_implementation(ipsurf->resource,
340 &input_panel_surface_implementation,
341 ipsurf,
342 destroy_input_panel_surface_resource);
343}
344
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800345static const struct zwp_input_panel_v1_interface input_panel_implementation = {
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800346 input_panel_get_input_panel_surface
347};
348
349static void
350unbind_input_panel(struct wl_resource *resource)
351{
352 struct desktop_shell *shell = wl_resource_get_user_data(resource);
353
354 shell->input_panel.binding = NULL;
355}
356
357static void
358bind_input_panel(struct wl_client *client,
359 void *data, uint32_t version, uint32_t id)
360{
361 struct desktop_shell *shell = data;
362 struct wl_resource *resource;
363
364 resource = wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800365 &zwp_input_panel_v1_interface, 1, id);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800366
367 if (shell->input_panel.binding == NULL) {
368 wl_resource_set_implementation(resource,
369 &input_panel_implementation,
370 shell, unbind_input_panel);
371 shell->input_panel.binding = resource;
372 return;
373 }
374
375 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
376 "interface object already bound");
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800377}
378
379void
380input_panel_destroy(struct desktop_shell *shell)
381{
382 wl_list_remove(&shell->show_input_panel_listener.link);
383 wl_list_remove(&shell->hide_input_panel_listener.link);
384}
385
386int
387input_panel_setup(struct desktop_shell *shell)
388{
389 struct weston_compositor *ec = shell->compositor;
390
391 shell->show_input_panel_listener.notify = show_input_panels;
392 wl_signal_add(&ec->show_input_panel_signal,
393 &shell->show_input_panel_listener);
394 shell->hide_input_panel_listener.notify = hide_input_panels;
395 wl_signal_add(&ec->hide_input_panel_signal,
396 &shell->hide_input_panel_listener);
397 shell->update_input_panel_listener.notify = update_input_panels;
398 wl_signal_add(&ec->update_input_panel_signal,
399 &shell->update_input_panel_listener);
400
401 wl_list_init(&shell->input_panel.surfaces);
402
403 if (wl_global_create(shell->compositor->wl_display,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800404 &zwp_input_panel_v1_interface, 1,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800405 shell, bind_input_panel) == NULL)
406 return -1;
407
408 return 0;
409}