blob: c08a403f986111d5cd06f87587912c3bad3da251 [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 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
25#include "config.h"
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30
31#include "shell.h"
32#include "desktop-shell-server-protocol.h"
33#include "input-method-server-protocol.h"
34
35struct input_panel_surface {
36 struct wl_resource *resource;
37 struct wl_signal destroy_signal;
38
39 struct desktop_shell *shell;
40
41 struct wl_list link;
42 struct weston_surface *surface;
43 struct weston_view *view;
44 struct wl_listener surface_destroy_listener;
45
46 struct weston_output *output;
47 uint32_t panel;
48};
49
50static void
51show_input_panels(struct wl_listener *listener, void *data)
52{
53 struct desktop_shell *shell =
54 container_of(listener, struct desktop_shell,
55 show_input_panel_listener);
56 struct input_panel_surface *ipsurf, *next;
57
58 shell->text_input.surface = (struct weston_surface*)data;
59
60 if (shell->showing_input_panels)
61 return;
62
63 shell->showing_input_panels = true;
64
65 if (!shell->locked)
66 wl_list_insert(&shell->panel_layer.link,
67 &shell->input_panel_layer.link);
68
69 wl_list_for_each_safe(ipsurf, next,
70 &shell->input_panel.surfaces, link) {
Kristian Høgsberg2eebcd32014-01-02 01:27:06 -080071 if (ipsurf->surface->width == 0)
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080072 continue;
73 wl_list_insert(&shell->input_panel_layer.view_list,
74 &ipsurf->view->layer_link);
75 weston_view_geometry_dirty(ipsurf->view);
76 weston_view_update_transform(ipsurf->view);
77 weston_surface_damage(ipsurf->surface);
Kristian Høgsberg0e45e872014-01-02 01:33:42 -080078 weston_slide_run(ipsurf->view, ipsurf->surface->height * 0.9,
Kristian Høgsberg677a5f52013-12-04 11:00:19 -080079 0, NULL, NULL);
80 }
81}
82
83static void
84hide_input_panels(struct wl_listener *listener, void *data)
85{
86 struct desktop_shell *shell =
87 container_of(listener, struct desktop_shell,
88 hide_input_panel_listener);
89 struct weston_view *view, *next;
90
91 if (!shell->showing_input_panels)
92 return;
93
94 shell->showing_input_panels = false;
95
96 if (!shell->locked)
97 wl_list_remove(&shell->input_panel_layer.link);
98
99 wl_list_for_each_safe(view, next,
100 &shell->input_panel_layer.view_list, layer_link)
101 weston_view_unmap(view);
102}
103
104static void
105update_input_panels(struct wl_listener *listener, void *data)
106{
107 struct desktop_shell *shell =
108 container_of(listener, struct desktop_shell,
109 update_input_panel_listener);
110
111 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
112}
113
114static void
115input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
116{
117 struct input_panel_surface *ip_surface = surface->configure_private;
118 struct desktop_shell *shell = ip_surface->shell;
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800119 struct weston_view *view;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800120 float x, y;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800121
122 if (surface->width == 0)
123 return;
124
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800125 if (ip_surface->panel) {
U. Artie Eoffc4c7a4f2014-01-15 14:03:56 -0800126 view = get_default_view(shell->text_input.surface);
127 if (view == NULL)
128 return;
129 x = view->geometry.x + shell->text_input.cursor_rectangle.x2;
130 y = view->geometry.y + shell->text_input.cursor_rectangle.y2;
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800131 } else {
132 x = ip_surface->output->x + (ip_surface->output->width - surface->width) / 2;
133 y = ip_surface->output->y + ip_surface->output->height - surface->height;
134 }
135
136 weston_view_set_position(ip_surface->view, x, y);
137
Kristian Høgsbergc30c8a32014-01-02 00:45:17 -0800138 if (!weston_surface_is_mapped(surface) && shell->showing_input_panels) {
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800139 wl_list_insert(&shell->input_panel_layer.view_list,
140 &ip_surface->view->layer_link);
141 weston_view_update_transform(ip_surface->view);
142 weston_surface_damage(surface);
Kristian Høgsberg0e45e872014-01-02 01:33:42 -0800143 weston_slide_run(ip_surface->view, ip_surface->view->surface->height * 0.9, 0, NULL, NULL);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -0800144 }
145}
146
147static void
148destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
149{
150 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
151
152 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
153 wl_list_remove(&input_panel_surface->link);
154
155 input_panel_surface->surface->configure = NULL;
156 weston_view_destroy(input_panel_surface->view);
157
158 free(input_panel_surface);
159}
160
161static struct input_panel_surface *
162get_input_panel_surface(struct weston_surface *surface)
163{
164 if (surface->configure == input_panel_configure) {
165 return surface->configure_private;
166 } else {
167 return NULL;
168 }
169}
170
171static void
172input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
173{
174 struct input_panel_surface *ipsurface = container_of(listener,
175 struct input_panel_surface,
176 surface_destroy_listener);
177
178 if (ipsurface->resource) {
179 wl_resource_destroy(ipsurface->resource);
180 } else {
181 destroy_input_panel_surface(ipsurface);
182 }
183}
184
185static struct input_panel_surface *
186create_input_panel_surface(struct desktop_shell *shell,
187 struct weston_surface *surface)
188{
189 struct input_panel_surface *input_panel_surface;
190
191 input_panel_surface = calloc(1, sizeof *input_panel_surface);
192 if (!input_panel_surface)
193 return NULL;
194
195 surface->configure = input_panel_configure;
196 surface->configure_private = input_panel_surface;
197
198 input_panel_surface->shell = shell;
199
200 input_panel_surface->surface = surface;
201 input_panel_surface->view = weston_view_create(surface);
202
203 wl_signal_init(&input_panel_surface->destroy_signal);
204 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
205 wl_signal_add(&surface->destroy_signal,
206 &input_panel_surface->surface_destroy_listener);
207
208 wl_list_init(&input_panel_surface->link);
209
210 return input_panel_surface;
211}
212
213static void
214input_panel_surface_set_toplevel(struct wl_client *client,
215 struct wl_resource *resource,
216 struct wl_resource *output_resource,
217 uint32_t position)
218{
219 struct input_panel_surface *input_panel_surface =
220 wl_resource_get_user_data(resource);
221 struct desktop_shell *shell = input_panel_surface->shell;
222
223 wl_list_insert(&shell->input_panel.surfaces,
224 &input_panel_surface->link);
225
226 input_panel_surface->output = wl_resource_get_user_data(output_resource);
227 input_panel_surface->panel = 0;
228}
229
230static void
231input_panel_surface_set_overlay_panel(struct wl_client *client,
232 struct wl_resource *resource)
233{
234 struct input_panel_surface *input_panel_surface =
235 wl_resource_get_user_data(resource);
236 struct desktop_shell *shell = input_panel_surface->shell;
237
238 wl_list_insert(&shell->input_panel.surfaces,
239 &input_panel_surface->link);
240
241 input_panel_surface->panel = 1;
242}
243
244static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
245 input_panel_surface_set_toplevel,
246 input_panel_surface_set_overlay_panel
247};
248
249static void
250destroy_input_panel_surface_resource(struct wl_resource *resource)
251{
252 struct input_panel_surface *ipsurf =
253 wl_resource_get_user_data(resource);
254
255 destroy_input_panel_surface(ipsurf);
256}
257
258static void
259input_panel_get_input_panel_surface(struct wl_client *client,
260 struct wl_resource *resource,
261 uint32_t id,
262 struct wl_resource *surface_resource)
263{
264 struct weston_surface *surface =
265 wl_resource_get_user_data(surface_resource);
266 struct desktop_shell *shell = wl_resource_get_user_data(resource);
267 struct input_panel_surface *ipsurf;
268
269 if (get_input_panel_surface(surface)) {
270 wl_resource_post_error(surface_resource,
271 WL_DISPLAY_ERROR_INVALID_OBJECT,
272 "wl_input_panel::get_input_panel_surface already requested");
273 return;
274 }
275
276 ipsurf = create_input_panel_surface(shell, surface);
277 if (!ipsurf) {
278 wl_resource_post_error(surface_resource,
279 WL_DISPLAY_ERROR_INVALID_OBJECT,
280 "surface->configure already set");
281 return;
282 }
283
284 ipsurf->resource =
285 wl_resource_create(client,
286 &wl_input_panel_surface_interface, 1, id);
287 wl_resource_set_implementation(ipsurf->resource,
288 &input_panel_surface_implementation,
289 ipsurf,
290 destroy_input_panel_surface_resource);
291}
292
293static const struct wl_input_panel_interface input_panel_implementation = {
294 input_panel_get_input_panel_surface
295};
296
297static void
298unbind_input_panel(struct wl_resource *resource)
299{
300 struct desktop_shell *shell = wl_resource_get_user_data(resource);
301
302 shell->input_panel.binding = NULL;
303}
304
305static void
306bind_input_panel(struct wl_client *client,
307 void *data, uint32_t version, uint32_t id)
308{
309 struct desktop_shell *shell = data;
310 struct wl_resource *resource;
311
312 resource = wl_resource_create(client,
313 &wl_input_panel_interface, 1, id);
314
315 if (shell->input_panel.binding == NULL) {
316 wl_resource_set_implementation(resource,
317 &input_panel_implementation,
318 shell, unbind_input_panel);
319 shell->input_panel.binding = resource;
320 return;
321 }
322
323 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
324 "interface object already bound");
325 wl_resource_destroy(resource);
326}
327
328void
329input_panel_destroy(struct desktop_shell *shell)
330{
331 wl_list_remove(&shell->show_input_panel_listener.link);
332 wl_list_remove(&shell->hide_input_panel_listener.link);
333}
334
335int
336input_panel_setup(struct desktop_shell *shell)
337{
338 struct weston_compositor *ec = shell->compositor;
339
340 shell->show_input_panel_listener.notify = show_input_panels;
341 wl_signal_add(&ec->show_input_panel_signal,
342 &shell->show_input_panel_listener);
343 shell->hide_input_panel_listener.notify = hide_input_panels;
344 wl_signal_add(&ec->hide_input_panel_signal,
345 &shell->hide_input_panel_listener);
346 shell->update_input_panel_listener.notify = update_input_panels;
347 wl_signal_add(&ec->update_input_panel_signal,
348 &shell->update_input_panel_listener);
349
350 wl_list_init(&shell->input_panel.surfaces);
351
352 if (wl_global_create(shell->compositor->wl_display,
353 &wl_input_panel_interface, 1,
354 shell, bind_input_panel) == NULL)
355 return -1;
356
357 return 0;
358}