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