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