a5e3b36208b90996ceb3778497f5aea0c2db5c28
[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 bool
53 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface);
54
55 const char *
56 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
57 {
58         if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
59                 return " unknown surface role";
60
61         return ivi_roles_as_string[surf->role];
62 }
63
64 static void
65 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
66 {
67         struct weston_output *woutput = output->output;
68         struct ivi_surface *bg = output->background;
69         struct weston_view *view;
70
71         if (!bg) {
72                 weston_log("WARNING: Output does not have a background\n");
73                 return;
74         }
75
76         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
77
78         view = bg->view;
79
80         weston_view_set_output(view, woutput);
81         weston_view_set_position(view, woutput->x, woutput->y);
82
83         weston_log("(background) position view %p, x %d, y %d, on output %s\n", view,
84                         woutput->x, woutput->y, output->name);
85
86         view->is_mapped = true;
87         view->surface->is_mapped = true;
88
89         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
90 }
91
92 static void
93 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
94                struct ivi_surface *panel)
95 {
96         struct weston_output *woutput = output->output;
97         struct weston_desktop_surface *dsurface;
98         struct weston_view *view;
99         struct weston_geometry geom;
100         int x = woutput->x;
101         int y = woutput->y;
102
103         if (!panel)
104                 return;
105
106         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
107         dsurface = panel->dsurface;
108         view = panel->view;
109         geom = weston_desktop_surface_get_geometry(dsurface);
110
111         weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
112                         geom.width, geom.height, geom.x, geom.y);
113
114         switch (panel->panel.edge) {
115         case AGL_SHELL_EDGE_TOP:
116                 output->area.y += geom.height;
117                 output->area.height -= geom.height;
118                 break;
119         case AGL_SHELL_EDGE_BOTTOM:
120                 y += woutput->height - geom.height;
121                 output->area.height -= geom.height;
122                 break;
123         case AGL_SHELL_EDGE_LEFT:
124                 output->area.x += geom.width;
125                 output->area.width -= geom.width;
126                 break;
127         case AGL_SHELL_EDGE_RIGHT:
128                 x += woutput->width - geom.width;
129                 output->area.width -= geom.width;
130                 break;
131         }
132
133         x -= geom.x;
134         y -= geom.y;
135
136         weston_view_set_output(view, woutput);
137         weston_view_set_position(view, x, y);
138
139         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
140                         panel->panel.edge, view, x, y);
141
142         view->is_mapped = true;
143         view->surface->is_mapped = true;
144
145         weston_log("panel type %d inited on output %s\n", panel->panel.edge,
146                         output->name);
147
148         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
149 }
150
151 /*
152  * Initializes all static parts of the layout, i.e. the background and panels.
153  */
154 void
155 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
156 {
157         bool use_default_area = true;
158
159         ivi_background_init(ivi, output);
160
161         if (output->area_activation.width ||
162             output->area_activation.height ||
163             output->area_activation.x ||
164             output->area_activation.y) {
165                 /* Sanity check target area is within output bounds */
166                 if ((output->area_activation.x + output->area_activation.width) < output->output->width ||
167                     (output->area_activation.y + output->area_activation.height) < output->output->height) {
168                         weston_log("Using specified area for output %s, ignoring panels\n",
169                                    output->name);
170                         output->area.x = output->area_activation.x;
171                         output->area.y = output->area_activation.y;
172                         output->area.width = output->area_activation.width;
173                         output->area.height = output->area_activation.height;
174                         use_default_area = false;
175                 } else {
176                         weston_log("Invalid activation-area position for output %s, ignoring\n",
177                                    output->name);
178                 }
179         }
180         if (use_default_area) {
181                 output->area.x = 0;
182                 output->area.y = 0;
183                 output->area.width = output->output->width;
184                 output->area.height = output->output->height;
185
186                 ivi_panel_init(ivi, output, output->top);
187                 ivi_panel_init(ivi, output, output->bottom);
188                 ivi_panel_init(ivi, output, output->left);
189                 ivi_panel_init(ivi, output, output->right);
190         }
191
192         weston_compositor_schedule_repaint(ivi->compositor);
193
194         weston_log("Usable area: %dx%d+%d,%d\n",
195                    output->area.width, output->area.height,
196                    output->area.x, output->area.y);
197 }
198
199 struct ivi_surface *
200 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
201 {
202         struct ivi_surface *surf;
203         const char *id;
204
205         wl_list_for_each(surf, &ivi->surfaces, link) {
206                 id = weston_desktop_surface_get_app_id(surf->dsurface);
207                 if (id && strcmp(app_id, id) == 0)
208                         return surf;
209         }
210
211         return NULL;
212 }
213
214 static void
215 ivi_layout_activate_complete(struct ivi_output *output,
216                              struct ivi_surface *surf)
217 {
218         struct ivi_compositor *ivi = output->ivi;
219         struct weston_output *woutput = output->output;
220         struct weston_view *view = surf->view;
221         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
222         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
223         const char *app_id = weston_desktop_surface_get_app_id(surf->dsurface);
224         bool update_previous = true;
225
226         if (weston_view_is_mapped(view)) {
227                 weston_layer_entry_remove(&view->layer_link);
228         } else {
229                 weston_view_update_transform(view);
230         }
231
232         if (output_has_black_curtain(output)) {
233                 if (!output->background) {
234                         weston_log("Found that we have no background surface "
235                                     "for output %s. Using black curtain as background\n",
236                                     output->output->name);
237
238                         struct weston_view *ev =
239                                 output->fullscreen_view.fs->view;
240
241                         /* use the black curtain as background when we have
242                          * none added by the shell client. */
243                         weston_layer_entry_remove(&ev->layer_link);
244                         weston_layer_entry_insert(&ivi->normal.view_list,
245                                                   &ev->layer_link);
246                         weston_view_geometry_dirty(ev);
247                         weston_surface_damage(ev->surface);
248                 } else {
249                         remove_black_curtain(output);
250                 }
251         }
252
253
254         weston_view_set_output(view, woutput);
255         /* drop any previous masks set on this view */
256         weston_view_set_mask_infinite(view);
257
258         if (surf->role != IVI_SURFACE_ROLE_BACKGROUND)
259                 weston_view_set_position(view,
260                                          woutput->x + output->area.x,
261                                          woutput->y + output->area.y);
262
263         view->is_mapped = true;
264         surf->mapped = true;
265         view->surface->is_mapped = true;
266
267         /* handle a movement from one output to another */
268         if (surf->current_completed_output &&
269             surf->current_completed_output != output) {
270
271                 /* we're migrating the same surface but to another output */
272                 if (surf->current_completed_output->active == surf) {
273                         struct weston_view *ev =
274                                 surf->current_completed_output->active->view;
275
276                         weston_layer_entry_remove(&ev->layer_link);
277                         surf->current_completed_output->previous_active =
278                                 surf->current_completed_output->active;
279                         surf->current_completed_output->active = NULL;
280
281                         /* damage all possible outputs to avoid stale views */
282                         weston_compositor_damage_all(ivi->compositor);
283                 }
284         }
285
286         if (output->active) {
287                 /* keep the background surface mapped at all times */
288                 if (output->active->role != IVI_SURFACE_ROLE_BACKGROUND) {
289                         output->active->view->is_mapped = false;
290                         output->active->view->surface->is_mapped = false;
291
292                         weston_layer_entry_remove(&output->active->view->layer_link);
293                 }
294         }
295
296         if (output->previous_active && output->active) {
297                 const char *c_app_id =
298                         weston_desktop_surface_get_app_id(output->active->dsurface);
299
300                 /* if the currently activated app_id is the same as the one
301                  * we're trying to complete activation with means we're
302                  * operating on the same app_id so do update previous_active as
303                  * it will overwrite it with the same value */
304                 if (!strcmp(c_app_id, app_id)) {
305                         update_previous = false;
306                 }
307         }
308
309         if (update_previous)
310                 output->previous_active = output->active;
311         output->active = surf;
312         surf->current_completed_output = output;
313
314         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
315         weston_view_geometry_dirty(view);
316         weston_surface_damage(view->surface);
317
318         if (ivi_seat)
319                 ivi_shell_activate_surface(surf, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
320
321         /*
322          * the 'remote' role now makes use of this part so make sure we don't
323          * trip the enum such that we might end up with a modified output for
324          * 'remote' role
325          */
326         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
327                 if (surf->desktop.pending_output)
328                         surf->desktop.last_output = surf->desktop.pending_output;
329                 surf->desktop.pending_output = NULL;
330         }
331
332         weston_log("Activation completed for app_id %s, role %s, output %s\n",
333                         app_id,
334                         ivi_layout_get_surface_role_name(surf), output->name);
335
336         shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_ACTIVATED);
337 }
338
339 static bool
340 ivi_layout_find_output_with_app_id(const char *app_id, struct ivi_output *output)
341 {
342         char *cur;
343         size_t app_id_len;
344
345         cur = output->app_ids;
346         app_id_len = strlen(app_id);
347
348         while ((cur = strstr(cur, app_id))) {
349                 if ((cur[app_id_len] == ',' || cur[app_id_len] == '\0') &&
350                     (cur == output->app_ids || cur[-1] == ','))
351                         return true;
352                 cur++;
353         }
354
355         return false;
356 }
357
358 struct ivi_output *
359 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
360 {
361         struct ivi_output *out;
362
363         if (!app_id)
364                 return NULL;
365
366         wl_list_for_each(out, &ivi->outputs, link) {
367                 if (!out->app_ids)
368                         continue;
369
370                 if (ivi_layout_find_output_with_app_id(app_id, out))
371                         return out;
372         }
373         return NULL;
374 }
375
376 struct ivi_output *
377 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
378 {
379         struct ivi_output *out;
380
381         wl_list_for_each(out, &ivi->outputs, link) {
382                 if (out->background &&
383                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
384                         return out;
385         }
386
387         return NULL;
388 }
389
390
391 static void
392 ivi_layout_add_to_hidden_layer(struct ivi_surface *surf,
393                                struct ivi_output *ivi_output)
394 {
395         struct weston_desktop_surface *dsurf = surf->dsurface;
396         struct weston_view *ev = surf->view;
397         struct ivi_compositor *ivi = surf->ivi;
398         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
399
400         /*
401          * If the view isn't mapped, we put it onto the hidden layer so it will
402          * start receiving frame events, and will be able to act on our
403          * configure event.
404          */
405         if (!weston_view_is_mapped(ev)) {
406                 ev->is_mapped = true;
407                 ev->surface->is_mapped = true;
408
409                 weston_desktop_surface_set_maximized(dsurf, true);
410                 weston_desktop_surface_set_size(dsurf,
411                                                 ivi_output->area.width,
412                                                 ivi_output->area.height);
413
414                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
415                            app_id, ivi_layout_get_surface_role_name(surf),
416                            ivi_output->area.width, ivi_output->area.height);
417
418                 surf->hidden_layer_output = ivi_output;
419                 weston_view_set_output(ev, ivi_output->output);
420                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
421                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
422                                 app_id, ivi_layout_get_surface_role_name(surf),
423                                 ivi_output->output->name);
424
425                 weston_compositor_schedule_repaint(ivi->compositor);
426                 return;
427         }
428
429         /* we might have another output to activate */
430         if (surf->hidden_layer_output &&
431             surf->hidden_layer_output != ivi_output) {
432                 weston_layer_entry_remove(&ev->layer_link);
433
434                 if (ivi_output->area.width != surf->hidden_layer_output->area.width &&
435                     ivi_output->area.height != surf->hidden_layer_output->area.height) {
436                         weston_desktop_surface_set_maximized(dsurf, true);
437                         weston_desktop_surface_set_size(dsurf,
438                                                         ivi_output->area.width,
439                                                         ivi_output->area.height);
440                 }
441
442                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
443                                 app_id, ivi_layout_get_surface_role_name(surf),
444                                 ivi_output->area.width, ivi_output->area.height);
445
446                 surf->hidden_layer_output = ivi_output;
447                 weston_view_set_output(ev, ivi_output->output);
448                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
449                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
450                                 app_id, ivi_layout_get_surface_role_name(surf),
451                                 ivi_output->output->name);
452         }
453
454         weston_compositor_schedule_repaint(ivi->compositor);
455 }
456
457 void
458 ivi_layout_remote_committed(struct ivi_surface *surf)
459 {
460         struct weston_desktop_surface *dsurf = surf->dsurface;
461         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
462         struct ivi_policy *policy = surf->ivi->policy;
463         struct ivi_output *output;
464         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
465
466         assert(surf->role == IVI_SURFACE_ROLE_REMOTE);
467
468         output = surf->remote.output;
469
470         if (policy && policy->api.surface_activate_by_default &&
471             !policy->api.surface_activate_by_default(surf, surf->ivi))
472                 return;
473
474         /* we can only activate it again by using the protocol, but
475          * additionally the output is not reset when
476          * ivi_layout_activate_complete() terminates so we use the
477          * current active surface to avoid hitting this again and again
478          * */
479         if (surf->mapped && output->active == surf)
480                 return;
481
482         if (!surf->ivi->activate_by_default &&
483             !ivi_surf_in_hidden_layer(surf->ivi, surf)) {
484                 weston_log("Refusing to activate surface role %d, "
485                             "app_id %s\n", surf->role, app_id);
486
487                 if (!weston_desktop_surface_get_maximized(dsurf) ||
488                     geom.width != output->area.width ||
489                     geom.height != output->area.height) {
490                         ivi_layout_add_to_hidden_layer(surf, output);
491                 }
492
493                 return;
494         }
495
496         if (!weston_desktop_surface_get_maximized(dsurf) ||
497             geom.width != output->area.width ||
498             geom.height != output->area.height)
499                 return;
500
501         ivi_layout_activate_complete(output, surf);
502 }
503
504 void
505 ivi_layout_desktop_committed(struct ivi_surface *surf)
506 {
507         struct weston_desktop_surface *dsurf = surf->dsurface;
508         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
509         struct ivi_policy *policy = surf->ivi->policy;
510         struct ivi_output *output;
511         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
512
513         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
514
515         /*
516          * we can't make use here of the ivi_layout_get_output_from_surface()
517          * due to the fact that we'll always land here when a surface performs
518          * a commit and pending_output will not bet set. This works in tandem
519          * with 'mapped' at this point to avoid tripping over
520          * to a surface that continuously updates its content
521          */
522         output = surf->desktop.pending_output;
523
524         if (!output) {
525                 struct ivi_output *r_output;
526
527                 if (policy && policy->api.surface_activate_by_default &&
528                     !policy->api.surface_activate_by_default(surf, surf->ivi))
529                         return;
530
531                 /* we can only activate it again by using the protocol */
532                 if (surf->mapped)
533                         return;
534
535                 /* check first if there aren't any outputs being set */
536                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
537
538                 if (r_output) {
539                         struct weston_view *view = r_output->fullscreen_view.fs->view;
540                         if (view->is_mapped || view->surface->is_mapped)
541                                 remove_black_curtain(r_output);
542                 }
543
544
545                 /* try finding an output with a background and use that */
546                 if (!r_output)
547                         r_output = ivi_layout_find_bg_output(surf->ivi);
548
549                 /* if we couldn't still find an output by this point, there's
550                  * something wrong so we abort with a protocol error */
551                 if (!r_output) {
552                         wl_resource_post_error(surf->ivi->shell_client.resource,
553                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
554                                                "No valid output found to activate surface by default");
555                         return;
556                 }
557
558                 if (!surf->ivi->activate_by_default) {
559                         weston_log("Refusing to activate surface role %d, app_id %s\n",
560                                         surf->role, app_id);
561
562                         if (!weston_desktop_surface_get_maximized(dsurf) ||
563                             geom.width != r_output->area.width ||
564                             geom.height != r_output->area.height)
565                                 ivi_layout_add_to_hidden_layer(surf, r_output);
566
567                         return;
568                 }
569
570                 /* use the output of the bg to activate the app on start-up by
571                  * default */
572                 if (surf->view && r_output) {
573                         if (app_id && r_output) {
574                                 weston_log("Surface with app_id %s, role %s activating by default\n",
575                                         weston_desktop_surface_get_app_id(surf->dsurface),
576                                         ivi_layout_get_surface_role_name(surf));
577                                 ivi_layout_activate(r_output, app_id);
578                         } else if (!app_id) {
579                                 /*
580                                  * applications not setting an app_id, or
581                                  * setting an app_id but at a later point in
582                                  * time, might fall-back here so give them a
583                                  * chance to receive the configure event and
584                                  * act upon it
585                                  */
586                                 weston_log("Surface no app_id, role %s activating by default\n",
587                                         ivi_layout_get_surface_role_name(surf));
588                                 ivi_layout_activate_by_surf(r_output, surf);
589                         }
590                 }
591
592                 return;
593         }
594
595         if (!weston_desktop_surface_get_maximized(dsurf) ||
596             geom.width != output->area.width ||
597             geom.height != output->area.height)
598                 return;
599
600         ivi_layout_activate_complete(output, surf);
601 }
602
603 void
604 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
605 {
606         struct ivi_compositor *ivi = surface->ivi;
607         struct ivi_policy *policy = ivi->policy;
608
609         struct weston_desktop_surface *dsurface = surface->dsurface;
610         struct weston_surface *wsurface =
611                 weston_desktop_surface_get_surface(dsurface);
612         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
613
614         struct ivi_output *output = surface->split.output;
615         struct weston_output *woutput = output->output;
616         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
617
618         struct weston_view *view = surface->view;
619         struct weston_geometry geom =
620                 weston_desktop_surface_get_geometry(dsurface);
621
622         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
623         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
624
625         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
626         bool is_dim_same =
627                 geom.width == bg_output->output->width &&
628                 geom.height == bg_output->output->height;
629
630         if (policy && policy->api.surface_activate_by_default &&
631             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
632             !surface->mapped)
633                 return;
634
635         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
636
637
638         if (surface->state == FULLSCREEN && weston_view_is_mapped(view))
639                 return;
640
641         /* if we still get here but we haven't resized so far, send configure
642          * events to do so */
643         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
644                 struct ivi_output *bg_output =
645                         ivi_layout_find_bg_output(surface->ivi);
646
647                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
648                                 app_id, ivi_layout_get_surface_role_name(surface));
649                 weston_desktop_surface_set_fullscreen(dsurface, true);
650                 weston_desktop_surface_set_size(dsurface,
651                                                 bg_output->output->width,
652                                                 bg_output->output->height);
653
654                 surface->state = RESIZING;
655                 weston_view_set_output(view, output->output);
656                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
657                 return;
658         }
659
660         /* eventually, we would set the surface fullscreen, but the client
661          * hasn't resized correctly by this point, so terminate connection */
662         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
663                 struct weston_desktop_client *desktop_client =
664                         weston_desktop_surface_get_client(dsurface);
665                 struct wl_client *client =
666                         weston_desktop_client_get_client(desktop_client);
667                 wl_client_post_implementation_error(client,
668                                 "can not display surface due to invalid geometry."
669                                 " Client should perform a geometry resize!");
670                 return;
671         }
672
673         /* this implies we resized correctly */
674         if (!weston_view_is_mapped(view) || surface->state != FULLSCREEN) {
675                 weston_layer_entry_remove(&view->layer_link);
676
677                 weston_view_set_output(view, woutput);
678                 weston_view_set_position(view, woutput->x, woutput->y);
679                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
680
681                 wsurface->is_mapped = true;
682                 surface->view->is_mapped = true;
683                 surface->state = FULLSCREEN;
684
685                 weston_view_geometry_dirty(view);
686                 weston_surface_damage(view->surface);
687
688                 if (ivi_seat)
689                         ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
690
691                 shell_advertise_app_state(ivi, app_id,
692                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
693
694                 weston_log("Activation completed for app_id %s, role %s, "
695                            "output %s\n", app_id,
696                            ivi_layout_get_surface_role_name(surface),
697                            output->name);
698
699         }
700 }
701
702 void
703 ivi_layout_desktop_resize(struct ivi_surface *surface,
704                           struct weston_geometry area)
705 {
706         struct weston_desktop_surface *dsurf = surface->dsurface;
707         struct weston_view *view = surface->view;
708
709         int x = area.x;
710         int y = area.y;
711         int width = area.width;
712         int height = area.height;
713
714         weston_desktop_surface_set_size(dsurf,
715                                         width, height);
716
717         weston_view_set_position(view, x, y);
718
719         weston_view_geometry_dirty(view);
720         weston_surface_damage(view->surface);
721 }
722
723 void
724 ivi_layout_split_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         struct ivi_output *output = surface->split.output;
735         struct weston_output *woutput = output->output;
736
737         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
738         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
739
740         struct weston_view *view = surface->view;
741         struct weston_geometry geom;
742
743         int x, y;
744         int width, height;
745
746         x = woutput->x;
747         y = woutput->y;
748
749         if (policy && policy->api.surface_activate_by_default &&
750             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
751             !surface->mapped)
752                 return;
753
754         if (surface->view->is_mapped)
755                 return;
756
757         geom = weston_desktop_surface_get_geometry(dsurface);
758
759         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
760                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
761
762         /* save the previous area in order to recover it back when if this kind
763          * of surface is being destroyed/removed */
764         output->area_saved = output->area;
765
766         switch (surface->role) {
767         case IVI_SURFACE_ROLE_SPLIT_V:
768                 geom.width = (output->area.width / 2);
769
770                 x += woutput->width - geom.width;
771                 output->area.width -= geom.width;
772
773                 width = woutput->width - x;
774                 height = output->area.height;
775                 y = output->area.y;
776
777                 break;
778         case IVI_SURFACE_ROLE_SPLIT_H:
779                 geom.height = (output->area.height / 2);
780
781                 y = output->area.y;
782                 output->area.y += geom.height;
783                 output->area.height -= geom.height;
784
785                 width = output->area.width;
786                 height = output->area.height;
787
788                 x = output->area.x;
789
790                 break;
791         default:
792                 assert(!"Invalid split orientation\n");
793         }
794
795         weston_desktop_surface_set_size(dsurface,
796                                         width, height);
797
798         /* resize the active surface first, output->area already contains
799          * correct area to resize to */
800         if (output->active)
801                 ivi_layout_desktop_resize(output->active, output->area);
802
803         weston_view_set_output(view, woutput);
804         weston_view_set_position(view, x, y);
805         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
806
807         weston_view_geometry_dirty(view);
808         weston_surface_damage(view->surface);
809
810         if (ivi_seat)
811                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
812
813         wsurface->is_mapped = true;
814         surface->view->is_mapped = true;
815
816         shell_advertise_app_state(ivi, app_id,
817                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
818
819         weston_log("Activation completed for app_id %s, role %s, output %s\n",
820                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
821 }
822
823 static void
824 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
825                            int initial_x, int initial_y, int *new_x, int *new_y)
826 {
827         *new_x = output->x + initial_x;
828         *new_y = output->y + initial_y;
829 }
830
831
832 bool
833 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface)
834 {
835         struct weston_view *ev;
836
837         wl_list_for_each(ev, &ivi->hidden.view_list.link, layer_link.link) {
838                 if (ev == surface->view)
839                         return true;
840         }
841
842         wl_list_for_each(ev, &ivi->fullscreen.view_list.link, layer_link.link) {
843                 if (ev == surface->view)
844                         return true;
845         }
846
847         return false;
848 }
849
850 void
851 ivi_layout_popup_committed(struct ivi_surface *surface)
852 {
853         struct ivi_compositor *ivi = surface->ivi;
854         struct ivi_policy *policy = ivi->policy;
855
856         struct weston_desktop_surface *dsurface = surface->dsurface;
857         struct weston_surface *wsurface =
858                 weston_desktop_surface_get_surface(dsurface);
859         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
860
861         int new_x, new_y;
862
863         struct ivi_output *output = surface->popup.output;
864         struct weston_output *woutput = output->output;
865
866         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
867         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
868
869         struct weston_view *view = surface->view;
870
871         if (policy && policy->api.surface_activate_by_default &&
872             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
873             !surface->mapped)
874                 return;
875
876         if (surface->view->is_mapped || surface->state == HIDDEN)
877                 return;
878
879         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
880
881         /* remove it from hidden layer if present */
882         if (ivi_surf_in_hidden_layer(ivi, surface))
883                 weston_layer_entry_remove(&view->layer_link);
884
885         weston_view_set_output(view, woutput);
886
887         ivi_compute_popup_position(woutput, view,
888                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
889         weston_view_set_position(view, new_x, new_y);
890         weston_view_update_transform(view);
891
892         /* only clip the pop-up dialog window if we have a valid
893          * width and height being passed on. Users might not want to have one
894          * set-up so only enfore it is really passed on. */
895         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
896                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
897                                      surface->popup.bb.width, surface->popup.bb.height);
898
899         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
900
901         weston_view_geometry_dirty(view);
902         weston_surface_damage(view->surface);
903
904         if (ivi_seat)
905                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
906
907         wsurface->is_mapped = true;
908         surface->view->is_mapped = true;
909
910         shell_advertise_app_state(ivi, app_id,
911                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
912
913         weston_log("Activation completed for app_id %s, role %s, output %s\n",
914                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
915 }
916
917 static void
918 ivi_layout_popup_re_add(struct ivi_surface *surface)
919 {
920         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
921         struct weston_view *view = surface->view;
922
923         if (weston_view_is_mapped(view)) {
924                 struct weston_desktop_surface *dsurface = surface->dsurface;
925                 struct weston_surface *wsurface =
926                         weston_desktop_surface_get_surface(dsurface);
927
928                 weston_layer_entry_remove(&view->layer_link);
929
930                 wsurface->is_mapped = false;
931                 view->is_mapped = false;
932         }
933
934         /* reset the activate by default in order to (still) allow the surface
935          * to be activaved using the request */
936         if (!surface->mapped)
937                 surface->mapped = true;
938
939         surface->state = NORMAL;
940         ivi_layout_popup_committed(surface);
941 }
942
943 static bool
944 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
945 {
946         struct ivi_compositor *ivi = surf->ivi;
947         struct ivi_surface *is;
948
949         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
950             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
951             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
952                 return false;
953
954         /* reset the activate by default in order to (still) allow the surface
955          * to be activaved using the request */
956         if (!surf->mapped)
957                 surf->mapped = true;
958
959         wl_list_for_each(is, &ivi->surfaces, link)
960                 if (is == surf)
961                         return true;
962
963         return false;
964 }
965
966 void
967 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
968 {
969         struct ivi_compositor *ivi = output->ivi;
970         struct weston_desktop_surface *dsurf;
971         struct weston_geometry geom;
972         struct ivi_policy *policy = output->ivi->policy;
973
974         dsurf = surf->dsurface;
975
976         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
977
978         if (!surf)
979                 return;
980
981         if (policy && policy->api.surface_activate &&
982             !policy->api.surface_activate(surf, surf->ivi)) {
983                 return;
984         }
985
986 #ifdef AGL_COMP_DEBUG
987         weston_log("Activating app_id %s, type %s, on output %s\n", app_id,
988                         ivi_layout_get_surface_role_name(surf), output->output->name);
989 #endif
990
991         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
992                 ivi_layout_popup_re_add(surf);
993                 return;
994         }
995
996         /* do not 're'-activate surfaces that are split or active */
997         if (surf == output->active ||
998             ivi_layout_surface_is_split_or_fullscreen(surf)) {
999                 weston_log("Application %s is already active on output %s\n",
1000                                 app_id, output->output->name);
1001                 return;
1002         }
1003
1004         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
1005                 struct ivi_output *remote_output =
1006                         ivi_layout_find_with_app_id(app_id, ivi);
1007
1008                 weston_log("Changed activation for app_id %s, type %s, on output %s\n", app_id,
1009                                 ivi_layout_get_surface_role_name(surf), output->output->name);
1010
1011                 /* if already active on a remote output do not
1012                  * attempt to activate it again */
1013                 if (remote_output && remote_output->active == surf)
1014                         return;
1015         }
1016
1017
1018         geom = weston_desktop_surface_get_geometry(dsurf);
1019
1020         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
1021                 surf->desktop.pending_output = output;
1022         if (weston_desktop_surface_get_maximized(dsurf) &&
1023             geom.width == output->area.width &&
1024             geom.height == output->area.height) {
1025                 ivi_layout_activate_complete(output, surf);
1026                 return;
1027         }
1028
1029         /* the background surface is already "maximized" so we don't need to
1030          * add to the hidden layer */
1031         if (surf->role == IVI_SURFACE_ROLE_BACKGROUND) {
1032                 ivi_layout_activate_complete(output, surf);
1033                 return;
1034         }
1035
1036         ivi_layout_add_to_hidden_layer(surf, output);
1037 }
1038
1039 void
1040 ivi_layout_activate(struct ivi_output *output, const char *app_id)
1041 {
1042         struct ivi_surface *surf;
1043         struct ivi_compositor *ivi = output->ivi;
1044
1045         if (!app_id)
1046                 return;
1047
1048         surf = ivi_find_app(ivi, app_id);
1049         if (!surf)
1050                 return;
1051
1052         ivi_layout_activate_by_surf(output, surf);
1053 }
1054
1055 struct ivi_output *
1056 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
1057 {
1058         struct ivi_output *ivi_output = NULL;
1059
1060         switch (surf->role) {
1061         case IVI_SURFACE_ROLE_DESKTOP:
1062                 if (surf->desktop.pending_output)
1063                         ivi_output = surf->desktop.pending_output;
1064                 else
1065                         ivi_output = surf->desktop.last_output;
1066                 break;
1067         case IVI_SURFACE_ROLE_POPUP:
1068                 ivi_output = surf->popup.output;
1069                 break;
1070         case IVI_SURFACE_ROLE_BACKGROUND:
1071                 ivi_output = surf->bg.output;
1072                 break;
1073         case IVI_SURFACE_ROLE_PANEL:
1074                 ivi_output = surf->panel.output;
1075                 break;
1076         case IVI_SURFACE_ROLE_FULLSCREEN:
1077                 ivi_output = surf->fullscreen.output;
1078                 break;
1079         case IVI_SURFACE_ROLE_SPLIT_H:
1080         case IVI_SURFACE_ROLE_SPLIT_V:
1081                 ivi_output = surf->split.output;
1082                 break;
1083         case IVI_SURFACE_ROLE_REMOTE:
1084                 ivi_output = surf->remote.output;
1085                 break;
1086         case IVI_SURFACE_ROLE_NONE:
1087         default:
1088                 break;
1089         }
1090
1091         return ivi_output;
1092 }
1093
1094 void
1095 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
1096 {
1097         struct ivi_surface *surf;
1098         struct ivi_output *ivi_output;
1099         struct ivi_policy *policy = ivi->policy;
1100
1101         if (!app_id)
1102                 return;
1103
1104         surf = ivi_find_app(ivi, app_id);
1105         if (!surf)
1106                 return;
1107
1108         if (policy && policy->api.surface_deactivate &&
1109             !policy->api.surface_deactivate(surf, surf->ivi)) {
1110                 return;
1111         }
1112
1113         ivi_output = ivi_layout_get_output_from_surface(surf);
1114         weston_log("Deactiving %s, role %s\n", app_id,
1115                         ivi_layout_get_surface_role_name(surf));
1116
1117         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
1118                 struct ivi_surface *previous_active;
1119
1120                 previous_active = ivi_output->previous_active;
1121                 if (!previous_active) {
1122                         /* we don't have a previous active it means we should
1123                          * display the bg */
1124                         if (ivi_output->active) {
1125                                 struct weston_view *view;
1126
1127                                 view = ivi_output->active->view;
1128                                 view->is_mapped = false;
1129                                 view->surface->is_mapped = false;
1130
1131                                 weston_layer_entry_remove(&view->layer_link);
1132                                 weston_view_geometry_dirty(view);
1133                                 weston_surface_damage(view->surface);
1134                                 ivi_output->active = NULL;
1135                         }
1136                 } else {
1137                         struct weston_desktop_surface *dsurface;
1138                         const char *previous_active_app_id;
1139
1140                         dsurface = previous_active->dsurface;
1141                         previous_active_app_id =
1142                                 weston_desktop_surface_get_app_id(dsurface);
1143                         ivi_layout_activate(ivi_output, previous_active_app_id);
1144                 }
1145         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1146                 struct weston_view *view  = surf->view;
1147
1148                 weston_view_unmap(view);
1149                 surf->state = HIDDEN;
1150
1151                 weston_layer_entry_remove(&view->layer_link);
1152                 weston_view_geometry_dirty(view);
1153                 weston_surface_damage(view->surface);
1154         }
1155
1156       shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_DEACTIVATED);
1157 }