blob: 95d6287382795817c0ade7c0b6f098a117aedb8a [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"
Jon Cruz867d50e2015-06-15 15:37:10 -070035#include "shared/helpers.h"
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080036
37struct input_panel_surface {
38 struct wl_resource *resource;
39 struct wl_signal destroy_signal;
40
41 struct desktop_shell *shell;
42
43 struct wl_list link;
44 struct weston_surface *surface;
45 struct weston_view *view;
46 struct wl_listener surface_destroy_listener;
47
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030048 struct weston_view_animation *anim;
49
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080050 struct weston_output *output;
51 uint32_t panel;
52};
53
54static void
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030055input_panel_slide_done(struct weston_view_animation *animation, void *data)
56{
57 struct input_panel_surface *ipsurf = data;
58
59 ipsurf->anim = NULL;
60}
61
62static void
63show_input_panel_surface(struct input_panel_surface *ipsurf)
64{
65 struct desktop_shell *shell = ipsurf->shell;
Manuel Bachmann5082ad62014-04-17 14:04:32 +020066 struct weston_seat *seat;
67 struct weston_surface *focus;
68 float x, y;
69
70 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050071 struct weston_keyboard *keyboard =
72 weston_seat_get_keyboard(seat);
73
74 if (!keyboard || !keyboard->focus)
Manuel Bachmann5082ad62014-04-17 14:04:32 +020075 continue;
Derek Foreman1281a362015-07-31 16:55:32 -050076 focus = weston_surface_get_main_surface(keyboard->focus);
Nicolas Guyomard48453542015-10-06 10:40:28 -050077 if (!focus)
78 continue;
Manuel Bachmann5082ad62014-04-17 14:04:32 +020079 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 }
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030084
Giulio Camuffo412e6a52014-07-09 22:12:56 +030085 weston_layer_entry_insert(&shell->input_panel_layer.view_list,
86 &ipsurf->view->layer_link);
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030087 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
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800101show_input_panels(struct wl_listener *listener, void *data)
102{
103 struct desktop_shell *shell =
104 container_of(listener, struct desktop_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)
Manuel Bachmann805d2f52014-03-05 12:21:34 +0100116 wl_list_insert(&shell->compositor->cursor_layer.link,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800117 &shell->input_panel_layer.link);
118
119 wl_list_for_each_safe(ipsurf, next,
120 &shell->input_panel.surfaces, link) {
Kristian Høgsberg2eebcd32014-01-02 01:27:06 -0800121 if (ipsurf->surface->width == 0)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800122 continue;
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300123
124 show_input_panel_surface(ipsurf);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800125 }
126}
127
128static void
129hide_input_panels(struct wl_listener *listener, void *data)
130{
131 struct desktop_shell *shell =
132 container_of(listener, struct desktop_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,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300145 &shell->input_panel_layer.view_list.link,
146 layer_link.link)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800147 weston_view_unmap(view);
148}
149
150static void
151update_input_panels(struct wl_listener *listener, void *data)
152{
153 struct desktop_shell *shell =
154 container_of(listener, struct desktop_shell,
155 update_input_panel_listener);
156
157 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
158}
159
Pekka Paalanen8274d902014-08-06 19:36:51 +0300160static int
161input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
162{
163 return snprintf(buf, len, "input panel");
164}
165
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800166static void
167input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
168{
169 struct input_panel_surface *ip_surface = surface->configure_private;
170 struct desktop_shell *shell = ip_surface->shell;
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800171 struct weston_view *view;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800172 float x, y;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800173
174 if (surface->width == 0)
175 return;
176
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800177 if (ip_surface->panel) {
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800178 view = get_default_view(shell->text_input.surface);
179 if (view == NULL)
180 return;
181 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
182 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800183 } else {
184 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
185 y = ip_surface->output->y + ip_surface->output->height - surface->height;
186 }
187
188 weston_view_set_position(ip_surface->view, x, y);
189
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300190 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
191 show_input_panel_surface(ip_surface);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800192}
193
194static void
195destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
196{
197 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
198
199 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
200 wl_list_remove(&input_panel_surface->link);
201
202 input_panel_surface->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300203 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800204 weston_view_destroy(input_panel_surface->view);
205
206 free(input_panel_surface);
207}
208
209static struct input_panel_surface *
210get_input_panel_surface(struct weston_surface *surface)
211{
212 if (surface->configure == input_panel_configure) {
213 return surface->configure_private;
214 } else {
215 return NULL;
216 }
217}
218
219static void
220input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
221{
222 struct input_panel_surface *ipsurface = container_of(listener,
223 struct input_panel_surface,
224 surface_destroy_listener);
225
226 if (ipsurface->resource) {
227 wl_resource_destroy(ipsurface->resource);
228 } else {
229 destroy_input_panel_surface(ipsurface);
230 }
231}
232
233static struct input_panel_surface *
234create_input_panel_surface(struct desktop_shell *shell,
235 struct weston_surface *surface)
236{
237 struct input_panel_surface *input_panel_surface;
238
239 input_panel_surface = calloc(1, sizeof *input_panel_surface);
240 if (!input_panel_surface)
241 return NULL;
242
243 surface->configure = input_panel_configure;
244 surface->configure_private = input_panel_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300245 weston_surface_set_label_func(surface, input_panel_get_label);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800246
247 input_panel_surface->shell = shell;
248
249 input_panel_surface->surface = surface;
250 input_panel_surface->view = weston_view_create(surface);
251
252 wl_signal_init(&input_panel_surface->destroy_signal);
253 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
254 wl_signal_add(&surface->destroy_signal,
255 &input_panel_surface->surface_destroy_listener);
256
257 wl_list_init(&input_panel_surface->link);
258
259 return input_panel_surface;
260}
261
262static void
263input_panel_surface_set_toplevel(struct wl_client *client,
264 struct wl_resource *resource,
265 struct wl_resource *output_resource,
266 uint32_t position)
267{
268 struct input_panel_surface *input_panel_surface =
269 wl_resource_get_user_data(resource);
270 struct desktop_shell *shell = input_panel_surface->shell;
271
272 wl_list_insert(&shell->input_panel.surfaces,
273 &input_panel_surface->link);
274
275 input_panel_surface->output = wl_resource_get_user_data(output_resource);
276 input_panel_surface->panel = 0;
277}
278
279static void
280input_panel_surface_set_overlay_panel(struct wl_client *client,
281 struct wl_resource *resource)
282{
283 struct input_panel_surface *input_panel_surface =
284 wl_resource_get_user_data(resource);
285 struct desktop_shell *shell = input_panel_surface->shell;
286
287 wl_list_insert(&shell->input_panel.surfaces,
288 &input_panel_surface->link);
289
290 input_panel_surface->panel = 1;
291}
292
293static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
294 input_panel_surface_set_toplevel,
295 input_panel_surface_set_overlay_panel
296};
297
298static void
299destroy_input_panel_surface_resource(struct wl_resource *resource)
300{
301 struct input_panel_surface *ipsurf =
302 wl_resource_get_user_data(resource);
303
304 destroy_input_panel_surface(ipsurf);
305}
306
307static void
308input_panel_get_input_panel_surface(struct wl_client *client,
309 struct wl_resource *resource,
310 uint32_t id,
311 struct wl_resource *surface_resource)
312{
313 struct weston_surface *surface =
314 wl_resource_get_user_data(surface_resource);
315 struct desktop_shell *shell = wl_resource_get_user_data(resource);
316 struct input_panel_surface *ipsurf;
317
318 if (get_input_panel_surface(surface)) {
319 wl_resource_post_error(surface_resource,
320 WL_DISPLAY_ERROR_INVALID_OBJECT,
321 "wl_input_panel::get_input_panel_surface already requested");
322 return;
323 }
324
325 ipsurf = create_input_panel_surface(shell, surface);
326 if (!ipsurf) {
327 wl_resource_post_error(surface_resource,
328 WL_DISPLAY_ERROR_INVALID_OBJECT,
329 "surface->configure already set");
330 return;
331 }
332
333 ipsurf->resource =
334 wl_resource_create(client,
335 &wl_input_panel_surface_interface, 1, id);
336 wl_resource_set_implementation(ipsurf->resource,
337 &input_panel_surface_implementation,
338 ipsurf,
339 destroy_input_panel_surface_resource);
340}
341
342static const struct wl_input_panel_interface input_panel_implementation = {
343 input_panel_get_input_panel_surface
344};
345
346static void
347unbind_input_panel(struct wl_resource *resource)
348{
349 struct desktop_shell *shell = wl_resource_get_user_data(resource);
350
351 shell->input_panel.binding = NULL;
352}
353
354static void
355bind_input_panel(struct wl_client *client,
356 void *data, uint32_t version, uint32_t id)
357{
358 struct desktop_shell *shell = data;
359 struct wl_resource *resource;
360
361 resource = wl_resource_create(client,
362 &wl_input_panel_interface, 1, id);
363
364 if (shell->input_panel.binding == NULL) {
365 wl_resource_set_implementation(resource,
366 &input_panel_implementation,
367 shell, unbind_input_panel);
368 shell->input_panel.binding = resource;
369 return;
370 }
371
372 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
373 "interface object already bound");
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800374}
375
376void
377input_panel_destroy(struct desktop_shell *shell)
378{
379 wl_list_remove(&shell->show_input_panel_listener.link);
380 wl_list_remove(&shell->hide_input_panel_listener.link);
381}
382
383int
384input_panel_setup(struct desktop_shell *shell)
385{
386 struct weston_compositor *ec = shell->compositor;
387
388 shell->show_input_panel_listener.notify = show_input_panels;
389 wl_signal_add(&ec->show_input_panel_signal,
390 &shell->show_input_panel_listener);
391 shell->hide_input_panel_listener.notify = hide_input_panels;
392 wl_signal_add(&ec->hide_input_panel_signal,
393 &shell->hide_input_panel_listener);
394 shell->update_input_panel_listener.notify = update_input_panels;
395 wl_signal_add(&ec->update_input_panel_signal,
396 &shell->update_input_panel_listener);
397
398 wl_list_init(&shell->input_panel.surfaces);
399
400 if (wl_global_create(shell->compositor->wl_display,
401 &wl_input_panel_interface, 1,
402 shell, bind_input_panel) == NULL)
403 return -1;
404
405 return 0;
406}