layout: Fix panel initialization with no weston surface
[src/agl-compositor.git] / src / layout.c
1 /*
2  * Copyright © 2019, 2024 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/config-parser.h>
34 #include <libweston/libweston.h>
35 #include <libweston/desktop.h>
36
37 #include "agl-shell-desktop-server-protocol.h"
38
39 #define AGL_COMP_DEBUG
40
41 static const char *ivi_roles_as_string[] = {
42         [IVI_SURFACE_ROLE_NONE]         = "NONE",
43         [IVI_SURFACE_ROLE_BACKGROUND]   = "BACKGROUND",
44         [IVI_SURFACE_ROLE_PANEL]        = "PANEL",
45         [IVI_SURFACE_ROLE_DESKTOP]      = "DESKTOP",
46         [IVI_SURFACE_ROLE_POPUP]        = "POPUP",
47         [IVI_SURFACE_ROLE_SPLIT_H]      = "SPLIT_H",
48         [IVI_SURFACE_ROLE_SPLIT_V]      = "SPLIT_V",
49         [IVI_SURFACE_ROLE_FULLSCREEN]   = "FULLSCREEN",
50         [IVI_SURFACE_ROLE_REMOTE]       = "REMOTE",
51         [IVI_SURFACE_ROLE_TILE]         = "SPLIT",
52 };
53
54 bool
55 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface);
56
57 void
58 ivi_layout_save(struct ivi_compositor *ivi, struct ivi_output *output)
59 {
60         struct ivi_output *new_output;
61         ivi->need_ivi_output_relayout = true;
62
63         new_output = zalloc(sizeof(*new_output));
64
65         new_output->ivi = ivi;
66         new_output->background = output->background;
67
68         new_output->top = output->top;
69         new_output->bottom = output->bottom;
70         new_output->left = output->left;
71         new_output->right = output->right;
72
73         new_output->active = output->active;
74         new_output->previous_active = output->previous_active;
75         new_output->name = strdup(output->name);
76         if (output->app_ids)
77                 new_output->app_ids = strdup(output->app_ids);
78
79         new_output->area = output->area;
80         new_output->area_saved = output->area_saved;
81         new_output->area_activation = output->area_activation;
82
83         weston_log("saving output layout for output %s\n", new_output->name);
84
85         wl_list_insert(&ivi->saved_outputs, &new_output->link);
86 }
87
88 void
89 ivi_layout_restore(struct ivi_compositor *ivi, struct ivi_output *n_output)
90 {
91         struct ivi_output *output = NULL;
92         struct ivi_output *iter_output;
93
94         if (!ivi->need_ivi_output_relayout)
95                 return;
96
97         ivi->need_ivi_output_relayout = false;
98
99         wl_list_for_each(iter_output, &ivi->saved_outputs, link) {
100                 if (strcmp(n_output->name, iter_output->name) == 0) {
101                         output = iter_output;
102                         break;
103                 }
104         }
105
106         if (!output)
107                 return;
108
109         weston_log("restoring output layout for output %s\n", output->name);
110         n_output->background = output->background;
111
112         n_output->top = output->top;
113         n_output->bottom = output->bottom;
114         n_output->left = output->left;
115         n_output->right = output->right;
116
117         n_output->active = output->active;
118         n_output->previous_active = output->previous_active;
119         if (output->app_ids)
120                 n_output->app_ids = strdup(output->app_ids);
121
122         n_output->area = output->area;
123         n_output->area_saved = output->area_saved;
124         n_output->area_activation = output->area_activation;
125
126         free(output->app_ids);
127         free(output->name);
128         wl_list_remove(&output->link);
129         free(output);
130 }
131
132 const char *
133 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
134 {
135         if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
136                 return " unknown surface role";
137
138         return ivi_roles_as_string[surf->role];
139 }
140
141 static void
142 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
143 {
144         struct weston_output *woutput = output->output;
145         struct ivi_surface *bg = output->background;
146         struct weston_view *view;
147         struct weston_surface *wsurface =
148                 weston_desktop_surface_get_surface(bg->dsurface);
149
150         if (!bg) {
151                 weston_log("WARNING: Output does not have a background\n");
152                 return;
153         }
154
155         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
156
157         view = bg->view;
158         weston_surface_map(wsurface);
159
160         weston_view_set_output(view, woutput);
161         weston_view_set_position(view, woutput->pos);
162
163         weston_view_move_to_layer(view, &ivi->background.view_list);
164         weston_log("(background) position view %p, x %f, y %f, on output %s\n", view,
165                         woutput->pos.c.x, woutput->pos.c.y, output->name);
166
167 }
168
169 static void
170 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
171                struct ivi_surface *panel)
172 {
173         struct weston_output *woutput = output->output;
174         struct weston_desktop_surface *dsurface;
175         struct weston_view *view;
176         struct weston_geometry geom;
177         struct weston_coord_global pos = woutput->pos;
178         struct weston_surface *wsurface;
179
180         if (!panel)
181                 return;
182
183         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
184         dsurface = panel->dsurface;
185         view = panel->view;
186         geom = weston_desktop_surface_get_geometry(dsurface);
187         wsurface = weston_desktop_surface_get_surface(panel->dsurface);
188
189         weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
190                         geom.width, geom.height, geom.x, geom.y);
191
192         switch (panel->panel.edge) {
193         case AGL_SHELL_EDGE_TOP:
194                 output->area.y += geom.height;
195                 output->area.height -= geom.height;
196                 break;
197         case AGL_SHELL_EDGE_BOTTOM:
198                 pos.c.y += woutput->height - geom.height;
199                 output->area.height -= geom.height;
200                 break;
201         case AGL_SHELL_EDGE_LEFT:
202                 output->area.x += geom.width;
203                 output->area.width -= geom.width;
204                 break;
205         case AGL_SHELL_EDGE_RIGHT:
206                 pos.c.x += woutput->width - geom.width;
207                 output->area.width -= geom.width;
208                 break;
209         }
210
211         pos.c.x -= geom.x;
212         pos.c.y -= geom.y;
213
214         weston_view_set_output(view, woutput);
215         weston_view_set_position(view, pos);
216
217         weston_surface_map(wsurface);
218         weston_view_move_to_layer(view, &ivi->panel.view_list);
219
220         weston_log("(panel) edge %d position view %p, x %f, y %f\n",
221                         panel->panel.edge, view, pos.c.x, pos.c.y);
222
223         weston_log("panel type %d inited on output %s\n", panel->panel.edge,
224                         output->name);
225 }
226
227 /*
228  * Initializes all static parts of the layout, i.e. the background and panels.
229  */
230 void
231 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
232 {
233         bool use_default_area = true;
234         struct weston_config_section *section = output->config;
235         char *t;
236
237         weston_config_section_get_string(section, "activation-area", &t, NULL);
238         if (t) {
239                 if (output->area_activation.width == 0 &&
240                     output->area_activation.height == 0 &&
241                     output->area_activation.x == 0 &&
242                     output->area_activation.y == 0) {
243                         weston_log("WARNING: activation-area set in "
244                                         "configuration file, but yet applied!\n");
245                         if (parse_activation_area(t, output) < 0)
246                                 weston_log("Invalid activation-area \"%s\" for output %s\n",
247                                            t, output->name);
248                 } else {
249                         weston_log("WARNING: activation-area detected in ini file, "
250                                         "but agl_shell override detected!\n");
251                         if (parse_activation_area(t, output) < 0)
252                                 weston_log("Invalid activation-area \"%s\" for output %s\n",
253                                            t, output->name);
254                 }
255         }
256         free(t);
257
258         ivi_background_init(ivi, output);
259
260         if (output->area_activation.width ||
261             output->area_activation.height ||
262             output->area_activation.x ||
263             output->area_activation.y) {
264                 /* Sanity check target area is within output bounds */
265                 if ((output->area_activation.x + output->area_activation.width) < output->output->width ||
266                     (output->area_activation.y + output->area_activation.height) < output->output->height) {
267                         weston_log("Using specified area for output %s, ignoring panels\n",
268                                    output->name);
269                         output->area.x = output->area_activation.x;
270                         output->area.y = output->area_activation.y;
271                         output->area.width = output->area_activation.width;
272                         output->area.height = output->area_activation.height;
273                         use_default_area = false;
274                 } else {
275                         weston_log("Invalid activation-area position for output %s, ignoring\n",
276                                    output->name);
277                 }
278         }
279         if (use_default_area) {
280                 output->area.x = 0;
281                 output->area.y = 0;
282                 output->area.width = output->output->width;
283                 output->area.height = output->output->height;
284
285                 ivi_panel_init(ivi, output, output->top);
286                 ivi_panel_init(ivi, output, output->bottom);
287                 ivi_panel_init(ivi, output, output->left);
288                 ivi_panel_init(ivi, output, output->right);
289         }
290
291         weston_compositor_schedule_repaint(ivi->compositor);
292
293         weston_log("Usable area: %dx%d+%d,%d\n",
294                    output->area.width, output->area.height,
295                    output->area.x, output->area.y);
296 }
297
298 struct ivi_surface *
299 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
300 {
301         struct ivi_surface *surf;
302         const char *id;
303
304         wl_list_for_each(surf, &ivi->surfaces, link) {
305                 id = weston_desktop_surface_get_app_id(surf->dsurface);
306                 if (id && strcmp(app_id, id) == 0)
307                         return surf;
308         }
309
310         return NULL;
311 }
312
313 static void
314 ivi_layout_activate_complete(struct ivi_output *output,
315                              struct ivi_surface *surf)
316 {
317         struct ivi_compositor *ivi = output->ivi;
318         struct weston_output *woutput = output->output;
319         struct weston_view *view = surf->view;
320         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
321         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
322         const char *app_id = weston_desktop_surface_get_app_id(surf->dsurface);
323         bool update_previous = true;
324         struct weston_coord_global pos;
325
326         if (weston_view_is_mapped(view)) {
327                 weston_layer_entry_remove(&view->layer_link);
328         } else {
329                 weston_view_update_transform(view);
330         }
331
332         if (output_has_black_curtain(output)) {
333                 if (!output->background) {
334                         weston_log("Found that we have no background surface "
335                                     "for output %s. Using black curtain as background\n",
336                                     output->output->name);
337
338                         struct weston_view *ev =
339                                 output->fullscreen_view.fs->view;
340
341                         /* use the black curtain as background when we have
342                          * none added by the shell client. */
343                         weston_layer_entry_remove(&ev->layer_link);
344                         weston_layer_entry_insert(&ivi->normal.view_list,
345                                                   &ev->layer_link);
346                         weston_view_geometry_dirty(ev);
347                         weston_surface_damage(ev->surface);
348                 } else {
349                         remove_black_curtain(output);
350                 }
351         }
352
353
354         weston_view_set_output(view, woutput);
355         /* drop any previous masks set on this view */
356         weston_view_set_mask_infinite(view);
357
358         if (surf->role != IVI_SURFACE_ROLE_BACKGROUND) {
359                 pos.c.x = woutput->pos.c.x + output->area.x;
360                 pos.c.y = woutput->pos.c.y + output->area.y;
361                 weston_view_set_position(view, pos);
362         }
363
364         /* reset any previous orientation */
365         if (surf->orientation != AGL_SHELL_TILE_ORIENTATION_NONE &&
366             !surf->sticky) {
367                 weston_log("%s() resetting itself to none orientation\n", __func__);
368                 surf->orientation = AGL_SHELL_TILE_ORIENTATION_NONE;
369                 weston_desktop_surface_set_orientation(surf->dsurface,
370                                                        surf->orientation);
371         }
372         view->is_mapped = true;
373         surf->mapped = true;
374         view->surface->is_mapped = true;
375
376         /* handle a movement from one output to another */
377         if (surf->current_completed_output &&
378             surf->current_completed_output != output) {
379
380                 /* we're migrating the same surface but to another output */
381                 if (surf->current_completed_output->active == surf) {
382                         struct weston_view *ev =
383                                 surf->current_completed_output->active->view;
384
385                         weston_layer_entry_remove(&ev->layer_link);
386                         surf->current_completed_output->previous_active =
387                                 surf->current_completed_output->active;
388                         surf->current_completed_output->active = NULL;
389
390                         /* damage all possible outputs to avoid stale views */
391                         weston_compositor_damage_all(ivi->compositor);
392                 }
393         }
394
395         if (output->active) {
396                 /* keep the background surface mapped at all times */
397                 if (output->active->role != IVI_SURFACE_ROLE_BACKGROUND &&
398                     !output->active->sticky) {
399
400                         output->active->view->is_mapped = false;
401                         output->active->view->surface->is_mapped = false;
402
403                         weston_layer_entry_remove(&output->active->view->layer_link);
404                 }
405         }
406
407         if (output->previous_active && output->active) {
408                 const char *c_app_id =
409                         weston_desktop_surface_get_app_id(output->active->dsurface);
410
411                 /* if the currently activated app_id is the same as the one
412                  * we're trying to complete activation with means we're
413                  * operating on the same app_id so do update previous_active as
414                  * it will overwrite it with the same value */
415                 if (app_id && !strcmp(c_app_id, app_id)) {
416                         update_previous = false;
417                 }
418         }
419
420         if (update_previous)
421                 output->previous_active = output->active;
422         output->active = surf;
423         surf->current_completed_output = output;
424
425         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
426         weston_view_geometry_dirty(view);
427         weston_surface_damage(view->surface);
428
429         if (ivi_seat)
430                 ivi_shell_activate_surface(surf, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
431
432         /*
433          * the 'remote' role now makes use of this part so make sure we don't
434          * trip the enum such that we might end up with a modified output for
435          * 'remote' role
436          */
437         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
438                 if (surf->desktop.pending_output)
439                         surf->desktop.last_output = surf->desktop.pending_output;
440                 surf->desktop.pending_output = NULL;
441         }
442
443         weston_log("Activation completed for app_id %s, role %s, output %s\n",
444                         app_id,
445                         ivi_layout_get_surface_role_name(surf), output->name);
446
447         shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_ACTIVATED);
448 }
449
450 static bool
451 ivi_layout_find_output_with_app_id(const char *app_id, struct ivi_output *output)
452 {
453         char *cur;
454         size_t app_id_len;
455
456         cur = output->app_ids;
457         app_id_len = strlen(app_id);
458
459         while ((cur = strstr(cur, app_id))) {
460                 if ((cur[app_id_len] == ',' || cur[app_id_len] == '\0') &&
461                     (cur == output->app_ids || cur[-1] == ','))
462                         return true;
463                 cur++;
464         }
465
466         return false;
467 }
468
469 struct ivi_output *
470 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
471 {
472         struct ivi_output *out;
473
474         if (!app_id)
475                 return NULL;
476
477         wl_list_for_each(out, &ivi->outputs, link) {
478                 if (!out->app_ids)
479                         continue;
480
481                 if (ivi_layout_find_output_with_app_id(app_id, out))
482                         return out;
483         }
484         return NULL;
485 }
486
487 struct ivi_output *
488 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
489 {
490         struct ivi_output *out;
491
492         wl_list_for_each(out, &ivi->outputs, link) {
493                 if (out->background &&
494                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
495                         return out;
496         }
497
498         return NULL;
499 }
500
501
502 static void
503 ivi_layout_add_to_hidden_layer(struct ivi_surface *surf,
504                                struct ivi_output *ivi_output)
505 {
506         struct weston_desktop_surface *dsurf = surf->dsurface;
507         struct weston_view *ev = surf->view;
508         struct ivi_compositor *ivi = surf->ivi;
509         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
510
511         /*
512          * If the view isn't mapped, we put it onto the hidden layer so it will
513          * start receiving frame events, and will be able to act on our
514          * configure event.
515          */
516         if (!weston_view_is_mapped(ev)) {
517                 ev->is_mapped = true;
518                 ev->surface->is_mapped = true;
519
520                 weston_desktop_surface_set_maximized(dsurf, true);
521                 weston_desktop_surface_set_size(dsurf,
522                                                 ivi_output->area.width,
523                                                 ivi_output->area.height);
524
525                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
526                            app_id, ivi_layout_get_surface_role_name(surf),
527                            ivi_output->area.width, ivi_output->area.height);
528
529                 surf->hidden_layer_output = ivi_output;
530                 weston_view_set_output(ev, ivi_output->output);
531                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
532                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
533                                 app_id, ivi_layout_get_surface_role_name(surf),
534                                 ivi_output->output->name);
535
536                 weston_compositor_schedule_repaint(ivi->compositor);
537                 return;
538         }
539
540         /* we might have another output to activate */
541         if (surf->hidden_layer_output &&
542             surf->hidden_layer_output != ivi_output) {
543                 weston_layer_entry_remove(&ev->layer_link);
544                 weston_view_geometry_dirty(ev);
545                 weston_surface_damage(ev->surface);
546
547                 if (ivi_output->area.width != surf->hidden_layer_output->area.width ||
548                     ivi_output->area.height != surf->hidden_layer_output->area.height) {
549                         weston_desktop_surface_set_maximized(dsurf, true);
550                         weston_desktop_surface_set_size(dsurf,
551                                                         ivi_output->area.width,
552                                                         ivi_output->area.height);
553                 }
554
555                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
556                                 app_id, ivi_layout_get_surface_role_name(surf),
557                                 ivi_output->area.width, ivi_output->area.height);
558
559                 surf->hidden_layer_output = ivi_output;
560                 weston_view_set_output(ev, ivi_output->output);
561                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
562                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
563                                 app_id, ivi_layout_get_surface_role_name(surf),
564                                 ivi_output->output->name);
565         }
566
567         weston_compositor_schedule_repaint(ivi->compositor);
568 }
569
570 void
571 ivi_layout_remote_committed(struct ivi_surface *surf)
572 {
573         struct weston_desktop_surface *dsurf = surf->dsurface;
574         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
575         struct ivi_policy *policy = surf->ivi->policy;
576         struct ivi_output *output;
577         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
578
579         assert(surf->role == IVI_SURFACE_ROLE_REMOTE);
580
581         output = surf->remote.output;
582
583         if (policy && policy->api.surface_activate_by_default &&
584             !policy->api.surface_activate_by_default(surf, surf->ivi))
585                 return;
586
587         /* we can only activate it again by using the protocol, but
588          * additionally the output is not reset when
589          * ivi_layout_activate_complete() terminates so we use the
590          * current active surface to avoid hitting this again and again
591          * */
592         if (surf->mapped && output->active == surf)
593                 return;
594
595         if (!surf->ivi->activate_by_default &&
596             !ivi_surf_in_hidden_layer(surf->ivi, surf)) {
597                 weston_log("Refusing to activate surface role %d, "
598                             "app_id %s\n", surf->role, app_id);
599
600                 if (!weston_desktop_surface_get_maximized(dsurf) ||
601                     geom.width != output->area.width ||
602                     geom.height != output->area.height) {
603                         ivi_layout_add_to_hidden_layer(surf, output);
604                 }
605
606                 return;
607         }
608
609         if (!weston_desktop_surface_get_maximized(dsurf) ||
610             geom.width != output->area.width ||
611             geom.height != output->area.height)
612                 return;
613
614         ivi_layout_activate_complete(output, surf);
615 }
616
617 void
618 ivi_layout_desktop_committed(struct ivi_surface *surf)
619 {
620         struct weston_desktop_surface *dsurf = surf->dsurface;
621         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
622         struct ivi_policy *policy = surf->ivi->policy;
623         struct ivi_output *output;
624         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
625
626         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
627
628         /*
629          * we can't make use here of the ivi_layout_get_output_from_surface()
630          * due to the fact that we'll always land here when a surface performs
631          * a commit and pending_output will not bet set. This works in tandem
632          * with 'mapped' at this point to avoid tripping over
633          * to a surface that continuously updates its content
634          */
635         output = surf->desktop.pending_output;
636
637         if (!output) {
638                 struct ivi_output *r_output;
639
640                 if (policy && policy->api.surface_activate_by_default &&
641                     !policy->api.surface_activate_by_default(surf, surf->ivi))
642                         return;
643
644                 /* we can only activate it again by using the protocol */
645                 if (surf->mapped)
646                         return;
647
648                 /* check first if there aren't any outputs being set */
649                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
650
651                 if (r_output) {
652                         struct weston_view *view = r_output->fullscreen_view.fs->view;
653                         if (view->is_mapped || view->surface->is_mapped)
654                                 remove_black_curtain(r_output);
655                 }
656
657
658                 /* try finding an output with a background and use that */
659                 if (!r_output)
660                         r_output = ivi_layout_find_bg_output(surf->ivi);
661
662                 /* if we couldn't still find an output by this point, there's
663                  * something wrong so we abort with a protocol error */
664                 if (!r_output) {
665                         wl_resource_post_error(surf->ivi->shell_client.resource,
666                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
667                                                "No valid output found to activate surface by default");
668                         return;
669                 }
670
671                 if (!surf->ivi->activate_by_default &&
672                     (!surf->xwayland.is_set && !is_shell_surface_xwayland(surf))) {
673                         weston_log("Refusing to activate surface role %d, app_id %s, type %s\n",
674                                         surf->role, app_id,
675                                         is_shell_surface_xwayland(surf) ?
676                                         "xwayland" : "regular");
677
678                         if (!weston_desktop_surface_get_maximized(dsurf) ||
679                             geom.width != r_output->area.width ||
680                             geom.height != r_output->area.height)
681                                 ivi_layout_add_to_hidden_layer(surf, r_output);
682
683                         return;
684                 }
685
686                 /* use the output of the bg to activate the app on start-up by
687                  * default */
688                 if (surf->view && r_output) {
689                         if (app_id && r_output) {
690                                 weston_log("Surface with app_id %s, role %s activating by default\n",
691                                         weston_desktop_surface_get_app_id(surf->dsurface),
692                                         ivi_layout_get_surface_role_name(surf));
693                                 ivi_layout_activate(r_output, app_id);
694                         } else if (!app_id) {
695                                 /*
696                                  * applications not setting an app_id, or
697                                  * setting an app_id but at a later point in
698                                  * time, might fall-back here so give them a
699                                  * chance to receive the configure event and
700                                  * act upon it
701                                  */
702                                 weston_log("Surface no app_id, role %s activating by default\n",
703                                         ivi_layout_get_surface_role_name(surf));
704                                 if (surf->xwayland.is_set || is_shell_surface_xwayland(surf)) {
705                                         ivi_layout_activate_by_surf(r_output, surf);
706                                         ivi_layout_activate_complete(r_output, surf);
707                                 } else {
708                                         ivi_layout_activate_by_surf(r_output, surf);
709                                 }
710                         }
711                 }
712
713                 return;
714         }
715
716         if (!weston_desktop_surface_get_maximized(dsurf) ||
717             geom.width != output->area.width ||
718             geom.height != output->area.height)
719                 return;
720
721         ivi_layout_activate_complete(output, surf);
722 }
723
724 void
725 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
726 {
727         struct ivi_compositor *ivi = surface->ivi;
728         struct ivi_policy *policy = ivi->policy;
729
730         struct weston_desktop_surface *dsurface = surface->dsurface;
731         struct weston_surface *wsurface =
732                 weston_desktop_surface_get_surface(dsurface);
733         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
734
735         struct ivi_output *output = surface->split.output;
736         struct weston_output *woutput = output->output;
737         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
738
739         struct weston_view *view = surface->view;
740         struct weston_geometry geom =
741                 weston_desktop_surface_get_geometry(dsurface);
742
743         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
744         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
745
746         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
747         bool is_dim_same =
748                 geom.width == bg_output->output->width &&
749                 geom.height == bg_output->output->height;
750
751         if (policy && policy->api.surface_activate_by_default &&
752             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
753             !surface->mapped)
754                 return;
755
756         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
757
758
759         if (surface->state == FULLSCREEN && weston_view_is_mapped(view))
760                 return;
761
762         /* if we still get here but we haven't resized so far, send configure
763          * events to do so */
764         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
765                 struct ivi_output *bg_output =
766                         ivi_layout_find_bg_output(surface->ivi);
767
768                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
769                                 app_id, ivi_layout_get_surface_role_name(surface));
770                 weston_desktop_surface_set_fullscreen(dsurface, true);
771                 weston_desktop_surface_set_size(dsurface,
772                                                 bg_output->output->width,
773                                                 bg_output->output->height);
774
775                 surface->state = RESIZING;
776                 weston_view_set_output(view, output->output);
777                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
778                 return;
779         }
780
781         /* eventually, we would set the surface fullscreen, but the client
782          * hasn't resized correctly by this point, so terminate connection */
783         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
784                 struct weston_desktop_client *desktop_client =
785                         weston_desktop_surface_get_client(dsurface);
786                 struct wl_client *client =
787                         weston_desktop_client_get_client(desktop_client);
788                 wl_client_post_implementation_error(client,
789                                 "can not display surface due to invalid geometry."
790                                 " Client should perform a geometry resize!");
791                 return;
792         }
793
794         /* this implies we resized correctly */
795         if (!weston_view_is_mapped(view) || surface->state != FULLSCREEN) {
796                 weston_layer_entry_remove(&view->layer_link);
797
798                 weston_view_set_output(view, woutput);
799                 weston_view_set_position(view, woutput->pos);
800                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
801
802                 wsurface->is_mapped = true;
803                 surface->view->is_mapped = true;
804                 surface->state = FULLSCREEN;
805
806                 weston_view_geometry_dirty(view);
807                 weston_surface_damage(view->surface);
808
809                 if (ivi_seat)
810                         ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
811
812                 shell_advertise_app_state(ivi, app_id,
813                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
814
815                 weston_log("Activation completed for app_id %s, role %s, "
816                            "output %s\n", app_id,
817                            ivi_layout_get_surface_role_name(surface),
818                            output->name);
819
820                 weston_compositor_schedule_repaint(ivi->compositor);
821         }
822 }
823
824 void
825 ivi_layout_desktop_resize(struct ivi_surface *surface,
826                           struct weston_geometry area)
827 {
828         struct weston_desktop_surface *dsurf = surface->dsurface;
829         struct weston_view *view = surface->view;
830
831         struct weston_coord_global pos;
832         int width = area.width;
833         int height = area.height;
834
835         pos.c.x = area.x;
836         pos.c.y = area.y;
837
838         weston_desktop_surface_set_size(dsurf,
839                                         width, height);
840
841         weston_view_set_position(view, pos);
842
843         weston_view_geometry_dirty(view);
844         weston_surface_damage(view->surface);
845 }
846
847 void
848 ivi_layout_split_committed(struct ivi_surface *surface)
849 {
850         struct ivi_compositor *ivi = surface->ivi;
851         struct ivi_policy *policy = ivi->policy;
852
853         struct weston_desktop_surface *dsurface = surface->dsurface;
854         struct weston_surface *wsurface =
855                 weston_desktop_surface_get_surface(dsurface);
856         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
857
858         struct ivi_output *output = surface->split.output;
859         struct weston_output *woutput = output->output;
860
861         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
862         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
863
864         struct weston_view *view = surface->view;
865         struct weston_geometry geom;
866
867         struct weston_coord_global pos = woutput->pos;
868         int width, height;
869
870         if (policy && policy->api.surface_activate_by_default &&
871             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
872             !surface->mapped)
873                 return;
874
875         if (surface->view->is_mapped)
876                 return;
877
878         geom = weston_desktop_surface_get_geometry(dsurface);
879
880         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
881                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
882
883         /* save the previous area in order to recover it back when if this kind
884          * of surface is being destroyed/removed */
885         output->area_saved = output->area;
886
887         switch (surface->role) {
888         case IVI_SURFACE_ROLE_SPLIT_V:
889                 geom.width = (output->area.width / 2);
890
891                 pos.c.x += woutput->width - geom.width;
892                 output->area.width -= geom.width;
893
894                 width = woutput->width - pos.c.x;
895                 height = output->area.height;
896                 pos.c.y = output->area.y;
897
898                 break;
899         case IVI_SURFACE_ROLE_SPLIT_H:
900                 geom.height = (output->area.height / 2);
901
902                 pos.c.y = output->area.y;
903                 output->area.y += geom.height;
904                 output->area.height -= geom.height;
905
906                 width = output->area.width;
907                 height = output->area.height;
908
909                 pos.c.x = output->area.x;
910
911                 break;
912         default:
913                 assert(!"Invalid split orientation\n");
914         }
915
916         weston_desktop_surface_set_size(dsurface,
917                                         width, height);
918
919         /* resize the active surface first, output->area already contains
920          * correct area to resize to */
921         if (output->active)
922                 ivi_layout_desktop_resize(output->active, output->area);
923
924         weston_view_set_output(view, woutput);
925         weston_view_set_position(view, pos);
926         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
927
928         weston_view_geometry_dirty(view);
929         weston_surface_damage(view->surface);
930
931         if (ivi_seat)
932                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
933
934         wsurface->is_mapped = true;
935         surface->view->is_mapped = true;
936
937         shell_advertise_app_state(ivi, app_id,
938                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
939
940         weston_log("Activation completed for app_id %s, role %s, output %s\n",
941                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
942 }
943
944 static void
945 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
946                            int initial_x, int initial_y, double *new_x, double *new_y)
947 {
948         *new_x = output->pos.c.x + initial_x;
949         *new_y = output->pos.c.y + initial_y;
950 }
951
952
953 bool
954 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface)
955 {
956         struct weston_view *ev;
957
958         wl_list_for_each(ev, &ivi->hidden.view_list.link, layer_link.link) {
959                 if (ev == surface->view)
960                         return true;
961         }
962
963         wl_list_for_each(ev, &ivi->fullscreen.view_list.link, layer_link.link) {
964                 if (ev == surface->view)
965                         return true;
966         }
967
968         return false;
969 }
970
971 void
972 ivi_layout_popup_committed(struct ivi_surface *surface)
973 {
974         struct ivi_compositor *ivi = surface->ivi;
975         struct ivi_policy *policy = ivi->policy;
976
977         struct weston_desktop_surface *dsurface = surface->dsurface;
978         struct weston_surface *wsurface =
979                 weston_desktop_surface_get_surface(dsurface);
980         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
981
982         struct weston_coord_global pos;
983
984         struct ivi_output *output = surface->popup.output;
985         struct weston_output *woutput = output->output;
986
987         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
988         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
989
990         struct weston_view *view = surface->view;
991
992         if (policy && policy->api.surface_activate_by_default &&
993             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
994             !surface->mapped)
995                 return;
996
997         if (surface->view->is_mapped || surface->state == HIDDEN)
998                 return;
999
1000         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
1001
1002         /* remove it from hidden layer if present */
1003         if (ivi_surf_in_hidden_layer(ivi, surface))
1004                 weston_layer_entry_remove(&view->layer_link);
1005
1006         weston_view_set_output(view, woutput);
1007
1008         ivi_compute_popup_position(woutput, view,
1009                                    surface->popup.x, surface->popup.y, &pos.c.x, &pos.c.y);
1010         weston_view_set_position(view, pos);
1011         weston_view_update_transform(view);
1012
1013         /* only clip the pop-up dialog window if we have a valid
1014          * width and height being passed on. Users might not want to have one
1015          * set-up so only enfore it is really passed on. */
1016         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
1017                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
1018                                      surface->popup.bb.width, surface->popup.bb.height);
1019
1020         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
1021
1022         weston_view_geometry_dirty(view);
1023         weston_surface_damage(view->surface);
1024
1025         if (ivi_seat)
1026                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
1027
1028         wsurface->is_mapped = true;
1029         surface->view->is_mapped = true;
1030
1031         shell_advertise_app_state(ivi, app_id,
1032                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
1033
1034         weston_log("Activation completed for app_id %s, role %s, output %s\n",
1035                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
1036 }
1037
1038 static void
1039 ivi_layout_popup_re_add(struct ivi_surface *surface)
1040 {
1041         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
1042         struct weston_view *view = surface->view;
1043
1044         if (weston_view_is_mapped(view)) {
1045                 struct weston_desktop_surface *dsurface = surface->dsurface;
1046                 struct weston_surface *wsurface =
1047                         weston_desktop_surface_get_surface(dsurface);
1048
1049                 weston_layer_entry_remove(&view->layer_link);
1050
1051                 wsurface->is_mapped = false;
1052                 view->is_mapped = false;
1053         }
1054
1055         /* reset the activate by default in order to (still) allow the surface
1056          * to be activaved using the request */
1057         if (!surface->mapped)
1058                 surface->mapped = true;
1059
1060         surface->state = NORMAL;
1061         ivi_layout_popup_committed(surface);
1062 }
1063
1064 static void
1065 ivi_layout_fullscreen_re_add(struct ivi_surface *surface)
1066 {
1067         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
1068         struct weston_view *view = surface->view;
1069
1070         if (weston_view_is_mapped(view)) {
1071                 struct weston_desktop_surface *dsurface = surface->dsurface;
1072                 struct weston_surface *wsurface =
1073                         weston_desktop_surface_get_surface(dsurface);
1074
1075                 weston_layer_entry_remove(&view->layer_link);
1076
1077                 wsurface->is_mapped = false;
1078                 view->is_mapped = false;
1079         }
1080
1081         /* reset the activate by default in order to (still) allow the surface
1082          * to be activaved using the request */
1083         if (!surface->mapped)
1084                 surface->mapped = true;
1085
1086         surface->state = NORMAL;
1087         ivi_layout_fullscreen_committed(surface);
1088 }
1089
1090 static bool
1091 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
1092 {
1093         struct ivi_compositor *ivi = surf->ivi;
1094         struct ivi_surface *is;
1095
1096         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
1097             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
1098             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
1099                 return false;
1100
1101         /* reset the activate by default in order to (still) allow the surface
1102          * to be activaved using the request */
1103         if (!surf->mapped)
1104                 surf->mapped = true;
1105
1106         wl_list_for_each(is, &ivi->surfaces, link)
1107                 if (is == surf)
1108                         return true;
1109
1110         return false;
1111 }
1112
1113 void
1114 ivi_layout_reset_split_surfaces(struct ivi_compositor *ivi)
1115 {
1116         struct ivi_surface *ivisurf;
1117         struct ivi_surface *found_ivi_surf = NULL;
1118         bool found_split_surface = false;
1119         struct ivi_output *output = NULL;
1120
1121         wl_list_for_each(ivisurf, &ivi->surfaces, link) {
1122                 if (ivisurf->orientation != AGL_SHELL_TILE_ORIENTATION_NONE) {
1123                         found_ivi_surf = ivisurf;
1124                         found_split_surface = true;
1125                         break;
1126                 }
1127         }
1128
1129         if (!found_split_surface)
1130                 return;
1131
1132         output = found_ivi_surf->current_completed_output;
1133
1134         if (found_ivi_surf->sticky)
1135                 return;
1136
1137         if (output->previous_active && output->background != output->previous_active) {
1138                 struct weston_view *ev = output->previous_active->view;
1139                 struct weston_output *woutput = output->output;
1140
1141                 if (!weston_view_is_mapped(ev))
1142                         weston_view_update_transform(ev);
1143                 else
1144                         weston_layer_entry_remove(&ev->layer_link);
1145
1146                 ev->is_mapped = true;
1147                 ev->surface->is_mapped = true;
1148                 output->previous_active->mapped = true;
1149
1150                 weston_view_set_output(ev, woutput);
1151
1152                 weston_layer_entry_insert(&ivi->normal.view_list, &ev->layer_link);
1153
1154                 _ivi_set_shell_surface_split(output->previous_active, NULL, 0,
1155                                              AGL_SHELL_TILE_ORIENTATION_NONE, false, false);
1156
1157                 if (output->active == ivisurf) {
1158                         output->active = output->previous_active;
1159                 }
1160         }
1161
1162         _ivi_set_shell_surface_split(ivisurf, NULL, 0,
1163                                      AGL_SHELL_TILE_ORIENTATION_NONE, false, false);
1164 }
1165
1166 void
1167 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
1168 {
1169         struct ivi_compositor *ivi = output->ivi;
1170         struct weston_desktop_surface *dsurf;
1171         struct weston_geometry geom;
1172         struct ivi_policy *policy = output->ivi->policy;
1173
1174         dsurf = surf->dsurface;
1175
1176         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
1177
1178         if (!surf)
1179                 return;
1180
1181         if (policy && policy->api.surface_activate &&
1182             !policy->api.surface_activate(surf, surf->ivi)) {
1183                 return;
1184         }
1185
1186 #ifdef AGL_COMP_DEBUG
1187         weston_log("Activating app_id %s, type %s, on output %s\n", app_id,
1188                         ivi_layout_get_surface_role_name(surf), output->output->name);
1189 #endif
1190
1191         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1192                 ivi_layout_popup_re_add(surf);
1193                 return;
1194         }
1195
1196         if (surf->role == IVI_SURFACE_ROLE_FULLSCREEN) {
1197                 ivi_layout_fullscreen_re_add(surf);
1198                 return;
1199         }
1200 #if 0
1201         /* reset tile to desktop to allow to resize correctly */
1202         if (surf->role == IVI_SURFACE_ROLE_TILE && output->active == surf) {
1203                 weston_log("%s() resetting tile role!\n", __func__);
1204                 surf->role = IVI_SURFACE_ROLE_DESKTOP;
1205         }
1206 #endif
1207
1208         /* do not 're'-activate surfaces that are split or active */
1209         if (surf == output->active) {
1210                 weston_log("Application %s is already active on output %s\n",
1211                                 app_id, output->output->name);
1212                 return;
1213         }
1214
1215         if (surf->sticky && surf->role == IVI_SURFACE_ROLE_TILE && output->active == surf) {
1216                 weston_log("Application %s is already active on output %s (split role)\n",
1217                                 app_id, output->output->name);
1218                 return;
1219         }
1220
1221         if (ivi_layout_surface_is_split_or_fullscreen(surf)) {
1222                 weston_log("Application %s is fullscreen or split on output %s\n",
1223                                 app_id, output->output->name);
1224                 return;
1225         }
1226
1227         // destroy any split types to allow correct re-activation
1228         ivi_layout_reset_split_surfaces(surf->ivi);
1229
1230         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
1231                 struct ivi_output *remote_output =
1232                         ivi_layout_find_with_app_id(app_id, ivi);
1233
1234                 weston_log("Changed activation for app_id %s, type %s, on output %s\n", app_id,
1235                                 ivi_layout_get_surface_role_name(surf), output->output->name);
1236
1237                 /* if already active on a remote output do not
1238                  * attempt to activate it again */
1239                 if (remote_output && remote_output->active == surf)
1240                         return;
1241         }
1242
1243
1244         geom = weston_desktop_surface_get_geometry(dsurf);
1245
1246         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
1247                 surf->desktop.pending_output = output;
1248         if (weston_desktop_surface_get_maximized(dsurf) &&
1249             geom.width == output->area.width &&
1250             geom.height == output->area.height) {
1251                 ivi_layout_activate_complete(output, surf);
1252                 return;
1253         }
1254
1255         /* the background surface is already "maximized" so we don't need to
1256          * add to the hidden layer */
1257         if (surf->role == IVI_SURFACE_ROLE_BACKGROUND &&
1258             output->active->role != IVI_SURFACE_ROLE_TILE) {
1259                 ivi_layout_activate_complete(output, surf);
1260                 return;
1261         }
1262
1263         ivi_layout_add_to_hidden_layer(surf, output);
1264 }
1265
1266 void
1267 ivi_layout_activate(struct ivi_output *output, const char *app_id)
1268 {
1269         struct ivi_surface *surf;
1270         struct ivi_compositor *ivi = output->ivi;
1271
1272         if (!app_id)
1273                 return;
1274
1275         surf = ivi_find_app(ivi, app_id);
1276         if (!surf)
1277                 return;
1278
1279         ivi_layout_activate_by_surf(output, surf);
1280 }
1281
1282 struct ivi_output *
1283 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
1284 {
1285         struct ivi_output *ivi_output = NULL;
1286
1287         switch (surf->role) {
1288         case IVI_SURFACE_ROLE_DESKTOP:
1289                 if (surf->desktop.pending_output)
1290                         ivi_output = surf->desktop.pending_output;
1291                 else
1292                         ivi_output = surf->desktop.last_output;
1293                 break;
1294         case IVI_SURFACE_ROLE_POPUP:
1295                 ivi_output = surf->popup.output;
1296                 break;
1297         case IVI_SURFACE_ROLE_BACKGROUND:
1298                 ivi_output = surf->bg.output;
1299                 break;
1300         case IVI_SURFACE_ROLE_PANEL:
1301                 ivi_output = surf->panel.output;
1302                 break;
1303         case IVI_SURFACE_ROLE_FULLSCREEN:
1304                 ivi_output = surf->fullscreen.output;
1305                 break;
1306         case IVI_SURFACE_ROLE_SPLIT_H:
1307         case IVI_SURFACE_ROLE_SPLIT_V:
1308                 ivi_output = surf->split.output;
1309                 break;
1310         case IVI_SURFACE_ROLE_REMOTE:
1311                 ivi_output = surf->remote.output;
1312                 break;
1313         case IVI_SURFACE_ROLE_TILE:
1314                 ivi_output = surf->current_completed_output;
1315                 break;
1316         case IVI_SURFACE_ROLE_NONE:
1317         default:
1318                 if (surf->view->output)
1319                         return to_ivi_output(surf->view->output);
1320                 break;
1321         }
1322
1323         return ivi_output;
1324 }
1325
1326 void
1327 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
1328 {
1329         struct ivi_surface *surf;
1330         struct ivi_output *ivi_output;
1331         struct ivi_policy *policy = ivi->policy;
1332
1333         if (!app_id)
1334                 return;
1335
1336         surf = ivi_find_app(ivi, app_id);
1337         if (!surf)
1338                 return;
1339
1340         if (policy && policy->api.surface_deactivate &&
1341             !policy->api.surface_deactivate(surf, surf->ivi)) {
1342                 return;
1343         }
1344
1345         ivi_output = ivi_layout_get_output_from_surface(surf);
1346         weston_log("Deactiving %s, role %s\n", app_id,
1347                         ivi_layout_get_surface_role_name(surf));
1348
1349         if (surf->role == IVI_SURFACE_ROLE_DESKTOP ||
1350             surf->role == IVI_SURFACE_ROLE_REMOTE) {
1351                 struct ivi_surface *previous_active;
1352
1353                 previous_active = ivi_output->previous_active;
1354                 if (!previous_active) {
1355                         /* we don't have a previous active it means we should
1356                          * display the bg */
1357                         if (ivi_output->active) {
1358                                 struct weston_view *view;
1359
1360                                 view = ivi_output->active->view;
1361                                 view->is_mapped = false;
1362                                 view->surface->is_mapped = false;
1363
1364                                 weston_layer_entry_remove(&view->layer_link);
1365                                 weston_view_geometry_dirty(view);
1366                                 weston_surface_damage(view->surface);
1367                                 ivi_output->active = NULL;
1368                         }
1369                 } else {
1370                         struct weston_desktop_surface *dsurface;
1371                         const char *previous_active_app_id;
1372
1373                         dsurface = previous_active->dsurface;
1374                         previous_active_app_id =
1375                                 weston_desktop_surface_get_app_id(dsurface);
1376                         ivi_layout_activate(ivi_output, previous_active_app_id);
1377                 }
1378         } else if (surf->role == IVI_SURFACE_ROLE_POPUP ||
1379                    surf->role == IVI_SURFACE_ROLE_FULLSCREEN) {
1380                 struct weston_view *view  = surf->view;
1381
1382                 weston_view_unmap(view);
1383                 surf->state = HIDDEN;
1384
1385                 weston_layer_entry_remove(&view->layer_link);
1386                 weston_view_geometry_dirty(view);
1387                 weston_surface_damage(view->surface);
1388         }
1389
1390         shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_DEACTIVATED);
1391 }