compositor: Add XWayland basic support
[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 "shared/helpers.h"
31 #include <libweston/libweston.h>
32 #include <libweston-desktop/libweston-desktop.h>
33 #include <libweston/xwayland-api.h>
34
35 #include "agl-shell-desktop-server-protocol.h"
36
37 static void
38 desktop_advertise_app(struct wl_listener *listener, void *data)
39 {
40         struct ivi_surface *surface;
41
42         surface = wl_container_of(listener, surface, listener_advertise_app);
43
44         agl_shell_desktop_advertise_application_id(surface->ivi, surface);
45 }
46
47 static void
48 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
49 {
50         /* not supported */
51 }
52
53 static void
54 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
55 {
56         /* not supported */
57 }
58
59 struct weston_output *
60 get_default_output(struct weston_compositor *compositor)
61 {
62         if (wl_list_empty(&compositor->output_list))
63                 return NULL;
64
65         return container_of(compositor->output_list.next,
66                         struct weston_output, link);
67 }
68
69 struct weston_output *
70 get_focused_output(struct weston_compositor *compositor)
71 {
72         struct weston_seat *seat;
73         struct weston_output *output = NULL;
74
75         wl_list_for_each(seat, &compositor->seat_list, link) {
76                 struct weston_touch *touch = weston_seat_get_touch(seat);
77                 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
78                 struct weston_keyboard *keyboard =
79                         weston_seat_get_keyboard(seat);
80
81                 if (touch && touch->focus)
82                         output = touch->focus->output;
83                 else if (pointer && pointer->focus)
84                         output = pointer->focus->output;
85                 else if (keyboard && keyboard->focus)
86                         output = keyboard->focus->output;
87
88                 if (output)
89                         break;
90         }
91
92         return output;
93 }
94
95 void
96 ivi_shell_activate_surface(struct ivi_surface *ivi_surf,
97                           struct ivi_shell_seat *ivi_seat,
98                           uint32_t flags)
99 {
100        struct weston_desktop_surface *dsurface = ivi_surf->dsurface;
101        struct weston_surface *surface =
102                weston_desktop_surface_get_surface(dsurface);
103
104        weston_view_activate_input(ivi_surf->view, ivi_seat->seat, flags);
105
106        if (ivi_seat->focused_surface) {
107                struct ivi_surface *current_focus =
108                        get_ivi_shell_surface(ivi_seat->focused_surface);
109                struct weston_desktop_surface *dsurface_focus;
110                assert(current_focus);
111
112                dsurface_focus = current_focus->dsurface;
113                if (--current_focus->focus_count == 0)
114                        weston_desktop_surface_set_activated(dsurface_focus, false);
115        }
116
117        ivi_seat->focused_surface = surface;
118        if (ivi_surf->focus_count++ == 0)
119                weston_desktop_surface_set_activated(dsurface, true);
120 }
121
122
123 static void
124 desktop_surface_added_configure(struct ivi_surface *surface,
125                                 struct ivi_output *ivi_output)
126 {
127         enum ivi_surface_role role = IVI_SURFACE_ROLE_NONE;
128         struct weston_desktop_surface *dsurface = surface->dsurface;
129
130         ivi_check_pending_surface_desktop(surface, &role);
131         if ((role != IVI_SURFACE_ROLE_DESKTOP &&
132              role != IVI_SURFACE_ROLE_FULLSCREEN &&
133              role != IVI_SURFACE_ROLE_REMOTE) ||
134              role == IVI_SURFACE_ROLE_NONE)
135                 return;
136
137         if (role == IVI_SURFACE_ROLE_FULLSCREEN) {
138                 struct ivi_output *bg_output =
139                         ivi_layout_find_bg_output(surface->ivi);
140                 assert(bg_output);
141                 weston_desktop_surface_set_fullscreen(dsurface, true);
142                 weston_desktop_surface_set_size(dsurface,
143                                                 bg_output->output->width,
144                                                 bg_output->output->height);
145                 return;
146         }
147
148         weston_desktop_surface_set_maximized(dsurface, true);
149         weston_desktop_surface_set_size(dsurface,
150                                         ivi_output->area.width,
151                                         ivi_output->area.height);
152 }
153
154
155 static void
156 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
157 {
158         struct ivi_compositor *ivi = userdata;
159         struct weston_desktop_client *dclient;
160         struct wl_client *client;
161         struct ivi_surface *surface;
162         struct ivi_output *active_output = NULL;
163         struct weston_output *output = NULL;
164         const char *app_id = NULL;
165
166         dclient = weston_desktop_surface_get_client(dsurface);
167         client = weston_desktop_client_get_client(dclient);
168
169         if (ivi->shell_client.resource &&
170             ivi->shell_client.status == BOUND_FAILED) {
171                 wl_client_post_implementation_error(client,
172                                        "agl_shell has already been bound. "
173                                        "Check out bound_fail event");
174                 return;
175         }
176
177         surface = zalloc(sizeof *surface);
178         if (!surface) {
179                 wl_client_post_no_memory(client);
180                 return;
181         }
182
183         surface->view = weston_desktop_surface_create_view(dsurface);
184         if (!surface->view) {
185                 free(surface);
186                 wl_client_post_no_memory(client);
187                 return;
188         }
189
190         surface->ivi = ivi;
191         surface->dsurface = dsurface;
192         surface->role = IVI_SURFACE_ROLE_NONE;
193         surface->mapped = false;
194         surface->advertised_on_launch = false;
195         surface->checked_pending = false;
196         wl_list_init(&surface->link);
197
198         wl_signal_init(&surface->signal_advertise_app);
199
200         surface->listener_advertise_app.notify = desktop_advertise_app;
201         wl_signal_add(&surface->signal_advertise_app,
202                       &surface->listener_advertise_app);
203
204         weston_desktop_surface_set_user_data(dsurface, surface);
205
206         if (ivi->policy && ivi->policy->api.surface_create &&
207             !ivi->policy->api.surface_create(surface, ivi)) {
208                 wl_client_post_no_memory(client);
209                 return;
210         }
211
212
213         app_id = weston_desktop_surface_get_app_id(dsurface);
214
215         if ((active_output = ivi_layout_find_with_app_id(app_id, ivi))) {
216                 ivi_set_pending_desktop_surface_remote(active_output, app_id);
217                 shell_send_app_on_output(ivi, app_id, active_output->output->name);
218         }
219
220         /* reset any caps to make sure we apply the new caps */
221         ivi_seat_reset_caps_sent(ivi);
222
223         output =  get_focused_output(ivi->compositor);
224         if (!output)
225                 output = get_default_output(ivi->compositor);
226
227         if (output && ivi->shell_client.ready) {
228                 struct ivi_output *ivi_output = to_ivi_output(output);
229                 if (active_output)
230                         desktop_surface_added_configure(surface, active_output);
231                 else
232                         desktop_surface_added_configure(surface, ivi_output);
233         }
234         /*
235          * We delay creating "normal" desktop surfaces until later, to
236          * give the shell-client an oppurtunity to set the surface as a
237          * background/panel.
238          * Also delay the creation in order to have a valid app_id
239          * which will be used to set the proper role.
240          */
241         weston_log("Added surface %p, app_id %s to pending list\n",
242                         surface, app_id);
243         wl_list_insert(&ivi->pending_surfaces, &surface->link);
244
245 }
246
247 bool
248 ivi_surface_count_one(struct ivi_output *ivi_output,
249                       enum ivi_surface_role role)
250 {
251         int count = 0;
252         struct ivi_surface *surf;
253
254         wl_list_for_each(surf, &ivi_output->ivi->surfaces, link)
255                 if (surf->role == role &&
256                     surf->current_completed_output == ivi_output)
257                         count++;
258
259         return (count == 1);
260 }
261
262 static void
263 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
264 {
265         struct ivi_surface *surface =
266                 weston_desktop_surface_get_user_data(dsurface);
267         struct weston_surface *wsurface =
268                 weston_desktop_surface_get_surface(dsurface);
269         const char *app_id = NULL;
270         struct weston_seat *wseat = NULL;
271         struct ivi_shell_seat *ivi_seat = NULL;
272         struct ivi_output *output = NULL;
273
274         /* we might not have a valid ivi_surface if _added failed due to
275          * protocol errors */
276         if (!surface)
277                 return;
278
279         wseat = get_ivi_shell_weston_first_seat(surface->ivi);
280         if (wseat)
281                 ivi_seat = get_ivi_shell_seat(wseat);
282
283         output = ivi_layout_get_output_from_surface(surface);
284
285         wl_list_remove(&surface->listener_advertise_app.link);
286         surface->listener_advertise_app.notify = NULL;
287
288         app_id = weston_desktop_surface_get_app_id(dsurface);
289
290         /* special corner-case, pending_surfaces which are never activated or
291          * being assigned an output might land here so just remove the surface;
292          *
293          * the DESKTOP role can happen here as well, because we can fall-back 
294          * to that when we try to determine the role type. Application that
295          * do not set the app_id will be land here, when destroyed */
296         if ((output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
297                                 surface->role == IVI_SURFACE_ROLE_DESKTOP)) ||
298              output->output == NULL)
299                 goto skip_output_asignment;
300
301         assert(output != NULL);
302
303         /* resize the active surface to the original size */
304         if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
305             surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
306                 if (output && output->active) {
307                         ivi_layout_desktop_resize(output->active, output->area_saved);
308                 }
309                 /* restore the area back so we can re-use it again if needed */
310                 output->area = output->area_saved;
311         }
312
313         /* reset the active surface as well */
314         if (output && output->active && output->active == surface) {
315                 output->active->view->is_mapped = false;
316                 output->active->view->surface->is_mapped = false;
317
318                 weston_layer_entry_remove(&output->active->view->layer_link);
319                 output->active = NULL;
320         }
321
322         /* clear out focused_surface to avoid a stale focused_surface. the
323          * client shell is responsible for keeping track and switch back to the
324          * last active surface so we don't get do anything at removal, just
325          * reset it */
326         if (ivi_seat && ivi_seat->focused_surface == wsurface)
327                 ivi_seat->focused_surface = NULL;
328
329         /* check if there's a last 'remote' surface and insert a black
330          * surface view if there's no background set for that output
331          */
332         if (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
333             ivi_surface_count_one(output, IVI_SURFACE_ROLE_DESKTOP))
334                 if (!output->background)
335                         insert_black_curtain(output);
336
337
338         if (weston_surface_is_mapped(wsurface)) {
339                 weston_desktop_surface_unlink_view(surface->view);
340                 weston_view_destroy(surface->view);
341         }
342
343         /* invalidate agl-shell surfaces so we can re-use them when
344          * binding again */
345         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
346                 switch (surface->panel.edge) {
347                 case AGL_SHELL_EDGE_TOP:
348                         output->top = NULL;
349                         break;
350                 case AGL_SHELL_EDGE_BOTTOM:
351                         output->bottom = NULL;
352                         break;
353                 case AGL_SHELL_EDGE_LEFT:
354                         output->left = NULL;
355                         break;
356                 case AGL_SHELL_EDGE_RIGHT:
357                         output->right = NULL;
358                         break;
359                 default:
360                         assert(!"Invalid edge detected\n");
361                 }
362         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
363                 output->background = NULL;
364         }
365
366 skip_output_asignment:
367         weston_log("Removed surface %p, app_id %s, role %s\n", surface,
368                         app_id, ivi_layout_get_surface_role_name(surface));
369
370         if (app_id && output && output->output) {
371                 shell_advertise_app_state(output->ivi, app_id,
372                                           NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
373                 if (output->ivi->shell_client.ready)
374                         shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
375         }
376
377         wl_list_remove(&surface->link);
378
379         free(surface);
380 }
381
382 static void
383 desktop_committed(struct weston_desktop_surface *dsurface, 
384                   int32_t sx, int32_t sy, void *userdata)
385 {
386         struct ivi_compositor *ivi = userdata;
387         struct ivi_surface *surface =
388                 weston_desktop_surface_get_user_data(dsurface);
389         struct ivi_policy *policy = surface->ivi->policy;
390
391         if (policy && policy->api.surface_commited &&
392             !policy->api.surface_commited(surface, surface->ivi))
393                 return;
394
395         if (ivi->shell_client.ready && !surface->checked_pending) {
396                 struct ivi_output *remote_output = NULL;
397                 const char *app_id =    weston_desktop_surface_get_app_id(dsurface);
398                 weston_log("Checking pending surface %p, app_id %s\n", surface,
399                         app_id);
400                 wl_list_remove(&surface->link);
401                 wl_list_init(&surface->link);
402
403                 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
404                         ivi_set_pending_desktop_surface_remote(remote_output, app_id);
405                         shell_send_app_on_output(ivi, app_id, remote_output->output->name);
406                 }
407
408
409                 ivi_check_pending_desktop_surface(surface);
410                 surface->checked_pending = true;
411
412                 /* we'll do it now at commit time, because we might not have an
413                  * appid by the time we've created the weston_desktop_surface
414                  * */
415                 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
416         }
417
418         if (!surface->advertised_on_launch &&
419             !wl_list_empty(&surface->ivi->desktop_clients))
420                 wl_signal_emit(&surface->signal_advertise_app, surface);
421
422         /* this repaint schedule is needed to allow resizing to work with the
423          * help of the hidden layer:
424          *
425          * 1. add the view in the hidden layer and send out correct dimensions
426          * 2. clients changes its dimensions
427          * 3. client commits with the new dimensions
428          *
429          * For desktop and fullscreen, desktop_surface_added() sends the
430          * dimensions from the beginning so applications no need to resize, but
431          * if that weren't the case we still need this in.
432          */
433         weston_compositor_schedule_repaint(surface->ivi->compositor);
434
435         switch (surface->role) {
436         case IVI_SURFACE_ROLE_DESKTOP:
437                 ivi_layout_desktop_committed(surface);
438                 break;
439         case IVI_SURFACE_ROLE_REMOTE:
440                 ivi_layout_remote_committed(surface);
441                 break;
442         case IVI_SURFACE_ROLE_POPUP:
443                 ivi_layout_popup_committed(surface);
444                 break;
445         case IVI_SURFACE_ROLE_FULLSCREEN:
446                 ivi_layout_fullscreen_committed(surface);
447                 break;
448         case IVI_SURFACE_ROLE_SPLIT_H:
449         case IVI_SURFACE_ROLE_SPLIT_V:
450                 ivi_layout_split_committed(surface);
451                 break;
452         case IVI_SURFACE_ROLE_NONE:
453         case IVI_SURFACE_ROLE_BACKGROUND:
454         case IVI_SURFACE_ROLE_PANEL:
455         default: /* fall through */
456                 break;
457         }
458 }
459
460 static void
461 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
462                          struct weston_seat *seat, int32_t x, int32_t y,
463                          void *userdata)
464 {
465         /* not supported */
466 }
467
468 static void
469 desktop_set_parent(struct weston_desktop_surface *dsurface,
470                    struct weston_desktop_surface *parent, void *userdata)
471 {
472         /* not supported */
473 }
474
475 static void
476 desktop_move(struct weston_desktop_surface *dsurface,
477              struct weston_seat *seat, uint32_t serial, void *userdata)
478 {
479         /* not supported */
480 }
481
482 static void
483 desktop_resize(struct weston_desktop_surface *dsurface,
484                struct weston_seat *seat, uint32_t serial,
485                enum weston_desktop_surface_edge edges, void *user_data)
486 {
487         /* not supported */
488 }
489
490 static void
491 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
492                              bool fullscreen, struct weston_output *output,
493                              void *userdata)
494 {
495         /* not supported */
496 }
497
498 static void
499 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
500                             bool maximized, void *userdata)
501 {
502         /* not supported */
503 }
504
505 static void
506 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
507                             void *userdata)
508 {
509         /* not supported */
510 }
511
512 static void
513 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
514                               int32_t x, int32_t y, void *userdata)
515 {
516         struct ivi_surface *ivisurf =
517                 weston_desktop_surface_get_user_data(dsurface);
518
519         ivisurf->xwayland.x = x;
520         ivisurf->xwayland.y = y;
521         ivisurf->xwayland.is_set = true;
522 }
523
524 static const struct weston_desktop_api desktop_api = {
525         .struct_size = sizeof desktop_api,
526         .ping_timeout = desktop_ping_timeout,
527         .pong = desktop_pong,
528         .surface_added = desktop_surface_added,
529         .surface_removed = desktop_surface_removed,
530         .committed = desktop_committed,
531         .show_window_menu = desktop_show_window_menu,
532         .set_parent = desktop_set_parent,
533         .move = desktop_move,
534         .resize = desktop_resize,
535         .fullscreen_requested = desktop_fullscreen_requested,
536         .maximized_requested = desktop_maximized_requested,
537         .minimized_requested = desktop_minimized_requested,
538         .set_xwayland_position = desktop_set_xwayland_position,
539 };
540
541 static void
542 ivi_shell_destroy(struct wl_listener *listener, void *data)
543 {
544         struct ivi_compositor *ivi = container_of(listener,
545                                 struct ivi_compositor, destroy_listener);
546
547         ivi_shell_finalize(ivi);
548         ivi_compositor_destroy_pending_surfaces(ivi);
549         ivi_layout_destroy_saved_outputs(ivi);
550
551         weston_desktop_destroy(ivi->desktop);
552         wl_list_remove(&ivi->transform_listener.link);
553         wl_list_remove(&listener->link);
554 }
555
556 static void
557 transform_handler(struct wl_listener *listener, void *data)
558 {
559         struct weston_surface *surface = data;
560         struct ivi_surface *ivisurf = get_ivi_shell_surface(surface);
561         const struct weston_xwayland_surface_api *api;
562         int x, y;
563
564         if (!ivisurf)
565                 return;
566
567         api = ivisurf->ivi->xwayland_surface_api;
568         if (!api) {
569                 api = weston_xwayland_surface_get_api(ivisurf->ivi->compositor);
570                 ivisurf->ivi->xwayland_surface_api = api;
571         }
572
573         if (!api || !api->is_xwayland_surface(surface))
574                 return;
575
576         if (!weston_view_is_mapped(ivisurf->view))
577                 return;
578
579         x = ivisurf->view->geometry.x;
580         y = ivisurf->view->geometry.y;
581
582         api->send_position(surface, x, y);
583 }
584
585 int
586 ivi_desktop_init(struct ivi_compositor *ivi)
587 {
588         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
589         if (!ivi->desktop) {
590                 weston_log("Failed to create desktop globals");
591                 return -1;
592         }
593
594         if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
595                         &ivi->destroy_listener, ivi_shell_destroy)) {
596                 return -1;
597         }
598
599         ivi->transform_listener.notify = transform_handler;
600         wl_signal_add(&ivi->compositor->transform_signal,
601                       &ivi->transform_listener);
602
603
604         return 0;
605 }