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("Activated 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);
328 struct weston_view *view = r_output->fullscreen_view.fs->view;
329 if (view->is_mapped || view->surface->is_mapped)
330 remove_black_surface(r_output);
334 /* try finding an output with a background and use that */
336 r_output = ivi_layout_find_bg_output(surf->ivi);
338 /* use the output of the bg to activate the app on start-up by
340 if (surf->view && r_output) {
341 if (app_id && r_output) {
342 weston_log("Surface with app_id %s, role %s activating by default\n",
343 weston_desktop_surface_get_app_id(surf->dsurface),
344 ivi_layout_get_surface_role_name(surf));
345 ivi_layout_activate(r_output, app_id);
346 surf->activated_by_default = true;
353 if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
354 if (policy && policy->api.surface_activate_by_default &&
355 !policy->api.surface_activate_by_default(surf, surf->ivi))
358 /* we can only activate it again by using the protocol, but
359 * additionally the output is not reset when
360 * ivi_layout_activate_complete() terminates so we use the
361 * current active surface to avoid hitting this again and again
363 if (surf->activated_by_default && output->active == surf)
367 weston_log("Surface with app_id %s, role %s activating by default\n",
368 weston_desktop_surface_get_app_id(surf->dsurface),
369 ivi_layout_get_surface_role_name(surf));
370 ivi_layout_activate(output, app_id);
371 surf->activated_by_default = true;
376 if (!weston_desktop_surface_get_maximized(dsurf) ||
377 geom.width != output->area.width ||
378 geom.height != output->area.height)
381 ivi_layout_activate_complete(output, surf);
385 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
387 struct ivi_compositor *ivi = surface->ivi;
389 struct weston_desktop_surface *dsurface = surface->dsurface;
390 struct weston_surface *wsurface =
391 weston_desktop_surface_get_surface(dsurface);
393 struct ivi_output *output = surface->split.output;
394 struct weston_output *woutput = output->output;
396 struct weston_view *view = surface->view;
397 struct weston_geometry geom;
399 if (surface->view->is_mapped)
402 geom = weston_desktop_surface_get_geometry(dsurface);
403 weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
404 geom.width, geom.height);
406 assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
408 weston_desktop_surface_set_fullscreen(dsurface, true);
410 weston_view_set_output(view, woutput);
411 weston_view_set_position(view, woutput->x, woutput->y);
412 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
414 weston_view_update_transform(view);
415 weston_view_damage_below(view);
417 wsurface->is_mapped = true;
418 surface->view->is_mapped = true;
422 ivi_layout_desktop_resize(struct ivi_surface *surface,
423 struct weston_geometry area)
425 struct weston_desktop_surface *dsurf = surface->dsurface;
426 struct weston_view *view = surface->view;
430 int width = area.width;
431 int height = area.height;
433 weston_desktop_surface_set_size(dsurf,
436 weston_view_set_position(view, x, y);
437 weston_view_update_transform(view);
438 weston_view_damage_below(view);
442 ivi_layout_split_committed(struct ivi_surface *surface)
444 struct ivi_compositor *ivi = surface->ivi;
446 struct weston_desktop_surface *dsurface = surface->dsurface;
447 struct weston_surface *wsurface =
448 weston_desktop_surface_get_surface(dsurface);
450 struct ivi_output *output = surface->split.output;
451 struct weston_output *woutput = output->output;
453 struct weston_view *view = surface->view;
454 struct weston_geometry geom;
462 if (surface->view->is_mapped)
465 geom = weston_desktop_surface_get_geometry(dsurface);
467 assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
468 surface->role == IVI_SURFACE_ROLE_SPLIT_V);
470 /* save the previous area in order to recover it back when if this kind
471 * of surface is being destroyed/removed */
472 output->area_saved = output->area;
474 switch (surface->role) {
475 case IVI_SURFACE_ROLE_SPLIT_V:
476 if (geom.width == woutput->width &&
477 geom.height == woutput->height)
478 geom.width = (output->area.width / 2);
480 x += woutput->width - geom.width;
481 output->area.width -= geom.width;
483 width = woutput->width - x;
484 height = output->area.height;
488 case IVI_SURFACE_ROLE_SPLIT_H:
489 if (geom.width == woutput->width &&
490 geom.height == woutput->height)
491 geom.height = (output->area.height / 2);
494 output->area.y += geom.height;
495 output->area.height -= geom.height;
497 width = output->area.width;
498 height = output->area.height;
504 assert(!"Invalid split orientation\n");
507 weston_desktop_surface_set_size(dsurface,
510 /* resize the active surface first, output->area already contains
511 * correct area to resize to */
513 ivi_layout_desktop_resize(output->active, output->area);
515 weston_view_set_output(view, woutput);
516 weston_view_set_position(view, x, y);
517 weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
519 weston_view_update_transform(view);
520 weston_view_damage_below(view);
522 wsurface->is_mapped = true;
523 surface->view->is_mapped = true;
527 ivi_layout_popup_committed(struct ivi_surface *surface)
529 struct ivi_compositor *ivi = surface->ivi;
531 struct weston_desktop_surface *dsurface = surface->dsurface;
532 struct weston_surface *wsurface =
533 weston_desktop_surface_get_surface(dsurface);
535 struct ivi_output *output = surface->popup.output;
536 struct weston_output *woutput = output->output;
538 struct weston_view *view = surface->view;
539 struct weston_geometry geom;
541 if (surface->view->is_mapped)
544 geom = weston_desktop_surface_get_geometry(dsurface);
545 weston_log("(popup) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
546 geom.width, geom.height);
548 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
550 weston_view_set_output(view, woutput);
551 if (surface->popup.x || surface->popup.y)
552 weston_view_set_position(view, surface->popup.x, surface->popup.y);
554 weston_view_set_position(view, geom.x, geom.y);
555 weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
557 weston_view_update_transform(view);
558 weston_view_damage_below(view);
560 wsurface->is_mapped = true;
561 surface->view->is_mapped = true;
565 ivi_layout_popup_re_add(struct ivi_surface *surface)
567 assert(surface->role == IVI_SURFACE_ROLE_POPUP);
568 struct weston_view *view = surface->view;
570 if (weston_view_is_mapped(view)) {
571 struct weston_desktop_surface *dsurface = surface->dsurface;
572 struct weston_surface *wsurface =
573 weston_desktop_surface_get_surface(dsurface);
575 weston_layer_entry_remove(&view->layer_link);
577 wsurface->is_mapped = false;
578 view->is_mapped = false;
581 ivi_layout_popup_committed(surface);
585 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
587 struct ivi_compositor *ivi = surf->ivi;
588 struct ivi_surface *is;
590 if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
591 surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
592 surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
595 wl_list_for_each(is, &ivi->surfaces, link)
603 ivi_layout_activate(struct ivi_output *output, const char *app_id)
605 struct ivi_compositor *ivi = output->ivi;
606 struct ivi_surface *surf;
607 struct weston_desktop_surface *dsurf;
608 struct weston_view *view;
609 struct weston_geometry geom;
610 struct ivi_policy *policy = output->ivi->policy;
612 surf = ivi_find_app(ivi, app_id);
616 if (policy && policy->api.surface_activate &&
617 !policy->api.surface_activate(surf, surf->ivi)) {
621 #ifdef AGL_COMP_DEBUG
622 weston_log("Activating app_id %s, type %s\n", app_id,
623 ivi_layout_get_surface_role_name(surf));
626 if (surf->role == IVI_SURFACE_ROLE_POPUP) {
627 ivi_layout_popup_re_add(surf);
631 /* do not 're'-activate surfaces that are split or active */
632 if (surf == output->active ||
633 ivi_layout_surface_is_split_or_fullscreen(surf))
637 dsurf = surf->dsurface;
639 geom = weston_desktop_surface_get_geometry(dsurf);
641 if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
642 surf->desktop.pending_output = output;
643 if (weston_desktop_surface_get_maximized(dsurf) &&
644 geom.width == output->area.width &&
645 geom.height == output->area.height) {
646 ivi_layout_activate_complete(output, surf);
650 weston_desktop_surface_set_maximized(dsurf, true);
651 weston_desktop_surface_set_size(dsurf,
653 output->area.height);
655 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
656 app_id, ivi_layout_get_surface_role_name(surf),
657 output->area.width, output->area.height);
659 * If the view isn't mapped, we put it onto the hidden layer so it will
660 * start receiving frame events, and will be able to act on our
663 if (!weston_view_is_mapped(view)) {
664 view->is_mapped = true;
665 view->surface->is_mapped = true;
667 weston_view_set_output(view, output->output);
668 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
669 /* force repaint of the entire output */
671 weston_log("Placed app_id %s, type %s in hidden layer\n",
672 app_id, ivi_layout_get_surface_role_name(surf));
673 weston_output_damage(output->output);
678 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
680 struct ivi_output *ivi_output = NULL;
682 switch (surf->role) {
683 case IVI_SURFACE_ROLE_DESKTOP:
684 if (surf->desktop.pending_output)
685 ivi_output = surf->desktop.pending_output;
687 ivi_output = surf->desktop.last_output;
689 case IVI_SURFACE_ROLE_POPUP:
690 ivi_output = surf->popup.output;
692 case IVI_SURFACE_ROLE_BACKGROUND:
693 ivi_output = surf->bg.output;
695 case IVI_SURFACE_ROLE_PANEL:
696 ivi_output = surf->panel.output;
698 case IVI_SURFACE_ROLE_FULLSCREEN:
699 ivi_output = surf->fullscreen.output;
701 case IVI_SURFACE_ROLE_SPLIT_H:
702 case IVI_SURFACE_ROLE_SPLIT_V:
703 ivi_output = surf->split.output;
705 case IVI_SURFACE_ROLE_REMOTE:
706 ivi_output = surf->remote.output;
708 case IVI_SURFACE_ROLE_NONE:
717 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
719 struct ivi_surface *surf;
720 struct ivi_output *ivi_output;
721 struct ivi_policy *policy = ivi->policy;
723 surf = ivi_find_app(ivi, app_id);
727 if (policy && policy->api.surface_deactivate &&
728 !policy->api.surface_deactivate(surf, surf->ivi)) {
732 ivi_output = ivi_layout_get_output_from_surface(surf);
733 weston_log("Deactiving %s, role %s\n", app_id,
734 ivi_layout_get_surface_role_name(surf));
736 if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
737 struct ivi_surface *previous_active;
739 previous_active = ivi_output->previous_active;
740 if (!previous_active) {
741 /* we don't have a previous active it means we should
743 if (ivi_output->active) {
744 struct weston_view *view;
746 view = ivi_output->active->view;
747 view->is_mapped = false;
748 view->surface->is_mapped = false;
750 weston_layer_entry_remove(&view->layer_link);
751 weston_output_damage(ivi_output->output);
754 struct weston_desktop_surface *dsurface;
755 const char *previous_active_app_id;
757 dsurface = previous_active->dsurface;
758 previous_active_app_id =
759 weston_desktop_surface_get_app_id(dsurface);
760 ivi_layout_activate(ivi_output, previous_active_app_id);
762 } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
763 struct weston_view *view = surf->view;
765 weston_layer_entry_remove(&view->layer_link);
766 weston_view_damage_below(view);