layout: Fix no-op check due to invalid checks
[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                                 ivi_layout_activate_by_surf(r_output, surf);
591                         }
592                 }
593
594                 return;
595         }
596
597         if (!weston_desktop_surface_get_maximized(dsurf) ||
598             geom.width != output->area.width ||
599             geom.height != output->area.height)
600                 return;
601
602         ivi_layout_activate_complete(output, surf);
603 }
604
605 void
606 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
607 {
608         struct ivi_compositor *ivi = surface->ivi;
609         struct ivi_policy *policy = ivi->policy;
610
611         struct weston_desktop_surface *dsurface = surface->dsurface;
612         struct weston_surface *wsurface =
613                 weston_desktop_surface_get_surface(dsurface);
614         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
615
616         struct ivi_output *output = surface->split.output;
617         struct weston_output *woutput = output->output;
618         struct ivi_output *bg_output = ivi_layout_find_bg_output(ivi);
619
620         struct weston_view *view = surface->view;
621         struct weston_geometry geom =
622                 weston_desktop_surface_get_geometry(dsurface);
623
624         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
625         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
626
627         bool is_fullscreen = weston_desktop_surface_get_fullscreen(dsurface);
628         bool is_dim_same =
629                 geom.width == bg_output->output->width &&
630                 geom.height == bg_output->output->height;
631
632         if (policy && policy->api.surface_activate_by_default &&
633             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
634             !surface->mapped)
635                 return;
636
637         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
638
639
640         if (surface->state == FULLSCREEN && weston_view_is_mapped(view))
641                 return;
642
643         /* if we still get here but we haven't resized so far, send configure
644          * events to do so */
645         if (surface->state != RESIZING && (!is_fullscreen || !is_dim_same)) {
646                 struct ivi_output *bg_output =
647                         ivi_layout_find_bg_output(surface->ivi);
648
649                 weston_log("Placing fullscreen app_id %s, type %s in hidden layer\n",
650                                 app_id, ivi_layout_get_surface_role_name(surface));
651                 weston_desktop_surface_set_fullscreen(dsurface, true);
652                 weston_desktop_surface_set_size(dsurface,
653                                                 bg_output->output->width,
654                                                 bg_output->output->height);
655
656                 surface->state = RESIZING;
657                 weston_view_set_output(view, output->output);
658                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
659                 return;
660         }
661
662         /* eventually, we would set the surface fullscreen, but the client
663          * hasn't resized correctly by this point, so terminate connection */
664         if (surface->state == RESIZING && is_fullscreen && !is_dim_same) {
665                 struct weston_desktop_client *desktop_client =
666                         weston_desktop_surface_get_client(dsurface);
667                 struct wl_client *client =
668                         weston_desktop_client_get_client(desktop_client);
669                 wl_client_post_implementation_error(client,
670                                 "can not display surface due to invalid geometry."
671                                 " Client should perform a geometry resize!");
672                 return;
673         }
674
675         /* this implies we resized correctly */
676         if (!weston_view_is_mapped(view) || surface->state != FULLSCREEN) {
677                 weston_layer_entry_remove(&view->layer_link);
678
679                 weston_view_set_output(view, woutput);
680                 weston_view_set_position(view, woutput->x, woutput->y);
681                 weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
682
683                 wsurface->is_mapped = true;
684                 surface->view->is_mapped = true;
685                 surface->state = FULLSCREEN;
686
687                 weston_view_geometry_dirty(view);
688                 weston_surface_damage(view->surface);
689
690                 if (ivi_seat)
691                         ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
692
693                 shell_advertise_app_state(ivi, app_id,
694                                 NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
695
696                 weston_log("Activation completed for app_id %s, role %s, "
697                            "output %s\n", app_id,
698                            ivi_layout_get_surface_role_name(surface),
699                            output->name);
700
701         }
702 }
703
704 void
705 ivi_layout_desktop_resize(struct ivi_surface *surface,
706                           struct weston_geometry area)
707 {
708         struct weston_desktop_surface *dsurf = surface->dsurface;
709         struct weston_view *view = surface->view;
710
711         int x = area.x;
712         int y = area.y;
713         int width = area.width;
714         int height = area.height;
715
716         weston_desktop_surface_set_size(dsurf,
717                                         width, height);
718
719         weston_view_set_position(view, x, y);
720
721         weston_view_geometry_dirty(view);
722         weston_surface_damage(view->surface);
723 }
724
725 void
726 ivi_layout_split_committed(struct ivi_surface *surface)
727 {
728         struct ivi_compositor *ivi = surface->ivi;
729         struct ivi_policy *policy = ivi->policy;
730
731         struct weston_desktop_surface *dsurface = surface->dsurface;
732         struct weston_surface *wsurface =
733                 weston_desktop_surface_get_surface(dsurface);
734         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
735
736         struct ivi_output *output = surface->split.output;
737         struct weston_output *woutput = output->output;
738
739         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
740         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
741
742         struct weston_view *view = surface->view;
743         struct weston_geometry geom;
744
745         int x, y;
746         int width, height;
747
748         x = woutput->x;
749         y = woutput->y;
750
751         if (policy && policy->api.surface_activate_by_default &&
752             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
753             !surface->mapped)
754                 return;
755
756         if (surface->view->is_mapped)
757                 return;
758
759         geom = weston_desktop_surface_get_geometry(dsurface);
760
761         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
762                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
763
764         /* save the previous area in order to recover it back when if this kind
765          * of surface is being destroyed/removed */
766         output->area_saved = output->area;
767
768         switch (surface->role) {
769         case IVI_SURFACE_ROLE_SPLIT_V:
770                 geom.width = (output->area.width / 2);
771
772                 x += woutput->width - geom.width;
773                 output->area.width -= geom.width;
774
775                 width = woutput->width - x;
776                 height = output->area.height;
777                 y = output->area.y;
778
779                 break;
780         case IVI_SURFACE_ROLE_SPLIT_H:
781                 geom.height = (output->area.height / 2);
782
783                 y = output->area.y;
784                 output->area.y += geom.height;
785                 output->area.height -= geom.height;
786
787                 width = output->area.width;
788                 height = output->area.height;
789
790                 x = output->area.x;
791
792                 break;
793         default:
794                 assert(!"Invalid split orientation\n");
795         }
796
797         weston_desktop_surface_set_size(dsurface,
798                                         width, height);
799
800         /* resize the active surface first, output->area already contains
801          * correct area to resize to */
802         if (output->active)
803                 ivi_layout_desktop_resize(output->active, output->area);
804
805         weston_view_set_output(view, woutput);
806         weston_view_set_position(view, x, y);
807         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
808
809         weston_view_geometry_dirty(view);
810         weston_surface_damage(view->surface);
811
812         if (ivi_seat)
813                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
814
815         wsurface->is_mapped = true;
816         surface->view->is_mapped = true;
817
818         shell_advertise_app_state(ivi, app_id,
819                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
820
821         weston_log("Activation completed for app_id %s, role %s, output %s\n",
822                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
823 }
824
825 static void
826 ivi_compute_popup_position(const struct weston_output *output, struct weston_view *view,
827                            int initial_x, int initial_y, int *new_x, int *new_y)
828 {
829         *new_x = output->x + initial_x;
830         *new_y = output->y + initial_y;
831 }
832
833
834 bool
835 ivi_surf_in_hidden_layer(struct ivi_compositor *ivi, struct ivi_surface *surface)
836 {
837         struct weston_view *ev;
838
839         wl_list_for_each(ev, &ivi->hidden.view_list.link, layer_link.link) {
840                 if (ev == surface->view)
841                         return true;
842         }
843
844         wl_list_for_each(ev, &ivi->fullscreen.view_list.link, layer_link.link) {
845                 if (ev == surface->view)
846                         return true;
847         }
848
849         return false;
850 }
851
852 void
853 ivi_layout_popup_committed(struct ivi_surface *surface)
854 {
855         struct ivi_compositor *ivi = surface->ivi;
856         struct ivi_policy *policy = ivi->policy;
857
858         struct weston_desktop_surface *dsurface = surface->dsurface;
859         struct weston_surface *wsurface =
860                 weston_desktop_surface_get_surface(dsurface);
861         const char *app_id = weston_desktop_surface_get_app_id(dsurface);
862
863         int new_x, new_y;
864
865         struct ivi_output *output = surface->popup.output;
866         struct weston_output *woutput = output->output;
867
868         struct weston_seat *wseat = get_ivi_shell_weston_first_seat(ivi);
869         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(wseat);
870
871         struct weston_view *view = surface->view;
872
873         if (policy && policy->api.surface_activate_by_default &&
874             !policy->api.surface_activate_by_default(surface, surface->ivi) &&
875             !surface->mapped)
876                 return;
877
878         if (surface->view->is_mapped || surface->state == HIDDEN)
879                 return;
880
881         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
882
883         /* remove it from hidden layer if present */
884         if (ivi_surf_in_hidden_layer(ivi, surface))
885                 weston_layer_entry_remove(&view->layer_link);
886
887         weston_view_set_output(view, woutput);
888
889         ivi_compute_popup_position(woutput, view,
890                                    surface->popup.x, surface->popup.y, &new_x, &new_y);
891         weston_view_set_position(view, new_x, new_y);
892         weston_view_update_transform(view);
893
894         /* only clip the pop-up dialog window if we have a valid
895          * width and height being passed on. Users might not want to have one
896          * set-up so only enfore it is really passed on. */
897         if (surface->popup.bb.width > 0 && surface->popup.bb.height > 0)
898                 weston_view_set_mask(view, surface->popup.bb.x, surface->popup.bb.y,
899                                      surface->popup.bb.width, surface->popup.bb.height);
900
901         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
902
903         weston_view_geometry_dirty(view);
904         weston_surface_damage(view->surface);
905
906         if (ivi_seat)
907                 ivi_shell_activate_surface(surface, ivi_seat, WESTON_ACTIVATE_FLAG_NONE);
908
909         wsurface->is_mapped = true;
910         surface->view->is_mapped = true;
911
912         shell_advertise_app_state(ivi, app_id,
913                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
914
915         weston_log("Activation completed for app_id %s, role %s, output %s\n",
916                         app_id, ivi_layout_get_surface_role_name(surface), output->name);
917 }
918
919 static void
920 ivi_layout_popup_re_add(struct ivi_surface *surface)
921 {
922         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
923         struct weston_view *view = surface->view;
924
925         if (weston_view_is_mapped(view)) {
926                 struct weston_desktop_surface *dsurface = surface->dsurface;
927                 struct weston_surface *wsurface =
928                         weston_desktop_surface_get_surface(dsurface);
929
930                 weston_layer_entry_remove(&view->layer_link);
931
932                 wsurface->is_mapped = false;
933                 view->is_mapped = false;
934         }
935
936         /* reset the activate by default in order to (still) allow the surface
937          * to be activaved using the request */
938         if (!surface->mapped)
939                 surface->mapped = true;
940
941         surface->state = NORMAL;
942         ivi_layout_popup_committed(surface);
943 }
944
945 static bool
946 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
947 {
948         struct ivi_compositor *ivi = surf->ivi;
949         struct ivi_surface *is;
950
951         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
952             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
953             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
954                 return false;
955
956         /* reset the activate by default in order to (still) allow the surface
957          * to be activaved using the request */
958         if (!surf->mapped)
959                 surf->mapped = true;
960
961         wl_list_for_each(is, &ivi->surfaces, link)
962                 if (is == surf)
963                         return true;
964
965         return false;
966 }
967
968 void
969 ivi_layout_activate_by_surf(struct ivi_output *output, struct ivi_surface *surf)
970 {
971         struct ivi_compositor *ivi = output->ivi;
972         struct weston_desktop_surface *dsurf;
973         struct weston_geometry geom;
974         struct ivi_policy *policy = output->ivi->policy;
975
976         dsurf = surf->dsurface;
977
978         const char *app_id = weston_desktop_surface_get_app_id(dsurf);
979
980         if (!surf)
981                 return;
982
983         if (policy && policy->api.surface_activate &&
984             !policy->api.surface_activate(surf, surf->ivi)) {
985                 return;
986         }
987
988 #ifdef AGL_COMP_DEBUG
989         weston_log("Activating app_id %s, type %s, on output %s\n", app_id,
990                         ivi_layout_get_surface_role_name(surf), output->output->name);
991 #endif
992
993         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
994                 ivi_layout_popup_re_add(surf);
995                 return;
996         }
997
998         /* do not 're'-activate surfaces that are split or active */
999         if (surf == output->active ||
1000             ivi_layout_surface_is_split_or_fullscreen(surf)) {
1001                 weston_log("Application %s is already active on output %s\n",
1002                                 app_id, output->output->name);
1003                 return;
1004         }
1005
1006         if (surf->role == IVI_SURFACE_ROLE_REMOTE) {
1007                 struct ivi_output *remote_output =
1008                         ivi_layout_find_with_app_id(app_id, ivi);
1009
1010                 weston_log("Changed activation for app_id %s, type %s, on output %s\n", app_id,
1011                                 ivi_layout_get_surface_role_name(surf), output->output->name);
1012
1013                 /* if already active on a remote output do not
1014                  * attempt to activate it again */
1015                 if (remote_output && remote_output->active == surf)
1016                         return;
1017         }
1018
1019
1020         geom = weston_desktop_surface_get_geometry(dsurf);
1021
1022         if (surf->role == IVI_SURFACE_ROLE_DESKTOP)
1023                 surf->desktop.pending_output = output;
1024         if (weston_desktop_surface_get_maximized(dsurf) &&
1025             geom.width == output->area.width &&
1026             geom.height == output->area.height) {
1027                 ivi_layout_activate_complete(output, surf);
1028                 return;
1029         }
1030
1031         /* the background surface is already "maximized" so we don't need to
1032          * add to the hidden layer */
1033         if (surf->role == IVI_SURFACE_ROLE_BACKGROUND) {
1034                 ivi_layout_activate_complete(output, surf);
1035                 return;
1036         }
1037
1038         ivi_layout_add_to_hidden_layer(surf, output);
1039 }
1040
1041 void
1042 ivi_layout_activate(struct ivi_output *output, const char *app_id)
1043 {
1044         struct ivi_surface *surf;
1045         struct ivi_compositor *ivi = output->ivi;
1046
1047         if (!app_id)
1048                 return;
1049
1050         surf = ivi_find_app(ivi, app_id);
1051         if (!surf)
1052                 return;
1053
1054         ivi_layout_activate_by_surf(output, surf);
1055 }
1056
1057 struct ivi_output *
1058 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
1059 {
1060         struct ivi_output *ivi_output = NULL;
1061
1062         switch (surf->role) {
1063         case IVI_SURFACE_ROLE_DESKTOP:
1064                 if (surf->desktop.pending_output)
1065                         ivi_output = surf->desktop.pending_output;
1066                 else
1067                         ivi_output = surf->desktop.last_output;
1068                 break;
1069         case IVI_SURFACE_ROLE_POPUP:
1070                 ivi_output = surf->popup.output;
1071                 break;
1072         case IVI_SURFACE_ROLE_BACKGROUND:
1073                 ivi_output = surf->bg.output;
1074                 break;
1075         case IVI_SURFACE_ROLE_PANEL:
1076                 ivi_output = surf->panel.output;
1077                 break;
1078         case IVI_SURFACE_ROLE_FULLSCREEN:
1079                 ivi_output = surf->fullscreen.output;
1080                 break;
1081         case IVI_SURFACE_ROLE_SPLIT_H:
1082         case IVI_SURFACE_ROLE_SPLIT_V:
1083                 ivi_output = surf->split.output;
1084                 break;
1085         case IVI_SURFACE_ROLE_REMOTE:
1086                 ivi_output = surf->remote.output;
1087                 break;
1088         case IVI_SURFACE_ROLE_NONE:
1089         default:
1090                 break;
1091         }
1092
1093         return ivi_output;
1094 }
1095
1096 void
1097 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
1098 {
1099         struct ivi_surface *surf;
1100         struct ivi_output *ivi_output;
1101         struct ivi_policy *policy = ivi->policy;
1102
1103         if (!app_id)
1104                 return;
1105
1106         surf = ivi_find_app(ivi, app_id);
1107         if (!surf)
1108                 return;
1109
1110         if (policy && policy->api.surface_deactivate &&
1111             !policy->api.surface_deactivate(surf, surf->ivi)) {
1112                 return;
1113         }
1114
1115         ivi_output = ivi_layout_get_output_from_surface(surf);
1116         weston_log("Deactiving %s, role %s\n", app_id,
1117                         ivi_layout_get_surface_role_name(surf));
1118
1119         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
1120                 struct ivi_surface *previous_active;
1121
1122                 previous_active = ivi_output->previous_active;
1123                 if (!previous_active) {
1124                         /* we don't have a previous active it means we should
1125                          * display the bg */
1126                         if (ivi_output->active) {
1127                                 struct weston_view *view;
1128
1129                                 view = ivi_output->active->view;
1130                                 view->is_mapped = false;
1131                                 view->surface->is_mapped = false;
1132
1133                                 weston_layer_entry_remove(&view->layer_link);
1134                                 weston_view_geometry_dirty(view);
1135                                 weston_surface_damage(view->surface);
1136                                 ivi_output->active = NULL;
1137                         }
1138                 } else {
1139                         struct weston_desktop_surface *dsurface;
1140                         const char *previous_active_app_id;
1141
1142                         dsurface = previous_active->dsurface;
1143                         previous_active_app_id =
1144                                 weston_desktop_surface_get_app_id(dsurface);
1145                         ivi_layout_activate(ivi_output, previous_active_app_id);
1146                 }
1147         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
1148                 struct weston_view *view  = surf->view;
1149
1150                 weston_view_unmap(view);
1151                 surf->state = HIDDEN;
1152
1153                 weston_layer_entry_remove(&view->layer_link);
1154                 weston_view_geometry_dirty(view);
1155                 weston_surface_damage(view->surface);
1156         }
1157
1158       shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_DEACTIVATED);
1159 }