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