layout: Add a wrapper to print out the surface role
[src/agl-compositor.git] / src / layout.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
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:
11  *
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.
15  *
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
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28 #include "shared/helpers.h"
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include <libweston/libweston.h>
34 #include <libweston-desktop/libweston-desktop.h>
35
36 #include "agl-shell-desktop-server-protocol.h"
37
38 #define AGL_COMP_DEBUG
39
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",
50 };
51
52 const char *
53 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
54 {
55         if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
56                 return " unknown surface role";
57
58         return ivi_roles_as_string[surf->role];
59 }
60
61 static void
62 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
63 {
64         struct weston_output *woutput = output->output;
65         struct ivi_surface *bg = output->background;
66         struct weston_view *view;
67
68         if (!bg) {
69                 weston_log("WARNING: Output does not have a background\n");
70                 return;
71         }
72
73         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
74
75         view = bg->view;
76
77         weston_view_set_output(view, woutput);
78         weston_view_set_position(view, woutput->x, woutput->y);
79
80 #ifdef AGL_COMP_DEBUG
81         weston_log("(background) position view %p, x %d, y %d\n", view,
82                         woutput->x, woutput->y);
83 #endif
84
85         view->is_mapped = true;
86         view->surface->is_mapped = true;
87
88         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
89 }
90
91 static void
92 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
93                struct ivi_surface *panel)
94 {
95         struct weston_output *woutput = output->output;
96         struct weston_desktop_surface *dsurface;
97         struct weston_view *view;
98         struct weston_geometry geom;
99         int x = woutput->x;
100         int y = woutput->y;
101
102         if (!panel)
103                 return;
104
105         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
106         dsurface = panel->dsurface;
107         view = panel->view;
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);
112 #endif
113         switch (panel->panel.edge) {
114         case AGL_SHELL_EDGE_TOP:
115                 output->area.y += geom.height;
116                 output->area.height -= geom.height;
117                 break;
118         case AGL_SHELL_EDGE_BOTTOM:
119                 y += woutput->height - geom.height;
120                 output->area.height -= geom.height;
121                 break;
122         case AGL_SHELL_EDGE_LEFT:
123                 output->area.x += geom.width;
124                 output->area.width -= geom.width;
125                 break;
126         case AGL_SHELL_EDGE_RIGHT:
127                 x += woutput->width - geom.width;
128                 output->area.width -= geom.width;
129                 break;
130         }
131
132         x -= geom.x;
133         y -= geom.y;
134
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);
140 #endif
141
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);
147
148                 view->is_mapped = false;
149                 view->surface->is_mapped = false;
150         }
151
152         /* give ivi_layout_panel_committed() a chance to map the view/surface
153          * instead */
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)
157                 return;
158
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);
163 #endif
164         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
165 }
166
167 /*
168  * Initializes all static parts of the layout, i.e. the background and panels.
169  */
170 void
171 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
172 {
173         ivi_background_init(ivi, output);
174
175         output->area.x = 0;
176         output->area.y = 0;
177         output->area.width = output->output->width;
178         output->area.height = output->output->height;
179
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);
184
185         weston_compositor_schedule_repaint(ivi->compositor);
186
187         weston_log("Usable area: %dx%d+%d,%d\n",
188                    output->area.width, output->area.height,
189                    output->area.x, output->area.y);
190 }
191
192 struct ivi_surface *
193 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
194 {
195         struct ivi_surface *surf;
196         const char *id;
197
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)
201                         return surf;
202         }
203
204         return NULL;
205 }
206
207 static void
208 ivi_layout_activate_complete(struct ivi_output *output,
209                              struct ivi_surface *surf)
210 {
211         struct ivi_compositor *ivi = output->ivi;
212         struct weston_output *woutput = output->output;
213         struct weston_view *view = surf->view;
214
215         if (weston_view_is_mapped(view)) {
216                 weston_layer_entry_remove(&view->layer_link);
217         }
218
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);
223
224         view->is_mapped = true;
225         view->surface->is_mapped = true;
226
227         if (output->active) {
228                 output->active->view->is_mapped = false;
229                 output->active->view->surface->is_mapped = false;
230
231                 weston_layer_entry_remove(&output->active->view->layer_link);
232         }
233         output->previous_active = output->active;
234         output->active = surf;
235
236         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
237         weston_view_update_transform(view);
238
239         /* force repaint of the entire output */
240         weston_output_damage(output->output);
241
242         /*
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
245          * 'remote' role
246          */
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;
251         }
252
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));
256 }
257
258 static struct ivi_output *
259 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
260 {
261         struct ivi_output *out;
262
263         wl_list_for_each(out, &ivi->outputs, link) {
264                 if (out->background &&
265                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
266                         return out;
267         }
268
269         return NULL;
270 }
271
272 void
273 ivi_layout_desktop_committed(struct ivi_surface *surf)
274 {
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;
279
280         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
281                surf->role == IVI_SURFACE_ROLE_REMOTE);
282
283         /*
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
289          */
290         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
291                 output = surf->desktop.pending_output;
292         else
293                 output = surf->remote.output;
294
295         if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
296                 struct ivi_output *ivi_bg_output;
297
298                 if (policy && policy->api.surface_activate_by_default &&
299                     !policy->api.surface_activate_by_default(surf, surf->ivi))
300                         return;
301
302                 /* we can only activate it again by using the protocol */
303                 if (surf->activated_by_default)
304                         return;
305
306                 ivi_bg_output = ivi_layout_find_bg_output(surf->ivi);
307
308                 /* use the output of the bg to activate the app on start-up by
309                  * default */
310                 if (surf->view && ivi_bg_output) {
311                         const char *app_id =
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;
319                         }
320                 }
321
322                 return;
323         }
324
325         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
326                 const char *app_id;
327                 if (policy && policy->api.surface_activate_by_default &&
328                     !policy->api.surface_activate_by_default(surf, surf->ivi))
329                         return;
330
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
335                  * */
336                 if (surf->activated_by_default && output->active == surf)
337                         return;
338
339                 app_id = weston_desktop_surface_get_app_id(dsurf);
340                 if (app_id) {
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;
346                 }
347                 return;
348         }
349
350         if (!weston_desktop_surface_get_maximized(dsurf) ||
351             geom.width != output->area.width ||
352             geom.height != output->area.height)
353                 return;
354
355         ivi_layout_activate_complete(output, surf);
356 }
357
358 void
359 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
360 {
361         struct ivi_compositor *ivi = surface->ivi;
362
363         struct weston_desktop_surface *dsurface = surface->dsurface;
364         struct weston_surface *wsurface =
365                 weston_desktop_surface_get_surface(dsurface);
366
367         struct ivi_output *output = surface->split.output;
368         struct weston_output *woutput = output->output;
369
370         struct weston_view *view = surface->view;
371         struct weston_geometry geom;
372
373         if (surface->view->is_mapped)
374                 return;
375
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);
379
380         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
381
382         weston_desktop_surface_set_fullscreen(dsurface, true);
383
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);
387
388         weston_view_update_transform(view);
389         weston_view_damage_below(view);
390
391         wsurface->is_mapped = true;
392         surface->view->is_mapped = true;
393 }
394
395 void
396 ivi_layout_desktop_resize(struct ivi_surface *surface,
397                           struct weston_geometry area)
398 {
399         struct weston_desktop_surface *dsurf = surface->dsurface;
400         struct weston_view *view = surface->view;
401
402         int x = area.x;
403         int y = area.y;
404         int width = area.width;
405         int height = area.height;
406
407         weston_desktop_surface_set_size(dsurf,
408                                         width, height);
409
410         weston_view_set_position(view, x, y);
411         weston_view_update_transform(view);
412         weston_view_damage_below(view);
413 }
414
415 void
416 ivi_layout_split_committed(struct ivi_surface *surface)
417 {
418         struct ivi_compositor *ivi = surface->ivi;
419
420         struct weston_desktop_surface *dsurface = surface->dsurface;
421         struct weston_surface *wsurface =
422                 weston_desktop_surface_get_surface(dsurface);
423
424         struct ivi_output *output = surface->split.output;
425         struct weston_output *woutput = output->output;
426
427         struct weston_view *view = surface->view;
428         struct weston_geometry geom;
429
430         int x, y;
431         int width, height;
432
433         x = woutput->x;
434         y = woutput->y;
435
436         if (surface->view->is_mapped)
437                 return;
438
439         geom = weston_desktop_surface_get_geometry(dsurface);
440
441         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
442                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
443
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;
447
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);
453
454                 x += woutput->width - geom.width;
455                 output->area.width -= geom.width;
456
457                 width = woutput->width - x;
458                 height = output->area.height;
459                 y = output->area.y;
460
461                 break;
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);
466
467                 y = output->area.y;
468                 output->area.y += geom.height;
469                 output->area.height -= geom.height;
470
471                 width = output->area.width;
472                 height = output->area.height;
473
474                 x = output->area.x;
475
476                 break;
477         default:
478                 assert(!"Invalid split orientation\n");
479         }
480
481         weston_desktop_surface_set_size(dsurface,
482                                         width, height);
483
484         /* resize the active surface first, output->area already contains
485          * correct area to resize to */
486         if (output->active)
487                 ivi_layout_desktop_resize(output->active, output->area);
488
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);
492
493         weston_view_update_transform(view);
494         weston_view_damage_below(view);
495
496         wsurface->is_mapped = true;
497         surface->view->is_mapped = true;
498 }
499
500 void
501 ivi_layout_popup_committed(struct ivi_surface *surface)
502 {
503         struct ivi_compositor *ivi = surface->ivi;
504
505         struct weston_desktop_surface *dsurface = surface->dsurface;
506         struct weston_surface *wsurface =
507                 weston_desktop_surface_get_surface(dsurface);
508
509         struct ivi_output *output = surface->popup.output;
510         struct weston_output *woutput = output->output;
511
512         struct weston_view *view = surface->view;
513         struct weston_geometry geom;
514
515         if (surface->view->is_mapped)
516                 return;
517
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);
521
522         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
523
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);
527         else
528                 weston_view_set_position(view, geom.x, geom.y);
529         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
530
531         weston_view_update_transform(view);
532         weston_view_damage_below(view);
533
534         wsurface->is_mapped = true;
535         surface->view->is_mapped = true;
536 }
537
538 static void
539 ivi_layout_popup_re_add(struct ivi_surface *surface)
540 {
541         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
542         struct weston_view *view = surface->view;
543
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);
548
549                 weston_layer_entry_remove(&view->layer_link);
550
551                 wsurface->is_mapped = false;
552                 view->is_mapped = false;
553         }
554
555         ivi_layout_popup_committed(surface);
556 }
557
558 void
559 ivi_layout_panel_committed(struct ivi_surface *surface)
560 {
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;
568         int x = woutput->x;
569         int y = woutput->y;
570
571         assert(surface->role == IVI_SURFACE_ROLE_PANEL);
572
573         /*
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
579          * displayed at all.
580          *
581          * Later versions of qtwayland do have the correct window geometry for
582          * the desktop surface so the weston_surface is already mapped in
583          * ivi_panel_init().
584          */
585         if (wsurface->is_mapped)
586                 return;
587
588         geom = weston_desktop_surface_get_geometry(dsurface);
589
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);
593 #endif
594
595         switch (surface->panel.edge) {
596         case AGL_SHELL_EDGE_TOP:
597                 /* Do nothing */
598                 break;
599         case AGL_SHELL_EDGE_BOTTOM:
600                 y += woutput->height - geom.height;
601                 break;
602         case AGL_SHELL_EDGE_LEFT:
603                 /* Do nothing */
604                 break;
605         case AGL_SHELL_EDGE_RIGHT:
606                 x += woutput->width - geom.width;
607                 break;
608         }
609 #ifndef AGL_COMP_DEBUG
610         weston_log("panel type %d commited\n", surface->panel.edge);
611 #endif
612
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);
617
618         weston_view_update_transform(surface->view);
619         weston_view_schedule_repaint(surface->view);
620
621         wsurface->is_mapped = true;
622         surface->view->is_mapped = true;
623 }
624
625 static bool
626 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
627 {
628         struct ivi_compositor *ivi = surf->ivi;
629         struct ivi_surface *is;
630
631         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
632             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
633             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
634                 return false;
635
636         wl_list_for_each(is, &ivi->surfaces, link)
637                 if (is == surf)
638                         return true;
639
640         return false;
641 }
642
643 void
644 ivi_layout_activate(struct ivi_output *output, const char *app_id)
645 {
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;
652
653         surf = ivi_find_app(ivi, app_id);
654         if (!surf)
655                 return;
656
657         if (policy && policy->api.surface_activate &&
658             !policy->api.surface_activate(surf, surf->ivi)) {
659                 return;
660         }
661
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));
665 #endif
666
667         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
668                 ivi_layout_popup_re_add(surf);
669                 return;
670         }
671
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))
675                 return;
676
677
678         dsurf = surf->dsurface;
679         view = surf->view;
680         geom = weston_desktop_surface_get_geometry(dsurf);
681
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);
688                 return;
689         }
690
691         weston_desktop_surface_set_maximized(dsurf, true);
692         weston_desktop_surface_set_size(dsurf,
693                                         output->area.width,
694                                         output->area.height);
695
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);
699         /*
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
702          * configure event.
703          */
704         if (!weston_view_is_mapped(view)) {
705                 view->is_mapped = true;
706                 view->surface->is_mapped = true;
707
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 */
711
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);
715         }
716 }
717
718 struct ivi_output *
719 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
720 {
721         struct ivi_output *ivi_output = NULL;
722
723         switch (surf->role) {
724         case IVI_SURFACE_ROLE_DESKTOP:
725                 if (surf->desktop.pending_output)
726                         ivi_output = surf->desktop.pending_output;
727                 else
728                         ivi_output = surf->desktop.last_output;
729                 break;
730         case IVI_SURFACE_ROLE_POPUP:
731                 ivi_output = surf->popup.output;
732                 break;
733         case IVI_SURFACE_ROLE_BACKGROUND:
734                 ivi_output = surf->bg.output;
735                 break;
736         case IVI_SURFACE_ROLE_PANEL:
737                 ivi_output = surf->panel.output;
738                 break;
739         case IVI_SURFACE_ROLE_FULLSCREEN:
740                 ivi_output = surf->fullscreen.output;
741                 break;
742         case IVI_SURFACE_ROLE_SPLIT_H:
743         case IVI_SURFACE_ROLE_SPLIT_V:
744                 ivi_output = surf->split.output;
745                 break;
746         case IVI_SURFACE_ROLE_REMOTE:
747                 ivi_output = surf->remote.output;
748                 break;
749         case IVI_SURFACE_ROLE_NONE:
750         default:
751                 break;
752         }
753
754         return ivi_output;
755 }
756
757 void
758 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
759 {
760         struct ivi_surface *surf;
761         struct ivi_output *ivi_output;
762         struct ivi_policy *policy = ivi->policy;
763
764         surf = ivi_find_app(ivi, app_id);
765         if (!surf)
766                 return;
767
768         if (policy && policy->api.surface_deactivate &&
769             !policy->api.surface_deactivate(surf, surf->ivi)) {
770                 return;
771         }
772
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));
776
777         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
778                 struct ivi_surface *previous_active;
779
780                 previous_active = ivi_output->previous_active;
781                 if (!previous_active) {
782                         /* we don't have a previous active it means we should
783                          * display the bg */
784                         if (ivi_output->active) {
785                                 struct weston_view *view;
786
787                                 view = ivi_output->active->view;
788                                 view->is_mapped = false;
789                                 view->surface->is_mapped = false;
790
791                                 weston_layer_entry_remove(&view->layer_link);
792                                 weston_output_damage(ivi_output->output);
793                         }
794                 } else {
795                         struct weston_desktop_surface *dsurface;
796                         const char *previous_active_app_id;
797
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);
802                 }
803         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
804                 struct weston_view *view  = surf->view;
805
806                 weston_layer_entry_remove(&view->layer_link);
807                 weston_view_damage_below(view);
808         }
809 }