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);
216 shell_send_app_on_output(ivi, app_id, active_output->output->name);
219 /* reset any caps to make sure we apply the new caps */
220 ivi_seat_reset_caps_sent(ivi);
222 output = get_focused_output(ivi->compositor);
224 output = get_default_output(ivi->compositor);
226 if (output && ivi->shell_client.ready) {
227 struct ivi_output *ivi_output = to_ivi_output(output);
229 desktop_surface_added_configure(surface, active_output);
231 desktop_surface_added_configure(surface, ivi_output);
234 * We delay creating "normal" desktop surfaces until later, to
235 * give the shell-client an oppurtunity to set the surface as a
237 * Also delay the creation in order to have a valid app_id
238 * which will be used to set the proper role.
240 weston_log("Added surface %p, app_id %s to pending list\n",
242 wl_list_insert(&ivi->pending_surfaces, &surface->link);
247 ivi_surface_count_one(struct ivi_output *ivi_output,
248 enum ivi_surface_role role)
251 struct ivi_surface *surf;
253 wl_list_for_each(surf, &ivi_output->ivi->surfaces, link)
254 if (surf->role == role &&
255 surf->current_completed_output == ivi_output)
262 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
264 struct ivi_surface *surface =
265 weston_desktop_surface_get_user_data(dsurface);
266 struct weston_surface *wsurface =
267 weston_desktop_surface_get_surface(dsurface);
268 const char *app_id = NULL;
269 struct weston_seat *wseat = NULL;
270 struct ivi_shell_seat *ivi_seat = NULL;
271 struct ivi_output *output = NULL;
273 /* we might not have a valid ivi_surface if _added failed due to
278 wseat = get_ivi_shell_weston_first_seat(surface->ivi);
280 ivi_seat = get_ivi_shell_seat(wseat);
282 output = ivi_layout_get_output_from_surface(surface);
284 wl_list_remove(&surface->listener_advertise_app.link);
285 surface->listener_advertise_app.notify = NULL;
287 app_id = weston_desktop_surface_get_app_id(dsurface);
289 /* special corner-case, pending_surfaces which are never activated or
290 * being assigned an output might land here so just remove the surface;
292 * the DESKTOP role can happen here as well, because we can fall-back
293 * to that when we try to determine the role type. Application that
294 * do not set the app_id will be land here, when destroyed */
295 if ((output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
296 surface->role == IVI_SURFACE_ROLE_DESKTOP)) ||
297 output->output == NULL)
298 goto skip_output_asignment;
300 assert(output != NULL);
302 /* resize the active surface to the original size */
303 if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
304 surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
305 if (output && output->active) {
306 ivi_layout_desktop_resize(output->active, output->area_saved);
308 /* restore the area back so we can re-use it again if needed */
309 output->area = output->area_saved;
312 /* reset the active surface as well */
313 if (output && output->active && output->active == surface) {
314 output->active->view->is_mapped = false;
315 output->active->view->surface->is_mapped = false;
317 weston_layer_entry_remove(&output->active->view->layer_link);
318 output->active = NULL;
321 /* clear out focused_surface to avoid a stale focused_surface. the
322 * client shell is responsible for keeping track and switch back to the
323 * last active surface so we don't get do anything at removal, just
325 if (ivi_seat && ivi_seat->focused_surface == wsurface)
326 ivi_seat->focused_surface = NULL;
328 if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
329 output->type == OUTPUT_REMOTE)
330 ivi_destroy_waltham_destroy(surface);
332 /* check if there's a last 'remote' surface and insert a black
333 * surface view if there's no background set for that output
335 if (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
336 ivi_surface_count_one(output, IVI_SURFACE_ROLE_DESKTOP))
337 if (!output->background)
338 insert_black_curtain(output);
341 if (weston_surface_is_mapped(wsurface)) {
342 weston_desktop_surface_unlink_view(surface->view);
343 weston_view_destroy(surface->view);
346 /* invalidate agl-shell surfaces so we can re-use them when
348 if (surface->role == IVI_SURFACE_ROLE_PANEL) {
349 switch (surface->panel.edge) {
350 case AGL_SHELL_EDGE_TOP:
353 case AGL_SHELL_EDGE_BOTTOM:
354 output->bottom = NULL;
356 case AGL_SHELL_EDGE_LEFT:
359 case AGL_SHELL_EDGE_RIGHT:
360 output->right = NULL;
363 assert(!"Invalid edge detected\n");
365 } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
366 output->background = NULL;
369 skip_output_asignment:
370 weston_log("Removed surface %p, app_id %s, role %s\n", surface,
371 app_id, ivi_layout_get_surface_role_name(surface));
373 if (app_id && output && output->output) {
374 shell_advertise_app_state(output->ivi, app_id,
375 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
376 if (output->ivi->shell_client.ready)
377 shell_send_app_state(output->ivi, 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 struct ivi_output *remote_output = NULL;
400 const char *app_id = weston_desktop_surface_get_app_id(dsurface);
401 weston_log("Checking pending surface %p, app_id %s\n", surface,
403 wl_list_remove(&surface->link);
404 wl_list_init(&surface->link);
406 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
407 ivi_set_pending_desktop_surface_remote(remote_output, app_id);
408 shell_send_app_on_output(ivi, app_id, remote_output->output->name);
412 ivi_check_pending_desktop_surface(surface);
413 surface->checked_pending = true;
415 /* we'll do it now at commit time, because we might not have an
416 * appid by the time we've created the weston_desktop_surface
418 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
421 if (!surface->advertised_on_launch &&
422 !wl_list_empty(&surface->ivi->desktop_clients))
423 wl_signal_emit(&surface->signal_advertise_app, surface);
425 /* this repaint schedule is needed to allow resizing to work with the
426 * help of the hidden layer:
428 * 1. add the view in the hidden layer and send out correct dimensions
429 * 2. clients changes its dimensions
430 * 3. client commits with the new dimensions
432 * For desktop and fullscreen, desktop_surface_added() sends the
433 * dimensions from the beginning so applications no need to resize, but
434 * if that weren't the case we still need this in.
436 weston_compositor_schedule_repaint(surface->ivi->compositor);
438 switch (surface->role) {
439 case IVI_SURFACE_ROLE_DESKTOP:
440 ivi_layout_desktop_committed(surface);
442 case IVI_SURFACE_ROLE_REMOTE:
443 ivi_layout_remote_committed(surface);
445 case IVI_SURFACE_ROLE_POPUP:
446 ivi_layout_popup_committed(surface);
448 case IVI_SURFACE_ROLE_FULLSCREEN:
449 ivi_layout_fullscreen_committed(surface);
451 case IVI_SURFACE_ROLE_SPLIT_H:
452 case IVI_SURFACE_ROLE_SPLIT_V:
453 ivi_layout_split_committed(surface);
455 case IVI_SURFACE_ROLE_NONE:
456 case IVI_SURFACE_ROLE_BACKGROUND:
457 case IVI_SURFACE_ROLE_PANEL:
458 default: /* fall through */
464 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
465 struct weston_seat *seat, int32_t x, int32_t y,
472 desktop_set_parent(struct weston_desktop_surface *dsurface,
473 struct weston_desktop_surface *parent, void *userdata)
479 desktop_move(struct weston_desktop_surface *dsurface,
480 struct weston_seat *seat, uint32_t serial, void *userdata)
486 desktop_resize(struct weston_desktop_surface *dsurface,
487 struct weston_seat *seat, uint32_t serial,
488 enum weston_desktop_surface_edge edges, void *user_data)
494 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
495 bool fullscreen, struct weston_output *output,
502 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
503 bool maximized, void *userdata)
509 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
516 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
517 int32_t x, int32_t y, void *userdata)
522 static const struct weston_desktop_api desktop_api = {
523 .struct_size = sizeof desktop_api,
524 .ping_timeout = desktop_ping_timeout,
525 .pong = desktop_pong,
526 .surface_added = desktop_surface_added,
527 .surface_removed = desktop_surface_removed,
528 .committed = desktop_committed,
529 .show_window_menu = desktop_show_window_menu,
530 .set_parent = desktop_set_parent,
531 .move = desktop_move,
532 .resize = desktop_resize,
533 .fullscreen_requested = desktop_fullscreen_requested,
534 .maximized_requested = desktop_maximized_requested,
535 .minimized_requested = desktop_minimized_requested,
536 .set_xwayland_position = desktop_set_xwayland_position,
540 ivi_shell_destroy(struct wl_listener *listener, void *data)
542 struct ivi_compositor *ivi = container_of(listener,
543 struct ivi_compositor, destroy_listener);
545 ivi_shell_finalize(ivi);
546 ivi_compositor_destroy_pending_surfaces(ivi);
547 ivi_layout_destroy_saved_outputs(ivi);
549 weston_desktop_destroy(ivi->desktop);
550 wl_list_remove(&listener->link);
554 ivi_desktop_init(struct ivi_compositor *ivi)
556 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
558 weston_log("Failed to create desktop globals");
562 if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
563 &ivi->destroy_listener, ivi_shell_destroy)) {