layout: Address all other callsites w/ dirty+surface damage
[src/agl-compositor.git] / src / layout.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28 #include "shared/helpers.h"
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include <libweston/libweston.h>
34 #include <libweston-desktop/libweston-desktop.h>
35
36 #include "agl-shell-desktop-server-protocol.h"
37
38 #define AGL_COMP_DEBUG
39
40 static const char *ivi_roles_as_string[] = {
41         [IVI_SURFACE_ROLE_NONE]         = "NONE",
42         [IVI_SURFACE_ROLE_BACKGROUND]   = "BACKGROUND",
43         [IVI_SURFACE_ROLE_PANEL]        = "PANEL",
44         [IVI_SURFACE_ROLE_DESKTOP]      = "DESKTOP",
45         [IVI_SURFACE_ROLE_POPUP]        = "POPUP",
46         [IVI_SURFACE_ROLE_SPLIT_H]      = "SPLIT_H",
47         [IVI_SURFACE_ROLE_SPLIT_V]      = "SPLIT_V",
48         [IVI_SURFACE_ROLE_FULLSCREEN]   = "FULLSCREEN",
49         [IVI_SURFACE_ROLE_REMOTE]       = "REMOTE",
50 };
51
52 const char *
53 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
54 {
55         if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
56                 return " unknown surface role";
57
58         return ivi_roles_as_string[surf->role];
59 }
60
61 static void
62 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
63 {
64         struct weston_output *woutput = output->output;
65         struct ivi_surface *bg = output->background;
66         struct weston_view *view;
67
68         if (!bg) {
69                 weston_log("WARNING: Output does not have a background\n");
70                 return;
71         }
72
73         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
74
75         view = bg->view;
76
77         weston_view_set_output(view, woutput);
78         weston_view_set_position(view, woutput->x, woutput->y);
79
80         weston_log("(background) position view %p, x %d, y %d, on output %s\n", view,
81                         woutput->x, woutput->y, output->name);
82
83         view->is_mapped = true;
84         view->surface->is_mapped = true;
85
86         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
87 }
88
89 static void
90 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
91                struct ivi_surface *panel)
92 {
93         struct weston_output *woutput = output->output;
94         struct weston_desktop_surface *dsurface;
95         struct weston_view *view;
96         struct weston_geometry geom;
97         int x = woutput->x;
98         int y = woutput->y;
99
100         if (!panel)
101                 return;
102
103         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
104         dsurface = panel->dsurface;
105         view = panel->view;
106         geom = weston_desktop_surface_get_geometry(dsurface);
107
108         weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
109                         geom.width, geom.height, geom.x, geom.y);
110
111         switch (panel->panel.edge) {
112         case AGL_SHELL_EDGE_TOP:
113                 output->area.y += geom.height;
114                 output->area.height -= geom.height;
115                 break;
116         case AGL_SHELL_EDGE_BOTTOM:
117                 y += woutput->height - geom.height;
118                 output->area.height -= geom.height;
119                 break;
120         case AGL_SHELL_EDGE_LEFT:
121                 output->area.x += geom.width;
122                 output->area.width -= geom.width;
123                 break;
124         case AGL_SHELL_EDGE_RIGHT:
125                 x += woutput->width - geom.width;
126                 output->area.width -= geom.width;
127                 break;
128         }
129
130         x -= geom.x;
131         y -= geom.y;
132
133         weston_view_set_output(view, woutput);
134         weston_view_set_position(view, x, y);
135
136         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
137                         panel->panel.edge, view, x, y);
138
139         view->is_mapped = true;
140         view->surface->is_mapped = true;
141
142         weston_log("panel type %d inited on output %s\n", panel->panel.edge,
143                         output->name);
144
145         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
146 }
147
148 /*
149  * Initializes all static parts of the layout, i.e. the background and panels.
150  */
151 void
152 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
153 {
154         ivi_background_init(ivi, output);
155
156         output->area.x = 0;
157         output->area.y = 0;
158         output->area.width = output->output->width;
159         output->area.height = output->output->height;
160
161         ivi_panel_init(ivi, output, output->top);
162         ivi_panel_init(ivi, output, output->bottom);
163         ivi_panel_init(ivi, output, output->left);
164         ivi_panel_init(ivi, output, output->right);
165
166         weston_compositor_schedule_repaint(ivi->compositor);
167
168         weston_log("Usable area: %dx%d+%d,%d\n",
169                    output->area.width, output->area.height,
170                    output->area.x, output->area.y);
171 }
172
173 struct ivi_surface *
174 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
175 {
176         struct ivi_surface *surf;
177         const char *id;
178
179         wl_list_for_each(surf, &ivi->surfaces, link) {
180                 id = weston_desktop_surface_get_app_id(surf->dsurface);
181                 if (id && strcmp(app_id, id) == 0)
182                         return surf;
183         }
184
185         return NULL;
186 }
187
188 static void
189 ivi_layout_activate_complete(struct ivi_output *output,
190                              struct ivi_surface *surf)
191 {
192         struct ivi_compositor *ivi = output->ivi;
193         struct weston_output *woutput = output->output;
194         struct weston_view *view = surf->view;
195
196         if (weston_view_is_mapped(view)) {
197                 weston_layer_entry_remove(&view->layer_link);
198         }
199
200         weston_view_set_output(view, woutput);
201         weston_view_set_position(view,
202                                  woutput->x + output->area.x,
203                                  woutput->y + output->area.y);
204
205         view->is_mapped = true;
206         view->surface->is_mapped = true;
207
208         if (output->active) {
209                 output->active->view->is_mapped = false;
210                 output->active->view->surface->is_mapped = false;
211
212                 weston_layer_entry_remove(&output->active->view->layer_link);
213         }
214         output->previous_active = output->active;
215         output->active = surf;
216
217         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
218         weston_view_geometry_dirty(view);
219         weston_surface_damage(view->surface);
220
221         /*
222          * the 'remote' role now makes use of this part so make sure we don't
223          * trip the enum such that we might end up with a modified output for
224          * 'remote' role
225          */
226         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
227                 if (surf->desktop.pending_output)
228                         surf->desktop.last_output = surf->desktop.pending_output;
229                 surf->desktop.pending_output = NULL;
230         }
231
232         weston_log("Activation completed for app_id %s, role %s, output %s\n",
233                         weston_desktop_surface_get_app_id(surf->dsurface),
234                         ivi_layout_get_surface_role_name(surf), output->name);
235 }
236
237 struct ivi_output *
238 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
239 {
240         struct ivi_output *out;
241
242         if (!app_id)
243                 return NULL;
244
245         wl_list_for_each(out, &ivi->outputs, link) {
246                 if (!out->app_id)
247                         continue;
248
249                 if (!strcmp(app_id, out->app_id))
250                         return out;
251         }
252
253         return NULL;
254 }
255
256
257 static struct ivi_output *
258 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
259 {
260         struct ivi_output *out;
261
262         wl_list_for_each(out, &ivi->outputs, link) {
263                 if (out->background &&
264                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
265                         return out;
266         }
267
268         return NULL;
269 }
270
271 void
272 ivi_layout_desktop_committed(struct ivi_surface *surf)
273 {
274         struct weston_desktop_surface *dsurf = surf->dsurface;
275         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
276         struct ivi_policy *policy = surf->ivi->policy;
277         struct ivi_output *output;
278         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
279
280         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
281                surf->role == IVI_SURFACE_ROLE_REMOTE);
282
283         /*
284          * we can't make use here of the ivi_layout_get_output_from_surface()
285          * due to the fact that we'll always land here when a surface performs
286          * a commit and pending_output will not bet set. This works in tandem
287          * with 'activated_by_default' at this point to avoid tripping over
288          * to a surface that continuously updates its content
289          */
290         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
291                 output = surf->desktop.pending_output;
292         else
293                 output = surf->remote.output;
294
295         if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
296                 struct ivi_output *r_output;
297
298                 if (policy && policy->api.surface_activate_by_default &&
299                     !policy->api.surface_activate_by_default(surf, surf->ivi))
300                         return;
301
302                 /* we can only activate it again by using the protocol */
303                 if (surf->activated_by_default)
304                         return;
305
306                 /* check first if there aren't any outputs being set */
307                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
308
309                 if (r_output) {
310                         struct weston_view *view = r_output->fullscreen_view.fs->view;
311                         if (view->is_mapped || view->surface->is_mapped)
312                                 remove_black_surface(r_output);
313                 }
314
315
316                 /* try finding an output with a background and use that */
317                 if (!r_output)
318                         r_output = ivi_layout_find_bg_output(surf->ivi);
319
320                 /* if we couldn't still find an output by this point, there's
321                  * something wrong so we abort with a protocol error */
322                 if (!r_output) {
323                         wl_resource_post_error(surf->ivi->shell_client.resource,
324                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
325                                                "No valid output found to activate surface by default");
326                         return;
327                 }
328
329                 if (!surf->ivi->activate_by_default) {
330                         weston_log("Refusing to activate surface role %d, app_id %s\n",
331                                         surf->role, app_id);
332                         return;
333                 }
334
335                 /* use the output of the bg to activate the app on start-up by
336                  * default */
337                 if (surf->view && r_output) {
338                         if (app_id && r_output) {
339                                 weston_log("Surface with app_id %s, role %s activating by default\n",
340                                         weston_desktop_surface_get_app_id(surf->dsurface),
341                                         ivi_layout_get_surface_role_name(surf));
342                                 ivi_layout_activate(r_output, app_id);
343                                 surf->activated_by_default = true;
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                                 surf->activated_by_default = true;
356                         }
357                 }
358
359                 return;
360         }
361
362         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
363                 if (policy && policy->api.surface_activate_by_default &&
364                     !policy->api.surface_activate_by_default(surf, surf->ivi))
365                         return;
366
367                 /* we can only activate it again by using the protocol, but
368                  * additionally the output is not reset when
369                  * ivi_layout_activate_complete() terminates so we use the
370                  * current active surface to avoid hitting this again and again
371                  * */
372                 if (surf->activated_by_default && output->active == surf)
373                         return;
374
375                 if (app_id) {
376                         weston_log("Surface with app_id %s, role %s activating by default\n",
377                                         weston_desktop_surface_get_app_id(surf->dsurface),
378                                         ivi_layout_get_surface_role_name(surf));
379                         ivi_layout_activate(output, app_id);
380                         surf->activated_by_default = true;
381                 }
382                 return;
383         }
384
385         if (!weston_desktop_surface_get_maximized(dsurf) ||
386             geom.width != output->area.width ||
387             geom.height != output->area.height)
388                 return;
389
390         ivi_layout_activate_complete(output, surf);
391 }
392
393 void
394 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
395 {
396         struct ivi_compositor *ivi = surface->ivi;
397         struct ivi_policy *policy = ivi->policy;
398
399         struct weston_desktop_surface *dsurface = surface->dsurface;
400         struct weston_surface *wsurface =
401                 weston_desktop_surface_get_surface(dsurface);
402         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
403
404         struct ivi_output *output = surface->split.output;
405         struct weston_output *woutput = output->output;
406
407         struct weston_view *view = surface->view;
408         struct weston_geometry geom;
409
410         if (policy && policy->api.surface_activate_by_default &&
411             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
412             !surface->activated_by_default)
413                 return;
414
415         if (surface->view->is_mapped)
416                 return;
417
418         geom = weston_desktop_surface_get_geometry(dsurface);
419         weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
420                         geom.width, geom.height);
421
422         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
423
424         weston_desktop_surface_set_fullscreen(dsurface, true);
425
426         weston_view_set_output(view, woutput);
427         weston_view_set_position(view, woutput->x, woutput->y);
428         weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
429
430         weston_view_geometry_dirty(view);
431         weston_surface_damage(view->surface);
432
433         wsurface->is_mapped = true;
434         surface->view->is_mapped = true;
435
436         shell_advertise_app_state(ivi, app_id,
437                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
438
439         weston_log("Activation completed for app_id %s, role %s, output %s\n",
440                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
441 }
442
443 void
444 ivi_layout_desktop_resize(struct ivi_surface *surface,
445                           struct weston_geometry area)
446 {
447         struct weston_desktop_surface *dsurf = surface->dsurface;
448         struct weston_view *view = surface->view;
449
450         int x = area.x;
451         int y = area.y;
452         int width = area.width;
453         int height = area.height;
454
455         weston_desktop_surface_set_size(dsurf,
456                                         width, height);
457
458         weston_view_set_position(view, x, y);
459
460         weston_view_geometry_dirty(view);
461         weston_surface_damage(view->surface);
462 }
463
464 void
465 ivi_layout_split_committed(struct ivi_surface *surface)
466 {
467         struct ivi_compositor *ivi = surface->ivi;
468         struct ivi_policy *policy = ivi->policy;
469
470         struct weston_desktop_surface *dsurface = surface->dsurface;
471         struct weston_surface *wsurface =
472                 weston_desktop_surface_get_surface(dsurface);
473         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
474
475         struct ivi_output *output = surface->split.output;
476         struct weston_output *woutput = output->output;
477
478         struct weston_view *view = surface->view;
479         struct weston_geometry geom;
480
481         int x, y;
482         int width, height;
483
484         x = woutput->x;
485         y = woutput->y;
486
487         if (policy && policy->api.surface_activate_by_default &&
488             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
489             !surface->activated_by_default)
490                 return;
491
492         if (surface->view->is_mapped)
493                 return;
494
495         geom = weston_desktop_surface_get_geometry(dsurface);
496
497         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
498                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
499
500         /* save the previous area in order to recover it back when if this kind
501          * of surface is being destroyed/removed */
502         output->area_saved = output->area;
503
504         switch (surface->role) {
505         case IVI_SURFACE_ROLE_SPLIT_V:
506                 geom.width = (output->area.width / 2);
507
508                 x += woutput->width - geom.width;
509                 output->area.width -= geom.width;
510
511                 width = woutput->width - x;
512                 height = output->area.height;
513                 y = output->area.y;
514
515                 break;
516         case IVI_SURFACE_ROLE_SPLIT_H:
517                 geom.height = (output->area.height / 2);
518
519                 y = output->area.y;
520                 output->area.y += geom.height;
521                 output->area.height -= geom.height;
522
523                 width = output->area.width;
524                 height = output->area.height;
525
526                 x = output->area.x;
527
528                 break;
529         default:
530                 assert(!"Invalid split orientation\n");
531         }
532
533         weston_desktop_surface_set_size(dsurface,
534                                         width, height);
535
536         /* resize the active surface first, output->area already contains
537          * correct area to resize to */
538         if (output->active)
539                 ivi_layout_desktop_resize(output->active, output->area);
540
541         weston_view_set_output(view, woutput);
542         weston_view_set_position(view, x, y);
543         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
544
545         weston_view_geometry_dirty(view);
546         weston_surface_damage(view->surface);
547
548         wsurface->is_mapped = true;
549         surface->view->is_mapped = true;
550
551         shell_advertise_app_state(ivi, app_id,
552                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
553
554         weston_log("Activation completed for app_id %s, role %s, output %s\n",
555                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
556 }
557
558 static void
559 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
560                            int initial_x, int initial_y, int *new_x, int *new_y)
561 {
562         *new_x = output->x + initial_x;
563         *new_y = output->y + initial_y;
564 }
565
566
567 void
568 ivi_layout_popup_committed(struct ivi_surface *surface)
569 {
570         struct ivi_compositor *ivi = surface->ivi;
571         struct ivi_policy *policy = ivi->policy;
572
573         struct weston_desktop_surface *dsurface = surface->dsurface;
574         struct weston_surface *wsurface =
575                 weston_desktop_surface_get_surface(dsurface);
576         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
577
578         int new_x, new_y;
579
580         struct ivi_output *output = surface->popup.output;
581         struct weston_output *woutput = output->output;
582
583         struct weston_view *view = surface->view;
584
585         if (policy && policy->api.surface_activate_by_default &&
586             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
587             !surface->activated_by_default)
588                 return;
589
590         if (surface->view->is_mapped)
591                 return;
592
593         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
594
595         weston_view_set_output(view, woutput);
596
597         ivi_compute_popup_position(woutput, view,
598                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
599         weston_view_set_position(view, new_x, new_y);
600
601         /* only clip the pop-up dialog window if we have a valid
602          * width and height being passed on. Users might not want to have one
603          * set-up so only enfore it is really passed on. */
604         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
605                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
606                                      surface->popup.bb.width, surface->popup.bb.height);
607
608         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
609
610         weston_view_geometry_dirty(view);
611         weston_surface_damage(view->surface);
612
613         wsurface->is_mapped = true;
614         surface->view->is_mapped = true;
615
616         shell_advertise_app_state(ivi, app_id,
617                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
618
619         weston_log("Activation completed for app_id %s, role %s, output %s\n",
620                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
621 }
622
623 static void
624 ivi_layout_popup_re_add(struct ivi_surface *surface)
625 {
626         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
627         struct weston_view *view = surface->view;
628
629         if (weston_view_is_mapped(view)) {
630                 struct weston_desktop_surface *dsurface = surface->dsurface;
631                 struct weston_surface *wsurface =
632                         weston_desktop_surface_get_surface(dsurface);
633
634                 weston_layer_entry_remove(&view->layer_link);
635
636                 wsurface->is_mapped = false;
637                 view->is_mapped = false;
638         }
639
640         /* reset the activate by default in order to (still) allow the surface
641          * to be activaved using the request */
642         if (!surface->activated_by_default)
643                 surface->activated_by_default = true;
644
645         ivi_layout_popup_committed(surface);
646 }
647
648 static bool
649 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
650 {
651         struct ivi_compositor *ivi = surf->ivi;
652         struct ivi_surface *is;
653
654         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
655             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
656             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
657                 return false;
658
659         /* reset the activate by default in order to (still) allow the surface
660          * to be activaved using the request */
661         if (!surf->activated_by_default)
662                 surf->activated_by_default = true;
663
664         wl_list_for_each(is, &ivi->surfaces, link)
665                 if (is == surf)
666                         return true;
667
668         return false;
669 }
670
671 void
672 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
673 {
674         struct ivi_compositor *ivi = output->ivi;
675         struct weston_desktop_surface *dsurf;
676         struct weston_view *view;
677         struct weston_geometry geom;
678         struct ivi_policy *policy = output->ivi->policy;
679
680         dsurf = surf->dsurface;
681         view = surf->view;
682
683         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
684
685         if (!surf)
686                 return;
687
688         if (policy && policy->api.surface_activate &&
689             !policy->api.surface_activate(surf, surf->ivi)) {
690                 return;
691         }
692
693 #ifdef AGL_COMP_DEBUG
694         weston_log("Activating app_id %s, type %s\n", app_id,
695                         ivi_layout_get_surface_role_name(surf));
696 #endif
697
698         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
699                 ivi_layout_popup_re_add(surf);
700                 return;
701         }
702
703         /* do not 're'-activate surfaces that are split or active */
704         if (surf == output->active ||
705             ivi_layout_surface_is_split_or_fullscreen(surf))
706                 return;
707
708         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
709                 struct ivi_output *remote_output =
710                         ivi_layout_find_with_app_id(app_id, ivi);
711
712                 /* if already active on a remote output do not
713                  * attempt to activate it again */
714                 if (remote_output && remote_output->active == surf)
715                         return;
716         }
717
718
719         geom = weston_desktop_surface_get_geometry(dsurf);
720
721         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
722                 surf->desktop.pending_output = output;
723         if (weston_desktop_surface_get_maximized(dsurf) &&
724             geom.width == output->area.width &&
725             geom.height == output->area.height) {
726                 ivi_layout_activate_complete(output, surf);
727                 return;
728         }
729
730         weston_desktop_surface_set_maximized(dsurf, true);
731         weston_desktop_surface_set_size(dsurf,
732                                         output->area.width,
733                                         output->area.height);
734
735         weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
736                         app_id, ivi_layout_get_surface_role_name(surf),
737                         output->area.width, output->area.height);
738         /*
739          * If the view isn't mapped, we put it onto the hidden layer so it will
740          * start receiving frame events, and will be able to act on our
741          * configure event.
742          */
743         if (!weston_view_is_mapped(view)) {
744                 view->is_mapped = true;
745                 view->surface->is_mapped = true;
746
747                 weston_view_set_output(view, output->output);
748                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
749                 weston_log("Placed app_id %s, type %s in hidden layer\n",
750                                 app_id, ivi_layout_get_surface_role_name(surf));
751         }
752 }
753
754 void
755 ivi_layout_activate(struct ivi_output *output, const char *app_id)
756 {
757         struct ivi_surface *surf;
758         struct ivi_compositor *ivi = output->ivi;
759
760         if (!app_id)
761                 return;
762
763         surf = ivi_find_app(ivi, app_id);
764         if (!surf)
765                 return;
766
767         ivi_layout_activate_by_surf(output, surf);
768 }
769
770 struct ivi_output *
771 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
772 {
773         struct ivi_output *ivi_output = NULL;
774
775         switch (surf->role) {
776         case IVI_SURFACE_ROLE_DESKTOP:
777                 if (surf->desktop.pending_output)
778                         ivi_output = surf->desktop.pending_output;
779                 else
780                         ivi_output = surf->desktop.last_output;
781                 break;
782         case IVI_SURFACE_ROLE_POPUP:
783                 ivi_output = surf->popup.output;
784                 break;
785         case IVI_SURFACE_ROLE_BACKGROUND:
786                 ivi_output = surf->bg.output;
787                 break;
788         case IVI_SURFACE_ROLE_PANEL:
789                 ivi_output = surf->panel.output;
790                 break;
791         case IVI_SURFACE_ROLE_FULLSCREEN:
792                 ivi_output = surf->fullscreen.output;
793                 break;
794         case IVI_SURFACE_ROLE_SPLIT_H:
795         case IVI_SURFACE_ROLE_SPLIT_V:
796                 ivi_output = surf->split.output;
797                 break;
798         case IVI_SURFACE_ROLE_REMOTE:
799                 ivi_output = surf->remote.output;
800                 break;
801         case IVI_SURFACE_ROLE_NONE:
802         default:
803                 break;
804         }
805
806         return ivi_output;
807 }
808
809 void
810 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
811 {
812         struct ivi_surface *surf;
813         struct ivi_output *ivi_output;
814         struct ivi_policy *policy = ivi->policy;
815
816         if (!app_id)
817                 return;
818
819         surf = ivi_find_app(ivi, app_id);
820         if (!surf)
821                 return;
822
823         if (policy && policy->api.surface_deactivate &&
824             !policy->api.surface_deactivate(surf, surf->ivi)) {
825                 return;
826         }
827
828         ivi_output = ivi_layout_get_output_from_surface(surf);
829         weston_log("Deactiving %s, role %s\n", app_id,
830                         ivi_layout_get_surface_role_name(surf));
831
832         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
833                 struct ivi_surface *previous_active;
834
835                 previous_active = ivi_output->previous_active;
836                 if (!previous_active) {
837                         /* we don't have a previous active it means we should
838                          * display the bg */
839                         if (ivi_output->active) {
840                                 struct weston_view *view;
841
842                                 view = ivi_output->active->view;
843                                 view->is_mapped = false;
844                                 view->surface->is_mapped = false;
845
846                                 weston_layer_entry_remove(&view->layer_link);
847                                 weston_view_geometry_dirty(view);
848                                 weston_surface_damage(view->surface);
849                                 ivi_output->active = NULL;
850                         }
851                 } else {
852                         struct weston_desktop_surface *dsurface;
853                         const char *previous_active_app_id;
854
855                         dsurface = previous_active->dsurface;
856                         previous_active_app_id =
857                                 weston_desktop_surface_get_app_id(dsurface);
858                         ivi_layout_activate(ivi_output, previous_active_app_id);
859                 }
860         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
861                 struct weston_view *view  = surf->view;
862
863                 weston_layer_entry_remove(&view->layer_link);
864                 weston_view_geometry_dirty(view);
865                 weston_surface_damage(view->surface);
866         }
867 }