layout: Make the view mapped at activation completion
[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         surf->mapped = true;
207         view->surface->is_mapped = true;
208
209         if (output->active) {
210                 output->active->view->is_mapped = false;
211                 output->active->view->surface->is_mapped = false;
212
213                 weston_layer_entry_remove(&output->active->view->layer_link);
214         }
215         output->previous_active = output->active;
216         output->active = surf;
217
218         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
219         weston_view_geometry_dirty(view);
220         weston_surface_damage(view->surface);
221
222         /*
223          * the 'remote' role now makes use of this part so make sure we don't
224          * trip the enum such that we might end up with a modified output for
225          * 'remote' role
226          */
227         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
228                 if (surf->desktop.pending_output)
229                         surf->desktop.last_output = surf->desktop.pending_output;
230                 surf->desktop.pending_output = NULL;
231         }
232
233         weston_log("Activation completed for app_id %s, role %s, output %s\n",
234                         weston_desktop_surface_get_app_id(surf->dsurface),
235                         ivi_layout_get_surface_role_name(surf), output->name);
236 }
237
238 struct ivi_output *
239 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
240 {
241         struct ivi_output *out;
242
243         if (!app_id)
244                 return NULL;
245
246         wl_list_for_each(out, &ivi->outputs, link) {
247                 if (!out->app_id)
248                         continue;
249
250                 if (!strcmp(app_id, out->app_id))
251                         return out;
252         }
253
254         return NULL;
255 }
256
257
258 static struct ivi_output *
259 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
260 {
261         struct ivi_output *out;
262
263         wl_list_for_each(out, &ivi->outputs, link) {
264                 if (out->background &&
265                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
266                         return out;
267         }
268
269         return NULL;
270 }
271
272 void
273 ivi_layout_desktop_committed(struct ivi_surface *surf)
274 {
275         struct weston_desktop_surface *dsurf = surf->dsurface;
276         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
277         struct ivi_policy *policy = surf->ivi->policy;
278         struct ivi_output *output;
279         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
280
281         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
282                surf->role == IVI_SURFACE_ROLE_REMOTE);
283
284         /*
285          * we can't make use here of the ivi_layout_get_output_from_surface()
286          * due to the fact that we'll always land here when a surface performs
287          * a commit and pending_output will not bet set. This works in tandem
288          * with 'mapped' at this point to avoid tripping over
289          * to a surface that continuously updates its content
290          */
291         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
292                 output = surf->desktop.pending_output;
293         else
294                 output = surf->remote.output;
295
296         if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
297                 struct ivi_output *r_output;
298
299                 if (policy && policy->api.surface_activate_by_default &&
300                     !policy->api.surface_activate_by_default(surf, surf->ivi))
301                         return;
302
303                 /* we can only activate it again by using the protocol */
304                 if (surf->mapped)
305                         return;
306
307                 /* check first if there aren't any outputs being set */
308                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
309
310                 if (r_output) {
311                         struct weston_view *view = r_output->fullscreen_view.fs->view;
312                         if (view->is_mapped || view->surface->is_mapped)
313                                 remove_black_surface(r_output);
314                 }
315
316
317                 /* try finding an output with a background and use that */
318                 if (!r_output)
319                         r_output = ivi_layout_find_bg_output(surf->ivi);
320
321                 /* if we couldn't still find an output by this point, there's
322                  * something wrong so we abort with a protocol error */
323                 if (!r_output) {
324                         wl_resource_post_error(surf->ivi->shell_client.resource,
325                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
326                                                "No valid output found to activate surface by default");
327                         return;
328                 }
329
330                 if (!surf->ivi->activate_by_default) {
331                         weston_log("Refusing to activate surface role %d, app_id %s\n",
332                                         surf->role, app_id);
333                         return;
334                 }
335
336                 /* use the output of the bg to activate the app on start-up by
337                  * default */
338                 if (surf->view && r_output) {
339                         if (app_id && r_output) {
340                                 weston_log("Surface with app_id %s, role %s activating by default\n",
341                                         weston_desktop_surface_get_app_id(surf->dsurface),
342                                         ivi_layout_get_surface_role_name(surf));
343                                 ivi_layout_activate(r_output, app_id);
344                         } else if (!app_id) {
345                                 /*
346                                  * applications not setting an app_id, or
347                                  * setting an app_id but at a later point in
348                                  * time, might fall-back here so give them a
349                                  * chance to receive the configure event and
350                                  * act upon it
351                                  */
352                                 weston_log("Surface no app_id, role %s activating by default\n",
353                                         ivi_layout_get_surface_role_name(surf));
354                                 ivi_layout_activate_by_surf(r_output, surf);
355                         }
356                 }
357
358                 return;
359         }
360
361         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
362                 if (policy && policy->api.surface_activate_by_default &&
363                     !policy->api.surface_activate_by_default(surf, surf->ivi))
364                         return;
365
366                 /* we can only activate it again by using the protocol, but
367                  * additionally the output is not reset when
368                  * ivi_layout_activate_complete() terminates so we use the
369                  * current active surface to avoid hitting this again and again
370                  * */
371                 if (surf->mapped && output->active == surf)
372                         return;
373
374                 if (app_id) {
375                         weston_log("Surface with app_id %s, role %s activating by default\n",
376                                         weston_desktop_surface_get_app_id(surf->dsurface),
377                                         ivi_layout_get_surface_role_name(surf));
378                         ivi_layout_activate(output, app_id);
379                 }
380                 return;
381         }
382
383         if (!weston_desktop_surface_get_maximized(dsurf) ||
384             geom.width != output->area.width ||
385             geom.height != output->area.height)
386                 return;
387
388         ivi_layout_activate_complete(output, surf);
389 }
390
391 void
392 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
393 {
394         struct ivi_compositor *ivi = surface->ivi;
395         struct ivi_policy *policy = ivi->policy;
396
397         struct weston_desktop_surface *dsurface = surface->dsurface;
398         struct weston_surface *wsurface =
399                 weston_desktop_surface_get_surface(dsurface);
400         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
401
402         struct ivi_output *output = surface->split.output;
403         struct weston_output *woutput = output->output;
404
405         struct weston_view *view = surface->view;
406         struct weston_geometry geom;
407
408         if (policy && policy->api.surface_activate_by_default &&
409             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
410             !surface->mapped)
411                 return;
412
413         if (surface->view->is_mapped)
414                 return;
415
416         geom = weston_desktop_surface_get_geometry(dsurface);
417         weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
418                         geom.width, geom.height);
419
420         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
421
422         weston_desktop_surface_set_fullscreen(dsurface, true);
423
424         weston_view_set_output(view, woutput);
425         weston_view_set_position(view, woutput->x, woutput->y);
426         weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
427
428         weston_view_geometry_dirty(view);
429         weston_surface_damage(view->surface);
430
431         wsurface->is_mapped = true;
432         surface->view->is_mapped = true;
433
434         shell_advertise_app_state(ivi, app_id,
435                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
436
437         weston_log("Activation completed for app_id %s, role %s, output %s\n",
438                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
439 }
440
441 void
442 ivi_layout_desktop_resize(struct ivi_surface *surface,
443                           struct weston_geometry area)
444 {
445         struct weston_desktop_surface *dsurf = surface->dsurface;
446         struct weston_view *view = surface->view;
447
448         int x = area.x;
449         int y = area.y;
450         int width = area.width;
451         int height = area.height;
452
453         weston_desktop_surface_set_size(dsurf,
454                                         width, height);
455
456         weston_view_set_position(view, x, y);
457
458         weston_view_geometry_dirty(view);
459         weston_surface_damage(view->surface);
460 }
461
462 void
463 ivi_layout_split_committed(struct ivi_surface *surface)
464 {
465         struct ivi_compositor *ivi = surface->ivi;
466         struct ivi_policy *policy = ivi->policy;
467
468         struct weston_desktop_surface *dsurface = surface->dsurface;
469         struct weston_surface *wsurface =
470                 weston_desktop_surface_get_surface(dsurface);
471         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
472
473         struct ivi_output *output = surface->split.output;
474         struct weston_output *woutput = output->output;
475
476         struct weston_view *view = surface->view;
477         struct weston_geometry geom;
478
479         int x, y;
480         int width, height;
481
482         x = woutput->x;
483         y = woutput->y;
484
485         if (policy && policy->api.surface_activate_by_default &&
486             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
487             !surface->mapped)
488                 return;
489
490         if (surface->view->is_mapped)
491                 return;
492
493         geom = weston_desktop_surface_get_geometry(dsurface);
494
495         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
496                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
497
498         /* save the previous area in order to recover it back when if this kind
499          * of surface is being destroyed/removed */
500         output->area_saved = output->area;
501
502         switch (surface->role) {
503         case IVI_SURFACE_ROLE_SPLIT_V:
504                 geom.width = (output->area.width / 2);
505
506                 x += woutput->width - geom.width;
507                 output->area.width -= geom.width;
508
509                 width = woutput->width - x;
510                 height = output->area.height;
511                 y = output->area.y;
512
513                 break;
514         case IVI_SURFACE_ROLE_SPLIT_H:
515                 geom.height = (output->area.height / 2);
516
517                 y = output->area.y;
518                 output->area.y += geom.height;
519                 output->area.height -= geom.height;
520
521                 width = output->area.width;
522                 height = output->area.height;
523
524                 x = output->area.x;
525
526                 break;
527         default:
528                 assert(!"Invalid split orientation\n");
529         }
530
531         weston_desktop_surface_set_size(dsurface,
532                                         width, height);
533
534         /* resize the active surface first, output->area already contains
535          * correct area to resize to */
536         if (output->active)
537                 ivi_layout_desktop_resize(output->active, output->area);
538
539         weston_view_set_output(view, woutput);
540         weston_view_set_position(view, x, y);
541         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
542
543         weston_view_geometry_dirty(view);
544         weston_surface_damage(view->surface);
545
546         wsurface->is_mapped = true;
547         surface->view->is_mapped = true;
548
549         shell_advertise_app_state(ivi, app_id,
550                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
551
552         weston_log("Activation completed for app_id %s, role %s, output %s\n",
553                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
554 }
555
556 static void
557 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
558                            int initial_x, int initial_y, int *new_x, int *new_y)
559 {
560         *new_x = output->x + initial_x;
561         *new_y = output->y + initial_y;
562 }
563
564
565 void
566 ivi_layout_popup_committed(struct ivi_surface *surface)
567 {
568         struct ivi_compositor *ivi = surface->ivi;
569         struct ivi_policy *policy = ivi->policy;
570
571         struct weston_desktop_surface *dsurface = surface->dsurface;
572         struct weston_surface *wsurface =
573                 weston_desktop_surface_get_surface(dsurface);
574         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
575
576         int new_x, new_y;
577
578         struct ivi_output *output = surface->popup.output;
579         struct weston_output *woutput = output->output;
580
581         struct weston_view *view = surface->view;
582
583         if (policy && policy->api.surface_activate_by_default &&
584             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
585             !surface->mapped)
586                 return;
587
588         if (surface->view->is_mapped)
589                 return;
590
591         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
592
593         weston_view_set_output(view, woutput);
594
595         ivi_compute_popup_position(woutput, view,
596                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
597         weston_view_set_position(view, new_x, new_y);
598
599         /* only clip the pop-up dialog window if we have a valid
600          * width and height being passed on. Users might not want to have one
601          * set-up so only enfore it is really passed on. */
602         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
603                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
604                                      surface->popup.bb.width, surface->popup.bb.height);
605
606         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
607
608         weston_view_geometry_dirty(view);
609         weston_surface_damage(view->surface);
610
611         wsurface->is_mapped = true;
612         surface->view->is_mapped = true;
613
614         shell_advertise_app_state(ivi, app_id,
615                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
616
617         weston_log("Activation completed for app_id %s, role %s, output %s\n",
618                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
619 }
620
621 static void
622 ivi_layout_popup_re_add(struct ivi_surface *surface)
623 {
624         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
625         struct weston_view *view = surface->view;
626
627         if (weston_view_is_mapped(view)) {
628                 struct weston_desktop_surface *dsurface = surface->dsurface;
629                 struct weston_surface *wsurface =
630                         weston_desktop_surface_get_surface(dsurface);
631
632                 weston_layer_entry_remove(&view->layer_link);
633
634                 wsurface->is_mapped = false;
635                 view->is_mapped = false;
636         }
637
638         /* reset the activate by default in order to (still) allow the surface
639          * to be activaved using the request */
640         if (!surface->mapped)
641                 surface->mapped = true;
642
643         ivi_layout_popup_committed(surface);
644 }
645
646 static bool
647 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
648 {
649         struct ivi_compositor *ivi = surf->ivi;
650         struct ivi_surface *is;
651
652         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
653             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
654             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
655                 return false;
656
657         /* reset the activate by default in order to (still) allow the surface
658          * to be activaved using the request */
659         if (!surf->mapped)
660                 surf->mapped = true;
661
662         wl_list_for_each(is, &ivi->surfaces, link)
663                 if (is == surf)
664                         return true;
665
666         return false;
667 }
668
669 void
670 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
671 {
672         struct ivi_compositor *ivi = output->ivi;
673         struct weston_desktop_surface *dsurf;
674         struct weston_view *view;
675         struct weston_geometry geom;
676         struct ivi_policy *policy = output->ivi->policy;
677
678         dsurf = surf->dsurface;
679         view = surf->view;
680
681         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
682
683         if (!surf)
684                 return;
685
686         if (policy && policy->api.surface_activate &&
687             !policy->api.surface_activate(surf, surf->ivi)) {
688                 return;
689         }
690
691 #ifdef AGL_COMP_DEBUG
692         weston_log("Activating app_id %s, type %s\n", app_id,
693                         ivi_layout_get_surface_role_name(surf));
694 #endif
695
696         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
697                 ivi_layout_popup_re_add(surf);
698                 return;
699         }
700
701         /* do not 're'-activate surfaces that are split or active */
702         if (surf == output->active ||
703             ivi_layout_surface_is_split_or_fullscreen(surf))
704                 return;
705
706         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
707                 struct ivi_output *remote_output =
708                         ivi_layout_find_with_app_id(app_id, ivi);
709
710                 /* if already active on a remote output do not
711                  * attempt to activate it again */
712                 if (remote_output && remote_output->active == surf)
713                         return;
714         }
715
716
717         geom = weston_desktop_surface_get_geometry(dsurf);
718
719         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
720                 surf->desktop.pending_output = output;
721         if (weston_desktop_surface_get_maximized(dsurf) &&
722             geom.width == output->area.width &&
723             geom.height == output->area.height) {
724                 ivi_layout_activate_complete(output, surf);
725                 return;
726         }
727
728         weston_desktop_surface_set_maximized(dsurf, true);
729         weston_desktop_surface_set_size(dsurf,
730                                         output->area.width,
731                                         output->area.height);
732
733         weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
734                         app_id, ivi_layout_get_surface_role_name(surf),
735                         output->area.width, output->area.height);
736         /*
737          * If the view isn't mapped, we put it onto the hidden layer so it will
738          * start receiving frame events, and will be able to act on our
739          * configure event.
740          */
741         if (!weston_view_is_mapped(view)) {
742                 view->is_mapped = true;
743                 view->surface->is_mapped = true;
744
745                 weston_view_set_output(view, output->output);
746                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
747                 weston_log("Placed app_id %s, type %s in hidden layer\n",
748                                 app_id, ivi_layout_get_surface_role_name(surf));
749         }
750 }
751
752 void
753 ivi_layout_activate(struct ivi_output *output, const char *app_id)
754 {
755         struct ivi_surface *surf;
756         struct ivi_compositor *ivi = output->ivi;
757
758         if (!app_id)
759                 return;
760
761         surf = ivi_find_app(ivi, app_id);
762         if (!surf)
763                 return;
764
765         ivi_layout_activate_by_surf(output, surf);
766 }
767
768 struct ivi_output *
769 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
770 {
771         struct ivi_output *ivi_output = NULL;
772
773         switch (surf->role) {
774         case IVI_SURFACE_ROLE_DESKTOP:
775                 if (surf->desktop.pending_output)
776                         ivi_output = surf->desktop.pending_output;
777                 else
778                         ivi_output = surf->desktop.last_output;
779                 break;
780         case IVI_SURFACE_ROLE_POPUP:
781                 ivi_output = surf->popup.output;
782                 break;
783         case IVI_SURFACE_ROLE_BACKGROUND:
784                 ivi_output = surf->bg.output;
785                 break;
786         case IVI_SURFACE_ROLE_PANEL:
787                 ivi_output = surf->panel.output;
788                 break;
789         case IVI_SURFACE_ROLE_FULLSCREEN:
790                 ivi_output = surf->fullscreen.output;
791                 break;
792         case IVI_SURFACE_ROLE_SPLIT_H:
793         case IVI_SURFACE_ROLE_SPLIT_V:
794                 ivi_output = surf->split.output;
795                 break;
796         case IVI_SURFACE_ROLE_REMOTE:
797                 ivi_output = surf->remote.output;
798                 break;
799         case IVI_SURFACE_ROLE_NONE:
800         default:
801                 break;
802         }
803
804         return ivi_output;
805 }
806
807 void
808 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
809 {
810         struct ivi_surface *surf;
811         struct ivi_output *ivi_output;
812         struct ivi_policy *policy = ivi->policy;
813
814         if (!app_id)
815                 return;
816
817         surf = ivi_find_app(ivi, app_id);
818         if (!surf)
819                 return;
820
821         if (policy && policy->api.surface_deactivate &&
822             !policy->api.surface_deactivate(surf, surf->ivi)) {
823                 return;
824         }
825
826         ivi_output = ivi_layout_get_output_from_surface(surf);
827         weston_log("Deactiving %s, role %s\n", app_id,
828                         ivi_layout_get_surface_role_name(surf));
829
830         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
831                 struct ivi_surface *previous_active;
832
833                 previous_active = ivi_output->previous_active;
834                 if (!previous_active) {
835                         /* we don't have a previous active it means we should
836                          * display the bg */
837                         if (ivi_output->active) {
838                                 struct weston_view *view;
839
840                                 view = ivi_output->active->view;
841                                 view->is_mapped = false;
842                                 view->surface->is_mapped = false;
843
844                                 weston_layer_entry_remove(&view->layer_link);
845                                 weston_view_geometry_dirty(view);
846                                 weston_surface_damage(view->surface);
847                                 ivi_output->active = NULL;
848                         }
849                 } else {
850                         struct weston_desktop_surface *dsurface;
851                         const char *previous_active_app_id;
852
853                         dsurface = previous_active->dsurface;
854                         previous_active_app_id =
855                                 weston_desktop_surface_get_app_id(dsurface);
856                         ivi_layout_activate(ivi_output, previous_active_app_id);
857                 }
858         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
859                 struct weston_view *view  = surf->view;
860
861                 weston_layer_entry_remove(&view->layer_link);
862                 weston_view_geometry_dirty(view);
863                 weston_surface_damage(view->surface);
864         }
865 }