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