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