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