src/: Add basic support for app switching
[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
28 #include <assert.h>
29 #include <string.h>
30
31 #include <libweston-6/compositor.h>
32 #include <libweston-6/libweston-desktop.h>
33
34 static void
35 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
36 {
37         struct weston_output *woutput = output->output;
38         struct ivi_surface *bg = output->background;
39         struct weston_view *view;
40
41         if (!bg) {
42                 weston_log("WARNING: Output does not have a background\n");
43                 return;
44         }
45
46         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
47
48         view = bg->view;
49
50         weston_view_set_output(view, woutput);
51         weston_view_set_position(view, woutput->x, woutput->y);
52
53         view->is_mapped = true;
54         view->surface->is_mapped = true;
55
56         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
57 }
58
59 static void
60 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
61                struct ivi_surface *panel)
62 {
63         struct weston_output *woutput = output->output;
64         struct weston_desktop_surface *dsurface;
65         struct weston_view *view;
66         struct weston_geometry geom;
67         int x = woutput->x;
68         int y = woutput->y;
69
70         if (!panel)
71                 return;
72
73         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
74         dsurface = panel->dsurface;
75         view = panel->view;
76         geom = weston_desktop_surface_get_geometry(dsurface);
77
78         switch (panel->panel.edge) {
79         case AGL_SHELL_EDGE_TOP:
80                 output->area.y += geom.height;
81                 output->area.height -= geom.height;
82                 break;
83         case AGL_SHELL_EDGE_BOTTOM:
84                 y += woutput->height - geom.height;
85                 output->area.height -= geom.height;
86                 break;
87         case AGL_SHELL_EDGE_LEFT:
88                 output->area.x += geom.width;
89                 output->area.width -= geom.width;
90                 break;
91         case AGL_SHELL_EDGE_RIGHT:
92                 x += woutput->width - geom.width;
93                 output->area.width -= geom.width;
94                 break;
95         }
96
97         x -= geom.x;
98         y -= geom.y;
99
100         weston_view_set_output(view, woutput);
101         weston_view_set_position(view, x, y);
102
103         view->is_mapped = true;
104         view->surface->is_mapped = true;
105
106         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
107 }
108
109 /*
110  * Initializes all static parts of the layout, i.e. the background and panels.
111  */
112 void
113 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
114 {
115         ivi_background_init(ivi, output);
116
117         output->area.x = 0;
118         output->area.y = 0;
119         output->area.width = output->output->width;
120         output->area.height = output->output->height;
121
122         ivi_panel_init(ivi, output, output->top);
123         ivi_panel_init(ivi, output, output->bottom);
124         ivi_panel_init(ivi, output, output->left);
125         ivi_panel_init(ivi, output, output->right);
126
127         weston_compositor_schedule_repaint(ivi->compositor);
128
129         weston_log("Usable area: %dx%d+%d,%d\n",
130                    output->area.width, output->area.height,
131                    output->area.x, output->area.y);
132 }
133
134 static struct ivi_surface *
135 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
136 {
137         struct ivi_surface *surf;
138         const char *id;
139
140         wl_list_for_each(surf, &ivi->surfaces, link) {
141                 id = weston_desktop_surface_get_app_id(surf->dsurface);
142                 if (id && strcmp(app_id, id) == 0)
143                         return surf;
144         }
145
146         return NULL;
147 }
148
149 static void
150 ivi_layout_activate_complete(struct ivi_output *output,
151                              struct ivi_surface *surf)
152 {
153         struct ivi_compositor *ivi = output->ivi;
154         struct weston_output *woutput = output->output;
155         struct weston_view *view = surf->view;
156
157         if (weston_view_is_mapped(view)) {
158                 weston_layer_entry_remove(&view->layer_link);
159         }
160
161         weston_view_set_output(view, woutput);
162         weston_view_set_position(view,
163                                  woutput->x + output->area.x,
164                                  woutput->y + output->area.y);
165
166         view->is_mapped = true;
167         view->surface->is_mapped = true;
168
169         if (output->active) {
170                 output->active->view->is_mapped = false;
171                 output->active->view->surface->is_mapped = false;
172
173                 weston_layer_entry_remove(&output->active->view->layer_link);
174         }
175         output->active = surf;
176
177         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
178         weston_view_update_transform(view);
179
180         weston_view_schedule_repaint(view);
181         surf->desktop.pending_output = NULL;
182 }
183
184 void
185 ivi_layout_desktop_committed(struct ivi_surface *surf)
186 {
187         struct weston_desktop_surface *dsurf = surf->dsurface;
188         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
189         struct ivi_output *output;
190
191         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
192
193         output = surf->desktop.pending_output;
194         if (!output)
195                 return;
196
197         if (!weston_desktop_surface_get_maximized(dsurf) ||
198             geom.width != output->area.width ||
199             geom.height != output->area.height)
200                 return;
201
202         ivi_layout_activate_complete(output, surf);
203 }
204
205 void
206 ivi_layout_activate(struct ivi_output *output, const char *app_id)
207 {
208         struct ivi_compositor *ivi = output->ivi;
209         struct ivi_surface *surf;
210         struct weston_desktop_surface *dsurf;
211         struct weston_view *view;
212         struct weston_geometry geom;
213
214         surf = ivi_find_app(ivi, app_id);
215         if (!surf)
216                 return;
217
218         weston_log("Found app_id %s\n", app_id);
219
220         if (surf == output->active)
221                 return;
222
223         dsurf = surf->dsurface;
224         view = surf->view;
225         geom = weston_desktop_surface_get_geometry(dsurf);
226
227         if (weston_desktop_surface_get_maximized(dsurf) &&
228             geom.width == output->area.width &&
229             geom.height == output->area.height) {
230                 ivi_layout_activate_complete(output, surf);
231                 return;
232         }
233
234         weston_desktop_surface_set_maximized(dsurf, true);
235         weston_desktop_surface_set_size(dsurf,
236                                         output->area.width,
237                                         output->area.height);
238
239         /*
240          * If the view isn't mapped, we put it onto the hidden layer so it will
241          * start receiving frame events, and will be able to act on our
242          * configure event.
243          */
244         if (!weston_view_is_mapped(view)) {
245                 view->is_mapped = true;
246                 view->surface->is_mapped = true;
247
248                 weston_view_set_output(view, output->output);
249                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
250                 weston_view_schedule_repaint(view);
251         }
252
253         surf->desktop.pending_output = output;
254 }