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.h>
34 #include <libweston/xwayland-api.h>
37 #include "agl-shell-desktop-server-protocol.h"
40 ivi_layout_destroy_saved_outputs(struct ivi_compositor *ivi)
42 struct ivi_output *output, *output_next;
44 wl_list_for_each_safe(output, output_next, &ivi->saved_outputs, link) {
45 free(output->app_ids);
48 wl_list_remove(&output->link);
54 desktop_advertise_app(struct wl_listener *listener, void *data)
56 struct ivi_surface *surface;
58 surface = wl_container_of(listener, surface, listener_advertise_app);
60 agl_shell_desktop_advertise_application_id(surface->ivi, surface);
64 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
70 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
75 struct weston_output *
76 get_default_output(struct weston_compositor *compositor)
78 if (wl_list_empty(&compositor->output_list))
81 return container_of(compositor->output_list.next,
82 struct weston_output, link);
85 struct weston_output *
86 get_focused_output(struct weston_compositor *compositor)
88 struct weston_seat *seat;
89 struct weston_output *output = NULL;
91 wl_list_for_each(seat, &compositor->seat_list, link) {
92 struct weston_touch *touch = weston_seat_get_touch(seat);
93 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
94 struct weston_keyboard *keyboard =
95 weston_seat_get_keyboard(seat);
97 if (touch && touch->focus)
98 output = touch->focus->output;
99 else if (pointer && pointer->focus)
100 output = pointer->focus->output;
101 else if (keyboard && keyboard->focus)
102 output = keyboard->focus->output;
112 ivi_shell_activate_surface(struct ivi_surface *ivi_surf,
113 struct ivi_shell_seat *ivi_seat,
116 struct weston_desktop_surface *dsurface = ivi_surf->dsurface;
117 struct weston_surface *surface =
118 weston_desktop_surface_get_surface(dsurface);
120 weston_view_activate_input(ivi_surf->view, ivi_seat->seat, flags);
122 if (ivi_seat->focused_surface) {
123 struct ivi_surface *current_focus =
124 get_ivi_shell_surface(ivi_seat->focused_surface);
125 struct weston_desktop_surface *dsurface_focus;
126 assert(current_focus);
128 dsurface_focus = current_focus->dsurface;
129 if (--current_focus->focus_count == 0)
130 weston_desktop_surface_set_activated(dsurface_focus, false);
133 ivi_seat->focused_surface = surface;
134 if (ivi_surf->focus_count++ == 0)
135 weston_desktop_surface_set_activated(dsurface, true);
140 desktop_surface_added_configure(struct ivi_surface *surface,
141 struct ivi_output *ivi_output)
143 enum ivi_surface_role role = IVI_SURFACE_ROLE_NONE;
144 struct weston_desktop_surface *dsurface = surface->dsurface;
146 ivi_check_pending_surface_desktop(surface, &role);
147 if ((role != IVI_SURFACE_ROLE_DESKTOP &&
148 role != IVI_SURFACE_ROLE_FULLSCREEN &&
149 role != IVI_SURFACE_ROLE_REMOTE) ||
150 role == IVI_SURFACE_ROLE_NONE)
153 if (role == IVI_SURFACE_ROLE_FULLSCREEN) {
154 struct ivi_output *bg_output =
155 ivi_layout_find_bg_output(surface->ivi);
157 weston_desktop_surface_set_fullscreen(dsurface, true);
158 weston_desktop_surface_set_size(dsurface,
159 bg_output->output->width,
160 bg_output->output->height);
164 weston_desktop_surface_set_maximized(dsurface, true);
165 weston_desktop_surface_set_size(dsurface,
166 ivi_output->area.width,
167 ivi_output->area.height);
172 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
174 struct ivi_compositor *ivi = userdata;
175 struct weston_desktop_client *dclient;
176 struct wl_client *client;
177 struct ivi_surface *surface;
178 struct ivi_output *active_output = NULL;
179 struct weston_output *output = NULL;
180 const char *app_id = NULL;
182 dclient = weston_desktop_surface_get_client(dsurface);
183 client = weston_desktop_client_get_client(dclient);
185 if (ivi->shell_client.resource &&
186 ivi->shell_client.status == BOUND_FAILED) {
187 wl_client_post_implementation_error(client,
188 "agl_shell has already been bound. "
189 "Check out bound_fail event");
193 surface = zalloc(sizeof *surface);
195 wl_client_post_no_memory(client);
199 surface->view = weston_desktop_surface_create_view(dsurface);
200 if (!surface->view) {
202 wl_client_post_no_memory(client);
207 surface->dsurface = dsurface;
208 surface->role = IVI_SURFACE_ROLE_NONE;
209 surface->mapped = false;
210 surface->advertised_on_launch = false;
211 surface->checked_pending = false;
212 wl_list_init(&surface->link);
214 wl_signal_init(&surface->signal_advertise_app);
216 surface->listener_advertise_app.notify = desktop_advertise_app;
217 wl_signal_add(&surface->signal_advertise_app,
218 &surface->listener_advertise_app);
220 weston_desktop_surface_set_user_data(dsurface, surface);
222 if (ivi->policy && ivi->policy->api.surface_create &&
223 !ivi->policy->api.surface_create(surface, ivi)) {
224 wl_client_post_no_memory(client);
229 app_id = weston_desktop_surface_get_app_id(dsurface);
231 if ((active_output = ivi_layout_find_with_app_id(app_id, ivi))) {
232 ivi_set_pending_desktop_surface_remote(active_output, app_id);
233 shell_send_app_on_output(ivi, app_id, active_output->output->name);
236 /* reset any caps to make sure we apply the new caps */
237 ivi_seat_reset_caps_sent(ivi);
239 output = get_focused_output(ivi->compositor);
241 output = get_default_output(ivi->compositor);
243 if (output && ivi->shell_client.ready) {
244 struct ivi_output *ivi_output = to_ivi_output(output);
246 desktop_surface_added_configure(surface, active_output);
248 desktop_surface_added_configure(surface, ivi_output);
251 * We delay creating "normal" desktop surfaces until later, to
252 * give the shell-client an oppurtunity to set the surface as a
254 * Also delay the creation in order to have a valid app_id
255 * which will be used to set the proper role.
257 weston_log("Added surface %p, app_id %s to pending list\n",
259 wl_list_insert(&ivi->pending_surfaces, &surface->link);
264 ivi_surface_count_one(struct ivi_output *ivi_output,
265 enum ivi_surface_role role)
268 struct ivi_surface *surf;
270 wl_list_for_each(surf, &ivi_output->ivi->surfaces, link)
271 if (surf->role == role &&
272 surf->current_completed_output == ivi_output)
279 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
281 struct ivi_surface *surface =
282 weston_desktop_surface_get_user_data(dsurface);
283 struct weston_surface *wsurface =
284 weston_desktop_surface_get_surface(dsurface);
285 const char *app_id = NULL;
286 struct weston_seat *wseat = NULL;
287 struct ivi_shell_seat *ivi_seat = NULL;
288 struct ivi_output *output = NULL;
290 /* we might not have a valid ivi_surface if _added failed due to
295 wseat = get_ivi_shell_weston_first_seat(surface->ivi);
297 ivi_seat = get_ivi_shell_seat(wseat);
299 output = ivi_layout_get_output_from_surface(surface);
301 wl_list_remove(&surface->listener_advertise_app.link);
302 surface->listener_advertise_app.notify = NULL;
304 app_id = weston_desktop_surface_get_app_id(dsurface);
306 /* special corner-case, pending_surfaces which are never activated or
307 * being assigned an output might land here so just remove the surface;
309 * the DESKTOP role can happen here as well, because we can fall-back
310 * to that when we try to determine the role type. Application that
311 * do not set the app_id will be land here, when destroyed */
312 if ((output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
313 surface->role == IVI_SURFACE_ROLE_DESKTOP)) ||
314 output->output == NULL)
315 goto skip_output_asignment;
317 assert(output != NULL);
319 /* resize the active surface to the original size */
320 if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
321 surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
322 if (output && output->active) {
323 ivi_layout_desktop_resize(output->active, output->area_saved);
325 /* restore the area back so we can re-use it again if needed */
326 output->area = output->area_saved;
330 /* reset the active surface as well */
331 if (output && output->active && output->active == surface) {
332 output->active->view->is_mapped = false;
333 output->active->view->surface->is_mapped = false;
335 weston_view_move_to_layer(output->active->view, NULL);
336 output->active = NULL;
339 /* clear out focused_surface to avoid a stale focused_surface. the
340 * client shell is responsible for keeping track and switch back to the
341 * last active surface so we don't get do anything at removal, just
343 if (ivi_seat && ivi_seat->focused_surface == wsurface)
344 ivi_seat->focused_surface = NULL;
346 /* check if there's a last 'remote' surface and insert a black
347 * surface view if there's no background set for that output
349 if (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
350 ivi_surface_count_one(output, IVI_SURFACE_ROLE_DESKTOP))
351 if (!output->background)
352 insert_black_curtain(output);
355 if (weston_surface_is_mapped(wsurface)) {
356 weston_desktop_surface_unlink_view(surface->view);
357 weston_view_destroy(surface->view);
360 if (surface->role == IVI_SURFACE_ROLE_TILE) {
361 ivi_layout_reset_split_surfaces(surface->ivi);
362 // activate previous when resizing back to give input set
363 // output active for allowing to resizing again if needed
364 if (output->previous_active)
365 ivi_layout_activate_by_surf(output, output->previous_active);
368 /* invalidate agl-shell surfaces so we can re-use them when
370 if (surface->role == IVI_SURFACE_ROLE_PANEL) {
371 switch (surface->panel.edge) {
372 case AGL_SHELL_EDGE_TOP:
375 case AGL_SHELL_EDGE_BOTTOM:
376 output->bottom = NULL;
378 case AGL_SHELL_EDGE_LEFT:
381 case AGL_SHELL_EDGE_RIGHT:
382 output->right = NULL;
385 assert(!"Invalid edge detected\n");
387 } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
388 output->background = NULL;
391 skip_output_asignment:
392 weston_log("Removed surface %p, app_id %s, role %s\n", surface,
393 app_id, ivi_layout_get_surface_role_name(surface));
395 if (app_id && output && output->output) {
396 shell_advertise_app_state(output->ivi, app_id,
397 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
398 if (output->ivi->shell_client.ready)
399 shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
402 wl_list_remove(&surface->link);
408 desktop_committed(struct weston_desktop_surface *dsurface,
409 struct weston_coord_surface buf_offset, void *userdata)
411 struct ivi_compositor *ivi = userdata;
412 struct ivi_surface *surface =
413 weston_desktop_surface_get_user_data(dsurface);
414 struct ivi_policy *policy = surface->ivi->policy;
416 if (policy && policy->api.surface_commited &&
417 !policy->api.surface_commited(surface, surface->ivi))
420 if (ivi->shell_client.ready && !surface->checked_pending) {
421 struct ivi_output *remote_output = NULL;
422 const char *app_id = weston_desktop_surface_get_app_id(dsurface);
423 weston_log("Checking pending surface %p, app_id %s\n", surface,
425 wl_list_remove(&surface->link);
426 wl_list_init(&surface->link);
428 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
429 ivi_set_pending_desktop_surface_remote(remote_output, app_id);
430 shell_send_app_on_output(ivi, app_id, remote_output->output->name);
434 ivi_check_pending_desktop_surface(surface);
435 surface->checked_pending = true;
437 /* we'll do it now at commit time, because we might not have an
438 * appid by the time we've created the weston_desktop_surface
440 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
443 if (!surface->advertised_on_launch &&
444 !wl_list_empty(&surface->ivi->desktop_clients))
445 wl_signal_emit(&surface->signal_advertise_app, surface);
447 /* this repaint schedule is needed to allow resizing to work with the
448 * help of the hidden layer:
450 * 1. add the view in the hidden layer and send out correct dimensions
451 * 2. clients changes its dimensions
452 * 3. client commits with the new dimensions
454 * For desktop and fullscreen, desktop_surface_added() sends the
455 * dimensions from the beginning so applications no need to resize, but
456 * if that weren't the case we still need this in.
458 weston_compositor_schedule_repaint(surface->ivi->compositor);
460 switch (surface->role) {
461 case IVI_SURFACE_ROLE_DESKTOP:
462 ivi_layout_desktop_committed(surface);
464 case IVI_SURFACE_ROLE_REMOTE:
465 ivi_layout_remote_committed(surface);
467 case IVI_SURFACE_ROLE_POPUP:
468 ivi_layout_popup_committed(surface);
470 case IVI_SURFACE_ROLE_FULLSCREEN:
471 ivi_layout_fullscreen_committed(surface);
473 case IVI_SURFACE_ROLE_SPLIT_H:
474 case IVI_SURFACE_ROLE_SPLIT_V:
475 ivi_layout_split_committed(surface);
477 case IVI_SURFACE_ROLE_NONE:
478 case IVI_SURFACE_ROLE_BACKGROUND:
479 case IVI_SURFACE_ROLE_PANEL:
480 default: /* fall through */
486 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
487 struct weston_seat *seat, struct weston_coord_surface offset,
494 desktop_set_parent(struct weston_desktop_surface *dsurface,
495 struct weston_desktop_surface *parent, void *userdata)
501 desktop_move(struct weston_desktop_surface *dsurface,
502 struct weston_seat *seat, uint32_t serial, void *userdata)
508 desktop_resize(struct weston_desktop_surface *dsurface,
509 struct weston_seat *seat, uint32_t serial,
510 enum weston_desktop_surface_edge edges, void *user_data)
516 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
517 bool fullscreen, struct weston_output *output,
524 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
525 bool maximized, void *userdata)
531 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
538 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
539 struct weston_coord_global pos, void *userdata)
541 struct ivi_surface *ivisurf =
542 weston_desktop_surface_get_user_data(dsurface);
544 ivisurf->xwayland.x = pos.c.x;
545 ivisurf->xwayland.y = pos.c.y;
546 ivisurf->xwayland.is_set = true;
549 static const struct weston_desktop_api desktop_api = {
550 .struct_size = sizeof desktop_api,
551 .ping_timeout = desktop_ping_timeout,
552 .pong = desktop_pong,
553 .surface_added = desktop_surface_added,
554 .surface_removed = desktop_surface_removed,
555 .committed = desktop_committed,
556 .show_window_menu = desktop_show_window_menu,
557 .set_parent = desktop_set_parent,
558 .move = desktop_move,
559 .resize = desktop_resize,
560 .fullscreen_requested = desktop_fullscreen_requested,
561 .maximized_requested = desktop_maximized_requested,
562 .minimized_requested = desktop_minimized_requested,
563 .set_xwayland_position = desktop_set_xwayland_position,
567 ivi_shell_destroy(struct wl_listener *listener, void *data)
569 struct ivi_compositor *ivi = container_of(listener,
570 struct ivi_compositor, destroy_listener);
572 ivi_shell_finalize(ivi);
573 ivi_compositor_destroy_pending_surfaces(ivi);
574 ivi_layout_destroy_saved_outputs(ivi);
576 weston_desktop_destroy(ivi->desktop);
577 wl_list_remove(&ivi->transform_listener.link);
578 wl_list_remove(&listener->link);
582 transform_handler(struct wl_listener *listener, void *data)
584 #ifdef BUILD_XWAYLAND
585 struct weston_surface *surface = data;
586 struct ivi_surface *ivisurf = get_ivi_shell_surface(surface);
587 const struct weston_xwayland_surface_api *api;
593 api = ivisurf->ivi->xwayland_surface_api;
595 api = weston_xwayland_surface_get_api(ivisurf->ivi->compositor);
596 ivisurf->ivi->xwayland_surface_api = api;
599 if (!api || !api->is_xwayland_surface(surface))
602 if (!weston_view_is_mapped(ivisurf->view))
605 x = ivisurf->view->geometry.pos_offset.x;
606 y = ivisurf->view->geometry.pos_offset.y;
608 api->send_position(surface, x, y);
613 is_shell_surface_xwayland(struct ivi_surface *surf)
615 #ifdef BUILD_XWAYLAND
616 const struct weston_xwayland_surface_api *api;
617 struct ivi_compositor *ivi = surf->ivi;
618 struct weston_surface *surface;
620 api = ivi->xwayland_surface_api;
625 surface = weston_desktop_surface_get_surface(surf->dsurface);
626 return api->is_xwayland_surface(surface);
633 ivi_desktop_init(struct ivi_compositor *ivi)
635 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
637 weston_log("Failed to create desktop globals");
641 if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
642 &ivi->destroy_listener, ivi_shell_destroy)) {
646 ivi->transform_listener.notify = transform_handler;
647 wl_signal_add(&ivi->compositor->transform_signal,
648 &ivi->transform_listener);