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);
81 weston_log("(background) position view %p, x %d, y %d\n", view,
82 woutput->x, woutput->y);
85 view->is_mapped = true;
86 view->surface->is_mapped = true;
88 weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
92 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
93 struct ivi_surface *panel)
95 struct weston_output *woutput = output->output;
96 struct weston_desktop_surface *dsurface;
97 struct weston_view *view;
98 struct weston_geometry geom;
105 assert(panel->role == IVI_SURFACE_ROLE_PANEL);
106 dsurface = panel->dsurface;
108 geom = weston_desktop_surface_get_geometry(dsurface);
109 #ifdef AGL_COMP_DEBUG
110 weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
111 geom.width, geom.height, geom.x, geom.y);
113 switch (panel->panel.edge) {
114 case AGL_SHELL_EDGE_TOP:
115 output->area.y += geom.height;
116 output->area.height -= geom.height;
118 case AGL_SHELL_EDGE_BOTTOM:
119 y += woutput->height - geom.height;
120 output->area.height -= geom.height;
122 case AGL_SHELL_EDGE_LEFT:
123 output->area.x += geom.width;
124 output->area.width -= geom.width;
126 case AGL_SHELL_EDGE_RIGHT:
127 x += woutput->width - geom.width;
128 output->area.width -= geom.width;
135 weston_view_set_output(view, woutput);
136 weston_view_set_position(view, x, y);
137 #ifdef AGL_COMP_DEBUG
138 weston_log("(panel) edge %d position view %p, x %d, y %d\n",
139 panel->panel.edge, view, x, y);
142 /* this is necessary for cases we already mapped it desktop_committed()
143 * but we not running the older qtwayland, so we still have a chance
144 * for this to run at the next test */
145 if (view->surface->is_mapped) {
146 weston_layer_entry_remove(&view->layer_link);
148 view->is_mapped = false;
149 view->surface->is_mapped = false;
152 /* give ivi_layout_panel_committed() a chance to map the view/surface
154 if ((geom.width == geom.height && geom.width == 0) &&
155 (geom.x == geom.y && geom.x == 0) &&
156 panel->panel.edge != AGL_SHELL_EDGE_TOP)
159 view->is_mapped = true;
160 view->surface->is_mapped = true;
161 #ifdef AGL_COMP_DEBUG
162 weston_log("panel type %d inited\n", panel->panel.edge);
164 weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
168 * Initializes all static parts of the layout, i.e. the background and panels.
171 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
173 ivi_background_init(ivi, output);
177 output->area.width = output->output->width;
178 output->area.height = output->output->height;
180 ivi_panel_init(ivi, output, output->top);
181 ivi_panel_init(ivi, output, output->bottom);
182 ivi_panel_init(ivi, output, output->left);
183 ivi_panel_init(ivi, output, output->right);
185 weston_compositor_schedule_repaint(ivi->compositor);
187 weston_log("Usable area: %dx%d+%d,%d\n",
188 output->area.width, output->area.height,
189 output->area.x, output->area.y);
193 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
195 struct ivi_surface *surf;
198 wl_list_for_each(surf, &ivi->surfaces, link) {
199 id = weston_desktop_surface_get_app_id(surf->dsurface);
200 if (id && strcmp(app_id, id) == 0)
208 ivi_layout_activate_complete(struct ivi_output *output,
209 struct ivi_surface *surf)
211 struct ivi_compositor *ivi = output->ivi;
212 struct weston_output *woutput = output->output;
213 struct weston_view *view = surf->view;
215 if (weston_view_is_mapped(view)) {
216 weston_layer_entry_remove(&view->layer_link);
219 weston_view_set_output(view, woutput);
220 weston_view_set_position(view,
221 woutput->x + output->area.x,
222 woutput->y + output->area.y);
224 view->is_mapped = true;
225 view->surface->is_mapped = true;
227 if (output->active) {
228 output->active->view->is_mapped = false;
229 output->active->view->surface->is_mapped = false;
231 weston_layer_entry_remove(&output->active->view->layer_link);
233 output->previous_active = output->active;
234 output->active = surf;
236 weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
237 weston_view_update_transform(view);
239 /* force repaint of the entire output */
240 weston_output_damage(output->output);
243 * the 'remote' role now makes use of this part so make sure we don't
244 * trip the enum such that we might end up with a modified output for
247 if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
248 if (surf->desktop.pending_output)
249 surf->desktop.last_output = surf->desktop.pending_output;
250 surf->desktop.pending_output = NULL;
253 weston_log("Activation completed for app_id %s, role %s\n",
254 weston_desktop_surface_get_app_id(surf->dsurface),
255 ivi_layout_get_surface_role_name(surf));
258 static struct ivi_output *
259 ivi_layout_find_app_id(const char *app_id, struct ivi_compositor *ivi)
261 struct ivi_output *out;
263 wl_list_for_each(out, &ivi->outputs, link) {
267 if (!strcmp(app_id, out->app_id))
275 static struct ivi_output *
276 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
278 struct ivi_output *out;
280 wl_list_for_each(out, &ivi->outputs, link) {
281 if (out->background &&
282 out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
290 ivi_layout_desktop_committed(struct ivi_surface *surf)
292 struct weston_desktop_surface *dsurf = surf->dsurface;
293 struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
294 struct ivi_policy *policy = surf->ivi->policy;
295 struct ivi_output *output;
296 const char *app_id = weston_desktop_surface_get_app_id(dsurf);
298 assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
299 surf->role == IVI_SURFACE_ROLE_REMOTE);
302 * we can't make use here of the ivi_layout_get_output_from_surface()
303 * due to the fact that we'll always land here when a surface performs
304 * a commit and pending_output will not bet set. This works in tandem
305 * with 'activated_by_default' at this point to avoid tripping over
306 * to a surface that continuously updates its content
308 if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
309 output = surf->desktop.pending_output;
311 output = surf->remote.output;
313 if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
314 struct ivi_output *r_output;
316 if (policy && policy->api.surface_activate_by_default &&
317 !policy->api.surface_activate_by_default(surf, surf->ivi))
320 /* we can only activate it again by using the protocol */
321 if (surf->activated_by_default)
324 /* check first if there aren't any outputs being set */
325 r_output = ivi_layout_find_app_id(app_id, surf->ivi);
327 /* try finding an output with a background and use that */
329 r_output = ivi_layout_find_bg_output(surf->ivi);
331 /* use the output of the bg to activate the app on start-up by
333 if (surf->view && r_output) {
334 if (app_id && r_output) {
335 weston_log("Surface with app_id %s, role %s activating by default\n",
336 weston_desktop_surface_get_app_id(surf->dsurface),
337 ivi_layout_get_surface_role_name(surf));
338 ivi_layout_activate(r_output, app_id);
339 surf->activated_by_default = true;
346 if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
347 if (policy && policy->api.surface_activate_by_default &&
348 !policy->api.surface_activate_by_default(surf, surf->ivi))
351 /* we can only activate it again by using the protocol, but
352 * additionally the output is not reset when
353 * ivi_layout_activate_complete() terminates so we use the
354 * current active surface to avoid hitting this again and again
356 if (surf->activated_by_default && output->active == surf)
360 weston_log("Surface with app_id %s, role %s activating by default\n",
361 weston_desktop_surface_get_app_id(surf->dsurface),
362 ivi_layout_get_surface_role_name(surf));
363 ivi_layout_activate(output, app_id);
364 surf->activated_by_default = true;
369 if (!weston_desktop_surface_get_maximized(dsurf) ||
370 geom.width != output->area.width ||
371 geom.height != output->area.height)
374 ivi_layout_activate_complete(output, surf);
378 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
380 struct ivi_compositor *ivi = surface->ivi;
382 struct weston_desktop_surface *dsurface = surface->dsurface;
383 struct weston_surface *wsurface =
384 weston_desktop_surface_get_surface(dsurface);
386 struct ivi_output *output = surface->split.output;
387 struct weston_output *woutput = output->output;
389 struct weston_view *view = surface->view;
390 struct weston_geometry geom;
392 if (surface->view->is_mapped)
395 geom = weston_desktop_surface_get_geometry(dsurface);
396 weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
397 geom.width, geom.height);
399 assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
401 weston_desktop_surface_set_fullscreen(dsurface, true);
403 weston_view_set_output(view, woutput);
404 weston_view_set_position(view, woutput->x, woutput->y);
405 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
407 weston_view_update_transform(view);
408 weston_view_damage_below(view);
410 wsurface->is_mapped = true;
411 surface->view->is_mapped = true;
415 ivi_layout_desktop_resize(struct ivi_surface *surface,
416 struct weston_geometry area)
418 struct weston_desktop_surface *dsurf = surface->dsurface;
419 struct weston_view *view = surface->view;
423 int width = area.width;
424 int height = area.height;
426 weston_desktop_surface_set_size(dsurf,
429 weston_view_set_position(view, x, y);
430 weston_view_update_transform(view);
431 weston_view_damage_below(view);
435 ivi_layout_split_committed(struct ivi_surface *surface)
437 struct ivi_compositor *ivi = surface->ivi;
439 struct weston_desktop_surface *dsurface = surface->dsurface;
440 struct weston_surface *wsurface =
441 weston_desktop_surface_get_surface(dsurface);
443 struct ivi_output *output = surface->split.output;
444 struct weston_output *woutput = output->output;
446 struct weston_view *view = surface->view;
447 struct weston_geometry geom;
455 if (surface->view->is_mapped)
458 geom = weston_desktop_surface_get_geometry(dsurface);
460 assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
461 surface->role == IVI_SURFACE_ROLE_SPLIT_V);
463 /* save the previous area in order to recover it back when if this kind
464 * of surface is being destroyed/removed */
465 output->area_saved = output->area;
467 switch (surface->role) {
468 case IVI_SURFACE_ROLE_SPLIT_V:
469 if (geom.width == woutput->width &&
470 geom.height == woutput->height)
471 geom.width = (output->area.width / 2);
473 x += woutput->width - geom.width;
474 output->area.width -= geom.width;
476 width = woutput->width - x;
477 height = output->area.height;
481 case IVI_SURFACE_ROLE_SPLIT_H:
482 if (geom.width == woutput->width &&
483 geom.height == woutput->height)
484 geom.height = (output->area.height / 2);
487 output->area.y += geom.height;
488 output->area.height -= geom.height;
490 width = output->area.width;
491 height = output->area.height;
497 assert(!"Invalid split orientation\n");
500 weston_desktop_surface_set_size(dsurface,
503 /* resize the active surface first, output->area already contains
504 * correct area to resize to */
506 ivi_layout_desktop_resize(output->active, output->area);
508 weston_view_set_output(view, woutput);
509 weston_view_set_position(view, x, y);
510 weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
512 weston_view_update_transform(view);
513 weston_view_damage_below(view);
515 wsurface->is_mapped = true;
516 surface->view->is_mapped = true;
520 ivi_layout_popup_committed(struct ivi_surface *surface)
522 struct ivi_compositor *ivi = surface->ivi;
524 struct weston_desktop_surface *dsurface = surface->dsurface;
525 struct weston_surface *wsurface =
526 weston_desktop_surface_get_surface(dsurface);
528 struct ivi_output *output = surface->popup.output;
529 struct weston_output *woutput = output->output;
531 struct weston_view *view = surface->view;
532 struct weston_geometry geom;
534 if (surface->view->is_mapped)
537 geom = weston_desktop_surface_get_geometry(dsurface);
538 weston_log("(popup) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
539 geom.width, geom.height);
541 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
543 weston_view_set_output(view, woutput);
544 if (surface->popup.x || surface->popup.y)
545 weston_view_set_position(view, surface->popup.x, surface->popup.y);
547 weston_view_set_position(view, geom.x, geom.y);
548 weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
550 weston_view_update_transform(view);
551 weston_view_damage_below(view);
553 wsurface->is_mapped = true;
554 surface->view->is_mapped = true;
558 ivi_layout_popup_re_add(struct ivi_surface *surface)
560 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
561 struct weston_view *view = surface->view;
563 if (weston_view_is_mapped(view)) {
564 struct weston_desktop_surface *dsurface = surface->dsurface;
565 struct weston_surface *wsurface =
566 weston_desktop_surface_get_surface(dsurface);
568 weston_layer_entry_remove(&view->layer_link);
570 wsurface->is_mapped = false;
571 view->is_mapped = false;
574 ivi_layout_popup_committed(surface);
578 ivi_layout_panel_committed(struct ivi_surface *surface)
580 struct ivi_compositor *ivi = surface->ivi;
581 struct ivi_output *output = surface->bg.output;
582 struct weston_output *woutput = output->output;
583 struct weston_desktop_surface *dsurface = surface->dsurface;
584 struct weston_surface *wsurface =
585 weston_desktop_surface_get_surface(dsurface);
586 struct weston_geometry geom;
590 assert(surface->role == IVI_SURFACE_ROLE_PANEL);
593 * If the desktop_surface geometry is not set and the panel is not a
594 * top one, we'll give this a chance to run, as some qtwayland version
595 * seem to have a 'problem', where the panel initilization part will
596 * have a desktop surface with 0 as geometry for *all* its members
597 * (width/height). Doing that will result in the panel not being
600 * Later versions of qtwayland do have the correct window geometry for
601 * the desktop surface so the weston_surface is already mapped in
604 if (wsurface->is_mapped)
607 geom = weston_desktop_surface_get_geometry(dsurface);
609 #ifdef AGL_COMP_DEBUG
610 weston_log("geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
611 geom.width, geom.height, geom.x, geom.y);
614 switch (surface->panel.edge) {
615 case AGL_SHELL_EDGE_TOP:
618 case AGL_SHELL_EDGE_BOTTOM:
619 y += woutput->height - geom.height;
621 case AGL_SHELL_EDGE_LEFT:
624 case AGL_SHELL_EDGE_RIGHT:
625 x += woutput->width - geom.width;
628 #ifndef AGL_COMP_DEBUG
629 weston_log("panel type %d commited\n", surface->panel.edge);
632 weston_view_set_output(surface->view, woutput);
633 weston_view_set_position(surface->view, x, y);
634 weston_layer_entry_insert(&ivi->panel.view_list,
635 &surface->view->layer_link);
637 weston_view_update_transform(surface->view);
638 weston_view_schedule_repaint(surface->view);
640 wsurface->is_mapped = true;
641 surface->view->is_mapped = true;
645 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
647 struct ivi_compositor *ivi = surf->ivi;
648 struct ivi_surface *is;
650 if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
651 surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
652 surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
655 wl_list_for_each(is, &ivi->surfaces, link)
663 ivi_layout_activate(struct ivi_output *output, const char *app_id)
665 struct ivi_compositor *ivi = output->ivi;
666 struct ivi_surface *surf;
667 struct weston_desktop_surface *dsurf;
668 struct weston_view *view;
669 struct weston_geometry geom;
670 struct ivi_policy *policy = output->ivi->policy;
672 surf = ivi_find_app(ivi, app_id);
676 if (policy && policy->api.surface_activate &&
677 !policy->api.surface_activate(surf, surf->ivi)) {
681 #ifdef AGL_COMP_DEBUG
682 weston_log("Activating app_id %s, type %s\n", app_id,
683 ivi_layout_get_surface_role_name(surf));
686 if (surf->role == IVI_SURFACE_ROLE_POPUP) {
687 ivi_layout_popup_re_add(surf);
691 /* do not 're'-activate surfaces that are split or active */
692 if (surf == output->active ||
693 ivi_layout_surface_is_split_or_fullscreen(surf))
697 dsurf = surf->dsurface;
699 geom = weston_desktop_surface_get_geometry(dsurf);
701 if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
702 surf->desktop.pending_output = output;
703 if (weston_desktop_surface_get_maximized(dsurf) &&
704 geom.width == output->area.width &&
705 geom.height == output->area.height) {
706 ivi_layout_activate_complete(output, surf);
710 weston_desktop_surface_set_maximized(dsurf, true);
711 weston_desktop_surface_set_size(dsurf,
713 output->area.height);
715 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
716 app_id, ivi_layout_get_surface_role_name(surf),
717 output->area.width, output->area.height);
719 * If the view isn't mapped, we put it onto the hidden layer so it will
720 * start receiving frame events, and will be able to act on our
723 if (!weston_view_is_mapped(view)) {
724 view->is_mapped = true;
725 view->surface->is_mapped = true;
727 weston_view_set_output(view, output->output);
728 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
729 /* force repaint of the entire output */
731 weston_log("Placed app_id %s, type %s in hidden layer\n",
732 app_id, ivi_layout_get_surface_role_name(surf));
733 weston_output_damage(output->output);
738 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
740 struct ivi_output *ivi_output = NULL;
742 switch (surf->role) {
743 case IVI_SURFACE_ROLE_DESKTOP:
744 if (surf->desktop.pending_output)
745 ivi_output = surf->desktop.pending_output;
747 ivi_output = surf->desktop.last_output;
749 case IVI_SURFACE_ROLE_POPUP:
750 ivi_output = surf->popup.output;
752 case IVI_SURFACE_ROLE_BACKGROUND:
753 ivi_output = surf->bg.output;
755 case IVI_SURFACE_ROLE_PANEL:
756 ivi_output = surf->panel.output;
758 case IVI_SURFACE_ROLE_FULLSCREEN:
759 ivi_output = surf->fullscreen.output;
761 case IVI_SURFACE_ROLE_SPLIT_H:
762 case IVI_SURFACE_ROLE_SPLIT_V:
763 ivi_output = surf->split.output;
765 case IVI_SURFACE_ROLE_REMOTE:
766 ivi_output = surf->remote.output;
768 case IVI_SURFACE_ROLE_NONE:
777 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
779 struct ivi_surface *surf;
780 struct ivi_output *ivi_output;
781 struct ivi_policy *policy = ivi->policy;
783 surf = ivi_find_app(ivi, app_id);
787 if (policy && policy->api.surface_deactivate &&
788 !policy->api.surface_deactivate(surf, surf->ivi)) {
792 ivi_output = ivi_layout_get_output_from_surface(surf);
793 weston_log("Deactiving %s, role %s\n", app_id,
794 ivi_layout_get_surface_role_name(surf));
796 if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
797 struct ivi_surface *previous_active;
799 previous_active = ivi_output->previous_active;
800 if (!previous_active) {
801 /* we don't have a previous active it means we should
803 if (ivi_output->active) {
804 struct weston_view *view;
806 view = ivi_output->active->view;
807 view->is_mapped = false;
808 view->surface->is_mapped = false;
810 weston_layer_entry_remove(&view->layer_link);
811 weston_output_damage(ivi_output->output);
814 struct weston_desktop_surface *dsurface;
815 const char *previous_active_app_id;
817 dsurface = previous_active->dsurface;
818 previous_active_app_id =
819 weston_desktop_surface_get_app_id(dsurface);
820 ivi_layout_activate(ivi_output, previous_active_app_id);
822 } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
823 struct weston_view *view = surf->view;
825 weston_layer_entry_remove(&view->layer_link);
826 weston_view_damage_below(view);