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