fa49163c850b1719803d6bb992ee6f6d88fd868f
[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         weston_log("(background) position view %p, x %d, y %d, on output %s\n", view,
81                         woutput->x, woutput->y, output->name);
82
83         view->is_mapped = true;
84         view->surface->is_mapped = true;
85
86         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
87 }
88
89 static void
90 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
91                struct ivi_surface *panel)
92 {
93         struct weston_output *woutput = output->output;
94         struct weston_desktop_surface *dsurface;
95         struct weston_view *view;
96         struct weston_geometry geom;
97         int x = woutput->x;
98         int y = woutput->y;
99
100         if (!panel)
101                 return;
102
103         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
104         dsurface = panel->dsurface;
105         view = panel->view;
106         geom = weston_desktop_surface_get_geometry(dsurface);
107
108         weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
109                         geom.width, geom.height, geom.x, geom.y);
110
111         switch (panel->panel.edge) {
112         case AGL_SHELL_EDGE_TOP:
113                 output->area.y += geom.height;
114                 output->area.height -= geom.height;
115                 break;
116         case AGL_SHELL_EDGE_BOTTOM:
117                 y += woutput->height - geom.height;
118                 output->area.height -= geom.height;
119                 break;
120         case AGL_SHELL_EDGE_LEFT:
121                 output->area.x += geom.width;
122                 output->area.width -= geom.width;
123                 break;
124         case AGL_SHELL_EDGE_RIGHT:
125                 x += woutput->width - geom.width;
126                 output->area.width -= geom.width;
127                 break;
128         }
129
130         x -= geom.x;
131         y -= geom.y;
132
133         weston_view_set_output(view, woutput);
134         weston_view_set_position(view, x, y);
135
136         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
137                         panel->panel.edge, view, x, y);
138
139         view->is_mapped = true;
140         view->surface->is_mapped = true;
141
142         weston_log("panel type %d inited on output %s\n", panel->panel.edge,
143                         output->name);
144
145         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
146 }
147
148 /*
149  * Initializes all static parts of the layout, i.e. the background and panels.
150  */
151 void
152 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
153 {
154         ivi_background_init(ivi, output);
155
156         output->area.x = 0;
157         output->area.y = 0;
158         output->area.width = output->output->width;
159         output->area.height = output->output->height;
160
161         ivi_panel_init(ivi, output, output->top);
162         ivi_panel_init(ivi, output, output->bottom);
163         ivi_panel_init(ivi, output, output->left);
164         ivi_panel_init(ivi, output, output->right);
165
166         weston_compositor_schedule_repaint(ivi->compositor);
167
168         weston_log("Usable area: %dx%d+%d,%d\n",
169                    output->area.width, output->area.height,
170                    output->area.x, output->area.y);
171 }
172
173 struct ivi_surface *
174 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
175 {
176         struct ivi_surface *surf;
177         const char *id;
178
179         wl_list_for_each(surf, &ivi->surfaces, link) {
180                 id = weston_desktop_surface_get_app_id(surf->dsurface);
181                 if (id && strcmp(app_id, id) == 0)
182                         return surf;
183         }
184
185         return NULL;
186 }
187
188 static void
189 ivi_layout_activate_complete(struct ivi_output *output,
190                              struct ivi_surface *surf)
191 {
192         struct ivi_compositor *ivi = output->ivi;
193         struct weston_output *woutput = output->output;
194         struct weston_view *view = surf->view;
195
196         if (weston_view_is_mapped(view)) {
197                 weston_layer_entry_remove(&view->layer_link);
198         }
199
200         weston_view_set_output(view, woutput);
201         weston_view_set_position(view,
202                                  woutput->x + output->area.x,
203                                  woutput->y + output->area.y);
204
205         view->is_mapped = true;
206         view->surface->is_mapped = true;
207
208         if (output->active) {
209                 output->active->view->is_mapped = false;
210                 output->active->view->surface->is_mapped = false;
211
212                 weston_layer_entry_remove(&output->active->view->layer_link);
213         }
214         output->previous_active = output->active;
215         output->active = surf;
216
217         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
218         weston_view_update_transform(view);
219
220         /* force repaint of the entire output */
221         weston_output_damage(output->output);
222
223         /*
224          * the 'remote' role now makes use of this part so make sure we don't
225          * trip the enum such that we might end up with a modified output for
226          * 'remote' role
227          */
228         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
229                 if (surf->desktop.pending_output)
230                         surf->desktop.last_output = surf->desktop.pending_output;
231                 surf->desktop.pending_output = NULL;
232         }
233
234         weston_log("Activation completed for app_id %s, role %s, output %s\n",
235                         weston_desktop_surface_get_app_id(surf->dsurface),
236                         ivi_layout_get_surface_role_name(surf), output->name);
237 }
238
239 struct ivi_output *
240 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
241 {
242         struct ivi_output *out;
243
244         wl_list_for_each(out, &ivi->outputs, link) {
245                 if (!out->app_id)
246                         continue;
247
248                 if (!strcmp(app_id, out->app_id))
249                         return out;
250         }
251
252         return NULL;
253 }
254
255
256 static struct ivi_output *
257 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
258 {
259         struct ivi_output *out;
260
261         wl_list_for_each(out, &ivi->outputs, link) {
262                 if (out->background &&
263                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
264                         return out;
265         }
266
267         return NULL;
268 }
269
270 void
271 ivi_layout_desktop_committed(struct ivi_surface *surf)
272 {
273         struct weston_desktop_surface *dsurf = surf->dsurface;
274         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
275         struct ivi_policy *policy = surf->ivi->policy;
276         struct ivi_output *output;
277         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
278
279         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
280                surf->role == IVI_SURFACE_ROLE_REMOTE);
281
282         /*
283          * we can't make use here of the ivi_layout_get_output_from_surface()
284          * due to the fact that we'll always land here when a surface performs
285          * a commit and pending_output will not bet set. This works in tandem
286          * with 'activated_by_default' at this point to avoid tripping over
287          * to a surface that continuously updates its content
288          */
289         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
290                 output = surf->desktop.pending_output;
291         else
292                 output = surf->remote.output;
293
294         if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
295                 struct ivi_output *r_output;
296
297                 if (policy && policy->api.surface_activate_by_default &&
298                     !policy->api.surface_activate_by_default(surf, surf->ivi))
299                         return;
300
301                 /* we can only activate it again by using the protocol */
302                 if (surf->activated_by_default)
303                         return;
304
305                 /* check first if there aren't any outputs being set */
306                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
307
308                 if (r_output) {
309                         struct weston_view *view = r_output->fullscreen_view.fs->view;
310                         if (view->is_mapped || view->surface->is_mapped)
311                                 remove_black_surface(r_output);
312                 }
313
314
315                 /* try finding an output with a background and use that */
316                 if (!r_output)
317                         r_output = ivi_layout_find_bg_output(surf->ivi);
318
319                 /* use the output of the bg to activate the app on start-up by
320                  * default */
321                 if (surf->view && r_output) {
322                         if (app_id && r_output) {
323                                 weston_log("Surface with app_id %s, role %s activating by default\n",
324                                         weston_desktop_surface_get_app_id(surf->dsurface),
325                                         ivi_layout_get_surface_role_name(surf));
326                                 ivi_layout_activate(r_output, app_id);
327                                 surf->activated_by_default = true;
328                         }
329                 }
330
331                 return;
332         }
333
334         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
335                 if (policy && policy->api.surface_activate_by_default &&
336                     !policy->api.surface_activate_by_default(surf, surf->ivi))
337                         return;
338
339                 /* we can only activate it again by using the protocol, but
340                  * additionally the output is not reset when
341                  * ivi_layout_activate_complete() terminates so we use the
342                  * current active surface to avoid hitting this again and again
343                  * */
344                 if (surf->activated_by_default && output->active == surf)
345                         return;
346
347                 if (app_id) {
348                         weston_log("Surface with app_id %s, role %s activating by default\n",
349                                         weston_desktop_surface_get_app_id(surf->dsurface),
350                                         ivi_layout_get_surface_role_name(surf));
351                         ivi_layout_activate(output, app_id);
352                         surf->activated_by_default = true;
353                 }
354                 return;
355         }
356
357         if (!weston_desktop_surface_get_maximized(dsurf) ||
358             geom.width != output->area.width ||
359             geom.height != output->area.height)
360                 return;
361
362         ivi_layout_activate_complete(output, surf);
363 }
364
365 void
366 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
367 {
368         struct ivi_compositor *ivi = surface->ivi;
369         struct ivi_policy *policy = ivi->policy;
370
371         struct weston_desktop_surface *dsurface = surface->dsurface;
372         struct weston_surface *wsurface =
373                 weston_desktop_surface_get_surface(dsurface);
374         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
375
376         struct ivi_output *output = surface->split.output;
377         struct weston_output *woutput = output->output;
378
379         struct weston_view *view = surface->view;
380         struct weston_geometry geom;
381
382         if (policy && policy->api.surface_activate_by_default &&
383             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
384             !surface->activated_by_default)
385                 return;
386
387         if (surface->view->is_mapped)
388                 return;
389
390         geom = weston_desktop_surface_get_geometry(dsurface);
391         weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
392                         geom.width, geom.height);
393
394         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
395
396         weston_desktop_surface_set_fullscreen(dsurface, true);
397
398         weston_view_set_output(view, woutput);
399         weston_view_set_position(view, woutput->x, woutput->y);
400         weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
401
402         weston_view_update_transform(view);
403         weston_view_damage_below(view);
404
405         wsurface->is_mapped = true;
406         surface->view->is_mapped = true;
407
408         shell_advertise_app_state(ivi, app_id,
409                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
410
411         weston_log("Activation completed for app_id %s, role %s, output %s\n",
412                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
413 }
414
415 void
416 ivi_layout_desktop_resize(struct ivi_surface *surface,
417                           struct weston_geometry area)
418 {
419         struct weston_desktop_surface *dsurf = surface->dsurface;
420         struct weston_view *view = surface->view;
421
422         int x = area.x;
423         int y = area.y;
424         int width = area.width;
425         int height = area.height;
426
427         weston_desktop_surface_set_size(dsurf,
428                                         width, height);
429
430         weston_view_set_position(view, x, y);
431         weston_view_update_transform(view);
432         weston_view_damage_below(view);
433 }
434
435 void
436 ivi_layout_split_committed(struct ivi_surface *surface)
437 {
438         struct ivi_compositor *ivi = surface->ivi;
439         struct ivi_policy *policy = ivi->policy;
440
441         struct weston_desktop_surface *dsurface = surface->dsurface;
442         struct weston_surface *wsurface =
443                 weston_desktop_surface_get_surface(dsurface);
444         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
445
446         struct ivi_output *output = surface->split.output;
447         struct weston_output *woutput = output->output;
448
449         struct weston_view *view = surface->view;
450         struct weston_geometry geom;
451
452         int x, y;
453         int width, height;
454
455         x = woutput->x;
456         y = woutput->y;
457
458         if (policy && policy->api.surface_activate_by_default &&
459             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
460             !surface->activated_by_default)
461                 return;
462
463         if (surface->view->is_mapped)
464                 return;
465
466         geom = weston_desktop_surface_get_geometry(dsurface);
467
468         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
469                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
470
471         /* save the previous area in order to recover it back when if this kind
472          * of surface is being destroyed/removed */
473         output->area_saved = output->area;
474
475         switch (surface->role) {
476         case IVI_SURFACE_ROLE_SPLIT_V:
477                 if (geom.width == woutput->width &&
478                     geom.height == woutput->height)
479                         geom.width = (output->area.width / 2);
480
481                 x += woutput->width - geom.width;
482                 output->area.width -= geom.width;
483
484                 width = woutput->width - x;
485                 height = output->area.height;
486                 y = output->area.y;
487
488                 break;
489         case IVI_SURFACE_ROLE_SPLIT_H:
490                 if (geom.width == woutput->width &&
491                     geom.height == woutput->height)
492                         geom.height = (output->area.height / 2);
493
494                 y = output->area.y;
495                 output->area.y += geom.height;
496                 output->area.height -= geom.height;
497
498                 width = output->area.width;
499                 height = output->area.height;
500
501                 x = output->area.x;
502
503                 break;
504         default:
505                 assert(!"Invalid split orientation\n");
506         }
507
508         weston_desktop_surface_set_size(dsurface,
509                                         width, height);
510
511         /* resize the active surface first, output->area already contains
512          * correct area to resize to */
513         if (output->active)
514                 ivi_layout_desktop_resize(output->active, output->area);
515
516         weston_view_set_output(view, woutput);
517         weston_view_set_position(view, x, y);
518         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
519
520         weston_view_update_transform(view);
521         weston_view_damage_below(view);
522
523         wsurface->is_mapped = true;
524         surface->view->is_mapped = true;
525
526         shell_advertise_app_state(ivi, app_id,
527                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
528
529         weston_log("Activation completed for app_id %s, role %s, output %s\n",
530                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
531 }
532
533 void
534 ivi_layout_popup_committed(struct ivi_surface *surface)
535 {
536         struct ivi_compositor *ivi = surface->ivi;
537         struct ivi_policy *policy = ivi->policy;
538
539         struct weston_desktop_surface *dsurface = surface->dsurface;
540         struct weston_surface *wsurface =
541                 weston_desktop_surface_get_surface(dsurface);
542         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
543
544         struct ivi_output *output = surface->popup.output;
545         struct weston_output *woutput = output->output;
546
547         struct weston_view *view = surface->view;
548
549         if (policy && policy->api.surface_activate_by_default &&
550             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
551             !surface->activated_by_default)
552                 return;
553
554         if (surface->view->is_mapped)
555                 return;
556
557         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
558
559         weston_view_set_output(view, woutput);
560         weston_view_set_position(view, surface->popup.x, surface->popup.y);
561
562         /* only clip the pop-up dialog window if we have a valid
563          * width and height being passed on. Users might not want to have one
564          * set-up so only enfore it is really passed on. */
565         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
566                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
567                                      surface->popup.bb.width, surface->popup.bb.height);
568
569         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
570
571         weston_view_update_transform(view);
572         weston_view_damage_below(view);
573
574         wsurface->is_mapped = true;
575         surface->view->is_mapped = true;
576
577         shell_advertise_app_state(ivi, app_id,
578                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
579
580         weston_log("Activation completed for app_id %s, role %s, output %s\n",
581                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
582 }
583
584 static void
585 ivi_layout_popup_re_add(struct ivi_surface *surface)
586 {
587         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
588         struct weston_view *view = surface->view;
589
590         if (weston_view_is_mapped(view)) {
591                 struct weston_desktop_surface *dsurface = surface->dsurface;
592                 struct weston_surface *wsurface =
593                         weston_desktop_surface_get_surface(dsurface);
594
595                 weston_layer_entry_remove(&view->layer_link);
596
597                 wsurface->is_mapped = false;
598                 view->is_mapped = false;
599         }
600
601         /* reset the activate by default in order to (still) allow the surface
602          * to be activaved using the request */
603         if (!surface->activated_by_default)
604                 surface->activated_by_default = true;
605
606         ivi_layout_popup_committed(surface);
607 }
608
609 static bool
610 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
611 {
612         struct ivi_compositor *ivi = surf->ivi;
613         struct ivi_surface *is;
614
615         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
616             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
617             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
618                 return false;
619
620         /* reset the activate by default in order to (still) allow the surface
621          * to be activaved using the request */
622         if (!surf->activated_by_default)
623                 surf->activated_by_default = true;
624
625         wl_list_for_each(is, &ivi->surfaces, link)
626                 if (is == surf)
627                         return true;
628
629         return false;
630 }
631
632 void
633 ivi_layout_activate(struct ivi_output *output, const char *app_id)
634 {
635         struct ivi_compositor *ivi = output->ivi;
636         struct ivi_surface *surf;
637         struct weston_desktop_surface *dsurf;
638         struct weston_view *view;
639         struct weston_geometry geom;
640         struct ivi_policy *policy = output->ivi->policy;
641
642         surf = ivi_find_app(ivi, app_id);
643         if (!surf)
644                 return;
645
646         if (policy && policy->api.surface_activate &&
647             !policy->api.surface_activate(surf, surf->ivi)) {
648                 return;
649         }
650
651 #ifdef AGL_COMP_DEBUG
652         weston_log("Activating app_id %s, type %s\n", app_id,
653                         ivi_layout_get_surface_role_name(surf));
654 #endif
655
656         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
657                 ivi_layout_popup_re_add(surf);
658                 return;
659         }
660
661         /* do not 're'-activate surfaces that are split or active */
662         if (surf == output->active ||
663             ivi_layout_surface_is_split_or_fullscreen(surf))
664                 return;
665
666         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
667                 struct ivi_output *remote_output =
668                         ivi_layout_find_with_app_id(app_id, ivi);
669
670                 /* if already active on a remote output do not
671                  * attempt to activate it again */
672                 if (remote_output && remote_output->active == surf)
673                         return;
674         }
675
676
677         dsurf = surf->dsurface;
678         view = surf->view;
679         geom = weston_desktop_surface_get_geometry(dsurf);
680
681         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
682                 surf->desktop.pending_output = output;
683         if (weston_desktop_surface_get_maximized(dsurf) &&
684             geom.width == output->area.width &&
685             geom.height == output->area.height) {
686                 ivi_layout_activate_complete(output, surf);
687                 return;
688         }
689
690         weston_desktop_surface_set_maximized(dsurf, true);
691         weston_desktop_surface_set_size(dsurf,
692                                         output->area.width,
693                                         output->area.height);
694
695         weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
696                         app_id, ivi_layout_get_surface_role_name(surf),
697                         output->area.width, output->area.height);
698         /*
699          * If the view isn't mapped, we put it onto the hidden layer so it will
700          * start receiving frame events, and will be able to act on our
701          * configure event.
702          */
703         if (!weston_view_is_mapped(view)) {
704                 view->is_mapped = true;
705                 view->surface->is_mapped = true;
706
707                 weston_view_set_output(view, output->output);
708                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
709                 /* force repaint of the entire output */
710
711                 weston_log("Placed app_id %s, type %s in hidden layer\n",
712                                 app_id, ivi_layout_get_surface_role_name(surf));
713                 weston_output_damage(output->output);
714         }
715 }
716
717 struct ivi_output *
718 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
719 {
720         struct ivi_output *ivi_output = NULL;
721
722         switch (surf->role) {
723         case IVI_SURFACE_ROLE_DESKTOP:
724                 if (surf->desktop.pending_output)
725                         ivi_output = surf->desktop.pending_output;
726                 else
727                         ivi_output = surf->desktop.last_output;
728                 break;
729         case IVI_SURFACE_ROLE_POPUP:
730                 ivi_output = surf->popup.output;
731                 break;
732         case IVI_SURFACE_ROLE_BACKGROUND:
733                 ivi_output = surf->bg.output;
734                 break;
735         case IVI_SURFACE_ROLE_PANEL:
736                 ivi_output = surf->panel.output;
737                 break;
738         case IVI_SURFACE_ROLE_FULLSCREEN:
739                 ivi_output = surf->fullscreen.output;
740                 break;
741         case IVI_SURFACE_ROLE_SPLIT_H:
742         case IVI_SURFACE_ROLE_SPLIT_V:
743                 ivi_output = surf->split.output;
744                 break;
745         case IVI_SURFACE_ROLE_REMOTE:
746                 ivi_output = surf->remote.output;
747                 break;
748         case IVI_SURFACE_ROLE_NONE:
749         default:
750                 break;
751         }
752
753         return ivi_output;
754 }
755
756 void
757 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
758 {
759         struct ivi_surface *surf;
760         struct ivi_output *ivi_output;
761         struct ivi_policy *policy = ivi->policy;
762
763         surf = ivi_find_app(ivi, app_id);
764         if (!surf)
765                 return;
766
767         if (policy && policy->api.surface_deactivate &&
768             !policy->api.surface_deactivate(surf, surf->ivi)) {
769                 return;
770         }
771
772         ivi_output = ivi_layout_get_output_from_surface(surf);
773         weston_log("Deactiving %s, role %s\n", app_id,
774                         ivi_layout_get_surface_role_name(surf));
775
776         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
777                 struct ivi_surface *previous_active;
778
779                 previous_active = ivi_output->previous_active;
780                 if (!previous_active) {
781                         /* we don't have a previous active it means we should
782                          * display the bg */
783                         if (ivi_output->active) {
784                                 struct weston_view *view;
785
786                                 view = ivi_output->active->view;
787                                 view->is_mapped = false;
788                                 view->surface->is_mapped = false;
789
790                                 weston_layer_entry_remove(&view->layer_link);
791                                 weston_output_damage(ivi_output->output);
792                         }
793                 } else {
794                         struct weston_desktop_surface *dsurface;
795                         const char *previous_active_app_id;
796
797                         dsurface = previous_active->dsurface;
798                         previous_active_app_id =
799                                 weston_desktop_surface_get_app_id(dsurface);
800                         ivi_layout_activate(ivi_output, previous_active_app_id);
801                 }
802         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
803                 struct weston_view *view  = surf->view;
804
805                 weston_layer_entry_remove(&view->layer_link);
806                 weston_view_damage_below(view);
807         }
808 }