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