blob: 251d0449a6c4f55d4c031054ec7119e20f45d4a1 [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);
Manuel Bachmann5082ad62014-04-17 14:04:32 +020077 ipsurf->output = focus->output;
78 x = ipsurf->output->x + (ipsurf->output->width - ipsurf->surface->width) / 2;
79 y = ipsurf->output->y + ipsurf->output->height - ipsurf->surface->height;
80 weston_view_set_position(ipsurf->view, x, y);
81 }
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030082
Giulio Camuffo412e6a52014-07-09 22:12:56 +030083 weston_layer_entry_insert(&shell->input_panel_layer.view_list,
84 &ipsurf->view->layer_link);
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +030085 weston_view_geometry_dirty(ipsurf->view);
86 weston_view_update_transform(ipsurf->view);
87 weston_surface_damage(ipsurf->surface);
88
89 if (ipsurf->anim)
90 weston_view_animation_destroy(ipsurf->anim);
91
92 ipsurf->anim =
93 weston_slide_run(ipsurf->view,
94 ipsurf->surface->height * 0.9, 0,
95 input_panel_slide_done, ipsurf);
96}
97
98static void
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080099show_input_panels(struct wl_listener *listener, void *data)
100{
101 struct desktop_shell *shell =
102 container_of(listener, struct desktop_shell,
103 show_input_panel_listener);
104 struct input_panel_surface *ipsurf, *next;
105
106 shell->text_input.surface = (struct weston_surface*)data;
107
108 if (shell->showing_input_panels)
109 return;
110
111 shell->showing_input_panels = true;
112
113 if (!shell->locked)
Manuel Bachmann805d2f52014-03-05 12:21:34 +0100114 wl_list_insert(&shell->compositor->cursor_layer.link,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800115 &shell->input_panel_layer.link);
116
117 wl_list_for_each_safe(ipsurf, next,
118 &shell->input_panel.surfaces, link) {
Kristian Høgsberg2eebcd32014-01-02 01:27:06 -0800119 if (ipsurf->surface->width == 0)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800120 continue;
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300121
122 show_input_panel_surface(ipsurf);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800123 }
124}
125
126static void
127hide_input_panels(struct wl_listener *listener, void *data)
128{
129 struct desktop_shell *shell =
130 container_of(listener, struct desktop_shell,
131 hide_input_panel_listener);
132 struct weston_view *view, *next;
133
134 if (!shell->showing_input_panels)
135 return;
136
137 shell->showing_input_panels = false;
138
139 if (!shell->locked)
140 wl_list_remove(&shell->input_panel_layer.link);
141
142 wl_list_for_each_safe(view, next,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300143 &shell->input_panel_layer.view_list.link,
144 layer_link.link)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800145 weston_view_unmap(view);
146}
147
148static void
149update_input_panels(struct wl_listener *listener, void *data)
150{
151 struct desktop_shell *shell =
152 container_of(listener, struct desktop_shell,
153 update_input_panel_listener);
154
155 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
156}
157
Pekka Paalanen8274d902014-08-06 19:36:51 +0300158static int
159input_panel_get_label(struct weston_surface *surface, char *buf, size_t len)
160{
161 return snprintf(buf, len, "input panel");
162}
163
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800164static void
165input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
166{
167 struct input_panel_surface *ip_surface = surface->configure_private;
168 struct desktop_shell *shell = ip_surface->shell;
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800169 struct weston_view *view;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800170 float x, y;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800171
172 if (surface->width == 0)
173 return;
174
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800175 if (ip_surface->panel) {
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800176 view = get_default_view(shell->text_input.surface);
177 if (view == NULL)
178 return;
179 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
180 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800181 } else {
182 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
183 y = ip_surface->output->y + ip_surface->output->height - surface->height;
184 }
185
186 weston_view_set_position(ip_surface->view, x, y);
187
Ander Conselvan de Oliveira75c373c2014-04-14 15:48:07 +0300188 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels)
189 show_input_panel_surface(ip_surface);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800190}
191
192static void
193destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
194{
195 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
196
197 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
198 wl_list_remove(&input_panel_surface->link);
199
200 input_panel_surface->surface->configure = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300201 weston_surface_set_label_func(input_panel_surface->surface, NULL);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800202 weston_view_destroy(input_panel_surface->view);
203
204 free(input_panel_surface);
205}
206
207static struct input_panel_surface *
208get_input_panel_surface(struct weston_surface *surface)
209{
210 if (surface->configure == input_panel_configure) {
211 return surface->configure_private;
212 } else {
213 return NULL;
214 }
215}
216
217static void
218input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
219{
220 struct input_panel_surface *ipsurface = container_of(listener,
221 struct input_panel_surface,
222 surface_destroy_listener);
223
224 if (ipsurface->resource) {
225 wl_resource_destroy(ipsurface->resource);
226 } else {
227 destroy_input_panel_surface(ipsurface);
228 }
229}
230
231static struct input_panel_surface *
232create_input_panel_surface(struct desktop_shell *shell,
233 struct weston_surface *surface)
234{
235 struct input_panel_surface *input_panel_surface;
236
237 input_panel_surface = calloc(1, sizeof *input_panel_surface);
238 if (!input_panel_surface)
239 return NULL;
240
241 surface->configure = input_panel_configure;
242 surface->configure_private = input_panel_surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300243 weston_surface_set_label_func(surface, input_panel_get_label);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800244
245 input_panel_surface->shell = shell;
246
247 input_panel_surface->surface = surface;
248 input_panel_surface->view = weston_view_create(surface);
249
250 wl_signal_init(&input_panel_surface->destroy_signal);
251 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
252 wl_signal_add(&surface->destroy_signal,
253 &input_panel_surface->surface_destroy_listener);
254
255 wl_list_init(&input_panel_surface->link);
256
257 return input_panel_surface;
258}
259
260static void
261input_panel_surface_set_toplevel(struct wl_client *client,
262 struct wl_resource *resource,
263 struct wl_resource *output_resource,
264 uint32_t position)
265{
266 struct input_panel_surface *input_panel_surface =
267 wl_resource_get_user_data(resource);
268 struct desktop_shell *shell = input_panel_surface->shell;
269
270 wl_list_insert(&shell->input_panel.surfaces,
271 &input_panel_surface->link);
272
273 input_panel_surface->output = wl_resource_get_user_data(output_resource);
274 input_panel_surface->panel = 0;
275}
276
277static void
278input_panel_surface_set_overlay_panel(struct wl_client *client,
279 struct wl_resource *resource)
280{
281 struct input_panel_surface *input_panel_surface =
282 wl_resource_get_user_data(resource);
283 struct desktop_shell *shell = input_panel_surface->shell;
284
285 wl_list_insert(&shell->input_panel.surfaces,
286 &input_panel_surface->link);
287
288 input_panel_surface->panel = 1;
289}
290
291static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
292 input_panel_surface_set_toplevel,
293 input_panel_surface_set_overlay_panel
294};
295
296static void
297destroy_input_panel_surface_resource(struct wl_resource *resource)
298{
299 struct input_panel_surface *ipsurf =
300 wl_resource_get_user_data(resource);
301
302 destroy_input_panel_surface(ipsurf);
303}
304
305static void
306input_panel_get_input_panel_surface(struct wl_client *client,
307 struct wl_resource *resource,
308 uint32_t id,
309 struct wl_resource *surface_resource)
310{
311 struct weston_surface *surface =
312 wl_resource_get_user_data(surface_resource);
313 struct desktop_shell *shell = wl_resource_get_user_data(resource);
314 struct input_panel_surface *ipsurf;
315
316 if (get_input_panel_surface(surface)) {
317 wl_resource_post_error(surface_resource,
318 WL_DISPLAY_ERROR_INVALID_OBJECT,
319 "wl_input_panel::get_input_panel_surface already requested");
320 return;
321 }
322
323 ipsurf = create_input_panel_surface(shell, surface);
324 if (!ipsurf) {
325 wl_resource_post_error(surface_resource,
326 WL_DISPLAY_ERROR_INVALID_OBJECT,
327 "surface->configure already set");
328 return;
329 }
330
331 ipsurf->resource =
332 wl_resource_create(client,
333 &wl_input_panel_surface_interface, 1, id);
334 wl_resource_set_implementation(ipsurf->resource,
335 &input_panel_surface_implementation,
336 ipsurf,
337 destroy_input_panel_surface_resource);
338}
339
340static const struct wl_input_panel_interface input_panel_implementation = {
341 input_panel_get_input_panel_surface
342};
343
344static void
345unbind_input_panel(struct wl_resource *resource)
346{
347 struct desktop_shell *shell = wl_resource_get_user_data(resource);
348
349 shell->input_panel.binding = NULL;
350}
351
352static void
353bind_input_panel(struct wl_client *client,
354 void *data, uint32_t version, uint32_t id)
355{
356 struct desktop_shell *shell = data;
357 struct wl_resource *resource;
358
359 resource = wl_resource_create(client,
360 &wl_input_panel_interface, 1, id);
361
362 if (shell->input_panel.binding == NULL) {
363 wl_resource_set_implementation(resource,
364 &input_panel_implementation,
365 shell, unbind_input_panel);
366 shell->input_panel.binding = resource;
367 return;
368 }
369
370 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
371 "interface object already bound");
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800372}
373
374void
375input_panel_destroy(struct desktop_shell *shell)
376{
377 wl_list_remove(&shell->show_input_panel_listener.link);
378 wl_list_remove(&shell->hide_input_panel_listener.link);
379}
380
381int
382input_panel_setup(struct desktop_shell *shell)
383{
384 struct weston_compositor *ec = shell->compositor;
385
386 shell->show_input_panel_listener.notify = show_input_panels;
387 wl_signal_add(&ec->show_input_panel_signal,
388 &shell->show_input_panel_listener);
389 shell->hide_input_panel_listener.notify = hide_input_panels;
390 wl_signal_add(&ec->hide_input_panel_signal,
391 &shell->hide_input_panel_listener);
392 shell->update_input_panel_listener.notify = update_input_panels;
393 wl_signal_add(&ec->update_input_panel_signal,
394 &shell->update_input_panel_listener);
395
396 wl_list_init(&shell->input_panel.surfaces);
397
398 if (wl_global_create(shell->compositor->wl_display,
399 &wl_input_panel_interface, 1,
400 shell, bind_input_panel) == NULL)
401 return -1;
402
403 return 0;
404}