input: Add basic seat handling
[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         weston_desktop_surface_set_user_data(dsurface, surface);
89
90         if (ivi->policy && ivi->policy->api.surface_create &&
91             !ivi->policy->api.surface_create(surface, ivi)) {
92                 wl_client_post_no_memory(client);
93                 return;
94         }
95
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         /* reset any caps to make sure we apply the new caps */
103         ivi_seat_reset_caps_sent(ivi);
104
105         if (ivi->shell_client.ready) {
106                 ivi_check_pending_desktop_surface(surface);
107                 weston_log("Added surface %p, app_id %s, role %s\n", surface,
108                                 app_id, ivi_layout_get_surface_role_name(surface));
109         } else {
110                 /*
111                  * We delay creating "normal" desktop surfaces until later, to
112                  * give the shell-client an oppurtunity to set the surface as a
113                  * background/panel.
114                  */
115                 weston_log("Added surface %p, app_id %s to pending list\n",
116                                 surface, app_id);
117                 wl_list_insert(&ivi->pending_surfaces, &surface->link);
118         }
119 }
120
121 static bool
122 desktop_surface_check_last_remote_surfaces(struct ivi_compositor *ivi, enum ivi_surface_role role)
123 {
124         int count = 0;
125         struct ivi_surface *surf;
126
127         wl_list_for_each(surf, &ivi->surfaces, link)
128                 if (surf->role == role)
129                         count++;
130
131         return (count == 1);
132 }
133
134 static void
135 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
136 {
137         struct ivi_surface *surface =
138                 weston_desktop_surface_get_user_data(dsurface);
139         struct weston_surface *wsurface =
140                 weston_desktop_surface_get_surface(dsurface);
141
142         struct ivi_output *output = ivi_layout_get_output_from_surface(surface);
143
144         /* special corner-case, pending_surfaces which are never activated or
145          * being assigned an output might land here so just remove the surface;
146          *
147          * the DESKTOP role can happen here as well, because we can fall-back 
148          * to that when we try to determine the role type. Application that
149          * do not set the app_id will be land here, when destroyed */
150         if (output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
151                                surface->role == IVI_SURFACE_ROLE_DESKTOP))
152                 goto skip_output_asignment;
153
154         assert(output != NULL);
155
156         /* resize the active surface to the original size */
157         if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
158             surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
159                 if (output && output->active) {
160                         ivi_layout_desktop_resize(output->active, output->area_saved);
161                 }
162                 /* restore the area back so we can re-use it again if needed */
163                 output->area = output->area_saved;
164         }
165
166         /* reset the active surface as well */
167         if (output && output->active && output->active == surface) {
168                 output->active->view->is_mapped = false;
169                 output->active->view->surface->is_mapped = false;
170
171                 weston_layer_entry_remove(&output->active->view->layer_link);
172                 output->active = NULL;
173         }
174
175         /* check if there's a last 'remote' surface and insert a black
176          * surface view if there's no background set for that output
177          */
178         if (desktop_surface_check_last_remote_surfaces(output->ivi,
179             IVI_SURFACE_ROLE_REMOTE))
180                 if (!output->background)
181                         insert_black_surface(output);
182
183         if (desktop_surface_check_last_remote_surfaces(output->ivi,
184             IVI_SURFACE_ROLE_DESKTOP))
185                 if (!output->background)
186                         insert_black_surface(output);
187
188         if (weston_surface_is_mapped(wsurface)) {
189                 weston_desktop_surface_unlink_view(surface->view);
190                 weston_view_destroy(surface->view);
191         }
192
193         /* invalidate agl-shell surfaces so we can re-use them when
194          * binding again */
195         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
196                 switch (surface->panel.edge) {
197                 case AGL_SHELL_EDGE_TOP:
198                         output->top = NULL;
199                         break;
200                 case AGL_SHELL_EDGE_BOTTOM:
201                         output->bottom = NULL;
202                         break;
203                 case AGL_SHELL_EDGE_LEFT:
204                         output->left = NULL;
205                         break;
206                 case AGL_SHELL_EDGE_RIGHT:
207                         output->right = NULL;
208                         break;
209                 default:
210                         assert(!"Invalid edge detected\n");
211                 }
212         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
213                 output->background = NULL;
214         }
215
216 skip_output_asignment:
217         weston_log("Removed surface %p, app_id %s, role %s\n", surface,
218                         weston_desktop_surface_get_app_id(dsurface),
219                         ivi_layout_get_surface_role_name(surface));
220
221         /* we weren't added to any list if we are still with 'none' as role */
222         if (surface->role != IVI_SURFACE_ROLE_NONE)
223                 wl_list_remove(&surface->link);
224
225         free(surface);
226 }
227
228 static void
229 desktop_committed(struct weston_desktop_surface *dsurface, 
230                   int32_t sx, int32_t sy, void *userdata)
231 {
232         struct ivi_surface *surface =
233                 weston_desktop_surface_get_user_data(dsurface);
234         struct ivi_policy *policy = surface->ivi->policy;
235
236         if (policy && policy->api.surface_commited &&
237             !policy->api.surface_commited(surface, surface->ivi))
238                 return;
239
240         weston_compositor_schedule_repaint(surface->ivi->compositor);
241
242         switch (surface->role) {
243         case IVI_SURFACE_ROLE_DESKTOP:
244         case IVI_SURFACE_ROLE_REMOTE:
245                 ivi_layout_desktop_committed(surface);
246                 break;
247         case IVI_SURFACE_ROLE_POPUP:
248                 ivi_layout_popup_committed(surface);
249                 break;
250         case IVI_SURFACE_ROLE_FULLSCREEN:
251                 ivi_layout_fullscreen_committed(surface);
252                 break;
253         case IVI_SURFACE_ROLE_SPLIT_H:
254         case IVI_SURFACE_ROLE_SPLIT_V:
255                 ivi_layout_split_committed(surface);
256                 break;
257         case IVI_SURFACE_ROLE_NONE:
258         case IVI_SURFACE_ROLE_BACKGROUND:
259         case IVI_SURFACE_ROLE_PANEL:
260         default: /* fall through */
261                 break;
262         }
263 }
264
265 static void
266 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
267                          struct weston_seat *seat, int32_t x, int32_t y,
268                          void *userdata)
269 {
270         /* not supported */
271 }
272
273 static void
274 desktop_set_parent(struct weston_desktop_surface *dsurface,
275                    struct weston_desktop_surface *parent, void *userdata)
276 {
277         /* not supported */
278 }
279
280 static void
281 desktop_move(struct weston_desktop_surface *dsurface,
282              struct weston_seat *seat, uint32_t serial, void *userdata)
283 {
284         /* not supported */
285 }
286
287 static void
288 desktop_resize(struct weston_desktop_surface *dsurface,
289                struct weston_seat *seat, uint32_t serial,
290                enum weston_desktop_surface_edge edges, void *user_data)
291 {
292         /* not supported */
293 }
294
295 static void
296 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
297                              bool fullscreen, struct weston_output *output,
298                              void *userdata)
299 {
300         /* not supported */
301 }
302
303 static void
304 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
305                             bool maximized, void *userdata)
306 {
307         /* not supported */
308 }
309
310 static void
311 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
312                             void *userdata)
313 {
314         /* not supported */
315 }
316
317 static void
318 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
319                               int32_t x, int32_t y, void *userdata)
320 {
321         /* not supported */
322 }
323
324 static const struct weston_desktop_api desktop_api = {
325         .struct_size = sizeof desktop_api,
326         .ping_timeout = desktop_ping_timeout,
327         .pong = desktop_pong,
328         .surface_added = desktop_surface_added,
329         .surface_removed = desktop_surface_removed,
330         .committed = desktop_committed,
331         .show_window_menu = desktop_show_window_menu,
332         .set_parent = desktop_set_parent,
333         .move = desktop_move,
334         .resize = desktop_resize,
335         .fullscreen_requested = desktop_fullscreen_requested,
336         .maximized_requested = desktop_maximized_requested,
337         .minimized_requested = desktop_minimized_requested,
338         .set_xwayland_position = desktop_set_xwayland_position,
339 };
340
341 int
342 ivi_desktop_init(struct ivi_compositor *ivi)
343 {
344         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
345         if (!ivi->desktop) {
346                 weston_log("Failed to create desktop globals");
347                 return -1;
348         }
349
350         return 0;
351 }