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_NONE)
135 if (role == IVI_SURFACE_ROLE_FULLSCREEN) {
136 struct ivi_output *bg_output =
137 ivi_layout_find_bg_output(surface->ivi);
139 weston_desktop_surface_set_fullscreen(dsurface, true);
140 weston_desktop_surface_set_size(dsurface,
141 bg_output->output->width,
142 bg_output->output->height);
146 weston_desktop_surface_set_maximized(dsurface, true);
147 weston_desktop_surface_set_size(dsurface,
148 ivi_output->area.width,
149 ivi_output->area.height);
154 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
156 struct ivi_compositor *ivi = userdata;
157 struct weston_desktop_client *dclient;
158 struct wl_client *client;
159 struct ivi_surface *surface;
160 struct ivi_output *active_output = NULL;
161 struct weston_output *output = NULL;
162 const char *app_id = NULL;
164 dclient = weston_desktop_surface_get_client(dsurface);
165 client = weston_desktop_client_get_client(dclient);
167 if (ivi->shell_client.resource &&
168 ivi->shell_client.status == BOUND_FAILED) {
169 wl_client_post_implementation_error(client,
170 "agl_shell has already been bound. "
171 "Check out bound_fail event");
175 surface = zalloc(sizeof *surface);
177 wl_client_post_no_memory(client);
181 surface->view = weston_desktop_surface_create_view(dsurface);
182 if (!surface->view) {
184 wl_client_post_no_memory(client);
189 surface->dsurface = dsurface;
190 surface->role = IVI_SURFACE_ROLE_NONE;
191 surface->mapped = false;
192 surface->advertised_on_launch = false;
193 surface->checked_pending = false;
194 wl_list_init(&surface->link);
196 wl_signal_init(&surface->signal_advertise_app);
198 surface->listener_advertise_app.notify = desktop_advertise_app;
199 wl_signal_add(&surface->signal_advertise_app,
200 &surface->listener_advertise_app);
202 weston_desktop_surface_set_user_data(dsurface, surface);
204 if (ivi->policy && ivi->policy->api.surface_create &&
205 !ivi->policy->api.surface_create(surface, ivi)) {
206 wl_client_post_no_memory(client);
211 app_id = weston_desktop_surface_get_app_id(dsurface);
213 if ((active_output = ivi_layout_find_with_app_id(app_id, ivi)))
214 ivi_set_pending_desktop_surface_remote(active_output, app_id);
216 /* reset any caps to make sure we apply the new caps */
217 ivi_seat_reset_caps_sent(ivi);
219 output = get_focused_output(ivi->compositor);
221 output = get_default_output(ivi->compositor);
223 if (output && ivi->shell_client.ready) {
224 struct ivi_output *ivi_output = to_ivi_output(output);
225 desktop_surface_added_configure(surface, ivi_output);
228 * We delay creating "normal" desktop surfaces until later, to
229 * give the shell-client an oppurtunity to set the surface as a
231 * Also delay the creation in order to have a valid app_id
232 * which will be used to set the proper role.
234 weston_log("Added surface %p, app_id %s to pending list\n",
236 wl_list_insert(&ivi->pending_surfaces, &surface->link);
241 desktop_surface_check_last_remote_surfaces(struct ivi_compositor *ivi, enum ivi_surface_role role)
244 struct ivi_surface *surf;
246 wl_list_for_each(surf, &ivi->surfaces, link)
247 if (surf->role == role)
254 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
256 struct ivi_surface *surface =
257 weston_desktop_surface_get_user_data(dsurface);
258 struct weston_surface *wsurface =
259 weston_desktop_surface_get_surface(dsurface);
260 const char *app_id = NULL;
261 struct weston_seat *wseat = NULL;
262 struct ivi_shell_seat *ivi_seat = NULL;
263 struct ivi_output *output = NULL;
265 /* we might not have a valid ivi_surface if _added failed due to
270 wseat = get_ivi_shell_weston_first_seat(surface->ivi);
272 ivi_seat = get_ivi_shell_seat(wseat);
274 output = ivi_layout_get_output_from_surface(surface);
276 wl_list_remove(&surface->listener_advertise_app.link);
277 surface->listener_advertise_app.notify = NULL;
279 app_id = weston_desktop_surface_get_app_id(dsurface);
281 /* special corner-case, pending_surfaces which are never activated or
282 * being assigned an output might land here so just remove the surface;
284 * the DESKTOP role can happen here as well, because we can fall-back
285 * to that when we try to determine the role type. Application that
286 * do not set the app_id will be land here, when destroyed */
287 if (output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
288 surface->role == IVI_SURFACE_ROLE_DESKTOP))
289 goto skip_output_asignment;
291 assert(output != NULL);
293 /* resize the active surface to the original size */
294 if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
295 surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
296 if (output && output->active) {
297 ivi_layout_desktop_resize(output->active, output->area_saved);
299 /* restore the area back so we can re-use it again if needed */
300 output->area = output->area_saved;
303 /* reset the active surface as well */
304 if (output && output->active && output->active == surface) {
305 output->active->view->is_mapped = false;
306 output->active->view->surface->is_mapped = false;
308 weston_layer_entry_remove(&output->active->view->layer_link);
309 output->active = NULL;
312 /* clear out focused_surface to avoid a stale focused_surface. the
313 * client shell is responsible for keeping track and switch back to the
314 * last active surface so we don't get do anything at removal, just
316 if (ivi_seat && ivi_seat->focused_surface == wsurface)
317 ivi_seat->focused_surface = NULL;
319 if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
320 output->type == OUTPUT_REMOTE)
321 ivi_destroy_waltham_destroy(surface);
323 /* check if there's a last 'remote' surface and insert a black
324 * surface view if there's no background set for that output
326 if ((desktop_surface_check_last_remote_surfaces(output->ivi,
327 IVI_SURFACE_ROLE_REMOTE) ||
328 desktop_surface_check_last_remote_surfaces(output->ivi,
329 IVI_SURFACE_ROLE_DESKTOP)) && output->type == OUTPUT_REMOTE)
330 if (!output->background)
331 insert_black_surface(output);
334 if (weston_surface_is_mapped(wsurface)) {
335 weston_desktop_surface_unlink_view(surface->view);
336 weston_view_destroy(surface->view);
339 /* invalidate agl-shell surfaces so we can re-use them when
341 if (surface->role == IVI_SURFACE_ROLE_PANEL) {
342 switch (surface->panel.edge) {
343 case AGL_SHELL_EDGE_TOP:
346 case AGL_SHELL_EDGE_BOTTOM:
347 output->bottom = NULL;
349 case AGL_SHELL_EDGE_LEFT:
352 case AGL_SHELL_EDGE_RIGHT:
353 output->right = NULL;
356 assert(!"Invalid edge detected\n");
358 } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
359 output->background = NULL;
362 skip_output_asignment:
363 weston_log("Removed surface %p, app_id %s, role %s\n", surface,
364 app_id, ivi_layout_get_surface_role_name(surface));
366 if (app_id && output)
367 shell_advertise_app_state(output->ivi, app_id,
368 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
370 wl_list_remove(&surface->link);
376 desktop_committed(struct weston_desktop_surface *dsurface,
377 int32_t sx, int32_t sy, void *userdata)
379 struct ivi_compositor *ivi = userdata;
380 struct ivi_surface *surface =
381 weston_desktop_surface_get_user_data(dsurface);
382 struct ivi_policy *policy = surface->ivi->policy;
384 if (policy && policy->api.surface_commited &&
385 !policy->api.surface_commited(surface, surface->ivi))
388 if (ivi->shell_client.ready && !surface->checked_pending) {
389 const char * app_id = weston_desktop_surface_get_app_id(dsurface);
390 weston_log("Checking pending surface %p, app_id %s\n", surface,
392 wl_list_remove(&surface->link);
393 wl_list_init(&surface->link);
394 ivi_check_pending_desktop_surface(surface);
395 surface->checked_pending = true;
398 if (!surface->advertised_on_launch &&
399 !wl_list_empty(&surface->ivi->desktop_clients))
400 wl_signal_emit(&surface->signal_advertise_app, surface);
402 /* this repaint schedule is needed to allow resizing to work with the
403 * help of the hidden layer:
405 * 1. add the view in the hidden layer and send out correct dimensions
406 * 2. clients changes its dimensions
407 * 3. client commits with the new dimensions
409 * For desktop and fullscreen, desktop_surface_added() sends the
410 * dimensions from the beginning so applications no need to resize, but
411 * if that weren't the case we still need this in.
413 weston_compositor_schedule_repaint(surface->ivi->compositor);
415 switch (surface->role) {
416 case IVI_SURFACE_ROLE_DESKTOP:
417 case IVI_SURFACE_ROLE_REMOTE:
418 ivi_layout_desktop_committed(surface);
420 case IVI_SURFACE_ROLE_POPUP:
421 ivi_layout_popup_committed(surface);
423 case IVI_SURFACE_ROLE_FULLSCREEN:
424 ivi_layout_fullscreen_committed(surface);
426 case IVI_SURFACE_ROLE_SPLIT_H:
427 case IVI_SURFACE_ROLE_SPLIT_V:
428 ivi_layout_split_committed(surface);
430 case IVI_SURFACE_ROLE_NONE:
431 case IVI_SURFACE_ROLE_BACKGROUND:
432 case IVI_SURFACE_ROLE_PANEL:
433 default: /* fall through */
439 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
440 struct weston_seat *seat, int32_t x, int32_t y,
447 desktop_set_parent(struct weston_desktop_surface *dsurface,
448 struct weston_desktop_surface *parent, void *userdata)
454 desktop_move(struct weston_desktop_surface *dsurface,
455 struct weston_seat *seat, uint32_t serial, void *userdata)
461 desktop_resize(struct weston_desktop_surface *dsurface,
462 struct weston_seat *seat, uint32_t serial,
463 enum weston_desktop_surface_edge edges, void *user_data)
469 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
470 bool fullscreen, struct weston_output *output,
477 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
478 bool maximized, void *userdata)
484 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
491 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
492 int32_t x, int32_t y, void *userdata)
497 static const struct weston_desktop_api desktop_api = {
498 .struct_size = sizeof desktop_api,
499 .ping_timeout = desktop_ping_timeout,
500 .pong = desktop_pong,
501 .surface_added = desktop_surface_added,
502 .surface_removed = desktop_surface_removed,
503 .committed = desktop_committed,
504 .show_window_menu = desktop_show_window_menu,
505 .set_parent = desktop_set_parent,
506 .move = desktop_move,
507 .resize = desktop_resize,
508 .fullscreen_requested = desktop_fullscreen_requested,
509 .maximized_requested = desktop_maximized_requested,
510 .minimized_requested = desktop_minimized_requested,
511 .set_xwayland_position = desktop_set_xwayland_position,
515 ivi_shell_destroy(struct wl_listener *listener, void *data)
517 struct ivi_compositor *ivi = container_of(listener,
518 struct ivi_compositor, destroy_listener);
520 ivi_shell_finalize(ivi);
521 ivi_compositor_destroy_pending_surfaces(ivi);
523 weston_desktop_destroy(ivi->desktop);
524 wl_list_remove(&listener->link);
528 ivi_desktop_init(struct ivi_compositor *ivi)
530 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
532 weston_log("Failed to create desktop globals");
536 if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
537 &ivi->destroy_listener, ivi_shell_destroy)) {