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>
34 #include "agl-shell-desktop-server-protocol.h"
37 desktop_advertise_app(struct wl_listener *listener, void *data)
39 struct ivi_surface *surface;
41 surface = wl_container_of(listener, surface, listener_advertise_app);
43 agl_shell_desktop_advertise_application_id(surface->ivi, surface);
47 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
53 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
58 struct weston_output *
59 get_default_output(struct weston_compositor *compositor)
61 if (wl_list_empty(&compositor->output_list))
64 return container_of(compositor->output_list.next,
65 struct weston_output, link);
68 struct weston_output *
69 get_focused_output(struct weston_compositor *compositor)
71 struct weston_seat *seat;
72 struct weston_output *output = NULL;
74 wl_list_for_each(seat, &compositor->seat_list, link) {
75 struct weston_touch *touch = weston_seat_get_touch(seat);
76 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
77 struct weston_keyboard *keyboard =
78 weston_seat_get_keyboard(seat);
80 if (touch && touch->focus)
81 output = touch->focus->output;
82 else if (pointer && pointer->focus)
83 output = pointer->focus->output;
84 else if (keyboard && keyboard->focus)
85 output = keyboard->focus->output;
95 ivi_shell_activate_surface(struct ivi_surface *ivi_surf,
96 struct ivi_shell_seat *ivi_seat,
99 struct weston_desktop_surface *dsurface = ivi_surf->dsurface;
100 struct weston_surface *surface =
101 weston_desktop_surface_get_surface(dsurface);
103 weston_view_activate_input(ivi_surf->view, ivi_seat->seat, flags);
105 if (ivi_seat->focused_surface) {
106 struct ivi_surface *current_focus =
107 get_ivi_shell_surface(ivi_seat->focused_surface);
108 struct weston_desktop_surface *dsurface_focus;
109 assert(current_focus);
111 dsurface_focus = current_focus->dsurface;
112 if (--current_focus->focus_count == 0)
113 weston_desktop_surface_set_activated(dsurface_focus, false);
116 ivi_seat->focused_surface = surface;
117 if (ivi_surf->focus_count++ == 0)
118 weston_desktop_surface_set_activated(dsurface, true);
123 desktop_surface_added_configure(struct ivi_surface *surface,
124 struct ivi_output *ivi_output)
126 enum ivi_surface_role role = IVI_SURFACE_ROLE_NONE;
127 struct weston_desktop_surface *dsurface = surface->dsurface;
129 ivi_check_pending_surface_desktop(surface, &role);
130 if ((role != IVI_SURFACE_ROLE_DESKTOP &&
131 role != IVI_SURFACE_ROLE_FULLSCREEN &&
132 role != IVI_SURFACE_ROLE_REMOTE) ||
133 role == IVI_SURFACE_ROLE_NONE)
136 if (role == IVI_SURFACE_ROLE_FULLSCREEN) {
137 struct ivi_output *bg_output =
138 ivi_layout_find_bg_output(surface->ivi);
140 weston_desktop_surface_set_fullscreen(dsurface, true);
141 weston_desktop_surface_set_size(dsurface,
142 bg_output->output->width,
143 bg_output->output->height);
147 weston_desktop_surface_set_maximized(dsurface, true);
148 weston_desktop_surface_set_size(dsurface,
149 ivi_output->area.width,
150 ivi_output->area.height);
155 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
157 struct ivi_compositor *ivi = userdata;
158 struct weston_desktop_client *dclient;
159 struct wl_client *client;
160 struct ivi_surface *surface;
161 struct ivi_output *active_output = NULL;
162 struct weston_output *output = NULL;
163 const char *app_id = NULL;
165 dclient = weston_desktop_surface_get_client(dsurface);
166 client = weston_desktop_client_get_client(dclient);
168 if (ivi->shell_client.resource &&
169 ivi->shell_client.status == BOUND_FAILED) {
170 wl_client_post_implementation_error(client,
171 "agl_shell has already been bound. "
172 "Check out bound_fail event");
176 surface = zalloc(sizeof *surface);
178 wl_client_post_no_memory(client);
182 surface->view = weston_desktop_surface_create_view(dsurface);
183 if (!surface->view) {
185 wl_client_post_no_memory(client);
190 surface->dsurface = dsurface;
191 surface->role = IVI_SURFACE_ROLE_NONE;
192 surface->mapped = false;
193 surface->advertised_on_launch = false;
194 surface->checked_pending = false;
195 wl_list_init(&surface->link);
197 wl_signal_init(&surface->signal_advertise_app);
199 surface->listener_advertise_app.notify = desktop_advertise_app;
200 wl_signal_add(&surface->signal_advertise_app,
201 &surface->listener_advertise_app);
203 weston_desktop_surface_set_user_data(dsurface, surface);
205 if (ivi->policy && ivi->policy->api.surface_create &&
206 !ivi->policy->api.surface_create(surface, ivi)) {
207 wl_client_post_no_memory(client);
212 app_id = weston_desktop_surface_get_app_id(dsurface);
214 if ((active_output = ivi_layout_find_with_app_id(app_id, ivi)))
215 ivi_set_pending_desktop_surface_remote(active_output, app_id);
217 /* reset any caps to make sure we apply the new caps */
218 ivi_seat_reset_caps_sent(ivi);
220 output = get_focused_output(ivi->compositor);
222 output = get_default_output(ivi->compositor);
224 if (output && ivi->shell_client.ready) {
225 struct ivi_output *ivi_output = to_ivi_output(output);
227 desktop_surface_added_configure(surface, active_output);
229 desktop_surface_added_configure(surface, ivi_output);
232 * We delay creating "normal" desktop surfaces until later, to
233 * give the shell-client an oppurtunity to set the surface as a
235 * Also delay the creation in order to have a valid app_id
236 * which will be used to set the proper role.
238 weston_log("Added surface %p, app_id %s to pending list\n",
240 wl_list_insert(&ivi->pending_surfaces, &surface->link);
245 desktop_surface_check_last_surfaces(struct ivi_output *ivi_output, enum ivi_surface_role role)
248 struct ivi_surface *surf;
250 wl_list_for_each(surf, &ivi_output->ivi->surfaces, link)
251 if (surf->role == role &&
252 surf->current_completed_output == ivi_output)
259 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
261 struct ivi_surface *surface =
262 weston_desktop_surface_get_user_data(dsurface);
263 struct weston_surface *wsurface =
264 weston_desktop_surface_get_surface(dsurface);
265 const char *app_id = NULL;
266 struct weston_seat *wseat = NULL;
267 struct ivi_shell_seat *ivi_seat = NULL;
268 struct ivi_output *output = NULL;
270 /* we might not have a valid ivi_surface if _added failed due to
275 wseat = get_ivi_shell_weston_first_seat(surface->ivi);
277 ivi_seat = get_ivi_shell_seat(wseat);
279 output = ivi_layout_get_output_from_surface(surface);
281 wl_list_remove(&surface->listener_advertise_app.link);
282 surface->listener_advertise_app.notify = NULL;
284 app_id = weston_desktop_surface_get_app_id(dsurface);
286 /* special corner-case, pending_surfaces which are never activated or
287 * being assigned an output might land here so just remove the surface;
289 * the DESKTOP role can happen here as well, because we can fall-back
290 * to that when we try to determine the role type. Application that
291 * do not set the app_id will be land here, when destroyed */
292 if (output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
293 surface->role == IVI_SURFACE_ROLE_DESKTOP))
294 goto skip_output_asignment;
296 assert(output != NULL);
298 /* resize the active surface to the original size */
299 if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
300 surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
301 if (output && output->active) {
302 ivi_layout_desktop_resize(output->active, output->area_saved);
304 /* restore the area back so we can re-use it again if needed */
305 output->area = output->area_saved;
308 /* reset the active surface as well */
309 if (output && output->active && output->active == surface) {
310 output->active->view->is_mapped = false;
311 output->active->view->surface->is_mapped = false;
313 weston_layer_entry_remove(&output->active->view->layer_link);
314 output->active = NULL;
317 /* clear out focused_surface to avoid a stale focused_surface. the
318 * client shell is responsible for keeping track and switch back to the
319 * last active surface so we don't get do anything at removal, just
321 if (ivi_seat && ivi_seat->focused_surface == wsurface)
322 ivi_seat->focused_surface = NULL;
324 if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
325 output->type == OUTPUT_REMOTE)
326 ivi_destroy_waltham_destroy(surface);
328 /* check if there's a last 'remote' surface and insert a black
329 * surface view if there's no background set for that output
331 if (desktop_surface_check_last_surfaces(output, IVI_SURFACE_ROLE_REMOTE) ||
332 desktop_surface_check_last_surfaces(output, IVI_SURFACE_ROLE_DESKTOP))
333 if (!output->background)
334 insert_black_curtain(output);
337 if (weston_surface_is_mapped(wsurface)) {
338 weston_desktop_surface_unlink_view(surface->view);
339 weston_view_destroy(surface->view);
342 /* invalidate agl-shell surfaces so we can re-use them when
344 if (surface->role == IVI_SURFACE_ROLE_PANEL) {
345 switch (surface->panel.edge) {
346 case AGL_SHELL_EDGE_TOP:
349 case AGL_SHELL_EDGE_BOTTOM:
350 output->bottom = NULL;
352 case AGL_SHELL_EDGE_LEFT:
355 case AGL_SHELL_EDGE_RIGHT:
356 output->right = NULL;
359 assert(!"Invalid edge detected\n");
361 } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
362 output->background = NULL;
365 skip_output_asignment:
366 weston_log("Removed surface %p, app_id %s, role %s\n", surface,
367 app_id, ivi_layout_get_surface_role_name(surface));
369 if (app_id && output) {
370 shell_advertise_app_state(output->ivi, app_id,
371 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
373 if (output->ivi->shell_client.ready &&
374 wl_resource_get_version(output->ivi->shell_client.resource)
375 >= AGL_SHELL_APP_STATE_SINCE_VERSION)
376 agl_shell_send_app_state(output->ivi->shell_client.resource,
377 app_id, AGL_SHELL_APP_STATE_TERMINATED);
380 wl_list_remove(&surface->link);
386 desktop_committed(struct weston_desktop_surface *dsurface,
387 int32_t sx, int32_t sy, void *userdata)
389 struct ivi_compositor *ivi = userdata;
390 struct ivi_surface *surface =
391 weston_desktop_surface_get_user_data(dsurface);
392 struct ivi_policy *policy = surface->ivi->policy;
394 if (policy && policy->api.surface_commited &&
395 !policy->api.surface_commited(surface, surface->ivi))
398 if (ivi->shell_client.ready && !surface->checked_pending) {
399 const char * app_id = weston_desktop_surface_get_app_id(dsurface);
400 weston_log("Checking pending surface %p, app_id %s\n", surface,
402 wl_list_remove(&surface->link);
403 wl_list_init(&surface->link);
404 ivi_check_pending_desktop_surface(surface);
405 surface->checked_pending = true;
407 /* we'll do it now at commit time, because we might not have an
408 * appid by the time we've created the weston_desktop_surface
410 if (wl_resource_get_version(ivi->shell_client.resource) >= AGL_SHELL_APP_STATE_SINCE_VERSION)
411 agl_shell_send_app_state(ivi->shell_client.resource,
412 app_id, AGL_SHELL_APP_STATE_STARTED);
415 if (!surface->advertised_on_launch &&
416 !wl_list_empty(&surface->ivi->desktop_clients))
417 wl_signal_emit(&surface->signal_advertise_app, surface);
419 /* this repaint schedule is needed to allow resizing to work with the
420 * help of the hidden layer:
422 * 1. add the view in the hidden layer and send out correct dimensions
423 * 2. clients changes its dimensions
424 * 3. client commits with the new dimensions
426 * For desktop and fullscreen, desktop_surface_added() sends the
427 * dimensions from the beginning so applications no need to resize, but
428 * if that weren't the case we still need this in.
430 weston_compositor_schedule_repaint(surface->ivi->compositor);
432 switch (surface->role) {
433 case IVI_SURFACE_ROLE_DESKTOP:
434 case IVI_SURFACE_ROLE_REMOTE:
435 ivi_layout_desktop_committed(surface);
437 case IVI_SURFACE_ROLE_POPUP:
438 ivi_layout_popup_committed(surface);
440 case IVI_SURFACE_ROLE_FULLSCREEN:
441 ivi_layout_fullscreen_committed(surface);
443 case IVI_SURFACE_ROLE_SPLIT_H:
444 case IVI_SURFACE_ROLE_SPLIT_V:
445 ivi_layout_split_committed(surface);
447 case IVI_SURFACE_ROLE_NONE:
448 case IVI_SURFACE_ROLE_BACKGROUND:
449 case IVI_SURFACE_ROLE_PANEL:
450 default: /* fall through */
456 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
457 struct weston_seat *seat, int32_t x, int32_t y,
464 desktop_set_parent(struct weston_desktop_surface *dsurface,
465 struct weston_desktop_surface *parent, void *userdata)
471 desktop_move(struct weston_desktop_surface *dsurface,
472 struct weston_seat *seat, uint32_t serial, void *userdata)
478 desktop_resize(struct weston_desktop_surface *dsurface,
479 struct weston_seat *seat, uint32_t serial,
480 enum weston_desktop_surface_edge edges, void *user_data)
486 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
487 bool fullscreen, struct weston_output *output,
494 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
495 bool maximized, void *userdata)
501 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
508 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
509 int32_t x, int32_t y, void *userdata)
514 static const struct weston_desktop_api desktop_api = {
515 .struct_size = sizeof desktop_api,
516 .ping_timeout = desktop_ping_timeout,
517 .pong = desktop_pong,
518 .surface_added = desktop_surface_added,
519 .surface_removed = desktop_surface_removed,
520 .committed = desktop_committed,
521 .show_window_menu = desktop_show_window_menu,
522 .set_parent = desktop_set_parent,
523 .move = desktop_move,
524 .resize = desktop_resize,
525 .fullscreen_requested = desktop_fullscreen_requested,
526 .maximized_requested = desktop_maximized_requested,
527 .minimized_requested = desktop_minimized_requested,
528 .set_xwayland_position = desktop_set_xwayland_position,
532 ivi_shell_destroy(struct wl_listener *listener, void *data)
534 struct ivi_compositor *ivi = container_of(listener,
535 struct ivi_compositor, destroy_listener);
537 ivi_shell_finalize(ivi);
538 ivi_compositor_destroy_pending_surfaces(ivi);
540 weston_desktop_destroy(ivi->desktop);
541 wl_list_remove(&listener->link);
545 ivi_desktop_init(struct ivi_compositor *ivi)
547 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
549 weston_log("Failed to create desktop globals");
553 if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
554 &ivi->destroy_listener, ivi_shell_destroy)) {