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