2 * Copyright © 2019 Collabora, Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include "ivi-compositor.h"
30 #include "shared/helpers.h"
31 #include <libweston/libweston.h>
32 #include <libweston-desktop/libweston-desktop.h>
33 #include <libweston/xwayland-api.h>
35 #include "agl-shell-desktop-server-protocol.h"
38 desktop_advertise_app(struct wl_listener *listener, void *data)
40 struct ivi_surface *surface;
42 surface = wl_container_of(listener, surface, listener_advertise_app);
44 agl_shell_desktop_advertise_application_id(surface->ivi, surface);
48 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
54 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
59 struct weston_output *
60 get_default_output(struct weston_compositor *compositor)
62 if (wl_list_empty(&compositor->output_list))
65 return container_of(compositor->output_list.next,
66 struct weston_output, link);
69 struct weston_output *
70 get_focused_output(struct weston_compositor *compositor)
72 struct weston_seat *seat;
73 struct weston_output *output = NULL;
75 wl_list_for_each(seat, &compositor->seat_list, link) {
76 struct weston_touch *touch = weston_seat_get_touch(seat);
77 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
78 struct weston_keyboard *keyboard =
79 weston_seat_get_keyboard(seat);
81 if (touch && touch->focus)
82 output = touch->focus->output;
83 else if (pointer && pointer->focus)
84 output = pointer->focus->output;
85 else if (keyboard && keyboard->focus)
86 output = keyboard->focus->output;
96 ivi_shell_activate_surface(struct ivi_surface *ivi_surf,
97 struct ivi_shell_seat *ivi_seat,
100 struct weston_desktop_surface *dsurface = ivi_surf->dsurface;
101 struct weston_surface *surface =
102 weston_desktop_surface_get_surface(dsurface);
104 weston_view_activate_input(ivi_surf->view, ivi_seat->seat, flags);
106 if (ivi_seat->focused_surface) {
107 struct ivi_surface *current_focus =
108 get_ivi_shell_surface(ivi_seat->focused_surface);
109 struct weston_desktop_surface *dsurface_focus;
110 assert(current_focus);
112 dsurface_focus = current_focus->dsurface;
113 if (--current_focus->focus_count == 0)
114 weston_desktop_surface_set_activated(dsurface_focus, false);
117 ivi_seat->focused_surface = surface;
118 if (ivi_surf->focus_count++ == 0)
119 weston_desktop_surface_set_activated(dsurface, true);
124 desktop_surface_added_configure(struct ivi_surface *surface,
125 struct ivi_output *ivi_output)
127 enum ivi_surface_role role = IVI_SURFACE_ROLE_NONE;
128 struct weston_desktop_surface *dsurface = surface->dsurface;
130 ivi_check_pending_surface_desktop(surface, &role);
131 if ((role != IVI_SURFACE_ROLE_DESKTOP &&
132 role != IVI_SURFACE_ROLE_FULLSCREEN &&
133 role != IVI_SURFACE_ROLE_REMOTE) ||
134 role == IVI_SURFACE_ROLE_NONE)
137 if (role == IVI_SURFACE_ROLE_FULLSCREEN) {
138 struct ivi_output *bg_output =
139 ivi_layout_find_bg_output(surface->ivi);
141 weston_desktop_surface_set_fullscreen(dsurface, true);
142 weston_desktop_surface_set_size(dsurface,
143 bg_output->output->width,
144 bg_output->output->height);
148 weston_desktop_surface_set_maximized(dsurface, true);
149 weston_desktop_surface_set_size(dsurface,
150 ivi_output->area.width,
151 ivi_output->area.height);
156 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
158 struct ivi_compositor *ivi = userdata;
159 struct weston_desktop_client *dclient;
160 struct wl_client *client;
161 struct ivi_surface *surface;
162 struct ivi_output *active_output = NULL;
163 struct weston_output *output = NULL;
164 const char *app_id = NULL;
166 dclient = weston_desktop_surface_get_client(dsurface);
167 client = weston_desktop_client_get_client(dclient);
169 if (ivi->shell_client.resource &&
170 ivi->shell_client.status == BOUND_FAILED) {
171 wl_client_post_implementation_error(client,
172 "agl_shell has already been bound. "
173 "Check out bound_fail event");
177 surface = zalloc(sizeof *surface);
179 wl_client_post_no_memory(client);
183 surface->view = weston_desktop_surface_create_view(dsurface);
184 if (!surface->view) {
186 wl_client_post_no_memory(client);
191 surface->dsurface = dsurface;
192 surface->role = IVI_SURFACE_ROLE_NONE;
193 surface->mapped = false;
194 surface->advertised_on_launch = false;
195 surface->checked_pending = false;
196 wl_list_init(&surface->link);
198 wl_signal_init(&surface->signal_advertise_app);
200 surface->listener_advertise_app.notify = desktop_advertise_app;
201 wl_signal_add(&surface->signal_advertise_app,
202 &surface->listener_advertise_app);
204 weston_desktop_surface_set_user_data(dsurface, surface);
206 if (ivi->policy && ivi->policy->api.surface_create &&
207 !ivi->policy->api.surface_create(surface, ivi)) {
208 wl_client_post_no_memory(client);
213 app_id = weston_desktop_surface_get_app_id(dsurface);
215 if ((active_output = ivi_layout_find_with_app_id(app_id, ivi))) {
216 ivi_set_pending_desktop_surface_remote(active_output, app_id);
217 shell_send_app_on_output(ivi, app_id, active_output->output->name);
220 /* reset any caps to make sure we apply the new caps */
221 ivi_seat_reset_caps_sent(ivi);
223 output = get_focused_output(ivi->compositor);
225 output = get_default_output(ivi->compositor);
227 if (output && ivi->shell_client.ready) {
228 struct ivi_output *ivi_output = to_ivi_output(output);
230 desktop_surface_added_configure(surface, active_output);
232 desktop_surface_added_configure(surface, ivi_output);
235 * We delay creating "normal" desktop surfaces until later, to
236 * give the shell-client an oppurtunity to set the surface as a
238 * Also delay the creation in order to have a valid app_id
239 * which will be used to set the proper role.
241 weston_log("Added surface %p, app_id %s to pending list\n",
243 wl_list_insert(&ivi->pending_surfaces, &surface->link);
248 ivi_surface_count_one(struct ivi_output *ivi_output,
249 enum ivi_surface_role role)
252 struct ivi_surface *surf;
254 wl_list_for_each(surf, &ivi_output->ivi->surfaces, link)
255 if (surf->role == role &&
256 surf->current_completed_output == ivi_output)
263 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
265 struct ivi_surface *surface =
266 weston_desktop_surface_get_user_data(dsurface);
267 struct weston_surface *wsurface =
268 weston_desktop_surface_get_surface(dsurface);
269 const char *app_id = NULL;
270 struct weston_seat *wseat = NULL;
271 struct ivi_shell_seat *ivi_seat = NULL;
272 struct ivi_output *output = NULL;
274 /* we might not have a valid ivi_surface if _added failed due to
279 wseat = get_ivi_shell_weston_first_seat(surface->ivi);
281 ivi_seat = get_ivi_shell_seat(wseat);
283 output = ivi_layout_get_output_from_surface(surface);
285 wl_list_remove(&surface->listener_advertise_app.link);
286 surface->listener_advertise_app.notify = NULL;
288 app_id = weston_desktop_surface_get_app_id(dsurface);
290 /* special corner-case, pending_surfaces which are never activated or
291 * being assigned an output might land here so just remove the surface;
293 * the DESKTOP role can happen here as well, because we can fall-back
294 * to that when we try to determine the role type. Application that
295 * do not set the app_id will be land here, when destroyed */
296 if ((output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
297 surface->role == IVI_SURFACE_ROLE_DESKTOP)) ||
298 output->output == NULL)
299 goto skip_output_asignment;
301 assert(output != NULL);
303 /* resize the active surface to the original size */
304 if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
305 surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
306 if (output && output->active) {
307 ivi_layout_desktop_resize(output->active, output->area_saved);
309 /* restore the area back so we can re-use it again if needed */
310 output->area = output->area_saved;
313 /* reset the active surface as well */
314 if (output && output->active && output->active == surface) {
315 output->active->view->is_mapped = false;
316 output->active->view->surface->is_mapped = false;
318 weston_layer_entry_remove(&output->active->view->layer_link);
319 output->active = NULL;
322 /* clear out focused_surface to avoid a stale focused_surface. the
323 * client shell is responsible for keeping track and switch back to the
324 * last active surface so we don't get do anything at removal, just
326 if (ivi_seat && ivi_seat->focused_surface == wsurface)
327 ivi_seat->focused_surface = NULL;
329 /* check if there's a last 'remote' surface and insert a black
330 * surface view if there's no background set for that output
332 if (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
333 ivi_surface_count_one(output, IVI_SURFACE_ROLE_DESKTOP))
334 if (!output->background)
335 insert_black_curtain(output);
338 if (weston_surface_is_mapped(wsurface)) {
339 weston_desktop_surface_unlink_view(surface->view);
340 weston_view_destroy(surface->view);
343 /* invalidate agl-shell surfaces so we can re-use them when
345 if (surface->role == IVI_SURFACE_ROLE_PANEL) {
346 switch (surface->panel.edge) {
347 case AGL_SHELL_EDGE_TOP:
350 case AGL_SHELL_EDGE_BOTTOM:
351 output->bottom = NULL;
353 case AGL_SHELL_EDGE_LEFT:
356 case AGL_SHELL_EDGE_RIGHT:
357 output->right = NULL;
360 assert(!"Invalid edge detected\n");
362 } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
363 output->background = NULL;
366 skip_output_asignment:
367 weston_log("Removed surface %p, app_id %s, role %s\n", surface,
368 app_id, ivi_layout_get_surface_role_name(surface));
370 if (app_id && output && output->output) {
371 shell_advertise_app_state(output->ivi, app_id,
372 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
373 if (output->ivi->shell_client.ready)
374 shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
377 wl_list_remove(&surface->link);
383 desktop_committed(struct weston_desktop_surface *dsurface,
384 int32_t sx, int32_t sy, void *userdata)
386 struct ivi_compositor *ivi = userdata;
387 struct ivi_surface *surface =
388 weston_desktop_surface_get_user_data(dsurface);
389 struct ivi_policy *policy = surface->ivi->policy;
391 if (policy && policy->api.surface_commited &&
392 !policy->api.surface_commited(surface, surface->ivi))
395 if (ivi->shell_client.ready && !surface->checked_pending) {
396 struct ivi_output *remote_output = NULL;
397 const char *app_id = weston_desktop_surface_get_app_id(dsurface);
398 weston_log("Checking pending surface %p, app_id %s\n", surface,
400 wl_list_remove(&surface->link);
401 wl_list_init(&surface->link);
403 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
404 ivi_set_pending_desktop_surface_remote(remote_output, app_id);
405 shell_send_app_on_output(ivi, app_id, remote_output->output->name);
409 ivi_check_pending_desktop_surface(surface);
410 surface->checked_pending = true;
412 /* we'll do it now at commit time, because we might not have an
413 * appid by the time we've created the weston_desktop_surface
415 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
418 if (!surface->advertised_on_launch &&
419 !wl_list_empty(&surface->ivi->desktop_clients))
420 wl_signal_emit(&surface->signal_advertise_app, surface);
422 /* this repaint schedule is needed to allow resizing to work with the
423 * help of the hidden layer:
425 * 1. add the view in the hidden layer and send out correct dimensions
426 * 2. clients changes its dimensions
427 * 3. client commits with the new dimensions
429 * For desktop and fullscreen, desktop_surface_added() sends the
430 * dimensions from the beginning so applications no need to resize, but
431 * if that weren't the case we still need this in.
433 weston_compositor_schedule_repaint(surface->ivi->compositor);
435 switch (surface->role) {
436 case IVI_SURFACE_ROLE_DESKTOP:
437 ivi_layout_desktop_committed(surface);
439 case IVI_SURFACE_ROLE_REMOTE:
440 ivi_layout_remote_committed(surface);
442 case IVI_SURFACE_ROLE_POPUP:
443 ivi_layout_popup_committed(surface);
445 case IVI_SURFACE_ROLE_FULLSCREEN:
446 ivi_layout_fullscreen_committed(surface);
448 case IVI_SURFACE_ROLE_SPLIT_H:
449 case IVI_SURFACE_ROLE_SPLIT_V:
450 ivi_layout_split_committed(surface);
452 case IVI_SURFACE_ROLE_NONE:
453 case IVI_SURFACE_ROLE_BACKGROUND:
454 case IVI_SURFACE_ROLE_PANEL:
455 default: /* fall through */
461 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
462 struct weston_seat *seat, int32_t x, int32_t y,
469 desktop_set_parent(struct weston_desktop_surface *dsurface,
470 struct weston_desktop_surface *parent, void *userdata)
476 desktop_move(struct weston_desktop_surface *dsurface,
477 struct weston_seat *seat, uint32_t serial, void *userdata)
483 desktop_resize(struct weston_desktop_surface *dsurface,
484 struct weston_seat *seat, uint32_t serial,
485 enum weston_desktop_surface_edge edges, void *user_data)
491 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
492 bool fullscreen, struct weston_output *output,
499 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
500 bool maximized, void *userdata)
506 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
513 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
514 int32_t x, int32_t y, void *userdata)
516 struct ivi_surface *ivisurf =
517 weston_desktop_surface_get_user_data(dsurface);
519 ivisurf->xwayland.x = x;
520 ivisurf->xwayland.y = y;
521 ivisurf->xwayland.is_set = true;
524 static const struct weston_desktop_api desktop_api = {
525 .struct_size = sizeof desktop_api,
526 .ping_timeout = desktop_ping_timeout,
527 .pong = desktop_pong,
528 .surface_added = desktop_surface_added,
529 .surface_removed = desktop_surface_removed,
530 .committed = desktop_committed,
531 .show_window_menu = desktop_show_window_menu,
532 .set_parent = desktop_set_parent,
533 .move = desktop_move,
534 .resize = desktop_resize,
535 .fullscreen_requested = desktop_fullscreen_requested,
536 .maximized_requested = desktop_maximized_requested,
537 .minimized_requested = desktop_minimized_requested,
538 .set_xwayland_position = desktop_set_xwayland_position,
542 ivi_shell_destroy(struct wl_listener *listener, void *data)
544 struct ivi_compositor *ivi = container_of(listener,
545 struct ivi_compositor, destroy_listener);
547 ivi_shell_finalize(ivi);
548 ivi_compositor_destroy_pending_surfaces(ivi);
549 ivi_layout_destroy_saved_outputs(ivi);
551 weston_desktop_destroy(ivi->desktop);
552 wl_list_remove(&ivi->transform_listener.link);
553 wl_list_remove(&listener->link);
557 transform_handler(struct wl_listener *listener, void *data)
559 struct weston_surface *surface = data;
560 struct ivi_surface *ivisurf = get_ivi_shell_surface(surface);
561 const struct weston_xwayland_surface_api *api;
567 api = ivisurf->ivi->xwayland_surface_api;
569 api = weston_xwayland_surface_get_api(ivisurf->ivi->compositor);
570 ivisurf->ivi->xwayland_surface_api = api;
573 if (!api || !api->is_xwayland_surface(surface))
576 if (!weston_view_is_mapped(ivisurf->view))
579 x = ivisurf->view->geometry.x;
580 y = ivisurf->view->geometry.y;
582 api->send_position(surface, x, y);
586 is_shell_surface_xwayland(struct ivi_surface *surf)
588 const struct weston_xwayland_surface_api *api;
589 struct ivi_compositor *ivi = surf->ivi;
590 struct weston_surface *surface;
592 api = ivi->xwayland_surface_api;
597 surface = weston_desktop_surface_get_surface(surf->dsurface);
598 return api->is_xwayland_surface(surface);
602 ivi_desktop_init(struct ivi_compositor *ivi)
604 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
606 weston_log("Failed to create desktop globals");
610 if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
611 &ivi->destroy_listener, ivi_shell_destroy)) {
615 ivi->transform_listener.notify = transform_handler;
616 wl_signal_add(&ivi->compositor->transform_signal,
617 &ivi->transform_listener);