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