main: Enable remote outputs later than local (DRM) outputs
[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 <assert.h>
27 #include "ivi-compositor.h"
28 #include "policy.h"
29
30 #include <libweston/libweston.h>
31 #include <libweston-desktop/libweston-desktop.h>
32
33 #if 0
34 static struct weston_output *
35 get_default_output(struct weston_compositor *compositor)
36 {
37         if (wl_list_empty(&compositor->output_list))
38                 return NULL;
39
40         return wl_container_of(compositor->output_list.next,
41                                struct weston_output, link);
42 }
43 #endif
44
45 static void
46 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
47 {
48         /* not supported */
49 }
50
51 static void
52 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
53 {
54         /* not supported */
55 }
56
57 static void
58 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
59 {
60         struct ivi_compositor *ivi = userdata;
61         struct weston_desktop_client *dclient;
62         struct wl_client *client;
63         struct ivi_surface *surface;
64         const char *app_id = NULL;
65
66         dclient = weston_desktop_surface_get_client(dsurface);
67         client = weston_desktop_client_get_client(dclient);
68
69         surface = zalloc(sizeof *surface);
70         if (!surface) {
71                 wl_client_post_no_memory(client);
72                 return;
73         }
74
75         surface->view = weston_desktop_surface_create_view(dsurface);
76         if (!surface->view) {
77                 free(surface);
78                 wl_client_post_no_memory(client);
79                 return;
80         }
81
82         surface->ivi = ivi;
83         surface->dsurface = dsurface;
84         surface->role = IVI_SURFACE_ROLE_NONE;
85         surface->activated_by_default = false;
86
87         if (ivi->policy && ivi->policy->api.surface_create &&
88             !ivi->policy->api.surface_create(surface, ivi)) {
89                 free(surface);
90                 wl_client_post_no_memory(client);
91                 return;
92         }
93
94         weston_desktop_surface_set_user_data(dsurface, surface);
95
96         app_id = weston_desktop_surface_get_app_id(dsurface);
97
98         if (ivi->shell_client.ready) {
99                 ivi_check_pending_desktop_surface(surface);
100                 weston_log("Added surface %p, app_id %s, role %s\n", surface,
101                                 app_id, ivi_layout_get_surface_role_name(surface));
102         } else {
103                 /*
104                  * We delay creating "normal" desktop surfaces until later, to
105                  * give the shell-client an oppurtunity to set the surface as a
106                  * background/panel.
107                  */
108                 weston_log("Added surface %p, app_id %s to pending list\n",
109                                 surface, app_id);
110                 wl_list_insert(&ivi->pending_surfaces, &surface->link);
111         }
112 }
113
114 static bool
115 desktop_surface_check_last_remote_surfaces(struct ivi_compositor *ivi, enum ivi_surface_role role)
116 {
117         int count = 0;
118         struct ivi_surface *surf;
119
120         wl_list_for_each(surf, &ivi->surfaces, link)
121                 if (surf->role == role)
122                         count++;
123
124         return (count == 1);
125 }
126
127 static void
128 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
129 {
130         struct ivi_surface *surface =
131                 weston_desktop_surface_get_user_data(dsurface);
132         struct weston_surface *wsurface =
133                 weston_desktop_surface_get_surface(dsurface);
134
135         struct ivi_output *output = ivi_layout_get_output_from_surface(surface);
136         assert(output != NULL);
137
138         /* resize the active surface to the original size */
139         if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
140             surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
141                 if (output && output->active) {
142                         ivi_layout_desktop_resize(output->active, output->area_saved);
143                 }
144                 /* restore the area back so we can re-use it again if needed */
145                 output->area = output->area_saved;
146         }
147
148         /* reset the active surface as well */
149         if (output && output->active && output->active == surface) {
150                 output->active->view->is_mapped = false;
151                 output->active->view->surface->is_mapped = false;
152
153                 weston_layer_entry_remove(&output->active->view->layer_link);
154                 output->active = NULL;
155         }
156
157         /* check if there's a last 'remote' surface and insert a black
158          * surface view if there's no background set for that output
159          */
160         if (desktop_surface_check_last_remote_surfaces(output->ivi,
161             IVI_SURFACE_ROLE_REMOTE))
162                 if (!output->background)
163                         insert_black_surface(output);
164
165         if (desktop_surface_check_last_remote_surfaces(output->ivi,
166             IVI_SURFACE_ROLE_DESKTOP))
167                 if (!output->background)
168                         insert_black_surface(output);
169
170         if (weston_surface_is_mapped(wsurface)) {
171                 weston_desktop_surface_unlink_view(surface->view);
172                 weston_view_destroy(surface->view);
173         }
174
175         /* invalidate agl-shell surfaces so we can re-use them when
176          * binding again */
177         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
178                 switch (surface->panel.edge) {
179                 case AGL_SHELL_EDGE_TOP:
180                         output->top = NULL;
181                         break;
182                 case AGL_SHELL_EDGE_BOTTOM:
183                         output->bottom = NULL;
184                         break;
185                 case AGL_SHELL_EDGE_LEFT:
186                         output->left = NULL;
187                         break;
188                 case AGL_SHELL_EDGE_RIGHT:
189                         output->right = NULL;
190                         break;
191                 default:
192                         assert(!"Invalid edge detected\n");
193                 }
194         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
195                 output->background = NULL;
196         }
197
198         weston_log("Removed surface %p, app_id %s, role %s\n", surface,
199                         weston_desktop_surface_get_app_id(dsurface),
200                         ivi_layout_get_surface_role_name(surface));
201         wl_list_remove(&surface->link);
202         free(surface);
203 }
204
205 static void
206 desktop_committed(struct weston_desktop_surface *dsurface, 
207                   int32_t sx, int32_t sy, void *userdata)
208 {
209         struct ivi_surface *surface =
210                 weston_desktop_surface_get_user_data(dsurface);
211         struct ivi_policy *policy = surface->ivi->policy;
212
213         if (policy && policy->api.surface_commited &&
214             !policy->api.surface_commited(surface, surface->ivi))
215                 return;
216
217         weston_compositor_schedule_repaint(surface->ivi->compositor);
218
219         switch (surface->role) {
220         case IVI_SURFACE_ROLE_DESKTOP:
221         case IVI_SURFACE_ROLE_REMOTE:
222                 ivi_layout_desktop_committed(surface);
223                 break;
224         case IVI_SURFACE_ROLE_POPUP:
225                 ivi_layout_popup_committed(surface);
226                 break;
227         case IVI_SURFACE_ROLE_FULLSCREEN:
228                 ivi_layout_fullscreen_committed(surface);
229                 break;
230         case IVI_SURFACE_ROLE_SPLIT_H:
231         case IVI_SURFACE_ROLE_SPLIT_V:
232                 ivi_layout_split_committed(surface);
233                 break;
234         case IVI_SURFACE_ROLE_NONE:
235         case IVI_SURFACE_ROLE_BACKGROUND:
236         case IVI_SURFACE_ROLE_PANEL:
237         default: /* fall through */
238                 break;
239         }
240 }
241
242 static void
243 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
244                          struct weston_seat *seat, int32_t x, int32_t y,
245                          void *userdata)
246 {
247         /* not supported */
248 }
249
250 static void
251 desktop_set_parent(struct weston_desktop_surface *dsurface,
252                    struct weston_desktop_surface *parent, void *userdata)
253 {
254         /* not supported */
255 }
256
257 static void
258 desktop_move(struct weston_desktop_surface *dsurface,
259              struct weston_seat *seat, uint32_t serial, void *userdata)
260 {
261         /* not supported */
262 }
263
264 static void
265 desktop_resize(struct weston_desktop_surface *dsurface,
266                struct weston_seat *seat, uint32_t serial,
267                enum weston_desktop_surface_edge edges, void *user_data)
268 {
269         /* not supported */
270 }
271
272 static void
273 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
274                              bool fullscreen, struct weston_output *output,
275                              void *userdata)
276 {
277         /* not supported */
278 }
279
280 static void
281 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
282                             bool maximized, void *userdata)
283 {
284         /* not supported */
285 }
286
287 static void
288 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
289                             void *userdata)
290 {
291         /* not supported */
292 }
293
294 static void
295 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
296                               int32_t x, int32_t y, void *userdata)
297 {
298         /* not supported */
299 }
300
301 static const struct weston_desktop_api desktop_api = {
302         .struct_size = sizeof desktop_api,
303         .ping_timeout = desktop_ping_timeout,
304         .pong = desktop_pong,
305         .surface_added = desktop_surface_added,
306         .surface_removed = desktop_surface_removed,
307         .committed = desktop_committed,
308         .show_window_menu = desktop_show_window_menu,
309         .set_parent = desktop_set_parent,
310         .move = desktop_move,
311         .resize = desktop_resize,
312         .fullscreen_requested = desktop_fullscreen_requested,
313         .maximized_requested = desktop_maximized_requested,
314         .minimized_requested = desktop_minimized_requested,
315         .set_xwayland_position = desktop_set_xwayland_position,
316 };
317
318 int
319 ivi_desktop_init(struct ivi_compositor *ivi)
320 {
321         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
322         if (!ivi->desktop) {
323                 weston_log("Failed to create desktop globals");
324                 return -1;
325         }
326
327         return 0;
328 }