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 static struct weston_output *
38 get_default_output(struct weston_compositor *compositor)
40 if (wl_list_empty(&compositor->output_list))
43 return wl_container_of(compositor->output_list.next,
44 struct weston_output, link);
49 desktop_advertise_app(struct wl_listener *listener, void *data)
51 struct ivi_surface *surface;
53 surface = wl_container_of(listener, surface, listener_advertise_app);
55 agl_shell_desktop_advertise_application_id(surface->ivi, surface);
59 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
65 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
70 struct weston_output *
71 get_default_output(struct weston_compositor *compositor)
73 if (wl_list_empty(&compositor->output_list))
76 return container_of(compositor->output_list.next,
77 struct weston_output, link);
80 struct weston_output *
81 get_focused_output(struct weston_compositor *compositor)
83 struct weston_seat *seat;
84 struct weston_output *output = NULL;
86 wl_list_for_each(seat, &compositor->seat_list, link) {
87 struct weston_touch *touch = weston_seat_get_touch(seat);
88 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
89 struct weston_keyboard *keyboard =
90 weston_seat_get_keyboard(seat);
92 if (touch && touch->focus)
93 output = touch->focus->output;
94 else if (pointer && pointer->focus)
95 output = pointer->focus->output;
96 else if (keyboard && keyboard->focus)
97 output = keyboard->focus->output;
108 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
110 struct ivi_compositor *ivi = userdata;
111 struct weston_desktop_client *dclient;
112 struct wl_client *client;
113 struct ivi_surface *surface;
114 struct ivi_output *active_output = NULL;
115 struct weston_output *output = NULL;
116 const char *app_id = NULL;
118 dclient = weston_desktop_surface_get_client(dsurface);
119 client = weston_desktop_client_get_client(dclient);
121 surface = zalloc(sizeof *surface);
123 wl_client_post_no_memory(client);
127 surface->view = weston_desktop_surface_create_view(dsurface);
128 if (!surface->view) {
130 wl_client_post_no_memory(client);
135 surface->dsurface = dsurface;
136 surface->role = IVI_SURFACE_ROLE_NONE;
137 surface->activated_by_default = false;
138 surface->advertised_on_launch = false;
139 surface->checked_pending = false;
140 wl_list_init(&surface->link);
142 wl_signal_init(&surface->signal_advertise_app);
144 surface->listener_advertise_app.notify = desktop_advertise_app;
145 wl_signal_add(&surface->signal_advertise_app,
146 &surface->listener_advertise_app);
148 weston_desktop_surface_set_user_data(dsurface, surface);
150 if (ivi->policy && ivi->policy->api.surface_create &&
151 !ivi->policy->api.surface_create(surface, ivi)) {
152 wl_client_post_no_memory(client);
157 app_id = weston_desktop_surface_get_app_id(dsurface);
159 if ((active_output = ivi_layout_find_with_app_id(app_id, ivi)))
160 ivi_set_pending_desktop_surface_remote(active_output, app_id);
162 /* reset any caps to make sure we apply the new caps */
163 ivi_seat_reset_caps_sent(ivi);
165 output = get_focused_output(ivi->compositor);
167 output = get_default_output(ivi->compositor);
169 if (output && ivi->shell_client.ready) {
170 struct ivi_output *ivi_output = to_ivi_output(output);
172 weston_log("Setting surface to initial size of surface to %dx%d\n",
173 ivi_output->area.width, ivi_output->area.height);
174 weston_desktop_surface_set_maximized(dsurface, true);
175 weston_desktop_surface_set_size(dsurface,
176 ivi_output->area.width, ivi_output->area.height);
179 * We delay creating "normal" desktop surfaces until later, to
180 * give the shell-client an oppurtunity to set the surface as a
182 * Also delay the creation in order to have a valid app_id
183 * which will be used to set the proper role.
185 weston_log("Added surface %p, app_id %s to pending list\n",
187 wl_list_insert(&ivi->pending_surfaces, &surface->link);
192 desktop_surface_check_last_remote_surfaces(struct ivi_compositor *ivi, enum ivi_surface_role role)
195 struct ivi_surface *surf;
197 wl_list_for_each(surf, &ivi->surfaces, link)
198 if (surf->role == role)
205 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
207 struct ivi_surface *surface =
208 weston_desktop_surface_get_user_data(dsurface);
209 struct weston_surface *wsurface =
210 weston_desktop_surface_get_surface(dsurface);
211 const char *app_id = NULL;
213 struct ivi_output *output = ivi_layout_get_output_from_surface(surface);
215 wl_list_remove(&surface->listener_advertise_app.link);
216 surface->listener_advertise_app.notify = NULL;
218 app_id = weston_desktop_surface_get_app_id(dsurface);
220 /* special corner-case, pending_surfaces which are never activated or
221 * being assigned an output might land here so just remove the surface;
223 * the DESKTOP role can happen here as well, because we can fall-back
224 * to that when we try to determine the role type. Application that
225 * do not set the app_id will be land here, when destroyed */
226 if (output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
227 surface->role == IVI_SURFACE_ROLE_DESKTOP))
228 goto skip_output_asignment;
230 assert(output != NULL);
232 /* resize the active surface to the original size */
233 if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
234 surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
235 if (output && output->active) {
236 ivi_layout_desktop_resize(output->active, output->area_saved);
238 /* restore the area back so we can re-use it again if needed */
239 output->area = output->area_saved;
242 /* reset the active surface as well */
243 if (output && output->active && output->active == surface) {
244 output->active->view->is_mapped = false;
245 output->active->view->surface->is_mapped = false;
247 weston_layer_entry_remove(&output->active->view->layer_link);
248 output->active = NULL;
251 if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
252 output->type == OUTPUT_REMOTE)
253 ivi_destroy_waltham_destroy(surface);
255 /* check if there's a last 'remote' surface and insert a black
256 * surface view if there's no background set for that output
258 if ((desktop_surface_check_last_remote_surfaces(output->ivi,
259 IVI_SURFACE_ROLE_REMOTE) ||
260 desktop_surface_check_last_remote_surfaces(output->ivi,
261 IVI_SURFACE_ROLE_DESKTOP)) && output->type == OUTPUT_REMOTE)
262 if (!output->background)
263 insert_black_surface(output);
266 if (weston_surface_is_mapped(wsurface)) {
267 weston_desktop_surface_unlink_view(surface->view);
268 weston_view_destroy(surface->view);
271 /* invalidate agl-shell surfaces so we can re-use them when
273 if (surface->role == IVI_SURFACE_ROLE_PANEL) {
274 switch (surface->panel.edge) {
275 case AGL_SHELL_EDGE_TOP:
278 case AGL_SHELL_EDGE_BOTTOM:
279 output->bottom = NULL;
281 case AGL_SHELL_EDGE_LEFT:
284 case AGL_SHELL_EDGE_RIGHT:
285 output->right = NULL;
288 assert(!"Invalid edge detected\n");
290 } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
291 output->background = NULL;
294 skip_output_asignment:
295 weston_log("Removed surface %p, app_id %s, role %s\n", surface,
296 app_id, ivi_layout_get_surface_role_name(surface));
298 if (app_id && output)
299 shell_advertise_app_state(output->ivi, app_id,
300 NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
302 wl_list_remove(&surface->link);
308 desktop_committed(struct weston_desktop_surface *dsurface,
309 int32_t sx, int32_t sy, void *userdata)
311 struct ivi_compositor *ivi = userdata;
312 struct ivi_surface *surface =
313 weston_desktop_surface_get_user_data(dsurface);
314 struct ivi_policy *policy = surface->ivi->policy;
316 if (policy && policy->api.surface_commited &&
317 !policy->api.surface_commited(surface, surface->ivi))
320 if (ivi->shell_client.ready && !surface->checked_pending) {
321 const char * app_id = weston_desktop_surface_get_app_id(dsurface);
322 weston_log("Checking pending surface %p, app_id %s\n", surface,
324 wl_list_remove(&surface->link);
325 wl_list_init(&surface->link);
326 ivi_check_pending_desktop_surface(surface);
327 surface->checked_pending = true;
330 if (!surface->advertised_on_launch &&
331 !wl_list_empty(&surface->ivi->desktop_clients))
332 wl_signal_emit(&surface->signal_advertise_app, surface);
334 weston_compositor_schedule_repaint(surface->ivi->compositor);
336 switch (surface->role) {
337 case IVI_SURFACE_ROLE_DESKTOP:
338 case IVI_SURFACE_ROLE_REMOTE:
339 ivi_layout_desktop_committed(surface);
341 case IVI_SURFACE_ROLE_POPUP:
342 ivi_layout_popup_committed(surface);
344 case IVI_SURFACE_ROLE_FULLSCREEN:
345 ivi_layout_fullscreen_committed(surface);
347 case IVI_SURFACE_ROLE_SPLIT_H:
348 case IVI_SURFACE_ROLE_SPLIT_V:
349 ivi_layout_split_committed(surface);
351 case IVI_SURFACE_ROLE_NONE:
352 case IVI_SURFACE_ROLE_BACKGROUND:
353 case IVI_SURFACE_ROLE_PANEL:
354 default: /* fall through */
360 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
361 struct weston_seat *seat, int32_t x, int32_t y,
368 desktop_set_parent(struct weston_desktop_surface *dsurface,
369 struct weston_desktop_surface *parent, void *userdata)
375 desktop_move(struct weston_desktop_surface *dsurface,
376 struct weston_seat *seat, uint32_t serial, void *userdata)
382 desktop_resize(struct weston_desktop_surface *dsurface,
383 struct weston_seat *seat, uint32_t serial,
384 enum weston_desktop_surface_edge edges, void *user_data)
390 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
391 bool fullscreen, struct weston_output *output,
398 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
399 bool maximized, void *userdata)
405 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
412 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
413 int32_t x, int32_t y, void *userdata)
418 static const struct weston_desktop_api desktop_api = {
419 .struct_size = sizeof desktop_api,
420 .ping_timeout = desktop_ping_timeout,
421 .pong = desktop_pong,
422 .surface_added = desktop_surface_added,
423 .surface_removed = desktop_surface_removed,
424 .committed = desktop_committed,
425 .show_window_menu = desktop_show_window_menu,
426 .set_parent = desktop_set_parent,
427 .move = desktop_move,
428 .resize = desktop_resize,
429 .fullscreen_requested = desktop_fullscreen_requested,
430 .maximized_requested = desktop_maximized_requested,
431 .minimized_requested = desktop_minimized_requested,
432 .set_xwayland_position = desktop_set_xwayland_position,
436 ivi_desktop_init(struct ivi_compositor *ivi)
438 ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
440 weston_log("Failed to create desktop globals");