Init Xwayland
[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         if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
330             output->type == OUTPUT_REMOTE)
331                 ivi_destroy_waltham_destroy(surface);
332
333         /* check if there's a last 'remote' surface and insert a black
334          * surface view if there's no background set for that output
335          */
336         if (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
337             ivi_surface_count_one(output, IVI_SURFACE_ROLE_DESKTOP))
338                 if (!output->background)
339                         insert_black_curtain(output);
340
341
342         if (weston_surface_is_mapped(wsurface)) {
343                 weston_desktop_surface_unlink_view(surface->view);
344                 weston_view_destroy(surface->view);
345         }
346
347         /* invalidate agl-shell surfaces so we can re-use them when
348          * binding again */
349         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
350                 switch (surface->panel.edge) {
351                 case AGL_SHELL_EDGE_TOP:
352                         output->top = NULL;
353                         break;
354                 case AGL_SHELL_EDGE_BOTTOM:
355                         output->bottom = NULL;
356                         break;
357                 case AGL_SHELL_EDGE_LEFT:
358                         output->left = NULL;
359                         break;
360                 case AGL_SHELL_EDGE_RIGHT:
361                         output->right = NULL;
362                         break;
363                 default:
364                         assert(!"Invalid edge detected\n");
365                 }
366         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
367                 output->background = NULL;
368         }
369
370 skip_output_asignment:
371         weston_log("Removed surface %p, app_id %s, role %s\n", surface,
372                         app_id, ivi_layout_get_surface_role_name(surface));
373
374         if (app_id && output && output->output) {
375                 shell_advertise_app_state(output->ivi, app_id,
376                                           NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
377                 if (output->ivi->shell_client.ready)
378                         shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
379         }
380
381         wl_list_remove(&surface->link);
382
383         free(surface);
384 }
385
386 static void
387 desktop_committed(struct weston_desktop_surface *dsurface, 
388                   int32_t sx, int32_t sy, void *userdata)
389 {
390         struct ivi_compositor *ivi = userdata;
391         struct ivi_surface *surface =
392                 weston_desktop_surface_get_user_data(dsurface);
393         struct ivi_policy *policy = surface->ivi->policy;
394
395         if (policy && policy->api.surface_commited &&
396             !policy->api.surface_commited(surface, surface->ivi))
397                 return;
398
399         if (ivi->shell_client.ready && !surface->checked_pending) {
400                 struct ivi_output *remote_output = NULL;
401                 const char *app_id =    weston_desktop_surface_get_app_id(dsurface);
402                 weston_log("Checking pending surface %p, app_id %s\n", surface,
403                         app_id);
404                 wl_list_remove(&surface->link);
405                 wl_list_init(&surface->link);
406
407                 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
408                         ivi_set_pending_desktop_surface_remote(remote_output, app_id);
409                         shell_send_app_on_output(ivi, app_id, remote_output->output->name);
410                 }
411
412
413                 ivi_check_pending_desktop_surface(surface);
414                 surface->checked_pending = true;
415
416                 /* we'll do it now at commit time, because we might not have an
417                  * appid by the time we've created the weston_desktop_surface
418                  * */
419                 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
420         }
421
422         if (!surface->advertised_on_launch &&
423             !wl_list_empty(&surface->ivi->desktop_clients))
424                 wl_signal_emit(&surface->signal_advertise_app, surface);
425
426         /* this repaint schedule is needed to allow resizing to work with the
427          * help of the hidden layer:
428          *
429          * 1. add the view in the hidden layer and send out correct dimensions
430          * 2. clients changes its dimensions
431          * 3. client commits with the new dimensions
432          *
433          * For desktop and fullscreen, desktop_surface_added() sends the
434          * dimensions from the beginning so applications no need to resize, but
435          * if that weren't the case we still need this in.
436          */
437         weston_compositor_schedule_repaint(surface->ivi->compositor);
438
439         switch (surface->role) {
440         case IVI_SURFACE_ROLE_DESKTOP:
441                 ivi_layout_desktop_committed(surface);
442                 break;
443         case IVI_SURFACE_ROLE_REMOTE:
444                 ivi_layout_remote_committed(surface);
445                 break;
446         case IVI_SURFACE_ROLE_POPUP:
447                 ivi_layout_popup_committed(surface);
448                 break;
449         case IVI_SURFACE_ROLE_FULLSCREEN:
450                 ivi_layout_fullscreen_committed(surface);
451                 break;
452         case IVI_SURFACE_ROLE_SPLIT_H:
453         case IVI_SURFACE_ROLE_SPLIT_V:
454                 ivi_layout_split_committed(surface);
455                 break;
456         case IVI_SURFACE_ROLE_NONE:
457         case IVI_SURFACE_ROLE_BACKGROUND:
458         case IVI_SURFACE_ROLE_PANEL:
459         default: /* fall through */
460                 break;
461         }
462 }
463
464 static void
465 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
466                          struct weston_seat *seat, int32_t x, int32_t y,
467                          void *userdata)
468 {
469         /* not supported */
470 }
471
472 static void
473 desktop_set_parent(struct weston_desktop_surface *dsurface,
474                    struct weston_desktop_surface *parent, void *userdata)
475 {
476         /* not supported */
477 }
478
479 static void
480 desktop_move(struct weston_desktop_surface *dsurface,
481              struct weston_seat *seat, uint32_t serial, void *userdata)
482 {
483         /* not supported */
484 }
485
486 static void
487 desktop_resize(struct weston_desktop_surface *dsurface,
488                struct weston_seat *seat, uint32_t serial,
489                enum weston_desktop_surface_edge edges, void *user_data)
490 {
491         /* not supported */
492 }
493
494 static void
495 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
496                              bool fullscreen, struct weston_output *output,
497                              void *userdata)
498 {
499         /* not supported */
500 }
501
502 static void
503 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
504                             bool maximized, void *userdata)
505 {
506         /* not supported */
507 }
508
509 static void
510 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
511                             void *userdata)
512 {
513         /* not supported */
514 }
515
516 static void
517 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
518                               int32_t x, int32_t y, void *userdata)
519 {
520         struct ivi_surface *ivisurf =
521                 weston_desktop_surface_get_user_data(dsurface);
522
523         ivisurf->xwayland.x = x;
524         ivisurf->xwayland.y = y;
525         ivisurf->xwayland.is_set = true;
526 }
527
528 static const struct weston_desktop_api desktop_api = {
529         .struct_size = sizeof desktop_api,
530         .ping_timeout = desktop_ping_timeout,
531         .pong = desktop_pong,
532         .surface_added = desktop_surface_added,
533         .surface_removed = desktop_surface_removed,
534         .committed = desktop_committed,
535         .show_window_menu = desktop_show_window_menu,
536         .set_parent = desktop_set_parent,
537         .move = desktop_move,
538         .resize = desktop_resize,
539         .fullscreen_requested = desktop_fullscreen_requested,
540         .maximized_requested = desktop_maximized_requested,
541         .minimized_requested = desktop_minimized_requested,
542         .set_xwayland_position = desktop_set_xwayland_position,
543 };
544
545 static void
546 ivi_shell_destroy(struct wl_listener *listener, void *data)
547 {
548         struct ivi_compositor *ivi = container_of(listener,
549                                 struct ivi_compositor, destroy_listener);
550
551         ivi_shell_finalize(ivi);
552         ivi_compositor_destroy_pending_surfaces(ivi);
553         ivi_layout_destroy_saved_outputs(ivi);
554
555         weston_desktop_destroy(ivi->desktop);
556         wl_list_remove(&ivi->transform_listener.link);
557         wl_list_remove(&listener->link);
558 }
559
560 static void
561 transform_handler(struct wl_listener *listener, void *data)
562 {
563         struct weston_surface *surface = data;
564         struct ivi_surface *ivisurf = get_ivi_shell_surface(surface);
565         const struct weston_xwayland_surface_api *api;
566         int x, y;
567
568         if (!ivisurf)
569                 return;
570
571         api = ivisurf->ivi->xwayland_surface_api;
572         if (!api) {
573                 api = weston_xwayland_surface_get_api(ivisurf->ivi->compositor);
574                 ivisurf->ivi->xwayland_surface_api = api;
575         }
576
577         if (!api || !api->is_xwayland_surface(surface))
578                 return;
579
580         if (!weston_view_is_mapped(ivisurf->view))
581                 return;
582
583         x = ivisurf->view->geometry.x;
584         y = ivisurf->view->geometry.y;
585
586         api->send_position(surface, x, y);
587 }
588
589 int
590 ivi_desktop_init(struct ivi_compositor *ivi)
591 {
592         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
593         if (!ivi->desktop) {
594                 weston_log("Failed to create desktop globals");
595                 return -1;
596         }
597
598         if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
599                         &ivi->destroy_listener, ivi_shell_destroy)) {
600                 return -1;
601         }
602
603         ivi->transform_listener.notify = transform_handler;
604         wl_signal_add(&ivi->compositor->transform_signal,
605                       &ivi->transform_listener);
606
607
608         return 0;
609 }