layout: Handle dynamic application/surface movement between outputs
[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         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
196         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
197
198         if (weston_view_is_mapped(view)) {
199                 weston_layer_entry_remove(&view->layer_link);
200         } else {
201                 weston_view_update_transform(view);
202         }
203
204         if (output_has_black_curtain(output)) {
205                 if (!output->background) {
206                         weston_log("Found that we have no background surface "
207                                     "for output %s. Using black curtain as background\n",
208                                     output->output->name);
209
210                         struct weston_view *ev =
211                                 output->fullscreen_view.fs->view;
212
213                         /* use the black curtain as background when we have
214                          * none added by the shell client. */
215                         weston_layer_entry_remove(&ev->layer_link);
216                         weston_layer_entry_insert(&ivi->normal.view_list,
217                                                   &ev->layer_link);
218                         weston_view_geometry_dirty(ev);
219                         weston_surface_damage(ev->surface);
220                 } else {
221                         remove_black_curtain(output);
222                 }
223         }
224
225
226         weston_view_set_output(view, woutput);
227         weston_view_set_position(view,
228                                  woutput->x + output->area.x,
229                                  woutput->y + output->area.y);
230
231         view->is_mapped = true;
232         surf->mapped = true;
233         view->surface->is_mapped = true;
234
235         /* handle a movement from one output to another */
236         if (surf->current_completed_output &&
237             surf->current_completed_output != output) {
238
239                 /* we're migrating the same surface but to another output */
240                 if (surf->current_completed_output->active == surf) {
241                         struct weston_view *ev =
242                                 surf->current_completed_output->active->view;
243
244                         weston_layer_entry_remove(&ev->layer_link);
245                         surf->current_completed_output->previous_active =
246                                 surf->current_completed_output->active;
247                         surf->current_completed_output->active = NULL;
248
249                         /* damage all possible outputs to avoid stale views */
250                         weston_compositor_damage_all(ivi->compositor);
251                 }
252         }
253
254
255         if (output->active) {
256                 output->active->view->is_mapped = false;
257                 output->active->view->surface->is_mapped = false;
258
259                 weston_layer_entry_remove(&output->active->view->layer_link);
260         }
261         output->previous_active = output->active;
262         output->active = surf;
263         surf->current_completed_output = output;
264
265         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
266         weston_view_geometry_dirty(view);
267         weston_surface_damage(view->surface);
268
269         if (ivi_seat)
270                 ivi_shell_activate_surface(surf, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
271
272         /*
273          * the 'remote' role now makes use of this part so make sure we don't
274          * trip the enum such that we might end up with a modified output for
275          * 'remote' role
276          */
277         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
278                 if (surf->desktop.pending_output)
279                         surf->desktop.last_output = surf->desktop.pending_output;
280                 surf->desktop.pending_output = NULL;
281         }
282
283         weston_log("Activation completed for app_id %s, role %s, output %s\n",
284                         weston_desktop_surface_get_app_id(surf->dsurface),
285                         ivi_layout_get_surface_role_name(surf), output->name);
286 }
287
288 struct ivi_output *
289 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
290 {
291         struct ivi_output *out;
292
293         if (!app_id)
294                 return NULL;
295
296         wl_list_for_each(out, &ivi->outputs, link) {
297                 if (!out->app_id)
298                         continue;
299
300                 if (!strcmp(app_id, out->app_id))
301                         return out;
302         }
303
304         return NULL;
305 }
306
307 struct ivi_output *
308 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
309 {
310         struct ivi_output *out;
311
312         wl_list_for_each(out, &ivi->outputs, link) {
313                 if (out->background &&
314                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
315                         return out;
316         }
317
318         return NULL;
319 }
320
321
322 static void
323 ivi_layout_add_to_hidden_layer(struct ivi_surface *surf,
324                                struct ivi_output *ivi_output)
325 {
326         struct weston_desktop_surface *dsurf = surf->dsurface;
327         struct weston_view *ev = surf->view;
328         struct ivi_compositor *ivi = surf->ivi;
329         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
330
331         /*
332          * If the view isn't mapped, we put it onto the hidden layer so it will
333          * start receiving frame events, and will be able to act on our
334          * configure event.
335          */
336         if (!weston_view_is_mapped(ev)) {
337                 ev->is_mapped = true;
338                 ev->surface->is_mapped = true;
339
340                 weston_desktop_surface_set_maximized(dsurf, true);
341                 weston_desktop_surface_set_size(dsurf,
342                                                 ivi_output->area.width,
343                                                 ivi_output->area.height);
344
345                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
346                                 app_id, ivi_layout_get_surface_role_name(surf),
347                                 ivi_output->area.width, ivi_output->area.height);
348
349                 surf->hidden_layer_output = ivi_output;
350                 weston_view_set_output(ev, ivi_output->output);
351                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
352                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
353                                 app_id, ivi_layout_get_surface_role_name(surf),
354                                 ivi_output->output->name);
355
356                 weston_compositor_schedule_repaint(ivi->compositor);
357                 return;
358         }
359
360         /* we might have another output to activate */
361         if (surf->hidden_layer_output &&
362             surf->hidden_layer_output != ivi_output) {
363                 weston_layer_entry_remove(&ev->layer_link);
364
365                 if (ivi_output->area.width != surf->hidden_layer_output->area.width &&
366                     ivi_output->area.height != surf->hidden_layer_output->area.height) {
367                         weston_desktop_surface_set_maximized(dsurf, true);
368                         weston_desktop_surface_set_size(dsurf,
369                                                         ivi_output->area.width,
370                                                         ivi_output->area.height);
371                 }
372
373                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
374                                 app_id, ivi_layout_get_surface_role_name(surf),
375                                 ivi_output->area.width, ivi_output->area.height);
376
377                 surf->hidden_layer_output = ivi_output;
378                 weston_view_set_output(ev, ivi_output->output);
379                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
380                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
381                                 app_id, ivi_layout_get_surface_role_name(surf),
382                                 ivi_output->output->name);
383         }
384
385         weston_compositor_schedule_repaint(ivi->compositor);
386 }
387
388 void
389 ivi_layout_desktop_committed(struct ivi_surface *surf)
390 {
391         struct weston_desktop_surface *dsurf = surf->dsurface;
392         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
393         struct ivi_policy *policy = surf->ivi->policy;
394         struct ivi_output *output;
395         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
396
397         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
398                surf->role == IVI_SURFACE_ROLE_REMOTE);
399
400         /*
401          * we can't make use here of the ivi_layout_get_output_from_surface()
402          * due to the fact that we'll always land here when a surface performs
403          * a commit and pending_output will not bet set. This works in tandem
404          * with 'mapped' at this point to avoid tripping over
405          * to a surface that continuously updates its content
406          */
407         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
408                 output = surf->desktop.pending_output;
409         else
410                 output = surf->remote.output;
411
412         if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
413                 struct ivi_output *r_output;
414
415                 if (policy && policy->api.surface_activate_by_default &&
416                     !policy->api.surface_activate_by_default(surf, surf->ivi))
417                         return;
418
419                 /* we can only activate it again by using the protocol */
420                 if (surf->mapped)
421                         return;
422
423                 /* check first if there aren't any outputs being set */
424                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
425
426                 if (r_output) {
427                         struct weston_view *view = r_output->fullscreen_view.fs->view;
428                         if (view->is_mapped || view->surface->is_mapped)
429                                 remove_black_curtain(r_output);
430                 }
431
432
433                 /* try finding an output with a background and use that */
434                 if (!r_output)
435                         r_output = ivi_layout_find_bg_output(surf->ivi);
436
437                 /* if we couldn't still find an output by this point, there's
438                  * something wrong so we abort with a protocol error */
439                 if (!r_output) {
440                         wl_resource_post_error(surf->ivi->shell_client.resource,
441                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
442                                                "No valid output found to activate surface by default");
443                         return;
444                 }
445
446                 if (!surf->ivi->activate_by_default) {
447                         weston_log("Refusing to activate surface role %d, app_id %s\n",
448                                         surf->role, app_id);
449
450                         if (!weston_desktop_surface_get_maximized(dsurf) ||
451                             geom.width != r_output->area.width ||
452                             geom.height != r_output->area.height)
453                                 ivi_layout_add_to_hidden_layer(surf, r_output);
454
455                         return;
456                 }
457
458                 /* use the output of the bg to activate the app on start-up by
459                  * default */
460                 if (surf->view && r_output) {
461                         if (app_id && r_output) {
462                                 weston_log("Surface with app_id %s, role %s activating by default\n",
463                                         weston_desktop_surface_get_app_id(surf->dsurface),
464                                         ivi_layout_get_surface_role_name(surf));
465                                 ivi_layout_activate(r_output, app_id);
466                         } else if (!app_id) {
467                                 /*
468                                  * applications not setting an app_id, or
469                                  * setting an app_id but at a later point in
470                                  * time, might fall-back here so give them a
471                                  * chance to receive the configure event and
472                                  * act upon it
473                                  */
474                                 weston_log("Surface no app_id, role %s activating by default\n",
475                                         ivi_layout_get_surface_role_name(surf));
476                                 ivi_layout_activate_by_surf(r_output, surf);
477                         }
478                 }
479
480                 return;
481         }
482
483         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
484                 if (policy && policy->api.surface_activate_by_default &&
485                     !policy->api.surface_activate_by_default(surf, surf->ivi))
486                         return;
487
488                 /* we can only activate it again by using the protocol, but
489                  * additionally the output is not reset when
490                  * ivi_layout_activate_complete() terminates so we use the
491                  * current active surface to avoid hitting this again and again
492                  * */
493                 if (surf->mapped && output->active == surf)
494                         return;
495
496                 if (app_id) {
497                         weston_log("Surface with app_id %s, role %s activating "
498                                    "by default on output %s\n",
499                                    weston_desktop_surface_get_app_id(surf->dsurface),
500                                    ivi_layout_get_surface_role_name(surf),
501                                    output->output->name);
502                         ivi_layout_activate(output, app_id);
503                 }
504                 return;
505         }
506
507         if (!weston_desktop_surface_get_maximized(dsurf) ||
508             geom.width != output->area.width ||
509             geom.height != output->area.height)
510                 return;
511
512         ivi_layout_activate_complete(output, surf);
513 }
514
515 void
516 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
517 {
518         struct ivi_compositor *ivi = surface->ivi;
519         struct ivi_policy *policy = ivi->policy;
520
521         struct weston_desktop_surface *dsurface = surface->dsurface;
522         struct weston_surface *wsurface =
523                 weston_desktop_surface_get_surface(dsurface);
524         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
525
526         struct ivi_output *output = surface->split.output;
527         struct weston_output *woutput = output->output;
528         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
529
530         struct weston_view *view = surface->view;
531         struct weston_geometry geom =
532                 weston_desktop_surface_get_geometry(dsurface);
533
534         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
535         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
536
537         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
538         bool is_dim_same =
539                 geom.width == bg_output->output->width &&
540                 geom.height == bg_output->output->height;
541
542         if (policy && policy->api.surface_activate_by_default &&
543             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
544             !surface->mapped)
545                 return;
546
547         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
548
549         if (weston_view_is_mapped(view))
550                 return;
551
552         /* if we still get here but we haven't resized so far, send configure
553          * events to do so */
554         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
555                 struct ivi_output *bg_output =
556                         ivi_layout_find_bg_output(surface->ivi);
557
558                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
559                                 app_id, ivi_layout_get_surface_role_name(surface));
560                 weston_desktop_surface_set_fullscreen(dsurface, true);
561                 weston_desktop_surface_set_size(dsurface,
562                                                 bg_output->output->width,
563                                                 bg_output->output->height);
564
565                 surface->state = RESIZING;
566                 weston_view_set_output(view, output->output);
567                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
568                 return;
569         }
570
571         /* eventually, we would set the surface fullscreen, but the client
572          * hasn't resized correctly by this point, so terminate connection */
573         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
574                 struct weston_desktop_client *desktop_client =
575                         weston_desktop_surface_get_client(dsurface);
576                 struct wl_client *client =
577                         weston_desktop_client_get_client(desktop_client);
578                 wl_client_post_implementation_error(client,
579                                 "can not display surface due to invalid geometry."
580                                 " Client should perform a geometry resize!");
581                 return;
582         }
583
584         /* this implies we resized correctly */
585         if (!weston_view_is_mapped(view)) {
586                 weston_layer_entry_remove(&view->layer_link);
587
588                 weston_view_set_output(view, woutput);
589                 weston_view_set_position(view, woutput->x, woutput->y);
590                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
591
592                 wsurface->is_mapped = true;
593                 surface->view->is_mapped = true;
594                 surface->state = FULLSCREEN;
595
596                 weston_view_geometry_dirty(view);
597                 weston_surface_damage(view->surface);
598
599                 if (ivi_seat)
600                         ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
601
602                 shell_advertise_app_state(ivi, app_id,
603                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
604
605                 weston_log("Activation completed for app_id %s, role %s, "
606                            "output %s\n", app_id,
607                            ivi_layout_get_surface_role_name(surface),
608                            output->name);
609
610         }
611 }
612
613 void
614 ivi_layout_desktop_resize(struct ivi_surface *surface,
615                           struct weston_geometry area)
616 {
617         struct weston_desktop_surface *dsurf = surface->dsurface;
618         struct weston_view *view = surface->view;
619
620         int x = area.x;
621         int y = area.y;
622         int width = area.width;
623         int height = area.height;
624
625         weston_desktop_surface_set_size(dsurf,
626                                         width, height);
627
628         weston_view_set_position(view, x, y);
629
630         weston_view_geometry_dirty(view);
631         weston_surface_damage(view->surface);
632 }
633
634 void
635 ivi_layout_split_committed(struct ivi_surface *surface)
636 {
637         struct ivi_compositor *ivi = surface->ivi;
638         struct ivi_policy *policy = ivi->policy;
639
640         struct weston_desktop_surface *dsurface = surface->dsurface;
641         struct weston_surface *wsurface =
642                 weston_desktop_surface_get_surface(dsurface);
643         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
644
645         struct ivi_output *output = surface->split.output;
646         struct weston_output *woutput = output->output;
647
648         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
649         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
650
651         struct weston_view *view = surface->view;
652         struct weston_geometry geom;
653
654         int x, y;
655         int width, height;
656
657         x = woutput->x;
658         y = woutput->y;
659
660         if (policy && policy->api.surface_activate_by_default &&
661             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
662             !surface->mapped)
663                 return;
664
665         if (surface->view->is_mapped)
666                 return;
667
668         geom = weston_desktop_surface_get_geometry(dsurface);
669
670         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
671                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
672
673         /* save the previous area in order to recover it back when if this kind
674          * of surface is being destroyed/removed */
675         output->area_saved = output->area;
676
677         switch (surface->role) {
678         case IVI_SURFACE_ROLE_SPLIT_V:
679                 geom.width = (output->area.width / 2);
680
681                 x += woutput->width - geom.width;
682                 output->area.width -= geom.width;
683
684                 width = woutput->width - x;
685                 height = output->area.height;
686                 y = output->area.y;
687
688                 break;
689         case IVI_SURFACE_ROLE_SPLIT_H:
690                 geom.height = (output->area.height / 2);
691
692                 y = output->area.y;
693                 output->area.y += geom.height;
694                 output->area.height -= geom.height;
695
696                 width = output->area.width;
697                 height = output->area.height;
698
699                 x = output->area.x;
700
701                 break;
702         default:
703                 assert(!"Invalid split orientation\n");
704         }
705
706         weston_desktop_surface_set_size(dsurface,
707                                         width, height);
708
709         /* resize the active surface first, output->area already contains
710          * correct area to resize to */
711         if (output->active)
712                 ivi_layout_desktop_resize(output->active, output->area);
713
714         weston_view_set_output(view, woutput);
715         weston_view_set_position(view, x, y);
716         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
717
718         weston_view_geometry_dirty(view);
719         weston_surface_damage(view->surface);
720
721         if (ivi_seat)
722                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
723
724         wsurface->is_mapped = true;
725         surface->view->is_mapped = true;
726
727         shell_advertise_app_state(ivi, app_id,
728                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
729
730         weston_log("Activation completed for app_id %s, role %s, output %s\n",
731                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
732 }
733
734 static void
735 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
736                            int initial_x, int initial_y, int *new_x, int *new_y)
737 {
738         *new_x = output->x + initial_x;
739         *new_y = output->y + initial_y;
740 }
741
742
743 void
744 ivi_layout_popup_committed(struct ivi_surface *surface)
745 {
746         struct ivi_compositor *ivi = surface->ivi;
747         struct ivi_policy *policy = ivi->policy;
748
749         struct weston_desktop_surface *dsurface = surface->dsurface;
750         struct weston_surface *wsurface =
751                 weston_desktop_surface_get_surface(dsurface);
752         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
753
754         int new_x, new_y;
755
756         struct ivi_output *output = surface->popup.output;
757         struct weston_output *woutput = output->output;
758
759         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
760         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
761
762         struct weston_view *view = surface->view;
763
764         if (policy && policy->api.surface_activate_by_default &&
765             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
766             !surface->mapped)
767                 return;
768
769         if (surface->view->is_mapped || surface->state == HIDDEN)
770                 return;
771
772         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
773
774         weston_view_set_output(view, woutput);
775
776         ivi_compute_popup_position(woutput, view,
777                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
778         weston_view_set_position(view, new_x, new_y);
779         weston_view_update_transform(view);
780
781         /* only clip the pop-up dialog window if we have a valid
782          * width and height being passed on. Users might not want to have one
783          * set-up so only enfore it is really passed on. */
784         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
785                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
786                                      surface->popup.bb.width, surface->popup.bb.height);
787
788         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
789
790         weston_view_geometry_dirty(view);
791         weston_surface_damage(view->surface);
792
793         if (ivi_seat)
794                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
795
796         wsurface->is_mapped = true;
797         surface->view->is_mapped = true;
798
799         shell_advertise_app_state(ivi, app_id,
800                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
801
802         weston_log("Activation completed for app_id %s, role %s, output %s\n",
803                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
804 }
805
806 static void
807 ivi_layout_popup_re_add(struct ivi_surface *surface)
808 {
809         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
810         struct weston_view *view = surface->view;
811
812         if (weston_view_is_mapped(view)) {
813                 struct weston_desktop_surface *dsurface = surface->dsurface;
814                 struct weston_surface *wsurface =
815                         weston_desktop_surface_get_surface(dsurface);
816
817                 weston_layer_entry_remove(&view->layer_link);
818
819                 wsurface->is_mapped = false;
820                 view->is_mapped = false;
821         }
822
823         /* reset the activate by default in order to (still) allow the surface
824          * to be activaved using the request */
825         if (!surface->mapped)
826                 surface->mapped = true;
827
828         surface->state = NORMAL;
829         ivi_layout_popup_committed(surface);
830 }
831
832 static bool
833 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
834 {
835         struct ivi_compositor *ivi = surf->ivi;
836         struct ivi_surface *is;
837
838         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
839             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
840             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
841                 return false;
842
843         /* reset the activate by default in order to (still) allow the surface
844          * to be activaved using the request */
845         if (!surf->mapped)
846                 surf->mapped = true;
847
848         wl_list_for_each(is, &ivi->surfaces, link)
849                 if (is == surf)
850                         return true;
851
852         return false;
853 }
854
855 void
856 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
857 {
858         struct ivi_compositor *ivi = output->ivi;
859         struct weston_desktop_surface *dsurf;
860         struct weston_geometry geom;
861         struct ivi_policy *policy = output->ivi->policy;
862
863         dsurf = surf->dsurface;
864
865         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
866
867         if (!surf)
868                 return;
869
870         if (policy && policy->api.surface_activate &&
871             !policy->api.surface_activate(surf, surf->ivi)) {
872                 return;
873         }
874
875 #ifdef AGL_COMP_DEBUG
876         weston_log("Activating app_id %s, type %s, on output %s\n", app_id,
877                         ivi_layout_get_surface_role_name(surf), output->output->name);
878 #endif
879
880         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
881                 ivi_layout_popup_re_add(surf);
882                 return;
883         }
884
885         /* do not 're'-activate surfaces that are split or active */
886         if (surf == output->active ||
887             ivi_layout_surface_is_split_or_fullscreen(surf)) {
888                 weston_log("Application %s is already active on output %s\n",
889                                 app_id, output->output->name);
890                 return;
891         }
892
893         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
894                 struct ivi_output *remote_output =
895                         ivi_layout_find_with_app_id(app_id, ivi);
896
897                 /* if already active on a remote output do not
898                  * attempt to activate it again */
899                 if (remote_output && remote_output->active == surf)
900                         return;
901         }
902
903
904         geom = weston_desktop_surface_get_geometry(dsurf);
905
906         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
907                 surf->desktop.pending_output = output;
908         if (weston_desktop_surface_get_maximized(dsurf) &&
909             geom.width == output->area.width &&
910             geom.height == output->area.height) {
911                 ivi_layout_activate_complete(output, surf);
912                 return;
913         }
914
915         ivi_layout_add_to_hidden_layer(surf, output);
916 }
917
918 void
919 ivi_layout_activate(struct ivi_output *output, const char *app_id)
920 {
921         struct ivi_surface *surf;
922         struct ivi_compositor *ivi = output->ivi;
923
924         if (!app_id)
925                 return;
926
927         surf = ivi_find_app(ivi, app_id);
928         if (!surf)
929                 return;
930
931         ivi_layout_activate_by_surf(output, surf);
932 }
933
934 struct ivi_output *
935 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
936 {
937         struct ivi_output *ivi_output = NULL;
938
939         switch (surf->role) {
940         case IVI_SURFACE_ROLE_DESKTOP:
941                 if (surf->desktop.pending_output)
942                         ivi_output = surf->desktop.pending_output;
943                 else
944                         ivi_output = surf->desktop.last_output;
945                 break;
946         case IVI_SURFACE_ROLE_POPUP:
947                 ivi_output = surf->popup.output;
948                 break;
949         case IVI_SURFACE_ROLE_BACKGROUND:
950                 ivi_output = surf->bg.output;
951                 break;
952         case IVI_SURFACE_ROLE_PANEL:
953                 ivi_output = surf->panel.output;
954                 break;
955         case IVI_SURFACE_ROLE_FULLSCREEN:
956                 ivi_output = surf->fullscreen.output;
957                 break;
958         case IVI_SURFACE_ROLE_SPLIT_H:
959         case IVI_SURFACE_ROLE_SPLIT_V:
960                 ivi_output = surf->split.output;
961                 break;
962         case IVI_SURFACE_ROLE_REMOTE:
963                 ivi_output = surf->remote.output;
964                 break;
965         case IVI_SURFACE_ROLE_NONE:
966         default:
967                 break;
968         }
969
970         return ivi_output;
971 }
972
973 void
974 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
975 {
976         struct ivi_surface *surf;
977         struct ivi_output *ivi_output;
978         struct ivi_policy *policy = ivi->policy;
979
980         if (!app_id)
981                 return;
982
983         surf = ivi_find_app(ivi, app_id);
984         if (!surf)
985                 return;
986
987         if (policy && policy->api.surface_deactivate &&
988             !policy->api.surface_deactivate(surf, surf->ivi)) {
989                 return;
990         }
991
992         ivi_output = ivi_layout_get_output_from_surface(surf);
993         weston_log("Deactiving %s, role %s\n", app_id,
994                         ivi_layout_get_surface_role_name(surf));
995
996         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
997                 struct ivi_surface *previous_active;
998
999                 previous_active = ivi_output->previous_active;
1000                 if (!previous_active) {
1001                         /* we don't have a previous active it means we should
1002                          * display the bg */
1003                         if (ivi_output->active) {
1004                                 struct weston_view *view;
1005
1006                                 view = ivi_output->active->view;
1007                                 view->is_mapped = false;
1008                                 view->surface->is_mapped = false;
1009
1010                                 weston_layer_entry_remove(&view->layer_link);
1011                                 weston_view_geometry_dirty(view);
1012                                 weston_surface_damage(view->surface);
1013                                 ivi_output->active = NULL;
1014                         }
1015                 } else {
1016                         struct weston_desktop_surface *dsurface;
1017                         const char *previous_active_app_id;
1018
1019                         dsurface = previous_active->dsurface;
1020                         previous_active_app_id =
1021                                 weston_desktop_surface_get_app_id(dsurface);
1022                         ivi_layout_activate(ivi_output, previous_active_app_id);
1023                 }
1024         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1025                 struct weston_view *view  = surf->view;
1026
1027                 weston_view_unmap(view);
1028                 surf->state = HIDDEN;
1029
1030                 weston_layer_entry_remove(&view->layer_link);
1031                 weston_view_geometry_dirty(view);
1032                 weston_surface_damage(view->surface);
1033         }
1034 }