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