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