layout: Remove weston_output_damage dependecy
[src/agl-compositor.git] / src / layout.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28 #include "shared/helpers.h"
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include <libweston/libweston.h>
34 #include <libweston-desktop/libweston-desktop.h>
35
36 #include "agl-shell-desktop-server-protocol.h"
37
38 #define AGL_COMP_DEBUG
39
40 static const char *ivi_roles_as_string[] = {
41         [IVI_SURFACE_ROLE_NONE]         = "NONE",
42         [IVI_SURFACE_ROLE_BACKGROUND]   = "BACKGROUND",
43         [IVI_SURFACE_ROLE_PANEL]        = "PANEL",
44         [IVI_SURFACE_ROLE_DESKTOP]      = "DESKTOP",
45         [IVI_SURFACE_ROLE_POPUP]        = "POPUP",
46         [IVI_SURFACE_ROLE_SPLIT_H]      = "SPLIT_H",
47         [IVI_SURFACE_ROLE_SPLIT_V]      = "SPLIT_V",
48         [IVI_SURFACE_ROLE_FULLSCREEN]   = "FULLSCREEN",
49         [IVI_SURFACE_ROLE_REMOTE]       = "REMOTE",
50 };
51
52 const char *
53 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
54 {
55         if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
56                 return " unknown surface role";
57
58         return ivi_roles_as_string[surf->role];
59 }
60
61 static void
62 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
63 {
64         struct weston_output *woutput = output->output;
65         struct ivi_surface *bg = output->background;
66         struct weston_view *view;
67
68         if (!bg) {
69                 weston_log("WARNING: Output does not have a background\n");
70                 return;
71         }
72
73         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
74
75         view = bg->view;
76
77         weston_view_set_output(view, woutput);
78         weston_view_set_position(view, woutput->x, woutput->y);
79
80         weston_log("(background) position view %p, x %d, y %d, on output %s\n", view,
81                         woutput->x, woutput->y, output->name);
82
83         view->is_mapped = true;
84         view->surface->is_mapped = true;
85
86         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
87 }
88
89 static void
90 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
91                struct ivi_surface *panel)
92 {
93         struct weston_output *woutput = output->output;
94         struct weston_desktop_surface *dsurface;
95         struct weston_view *view;
96         struct weston_geometry geom;
97         int x = woutput->x;
98         int y = woutput->y;
99
100         if (!panel)
101                 return;
102
103         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
104         dsurface = panel->dsurface;
105         view = panel->view;
106         geom = weston_desktop_surface_get_geometry(dsurface);
107
108         weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
109                         geom.width, geom.height, geom.x, geom.y);
110
111         switch (panel->panel.edge) {
112         case AGL_SHELL_EDGE_TOP:
113                 output->area.y += geom.height;
114                 output->area.height -= geom.height;
115                 break;
116         case AGL_SHELL_EDGE_BOTTOM:
117                 y += woutput->height - geom.height;
118                 output->area.height -= geom.height;
119                 break;
120         case AGL_SHELL_EDGE_LEFT:
121                 output->area.x += geom.width;
122                 output->area.width -= geom.width;
123                 break;
124         case AGL_SHELL_EDGE_RIGHT:
125                 x += woutput->width - geom.width;
126                 output->area.width -= geom.width;
127                 break;
128         }
129
130         x -= geom.x;
131         y -= geom.y;
132
133         weston_view_set_output(view, woutput);
134         weston_view_set_position(view, x, y);
135
136         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
137                         panel->panel.edge, view, x, y);
138
139         view->is_mapped = true;
140         view->surface->is_mapped = true;
141
142         weston_log("panel type %d inited on output %s\n", panel->panel.edge,
143                         output->name);
144
145         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
146 }
147
148 /*
149  * Initializes all static parts of the layout, i.e. the background and panels.
150  */
151 void
152 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
153 {
154         ivi_background_init(ivi, output);
155
156         output->area.x = 0;
157         output->area.y = 0;
158         output->area.width = output->output->width;
159         output->area.height = output->output->height;
160
161         ivi_panel_init(ivi, output, output->top);
162         ivi_panel_init(ivi, output, output->bottom);
163         ivi_panel_init(ivi, output, output->left);
164         ivi_panel_init(ivi, output, output->right);
165
166         weston_compositor_schedule_repaint(ivi->compositor);
167
168         weston_log("Usable area: %dx%d+%d,%d\n",
169                    output->area.width, output->area.height,
170                    output->area.x, output->area.y);
171 }
172
173 struct ivi_surface *
174 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
175 {
176         struct ivi_surface *surf;
177         const char *id;
178
179         wl_list_for_each(surf, &ivi->surfaces, link) {
180                 id = weston_desktop_surface_get_app_id(surf->dsurface);
181                 if (id && strcmp(app_id, id) == 0)
182                         return surf;
183         }
184
185         return NULL;
186 }
187
188 static void
189 ivi_layout_activate_complete(struct ivi_output *output,
190                              struct ivi_surface *surf)
191 {
192         struct ivi_compositor *ivi = output->ivi;
193         struct weston_output *woutput = output->output;
194         struct weston_view *view = surf->view;
195
196         if (weston_view_is_mapped(view)) {
197                 weston_layer_entry_remove(&view->layer_link);
198         }
199
200         weston_view_set_output(view, woutput);
201         weston_view_set_position(view,
202                                  woutput->x + output->area.x,
203                                  woutput->y + output->area.y);
204
205         view->is_mapped = true;
206         view->surface->is_mapped = true;
207
208         if (output->active) {
209                 output->active->view->is_mapped = false;
210                 output->active->view->surface->is_mapped = false;
211
212                 weston_layer_entry_remove(&output->active->view->layer_link);
213         }
214         output->previous_active = output->active;
215         output->active = surf;
216
217         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
218         weston_view_update_transform(view);
219
220         weston_view_damage_below(view);
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 'activated_by_default' 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->activated_by_default)
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                                 surf->activated_by_default = true;
345                         } else if (!app_id) {
346                                 /*
347                                  * applications not setting an app_id, or
348                                  * setting an app_id but at a later point in
349                                  * time, might fall-back here so give them a
350                                  * chance to receive the configure event and
351                                  * act upon it
352                                  */
353                                 weston_log("Surface no app_id, role %s activating by default\n",
354                                         ivi_layout_get_surface_role_name(surf));
355                                 ivi_layout_activate_by_surf(r_output, surf);
356                                 surf->activated_by_default = true;
357                         }
358                 }
359
360                 return;
361         }
362
363         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
364                 if (policy && policy->api.surface_activate_by_default &&
365                     !policy->api.surface_activate_by_default(surf, surf->ivi))
366                         return;
367
368                 /* we can only activate it again by using the protocol, but
369                  * additionally the output is not reset when
370                  * ivi_layout_activate_complete() terminates so we use the
371                  * current active surface to avoid hitting this again and again
372                  * */
373                 if (surf->activated_by_default && output->active == surf)
374                         return;
375
376                 if (app_id) {
377                         weston_log("Surface with app_id %s, role %s activating by default\n",
378                                         weston_desktop_surface_get_app_id(surf->dsurface),
379                                         ivi_layout_get_surface_role_name(surf));
380                         ivi_layout_activate(output, app_id);
381                         surf->activated_by_default = true;
382                 }
383                 return;
384         }
385
386         if (!weston_desktop_surface_get_maximized(dsurf) ||
387             geom.width != output->area.width ||
388             geom.height != output->area.height)
389                 return;
390
391         ivi_layout_activate_complete(output, surf);
392 }
393
394 void
395 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
396 {
397         struct ivi_compositor *ivi = surface->ivi;
398         struct ivi_policy *policy = ivi->policy;
399
400         struct weston_desktop_surface *dsurface = surface->dsurface;
401         struct weston_surface *wsurface =
402                 weston_desktop_surface_get_surface(dsurface);
403         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
404
405         struct ivi_output *output = surface->split.output;
406         struct weston_output *woutput = output->output;
407
408         struct weston_view *view = surface->view;
409         struct weston_geometry geom;
410
411         if (policy && policy->api.surface_activate_by_default &&
412             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
413             !surface->activated_by_default)
414                 return;
415
416         if (surface->view->is_mapped)
417                 return;
418
419         geom = weston_desktop_surface_get_geometry(dsurface);
420         weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
421                         geom.width, geom.height);
422
423         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
424
425         weston_desktop_surface_set_fullscreen(dsurface, true);
426
427         weston_view_set_output(view, woutput);
428         weston_view_set_position(view, woutput->x, woutput->y);
429         weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
430
431         weston_view_update_transform(view);
432         weston_view_damage_below(view);
433
434         wsurface->is_mapped = true;
435         surface->view->is_mapped = true;
436
437         shell_advertise_app_state(ivi, app_id,
438                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
439
440         weston_log("Activation completed for app_id %s, role %s, output %s\n",
441                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
442 }
443
444 void
445 ivi_layout_desktop_resize(struct ivi_surface *surface,
446                           struct weston_geometry area)
447 {
448         struct weston_desktop_surface *dsurf = surface->dsurface;
449         struct weston_view *view = surface->view;
450
451         int x = area.x;
452         int y = area.y;
453         int width = area.width;
454         int height = area.height;
455
456         weston_desktop_surface_set_size(dsurf,
457                                         width, height);
458
459         weston_view_set_position(view, x, y);
460         weston_view_update_transform(view);
461         weston_view_damage_below(view);
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_update_transform(view);
546         weston_view_damage_below(view);
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 void
559 ivi_layout_popup_committed(struct ivi_surface *surface)
560 {
561         struct ivi_compositor *ivi = surface->ivi;
562         struct ivi_policy *policy = ivi->policy;
563
564         struct weston_desktop_surface *dsurface = surface->dsurface;
565         struct weston_surface *wsurface =
566                 weston_desktop_surface_get_surface(dsurface);
567         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
568
569         struct ivi_output *output = surface->popup.output;
570         struct weston_output *woutput = output->output;
571
572         struct weston_view *view = surface->view;
573
574         if (policy && policy->api.surface_activate_by_default &&
575             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
576             !surface->activated_by_default)
577                 return;
578
579         if (surface->view->is_mapped)
580                 return;
581
582         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
583
584         weston_view_set_output(view, woutput);
585         weston_view_set_position(view, surface->popup.x, surface->popup.y);
586
587         /* only clip the pop-up dialog window if we have a valid
588          * width and height being passed on. Users might not want to have one
589          * set-up so only enfore it is really passed on. */
590         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
591                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
592                                      surface->popup.bb.width, surface->popup.bb.height);
593
594         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
595
596         weston_view_update_transform(view);
597         weston_view_damage_below(view);
598
599         wsurface->is_mapped = true;
600         surface->view->is_mapped = true;
601
602         shell_advertise_app_state(ivi, app_id,
603                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
604
605         weston_log("Activation completed for app_id %s, role %s, output %s\n",
606                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
607 }
608
609 static void
610 ivi_layout_popup_re_add(struct ivi_surface *surface)
611 {
612         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
613         struct weston_view *view = surface->view;
614
615         if (weston_view_is_mapped(view)) {
616                 struct weston_desktop_surface *dsurface = surface->dsurface;
617                 struct weston_surface *wsurface =
618                         weston_desktop_surface_get_surface(dsurface);
619
620                 weston_layer_entry_remove(&view->layer_link);
621
622                 wsurface->is_mapped = false;
623                 view->is_mapped = false;
624         }
625
626         /* reset the activate by default in order to (still) allow the surface
627          * to be activaved using the request */
628         if (!surface->activated_by_default)
629                 surface->activated_by_default = true;
630
631         ivi_layout_popup_committed(surface);
632 }
633
634 static bool
635 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
636 {
637         struct ivi_compositor *ivi = surf->ivi;
638         struct ivi_surface *is;
639
640         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
641             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
642             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
643                 return false;
644
645         /* reset the activate by default in order to (still) allow the surface
646          * to be activaved using the request */
647         if (!surf->activated_by_default)
648                 surf->activated_by_default = true;
649
650         wl_list_for_each(is, &ivi->surfaces, link)
651                 if (is == surf)
652                         return true;
653
654         return false;
655 }
656
657 void
658 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
659 {
660         struct ivi_compositor *ivi = output->ivi;
661         struct weston_desktop_surface *dsurf;
662         struct weston_view *view;
663         struct weston_geometry geom;
664         struct ivi_policy *policy = output->ivi->policy;
665
666         dsurf = surf->dsurface;
667         view = surf->view;
668
669         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
670
671         if (!surf)
672                 return;
673
674         if (policy && policy->api.surface_activate &&
675             !policy->api.surface_activate(surf, surf->ivi)) {
676                 return;
677         }
678
679 #ifdef AGL_COMP_DEBUG
680         weston_log("Activating app_id %s, type %s\n", app_id,
681                         ivi_layout_get_surface_role_name(surf));
682 #endif
683
684         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
685                 ivi_layout_popup_re_add(surf);
686                 return;
687         }
688
689         /* do not 're'-activate surfaces that are split or active */
690         if (surf == output->active ||
691             ivi_layout_surface_is_split_or_fullscreen(surf))
692                 return;
693
694         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
695                 struct ivi_output *remote_output =
696                         ivi_layout_find_with_app_id(app_id, ivi);
697
698                 /* if already active on a remote output do not
699                  * attempt to activate it again */
700                 if (remote_output && remote_output->active == surf)
701                         return;
702         }
703
704
705         geom = weston_desktop_surface_get_geometry(dsurf);
706
707         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
708                 surf->desktop.pending_output = output;
709         if (weston_desktop_surface_get_maximized(dsurf) &&
710             geom.width == output->area.width &&
711             geom.height == output->area.height) {
712                 ivi_layout_activate_complete(output, surf);
713                 return;
714         }
715
716         weston_desktop_surface_set_maximized(dsurf, true);
717         weston_desktop_surface_set_size(dsurf,
718                                         output->area.width,
719                                         output->area.height);
720
721         weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
722                         app_id, ivi_layout_get_surface_role_name(surf),
723                         output->area.width, output->area.height);
724         /*
725          * If the view isn't mapped, we put it onto the hidden layer so it will
726          * start receiving frame events, and will be able to act on our
727          * configure event.
728          */
729         if (!weston_view_is_mapped(view)) {
730                 view->is_mapped = true;
731                 view->surface->is_mapped = true;
732
733                 weston_view_set_output(view, output->output);
734                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
735                 /* force repaint of the entire output */
736
737                 weston_log("Placed app_id %s, type %s in hidden layer\n",
738                                 app_id, ivi_layout_get_surface_role_name(surf));
739         }
740 }
741
742 void
743 ivi_layout_activate(struct ivi_output *output, const char *app_id)
744 {
745         struct ivi_surface *surf;
746         struct ivi_compositor *ivi = output->ivi;
747
748         if (!app_id)
749                 return;
750
751         surf = ivi_find_app(ivi, app_id);
752         if (!surf)
753                 return;
754
755         ivi_layout_activate_by_surf(output, surf);
756 }
757
758 struct ivi_output *
759 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
760 {
761         struct ivi_output *ivi_output = NULL;
762
763         switch (surf->role) {
764         case IVI_SURFACE_ROLE_DESKTOP:
765                 if (surf->desktop.pending_output)
766                         ivi_output = surf->desktop.pending_output;
767                 else
768                         ivi_output = surf->desktop.last_output;
769                 break;
770         case IVI_SURFACE_ROLE_POPUP:
771                 ivi_output = surf->popup.output;
772                 break;
773         case IVI_SURFACE_ROLE_BACKGROUND:
774                 ivi_output = surf->bg.output;
775                 break;
776         case IVI_SURFACE_ROLE_PANEL:
777                 ivi_output = surf->panel.output;
778                 break;
779         case IVI_SURFACE_ROLE_FULLSCREEN:
780                 ivi_output = surf->fullscreen.output;
781                 break;
782         case IVI_SURFACE_ROLE_SPLIT_H:
783         case IVI_SURFACE_ROLE_SPLIT_V:
784                 ivi_output = surf->split.output;
785                 break;
786         case IVI_SURFACE_ROLE_REMOTE:
787                 ivi_output = surf->remote.output;
788                 break;
789         case IVI_SURFACE_ROLE_NONE:
790         default:
791                 break;
792         }
793
794         return ivi_output;
795 }
796
797 void
798 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
799 {
800         struct ivi_surface *surf;
801         struct ivi_output *ivi_output;
802         struct ivi_policy *policy = ivi->policy;
803
804         if (!app_id)
805                 return;
806
807         surf = ivi_find_app(ivi, app_id);
808         if (!surf)
809                 return;
810
811         if (policy && policy->api.surface_deactivate &&
812             !policy->api.surface_deactivate(surf, surf->ivi)) {
813                 return;
814         }
815
816         ivi_output = ivi_layout_get_output_from_surface(surf);
817         weston_log("Deactiving %s, role %s\n", app_id,
818                         ivi_layout_get_surface_role_name(surf));
819
820         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
821                 struct ivi_surface *previous_active;
822
823                 previous_active = ivi_output->previous_active;
824                 if (!previous_active) {
825                         /* we don't have a previous active it means we should
826                          * display the bg */
827                         if (ivi_output->active) {
828                                 struct weston_view *view;
829
830                                 view = ivi_output->active->view;
831                                 view->is_mapped = false;
832                                 view->surface->is_mapped = false;
833
834                                 weston_layer_entry_remove(&view->layer_link);
835                                 weston_view_damage_below(view);
836                                 ivi_output->active = NULL;
837                         }
838                 } else {
839                         struct weston_desktop_surface *dsurface;
840                         const char *previous_active_app_id;
841
842                         dsurface = previous_active->dsurface;
843                         previous_active_app_id =
844                                 weston_desktop_surface_get_app_id(dsurface);
845                         ivi_layout_activate(ivi_output, previous_active_app_id);
846                 }
847         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
848                 struct weston_view *view  = surf->view;
849
850                 weston_layer_entry_remove(&view->layer_link);
851                 weston_view_damage_below(view);
852         }
853 }