layout: Add a fallback for reading activation-area
[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/config-parser.h>
34 #include <libweston/libweston.h>
35 #include <libweston-desktop/libweston-desktop.h>
36
37 #include "agl-shell-desktop-server-protocol.h"
38
39 #define AGL_COMP_DEBUG
40
41 static const char *ivi_roles_as_string[] = {
42         [IVI_SURFACE_ROLE_NONE]         = "NONE",
43         [IVI_SURFACE_ROLE_BACKGROUND]   = "BACKGROUND",
44         [IVI_SURFACE_ROLE_PANEL]        = "PANEL",
45         [IVI_SURFACE_ROLE_DESKTOP]      = "DESKTOP",
46         [IVI_SURFACE_ROLE_POPUP]        = "POPUP",
47         [IVI_SURFACE_ROLE_SPLIT_H]      = "SPLIT_H",
48         [IVI_SURFACE_ROLE_SPLIT_V]      = "SPLIT_V",
49         [IVI_SURFACE_ROLE_FULLSCREEN]   = "FULLSCREEN",
50         [IVI_SURFACE_ROLE_REMOTE]       = "REMOTE",
51 };
52
53 bool
54 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface);
55
56 const char *
57 ivi_layout_get_surface_role_name(struct ivi_surface *surf)
58 {
59         if (surf->role < 0 || surf->role >= ARRAY_LENGTH(ivi_roles_as_string))
60                 return " unknown surface role";
61
62         return ivi_roles_as_string[surf->role];
63 }
64
65 static void
66 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
67 {
68         struct weston_output *woutput = output->output;
69         struct ivi_surface *bg = output->background;
70         struct weston_view *view;
71
72         if (!bg) {
73                 weston_log("WARNING: Output does not have a background\n");
74                 return;
75         }
76
77         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
78
79         view = bg->view;
80
81         weston_view_set_output(view, woutput);
82         weston_view_set_position(view, woutput->x, woutput->y);
83
84         weston_log("(background) position view %p, x %d, y %d, on output %s\n", view,
85                         woutput->x, woutput->y, output->name);
86
87         view->is_mapped = true;
88         view->surface->is_mapped = true;
89
90         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
91 }
92
93 static void
94 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
95                struct ivi_surface *panel)
96 {
97         struct weston_output *woutput = output->output;
98         struct weston_desktop_surface *dsurface;
99         struct weston_view *view;
100         struct weston_geometry geom;
101         int x = woutput->x;
102         int y = woutput->y;
103
104         if (!panel)
105                 return;
106
107         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
108         dsurface = panel->dsurface;
109         view = panel->view;
110         geom = weston_desktop_surface_get_geometry(dsurface);
111
112         weston_log("(panel) geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
113                         geom.width, geom.height, geom.x, geom.y);
114
115         switch (panel->panel.edge) {
116         case AGL_SHELL_EDGE_TOP:
117                 output->area.y += geom.height;
118                 output->area.height -= geom.height;
119                 break;
120         case AGL_SHELL_EDGE_BOTTOM:
121                 y += woutput->height - geom.height;
122                 output->area.height -= geom.height;
123                 break;
124         case AGL_SHELL_EDGE_LEFT:
125                 output->area.x += geom.width;
126                 output->area.width -= geom.width;
127                 break;
128         case AGL_SHELL_EDGE_RIGHT:
129                 x += woutput->width - geom.width;
130                 output->area.width -= geom.width;
131                 break;
132         }
133
134         x -= geom.x;
135         y -= geom.y;
136
137         weston_view_set_output(view, woutput);
138         weston_view_set_position(view, x, y);
139
140         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
141                         panel->panel.edge, view, x, y);
142
143         view->is_mapped = true;
144         view->surface->is_mapped = true;
145
146         weston_log("panel type %d inited on output %s\n", panel->panel.edge,
147                         output->name);
148
149         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
150 }
151
152 /*
153  * Initializes all static parts of the layout, i.e. the background and panels.
154  */
155 void
156 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
157 {
158         bool use_default_area = true;
159         struct weston_config_section *section = output->config;
160         char *t;
161
162         weston_config_section_get_string(section, "activation-area", &t, NULL);
163         if (t) {
164                 if (output->area_activation.width == 0 &&
165                     output->area_activation.height == 0 &&
166                     output->area_activation.x == 0 &&
167                     output->area_activation.y == 0) {
168                         weston_log("WARNING: activation-area set in "
169                                         "configuration file, but yet applied!\n");
170                         if (parse_activation_area(t, output) < 0)
171                                 weston_log("Invalid activation-area \"%s\" for output %s\n",
172                                            t, output->name);
173                 }
174         }
175         free(t);
176
177         ivi_background_init(ivi, output);
178
179         if (output->area_activation.width ||
180             output->area_activation.height ||
181             output->area_activation.x ||
182             output->area_activation.y) {
183                 /* Sanity check target area is within output bounds */
184                 if ((output->area_activation.x + output->area_activation.width) < output->output->width ||
185                     (output->area_activation.y + output->area_activation.height) < output->output->height) {
186                         weston_log("Using specified area for output %s, ignoring panels\n",
187                                    output->name);
188                         output->area.x = output->area_activation.x;
189                         output->area.y = output->area_activation.y;
190                         output->area.width = output->area_activation.width;
191                         output->area.height = output->area_activation.height;
192                         use_default_area = false;
193                 } else {
194                         weston_log("Invalid activation-area position for output %s, ignoring\n",
195                                    output->name);
196                 }
197         }
198         if (use_default_area) {
199                 output->area.x = 0;
200                 output->area.y = 0;
201                 output->area.width = output->output->width;
202                 output->area.height = output->output->height;
203
204                 ivi_panel_init(ivi, output, output->top);
205                 ivi_panel_init(ivi, output, output->bottom);
206                 ivi_panel_init(ivi, output, output->left);
207                 ivi_panel_init(ivi, output, output->right);
208         }
209
210         weston_compositor_schedule_repaint(ivi->compositor);
211
212         weston_log("Usable area: %dx%d+%d,%d\n",
213                    output->area.width, output->area.height,
214                    output->area.x, output->area.y);
215 }
216
217 struct ivi_surface *
218 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
219 {
220         struct ivi_surface *surf;
221         const char *id;
222
223         wl_list_for_each(surf, &ivi->surfaces, link) {
224                 id = weston_desktop_surface_get_app_id(surf->dsurface);
225                 if (id && strcmp(app_id, id) == 0)
226                         return surf;
227         }
228
229         return NULL;
230 }
231
232 static void
233 ivi_layout_activate_complete(struct ivi_output *output,
234                              struct ivi_surface *surf)
235 {
236         struct ivi_compositor *ivi = output->ivi;
237         struct weston_output *woutput = output->output;
238         struct weston_view *view = surf->view;
239         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
240         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
241         const char *app_id = weston_desktop_surface_get_app_id(surf->dsurface);
242         bool update_previous = true;
243
244         if (weston_view_is_mapped(view)) {
245                 weston_layer_entry_remove(&view->layer_link);
246         } else {
247                 weston_view_update_transform(view);
248         }
249
250         if (output_has_black_curtain(output)) {
251                 if (!output->background) {
252                         weston_log("Found that we have no background surface "
253                                     "for output %s. Using black curtain as background\n",
254                                     output->output->name);
255
256                         struct weston_view *ev =
257                                 output->fullscreen_view.fs->view;
258
259                         /* use the black curtain as background when we have
260                          * none added by the shell client. */
261                         weston_layer_entry_remove(&ev->layer_link);
262                         weston_layer_entry_insert(&ivi->normal.view_list,
263                                                   &ev->layer_link);
264                         weston_view_geometry_dirty(ev);
265                         weston_surface_damage(ev->surface);
266                 } else {
267                         remove_black_curtain(output);
268                 }
269         }
270
271
272         weston_view_set_output(view, woutput);
273         /* drop any previous masks set on this view */
274         weston_view_set_mask_infinite(view);
275
276         if (surf->role != IVI_SURFACE_ROLE_BACKGROUND)
277                 weston_view_set_position(view,
278                                          woutput->x + output->area.x,
279                                          woutput->y + output->area.y);
280
281         view->is_mapped = true;
282         surf->mapped = true;
283         view->surface->is_mapped = true;
284
285         /* handle a movement from one output to another */
286         if (surf->current_completed_output &&
287             surf->current_completed_output != output) {
288
289                 /* we're migrating the same surface but to another output */
290                 if (surf->current_completed_output->active == surf) {
291                         struct weston_view *ev =
292                                 surf->current_completed_output->active->view;
293
294                         weston_layer_entry_remove(&ev->layer_link);
295                         surf->current_completed_output->previous_active =
296                                 surf->current_completed_output->active;
297                         surf->current_completed_output->active = NULL;
298
299                         /* damage all possible outputs to avoid stale views */
300                         weston_compositor_damage_all(ivi->compositor);
301                 }
302         }
303
304         if (output->active) {
305                 /* keep the background surface mapped at all times */
306                 if (output->active->role != IVI_SURFACE_ROLE_BACKGROUND) {
307                         output->active->view->is_mapped = false;
308                         output->active->view->surface->is_mapped = false;
309
310                         weston_layer_entry_remove(&output->active->view->layer_link);
311                 }
312         }
313
314         if (output->previous_active && output->active) {
315                 const char *c_app_id =
316                         weston_desktop_surface_get_app_id(output->active->dsurface);
317
318                 /* if the currently activated app_id is the same as the one
319                  * we're trying to complete activation with means we're
320                  * operating on the same app_id so do update previous_active as
321                  * it will overwrite it with the same value */
322                 if (!strcmp(c_app_id, app_id)) {
323                         update_previous = false;
324                 }
325         }
326
327         if (update_previous)
328                 output->previous_active = output->active;
329         output->active = surf;
330         surf->current_completed_output = output;
331
332         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
333         weston_view_geometry_dirty(view);
334         weston_surface_damage(view->surface);
335
336         if (ivi_seat)
337                 ivi_shell_activate_surface(surf, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
338
339         /*
340          * the 'remote' role now makes use of this part so make sure we don't
341          * trip the enum such that we might end up with a modified output for
342          * 'remote' role
343          */
344         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
345                 if (surf->desktop.pending_output)
346                         surf->desktop.last_output = surf->desktop.pending_output;
347                 surf->desktop.pending_output = NULL;
348         }
349
350         weston_log("Activation completed for app_id %s, role %s, output %s\n",
351                         app_id,
352                         ivi_layout_get_surface_role_name(surf), output->name);
353
354         shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_ACTIVATED);
355 }
356
357 static bool
358 ivi_layout_find_output_with_app_id(const char *app_id, struct ivi_output *output)
359 {
360         char *cur;
361         size_t app_id_len;
362
363         cur = output->app_ids;
364         app_id_len = strlen(app_id);
365
366         while ((cur = strstr(cur, app_id))) {
367                 if ((cur[app_id_len] == ',' || cur[app_id_len] == '\0') &&
368                     (cur == output->app_ids || cur[-1] == ','))
369                         return true;
370                 cur++;
371         }
372
373         return false;
374 }
375
376 struct ivi_output *
377 ivi_layout_find_with_app_id(const char *app_id, struct ivi_compositor *ivi)
378 {
379         struct ivi_output *out;
380
381         if (!app_id)
382                 return NULL;
383
384         wl_list_for_each(out, &ivi->outputs, link) {
385                 if (!out->app_ids)
386                         continue;
387
388                 if (ivi_layout_find_output_with_app_id(app_id, out))
389                         return out;
390         }
391         return NULL;
392 }
393
394 struct ivi_output *
395 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
396 {
397         struct ivi_output *out;
398
399         wl_list_for_each(out, &ivi->outputs, link) {
400                 if (out->background &&
401                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
402                         return out;
403         }
404
405         return NULL;
406 }
407
408
409 static void
410 ivi_layout_add_to_hidden_layer(struct ivi_surface *surf,
411                                struct ivi_output *ivi_output)
412 {
413         struct weston_desktop_surface *dsurf = surf->dsurface;
414         struct weston_view *ev = surf->view;
415         struct ivi_compositor *ivi = surf->ivi;
416         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
417
418         /*
419          * If the view isn't mapped, we put it onto the hidden layer so it will
420          * start receiving frame events, and will be able to act on our
421          * configure event.
422          */
423         if (!weston_view_is_mapped(ev)) {
424                 ev->is_mapped = true;
425                 ev->surface->is_mapped = true;
426
427                 weston_desktop_surface_set_maximized(dsurf, true);
428                 weston_desktop_surface_set_size(dsurf,
429                                                 ivi_output->area.width,
430                                                 ivi_output->area.height);
431
432                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
433                            app_id, ivi_layout_get_surface_role_name(surf),
434                            ivi_output->area.width, ivi_output->area.height);
435
436                 surf->hidden_layer_output = ivi_output;
437                 weston_view_set_output(ev, ivi_output->output);
438                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
439                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
440                                 app_id, ivi_layout_get_surface_role_name(surf),
441                                 ivi_output->output->name);
442
443                 weston_compositor_schedule_repaint(ivi->compositor);
444                 return;
445         }
446
447         /* we might have another output to activate */
448         if (surf->hidden_layer_output &&
449             surf->hidden_layer_output != ivi_output) {
450                 weston_layer_entry_remove(&ev->layer_link);
451                 weston_view_geometry_dirty(ev);
452                 weston_surface_damage(ev->surface);
453
454                 if (ivi_output->area.width != surf->hidden_layer_output->area.width ||
455                     ivi_output->area.height != surf->hidden_layer_output->area.height) {
456                         weston_desktop_surface_set_maximized(dsurf, true);
457                         weston_desktop_surface_set_size(dsurf,
458                                                         ivi_output->area.width,
459                                                         ivi_output->area.height);
460                 }
461
462                 weston_log("Setting app_id %s, role %s, set to maximized (%dx%d)\n",
463                                 app_id, ivi_layout_get_surface_role_name(surf),
464                                 ivi_output->area.width, ivi_output->area.height);
465
466                 surf->hidden_layer_output = ivi_output;
467                 weston_view_set_output(ev, ivi_output->output);
468                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
469                 weston_log("Placed app_id %s, type %s in hidden layer on output %s\n",
470                                 app_id, ivi_layout_get_surface_role_name(surf),
471                                 ivi_output->output->name);
472         }
473
474         weston_compositor_schedule_repaint(ivi->compositor);
475 }
476
477 void
478 ivi_layout_remote_committed(struct ivi_surface *surf)
479 {
480         struct weston_desktop_surface *dsurf = surf->dsurface;
481         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
482         struct ivi_policy *policy = surf->ivi->policy;
483         struct ivi_output *output;
484         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
485
486         assert(surf->role == IVI_SURFACE_ROLE_REMOTE);
487
488         output = surf->remote.output;
489
490         if (policy && policy->api.surface_activate_by_default &&
491             !policy->api.surface_activate_by_default(surf, surf->ivi))
492                 return;
493
494         /* we can only activate it again by using the protocol, but
495          * additionally the output is not reset when
496          * ivi_layout_activate_complete() terminates so we use the
497          * current active surface to avoid hitting this again and again
498          * */
499         if (surf->mapped && output->active == surf)
500                 return;
501
502         if (!surf->ivi->activate_by_default &&
503             !ivi_surf_in_hidden_layer(surf->ivi, surf)) {
504                 weston_log("Refusing to activate surface role %d, "
505                             "app_id %s\n", surf->role, app_id);
506
507                 if (!weston_desktop_surface_get_maximized(dsurf) ||
508                     geom.width != output->area.width ||
509                     geom.height != output->area.height) {
510                         ivi_layout_add_to_hidden_layer(surf, output);
511                 }
512
513                 return;
514         }
515
516         if (!weston_desktop_surface_get_maximized(dsurf) ||
517             geom.width != output->area.width ||
518             geom.height != output->area.height)
519                 return;
520
521         ivi_layout_activate_complete(output, surf);
522 }
523
524 void
525 ivi_layout_desktop_committed(struct ivi_surface *surf)
526 {
527         struct weston_desktop_surface *dsurf = surf->dsurface;
528         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
529         struct ivi_policy *policy = surf->ivi->policy;
530         struct ivi_output *output;
531         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
532
533         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
534
535         /*
536          * we can't make use here of the ivi_layout_get_output_from_surface()
537          * due to the fact that we'll always land here when a surface performs
538          * a commit and pending_output will not bet set. This works in tandem
539          * with 'mapped' at this point to avoid tripping over
540          * to a surface that continuously updates its content
541          */
542         output = surf->desktop.pending_output;
543
544         if (!output) {
545                 struct ivi_output *r_output;
546
547                 if (policy && policy->api.surface_activate_by_default &&
548                     !policy->api.surface_activate_by_default(surf, surf->ivi))
549                         return;
550
551                 /* we can only activate it again by using the protocol */
552                 if (surf->mapped)
553                         return;
554
555                 /* check first if there aren't any outputs being set */
556                 r_output = ivi_layout_find_with_app_id(app_id, surf->ivi);
557
558                 if (r_output) {
559                         struct weston_view *view = r_output->fullscreen_view.fs->view;
560                         if (view->is_mapped || view->surface->is_mapped)
561                                 remove_black_curtain(r_output);
562                 }
563
564
565                 /* try finding an output with a background and use that */
566                 if (!r_output)
567                         r_output = ivi_layout_find_bg_output(surf->ivi);
568
569                 /* if we couldn't still find an output by this point, there's
570                  * something wrong so we abort with a protocol error */
571                 if (!r_output) {
572                         wl_resource_post_error(surf->ivi->shell_client.resource,
573                                                AGL_SHELL_ERROR_INVALID_ARGUMENT,
574                                                "No valid output found to activate surface by default");
575                         return;
576                 }
577
578                 if (!surf->ivi->activate_by_default) {
579                         weston_log("Refusing to activate surface role %d, app_id %s\n",
580                                         surf->role, app_id);
581
582                         if (!weston_desktop_surface_get_maximized(dsurf) ||
583                             geom.width != r_output->area.width ||
584                             geom.height != r_output->area.height)
585                                 ivi_layout_add_to_hidden_layer(surf, r_output);
586
587                         return;
588                 }
589
590                 /* use the output of the bg to activate the app on start-up by
591                  * default */
592                 if (surf->view && r_output) {
593                         if (app_id && r_output) {
594                                 weston_log("Surface with app_id %s, role %s activating by default\n",
595                                         weston_desktop_surface_get_app_id(surf->dsurface),
596                                         ivi_layout_get_surface_role_name(surf));
597                                 ivi_layout_activate(r_output, app_id);
598                         } else if (!app_id) {
599                                 /*
600                                  * applications not setting an app_id, or
601                                  * setting an app_id but at a later point in
602                                  * time, might fall-back here so give them a
603                                  * chance to receive the configure event and
604                                  * act upon it
605                                  */
606                                 weston_log("Surface no app_id, role %s activating by default\n",
607                                         ivi_layout_get_surface_role_name(surf));
608                                 ivi_layout_activate_by_surf(r_output, surf);
609                         }
610                 }
611
612                 return;
613         }
614
615         if (!weston_desktop_surface_get_maximized(dsurf) ||
616             geom.width != output->area.width ||
617             geom.height != output->area.height)
618                 return;
619
620         ivi_layout_activate_complete(output, surf);
621 }
622
623 void
624 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
625 {
626         struct ivi_compositor *ivi = surface->ivi;
627         struct ivi_policy *policy = ivi->policy;
628
629         struct weston_desktop_surface *dsurface = surface->dsurface;
630         struct weston_surface *wsurface =
631                 weston_desktop_surface_get_surface(dsurface);
632         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
633
634         struct ivi_output *output = surface->split.output;
635         struct weston_output *woutput = output->output;
636         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
637
638         struct weston_view *view = surface->view;
639         struct weston_geometry geom =
640                 weston_desktop_surface_get_geometry(dsurface);
641
642         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
643         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
644
645         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
646         bool is_dim_same =
647                 geom.width == bg_output->output->width &&
648                 geom.height == bg_output->output->height;
649
650         if (policy && policy->api.surface_activate_by_default &&
651             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
652             !surface->mapped)
653                 return;
654
655         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
656
657
658         if (surface->state == FULLSCREEN && weston_view_is_mapped(view))
659                 return;
660
661         /* if we still get here but we haven't resized so far, send configure
662          * events to do so */
663         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
664                 struct ivi_output *bg_output =
665                         ivi_layout_find_bg_output(surface->ivi);
666
667                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
668                                 app_id, ivi_layout_get_surface_role_name(surface));
669                 weston_desktop_surface_set_fullscreen(dsurface, true);
670                 weston_desktop_surface_set_size(dsurface,
671                                                 bg_output->output->width,
672                                                 bg_output->output->height);
673
674                 surface->state = RESIZING;
675                 weston_view_set_output(view, output->output);
676                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
677                 return;
678         }
679
680         /* eventually, we would set the surface fullscreen, but the client
681          * hasn't resized correctly by this point, so terminate connection */
682         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
683                 struct weston_desktop_client *desktop_client =
684                         weston_desktop_surface_get_client(dsurface);
685                 struct wl_client *client =
686                         weston_desktop_client_get_client(desktop_client);
687                 wl_client_post_implementation_error(client,
688                                 "can not display surface due to invalid geometry."
689                                 " Client should perform a geometry resize!");
690                 return;
691         }
692
693         /* this implies we resized correctly */
694         if (!weston_view_is_mapped(view) || surface->state != FULLSCREEN) {
695                 weston_layer_entry_remove(&view->layer_link);
696
697                 weston_view_set_output(view, woutput);
698                 weston_view_set_position(view, woutput->x, woutput->y);
699                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
700
701                 wsurface->is_mapped = true;
702                 surface->view->is_mapped = true;
703                 surface->state = FULLSCREEN;
704
705                 weston_view_geometry_dirty(view);
706                 weston_surface_damage(view->surface);
707
708                 if (ivi_seat)
709                         ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
710
711                 shell_advertise_app_state(ivi, app_id,
712                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
713
714                 weston_log("Activation completed for app_id %s, role %s, "
715                            "output %s\n", app_id,
716                            ivi_layout_get_surface_role_name(surface),
717                            output->name);
718
719         }
720 }
721
722 void
723 ivi_layout_desktop_resize(struct ivi_surface *surface,
724                           struct weston_geometry area)
725 {
726         struct weston_desktop_surface *dsurf = surface->dsurface;
727         struct weston_view *view = surface->view;
728
729         int x = area.x;
730         int y = area.y;
731         int width = area.width;
732         int height = area.height;
733
734         weston_desktop_surface_set_size(dsurf,
735                                         width, height);
736
737         weston_view_set_position(view, x, y);
738
739         weston_view_geometry_dirty(view);
740         weston_surface_damage(view->surface);
741 }
742
743 void
744 ivi_layout_split_committed(struct ivi_surface *surface)
745 {
746         struct ivi_compositor *ivi = surface->ivi;
747         struct ivi_policy *policy = ivi->policy;
748
749         struct weston_desktop_surface *dsurface = surface->dsurface;
750         struct weston_surface *wsurface =
751                 weston_desktop_surface_get_surface(dsurface);
752         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
753
754         struct ivi_output *output = surface->split.output;
755         struct weston_output *woutput = output->output;
756
757         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
758         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
759
760         struct weston_view *view = surface->view;
761         struct weston_geometry geom;
762
763         int x, y;
764         int width, height;
765
766         x = woutput->x;
767         y = woutput->y;
768
769         if (policy && policy->api.surface_activate_by_default &&
770             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
771             !surface->mapped)
772                 return;
773
774         if (surface->view->is_mapped)
775                 return;
776
777         geom = weston_desktop_surface_get_geometry(dsurface);
778
779         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
780                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
781
782         /* save the previous area in order to recover it back when if this kind
783          * of surface is being destroyed/removed */
784         output->area_saved = output->area;
785
786         switch (surface->role) {
787         case IVI_SURFACE_ROLE_SPLIT_V:
788                 geom.width = (output->area.width / 2);
789
790                 x += woutput->width - geom.width;
791                 output->area.width -= geom.width;
792
793                 width = woutput->width - x;
794                 height = output->area.height;
795                 y = output->area.y;
796
797                 break;
798         case IVI_SURFACE_ROLE_SPLIT_H:
799                 geom.height = (output->area.height / 2);
800
801                 y = output->area.y;
802                 output->area.y += geom.height;
803                 output->area.height -= geom.height;
804
805                 width = output->area.width;
806                 height = output->area.height;
807
808                 x = output->area.x;
809
810                 break;
811         default:
812                 assert(!"Invalid split orientation\n");
813         }
814
815         weston_desktop_surface_set_size(dsurface,
816                                         width, height);
817
818         /* resize the active surface first, output->area already contains
819          * correct area to resize to */
820         if (output->active)
821                 ivi_layout_desktop_resize(output->active, output->area);
822
823         weston_view_set_output(view, woutput);
824         weston_view_set_position(view, x, y);
825         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
826
827         weston_view_geometry_dirty(view);
828         weston_surface_damage(view->surface);
829
830         if (ivi_seat)
831                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
832
833         wsurface->is_mapped = true;
834         surface->view->is_mapped = true;
835
836         shell_advertise_app_state(ivi, app_id,
837                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
838
839         weston_log("Activation completed for app_id %s, role %s, output %s\n",
840                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
841 }
842
843 static void
844 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
845                            int initial_x, int initial_y, int *new_x, int *new_y)
846 {
847         *new_x = output->x + initial_x;
848         *new_y = output->y + initial_y;
849 }
850
851
852 bool
853 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface)
854 {
855         struct weston_view *ev;
856
857         wl_list_for_each(ev, &ivi->hidden.view_list.link, layer_link.link) {
858                 if (ev == surface->view)
859                         return true;
860         }
861
862         wl_list_for_each(ev, &ivi->fullscreen.view_list.link, layer_link.link) {
863                 if (ev == surface->view)
864                         return true;
865         }
866
867         return false;
868 }
869
870 void
871 ivi_layout_popup_committed(struct ivi_surface *surface)
872 {
873         struct ivi_compositor *ivi = surface->ivi;
874         struct ivi_policy *policy = ivi->policy;
875
876         struct weston_desktop_surface *dsurface = surface->dsurface;
877         struct weston_surface *wsurface =
878                 weston_desktop_surface_get_surface(dsurface);
879         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
880
881         int new_x, new_y;
882
883         struct ivi_output *output = surface->popup.output;
884         struct weston_output *woutput = output->output;
885
886         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
887         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
888
889         struct weston_view *view = surface->view;
890
891         if (policy && policy->api.surface_activate_by_default &&
892             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
893             !surface->mapped)
894                 return;
895
896         if (surface->view->is_mapped || surface->state == HIDDEN)
897                 return;
898
899         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
900
901         /* remove it from hidden layer if present */
902         if (ivi_surf_in_hidden_layer(ivi, surface))
903                 weston_layer_entry_remove(&view->layer_link);
904
905         weston_view_set_output(view, woutput);
906
907         ivi_compute_popup_position(woutput, view,
908                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
909         weston_view_set_position(view, new_x, new_y);
910         weston_view_update_transform(view);
911
912         /* only clip the pop-up dialog window if we have a valid
913          * width and height being passed on. Users might not want to have one
914          * set-up so only enfore it is really passed on. */
915         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
916                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
917                                      surface->popup.bb.width, surface->popup.bb.height);
918
919         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
920
921         weston_view_geometry_dirty(view);
922         weston_surface_damage(view->surface);
923
924         if (ivi_seat)
925                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
926
927         wsurface->is_mapped = true;
928         surface->view->is_mapped = true;
929
930         shell_advertise_app_state(ivi, app_id,
931                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
932
933         weston_log("Activation completed for app_id %s, role %s, output %s\n",
934                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
935 }
936
937 static void
938 ivi_layout_popup_re_add(struct ivi_surface *surface)
939 {
940         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
941         struct weston_view *view = surface->view;
942
943         if (weston_view_is_mapped(view)) {
944                 struct weston_desktop_surface *dsurface = surface->dsurface;
945                 struct weston_surface *wsurface =
946                         weston_desktop_surface_get_surface(dsurface);
947
948                 weston_layer_entry_remove(&view->layer_link);
949
950                 wsurface->is_mapped = false;
951                 view->is_mapped = false;
952         }
953
954         /* reset the activate by default in order to (still) allow the surface
955          * to be activaved using the request */
956         if (!surface->mapped)
957                 surface->mapped = true;
958
959         surface->state = NORMAL;
960         ivi_layout_popup_committed(surface);
961 }
962
963 static bool
964 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
965 {
966         struct ivi_compositor *ivi = surf->ivi;
967         struct ivi_surface *is;
968
969         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
970             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
971             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
972                 return false;
973
974         /* reset the activate by default in order to (still) allow the surface
975          * to be activaved using the request */
976         if (!surf->mapped)
977                 surf->mapped = true;
978
979         wl_list_for_each(is, &ivi->surfaces, link)
980                 if (is == surf)
981                         return true;
982
983         return false;
984 }
985
986 void
987 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
988 {
989         struct ivi_compositor *ivi = output->ivi;
990         struct weston_desktop_surface *dsurf;
991         struct weston_geometry geom;
992         struct ivi_policy *policy = output->ivi->policy;
993
994         dsurf = surf->dsurface;
995
996         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
997
998         if (!surf)
999                 return;
1000
1001         if (policy && policy->api.surface_activate &&
1002             !policy->api.surface_activate(surf, surf->ivi)) {
1003                 return;
1004         }
1005
1006 #ifdef AGL_COMP_DEBUG
1007         weston_log("Activating app_id %s, type %s, on output %s\n", app_id,
1008                         ivi_layout_get_surface_role_name(surf), output->output->name);
1009 #endif
1010
1011         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1012                 ivi_layout_popup_re_add(surf);
1013                 return;
1014         }
1015
1016         /* do not 're'-activate surfaces that are split or active */
1017         if (surf == output->active ||
1018             ivi_layout_surface_is_split_or_fullscreen(surf)) {
1019                 weston_log("Application %s is already active on output %s\n",
1020                                 app_id, output->output->name);
1021                 return;
1022         }
1023
1024         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
1025                 struct ivi_output *remote_output =
1026                         ivi_layout_find_with_app_id(app_id, ivi);
1027
1028                 weston_log("Changed activation for app_id %s, type %s, on output %s\n", app_id,
1029                                 ivi_layout_get_surface_role_name(surf), output->output->name);
1030
1031                 /* if already active on a remote output do not
1032                  * attempt to activate it again */
1033                 if (remote_output && remote_output->active == surf)
1034                         return;
1035         }
1036
1037
1038         geom = weston_desktop_surface_get_geometry(dsurf);
1039
1040         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
1041                 surf->desktop.pending_output = output;
1042         if (weston_desktop_surface_get_maximized(dsurf) &&
1043             geom.width == output->area.width &&
1044             geom.height == output->area.height) {
1045                 ivi_layout_activate_complete(output, surf);
1046                 return;
1047         }
1048
1049         /* the background surface is already "maximized" so we don't need to
1050          * add to the hidden layer */
1051         if (surf->role == IVI_SURFACE_ROLE_BACKGROUND) {
1052                 ivi_layout_activate_complete(output, surf);
1053                 return;
1054         }
1055
1056         ivi_layout_add_to_hidden_layer(surf, output);
1057 }
1058
1059 void
1060 ivi_layout_activate(struct ivi_output *output, const char *app_id)
1061 {
1062         struct ivi_surface *surf;
1063         struct ivi_compositor *ivi = output->ivi;
1064
1065         if (!app_id)
1066                 return;
1067
1068         surf = ivi_find_app(ivi, app_id);
1069         if (!surf)
1070                 return;
1071
1072         ivi_layout_activate_by_surf(output, surf);
1073 }
1074
1075 struct ivi_output *
1076 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
1077 {
1078         struct ivi_output *ivi_output = NULL;
1079
1080         switch (surf->role) {
1081         case IVI_SURFACE_ROLE_DESKTOP:
1082                 if (surf->desktop.pending_output)
1083                         ivi_output = surf->desktop.pending_output;
1084                 else
1085                         ivi_output = surf->desktop.last_output;
1086                 break;
1087         case IVI_SURFACE_ROLE_POPUP:
1088                 ivi_output = surf->popup.output;
1089                 break;
1090         case IVI_SURFACE_ROLE_BACKGROUND:
1091                 ivi_output = surf->bg.output;
1092                 break;
1093         case IVI_SURFACE_ROLE_PANEL:
1094                 ivi_output = surf->panel.output;
1095                 break;
1096         case IVI_SURFACE_ROLE_FULLSCREEN:
1097                 ivi_output = surf->fullscreen.output;
1098                 break;
1099         case IVI_SURFACE_ROLE_SPLIT_H:
1100         case IVI_SURFACE_ROLE_SPLIT_V:
1101                 ivi_output = surf->split.output;
1102                 break;
1103         case IVI_SURFACE_ROLE_REMOTE:
1104                 ivi_output = surf->remote.output;
1105                 break;
1106         case IVI_SURFACE_ROLE_NONE:
1107         default:
1108                 break;
1109         }
1110
1111         return ivi_output;
1112 }
1113
1114 void
1115 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
1116 {
1117         struct ivi_surface *surf;
1118         struct ivi_output *ivi_output;
1119         struct ivi_policy *policy = ivi->policy;
1120
1121         if (!app_id)
1122                 return;
1123
1124         surf = ivi_find_app(ivi, app_id);
1125         if (!surf)
1126                 return;
1127
1128         if (policy && policy->api.surface_deactivate &&
1129             !policy->api.surface_deactivate(surf, surf->ivi)) {
1130                 return;
1131         }
1132
1133         ivi_output = ivi_layout_get_output_from_surface(surf);
1134         weston_log("Deactiving %s, role %s\n", app_id,
1135                         ivi_layout_get_surface_role_name(surf));
1136
1137         if (surf->role == IVI_SURFACE_ROLE_DESKTOP ||
1138             surf->role == IVI_SURFACE_ROLE_REMOTE) {
1139                 struct ivi_surface *previous_active;
1140
1141                 previous_active = ivi_output->previous_active;
1142                 if (!previous_active) {
1143                         /* we don't have a previous active it means we should
1144                          * display the bg */
1145                         if (ivi_output->active) {
1146                                 struct weston_view *view;
1147
1148                                 view = ivi_output->active->view;
1149                                 view->is_mapped = false;
1150                                 view->surface->is_mapped = false;
1151
1152                                 weston_layer_entry_remove(&view->layer_link);
1153                                 weston_view_geometry_dirty(view);
1154                                 weston_surface_damage(view->surface);
1155                                 ivi_output->active = NULL;
1156                         }
1157                 } else {
1158                         struct weston_desktop_surface *dsurface;
1159                         const char *previous_active_app_id;
1160
1161                         dsurface = previous_active->dsurface;
1162                         previous_active_app_id =
1163                                 weston_desktop_surface_get_app_id(dsurface);
1164                         ivi_layout_activate(ivi_output, previous_active_app_id);
1165                 }
1166         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1167                 struct weston_view *view  = surf->view;
1168
1169                 weston_view_unmap(view);
1170                 surf->state = HIDDEN;
1171
1172                 weston_layer_entry_remove(&view->layer_link);
1173                 weston_view_geometry_dirty(view);
1174                 weston_surface_damage(view->surface);
1175         }
1176
1177       shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_DEACTIVATED);
1178 }