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
26 #include "ivi-compositor.h"
28 #include "shared/helpers.h"
33 #include <libweston/libweston.h>
34 #include <libweston-desktop/libweston-desktop.h>
36 #include "agl-shell-desktop-server-protocol.h"
38 #define AGL_COMP_DEBUG
40 static const char *ivi_roles_as_string[] = {
41 [IVI_SURFACE_ROLE_NONE] = "NONE",
42 [IVI_SURFACE_ROLE_BACKGROUND] = "BACKGROUND",
43 [IVI_SURFACE_ROLE_PANEL] = "PANEL",
44 [IVI_SURFACE_ROLE_DESKTOP] = "DESKTOP",
45 [IVI_SURFACE_ROLE_POPUP] = "POPUP",
46 [IVI_SURFACE_ROLE_SPLIT_H] = "SPLIT_H",
47 [IVI_SURFACE_ROLE_SPLIT_V] = "SPLIT_V",
48 [IVI_SURFACE_ROLE_FULLSCREEN] = "FULLSCREEN",
49 [IVI_SURFACE_ROLE_REMOTE] = "REMOTE",
53 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
55 if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
56 return " unknown surface role";
58 return ivi_roles_as_string[surf->role];
62 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
64 struct weston_output *woutput = output->output;
65 struct ivi_surface *bg = output->background;
66 struct weston_view *view;
69 weston_log("WARNING: Output does not have a background\n");
73 assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
77 weston_view_set_output(view, woutput);
78 weston_view_set_position(view, woutput->x, woutput->y);
80 weston_log("(background) position view %p, x %d, y %d, on output %s\n", view,
81 woutput->x, woutput->y, output->name);
83 view->is_mapped = true;
84 view->surface->is_mapped = true;
86 weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
90 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
91 struct ivi_surface *panel)
93 struct weston_output *woutput = output->output;
94 struct weston_desktop_surface *dsurface;
95 struct weston_view *view;
96 struct weston_geometry geom;
103 assert(panel->role == IVI_SURFACE_ROLE_PANEL);
104 dsurface = panel->dsurface;
106 geom = weston_desktop_surface_get_geometry(dsurface);
108 weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
109 geom.width, geom.height, geom.x, geom.y);
111 switch (panel->panel.edge) {
112 case AGL_SHELL_EDGE_TOP:
113 output->area.y += geom.height;
114 output->area.height -= geom.height;
116 case AGL_SHELL_EDGE_BOTTOM:
117 y += woutput->height - geom.height;
118 output->area.height -= geom.height;
120 case AGL_SHELL_EDGE_LEFT:
121 output->area.x += geom.width;
122 output->area.width -= geom.width;
124 case AGL_SHELL_EDGE_RIGHT:
125 x += woutput->width - geom.width;
126 output->area.width -= geom.width;
133 weston_view_set_output(view, woutput);
134 weston_view_set_position(view, x, y);
136 weston_log("(panel) edge %d position view %p, x %d, y %d\n",
137 panel->panel.edge, view, x, y);
139 view->is_mapped = true;
140 view->surface->is_mapped = true;
142 weston_log("panel type %d inited on output %s\n", panel->panel.edge,
145 weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
149 * Initializes all static parts of the layout, i.e. the background and panels.
152 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
154 ivi_background_init(ivi, output);
158 output->area.width = output->output->width;
159 output->area.height = output->output->height;
161 ivi_panel_init(ivi, output, output->top);
162 ivi_panel_init(ivi, output, output->bottom);
163 ivi_panel_init(ivi, output, output->left);
164 ivi_panel_init(ivi, output, output->right);
166 weston_compositor_schedule_repaint(ivi->compositor);
168 weston_log("Usable area: %dx%d+%d,%d\n",
169 output->area.width, output->area.height,
170 output->area.x, output->area.y);
174 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
176 struct ivi_surface *surf;
179 wl_list_for_each(surf, &ivi->surfaces, link) {
180 id = weston_desktop_surface_get_app_id(surf->dsurface);
181 if (id && strcmp(app_id, id) == 0)
189 ivi_layout_activate_complete(struct ivi_output *output,
190 struct ivi_surface *surf)
192 struct ivi_compositor *ivi = output->ivi;
193 struct weston_output *woutput = output->output;
194 struct weston_view *view = surf->view;
196 if (weston_view_is_mapped(view)) {
197 weston_layer_entry_remove(&view->layer_link);
200 weston_view_set_output(view, woutput);
201 weston_view_set_position(view,
202 woutput->x + output->area.x,
203 woutput->y + output->area.y);
205 view->is_mapped = true;
206 view->surface->is_mapped = true;
208 if (output->active) {
209 output->active->view->is_mapped = false;
210 output->active->view->surface->is_mapped = false;
212 weston_layer_entry_remove(&output->active->view->layer_link);
214 output->previous_active = output->active;
215 output->active = surf;
217 weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
218 weston_view_update_transform(view);
220 /* force repaint of the entire output */
221 weston_output_damage(output->output);
224 * the 'remote' role now makes use of this part so make sure we don't
225 * trip the enum such that we might end up with a modified output for
228 if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
229 if (surf->desktop.pending_output)
230 surf->desktop.last_output = surf->desktop.pending_output;
231 surf->desktop.pending_output = NULL;
234 weston_log("Activation completed for app_id %s, role %s, output %s\n",
235 weston_desktop_surface_get_app_id(surf->dsurface),
236 ivi_layout_get_surface_role_name(surf), output->name);
239 static struct ivi_output *
240 ivi_layout_find_app_id(const char *app_id, struct ivi_compositor *ivi)
242 struct ivi_output *out;
244 wl_list_for_each(out, &ivi->outputs, link) {
248 if (!strcmp(app_id, out->app_id))
256 static struct ivi_output *
257 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
259 struct ivi_output *out;
261 wl_list_for_each(out, &ivi->outputs, link) {
262 if (out->background &&
263 out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
271 ivi_layout_desktop_committed(struct ivi_surface *surf)
273 struct weston_desktop_surface *dsurf = surf->dsurface;
274 struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
275 struct ivi_policy *policy = surf->ivi->policy;
276 struct ivi_output *output;
277 const char *app_id = weston_desktop_surface_get_app_id(dsurf);
279 assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
280 surf->role == IVI_SURFACE_ROLE_REMOTE);
283 * we can't make use here of the ivi_layout_get_output_from_surface()
284 * due to the fact that we'll always land here when a surface performs
285 * a commit and pending_output will not bet set. This works in tandem
286 * with 'activated_by_default' at this point to avoid tripping over
287 * to a surface that continuously updates its content
289 if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
290 output = surf->desktop.pending_output;
292 output = surf->remote.output;
294 if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
295 struct ivi_output *r_output;
297 if (policy && policy->api.surface_activate_by_default &&
298 !policy->api.surface_activate_by_default(surf, surf->ivi))
301 /* we can only activate it again by using the protocol */
302 if (surf->activated_by_default)
305 /* check first if there aren't any outputs being set */
306 r_output = ivi_layout_find_app_id(app_id, surf->ivi);
309 struct weston_view *view = r_output->fullscreen_view.fs->view;
310 if (view->is_mapped || view->surface->is_mapped)
311 remove_black_surface(r_output);
315 /* try finding an output with a background and use that */
317 r_output = ivi_layout_find_bg_output(surf->ivi);
319 /* use the output of the bg to activate the app on start-up by
321 if (surf->view && r_output) {
322 if (app_id && r_output) {
323 weston_log("Surface with app_id %s, role %s activating by default\n",
324 weston_desktop_surface_get_app_id(surf->dsurface),
325 ivi_layout_get_surface_role_name(surf));
326 ivi_layout_activate(r_output, app_id);
327 surf->activated_by_default = true;
334 if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
335 if (policy && policy->api.surface_activate_by_default &&
336 !policy->api.surface_activate_by_default(surf, surf->ivi))
339 /* we can only activate it again by using the protocol, but
340 * additionally the output is not reset when
341 * ivi_layout_activate_complete() terminates so we use the
342 * current active surface to avoid hitting this again and again
344 if (surf->activated_by_default && output->active == surf)
348 weston_log("Surface with app_id %s, role %s activating by default\n",
349 weston_desktop_surface_get_app_id(surf->dsurface),
350 ivi_layout_get_surface_role_name(surf));
351 ivi_layout_activate(output, app_id);
352 surf->activated_by_default = true;
357 if (!weston_desktop_surface_get_maximized(dsurf) ||
358 geom.width != output->area.width ||
359 geom.height != output->area.height)
362 ivi_layout_activate_complete(output, surf);
366 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
368 struct ivi_compositor *ivi = surface->ivi;
370 struct weston_desktop_surface *dsurface = surface->dsurface;
371 struct weston_surface *wsurface =
372 weston_desktop_surface_get_surface(dsurface);
374 struct ivi_output *output = surface->split.output;
375 struct weston_output *woutput = output->output;
377 struct weston_view *view = surface->view;
378 struct weston_geometry geom;
380 if (surface->view->is_mapped)
383 geom = weston_desktop_surface_get_geometry(dsurface);
384 weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
385 geom.width, geom.height);
387 assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
389 weston_desktop_surface_set_fullscreen(dsurface, true);
391 weston_view_set_output(view, woutput);
392 weston_view_set_position(view, woutput->x, woutput->y);
393 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
395 weston_view_update_transform(view);
396 weston_view_damage_below(view);
398 wsurface->is_mapped = true;
399 surface->view->is_mapped = true;
403 ivi_layout_desktop_resize(struct ivi_surface *surface,
404 struct weston_geometry area)
406 struct weston_desktop_surface *dsurf = surface->dsurface;
407 struct weston_view *view = surface->view;
411 int width = area.width;
412 int height = area.height;
414 weston_desktop_surface_set_size(dsurf,
417 weston_view_set_position(view, x, y);
418 weston_view_update_transform(view);
419 weston_view_damage_below(view);
423 ivi_layout_split_committed(struct ivi_surface *surface)
425 struct ivi_compositor *ivi = surface->ivi;
427 struct weston_desktop_surface *dsurface = surface->dsurface;
428 struct weston_surface *wsurface =
429 weston_desktop_surface_get_surface(dsurface);
431 struct ivi_output *output = surface->split.output;
432 struct weston_output *woutput = output->output;
434 struct weston_view *view = surface->view;
435 struct weston_geometry geom;
443 if (surface->view->is_mapped)
446 geom = weston_desktop_surface_get_geometry(dsurface);
448 assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
449 surface->role == IVI_SURFACE_ROLE_SPLIT_V);
451 /* save the previous area in order to recover it back when if this kind
452 * of surface is being destroyed/removed */
453 output->area_saved = output->area;
455 switch (surface->role) {
456 case IVI_SURFACE_ROLE_SPLIT_V:
457 if (geom.width == woutput->width &&
458 geom.height == woutput->height)
459 geom.width = (output->area.width / 2);
461 x += woutput->width - geom.width;
462 output->area.width -= geom.width;
464 width = woutput->width - x;
465 height = output->area.height;
469 case IVI_SURFACE_ROLE_SPLIT_H:
470 if (geom.width == woutput->width &&
471 geom.height == woutput->height)
472 geom.height = (output->area.height / 2);
475 output->area.y += geom.height;
476 output->area.height -= geom.height;
478 width = output->area.width;
479 height = output->area.height;
485 assert(!"Invalid split orientation\n");
488 weston_desktop_surface_set_size(dsurface,
491 /* resize the active surface first, output->area already contains
492 * correct area to resize to */
494 ivi_layout_desktop_resize(output->active, output->area);
496 weston_view_set_output(view, woutput);
497 weston_view_set_position(view, x, y);
498 weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
500 weston_view_update_transform(view);
501 weston_view_damage_below(view);
503 wsurface->is_mapped = true;
504 surface->view->is_mapped = true;
508 ivi_layout_popup_committed(struct ivi_surface *surface)
510 struct ivi_compositor *ivi = surface->ivi;
512 struct weston_desktop_surface *dsurface = surface->dsurface;
513 struct weston_surface *wsurface =
514 weston_desktop_surface_get_surface(dsurface);
516 struct ivi_output *output = surface->popup.output;
517 struct weston_output *woutput = output->output;
519 struct weston_view *view = surface->view;
520 struct weston_geometry geom;
522 if (surface->view->is_mapped)
525 geom = weston_desktop_surface_get_geometry(dsurface);
526 weston_log("(popup) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
527 geom.width, geom.height);
529 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
531 weston_view_set_output(view, woutput);
532 if (surface->popup.x || surface->popup.y)
533 weston_view_set_position(view, surface->popup.x, surface->popup.y);
535 weston_view_set_position(view, geom.x, geom.y);
536 weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
538 weston_view_update_transform(view);
539 weston_view_damage_below(view);
541 wsurface->is_mapped = true;
542 surface->view->is_mapped = true;
546 ivi_layout_popup_re_add(struct ivi_surface *surface)
548 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
549 struct weston_view *view = surface->view;
551 if (weston_view_is_mapped(view)) {
552 struct weston_desktop_surface *dsurface = surface->dsurface;
553 struct weston_surface *wsurface =
554 weston_desktop_surface_get_surface(dsurface);
556 weston_layer_entry_remove(&view->layer_link);
558 wsurface->is_mapped = false;
559 view->is_mapped = false;
562 ivi_layout_popup_committed(surface);
566 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
568 struct ivi_compositor *ivi = surf->ivi;
569 struct ivi_surface *is;
571 if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
572 surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
573 surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
576 wl_list_for_each(is, &ivi->surfaces, link)
584 ivi_layout_activate(struct ivi_output *output, const char *app_id)
586 struct ivi_compositor *ivi = output->ivi;
587 struct ivi_surface *surf;
588 struct weston_desktop_surface *dsurf;
589 struct weston_view *view;
590 struct weston_geometry geom;
591 struct ivi_policy *policy = output->ivi->policy;
593 surf = ivi_find_app(ivi, app_id);
597 if (policy && policy->api.surface_activate &&
598 !policy->api.surface_activate(surf, surf->ivi)) {
602 #ifdef AGL_COMP_DEBUG
603 weston_log("Activating app_id %s, type %s\n", app_id,
604 ivi_layout_get_surface_role_name(surf));
607 if (surf->role == IVI_SURFACE_ROLE_POPUP) {
608 ivi_layout_popup_re_add(surf);
612 /* do not 're'-activate surfaces that are split or active */
613 if (surf == output->active ||
614 ivi_layout_surface_is_split_or_fullscreen(surf))
618 dsurf = surf->dsurface;
620 geom = weston_desktop_surface_get_geometry(dsurf);
622 if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
623 surf->desktop.pending_output = output;
624 if (weston_desktop_surface_get_maximized(dsurf) &&
625 geom.width == output->area.width &&
626 geom.height == output->area.height) {
627 ivi_layout_activate_complete(output, surf);
631 weston_desktop_surface_set_maximized(dsurf, true);
632 weston_desktop_surface_set_size(dsurf,
634 output->area.height);
636 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
637 app_id, ivi_layout_get_surface_role_name(surf),
638 output->area.width, output->area.height);
640 * If the view isn't mapped, we put it onto the hidden layer so it will
641 * start receiving frame events, and will be able to act on our
644 if (!weston_view_is_mapped(view)) {
645 view->is_mapped = true;
646 view->surface->is_mapped = true;
648 weston_view_set_output(view, output->output);
649 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
650 /* force repaint of the entire output */
652 weston_log("Placed app_id %s, type %s in hidden layer\n",
653 app_id, ivi_layout_get_surface_role_name(surf));
654 weston_output_damage(output->output);
659 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
661 struct ivi_output *ivi_output = NULL;
663 switch (surf->role) {
664 case IVI_SURFACE_ROLE_DESKTOP:
665 if (surf->desktop.pending_output)
666 ivi_output = surf->desktop.pending_output;
668 ivi_output = surf->desktop.last_output;
670 case IVI_SURFACE_ROLE_POPUP:
671 ivi_output = surf->popup.output;
673 case IVI_SURFACE_ROLE_BACKGROUND:
674 ivi_output = surf->bg.output;
676 case IVI_SURFACE_ROLE_PANEL:
677 ivi_output = surf->panel.output;
679 case IVI_SURFACE_ROLE_FULLSCREEN:
680 ivi_output = surf->fullscreen.output;
682 case IVI_SURFACE_ROLE_SPLIT_H:
683 case IVI_SURFACE_ROLE_SPLIT_V:
684 ivi_output = surf->split.output;
686 case IVI_SURFACE_ROLE_REMOTE:
687 ivi_output = surf->remote.output;
689 case IVI_SURFACE_ROLE_NONE:
698 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
700 struct ivi_surface *surf;
701 struct ivi_output *ivi_output;
702 struct ivi_policy *policy = ivi->policy;
704 surf = ivi_find_app(ivi, app_id);
708 if (policy && policy->api.surface_deactivate &&
709 !policy->api.surface_deactivate(surf, surf->ivi)) {
713 ivi_output = ivi_layout_get_output_from_surface(surf);
714 weston_log("Deactiving %s, role %s\n", app_id,
715 ivi_layout_get_surface_role_name(surf));
717 if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
718 struct ivi_surface *previous_active;
720 previous_active = ivi_output->previous_active;
721 if (!previous_active) {
722 /* we don't have a previous active it means we should
724 if (ivi_output->active) {
725 struct weston_view *view;
727 view = ivi_output->active->view;
728 view->is_mapped = false;
729 view->surface->is_mapped = false;
731 weston_layer_entry_remove(&view->layer_link);
732 weston_output_damage(ivi_output->output);
735 struct weston_desktop_surface *dsurface;
736 const char *previous_active_app_id;
738 dsurface = previous_active->dsurface;
739 previous_active_app_id =
740 weston_desktop_surface_get_app_id(dsurface);
741 ivi_layout_activate(ivi_output, previous_active_app_id);
743 } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
744 struct weston_view *view = surf->view;
746 weston_layer_entry_remove(&view->layer_link);
747 weston_view_damage_below(view);