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