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_bg_output(struct ivi_compositor *ivi)
261 struct ivi_output *out;
263 wl_list_for_each(out, &ivi->outputs, link) {
264 if (out->background &&
265 out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
273 ivi_layout_desktop_committed(struct ivi_surface *surf)
275 struct weston_desktop_surface *dsurf = surf->dsurface;
276 struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
277 struct ivi_policy *policy = surf->ivi->policy;
278 struct ivi_output *output;
280 assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
281 surf->role == IVI_SURFACE_ROLE_REMOTE);
284 * we can't make use here of the ivi_layout_get_output_from_surface()
285 * due to the fact that we'll always land here when a surface performs
286 * a commit and pending_output will not bet set. This works in tandem
287 * with 'activated_by_default' at this point to avoid tripping over
288 * to a surface that continuously updates its content
290 if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
291 output = surf->desktop.pending_output;
293 output = surf->remote.output;
295 if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
296 struct ivi_output *ivi_bg_output;
298 if (policy && policy->api.surface_activate_by_default &&
299 !policy->api.surface_activate_by_default(surf, surf->ivi))
302 /* we can only activate it again by using the protocol */
303 if (surf->activated_by_default)
306 ivi_bg_output = ivi_layout_find_bg_output(surf->ivi);
308 /* use the output of the bg to activate the app on start-up by
310 if (surf->view && ivi_bg_output) {
312 weston_desktop_surface_get_app_id(dsurf);
313 if (app_id && ivi_bg_output) {
314 weston_log("Surface with app_id %s, role %s activating by default\n",
315 weston_desktop_surface_get_app_id(surf->dsurface),
316 ivi_layout_get_surface_role_name(surf));
317 ivi_layout_activate(ivi_bg_output, app_id);
318 surf->activated_by_default = true;
325 if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
327 if (policy && policy->api.surface_activate_by_default &&
328 !policy->api.surface_activate_by_default(surf, surf->ivi))
331 /* we can only activate it again by using the protocol, but
332 * additionally the output is not reset when
333 * ivi_layout_activate_complete() terminates so we use the
334 * current active surface to avoid hitting this again and again
336 if (surf->activated_by_default && output->active == surf)
339 app_id = weston_desktop_surface_get_app_id(dsurf);
341 weston_log("Surface with app_id %s, role %s activating by default\n",
342 weston_desktop_surface_get_app_id(surf->dsurface),
343 ivi_layout_get_surface_role_name(surf));
344 ivi_layout_activate(output, app_id);
345 surf->activated_by_default = true;
350 if (!weston_desktop_surface_get_maximized(dsurf) ||
351 geom.width != output->area.width ||
352 geom.height != output->area.height)
355 ivi_layout_activate_complete(output, surf);
359 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
361 struct ivi_compositor *ivi = surface->ivi;
363 struct weston_desktop_surface *dsurface = surface->dsurface;
364 struct weston_surface *wsurface =
365 weston_desktop_surface_get_surface(dsurface);
367 struct ivi_output *output = surface->split.output;
368 struct weston_output *woutput = output->output;
370 struct weston_view *view = surface->view;
371 struct weston_geometry geom;
373 if (surface->view->is_mapped)
376 geom = weston_desktop_surface_get_geometry(dsurface);
377 weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
378 geom.width, geom.height);
380 assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
382 weston_desktop_surface_set_fullscreen(dsurface, true);
384 weston_view_set_output(view, woutput);
385 weston_view_set_position(view, woutput->x, woutput->y);
386 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
388 weston_view_update_transform(view);
389 weston_view_damage_below(view);
391 wsurface->is_mapped = true;
392 surface->view->is_mapped = true;
396 ivi_layout_desktop_resize(struct ivi_surface *surface,
397 struct weston_geometry area)
399 struct weston_desktop_surface *dsurf = surface->dsurface;
400 struct weston_view *view = surface->view;
404 int width = area.width;
405 int height = area.height;
407 weston_desktop_surface_set_size(dsurf,
410 weston_view_set_position(view, x, y);
411 weston_view_update_transform(view);
412 weston_view_damage_below(view);
416 ivi_layout_split_committed(struct ivi_surface *surface)
418 struct ivi_compositor *ivi = surface->ivi;
420 struct weston_desktop_surface *dsurface = surface->dsurface;
421 struct weston_surface *wsurface =
422 weston_desktop_surface_get_surface(dsurface);
424 struct ivi_output *output = surface->split.output;
425 struct weston_output *woutput = output->output;
427 struct weston_view *view = surface->view;
428 struct weston_geometry geom;
436 if (surface->view->is_mapped)
439 geom = weston_desktop_surface_get_geometry(dsurface);
441 assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
442 surface->role == IVI_SURFACE_ROLE_SPLIT_V);
444 /* save the previous area in order to recover it back when if this kind
445 * of surface is being destroyed/removed */
446 output->area_saved = output->area;
448 switch (surface->role) {
449 case IVI_SURFACE_ROLE_SPLIT_V:
450 if (geom.width == woutput->width &&
451 geom.height == woutput->height)
452 geom.width = (output->area.width / 2);
454 x += woutput->width - geom.width;
455 output->area.width -= geom.width;
457 width = woutput->width - x;
458 height = output->area.height;
462 case IVI_SURFACE_ROLE_SPLIT_H:
463 if (geom.width == woutput->width &&
464 geom.height == woutput->height)
465 geom.height = (output->area.height / 2);
468 output->area.y += geom.height;
469 output->area.height -= geom.height;
471 width = output->area.width;
472 height = output->area.height;
478 assert(!"Invalid split orientation\n");
481 weston_desktop_surface_set_size(dsurface,
484 /* resize the active surface first, output->area already contains
485 * correct area to resize to */
487 ivi_layout_desktop_resize(output->active, output->area);
489 weston_view_set_output(view, woutput);
490 weston_view_set_position(view, x, y);
491 weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
493 weston_view_update_transform(view);
494 weston_view_damage_below(view);
496 wsurface->is_mapped = true;
497 surface->view->is_mapped = true;
501 ivi_layout_popup_committed(struct ivi_surface *surface)
503 struct ivi_compositor *ivi = surface->ivi;
505 struct weston_desktop_surface *dsurface = surface->dsurface;
506 struct weston_surface *wsurface =
507 weston_desktop_surface_get_surface(dsurface);
509 struct ivi_output *output = surface->popup.output;
510 struct weston_output *woutput = output->output;
512 struct weston_view *view = surface->view;
513 struct weston_geometry geom;
515 if (surface->view->is_mapped)
518 geom = weston_desktop_surface_get_geometry(dsurface);
519 weston_log("(popup) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
520 geom.width, geom.height);
522 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
524 weston_view_set_output(view, woutput);
525 if (surface->popup.x || surface->popup.y)
526 weston_view_set_position(view, surface->popup.x, surface->popup.y);
528 weston_view_set_position(view, geom.x, geom.y);
529 weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
531 weston_view_update_transform(view);
532 weston_view_damage_below(view);
534 wsurface->is_mapped = true;
535 surface->view->is_mapped = true;
539 ivi_layout_popup_re_add(struct ivi_surface *surface)
541 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
542 struct weston_view *view = surface->view;
544 if (weston_view_is_mapped(view)) {
545 struct weston_desktop_surface *dsurface = surface->dsurface;
546 struct weston_surface *wsurface =
547 weston_desktop_surface_get_surface(dsurface);
549 weston_layer_entry_remove(&view->layer_link);
551 wsurface->is_mapped = false;
552 view->is_mapped = false;
555 ivi_layout_popup_committed(surface);
559 ivi_layout_panel_committed(struct ivi_surface *surface)
561 struct ivi_compositor *ivi = surface->ivi;
562 struct ivi_output *output = surface->bg.output;
563 struct weston_output *woutput = output->output;
564 struct weston_desktop_surface *dsurface = surface->dsurface;
565 struct weston_surface *wsurface =
566 weston_desktop_surface_get_surface(dsurface);
567 struct weston_geometry geom;
571 assert(surface->role == IVI_SURFACE_ROLE_PANEL);
574 * If the desktop_surface geometry is not set and the panel is not a
575 * top one, we'll give this a chance to run, as some qtwayland version
576 * seem to have a 'problem', where the panel initilization part will
577 * have a desktop surface with 0 as geometry for *all* its members
578 * (width/height). Doing that will result in the panel not being
581 * Later versions of qtwayland do have the correct window geometry for
582 * the desktop surface so the weston_surface is already mapped in
585 if (wsurface->is_mapped)
588 geom = weston_desktop_surface_get_geometry(dsurface);
590 #ifdef AGL_COMP_DEBUG
591 weston_log("geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
592 geom.width, geom.height, geom.x, geom.y);
595 switch (surface->panel.edge) {
596 case AGL_SHELL_EDGE_TOP:
599 case AGL_SHELL_EDGE_BOTTOM:
600 y += woutput->height - geom.height;
602 case AGL_SHELL_EDGE_LEFT:
605 case AGL_SHELL_EDGE_RIGHT:
606 x += woutput->width - geom.width;
609 #ifndef AGL_COMP_DEBUG
610 weston_log("panel type %d commited\n", surface->panel.edge);
613 weston_view_set_output(surface->view, woutput);
614 weston_view_set_position(surface->view, x, y);
615 weston_layer_entry_insert(&ivi->panel.view_list,
616 &surface->view->layer_link);
618 weston_view_update_transform(surface->view);
619 weston_view_schedule_repaint(surface->view);
621 wsurface->is_mapped = true;
622 surface->view->is_mapped = true;
626 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
628 struct ivi_compositor *ivi = surf->ivi;
629 struct ivi_surface *is;
631 if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
632 surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
633 surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
636 wl_list_for_each(is, &ivi->surfaces, link)
644 ivi_layout_activate(struct ivi_output *output, const char *app_id)
646 struct ivi_compositor *ivi = output->ivi;
647 struct ivi_surface *surf;
648 struct weston_desktop_surface *dsurf;
649 struct weston_view *view;
650 struct weston_geometry geom;
651 struct ivi_policy *policy = output->ivi->policy;
653 surf = ivi_find_app(ivi, app_id);
657 if (policy && policy->api.surface_activate &&
658 !policy->api.surface_activate(surf, surf->ivi)) {
662 #ifdef AGL_COMP_DEBUG
663 weston_log("Activating app_id %s, type %s\n", app_id,
664 ivi_layout_get_surface_role_name(surf));
667 if (surf->role == IVI_SURFACE_ROLE_POPUP) {
668 ivi_layout_popup_re_add(surf);
672 /* do not 're'-activate surfaces that are split or active */
673 if (surf == output->active ||
674 ivi_layout_surface_is_split_or_fullscreen(surf))
678 dsurf = surf->dsurface;
680 geom = weston_desktop_surface_get_geometry(dsurf);
682 if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
683 surf->desktop.pending_output = output;
684 if (weston_desktop_surface_get_maximized(dsurf) &&
685 geom.width == output->area.width &&
686 geom.height == output->area.height) {
687 ivi_layout_activate_complete(output, surf);
691 weston_desktop_surface_set_maximized(dsurf, true);
692 weston_desktop_surface_set_size(dsurf,
694 output->area.height);
696 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
697 app_id, ivi_layout_get_surface_role_name(surf),
698 output->area.width, output->area.height);
700 * If the view isn't mapped, we put it onto the hidden layer so it will
701 * start receiving frame events, and will be able to act on our
704 if (!weston_view_is_mapped(view)) {
705 view->is_mapped = true;
706 view->surface->is_mapped = true;
708 weston_view_set_output(view, output->output);
709 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
710 /* force repaint of the entire output */
712 weston_log("Placed app_id %s, type %s in hidden layer\n",
713 app_id, ivi_layout_get_surface_role_name(surf));
714 weston_output_damage(output->output);
719 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
721 struct ivi_output *ivi_output = NULL;
723 switch (surf->role) {
724 case IVI_SURFACE_ROLE_DESKTOP:
725 if (surf->desktop.pending_output)
726 ivi_output = surf->desktop.pending_output;
728 ivi_output = surf->desktop.last_output;
730 case IVI_SURFACE_ROLE_POPUP:
731 ivi_output = surf->popup.output;
733 case IVI_SURFACE_ROLE_BACKGROUND:
734 ivi_output = surf->bg.output;
736 case IVI_SURFACE_ROLE_PANEL:
737 ivi_output = surf->panel.output;
739 case IVI_SURFACE_ROLE_FULLSCREEN:
740 ivi_output = surf->fullscreen.output;
742 case IVI_SURFACE_ROLE_SPLIT_H:
743 case IVI_SURFACE_ROLE_SPLIT_V:
744 ivi_output = surf->split.output;
746 case IVI_SURFACE_ROLE_REMOTE:
747 ivi_output = surf->remote.output;
749 case IVI_SURFACE_ROLE_NONE:
758 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
760 struct ivi_surface *surf;
761 struct ivi_output *ivi_output;
762 struct ivi_policy *policy = ivi->policy;
764 surf = ivi_find_app(ivi, app_id);
768 if (policy && policy->api.surface_deactivate &&
769 !policy->api.surface_deactivate(surf, surf->ivi)) {
773 ivi_output = ivi_layout_get_output_from_surface(surf);
774 weston_log("Deactiving %s, role %s\n", app_id,
775 ivi_layout_get_surface_role_name(surf));
777 if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
778 struct ivi_surface *previous_active;
780 previous_active = ivi_output->previous_active;
781 if (!previous_active) {
782 /* we don't have a previous active it means we should
784 if (ivi_output->active) {
785 struct weston_view *view;
787 view = ivi_output->active->view;
788 view->is_mapped = false;
789 view->surface->is_mapped = false;
791 weston_layer_entry_remove(&view->layer_link);
792 weston_output_damage(ivi_output->output);
795 struct weston_desktop_surface *dsurface;
796 const char *previous_active_app_id;
798 dsurface = previous_active->dsurface;
799 previous_active_app_id =
800 weston_desktop_surface_get_app_id(dsurface);
801 ivi_layout_activate(ivi_output, previous_active_app_id);
803 } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
804 struct weston_view *view = surf->view;
806 weston_layer_entry_remove(&view->layer_link);
807 weston_view_damage_below(view);