Add manual activation area configuration option
[src/agl-compositor.git] / src / layout.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28 #include "shared/helpers.h"
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include <libweston/libweston.h>
34 #include <libweston-desktop/libweston-desktop.h>
35
36 #include "agl-shell-desktop-server-protocol.h"
37
38 #define AGL_COMP_DEBUG
39
40 static const char *ivi_roles_as_string[] = {
41         [IVI_SURFACE_ROLE_NONE]         = "NONE",
42         [IVI_SURFACE_ROLE_BACKGROUND]   = "BACKGROUND",
43         [IVI_SURFACE_ROLE_PANEL]        = "PANEL",
44         [IVI_SURFACE_ROLE_DESKTOP]      = "DESKTOP",
45         [IVI_SURFACE_ROLE_POPUP]        = "POPUP",
46         [IVI_SURFACE_ROLE_SPLIT_H]      = "SPLIT_H",
47         [IVI_SURFACE_ROLE_SPLIT_V]      = "SPLIT_V",
48         [IVI_SURFACE_ROLE_FULLSCREEN]   = "FULLSCREEN",
49         [IVI_SURFACE_ROLE_REMOTE]       = "REMOTE",
50 };
51
52 const char *
53 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
54 {
55         if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
56                 return " unknown surface role";
57
58         return ivi_roles_as_string[surf->role];
59 }
60
61 static void
62 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
63 {
64         struct weston_output *woutput = output->output;
65         struct ivi_surface *bg = output->background;
66         struct weston_view *view;
67
68         if (!bg) {
69                 weston_log("WARNING: Output does not have a background\n");
70                 return;
71         }
72
73         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
74
75         view = bg->view;
76
77         weston_view_set_output(view, woutput);
78         weston_view_set_position(view, woutput->x, woutput->y);
79
80         weston_log("(background) position view %p, x %d, y %d, on output %s\n", view,
81                         woutput->x, woutput->y, output->name);
82
83         view->is_mapped = true;
84         view->surface->is_mapped = true;
85
86         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
87 }
88
89 static void
90 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
91                struct ivi_surface *panel)
92 {
93         struct weston_output *woutput = output->output;
94         struct weston_desktop_surface *dsurface;
95         struct weston_view *view;
96         struct weston_geometry geom;
97         int x = woutput->x;
98         int y = woutput->y;
99
100         if (!panel)
101                 return;
102
103         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
104         dsurface = panel->dsurface;
105         view = panel->view;
106         geom = weston_desktop_surface_get_geometry(dsurface);
107
108         weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
109                         geom.width, geom.height, geom.x, geom.y);
110
111         switch (panel->panel.edge) {
112         case AGL_SHELL_EDGE_TOP:
113                 output->area.y += geom.height;
114                 output->area.height -= geom.height;
115                 break;
116         case AGL_SHELL_EDGE_BOTTOM:
117                 y += woutput->height - geom.height;
118                 output->area.height -= geom.height;
119                 break;
120         case AGL_SHELL_EDGE_LEFT:
121                 output->area.x += geom.width;
122                 output->area.width -= geom.width;
123                 break;
124         case AGL_SHELL_EDGE_RIGHT:
125                 x += woutput->width - geom.width;
126                 output->area.width -= geom.width;
127                 break;
128         }
129
130         x -= geom.x;
131         y -= geom.y;
132
133         weston_view_set_output(view, woutput);
134         weston_view_set_position(view, x, y);
135
136         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
137                         panel->panel.edge, view, x, y);
138
139         view->is_mapped = true;
140         view->surface->is_mapped = true;
141
142         weston_log("panel type %d inited on output %s\n", panel->panel.edge,
143                         output->name);
144
145         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
146 }
147
148 /*
149  * Initializes all static parts of the layout, i.e. the background and panels.
150  */
151 void
152 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
153 {
154         bool use_default_area = true;
155
156         ivi_background_init(ivi, output);
157
158         if (output->area_activation.width ||
159             output->area_activation.height ||
160             output->area_activation.x ||
161             output->area_activation.y) {
162                 /* Sanity check target area is within output bounds */
163                 if ((output->area_activation.x + output->area_activation.width) < output->output->width ||
164                     (output->area_activation.y + output->area_activation.height) < output->output->height) {
165                         weston_log("Using specified area for output %s, ignoring panels\n",
166                                    output->name);
167                         output->area.x = output->area_activation.x;
168                         output->area.y = output->area_activation.y;
169                         output->area.width = output->area_activation.width;
170                         output->area.height = output->area_activation.height;
171                         use_default_area = false;
172                 } else {
173                         weston_log("Invalid activation-area position for output %s, ignoring\n",
174                                    output->name);
175                 }
176         }
177         if (use_default_area) {
178                 output->area.x = 0;
179                 output->area.y = 0;
180                 output->area.width = output->output->width;
181                 output->area.height = output->output->height;
182
183                 ivi_panel_init(ivi, output, output->top);
184                 ivi_panel_init(ivi, output, output->bottom);
185                 ivi_panel_init(ivi, output, output->left);
186                 ivi_panel_init(ivi, output, output->right);
187         }
188
189         weston_compositor_schedule_repaint(ivi->compositor);
190
191         weston_log("Usable area: %dx%d+%d,%d\n",
192                    output->area.width, output->area.height,
193                    output->area.x, output->area.y);
194 }
195
196 struct ivi_surface *
197 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
198 {
199         struct ivi_surface *surf;
200         const char *id;
201
202         wl_list_for_each(surf, &ivi->surfaces, link) {
203                 id = weston_desktop_surface_get_app_id(surf->dsurface);
204                 if (id && strcmp(app_id, id) == 0)
205                         return surf;
206         }
207
208         return NULL;
209 }
210
211 static void
212 ivi_layout_activate_complete(struct ivi_output *output,
213                              struct ivi_surface *surf)
214 {
215         struct ivi_compositor *ivi = output->ivi;
216         struct weston_output *woutput = output->output;
217         struct weston_view *view = surf->view;
218         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
219         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
220
221         if (weston_view_is_mapped(view)) {
222                 weston_layer_entry_remove(&view->layer_link);
223         } else {
224                 weston_view_update_transform(view);
225         }
226
227         weston_view_set_output(view, woutput);
228         weston_view_set_position(view,
229                                  woutput->x + output->area.x,
230                                  woutput->y + output->area.y);
231
232         view->is_mapped = true;
233         surf->mapped = true;
234         view->surface->is_mapped = true;
235
236         if (output->active) {
237                 output->active->view->is_mapped = false;
238                 output->active->view->surface->is_mapped = false;
239
240                 weston_layer_entry_remove(&output->active->view->layer_link);
241         }
242         output->previous_active = output->active;
243         output->active = surf;
244
245         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
246         weston_view_geometry_dirty(view);
247         weston_surface_damage(view->surface);
248
249         if (ivi_seat)
250                 ivi_shell_activate_surface(surf, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
251
252         /*
253          * the 'remote' role now makes use of this part so make sure we don't
254          * trip the enum such that we might end up with a modified output for
255          * 'remote' role
256          */
257         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
258                 if (surf->desktop.pending_output)
259                         surf->desktop.last_output = surf->desktop.pending_output;
260                 surf->desktop.pending_output = NULL;
261         }
262
263         weston_log("Activation completed for app_id %s, role %s, output %s\n",
264                         weston_desktop_surface_get_app_id(surf->dsurface),
265                         ivi_layout_get_surface_role_name(surf), output->name);
266 }
267
268 struct ivi_output *
269 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
270 {
271         struct ivi_output *out;
272
273         if (!app_id)
274                 return NULL;
275
276         wl_list_for_each(out, &ivi->outputs, link) {
277                 if (!out->app_id)
278                         continue;
279
280                 if (!strcmp(app_id, out->app_id))
281                         return out;
282         }
283
284         return NULL;
285 }
286
287 struct ivi_output *
288 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
289 {
290         struct ivi_output *out;
291
292         wl_list_for_each(out, &ivi->outputs, link) {
293                 if (out->background &&
294                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
295                         return out;
296         }
297
298         return NULL;
299 }
300
301
302 static void
303 ivi_layout_add_to_hidden_layer(struct ivi_surface *surf,
304                                struct ivi_output *ivi_output)
305 {
306         struct weston_desktop_surface *dsurf = surf->dsurface;
307         struct weston_view *ev = surf->view;
308         struct ivi_compositor *ivi = surf->ivi;
309         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
310
311         weston_desktop_surface_set_maximized(dsurf, true);
312         weston_desktop_surface_set_size(dsurf,
313                                         ivi_output->area.width,
314                                         ivi_output->area.height);
315
316         weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
317                         app_id, ivi_layout_get_surface_role_name(surf),
318                         ivi_output->area.width, ivi_output->area.height);
319         /*
320          * If the view isn't mapped, we put it onto the hidden layer so it will
321          * start receiving frame events, and will be able to act on our
322          * configure event.
323          */
324         if (!weston_view_is_mapped(ev)) {
325                 ev->is_mapped = true;
326                 ev->surface->is_mapped = true;
327
328                 weston_view_set_output(ev, ivi_output->output);
329                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
330                 weston_log("Placed app_id %s, type %s in hidden layer\n",
331                                 app_id, ivi_layout_get_surface_role_name(surf));
332         }
333 }
334
335 void
336 ivi_layout_desktop_committed(struct ivi_surface *surf)
337 {
338         struct weston_desktop_surface *dsurf = surf->dsurface;
339         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
340         struct ivi_policy *policy = surf->ivi->policy;
341         struct ivi_output *output;
342         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
343
344         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP ||
345                surf->role == IVI_SURFACE_ROLE_REMOTE);
346
347         /*
348          * we can't make use here of the ivi_layout_get_output_from_surface()
349          * due to the fact that we'll always land here when a surface performs
350          * a commit and pending_output will not bet set. This works in tandem
351          * with 'mapped' at this point to avoid tripping over
352          * to a surface that continuously updates its content
353          */
354         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
355                 output = surf->desktop.pending_output;
356         else
357                 output = surf->remote.output;
358
359         if (surf->role == IVI_SURFACE_ROLE_DESKTOP && !output) {
360                 struct ivi_output *r_output;
361
362                 if (policy && policy->api.surface_activate_by_default &&
363                     !policy->api.surface_activate_by_default(surf, surf->ivi))
364                         return;
365
366                 /* we can only activate it again by using the protocol */
367                 if (surf->mapped)
368                         return;
369
370                 /* check first if there aren't any outputs being set */
371                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
372
373                 if (r_output) {
374                         struct weston_view *view = r_output->fullscreen_view.fs->view;
375                         if (view->is_mapped || view->surface->is_mapped)
376                                 remove_black_surface(r_output);
377                 }
378
379
380                 /* try finding an output with a background and use that */
381                 if (!r_output)
382                         r_output = ivi_layout_find_bg_output(surf->ivi);
383
384                 /* if we couldn't still find an output by this point, there's
385                  * something wrong so we abort with a protocol error */
386                 if (!r_output) {
387                         wl_resource_post_error(surf->ivi->shell_client.resource,
388                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
389                                                "No valid output found to activate surface by default");
390                         return;
391                 }
392
393                 if (!surf->ivi->activate_by_default) {
394                         weston_log("Refusing to activate surface role %d, app_id %s\n",
395                                         surf->role, app_id);
396
397                         if (!weston_desktop_surface_get_maximized(dsurf) ||
398                             geom.width != r_output->area.width ||
399                             geom.height != r_output->area.height)
400                                 ivi_layout_add_to_hidden_layer(surf, r_output);
401
402                         return;
403                 }
404
405                 /* use the output of the bg to activate the app on start-up by
406                  * default */
407                 if (surf->view && r_output) {
408                         if (app_id && r_output) {
409                                 weston_log("Surface with app_id %s, role %s activating by default\n",
410                                         weston_desktop_surface_get_app_id(surf->dsurface),
411                                         ivi_layout_get_surface_role_name(surf));
412                                 ivi_layout_activate(r_output, app_id);
413                         } else if (!app_id) {
414                                 /*
415                                  * applications not setting an app_id, or
416                                  * setting an app_id but at a later point in
417                                  * time, might fall-back here so give them a
418                                  * chance to receive the configure event and
419                                  * act upon it
420                                  */
421                                 weston_log("Surface no app_id, role %s activating by default\n",
422                                         ivi_layout_get_surface_role_name(surf));
423                                 ivi_layout_activate_by_surf(r_output, surf);
424                         }
425                 }
426
427                 return;
428         }
429
430         if (surf->role == IVI_SURFACE_ROLE_REMOTE && output) {
431                 if (policy && policy->api.surface_activate_by_default &&
432                     !policy->api.surface_activate_by_default(surf, surf->ivi))
433                         return;
434
435                 /* we can only activate it again by using the protocol, but
436                  * additionally the output is not reset when
437                  * ivi_layout_activate_complete() terminates so we use the
438                  * current active surface to avoid hitting this again and again
439                  * */
440                 if (surf->mapped && output->active == surf)
441                         return;
442
443                 if (app_id) {
444                         weston_log("Surface with app_id %s, role %s activating by default\n",
445                                         weston_desktop_surface_get_app_id(surf->dsurface),
446                                         ivi_layout_get_surface_role_name(surf));
447                         ivi_layout_activate(output, app_id);
448                 }
449                 return;
450         }
451
452         if (!weston_desktop_surface_get_maximized(dsurf) ||
453             geom.width != output->area.width ||
454             geom.height != output->area.height)
455                 return;
456
457         ivi_layout_activate_complete(output, surf);
458 }
459
460 void
461 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
462 {
463         struct ivi_compositor *ivi = surface->ivi;
464         struct ivi_policy *policy = ivi->policy;
465
466         struct weston_desktop_surface *dsurface = surface->dsurface;
467         struct weston_surface *wsurface =
468                 weston_desktop_surface_get_surface(dsurface);
469         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
470
471         struct ivi_output *output = surface->split.output;
472         struct weston_output *woutput = output->output;
473         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
474
475         struct weston_view *view = surface->view;
476         struct weston_geometry geom =
477                 weston_desktop_surface_get_geometry(dsurface);
478
479         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
480         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
481
482         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
483         bool is_dim_same =
484                 geom.width == bg_output->output->width &&
485                 geom.height == bg_output->output->height;
486
487         if (policy && policy->api.surface_activate_by_default &&
488             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
489             !surface->mapped)
490                 return;
491
492         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
493
494         if (weston_view_is_mapped(view))
495                 return;
496
497         /* if we still get here but we haven't resized so far, send configure
498          * events to do so */
499         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
500                 struct ivi_output *bg_output =
501                         ivi_layout_find_bg_output(surface->ivi);
502
503                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
504                                 app_id, ivi_layout_get_surface_role_name(surface));
505                 weston_desktop_surface_set_fullscreen(dsurface, true);
506                 weston_desktop_surface_set_size(dsurface,
507                                                 bg_output->output->width,
508                                                 bg_output->output->height);
509
510                 surface->state = RESIZING;
511                 weston_view_set_output(view, output->output);
512                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
513                 return;
514         }
515
516         /* eventually, we would set the surface fullscreen, but the client
517          * hasn't resized correctly by this point, so terminate connection */
518         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
519                 struct weston_desktop_client *desktop_client =
520                         weston_desktop_surface_get_client(dsurface);
521                 struct wl_client *client =
522                         weston_desktop_client_get_client(desktop_client);
523                 wl_client_post_implementation_error(client,
524                                 "can not display surface due to invalid geometry."
525                                 " Client should perform a geometry resize!");
526                 return;
527         }
528
529         /* this implies we resized correctly */
530         if (!weston_view_is_mapped(view)) {
531                 weston_layer_entry_remove(&view->layer_link);
532
533                 weston_view_set_output(view, woutput);
534                 weston_view_set_position(view, woutput->x, woutput->y);
535                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
536
537                 wsurface->is_mapped = true;
538                 surface->view->is_mapped = true;
539                 surface->state = FULLSCREEN;
540
541                 weston_view_geometry_dirty(view);
542                 weston_surface_damage(view->surface);
543
544                 if (ivi_seat)
545                         ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
546
547                 shell_advertise_app_state(ivi, app_id,
548                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
549
550                 weston_log("Activation completed for app_id %s, role %s, "
551                            "output %s\n", app_id,
552                            ivi_layout_get_surface_role_name(surface),
553                            output->name);
554
555         }
556 }
557
558 void
559 ivi_layout_desktop_resize(struct ivi_surface *surface,
560                           struct weston_geometry area)
561 {
562         struct weston_desktop_surface *dsurf = surface->dsurface;
563         struct weston_view *view = surface->view;
564
565         int x = area.x;
566         int y = area.y;
567         int width = area.width;
568         int height = area.height;
569
570         weston_desktop_surface_set_size(dsurf,
571                                         width, height);
572
573         weston_view_set_position(view, x, y);
574
575         weston_view_geometry_dirty(view);
576         weston_surface_damage(view->surface);
577 }
578
579 void
580 ivi_layout_split_committed(struct ivi_surface *surface)
581 {
582         struct ivi_compositor *ivi = surface->ivi;
583         struct ivi_policy *policy = ivi->policy;
584
585         struct weston_desktop_surface *dsurface = surface->dsurface;
586         struct weston_surface *wsurface =
587                 weston_desktop_surface_get_surface(dsurface);
588         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
589
590         struct ivi_output *output = surface->split.output;
591         struct weston_output *woutput = output->output;
592
593         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
594         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
595
596         struct weston_view *view = surface->view;
597         struct weston_geometry geom;
598
599         int x, y;
600         int width, height;
601
602         x = woutput->x;
603         y = woutput->y;
604
605         if (policy && policy->api.surface_activate_by_default &&
606             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
607             !surface->mapped)
608                 return;
609
610         if (surface->view->is_mapped)
611                 return;
612
613         geom = weston_desktop_surface_get_geometry(dsurface);
614
615         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
616                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
617
618         /* save the previous area in order to recover it back when if this kind
619          * of surface is being destroyed/removed */
620         output->area_saved = output->area;
621
622         switch (surface->role) {
623         case IVI_SURFACE_ROLE_SPLIT_V:
624                 geom.width = (output->area.width / 2);
625
626                 x += woutput->width - geom.width;
627                 output->area.width -= geom.width;
628
629                 width = woutput->width - x;
630                 height = output->area.height;
631                 y = output->area.y;
632
633                 break;
634         case IVI_SURFACE_ROLE_SPLIT_H:
635                 geom.height = (output->area.height / 2);
636
637                 y = output->area.y;
638                 output->area.y += geom.height;
639                 output->area.height -= geom.height;
640
641                 width = output->area.width;
642                 height = output->area.height;
643
644                 x = output->area.x;
645
646                 break;
647         default:
648                 assert(!"Invalid split orientation\n");
649         }
650
651         weston_desktop_surface_set_size(dsurface,
652                                         width, height);
653
654         /* resize the active surface first, output->area already contains
655          * correct area to resize to */
656         if (output->active)
657                 ivi_layout_desktop_resize(output->active, output->area);
658
659         weston_view_set_output(view, woutput);
660         weston_view_set_position(view, x, y);
661         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
662
663         weston_view_geometry_dirty(view);
664         weston_surface_damage(view->surface);
665
666         if (ivi_seat)
667                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
668
669         wsurface->is_mapped = true;
670         surface->view->is_mapped = true;
671
672         shell_advertise_app_state(ivi, app_id,
673                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
674
675         weston_log("Activation completed for app_id %s, role %s, output %s\n",
676                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
677 }
678
679 static void
680 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
681                            int initial_x, int initial_y, int *new_x, int *new_y)
682 {
683         *new_x = output->x + initial_x;
684         *new_y = output->y + initial_y;
685 }
686
687
688 void
689 ivi_layout_popup_committed(struct ivi_surface *surface)
690 {
691         struct ivi_compositor *ivi = surface->ivi;
692         struct ivi_policy *policy = ivi->policy;
693
694         struct weston_desktop_surface *dsurface = surface->dsurface;
695         struct weston_surface *wsurface =
696                 weston_desktop_surface_get_surface(dsurface);
697         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
698
699         int new_x, new_y;
700
701         struct ivi_output *output = surface->popup.output;
702         struct weston_output *woutput = output->output;
703
704         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
705         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
706
707         struct weston_view *view = surface->view;
708
709         if (policy && policy->api.surface_activate_by_default &&
710             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
711             !surface->mapped)
712                 return;
713
714         if (surface->view->is_mapped || surface->state == HIDDEN)
715                 return;
716
717         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
718
719         weston_view_set_output(view, woutput);
720
721         ivi_compute_popup_position(woutput, view,
722                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
723         weston_view_set_position(view, new_x, new_y);
724         weston_view_update_transform(view);
725
726         /* only clip the pop-up dialog window if we have a valid
727          * width and height being passed on. Users might not want to have one
728          * set-up so only enfore it is really passed on. */
729         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
730                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
731                                      surface->popup.bb.width, surface->popup.bb.height);
732
733         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
734
735         weston_view_geometry_dirty(view);
736         weston_surface_damage(view->surface);
737
738         if (ivi_seat)
739                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
740
741         wsurface->is_mapped = true;
742         surface->view->is_mapped = true;
743
744         shell_advertise_app_state(ivi, app_id,
745                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
746
747         weston_log("Activation completed for app_id %s, role %s, output %s\n",
748                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
749 }
750
751 static void
752 ivi_layout_popup_re_add(struct ivi_surface *surface)
753 {
754         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
755         struct weston_view *view = surface->view;
756
757         if (weston_view_is_mapped(view)) {
758                 struct weston_desktop_surface *dsurface = surface->dsurface;
759                 struct weston_surface *wsurface =
760                         weston_desktop_surface_get_surface(dsurface);
761
762                 weston_layer_entry_remove(&view->layer_link);
763
764                 wsurface->is_mapped = false;
765                 view->is_mapped = false;
766         }
767
768         /* reset the activate by default in order to (still) allow the surface
769          * to be activaved using the request */
770         if (!surface->mapped)
771                 surface->mapped = true;
772
773         surface->state = NORMAL;
774         ivi_layout_popup_committed(surface);
775 }
776
777 static bool
778 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
779 {
780         struct ivi_compositor *ivi = surf->ivi;
781         struct ivi_surface *is;
782
783         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
784             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
785             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
786                 return false;
787
788         /* reset the activate by default in order to (still) allow the surface
789          * to be activaved using the request */
790         if (!surf->mapped)
791                 surf->mapped = true;
792
793         wl_list_for_each(is, &ivi->surfaces, link)
794                 if (is == surf)
795                         return true;
796
797         return false;
798 }
799
800 void
801 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
802 {
803         struct ivi_compositor *ivi = output->ivi;
804         struct weston_desktop_surface *dsurf;
805         struct weston_geometry geom;
806         struct ivi_policy *policy = output->ivi->policy;
807
808         dsurf = surf->dsurface;
809
810         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
811
812         if (!surf)
813                 return;
814
815         if (policy && policy->api.surface_activate &&
816             !policy->api.surface_activate(surf, surf->ivi)) {
817                 return;
818         }
819
820 #ifdef AGL_COMP_DEBUG
821         weston_log("Activating app_id %s, type %s\n", app_id,
822                         ivi_layout_get_surface_role_name(surf));
823 #endif
824
825         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
826                 ivi_layout_popup_re_add(surf);
827                 return;
828         }
829
830         /* do not 're'-activate surfaces that are split or active */
831         if (surf == output->active ||
832             ivi_layout_surface_is_split_or_fullscreen(surf))
833                 return;
834
835         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
836                 struct ivi_output *remote_output =
837                         ivi_layout_find_with_app_id(app_id, ivi);
838
839                 /* if already active on a remote output do not
840                  * attempt to activate it again */
841                 if (remote_output && remote_output->active == surf)
842                         return;
843         }
844
845
846         geom = weston_desktop_surface_get_geometry(dsurf);
847
848         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
849                 surf->desktop.pending_output = output;
850         if (weston_desktop_surface_get_maximized(dsurf) &&
851             geom.width == output->area.width &&
852             geom.height == output->area.height) {
853                 ivi_layout_activate_complete(output, surf);
854                 return;
855         }
856
857         ivi_layout_add_to_hidden_layer(surf, output);
858 }
859
860 void
861 ivi_layout_activate(struct ivi_output *output, const char *app_id)
862 {
863         struct ivi_surface *surf;
864         struct ivi_compositor *ivi = output->ivi;
865
866         if (!app_id)
867                 return;
868
869         surf = ivi_find_app(ivi, app_id);
870         if (!surf)
871                 return;
872
873         ivi_layout_activate_by_surf(output, surf);
874 }
875
876 struct ivi_output *
877 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
878 {
879         struct ivi_output *ivi_output = NULL;
880
881         switch (surf->role) {
882         case IVI_SURFACE_ROLE_DESKTOP:
883                 if (surf->desktop.pending_output)
884                         ivi_output = surf->desktop.pending_output;
885                 else
886                         ivi_output = surf->desktop.last_output;
887                 break;
888         case IVI_SURFACE_ROLE_POPUP:
889                 ivi_output = surf->popup.output;
890                 break;
891         case IVI_SURFACE_ROLE_BACKGROUND:
892                 ivi_output = surf->bg.output;
893                 break;
894         case IVI_SURFACE_ROLE_PANEL:
895                 ivi_output = surf->panel.output;
896                 break;
897         case IVI_SURFACE_ROLE_FULLSCREEN:
898                 ivi_output = surf->fullscreen.output;
899                 break;
900         case IVI_SURFACE_ROLE_SPLIT_H:
901         case IVI_SURFACE_ROLE_SPLIT_V:
902                 ivi_output = surf->split.output;
903                 break;
904         case IVI_SURFACE_ROLE_REMOTE:
905                 ivi_output = surf->remote.output;
906                 break;
907         case IVI_SURFACE_ROLE_NONE:
908         default:
909                 break;
910         }
911
912         return ivi_output;
913 }
914
915 void
916 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
917 {
918         struct ivi_surface *surf;
919         struct ivi_output *ivi_output;
920         struct ivi_policy *policy = ivi->policy;
921
922         if (!app_id)
923                 return;
924
925         surf = ivi_find_app(ivi, app_id);
926         if (!surf)
927                 return;
928
929         if (policy && policy->api.surface_deactivate &&
930             !policy->api.surface_deactivate(surf, surf->ivi)) {
931                 return;
932         }
933
934         ivi_output = ivi_layout_get_output_from_surface(surf);
935         weston_log("Deactiving %s, role %s\n", app_id,
936                         ivi_layout_get_surface_role_name(surf));
937
938         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
939                 struct ivi_surface *previous_active;
940
941                 previous_active = ivi_output->previous_active;
942                 if (!previous_active) {
943                         /* we don't have a previous active it means we should
944                          * display the bg */
945                         if (ivi_output->active) {
946                                 struct weston_view *view;
947
948                                 view = ivi_output->active->view;
949                                 view->is_mapped = false;
950                                 view->surface->is_mapped = false;
951
952                                 weston_layer_entry_remove(&view->layer_link);
953                                 weston_view_geometry_dirty(view);
954                                 weston_surface_damage(view->surface);
955                                 ivi_output->active = NULL;
956                         }
957                 } else {
958                         struct weston_desktop_surface *dsurface;
959                         const char *previous_active_app_id;
960
961                         dsurface = previous_active->dsurface;
962                         previous_active_app_id =
963                                 weston_desktop_surface_get_app_id(dsurface);
964                         ivi_layout_activate(ivi_output, previous_active_app_id);
965                 }
966         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
967                 struct weston_view *view  = surf->view;
968
969                 weston_view_unmap(view);
970                 surf->state = HIDDEN;
971
972                 weston_layer_entry_remove(&view->layer_link);
973                 weston_view_geometry_dirty(view);
974                 weston_surface_damage(view->surface);
975         }
976 }