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