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