Import source, backport to libweston 6.0
[src/agl-compositor.git] / src / desktop.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 <libweston-6/compositor.h>
29 #include <libweston-6/libweston-desktop.h>
30
31 #if 0
32 static struct weston_output *
33 get_default_output(struct weston_compositor *compositor)
34 {
35         if (wl_list_empty(&compositor->output_list))
36                 return NULL;
37
38         return wl_container_of(compositor->output_list.next,
39                                struct weston_output, link);
40 }
41 #endif
42
43 static void
44 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
45 {
46         /* not supported */
47 }
48
49 static void
50 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
51 {
52         /* not supported */
53 }
54
55 static void
56 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
57 {
58         struct ivi_compositor *ivi = userdata;
59         struct weston_desktop_client *dclient;
60         struct wl_client *client;
61         struct ivi_surface *surface;
62
63         dclient = weston_desktop_surface_get_client(dsurface);
64         client = weston_desktop_client_get_client(dclient);
65
66         surface = zalloc(sizeof *surface);
67         if (!surface) {
68                 wl_client_post_no_memory(client);
69                 return;
70         }
71
72         surface->ivi = ivi;
73         surface->dsurface = dsurface;
74         surface->role = IVI_SURFACE_ROLE_NONE;
75         surface->old_geom.width = -1;
76         surface->old_geom.height = -1;
77
78         weston_desktop_surface_set_user_data(dsurface, surface);
79
80         if (ivi->shell_client.ready) {
81                 ivi_set_desktop_surface(surface);
82
83                 ivi_reflow_outputs(ivi);
84         } else {
85                 /*
86                  * We delay creating "normal" desktop surfaces until later, to
87                  * give the shell-client an oppurtunity to set the surface as a
88                  * background/panel.
89                  */
90                 wl_list_insert(&ivi->pending_surfaces, &surface->link);
91         }
92 }
93
94 static void
95 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
96 {
97         struct ivi_surface *surface =
98                 weston_desktop_surface_get_user_data(dsurface);
99         struct weston_surface *wsurface =
100                 weston_desktop_surface_get_surface(dsurface);
101         struct ivi_compositor *ivi = surface->ivi;
102
103         /* TODO */
104         if (surface->role != IVI_SURFACE_ROLE_DESKTOP)
105                 return;
106
107         if (weston_surface_is_mapped(wsurface)) {
108                 weston_desktop_surface_unlink_view(surface->desktop.view);
109                 weston_view_destroy(surface->desktop.view);
110                 wl_list_remove(&surface->link);
111         }
112         free(surface);
113
114         ivi_reflow_outputs(ivi);
115 }
116
117 static void
118 surface_committed(struct ivi_surface *surface)
119 {
120         struct ivi_compositor *ivi = surface->ivi;
121         struct weston_desktop_surface *dsurface = surface->dsurface;
122         struct weston_geometry geom, old_geom;
123
124         old_geom = surface->old_geom;
125         geom = weston_desktop_surface_get_geometry(dsurface);
126
127         surface->old_geom = geom;
128
129         if (geom.width != old_geom.width || geom.height != old_geom.height) {
130                 ivi_reflow_outputs(ivi);
131         }
132
133         //wl_list_insert(&ivi->surfaces, &surface->link);
134 }
135
136 static void
137 background_committed(struct ivi_surface *surface)
138 {
139         struct ivi_compositor *ivi = surface->ivi;
140         struct ivi_output *output = surface->bg.output;
141         struct weston_output *woutput = output->output;
142         struct weston_desktop_surface *dsurface = surface->dsurface;
143         struct weston_surface *wsurface =
144                 weston_desktop_surface_get_surface(dsurface);
145
146         if (wsurface->is_mapped)
147                 return;
148
149         surface->bg.view = weston_desktop_surface_create_view(dsurface);
150
151         weston_view_set_output(surface->bg.view, woutput);
152         weston_view_set_position(surface->bg.view,
153                                  woutput->x,
154                                  woutput->y);
155         weston_layer_entry_insert(&ivi->background.view_list,
156                                   &surface->bg.view->layer_link);
157
158         weston_view_update_transform(surface->bg.view);
159         weston_view_schedule_repaint(surface->bg.view);
160
161         wsurface->is_mapped = true;
162 }
163
164 static void
165 panel_committed(struct ivi_surface *surface)
166 {
167         struct ivi_compositor *ivi = surface->ivi;
168         struct ivi_output *output = surface->bg.output;
169         struct weston_output *woutput = output->output;
170         struct weston_desktop_surface *dsurface = surface->dsurface;
171         struct weston_surface *wsurface =
172                 weston_desktop_surface_get_surface(dsurface);
173         struct weston_geometry geom;
174         int32_t x = woutput->x;
175         int32_t y = woutput->y;
176
177         if (wsurface->is_mapped)
178                 return;
179
180         surface->panel.view = weston_desktop_surface_create_view(dsurface);
181
182         geom = weston_desktop_surface_get_geometry(dsurface);
183         switch (surface->panel.edge) {
184         case AGL_SHELL_EDGE_TOP:
185                 /* Do nothing */
186                 break;
187         case AGL_SHELL_EDGE_BOTTOM:
188                 y += woutput->height - geom.height;
189                 break;
190         case AGL_SHELL_EDGE_LEFT:
191                 /* Do nothing */
192                 break;
193         case AGL_SHELL_EDGE_RIGHT:
194                 x += woutput->width - geom.width;
195                 break;
196         }
197
198         weston_view_set_output(surface->panel.view, woutput);
199         weston_view_set_position(surface->panel.view, x, y);
200         weston_layer_entry_insert(&ivi->normal.view_list,
201                                   &surface->panel.view->layer_link);
202
203         weston_view_update_transform(surface->panel.view);
204         weston_view_schedule_repaint(surface->panel.view);
205
206         wsurface->is_mapped = true;
207 }
208
209 static void
210 desktop_committed(struct weston_desktop_surface *dsurface, 
211                   int32_t sx, int32_t sy, void *userdata)
212 {
213         struct ivi_surface *surface =
214                 weston_desktop_surface_get_user_data(dsurface);
215
216         weston_compositor_schedule_repaint(surface->ivi->compositor);
217
218         switch (surface->role) {
219         case IVI_SURFACE_ROLE_NONE:
220                 break;
221         case IVI_SURFACE_ROLE_DESKTOP:
222                 surface_committed(surface);
223                 break;
224         case IVI_SURFACE_ROLE_BACKGROUND:
225                 background_committed(surface);
226                 break;
227         case IVI_SURFACE_ROLE_PANEL:
228                 panel_committed(surface);
229                 break;
230         }
231 }
232
233 static void
234 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
235                          struct weston_seat *seat, int32_t x, int32_t y,
236                          void *userdata)
237 {
238         /* not supported */
239 }
240
241 static void
242 desktop_set_parent(struct weston_desktop_surface *dsurface,
243                    struct weston_desktop_surface *parent, void *userdata)
244 {
245         /* not supported */
246 }
247
248 static void
249 desktop_move(struct weston_desktop_surface *dsurface,
250              struct weston_seat *seat, uint32_t serial, void *userdata)
251 {
252         /* not supported */
253 }
254
255 static void
256 desktop_resize(struct weston_desktop_surface *dsurface,
257                struct weston_seat *seat, uint32_t serial,
258                enum weston_desktop_surface_edge edges, void *user_data)
259 {
260         /* not supported */
261 }
262
263 static void
264 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
265                              bool fullscreen, struct weston_output *output,
266                              void *userdata)
267 {
268         /* not supported */
269 }
270
271 static void
272 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
273                             bool maximized, void *userdata)
274 {
275         /* not supported */
276 }
277
278 static void
279 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
280                             void *userdata)
281 {
282         /* not supported */
283 }
284
285 static void
286 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
287                               int32_t x, int32_t y, void *userdata)
288 {
289         /* not supported */
290 }
291
292 static const struct weston_desktop_api desktop_api = {
293         .struct_size = sizeof desktop_api,
294         .ping_timeout = desktop_ping_timeout,
295         .pong = desktop_pong,
296         .surface_added = desktop_surface_added,
297         .surface_removed = desktop_surface_removed,
298         .committed = desktop_committed,
299         .show_window_menu = desktop_show_window_menu,
300         .set_parent = desktop_set_parent,
301         .move = desktop_move,
302         .resize = desktop_resize,
303         .fullscreen_requested = desktop_fullscreen_requested,
304         .maximized_requested = desktop_maximized_requested,
305         .minimized_requested = desktop_minimized_requested,
306         .set_xwayland_position = desktop_set_xwayland_position,
307 };
308
309 int
310 ivi_desktop_init(struct ivi_compositor *ivi)
311 {
312         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
313         if (!ivi->desktop) {
314                 weston_log("Failed to create desktop globals");
315                 return -1;
316         }
317
318         return 0;
319 }