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