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