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