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