blob: 73eac5cea926cab51e6f0018eb9c5ccbb9067f5a [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);
88 weston_surface_damage(ipsurf->surface);
89
90 if (ipsurf->anim)
91 weston_view_animation_destroy(ipsurf->anim);
92
93 ipsurf->anim =
94 weston_slide_run(ipsurf->view,
95 ipsurf->surface->height * 0.9, 0,
96 input_panel_slide_done, ipsurf);
97}
98
99static void
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800100show_input_panels(struct wl_listener *listener, void *data)
101{
102 struct desktop_shell *shell =
103 container_of(listener, struct desktop_shell,
104 show_input_panel_listener);
105 struct input_panel_surface *ipsurf, *next;
106
107 shell->text_input.surface = (struct weston_surface*)data;
108
109 if (shell->showing_input_panels)
110 return;
111
112 shell->showing_input_panels = true;
113
114 if (!shell->locked)
Manuel Bachmann805d2f52014-03-05 12:21:34 +0100115 wl_list_insert(&shell->compositor->cursor_layer.link,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800116 &shell->input_panel_layer.link);
117
118 wl_list_for_each_safe(ipsurf, next,
119 &shell->input_panel.surfaces, link) {
Kristian Høgsberg2eebcd32014-01-02 01:27:06 -0800120 if (ipsurf->surface->width == 0)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800121 continue;
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300122
123 show_input_panel_surface(ipsurf);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800124 }
125}
126
127static void
128hide_input_panels(struct wl_listener *listener, void *data)
129{
130 struct desktop_shell *shell =
131 container_of(listener, struct desktop_shell,
132 hide_input_panel_listener);
133 struct weston_view *view, *next;
134
135 if (!shell->showing_input_panels)
136 return;
137
138 shell->showing_input_panels = false;
139
140 if (!shell->locked)
141 wl_list_remove(&shell->input_panel_layer.link);
142
143 wl_list_for_each_safe(view, next,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300144 &shell->input_panel_layer.view_list.link,
145 layer_link.link)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800146 weston_view_unmap(view);
147}
148
149static void
150update_input_panels(struct wl_listener *listener, void *data)
151{
152 struct desktop_shell *shell =
153 container_of(listener, struct desktop_shell,
154 update_input_panel_listener);
155
156 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
157}
158
Pekka Paalanen8274d902014-08-06 19:36:51 +0300159static int
160input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
161{
162 return snprintf(buf, len, "input panel");
163}
164
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800165static void
166input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
167{
168 struct input_panel_surface *ip_surface = surface->configure_private;
169 struct desktop_shell *shell = ip_surface->shell;
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800170 struct weston_view *view;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800171 float x, y;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800172
173 if (surface->width == 0)
174 return;
175
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800176 if (ip_surface->panel) {
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800177 view = get_default_view(shell->text_input.surface);
178 if (view == NULL)
179 return;
180 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
181 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800182 } else {
183 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
184 y = ip_surface->output->y + ip_surface->output->height - surface->height;
185 }
186
187 weston_view_set_position(ip_surface->view, x, y);
188
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300189 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
190 show_input_panel_surface(ip_surface);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800191}
192
193static void
194destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
195{
196 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
197
198 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
199 wl_list_remove(&input_panel_surface->link);
200
201 input_panel_surface->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300202 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800203 weston_view_destroy(input_panel_surface->view);
204
205 free(input_panel_surface);
206}
207
208static struct input_panel_surface *
209get_input_panel_surface(struct weston_surface *surface)
210{
211 if (surface->configure == input_panel_configure) {
212 return surface->configure_private;
213 } else {
214 return NULL;
215 }
216}
217
218static void
219input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
220{
221 struct input_panel_surface *ipsurface = container_of(listener,
222 struct input_panel_surface,
223 surface_destroy_listener);
224
225 if (ipsurface->resource) {
226 wl_resource_destroy(ipsurface->resource);
227 } else {
228 destroy_input_panel_surface(ipsurface);
229 }
230}
231
232static struct input_panel_surface *
233create_input_panel_surface(struct desktop_shell *shell,
234 struct weston_surface *surface)
235{
236 struct input_panel_surface *input_panel_surface;
237
238 input_panel_surface = calloc(1, sizeof *input_panel_surface);
239 if (!input_panel_surface)
240 return NULL;
241
242 surface->configure = input_panel_configure;
243 surface->configure_private = input_panel_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300244 weston_surface_set_label_func(surface, input_panel_get_label);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800245
246 input_panel_surface->shell = shell;
247
248 input_panel_surface->surface = surface;
249 input_panel_surface->view = weston_view_create(surface);
250
251 wl_signal_init(&input_panel_surface->destroy_signal);
252 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
253 wl_signal_add(&surface->destroy_signal,
254 &input_panel_surface->surface_destroy_listener);
255
256 wl_list_init(&input_panel_surface->link);
257
258 return input_panel_surface;
259}
260
261static void
262input_panel_surface_set_toplevel(struct wl_client *client,
263 struct wl_resource *resource,
264 struct wl_resource *output_resource,
265 uint32_t position)
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->output = wl_resource_get_user_data(output_resource);
275 input_panel_surface->panel = 0;
276}
277
278static void
279input_panel_surface_set_overlay_panel(struct wl_client *client,
280 struct wl_resource *resource)
281{
282 struct input_panel_surface *input_panel_surface =
283 wl_resource_get_user_data(resource);
284 struct desktop_shell *shell = input_panel_surface->shell;
285
286 wl_list_insert(&shell->input_panel.surfaces,
287 &input_panel_surface->link);
288
289 input_panel_surface->panel = 1;
290}
291
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800292static const struct zwp_input_panel_surface_v1_interface input_panel_surface_implementation = {
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800293 input_panel_surface_set_toplevel,
294 input_panel_surface_set_overlay_panel
295};
296
297static void
298destroy_input_panel_surface_resource(struct wl_resource *resource)
299{
300 struct input_panel_surface *ipsurf =
301 wl_resource_get_user_data(resource);
302
303 destroy_input_panel_surface(ipsurf);
304}
305
306static void
307input_panel_get_input_panel_surface(struct wl_client *client,
308 struct wl_resource *resource,
309 uint32_t id,
310 struct wl_resource *surface_resource)
311{
312 struct weston_surface *surface =
313 wl_resource_get_user_data(surface_resource);
314 struct desktop_shell *shell = wl_resource_get_user_data(resource);
315 struct input_panel_surface *ipsurf;
316
317 if (get_input_panel_surface(surface)) {
318 wl_resource_post_error(surface_resource,
319 WL_DISPLAY_ERROR_INVALID_OBJECT,
320 "wl_input_panel::get_input_panel_surface already requested");
321 return;
322 }
323
324 ipsurf = create_input_panel_surface(shell, surface);
325 if (!ipsurf) {
326 wl_resource_post_error(surface_resource,
327 WL_DISPLAY_ERROR_INVALID_OBJECT,
328 "surface->configure already set");
329 return;
330 }
331
332 ipsurf->resource =
333 wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800334 &zwp_input_panel_surface_v1_interface,
335 1,
336 id);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800337 wl_resource_set_implementation(ipsurf->resource,
338 &input_panel_surface_implementation,
339 ipsurf,
340 destroy_input_panel_surface_resource);
341}
342
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800343static const struct zwp_input_panel_v1_interface input_panel_implementation = {
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800344 input_panel_get_input_panel_surface
345};
346
347static void
348unbind_input_panel(struct wl_resource *resource)
349{
350 struct desktop_shell *shell = wl_resource_get_user_data(resource);
351
352 shell->input_panel.binding = NULL;
353}
354
355static void
356bind_input_panel(struct wl_client *client,
357 void *data, uint32_t version, uint32_t id)
358{
359 struct desktop_shell *shell = data;
360 struct wl_resource *resource;
361
362 resource = wl_resource_create(client,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800363 &zwp_input_panel_v1_interface, 1, id);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800364
365 if (shell->input_panel.binding == NULL) {
366 wl_resource_set_implementation(resource,
367 &input_panel_implementation,
368 shell, unbind_input_panel);
369 shell->input_panel.binding = resource;
370 return;
371 }
372
373 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
374 "interface object already bound");
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800375}
376
377void
378input_panel_destroy(struct desktop_shell *shell)
379{
380 wl_list_remove(&shell->show_input_panel_listener.link);
381 wl_list_remove(&shell->hide_input_panel_listener.link);
382}
383
384int
385input_panel_setup(struct desktop_shell *shell)
386{
387 struct weston_compositor *ec = shell->compositor;
388
389 shell->show_input_panel_listener.notify = show_input_panels;
390 wl_signal_add(&ec->show_input_panel_signal,
391 &shell->show_input_panel_listener);
392 shell->hide_input_panel_listener.notify = hide_input_panels;
393 wl_signal_add(&ec->hide_input_panel_signal,
394 &shell->hide_input_panel_listener);
395 shell->update_input_panel_listener.notify = update_input_panels;
396 wl_signal_add(&ec->update_input_panel_signal,
397 &shell->update_input_panel_listener);
398
399 wl_list_init(&shell->input_panel.surfaces);
400
401 if (wl_global_create(shell->compositor->wl_display,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800402 &zwp_input_panel_v1_interface, 1,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800403 shell, bind_input_panel) == NULL)
404 return -1;
405
406 return 0;
407}