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 /* 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 (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
332 ivi_surface_count_one(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 && output->output) {
370 shell_advertise_app_state(output->ivi, app_id,
371 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
372 if (output->ivi->shell_client.ready)
373 shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
376 wl_list_remove(&surface->link);
382 desktop_committed(struct weston_desktop_surface *dsurface,
383 int32_t sx, int32_t sy, void *userdata)
385 struct ivi_compositor *ivi = userdata;
386 struct ivi_surface *surface =
387 weston_desktop_surface_get_user_data(dsurface);
388 struct ivi_policy *policy = surface->ivi->policy;
390 if (policy && policy->api.surface_commited &&
391 !policy->api.surface_commited(surface, surface->ivi))
394 if (ivi->shell_client.ready && !surface->checked_pending) {
395 struct ivi_output *remote_output = NULL;
396 const char *app_id = weston_desktop_surface_get_app_id(dsurface);
397 weston_log("Checking pending surface %p, app_id %s\n", surface,
399 wl_list_remove(&surface->link);
400 wl_list_init(&surface->link);
402 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
403 ivi_set_pending_desktop_surface_remote(remote_output, app_id);
404 shell_send_app_on_output(ivi, app_id, remote_output->output->name);
408 ivi_check_pending_desktop_surface(surface);
409 surface->checked_pending = true;
411 /* we'll do it now at commit time, because we might not have an
412 * appid by the time we've created the weston_desktop_surface
414 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
417 if (!surface->advertised_on_launch &&
418 !wl_list_empty(&surface->ivi->desktop_clients))
419 wl_signal_emit(&surface->signal_advertise_app, surface);
421 /* this repaint schedule is needed to allow resizing to work with the
422 * help of the hidden layer:
424 * 1. add the view in the hidden layer and send out correct dimensions
425 * 2. clients changes its dimensions
426 * 3. client commits with the new dimensions
428 * For desktop and fullscreen, desktop_surface_added() sends the
429 * dimensions from the beginning so applications no need to resize, but
430 * if that weren't the case we still need this in.
432 weston_compositor_schedule_repaint(surface->ivi->compositor);
434 switch (surface->role) {
435 case IVI_SURFACE_ROLE_DESKTOP:
436 ivi_layout_desktop_committed(surface);
438 case IVI_SURFACE_ROLE_REMOTE:
439 ivi_layout_remote_committed(surface);
441 case IVI_SURFACE_ROLE_POPUP:
442 ivi_layout_popup_committed(surface);
444 case IVI_SURFACE_ROLE_FULLSCREEN:
445 ivi_layout_fullscreen_committed(surface);
447 case IVI_SURFACE_ROLE_SPLIT_H:
448 case IVI_SURFACE_ROLE_SPLIT_V:
449 ivi_layout_split_committed(surface);
451 case IVI_SURFACE_ROLE_NONE:
452 case IVI_SURFACE_ROLE_BACKGROUND:
453 case IVI_SURFACE_ROLE_PANEL:
454 default: /* fall through */
460 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
461 struct weston_seat *seat, int32_t x, int32_t y,
468 desktop_set_parent(struct weston_desktop_surface *dsurface,
469 struct weston_desktop_surface *parent, void *userdata)
475 desktop_move(struct weston_desktop_surface *dsurface,
476 struct weston_seat *seat, uint32_t serial, void *userdata)
482 desktop_resize(struct weston_desktop_surface *dsurface,
483 struct weston_seat *seat, uint32_t serial,
484 enum weston_desktop_surface_edge edges, void *user_data)
490 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
491 bool fullscreen, struct weston_output *output,
498 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
499 bool maximized, void *userdata)
505 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
512 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
513 int32_t x, int32_t y, void *userdata)
518 static const struct weston_desktop_api desktop_api = {
519 .struct_size = sizeof desktop_api,
520 .ping_timeout = desktop_ping_timeout,
521 .pong = desktop_pong,
522 .surface_added = desktop_surface_added,
523 .surface_removed = desktop_surface_removed,
524 .committed = desktop_committed,
525 .show_window_menu = desktop_show_window_menu,
526 .set_parent = desktop_set_parent,
527 .move = desktop_move,
528 .resize = desktop_resize,
529 .fullscreen_requested = desktop_fullscreen_requested,
530 .maximized_requested = desktop_maximized_requested,
531 .minimized_requested = desktop_minimized_requested,
532 .set_xwayland_position = desktop_set_xwayland_position,
536 ivi_shell_destroy(struct wl_listener *listener, void *data)
538 struct ivi_compositor *ivi = container_of(listener,
539 struct ivi_compositor, destroy_listener);
541 ivi_shell_finalize(ivi);
542 ivi_compositor_destroy_pending_surfaces(ivi);
543 ivi_layout_destroy_saved_outputs(ivi);
545 weston_desktop_destroy(ivi->desktop);
546 wl_list_remove(&listener->link);
550 ivi_desktop_init(struct ivi_compositor *ivi)
552 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
554 weston_log("Failed to create desktop globals");
558 if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
559 &ivi->destroy_listener, ivi_shell_destroy)) {