829e1d57fa3c4a92777a2908a80204cf27b41ca4
[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         } else {
199                 weston_view_update_transform(view);
200         }
201
202         weston_view_set_output(view, woutput);
203         weston_view_set_position(view,
204                                  woutput->x + output->area.x,
205                                  woutput->y + output->area.y);
206
207         view->is_mapped = true;
208         surf->mapped = true;
209         view->surface->is_mapped = true;
210
211         if (output->active) {
212                 output->active->view->is_mapped = false;
213                 output->active->view->surface->is_mapped = false;
214
215                 weston_layer_entry_remove(&output->active->view->layer_link);
216         }
217         output->previous_active = output->active;
218         output->active = surf;
219
220         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
221         weston_view_geometry_dirty(view);
222         weston_surface_damage(view->surface);
223
224         /*
225          * the 'remote' role now makes use of this part so make sure we don't
226          * trip the enum such that we might end up with a modified output for
227          * 'remote' role
228          */
229         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
230                 if (surf->desktop.pending_output)
231                         surf->desktop.last_output = surf->desktop.pending_output;
232                 surf->desktop.pending_output = NULL;
233         }
234
235         weston_log("Activation completed for app_id %s, role %s, output %s\n",
236                         weston_desktop_surface_get_app_id(surf->dsurface),
237                         ivi_layout_get_surface_role_name(surf), output->name);
238 }
239
240 struct ivi_output *
241 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
242 {
243         struct ivi_output *out;
244
245         if (!app_id)
246                 return NULL;
247
248         wl_list_for_each(out, &ivi->outputs, link) {
249                 if (!out->app_id)
250                         continue;
251
252                 if (!strcmp(app_id, out->app_id))
253                         return out;
254         }
255
256         return NULL;
257 }
258
259 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 'mapped' 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->mapped)
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                         } 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                         }
357                 }
358
359                 return;
360         }
361
362         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
363                 if (policy && policy->api.surface_activate_by_default &&
364                     !policy->api.surface_activate_by_default(surf, surf->ivi))
365                         return;
366
367                 /* we can only activate it again by using the protocol, but
368                  * additionally the output is not reset when
369                  * ivi_layout_activate_complete() terminates so we use the
370                  * current active surface to avoid hitting this again and again
371                  * */
372                 if (surf->mapped && output->active == surf)
373                         return;
374
375                 if (app_id) {
376                         weston_log("Surface with app_id %s, role %s activating by default\n",
377                                         weston_desktop_surface_get_app_id(surf->dsurface),
378                                         ivi_layout_get_surface_role_name(surf));
379                         ivi_layout_activate(output, app_id);
380                 }
381                 return;
382         }
383
384         if (!weston_desktop_surface_get_maximized(dsurf) ||
385             geom.width != output->area.width ||
386             geom.height != output->area.height)
387                 return;
388
389         ivi_layout_activate_complete(output, surf);
390 }
391
392 void
393 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
394 {
395         struct ivi_compositor *ivi = surface->ivi;
396         struct ivi_policy *policy = ivi->policy;
397
398         struct weston_desktop_surface *dsurface = surface->dsurface;
399         struct weston_surface *wsurface =
400                 weston_desktop_surface_get_surface(dsurface);
401         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
402
403         struct ivi_output *output = surface->split.output;
404         struct weston_output *woutput = output->output;
405         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
406
407         struct weston_view *view = surface->view;
408         struct weston_geometry geom =
409                 weston_desktop_surface_get_geometry(dsurface);
410
411         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
412         bool is_dim_same =
413                 geom.width == bg_output->output->width &&
414                 geom.height == bg_output->output->height;
415
416         if (policy && policy->api.surface_activate_by_default &&
417             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
418             !surface->mapped)
419                 return;
420
421         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
422
423         if (weston_view_is_mapped(view))
424                 return;
425
426         /* if we still get here but we haven't resized so far, send configure
427          * events to do so */
428         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
429                 struct ivi_output *bg_output =
430                         ivi_layout_find_bg_output(surface->ivi);
431
432                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
433                                 app_id, ivi_layout_get_surface_role_name(surface));
434                 weston_desktop_surface_set_fullscreen(dsurface, true);
435                 weston_desktop_surface_set_size(dsurface,
436                                                 bg_output->output->width,
437                                                 bg_output->output->height);
438
439                 surface->state = RESIZING;
440                 weston_view_set_output(view, output->output);
441                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
442                 return;
443         }
444
445         /* eventually, we would set the surface fullscreen, but the client
446          * hasn't resized correctly by this point, so terminate connection */
447         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
448                 struct weston_desktop_client *desktop_client =
449                         weston_desktop_surface_get_client(dsurface);
450                 struct wl_client *client =
451                         weston_desktop_client_get_client(desktop_client);
452                 wl_client_post_implementation_error(client,
453                                 "can not display surface due to invalid geometry."
454                                 " Client should perform a geometry resize!");
455                 return;
456         }
457
458         /* this implies we resized correctly */
459         if (!weston_view_is_mapped(view)) {
460                 weston_layer_entry_remove(&view->layer_link);
461
462                 weston_view_set_output(view, woutput);
463                 weston_view_set_position(view, woutput->x, woutput->y);
464                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
465
466                 wsurface->is_mapped = true;
467                 surface->view->is_mapped = true;
468                 surface->state = FULLSCREEN;
469
470                 weston_view_geometry_dirty(view);
471                 weston_surface_damage(view->surface);
472
473                 shell_advertise_app_state(ivi, app_id,
474                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
475
476                 weston_log("Activation completed for app_id %s, role %s, "
477                            "output %s\n", app_id,
478                            ivi_layout_get_surface_role_name(surface),
479                            output->name);
480
481         }
482 }
483
484 void
485 ivi_layout_desktop_resize(struct ivi_surface *surface,
486                           struct weston_geometry area)
487 {
488         struct weston_desktop_surface *dsurf = surface->dsurface;
489         struct weston_view *view = surface->view;
490
491         int x = area.x;
492         int y = area.y;
493         int width = area.width;
494         int height = area.height;
495
496         weston_desktop_surface_set_size(dsurf,
497                                         width, height);
498
499         weston_view_set_position(view, x, y);
500
501         weston_view_geometry_dirty(view);
502         weston_surface_damage(view->surface);
503 }
504
505 void
506 ivi_layout_split_committed(struct ivi_surface *surface)
507 {
508         struct ivi_compositor *ivi = surface->ivi;
509         struct ivi_policy *policy = ivi->policy;
510
511         struct weston_desktop_surface *dsurface = surface->dsurface;
512         struct weston_surface *wsurface =
513                 weston_desktop_surface_get_surface(dsurface);
514         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
515
516         struct ivi_output *output = surface->split.output;
517         struct weston_output *woutput = output->output;
518
519         struct weston_view *view = surface->view;
520         struct weston_geometry geom;
521
522         int x, y;
523         int width, height;
524
525         x = woutput->x;
526         y = woutput->y;
527
528         if (policy && policy->api.surface_activate_by_default &&
529             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
530             !surface->mapped)
531                 return;
532
533         if (surface->view->is_mapped)
534                 return;
535
536         geom = weston_desktop_surface_get_geometry(dsurface);
537
538         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
539                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
540
541         /* save the previous area in order to recover it back when if this kind
542          * of surface is being destroyed/removed */
543         output->area_saved = output->area;
544
545         switch (surface->role) {
546         case IVI_SURFACE_ROLE_SPLIT_V:
547                 geom.width = (output->area.width / 2);
548
549                 x += woutput->width - geom.width;
550                 output->area.width -= geom.width;
551
552                 width = woutput->width - x;
553                 height = output->area.height;
554                 y = output->area.y;
555
556                 break;
557         case IVI_SURFACE_ROLE_SPLIT_H:
558                 geom.height = (output->area.height / 2);
559
560                 y = output->area.y;
561                 output->area.y += geom.height;
562                 output->area.height -= geom.height;
563
564                 width = output->area.width;
565                 height = output->area.height;
566
567                 x = output->area.x;
568
569                 break;
570         default:
571                 assert(!"Invalid split orientation\n");
572         }
573
574         weston_desktop_surface_set_size(dsurface,
575                                         width, height);
576
577         /* resize the active surface first, output->area already contains
578          * correct area to resize to */
579         if (output->active)
580                 ivi_layout_desktop_resize(output->active, output->area);
581
582         weston_view_set_output(view, woutput);
583         weston_view_set_position(view, x, y);
584         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
585
586         weston_view_geometry_dirty(view);
587         weston_surface_damage(view->surface);
588
589         wsurface->is_mapped = true;
590         surface->view->is_mapped = true;
591
592         shell_advertise_app_state(ivi, app_id,
593                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
594
595         weston_log("Activation completed for app_id %s, role %s, output %s\n",
596                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
597 }
598
599 static void
600 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
601                            int initial_x, int initial_y, int *new_x, int *new_y)
602 {
603         *new_x = output->x + initial_x;
604         *new_y = output->y + initial_y;
605 }
606
607
608 void
609 ivi_layout_popup_committed(struct ivi_surface *surface)
610 {
611         struct ivi_compositor *ivi = surface->ivi;
612         struct ivi_policy *policy = ivi->policy;
613
614         struct weston_desktop_surface *dsurface = surface->dsurface;
615         struct weston_surface *wsurface =
616                 weston_desktop_surface_get_surface(dsurface);
617         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
618
619         int new_x, new_y;
620
621         struct ivi_output *output = surface->popup.output;
622         struct weston_output *woutput = output->output;
623
624         struct weston_view *view = surface->view;
625
626         if (policy && policy->api.surface_activate_by_default &&
627             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
628             !surface->mapped)
629                 return;
630
631         if (surface->view->is_mapped)
632                 return;
633
634         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
635
636         weston_view_set_output(view, woutput);
637
638         ivi_compute_popup_position(woutput, view,
639                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
640         weston_view_set_position(view, new_x, new_y);
641
642         /* only clip the pop-up dialog window if we have a valid
643          * width and height being passed on. Users might not want to have one
644          * set-up so only enfore it is really passed on. */
645         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
646                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
647                                      surface->popup.bb.width, surface->popup.bb.height);
648
649         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
650
651         weston_view_geometry_dirty(view);
652         weston_surface_damage(view->surface);
653
654         wsurface->is_mapped = true;
655         surface->view->is_mapped = true;
656
657         shell_advertise_app_state(ivi, app_id,
658                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
659
660         weston_log("Activation completed for app_id %s, role %s, output %s\n",
661                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
662 }
663
664 static void
665 ivi_layout_popup_re_add(struct ivi_surface *surface)
666 {
667         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
668         struct weston_view *view = surface->view;
669
670         if (weston_view_is_mapped(view)) {
671                 struct weston_desktop_surface *dsurface = surface->dsurface;
672                 struct weston_surface *wsurface =
673                         weston_desktop_surface_get_surface(dsurface);
674
675                 weston_layer_entry_remove(&view->layer_link);
676
677                 wsurface->is_mapped = false;
678                 view->is_mapped = false;
679         }
680
681         /* reset the activate by default in order to (still) allow the surface
682          * to be activaved using the request */
683         if (!surface->mapped)
684                 surface->mapped = true;
685
686         ivi_layout_popup_committed(surface);
687 }
688
689 static bool
690 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
691 {
692         struct ivi_compositor *ivi = surf->ivi;
693         struct ivi_surface *is;
694
695         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
696             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
697             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
698                 return false;
699
700         /* reset the activate by default in order to (still) allow the surface
701          * to be activaved using the request */
702         if (!surf->mapped)
703                 surf->mapped = true;
704
705         wl_list_for_each(is, &ivi->surfaces, link)
706                 if (is == surf)
707                         return true;
708
709         return false;
710 }
711
712 void
713 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
714 {
715         struct ivi_compositor *ivi = output->ivi;
716         struct weston_desktop_surface *dsurf;
717         struct weston_view *view;
718         struct weston_geometry geom;
719         struct ivi_policy *policy = output->ivi->policy;
720
721         dsurf = surf->dsurface;
722         view = surf->view;
723
724         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
725
726         if (!surf)
727                 return;
728
729         if (policy && policy->api.surface_activate &&
730             !policy->api.surface_activate(surf, surf->ivi)) {
731                 return;
732         }
733
734 #ifdef AGL_COMP_DEBUG
735         weston_log("Activating app_id %s, type %s\n", app_id,
736                         ivi_layout_get_surface_role_name(surf));
737 #endif
738
739         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
740                 ivi_layout_popup_re_add(surf);
741                 return;
742         }
743
744         /* do not 're'-activate surfaces that are split or active */
745         if (surf == output->active ||
746             ivi_layout_surface_is_split_or_fullscreen(surf))
747                 return;
748
749         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
750                 struct ivi_output *remote_output =
751                         ivi_layout_find_with_app_id(app_id, ivi);
752
753                 /* if already active on a remote output do not
754                  * attempt to activate it again */
755                 if (remote_output && remote_output->active == surf)
756                         return;
757         }
758
759
760         geom = weston_desktop_surface_get_geometry(dsurf);
761
762         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
763                 surf->desktop.pending_output = output;
764         if (weston_desktop_surface_get_maximized(dsurf) &&
765             geom.width == output->area.width &&
766             geom.height == output->area.height) {
767                 ivi_layout_activate_complete(output, surf);
768                 return;
769         }
770
771         weston_desktop_surface_set_maximized(dsurf, true);
772         weston_desktop_surface_set_size(dsurf,
773                                         output->area.width,
774                                         output->area.height);
775
776         weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
777                         app_id, ivi_layout_get_surface_role_name(surf),
778                         output->area.width, output->area.height);
779         /*
780          * If the view isn't mapped, we put it onto the hidden layer so it will
781          * start receiving frame events, and will be able to act on our
782          * configure event.
783          */
784         if (!weston_view_is_mapped(view)) {
785                 view->is_mapped = true;
786                 view->surface->is_mapped = true;
787
788                 weston_view_set_output(view, output->output);
789                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
790                 weston_log("Placed app_id %s, type %s in hidden layer\n",
791                                 app_id, ivi_layout_get_surface_role_name(surf));
792         }
793 }
794
795 void
796 ivi_layout_activate(struct ivi_output *output, const char *app_id)
797 {
798         struct ivi_surface *surf;
799         struct ivi_compositor *ivi = output->ivi;
800
801         if (!app_id)
802                 return;
803
804         surf = ivi_find_app(ivi, app_id);
805         if (!surf)
806                 return;
807
808         ivi_layout_activate_by_surf(output, surf);
809 }
810
811 struct ivi_output *
812 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
813 {
814         struct ivi_output *ivi_output = NULL;
815
816         switch (surf->role) {
817         case IVI_SURFACE_ROLE_DESKTOP:
818                 if (surf->desktop.pending_output)
819                         ivi_output = surf->desktop.pending_output;
820                 else
821                         ivi_output = surf->desktop.last_output;
822                 break;
823         case IVI_SURFACE_ROLE_POPUP:
824                 ivi_output = surf->popup.output;
825                 break;
826         case IVI_SURFACE_ROLE_BACKGROUND:
827                 ivi_output = surf->bg.output;
828                 break;
829         case IVI_SURFACE_ROLE_PANEL:
830                 ivi_output = surf->panel.output;
831                 break;
832         case IVI_SURFACE_ROLE_FULLSCREEN:
833                 ivi_output = surf->fullscreen.output;
834                 break;
835         case IVI_SURFACE_ROLE_SPLIT_H:
836         case IVI_SURFACE_ROLE_SPLIT_V:
837                 ivi_output = surf->split.output;
838                 break;
839         case IVI_SURFACE_ROLE_REMOTE:
840                 ivi_output = surf->remote.output;
841                 break;
842         case IVI_SURFACE_ROLE_NONE:
843         default:
844                 break;
845         }
846
847         return ivi_output;
848 }
849
850 void
851 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
852 {
853         struct ivi_surface *surf;
854         struct ivi_output *ivi_output;
855         struct ivi_policy *policy = ivi->policy;
856
857         if (!app_id)
858                 return;
859
860         surf = ivi_find_app(ivi, app_id);
861         if (!surf)
862                 return;
863
864         if (policy && policy->api.surface_deactivate &&
865             !policy->api.surface_deactivate(surf, surf->ivi)) {
866                 return;
867         }
868
869         ivi_output = ivi_layout_get_output_from_surface(surf);
870         weston_log("Deactiving %s, role %s\n", app_id,
871                         ivi_layout_get_surface_role_name(surf));
872
873         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
874                 struct ivi_surface *previous_active;
875
876                 previous_active = ivi_output->previous_active;
877                 if (!previous_active) {
878                         /* we don't have a previous active it means we should
879                          * display the bg */
880                         if (ivi_output->active) {
881                                 struct weston_view *view;
882
883                                 view = ivi_output->active->view;
884                                 view->is_mapped = false;
885                                 view->surface->is_mapped = false;
886
887                                 weston_layer_entry_remove(&view->layer_link);
888                                 weston_view_geometry_dirty(view);
889                                 weston_surface_damage(view->surface);
890                                 ivi_output->active = NULL;
891                         }
892                 } else {
893                         struct weston_desktop_surface *dsurface;
894                         const char *previous_active_app_id;
895
896                         dsurface = previous_active->dsurface;
897                         previous_active_app_id =
898                                 weston_desktop_surface_get_app_id(dsurface);
899                         ivi_layout_activate(ivi_output, previous_active_app_id);
900                 }
901         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
902                 struct weston_view *view  = surf->view;
903
904                 weston_layer_entry_remove(&view->layer_link);
905                 weston_view_geometry_dirty(view);
906                 weston_surface_damage(view->surface);
907         }
908 }