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 if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
330 output->type == OUTPUT_REMOTE)
331 ivi_destroy_waltham_destroy(surface);
333 /* check if there's a last 'remote' surface and insert a black
334 * surface view if there's no background set for that output
336 if (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
337 ivi_surface_count_one(output, IVI_SURFACE_ROLE_DESKTOP))
338 if (!output->background)
339 insert_black_curtain(output);
342 if (weston_surface_is_mapped(wsurface)) {
343 weston_desktop_surface_unlink_view(surface->view);
344 weston_view_destroy(surface->view);
347 /* invalidate agl-shell surfaces so we can re-use them when
349 if (surface->role == IVI_SURFACE_ROLE_PANEL) {
350 switch (surface->panel.edge) {
351 case AGL_SHELL_EDGE_TOP:
354 case AGL_SHELL_EDGE_BOTTOM:
355 output->bottom = NULL;
357 case AGL_SHELL_EDGE_LEFT:
360 case AGL_SHELL_EDGE_RIGHT:
361 output->right = NULL;
364 assert(!"Invalid edge detected\n");
366 } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
367 output->background = NULL;
370 skip_output_asignment:
371 weston_log("Removed surface %p, app_id %s, role %s\n", surface,
372 app_id, ivi_layout_get_surface_role_name(surface));
374 if (app_id && output && output->output) {
375 shell_advertise_app_state(output->ivi, app_id,
376 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
377 if (output->ivi->shell_client.ready)
378 shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
381 wl_list_remove(&surface->link);
387 desktop_committed(struct weston_desktop_surface *dsurface,
388 int32_t sx, int32_t sy, void *userdata)
390 struct ivi_compositor *ivi = userdata;
391 struct ivi_surface *surface =
392 weston_desktop_surface_get_user_data(dsurface);
393 struct ivi_policy *policy = surface->ivi->policy;
395 if (policy && policy->api.surface_commited &&
396 !policy->api.surface_commited(surface, surface->ivi))
399 if (ivi->shell_client.ready && !surface->checked_pending) {
400 struct ivi_output *remote_output = NULL;
401 const char *app_id = weston_desktop_surface_get_app_id(dsurface);
402 weston_log("Checking pending surface %p, app_id %s\n", surface,
404 wl_list_remove(&surface->link);
405 wl_list_init(&surface->link);
407 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
408 ivi_set_pending_desktop_surface_remote(remote_output, app_id);
409 shell_send_app_on_output(ivi, app_id, remote_output->output->name);
413 ivi_check_pending_desktop_surface(surface);
414 surface->checked_pending = true;
416 /* we'll do it now at commit time, because we might not have an
417 * appid by the time we've created the weston_desktop_surface
419 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
422 if (!surface->advertised_on_launch &&
423 !wl_list_empty(&surface->ivi->desktop_clients))
424 wl_signal_emit(&surface->signal_advertise_app, surface);
426 /* this repaint schedule is needed to allow resizing to work with the
427 * help of the hidden layer:
429 * 1. add the view in the hidden layer and send out correct dimensions
430 * 2. clients changes its dimensions
431 * 3. client commits with the new dimensions
433 * For desktop and fullscreen, desktop_surface_added() sends the
434 * dimensions from the beginning so applications no need to resize, but
435 * if that weren't the case we still need this in.
437 weston_compositor_schedule_repaint(surface->ivi->compositor);
439 switch (surface->role) {
440 case IVI_SURFACE_ROLE_DESKTOP:
441 ivi_layout_desktop_committed(surface);
443 case IVI_SURFACE_ROLE_REMOTE:
444 ivi_layout_remote_committed(surface);
446 case IVI_SURFACE_ROLE_POPUP:
447 ivi_layout_popup_committed(surface);
449 case IVI_SURFACE_ROLE_FULLSCREEN:
450 ivi_layout_fullscreen_committed(surface);
452 case IVI_SURFACE_ROLE_SPLIT_H:
453 case IVI_SURFACE_ROLE_SPLIT_V:
454 ivi_layout_split_committed(surface);
456 case IVI_SURFACE_ROLE_NONE:
457 case IVI_SURFACE_ROLE_BACKGROUND:
458 case IVI_SURFACE_ROLE_PANEL:
459 default: /* fall through */
465 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
466 struct weston_seat *seat, int32_t x, int32_t y,
473 desktop_set_parent(struct weston_desktop_surface *dsurface,
474 struct weston_desktop_surface *parent, void *userdata)
480 desktop_move(struct weston_desktop_surface *dsurface,
481 struct weston_seat *seat, uint32_t serial, void *userdata)
487 desktop_resize(struct weston_desktop_surface *dsurface,
488 struct weston_seat *seat, uint32_t serial,
489 enum weston_desktop_surface_edge edges, void *user_data)
495 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
496 bool fullscreen, struct weston_output *output,
503 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
504 bool maximized, void *userdata)
510 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
517 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
518 int32_t x, int32_t y, void *userdata)
520 struct ivi_surface *ivisurf =
521 weston_desktop_surface_get_user_data(dsurface);
523 ivisurf->xwayland.x = x;
524 ivisurf->xwayland.y = y;
525 ivisurf->xwayland.is_set = true;
528 static const struct weston_desktop_api desktop_api = {
529 .struct_size = sizeof desktop_api,
530 .ping_timeout = desktop_ping_timeout,
531 .pong = desktop_pong,
532 .surface_added = desktop_surface_added,
533 .surface_removed = desktop_surface_removed,
534 .committed = desktop_committed,
535 .show_window_menu = desktop_show_window_menu,
536 .set_parent = desktop_set_parent,
537 .move = desktop_move,
538 .resize = desktop_resize,
539 .fullscreen_requested = desktop_fullscreen_requested,
540 .maximized_requested = desktop_maximized_requested,
541 .minimized_requested = desktop_minimized_requested,
542 .set_xwayland_position = desktop_set_xwayland_position,
546 ivi_shell_destroy(struct wl_listener *listener, void *data)
548 struct ivi_compositor *ivi = container_of(listener,
549 struct ivi_compositor, destroy_listener);
551 ivi_shell_finalize(ivi);
552 ivi_compositor_destroy_pending_surfaces(ivi);
553 ivi_layout_destroy_saved_outputs(ivi);
555 weston_desktop_destroy(ivi->desktop);
556 wl_list_remove(&ivi->transform_listener.link);
557 wl_list_remove(&listener->link);
561 transform_handler(struct wl_listener *listener, void *data)
563 struct weston_surface *surface = data;
564 struct ivi_surface *ivisurf = get_ivi_shell_surface(surface);
565 const struct weston_xwayland_surface_api *api;
571 api = ivisurf->ivi->xwayland_surface_api;
573 api = weston_xwayland_surface_get_api(ivisurf->ivi->compositor);
574 ivisurf->ivi->xwayland_surface_api = api;
577 if (!api || !api->is_xwayland_surface(surface))
580 if (!weston_view_is_mapped(ivisurf->view))
583 x = ivisurf->view->geometry.x;
584 y = ivisurf->view->geometry.y;
586 api->send_position(surface, x, y);
590 ivi_desktop_init(struct ivi_compositor *ivi)
592 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
594 weston_log("Failed to create desktop globals");
598 if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
599 &ivi->destroy_listener, ivi_shell_destroy)) {
603 ivi->transform_listener.notify = transform_handler;
604 wl_signal_add(&ivi->compositor->transform_signal,
605 &ivi->transform_listener);