Testing
[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("Surface is not mapped at all\n");
415                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
416                            app_id, ivi_layout_get_surface_role_name(surf),
417                            ivi_output->area.width, ivi_output->area.height);
418
419                 surf->hidden_layer_output = ivi_output;
420                 weston_view_set_output(ev, ivi_output->output);
421                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
422                 weston_view_geometry_dirty(ev);
423                 weston_surface_damage(ev->surface);
424
425                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
426                                 app_id, ivi_layout_get_surface_role_name(surf),
427                                 ivi_output->output->name);
428
429                 weston_compositor_schedule_repaint(ivi->compositor);
430                 return;
431         }
432
433         /* we might have another output to activate */
434         if (surf->hidden_layer_output &&
435             surf->hidden_layer_output != ivi_output) {
436                 weston_layer_entry_remove(&ev->layer_link);
437                 weston_view_geometry_dirty(ev);
438                 weston_surface_damage(ev->surface);
439
440                 if (ivi_output->area.width != surf->hidden_layer_output->area.width ||
441                     ivi_output->area.height != surf->hidden_layer_output->area.height) {
442                         weston_desktop_surface_set_maximized(dsurf, true);
443                         weston_desktop_surface_set_size(dsurf,
444                                                         ivi_output->area.width,
445                                                         ivi_output->area.height);
446                 }
447
448                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
449                                 app_id, ivi_layout_get_surface_role_name(surf),
450                                 ivi_output->area.width, ivi_output->area.height);
451
452                 surf->hidden_layer_output = ivi_output;
453                 weston_view_set_output(ev, ivi_output->output);
454                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
455                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
456                                 app_id, ivi_layout_get_surface_role_name(surf),
457                                 ivi_output->output->name);
458         }
459
460         weston_compositor_schedule_repaint(ivi->compositor);
461 }
462
463 void
464 ivi_layout_remote_committed(struct ivi_surface *surf)
465 {
466         struct weston_desktop_surface *dsurf = surf->dsurface;
467         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
468         struct ivi_policy *policy = surf->ivi->policy;
469         struct ivi_output *output;
470         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
471
472         assert(surf->role == IVI_SURFACE_ROLE_REMOTE);
473
474         output = surf->remote.output;
475
476         if (policy && policy->api.surface_activate_by_default &&
477             !policy->api.surface_activate_by_default(surf, surf->ivi))
478                 return;
479
480         /* we can only activate it again by using the protocol, but
481          * additionally the output is not reset when
482          * ivi_layout_activate_complete() terminates so we use the
483          * current active surface to avoid hitting this again and again
484          * */
485         if (surf->mapped && output->active == surf)
486                 return;
487
488         if (!surf->ivi->activate_by_default &&
489             !ivi_surf_in_hidden_layer(surf->ivi, surf)) {
490                 weston_log("Refusing to activate surface role %d, "
491                             "app_id %s\n", surf->role, app_id);
492
493                 if (!weston_desktop_surface_get_maximized(dsurf) ||
494                     geom.width != output->area.width ||
495                     geom.height != output->area.height) {
496                         ivi_layout_add_to_hidden_layer(surf, output);
497                 }
498
499                 return;
500         }
501
502         if (!weston_desktop_surface_get_maximized(dsurf) ||
503             geom.width != output->area.width ||
504             geom.height != output->area.height)
505                 return;
506
507         ivi_layout_activate_complete(output, surf);
508 }
509
510 void
511 ivi_layout_desktop_committed(struct ivi_surface *surf)
512 {
513         struct weston_desktop_surface *dsurf = surf->dsurface;
514         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
515         struct ivi_policy *policy = surf->ivi->policy;
516         struct ivi_output *output;
517         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
518
519         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
520
521         /*
522          * we can't make use here of the ivi_layout_get_output_from_surface()
523          * due to the fact that we'll always land here when a surface performs
524          * a commit and pending_output will not bet set. This works in tandem
525          * with 'mapped' at this point to avoid tripping over
526          * to a surface that continuously updates its content
527          */
528         output = surf->desktop.pending_output;
529
530         if (!output) {
531                 struct ivi_output *r_output;
532
533                 if (policy && policy->api.surface_activate_by_default &&
534                     !policy->api.surface_activate_by_default(surf, surf->ivi))
535                         return;
536
537                 /* we can only activate it again by using the protocol */
538                 if (surf->mapped)
539                         return;
540
541                 /* check first if there aren't any outputs being set */
542                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
543
544                 if (r_output) {
545                         struct weston_view *view = r_output->fullscreen_view.fs->view;
546                         if (view->is_mapped || view->surface->is_mapped)
547                                 remove_black_curtain(r_output);
548                 }
549
550
551                 /* try finding an output with a background and use that */
552                 if (!r_output)
553                         r_output = ivi_layout_find_bg_output(surf->ivi);
554
555                 /* if we couldn't still find an output by this point, there's
556                  * something wrong so we abort with a protocol error */
557                 if (!r_output) {
558                         wl_resource_post_error(surf->ivi->shell_client.resource,
559                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
560                                                "No valid output found to activate surface by default");
561                         return;
562                 }
563
564                 if (!surf->ivi->activate_by_default) {
565                         weston_log("Refusing to activate surface role %d, app_id %s\n",
566                                         surf->role, app_id);
567
568                         if (!weston_desktop_surface_get_maximized(dsurf) ||
569                             geom.width != r_output->area.width ||
570                             geom.height != r_output->area.height)
571                                 ivi_layout_add_to_hidden_layer(surf, r_output);
572
573                         return;
574                 }
575
576                 /* use the output of the bg to activate the app on start-up by
577                  * default */
578                 if (surf->view && r_output) {
579                         if (app_id && r_output) {
580                                 weston_log("Surface with app_id %s, role %s activating by default\n",
581                                         weston_desktop_surface_get_app_id(surf->dsurface),
582                                         ivi_layout_get_surface_role_name(surf));
583                                 ivi_layout_activate(r_output, app_id);
584                         } else if (!app_id) {
585                                 /*
586                                  * applications not setting an app_id, or
587                                  * setting an app_id but at a later point in
588                                  * time, might fall-back here so give them a
589                                  * chance to receive the configure event and
590                                  * act upon it
591                                  */
592                                 weston_log("Surface no app_id, role %s activating by default\n",
593                                         ivi_layout_get_surface_role_name(surf));
594                                 if (surf->xwayland.is_set) {
595                                         ivi_layout_activate_by_surf(r_output, surf);
596                                         ivi_layout_activate_complete(r_output, surf);
597                                 } else {
598                                         ivi_layout_activate_by_surf(r_output, surf);
599                                 }
600                         }
601                 }
602
603                 return;
604         }
605
606         if (!weston_desktop_surface_get_maximized(dsurf) ||
607             geom.width != output->area.width ||
608             geom.height != output->area.height)
609                 return;
610
611         ivi_layout_activate_complete(output, surf);
612 }
613
614 void
615 ivi_layout_fullscreen_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         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
628
629         struct weston_view *view = surface->view;
630         struct weston_geometry geom =
631                 weston_desktop_surface_get_geometry(dsurface);
632
633         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
634         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
635
636         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
637         bool is_dim_same =
638                 geom.width == bg_output->output->width &&
639                 geom.height == bg_output->output->height;
640
641         if (policy && policy->api.surface_activate_by_default &&
642             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
643             !surface->mapped)
644                 return;
645
646         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
647
648
649         if (surface->state == FULLSCREEN && weston_view_is_mapped(view))
650                 return;
651
652         /* if we still get here but we haven't resized so far, send configure
653          * events to do so */
654         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
655                 struct ivi_output *bg_output =
656                         ivi_layout_find_bg_output(surface->ivi);
657
658                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
659                                 app_id, ivi_layout_get_surface_role_name(surface));
660                 weston_desktop_surface_set_fullscreen(dsurface, true);
661                 weston_desktop_surface_set_size(dsurface,
662                                                 bg_output->output->width,
663                                                 bg_output->output->height);
664
665                 surface->state = RESIZING;
666                 weston_view_set_output(view, output->output);
667                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
668                 return;
669         }
670
671         /* eventually, we would set the surface fullscreen, but the client
672          * hasn't resized correctly by this point, so terminate connection */
673         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
674                 struct weston_desktop_client *desktop_client =
675                         weston_desktop_surface_get_client(dsurface);
676                 struct wl_client *client =
677                         weston_desktop_client_get_client(desktop_client);
678                 wl_client_post_implementation_error(client,
679                                 "can not display surface due to invalid geometry."
680                                 " Client should perform a geometry resize!");
681                 return;
682         }
683
684         /* this implies we resized correctly */
685         if (!weston_view_is_mapped(view) || surface->state != FULLSCREEN) {
686                 weston_layer_entry_remove(&view->layer_link);
687
688                 weston_view_set_output(view, woutput);
689                 weston_view_set_position(view, woutput->x, woutput->y);
690                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
691
692                 wsurface->is_mapped = true;
693                 surface->view->is_mapped = true;
694                 surface->state = FULLSCREEN;
695
696                 weston_view_geometry_dirty(view);
697                 weston_surface_damage(view->surface);
698
699                 if (ivi_seat)
700                         ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
701
702                 shell_advertise_app_state(ivi, app_id,
703                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
704
705                 weston_log("Activation completed for app_id %s, role %s, "
706                            "output %s\n", app_id,
707                            ivi_layout_get_surface_role_name(surface),
708                            output->name);
709
710         }
711 }
712
713 void
714 ivi_layout_desktop_resize(struct ivi_surface *surface,
715                           struct weston_geometry area)
716 {
717         struct weston_desktop_surface *dsurf = surface->dsurface;
718         struct weston_view *view = surface->view;
719
720         int x = area.x;
721         int y = area.y;
722         int width = area.width;
723         int height = area.height;
724
725         weston_desktop_surface_set_size(dsurf,
726                                         width, height);
727
728         weston_view_set_position(view, x, y);
729
730         weston_view_geometry_dirty(view);
731         weston_surface_damage(view->surface);
732 }
733
734 void
735 ivi_layout_split_committed(struct ivi_surface *surface)
736 {
737         struct ivi_compositor *ivi = surface->ivi;
738         struct ivi_policy *policy = ivi->policy;
739
740         struct weston_desktop_surface *dsurface = surface->dsurface;
741         struct weston_surface *wsurface =
742                 weston_desktop_surface_get_surface(dsurface);
743         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
744
745         struct ivi_output *output = surface->split.output;
746         struct weston_output *woutput = output->output;
747
748         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
749         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
750
751         struct weston_view *view = surface->view;
752         struct weston_geometry geom;
753
754         int x, y;
755         int width, height;
756
757         x = woutput->x;
758         y = woutput->y;
759
760         if (policy && policy->api.surface_activate_by_default &&
761             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
762             !surface->mapped)
763                 return;
764
765         if (surface->view->is_mapped)
766                 return;
767
768         geom = weston_desktop_surface_get_geometry(dsurface);
769
770         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
771                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
772
773         /* save the previous area in order to recover it back when if this kind
774          * of surface is being destroyed/removed */
775         output->area_saved = output->area;
776
777         switch (surface->role) {
778         case IVI_SURFACE_ROLE_SPLIT_V:
779                 geom.width = (output->area.width / 2);
780
781                 x += woutput->width - geom.width;
782                 output->area.width -= geom.width;
783
784                 width = woutput->width - x;
785                 height = output->area.height;
786                 y = output->area.y;
787
788                 break;
789         case IVI_SURFACE_ROLE_SPLIT_H:
790                 geom.height = (output->area.height / 2);
791
792                 y = output->area.y;
793                 output->area.y += geom.height;
794                 output->area.height -= geom.height;
795
796                 width = output->area.width;
797                 height = output->area.height;
798
799                 x = output->area.x;
800
801                 break;
802         default:
803                 assert(!"Invalid split orientation\n");
804         }
805
806         weston_desktop_surface_set_size(dsurface,
807                                         width, height);
808
809         /* resize the active surface first, output->area already contains
810          * correct area to resize to */
811         if (output->active)
812                 ivi_layout_desktop_resize(output->active, output->area);
813
814         weston_view_set_output(view, woutput);
815         weston_view_set_position(view, x, y);
816         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
817
818         weston_view_geometry_dirty(view);
819         weston_surface_damage(view->surface);
820
821         if (ivi_seat)
822                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
823
824         wsurface->is_mapped = true;
825         surface->view->is_mapped = true;
826
827         shell_advertise_app_state(ivi, app_id,
828                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
829
830         weston_log("Activation completed for app_id %s, role %s, output %s\n",
831                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
832 }
833
834 static void
835 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
836                            int initial_x, int initial_y, int *new_x, int *new_y)
837 {
838         *new_x = output->x + initial_x;
839         *new_y = output->y + initial_y;
840 }
841
842
843 bool
844 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface)
845 {
846         struct weston_view *ev;
847
848         wl_list_for_each(ev, &ivi->hidden.view_list.link, layer_link.link) {
849                 if (ev == surface->view)
850                         return true;
851         }
852
853         wl_list_for_each(ev, &ivi->fullscreen.view_list.link, layer_link.link) {
854                 if (ev == surface->view)
855                         return true;
856         }
857
858         return false;
859 }
860
861 void
862 ivi_layout_popup_committed(struct ivi_surface *surface)
863 {
864         struct ivi_compositor *ivi = surface->ivi;
865         struct ivi_policy *policy = ivi->policy;
866
867         struct weston_desktop_surface *dsurface = surface->dsurface;
868         struct weston_surface *wsurface =
869                 weston_desktop_surface_get_surface(dsurface);
870         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
871
872         int new_x, new_y;
873
874         struct ivi_output *output = surface->popup.output;
875         struct weston_output *woutput = output->output;
876
877         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
878         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
879
880         struct weston_view *view = surface->view;
881
882         if (policy && policy->api.surface_activate_by_default &&
883             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
884             !surface->mapped)
885                 return;
886
887         if (surface->view->is_mapped || surface->state == HIDDEN)
888                 return;
889
890         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
891
892         /* remove it from hidden layer if present */
893         if (ivi_surf_in_hidden_layer(ivi, surface))
894                 weston_layer_entry_remove(&view->layer_link);
895
896         weston_view_set_output(view, woutput);
897
898         ivi_compute_popup_position(woutput, view,
899                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
900         weston_view_set_position(view, new_x, new_y);
901         weston_view_update_transform(view);
902
903         /* only clip the pop-up dialog window if we have a valid
904          * width and height being passed on. Users might not want to have one
905          * set-up so only enfore it is really passed on. */
906         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
907                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
908                                      surface->popup.bb.width, surface->popup.bb.height);
909
910         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
911
912         weston_view_geometry_dirty(view);
913         weston_surface_damage(view->surface);
914
915         if (ivi_seat)
916                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
917
918         wsurface->is_mapped = true;
919         surface->view->is_mapped = true;
920
921         shell_advertise_app_state(ivi, app_id,
922                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
923
924         weston_log("Activation completed for app_id %s, role %s, output %s\n",
925                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
926 }
927
928 static void
929 ivi_layout_popup_re_add(struct ivi_surface *surface)
930 {
931         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
932         struct weston_view *view = surface->view;
933
934         if (weston_view_is_mapped(view)) {
935                 struct weston_desktop_surface *dsurface = surface->dsurface;
936                 struct weston_surface *wsurface =
937                         weston_desktop_surface_get_surface(dsurface);
938
939                 weston_layer_entry_remove(&view->layer_link);
940
941                 wsurface->is_mapped = false;
942                 view->is_mapped = false;
943         }
944
945         /* reset the activate by default in order to (still) allow the surface
946          * to be activaved using the request */
947         if (!surface->mapped)
948                 surface->mapped = true;
949
950         surface->state = NORMAL;
951         ivi_layout_popup_committed(surface);
952 }
953
954 static bool
955 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
956 {
957         struct ivi_compositor *ivi = surf->ivi;
958         struct ivi_surface *is;
959
960         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
961             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
962             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
963                 return false;
964
965         /* reset the activate by default in order to (still) allow the surface
966          * to be activaved using the request */
967         if (!surf->mapped)
968                 surf->mapped = true;
969
970         wl_list_for_each(is, &ivi->surfaces, link)
971                 if (is == surf)
972                         return true;
973
974         return false;
975 }
976
977 void
978 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
979 {
980         struct ivi_compositor *ivi = output->ivi;
981         struct weston_desktop_surface *dsurf;
982         struct weston_geometry geom;
983         struct ivi_policy *policy = output->ivi->policy;
984
985         dsurf = surf->dsurface;
986
987         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
988
989         if (!surf)
990                 return;
991
992         if (policy && policy->api.surface_activate &&
993             !policy->api.surface_activate(surf, surf->ivi)) {
994                 return;
995         }
996
997 #ifdef AGL_COMP_DEBUG
998         weston_log("Activating app_id %s, type %s, on output %s\n", app_id,
999                         ivi_layout_get_surface_role_name(surf), output->output->name);
1000 #endif
1001
1002         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1003                 ivi_layout_popup_re_add(surf);
1004                 return;
1005         }
1006
1007         /* do not 're'-activate surfaces that are split or active */
1008         if (surf == output->active ||
1009             ivi_layout_surface_is_split_or_fullscreen(surf)) {
1010                 weston_log("Application %s is already active on output %s\n",
1011                                 app_id, output->output->name);
1012                 return;
1013         }
1014
1015         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
1016                 struct ivi_output *remote_output =
1017                         ivi_layout_find_with_app_id(app_id, ivi);
1018
1019                 weston_log("Changed activation for app_id %s, type %s, on output %s\n", app_id,
1020                                 ivi_layout_get_surface_role_name(surf), output->output->name);
1021
1022                 /* if already active on a remote output do not
1023                  * attempt to activate it again */
1024                 if (remote_output && remote_output->active == surf)
1025                         return;
1026         }
1027
1028
1029         geom = weston_desktop_surface_get_geometry(dsurf);
1030
1031         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
1032                 surf->desktop.pending_output = output;
1033         if (weston_desktop_surface_get_maximized(dsurf) &&
1034             geom.width == output->area.width &&
1035             geom.height == output->area.height) {
1036                 ivi_layout_activate_complete(output, surf);
1037                 return;
1038         }
1039
1040         /* the background surface is already "maximized" so we don't need to
1041          * add to the hidden layer */
1042         if (surf->role == IVI_SURFACE_ROLE_BACKGROUND) {
1043                 ivi_layout_activate_complete(output, surf);
1044                 return;
1045         }
1046
1047         ivi_layout_add_to_hidden_layer(surf, output);
1048 }
1049
1050 void
1051 ivi_layout_activate(struct ivi_output *output, const char *app_id)
1052 {
1053         struct ivi_surface *surf;
1054         struct ivi_compositor *ivi = output->ivi;
1055
1056         if (!app_id)
1057                 return;
1058
1059         surf = ivi_find_app(ivi, app_id);
1060         if (!surf)
1061                 return;
1062
1063         ivi_layout_activate_by_surf(output, surf);
1064 }
1065
1066 struct ivi_output *
1067 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
1068 {
1069         struct ivi_output *ivi_output = NULL;
1070
1071         switch (surf->role) {
1072         case IVI_SURFACE_ROLE_DESKTOP:
1073                 if (surf->desktop.pending_output)
1074                         ivi_output = surf->desktop.pending_output;
1075                 else
1076                         ivi_output = surf->desktop.last_output;
1077                 break;
1078         case IVI_SURFACE_ROLE_POPUP:
1079                 ivi_output = surf->popup.output;
1080                 break;
1081         case IVI_SURFACE_ROLE_BACKGROUND:
1082                 ivi_output = surf->bg.output;
1083                 break;
1084         case IVI_SURFACE_ROLE_PANEL:
1085                 ivi_output = surf->panel.output;
1086                 break;
1087         case IVI_SURFACE_ROLE_FULLSCREEN:
1088                 ivi_output = surf->fullscreen.output;
1089                 break;
1090         case IVI_SURFACE_ROLE_SPLIT_H:
1091         case IVI_SURFACE_ROLE_SPLIT_V:
1092                 ivi_output = surf->split.output;
1093                 break;
1094         case IVI_SURFACE_ROLE_REMOTE:
1095                 ivi_output = surf->remote.output;
1096                 break;
1097         case IVI_SURFACE_ROLE_NONE:
1098         default:
1099                 break;
1100         }
1101
1102         return ivi_output;
1103 }
1104
1105 void
1106 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
1107 {
1108         struct ivi_surface *surf;
1109         struct ivi_output *ivi_output;
1110         struct ivi_policy *policy = ivi->policy;
1111
1112         if (!app_id)
1113                 return;
1114
1115         surf = ivi_find_app(ivi, app_id);
1116         if (!surf)
1117                 return;
1118
1119         if (policy && policy->api.surface_deactivate &&
1120             !policy->api.surface_deactivate(surf, surf->ivi)) {
1121                 return;
1122         }
1123
1124         ivi_output = ivi_layout_get_output_from_surface(surf);
1125         weston_log("Deactiving %s, role %s\n", app_id,
1126                         ivi_layout_get_surface_role_name(surf));
1127
1128         if (surf->role == IVI_SURFACE_ROLE_DESKTOP ||
1129             surf->role == IVI_SURFACE_ROLE_REMOTE) {
1130                 struct ivi_surface *previous_active;
1131
1132                 previous_active = ivi_output->previous_active;
1133                 if (!previous_active) {
1134                         /* we don't have a previous active it means we should
1135                          * display the bg */
1136                         if (ivi_output->active) {
1137                                 struct weston_view *view;
1138
1139                                 view = ivi_output->active->view;
1140                                 view->is_mapped = false;
1141                                 view->surface->is_mapped = false;
1142
1143                                 weston_layer_entry_remove(&view->layer_link);
1144                                 weston_view_geometry_dirty(view);
1145                                 weston_surface_damage(view->surface);
1146                                 ivi_output->active = NULL;
1147                         }
1148                 } else {
1149                         struct weston_desktop_surface *dsurface;
1150                         const char *previous_active_app_id;
1151
1152                         dsurface = previous_active->dsurface;
1153                         previous_active_app_id =
1154                                 weston_desktop_surface_get_app_id(dsurface);
1155                         ivi_layout_activate(ivi_output, previous_active_app_id);
1156                 }
1157         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1158                 struct weston_view *view  = surf->view;
1159
1160                 weston_view_unmap(view);
1161                 surf->state = HIDDEN;
1162
1163                 weston_layer_entry_remove(&view->layer_link);
1164                 weston_view_geometry_dirty(view);
1165                 weston_surface_damage(view->surface);
1166         }
1167
1168       shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_DEACTIVATED);
1169 }