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