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