shell: Refactor pending surfaces for existing pending ones
[src/agl-compositor.git] / src / shell.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 "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <libweston/libweston.h>
38 #include <libweston/config-parser.h>
39
40 #include "shared/os-compatibility.h"
41
42 #include "agl-shell-server-protocol.h"
43 #include "agl-shell-desktop-server-protocol.h"
44
45 #ifdef HAVE_WALTHAM
46 #include <waltham-transmitter/transmitter_api.h>
47 #endif
48
49 static void
50 create_black_surface_view(struct ivi_output *output);
51
52 void
53 agl_shell_desktop_advertise_application_id(struct ivi_compositor *ivi,
54                                            struct ivi_surface *surface)
55 {
56         struct desktop_client *dclient;
57         static bool display_adv = false;
58
59         if (surface->advertised_on_launch)
60                 return;
61
62         /* advertise to all desktop clients the new surface */
63         wl_list_for_each(dclient, &ivi->desktop_clients, link) {
64                 const char *app_id =
65                         weston_desktop_surface_get_app_id(surface->dsurface);
66                 if (app_id == NULL) {
67                         if (!display_adv) {
68                                 weston_log("WARNING app_is is null, unable to advertise\n");
69                                 display_adv = true;
70                         }
71                         return;
72                 }
73                 agl_shell_desktop_send_application(dclient->resource, app_id);
74                 surface->advertised_on_launch = true;
75         }
76 }
77
78 void
79 ivi_set_desktop_surface(struct ivi_surface *surface)
80 {
81         struct ivi_compositor *ivi = surface->ivi;
82         assert(surface->role == IVI_SURFACE_ROLE_NONE);
83
84         surface->role = IVI_SURFACE_ROLE_DESKTOP;
85         wl_list_insert(&surface->ivi->surfaces, &surface->link);
86
87         agl_shell_desktop_advertise_application_id(ivi, surface);
88 }
89
90 static void
91 ivi_set_desktop_surface_popup(struct ivi_surface *surface)
92 {
93         struct ivi_compositor *ivi = surface->ivi;
94         assert(surface->role == IVI_SURFACE_ROLE_NONE);
95
96         surface->role = IVI_SURFACE_ROLE_POPUP;
97         wl_list_insert(&ivi->surfaces, &surface->link);
98
99         agl_shell_desktop_advertise_application_id(ivi, surface);
100 }
101
102 static void
103 ivi_set_desktop_surface_fullscreen(struct ivi_surface *surface)
104 {
105         struct ivi_compositor *ivi = surface->ivi;
106         assert(surface->role == IVI_SURFACE_ROLE_NONE);
107
108         surface->role = IVI_SURFACE_ROLE_FULLSCREEN;
109         wl_list_insert(&ivi->surfaces, &surface->link);
110
111         agl_shell_desktop_advertise_application_id(ivi, surface);
112 }
113
114 #ifdef HAVE_WALTHAM
115 void
116 ivi_destroy_waltham_destroy(struct ivi_surface *surface)
117 {
118         struct ivi_compositor *ivi = surface->ivi;
119         const struct weston_transmitter_api *api =
120                 ivi->waltham_transmitter_api;
121
122         if (!api)
123                 return;
124
125         if (surface->waltham_surface.transmitter_surface)
126                 api->surface_destroy(surface->waltham_surface.transmitter_surface);
127 }
128
129 static void
130 ivi_output_notify_waltham_plugin(struct ivi_surface *surface)
131 {
132         struct ivi_compositor *ivi = surface->ivi;
133         const struct weston_transmitter_api *api = ivi->waltham_transmitter_api;
134         struct weston_transmitter *transmitter;
135         struct weston_transmitter_remote *trans_remote;
136         struct weston_surface *weston_surface;
137         struct weston_output *woutput = surface->remote.output->output;
138         const char *app_id;
139
140         if (!api)
141                 return;
142
143         transmitter = api->transmitter_get(ivi->compositor);
144         if (!transmitter)
145                 return;
146
147         trans_remote = api->get_transmitter_remote(woutput->name, transmitter);
148         if (!trans_remote) {
149                 weston_log("Could not find a valie weston_transmitter_remote "
150                                 "that matches the output %s\n", woutput->name);
151                 return;
152         }
153
154         app_id = weston_desktop_surface_get_app_id(surface->dsurface);
155         weston_surface =
156                 weston_desktop_surface_get_surface(surface->dsurface);
157
158         weston_log("Forwarding app_id %s to remote %s\n", app_id, woutput->name);
159
160         /* this will have the effect of informing the remote side to create a
161          * surface with the name app_id. W/ xdg-shell the following happens:
162          *
163          * compositor (server):
164          * surface_push_to_remote():
165          *      waltham-transmitter plug-in
166          *              -> wthp_ivi_app_id_surface_create()
167          *
168          * client -- on the receiver side:
169          *      -> wthp_ivi_app_id_surface_create()
170          *              -> wth_receiver_weston_main()
171          *                      -> wl_compositor_create_surface()
172          *                      -> xdg_wm_base_get_xdg_surface
173          *                      -> xdg_toplevel_set_app_id()
174          *                      -> gst_init()
175          *                      -> gst_parse_launch()
176          *
177          * wth_receiver_weston_main() will be invoked from the handler of
178          * wthp_ivi_app_id_surface_create() and is responsible for setting-up
179          * the gstreamer pipeline as well.
180          */
181         surface->waltham_surface.transmitter_surface =
182             api->surface_push_to_remote(weston_surface, app_id, trans_remote, NULL);
183 }
184
185 #else
186 void
187 ivi_destroy_waltham_destroy(struct ivi_surface *surface)
188 {
189 }
190 static void
191 ivi_output_notify_waltham_plugin(struct ivi_surface *surface)
192 {
193 }
194 #endif
195
196 static void
197 ivi_set_desktop_surface_remote(struct ivi_surface *surface)
198 {
199         struct ivi_compositor *ivi = surface->ivi;
200         struct weston_view *view;
201         struct ivi_output *output = surface->remote.output;
202
203         assert(surface->role == IVI_SURFACE_ROLE_NONE);
204
205         /* remote type are the same as desktop just that client can tell
206          * the compositor to start on another output */
207         surface->role = IVI_SURFACE_ROLE_REMOTE;
208
209         /* if thew black surface view is mapped on the mean we need
210          * to remove it in order to start showing the 'remote' surface
211          * just being added */
212         view = output->fullscreen_view.fs->view;
213         if (view->is_mapped || view->surface->is_mapped)
214                 remove_black_surface(output);
215
216         if (output->type == OUTPUT_WALTHAM)
217                 ivi_output_notify_waltham_plugin(surface);
218
219         wl_list_insert(&ivi->surfaces, &surface->link);
220 }
221
222
223 static void
224 ivi_set_desktop_surface_split(struct ivi_surface *surface)
225 {
226         struct ivi_compositor *ivi = surface->ivi;
227         assert(surface->role == IVI_SURFACE_ROLE_NONE);
228
229         if (surface->split.orientation == AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL)
230                 surface->role = IVI_SURFACE_ROLE_SPLIT_V;
231         else
232                 surface->role = IVI_SURFACE_ROLE_SPLIT_H;
233
234         wl_list_insert(&ivi->surfaces, &surface->link);
235
236         agl_shell_desktop_advertise_application_id(ivi, surface);
237 }
238
239 static struct pending_popup *
240 ivi_ensure_popup(struct ivi_output *ioutput, int x, int y, int bx, int by,
241                  int width, int height, const char *app_id)
242 {
243         struct pending_popup *p_popup = zalloc(sizeof(*p_popup));
244         size_t len_app_id = strlen(app_id);
245
246         p_popup->app_id = zalloc(sizeof(char) * (len_app_id + 1));
247         memcpy(p_popup->app_id, app_id, len_app_id);
248         p_popup->ioutput = ioutput;
249         p_popup->x = x;
250         p_popup->y = y;
251
252         p_popup->bb.x = bx;
253         p_popup->bb.y = by;
254         p_popup->bb.width = width;
255         p_popup->bb.height = height;
256
257         return p_popup;
258 }
259
260 static void
261 ivi_update_popup(struct ivi_output *ioutput, int x, int y, int bx, int by,
262                  int width, int height, const char *app_id, struct pending_popup *p_popup)
263 {
264         size_t len_app_id = strlen(app_id);
265
266         wl_list_remove(&p_popup->link);
267         wl_list_init(&p_popup->link);
268
269         memset(p_popup->app_id, 0, strlen(app_id) + 1);
270         free(p_popup->app_id);
271
272         p_popup->app_id = zalloc(sizeof(char) * (len_app_id + 1));
273         memcpy(p_popup->app_id, app_id, len_app_id);
274
275         p_popup->ioutput = ioutput;
276         p_popup->x = x;
277         p_popup->y = y;
278
279         p_popup->bb.x = bx;
280         p_popup->bb.y = by;
281         p_popup->bb.width = width;
282         p_popup->bb.height = height;
283 }
284
285 static struct pending_fullscreen *
286 ivi_ensure_fullscreen(struct ivi_output *ioutput, const char *app_id)
287 {
288         struct pending_fullscreen *p_fullscreen = zalloc(sizeof(*p_fullscreen));
289         size_t len_app_id = strlen(app_id);
290
291         p_fullscreen->app_id = zalloc(sizeof(char) * (len_app_id + 1));
292         memcpy(p_fullscreen->app_id, app_id, len_app_id);
293
294         p_fullscreen->ioutput = ioutput;
295         return p_fullscreen;
296 }
297
298 static void
299 ivi_update_fullscreen(struct ivi_output *ioutput, const char *app_id,
300                       struct pending_fullscreen *p_fullscreen)
301 {
302         size_t len_app_id = strlen(app_id);
303
304         wl_list_remove(&p_fullscreen->link);
305         wl_list_init(&p_fullscreen->link);
306
307         memset(p_fullscreen->app_id, 0, strlen(app_id) + 1);
308         free(p_fullscreen->app_id);
309
310         p_fullscreen->app_id = zalloc(sizeof(char) * (len_app_id + 1));
311         memcpy(p_fullscreen->app_id, app_id, len_app_id);
312
313         p_fullscreen->ioutput = ioutput;
314 }
315
316 static struct pending_remote *
317 ivi_ensure_remote(struct ivi_output *ioutput, const char *app_id)
318 {
319         struct pending_remote *p_remote = zalloc(sizeof(*p_remote));
320         size_t len_app_id = strlen(app_id);
321
322         p_remote->app_id = zalloc(sizeof(char) * (len_app_id + 1));
323         memcpy(p_remote->app_id, app_id, len_app_id);
324
325         p_remote->ioutput = ioutput;
326         return p_remote;
327 }
328
329 static void
330 ivi_update_remote(struct ivi_output *ioutput, const char *app_id,
331                       struct pending_remote *p_remote)
332 {
333         size_t len_app_id = strlen(app_id);
334
335         wl_list_remove(&p_remote->link);
336         wl_list_init(&p_remote->link);
337
338         memset(p_remote->app_id, 0, strlen(app_id) + 1);
339         free(p_remote->app_id);
340
341         p_remote->app_id = zalloc(sizeof(char) * (len_app_id + 1));
342         memcpy(p_remote->app_id, app_id, len_app_id);
343
344         p_remote->ioutput = ioutput;
345 }
346
347 static void
348 ivi_set_pending_desktop_surface_popup(struct ivi_output *ioutput, int x, int y, int bx,
349                                       int by, int width, int height, const char *app_id)
350 {
351         struct ivi_compositor *ivi = ioutput->ivi;
352         struct pending_popup *p_popup = NULL;
353         struct pending_popup *popup;
354
355         wl_list_for_each(popup, &ivi->popup_pending_apps, link)
356                 if (!strcmp(app_id, popup->app_id))
357                         p_popup = popup;
358
359         if (!p_popup)
360                 p_popup = ivi_ensure_popup(ioutput, x, y, bx, by, width, height, app_id);
361         else
362                 ivi_update_popup(ioutput, x, y, bx, by, width, height, app_id, p_popup);
363
364         wl_list_insert(&ivi->popup_pending_apps, &p_popup->link);
365 }
366
367 static void
368 ivi_set_pending_desktop_surface_fullscreen(struct ivi_output *ioutput,
369                                            const char *app_id)
370 {
371         struct ivi_compositor *ivi = ioutput->ivi;
372         struct pending_fullscreen *p_fullscreen = NULL;
373         struct pending_fullscreen *fullscreen;
374
375         wl_list_for_each(fullscreen, &ivi->fullscreen_pending_apps, link)
376                 if (!strcmp(app_id, fullscreen->app_id))
377                         p_fullscreen = fullscreen;
378
379         if (!p_fullscreen)
380                 p_fullscreen = ivi_ensure_fullscreen(ioutput, app_id);
381         else
382                 ivi_update_fullscreen(ioutput, app_id, p_fullscreen);
383
384         wl_list_insert(&ivi->fullscreen_pending_apps, &p_fullscreen->link);
385 }
386
387 static void
388 ivi_set_pending_desktop_surface_split(struct ivi_output *ioutput,
389                                       const char *app_id, uint32_t orientation)
390 {
391         struct ivi_compositor *ivi = ioutput->ivi;
392         struct ivi_surface *surf;
393         size_t len_app_id = strlen(app_id);
394         struct pending_split *split;
395
396         if (orientation != AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL &&
397             orientation != AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL)
398                 return;
399
400         /* more than one is un-supported, do note we need to do
401          * conversion for surface roles instead of using the protocol ones */
402         wl_list_for_each(surf, &ivi->surfaces, link)
403                 if (surf->role == IVI_SURFACE_ROLE_SPLIT_V ||
404                     surf->role == IVI_SURFACE_ROLE_SPLIT_H)
405                         return;
406
407         split = zalloc(sizeof(*split));
408         split->app_id = zalloc(sizeof(char) * (len_app_id + 1));
409         memcpy(split->app_id, app_id, len_app_id);
410
411         split->ioutput = ioutput;
412         split->orientation = orientation;
413
414         wl_list_insert(&ivi->split_pending_apps, &split->link);
415 }
416
417 void
418 ivi_set_pending_desktop_surface_remote(struct ivi_output *ioutput,
419                 const char *app_id)
420 {
421         struct ivi_compositor *ivi = ioutput->ivi;
422         struct pending_remote *remote;
423         struct pending_remote *p_remote = NULL;
424
425         wl_list_for_each(remote, &ivi->remote_pending_apps, link)
426                 if (!strcmp(app_id, remote->app_id))
427                         p_remote = remote;
428
429         if (!p_remote)
430                 p_remote = ivi_ensure_remote(ioutput, app_id);
431         else
432                 ivi_update_remote(ioutput, app_id, p_remote);
433
434         wl_list_insert(&ivi->remote_pending_apps, &remote->link);
435 }
436
437
438 static void
439 ivi_remove_pending_desktop_surface_split(struct pending_split *split)
440 {
441         free(split->app_id);
442         wl_list_remove(&split->link);
443         free(split);
444 }
445
446 static void
447 ivi_remove_pending_desktop_surface_fullscreen(struct pending_fullscreen *fs)
448 {
449         free(fs->app_id);
450         wl_list_remove(&fs->link);
451         free(fs);
452 }
453
454 static void
455 ivi_remove_pending_desktop_surface_popup(struct pending_popup *p_popup)
456 {
457         free(p_popup->app_id);
458         wl_list_remove(&p_popup->link);
459         free(p_popup);
460 }
461
462 static void
463 ivi_remove_pending_desktop_surface_remote(struct pending_remote *remote)
464 {
465         free(remote->app_id);
466         wl_list_remove(&remote->link);
467         free(remote);
468 }
469
470 static bool
471 ivi_compositor_keep_pending_surfaces(struct ivi_surface *surface)
472 {
473         return surface->ivi->keep_pending_surfaces;
474 }
475
476 static bool
477 ivi_check_pending_desktop_surface_popup(struct ivi_surface *surface)
478 {
479         struct ivi_compositor *ivi = surface->ivi;
480         struct pending_popup *p_popup, *next_p_popup;
481         const char *_app_id =
482                         weston_desktop_surface_get_app_id(surface->dsurface);
483
484         if (wl_list_empty(&ivi->popup_pending_apps) || !_app_id)
485                 return false;
486
487         wl_list_for_each_safe(p_popup, next_p_popup,
488                               &ivi->popup_pending_apps, link) {
489                 if (!strcmp(_app_id, p_popup->app_id)) {
490                         surface->popup.output = p_popup->ioutput;
491                         surface->popup.x = p_popup->x;
492                         surface->popup.y = p_popup->y;
493
494                         surface->popup.bb.x = p_popup->bb.x;
495                         surface->popup.bb.y = p_popup->bb.y;
496                         surface->popup.bb.width = p_popup->bb.width;
497                         surface->popup.bb.height = p_popup->bb.height;
498
499                         if (!ivi_compositor_keep_pending_surfaces(surface))
500                                 ivi_remove_pending_desktop_surface_popup(p_popup);
501                         return true;
502                 }
503         }
504
505         return false;
506 }
507
508 static bool
509 ivi_check_pending_desktop_surface_split(struct ivi_surface *surface)
510 {
511         struct pending_split *split_surf, *next_split_surf;
512         struct ivi_compositor *ivi = surface->ivi;
513         const char *_app_id =
514                         weston_desktop_surface_get_app_id(surface->dsurface);
515
516         if (wl_list_empty(&ivi->split_pending_apps) || !_app_id)
517                 return false;
518
519         wl_list_for_each_safe(split_surf, next_split_surf,
520                               &ivi->split_pending_apps, link) {
521                 if (!strcmp(_app_id, split_surf->app_id)) {
522                         surface->split.output = split_surf->ioutput;
523                         surface->split.orientation = split_surf->orientation;
524                         if (!ivi_compositor_keep_pending_surfaces(surface))
525                                 ivi_remove_pending_desktop_surface_split(split_surf);
526                         return true;
527                 }
528         }
529
530         return false;
531 }
532
533 static bool
534 ivi_check_pending_desktop_surface_fullscreen(struct ivi_surface *surface)
535 {
536         struct pending_fullscreen *fs_surf, *next_fs_surf;
537         struct ivi_compositor *ivi = surface->ivi;
538         const char *_app_id =
539                         weston_desktop_surface_get_app_id(surface->dsurface);
540
541         if (wl_list_empty(&ivi->fullscreen_pending_apps) || !_app_id)
542                 return false;
543
544         wl_list_for_each_safe(fs_surf, next_fs_surf,
545                               &ivi->fullscreen_pending_apps, link) {
546                 if (!strcmp(_app_id, fs_surf->app_id)) {
547                         surface->fullscreen.output = fs_surf->ioutput;
548                         if (!ivi_compositor_keep_pending_surfaces(surface))
549                                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
550                         return true;
551                 }
552         }
553
554         return false;
555 }
556
557 static bool
558 ivi_check_pending_desktop_surface_remote(struct ivi_surface *surface)
559 {
560         struct pending_remote *remote_surf, *next_remote_surf;
561         struct ivi_compositor *ivi = surface->ivi;
562         const char *_app_id =
563                 weston_desktop_surface_get_app_id(surface->dsurface);
564
565         if (wl_list_empty(&ivi->remote_pending_apps) || !_app_id)
566                 return false;
567
568         wl_list_for_each_safe(remote_surf, next_remote_surf,
569                               &ivi->remote_pending_apps, link) {
570                 if (!strcmp(_app_id, remote_surf->app_id)) {
571                         surface->remote.output = remote_surf->ioutput;
572                         if (!ivi_compositor_keep_pending_surfaces(surface))
573                                 ivi_remove_pending_desktop_surface_remote(remote_surf);
574                         return true;
575                 }
576         }
577
578         return false;
579 }
580
581 bool
582 ivi_check_pending_surface(struct ivi_surface *surface)
583 {
584         struct ivi_compositor *ivi = surface->ivi;
585         struct wl_list *role_pending_list;
586         struct pending_popup *p_popup;
587         struct pending_split *p_split;
588         struct pending_fullscreen *p_fullscreen;
589         struct pending_remote *p_remote;
590         const char *app_id =
591                 weston_desktop_surface_get_app_id(surface->dsurface);
592
593         if (!app_id)
594                 return false;
595
596         role_pending_list = &ivi->popup_pending_apps;
597         wl_list_for_each(p_popup, role_pending_list, link) {
598                 if (!strcmp(app_id, p_popup->app_id)) {
599                         return true;
600                 }
601         }
602
603         role_pending_list = &ivi->split_pending_apps;
604         wl_list_for_each(p_split, role_pending_list, link) {
605                 if (!strcmp(app_id, p_split->app_id)) {
606                         return true;
607                 }
608         }
609
610         role_pending_list = &ivi->fullscreen_pending_apps;
611         wl_list_for_each(p_fullscreen, role_pending_list, link) {
612                 if (!strcmp(app_id, p_fullscreen->app_id)) {
613                         return true;
614                 }
615         }
616
617         role_pending_list = &ivi->remote_pending_apps;
618         wl_list_for_each(p_remote, role_pending_list, link) {
619                 if (!strcmp(app_id, p_remote->app_id)) {
620                         return true;
621                 }
622         }
623
624         /* else, we are a regular desktop surface */
625         return false;
626 }
627
628
629 void
630 ivi_check_pending_desktop_surface(struct ivi_surface *surface)
631 {
632         bool ret = false;
633
634         ret = ivi_check_pending_desktop_surface_popup(surface);
635         if (ret) {
636                 ivi_set_desktop_surface_popup(surface);
637                 ivi_layout_popup_committed(surface);
638                 return;
639         }
640
641         ret = ivi_check_pending_desktop_surface_split(surface);
642         if (ret) {
643                 ivi_set_desktop_surface_split(surface);
644                 ivi_layout_split_committed(surface);
645                 return;
646         }
647
648         ret = ivi_check_pending_desktop_surface_fullscreen(surface);
649         if (ret) {
650                 ivi_set_desktop_surface_fullscreen(surface);
651                 ivi_layout_fullscreen_committed(surface);
652                 return;
653         }
654
655         ret = ivi_check_pending_desktop_surface_remote(surface);
656         if (ret) {
657                 ivi_set_desktop_surface_remote(surface);
658                 ivi_layout_desktop_committed(surface);
659                 return;
660         }
661
662         /* if we end up here means we have a regular desktop app and
663          * try to activate it */
664         ivi_set_desktop_surface(surface);
665         ivi_layout_desktop_committed(surface);
666 }
667
668 void
669 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
670 {
671         struct ivi_output *out;
672
673         wl_list_for_each(out, &ivi->outputs, link) {
674                 create_black_surface_view(out);
675                 insert_black_surface(out);
676         }
677 }
678
679 int
680 ivi_shell_init(struct ivi_compositor *ivi)
681 {
682         weston_layer_init(&ivi->hidden, ivi->compositor);
683         weston_layer_init(&ivi->background, ivi->compositor);
684         weston_layer_init(&ivi->normal, ivi->compositor);
685         weston_layer_init(&ivi->panel, ivi->compositor);
686         weston_layer_init(&ivi->popup, ivi->compositor);
687         weston_layer_init(&ivi->fullscreen, ivi->compositor);
688
689         weston_layer_set_position(&ivi->hidden,
690                                   WESTON_LAYER_POSITION_HIDDEN);
691         weston_layer_set_position(&ivi->background,
692                                   WESTON_LAYER_POSITION_BACKGROUND);
693         weston_layer_set_position(&ivi->normal,
694                                   WESTON_LAYER_POSITION_NORMAL);
695         weston_layer_set_position(&ivi->panel,
696                                   WESTON_LAYER_POSITION_UI);
697         weston_layer_set_position(&ivi->popup,
698                                   WESTON_LAYER_POSITION_TOP_UI);
699         weston_layer_set_position(&ivi->fullscreen,
700                                   WESTON_LAYER_POSITION_FULLSCREEN);
701
702         return 0;
703 }
704
705 static void
706 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
707 {
708         struct ivi_surface *surface;
709
710         wl_list_for_each(surface, &ivi->surfaces, link) {
711                 const char *app_id =
712                         weston_desktop_surface_get_app_id(surface->dsurface);
713                 if (app_id == NULL) {
714                         weston_log("WARNING app_is is null, unable to advertise\n");
715                         return;
716                 }
717                 agl_shell_desktop_send_application(resource, app_id);
718         }
719 }
720
721 static void
722 client_exec(const char *command, int fd)
723 {
724         sigset_t sig;
725         char s[32];
726
727         /* Don't give the child our signal mask */
728         sigfillset(&sig);
729         sigprocmask(SIG_UNBLOCK, &sig, NULL);
730
731         /* Launch clients as the user; don't give them the wrong euid */
732         if (seteuid(getuid()) == -1) {
733                 weston_log("seteuid failed: %s\n", strerror(errno));
734                 return;
735         }
736
737         /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
738          * clobbering fd, as we'll exit/exec either way.
739          */
740         fd = dup(fd);
741         if (fd == -1) {
742                 weston_log("dup failed: %s\n", strerror(errno));
743                 return;
744         }
745
746         snprintf(s, sizeof s, "%d", fd);
747         setenv("WAYLAND_SOCKET", s, 1);
748
749         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
750         weston_log("executing '%s' failed: %s", command, strerror(errno));
751 }
752
753 static struct wl_client *
754 launch_shell_client(struct ivi_compositor *ivi, const char *command)
755 {
756         struct wl_client *client;
757         int sock[2];
758         pid_t pid;
759
760         weston_log("launching' %s'\n", command);
761
762         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
763                 weston_log("socketpair failed while launching '%s': %s\n",
764                            command, strerror(errno));
765                 return NULL;
766         }
767
768         pid = fork();
769         if (pid == -1) {
770                 close(sock[0]);
771                 close(sock[1]);
772                 weston_log("fork failed while launching '%s': %s\n",
773                            command, strerror(errno));
774                 return NULL;
775         }
776
777         if (pid == 0) {
778                 client_exec(command, sock[1]);
779                 _Exit(EXIT_FAILURE);
780         }
781         close(sock[1]);
782
783         client = wl_client_create(ivi->compositor->wl_display, sock[0]);
784         if (!client) {
785                 close(sock[0]);
786                 weston_log("Failed to create wayland client for '%s'",
787                            command);
788                 return NULL;
789         }
790
791         return client;
792 }
793
794 int
795 ivi_launch_shell_client(struct ivi_compositor *ivi)
796 {
797         struct weston_config_section *section;
798         char *command = NULL;
799
800         section = weston_config_get_section(ivi->config, "shell-client",
801                                             NULL, NULL);
802         if (section)
803                 weston_config_section_get_string(section, "command",
804                                                  &command, NULL);
805
806         if (!command)
807                 return -1;
808
809         ivi->shell_client.client = launch_shell_client(ivi, command);
810         if (!ivi->shell_client.client)
811                 return -1;
812
813         return 0;
814 }
815
816 static void
817 destroy_black_view(struct wl_listener *listener, void *data)
818 {
819         struct fullscreen_view *fs =
820                 wl_container_of(listener, fs, fs_destroy);
821
822
823         if (fs && fs->fs) {
824                 wl_list_remove(&fs->fs_destroy.link);
825                 free(fs->fs);
826         }
827 }
828
829
830 static void
831 create_black_surface_view(struct ivi_output *output)
832 {
833         struct weston_surface *surface = NULL;
834         struct weston_view *view;
835         struct ivi_compositor *ivi = output->ivi;
836         struct weston_compositor *wc= ivi->compositor;
837         struct weston_output *woutput = output->output;
838
839         if (!woutput)
840                 return;
841
842         surface = weston_surface_create(wc);
843         view = weston_view_create(surface);
844
845         assert(view || surface);
846
847         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
848         weston_surface_set_size(surface, woutput->width, woutput->height);
849         weston_view_set_position(view, woutput->x, woutput->y);
850
851         output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
852         output->fullscreen_view.fs->view = view;
853
854         output->fullscreen_view.fs_destroy.notify = destroy_black_view;
855         wl_signal_add(&woutput->destroy_signal,
856                       &output->fullscreen_view.fs_destroy);
857 }
858
859 void
860 remove_black_surface(struct ivi_output *output)
861 {
862         struct weston_view *view;
863
864         if (!output &&
865             !output->fullscreen_view.fs &&
866             !output->fullscreen_view.fs->view) {
867                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
868                 return;
869         }
870
871         view = output->fullscreen_view.fs->view;
872         assert(view->is_mapped == true ||
873                view->surface->is_mapped == true);
874
875         view->is_mapped = false;
876         view->surface->is_mapped = false;
877
878         weston_layer_entry_remove(&view->layer_link);
879         weston_view_update_transform(view);
880
881         weston_view_damage_below(view);
882 }
883
884 void
885 insert_black_surface(struct ivi_output *output)
886 {
887         struct weston_view *view;
888
889         if ((!output &&
890             !output->fullscreen_view.fs &&
891             !output->fullscreen_view.fs->view) || !output->output) {
892                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
893                 return;
894         }
895
896         view = output->fullscreen_view.fs->view;
897         if (view->is_mapped || view->surface->is_mapped)
898                 return;
899
900         weston_layer_entry_remove(&view->layer_link);
901         weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
902                                   &view->layer_link);
903
904         view->is_mapped = true;
905         view->surface->is_mapped = true;
906
907         weston_view_update_transform(view);
908         weston_view_damage_below(view);
909 }
910
911 static void
912 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
913 {
914         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
915         struct ivi_output *output;
916         struct ivi_surface *surface, *tmp;
917
918         /* Init already finished. Do nothing */
919         if (ivi->shell_client.ready)
920                 return;
921
922         ivi->shell_client.ready = true;
923
924         wl_list_for_each(output, &ivi->outputs, link) {
925                 if (output->background)
926                         remove_black_surface(output);
927                 ivi_layout_init(ivi, output);
928         }
929
930         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
931                 wl_list_remove(&surface->link);
932                 wl_list_init(&surface->link);
933                 ivi_check_pending_desktop_surface(surface);
934                 surface->checked_pending = true;
935         }
936 }
937
938 static void
939 shell_set_background(struct wl_client *client,
940                      struct wl_resource *shell_res,
941                      struct wl_resource *surface_res,
942                      struct wl_resource *output_res)
943 {
944         struct weston_head *head = weston_head_from_resource(output_res);
945         struct weston_output *woutput = weston_head_get_output(head);
946         struct ivi_output *output = to_ivi_output(woutput);
947         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
948         struct weston_desktop_surface *dsurface;
949         struct ivi_surface *surface;
950
951         dsurface = weston_surface_get_desktop_surface(wsurface);
952         if (!dsurface) {
953                 wl_resource_post_error(shell_res,
954                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
955                                        "surface must be a desktop surface");
956                 return;
957         }
958
959         surface = weston_desktop_surface_get_user_data(dsurface);
960         if (surface->role != IVI_SURFACE_ROLE_NONE) {
961                 wl_resource_post_error(shell_res,
962                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
963                                        "surface already has another ivi role");
964                 return;
965         }
966
967         if (output->background) {
968                 wl_resource_post_error(shell_res,
969                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
970                                        "output already has background");
971                 return;
972         }
973
974         surface->checked_pending = true;
975         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
976         surface->bg.output = output;
977         wl_list_remove(&surface->link);
978         wl_list_init(&surface->link);
979
980         output->background = surface;
981
982         weston_desktop_surface_set_maximized(dsurface, true);
983         weston_desktop_surface_set_size(dsurface,
984                                         output->output->width,
985                                         output->output->height);
986 }
987
988 static void
989 shell_set_panel(struct wl_client *client,
990                 struct wl_resource *shell_res,
991                 struct wl_resource *surface_res,
992                 struct wl_resource *output_res,
993                 uint32_t edge)
994 {
995         struct weston_head *head = weston_head_from_resource(output_res);
996         struct weston_output *woutput = weston_head_get_output(head);
997         struct ivi_output *output = to_ivi_output(woutput);
998         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
999         struct weston_desktop_surface *dsurface;
1000         struct ivi_surface *surface;
1001         struct ivi_surface **member;
1002         int32_t width = 0, height = 0;
1003
1004         dsurface = weston_surface_get_desktop_surface(wsurface);
1005         if (!dsurface) {
1006                 wl_resource_post_error(shell_res,
1007                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1008                                        "surface must be a desktop surface");
1009                 return;
1010         }
1011
1012         surface = weston_desktop_surface_get_user_data(dsurface);
1013         if (surface->role != IVI_SURFACE_ROLE_NONE) {
1014                 wl_resource_post_error(shell_res,
1015                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1016                                        "surface already has another ivi role");
1017                 return;
1018         }
1019
1020         switch (edge) {
1021         case AGL_SHELL_EDGE_TOP:
1022                 member = &output->top;
1023                 break;
1024         case AGL_SHELL_EDGE_BOTTOM:
1025                 member = &output->bottom;
1026                 break;
1027         case AGL_SHELL_EDGE_LEFT:
1028                 member = &output->left;
1029                 break;
1030         case AGL_SHELL_EDGE_RIGHT:
1031                 member = &output->right;
1032                 break;
1033         default:
1034                 wl_resource_post_error(shell_res,
1035                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1036                                        "invalid edge for panel");
1037                 return;
1038         }
1039
1040         if (*member) {
1041                 wl_resource_post_error(shell_res,
1042                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
1043                                        "output already has panel on this edge");
1044                 return;
1045         }
1046
1047         surface->checked_pending = true;
1048         surface->role = IVI_SURFACE_ROLE_PANEL;
1049         surface->panel.output = output;
1050         surface->panel.edge = edge;
1051         wl_list_remove(&surface->link);
1052         wl_list_init(&surface->link);
1053
1054         *member = surface;
1055
1056         switch (surface->panel.edge) {
1057         case AGL_SHELL_EDGE_TOP:
1058         case AGL_SHELL_EDGE_BOTTOM:
1059                 width = woutput->width;
1060                 break;
1061         case AGL_SHELL_EDGE_LEFT:
1062         case AGL_SHELL_EDGE_RIGHT:
1063                 height = woutput->height;
1064                 break;
1065         }
1066
1067         weston_desktop_surface_set_size(dsurface, width, height);
1068 }
1069
1070 void
1071 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
1072                           const char *data, uint32_t app_state)
1073 {
1074         struct desktop_client *dclient;
1075         uint32_t app_role;
1076         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1077         struct ivi_policy *policy = ivi->policy;
1078
1079         /* FIXME: should queue it here and see when binding agl-shell-desktop
1080          * if there are any to be sent */
1081         if (!surf)
1082                 return;
1083
1084         if (!app_id)
1085                 return;
1086
1087         if (policy && policy->api.surface_advertise_state_change &&
1088             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
1089                 return;
1090         }
1091
1092         app_role = surf->role;
1093         if (app_role == IVI_SURFACE_ROLE_POPUP)
1094                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
1095
1096         wl_list_for_each(dclient, &ivi->desktop_clients, link)
1097                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
1098                                                  data, app_state, app_role);
1099 }
1100
1101 static void
1102 shell_activate_app(struct wl_client *client,
1103                    struct wl_resource *shell_res,
1104                    const char *app_id,
1105                    struct wl_resource *output_res)
1106 {
1107         struct weston_head *head = weston_head_from_resource(output_res);
1108         struct weston_output *woutput = weston_head_get_output(head);
1109         struct ivi_output *output = to_ivi_output(woutput);
1110
1111         ivi_layout_activate(output, app_id);
1112 }
1113
1114 static void
1115 shell_desktop_activate_app(struct wl_client *client,
1116                            struct wl_resource *shell_res,
1117                            const char *app_id, const char *data,
1118                            struct wl_resource *output_res)
1119 {
1120         struct weston_head *head = weston_head_from_resource(output_res);
1121         struct weston_output *woutput = weston_head_get_output(head);
1122         struct ivi_output *output = to_ivi_output(woutput);
1123
1124         ivi_layout_activate(output, app_id);
1125         shell_advertise_app_state(output->ivi, app_id,
1126                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
1127 }
1128
1129 static void
1130 shell_deactivate_app(struct wl_client *client,
1131                    struct wl_resource *shell_res,
1132                    const char *app_id)
1133 {
1134         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1135         struct ivi_compositor *ivi = dclient->ivi;
1136
1137         ivi_layout_deactivate(ivi, app_id);
1138         shell_advertise_app_state(ivi, app_id,
1139                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
1140 }
1141
1142 static const struct agl_shell_interface agl_shell_implementation = {
1143         .ready = shell_ready,
1144         .set_background = shell_set_background,
1145         .set_panel = shell_set_panel,
1146         .activate_app = shell_activate_app,
1147 };
1148
1149 static void
1150 shell_desktop_set_app_property(struct wl_client *client,
1151                                struct wl_resource *shell_res,
1152                                const char *app_id, uint32_t role,
1153                                int x, int y, int bx, int by,
1154                                int width, int height,
1155                                struct wl_resource *output_res)
1156 {
1157         struct weston_head *head = weston_head_from_resource(output_res);
1158         struct weston_output *woutput = weston_head_get_output(head);
1159         struct ivi_output *output = to_ivi_output(woutput);
1160
1161         switch (role) {
1162         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
1163                 ivi_set_pending_desktop_surface_popup(output, x, y, bx, by,
1164                                                       width, height, app_id);
1165                 break;
1166         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
1167                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
1168                 break;
1169         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
1170         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
1171                 ivi_set_pending_desktop_surface_split(output, app_id, role);
1172                 break;
1173         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
1174                 ivi_set_pending_desktop_surface_remote(output, app_id);
1175                 break;
1176         default:
1177                 break;
1178         }
1179 }
1180
1181 void
1182 ivi_compositor_destroy_pending_surfaces(struct ivi_compositor *ivi)
1183 {
1184         struct pending_popup *p_popup, *next_p_popup;
1185         struct pending_split *split_surf, *next_split_surf;
1186         struct pending_fullscreen *fs_surf, *next_fs_surf;
1187         struct pending_remote *remote_surf, *next_remote_surf;
1188
1189         wl_list_for_each_safe(p_popup, next_p_popup,
1190                               &ivi->popup_pending_apps, link)
1191                 ivi_remove_pending_desktop_surface_popup(p_popup);
1192
1193         wl_list_for_each_safe(split_surf, next_split_surf,
1194                               &ivi->split_pending_apps, link)
1195                 ivi_remove_pending_desktop_surface_split(split_surf);
1196
1197         wl_list_for_each_safe(fs_surf, next_fs_surf,
1198                               &ivi->fullscreen_pending_apps, link)
1199                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
1200
1201         wl_list_for_each_safe(remote_surf, next_remote_surf,
1202                               &ivi->remote_pending_apps, link)
1203                 ivi_remove_pending_desktop_surface_remote(remote_surf);
1204 }
1205
1206 static void
1207 shell_desktop_set_app_property_mode(struct wl_client *client,
1208                                     struct wl_resource *shell_res, uint32_t perm)
1209 {
1210         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1211         if (perm) {
1212                 dclient->ivi->keep_pending_surfaces = true;
1213         } else {
1214                 dclient->ivi->keep_pending_surfaces = false;
1215                 /* remove any previous pending surfaces */
1216                 ivi_compositor_destroy_pending_surfaces(dclient->ivi);
1217         }
1218 }
1219
1220 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
1221         .activate_app = shell_desktop_activate_app,
1222         .set_app_property = shell_desktop_set_app_property,
1223         .deactivate_app = shell_deactivate_app,
1224         .set_app_property_mode = shell_desktop_set_app_property_mode,
1225 };
1226
1227 static void
1228 unbind_agl_shell(struct wl_resource *resource)
1229 {
1230         struct ivi_compositor *ivi;
1231         struct ivi_output *output;
1232         struct ivi_surface *surf, *surf_tmp;
1233
1234         ivi = wl_resource_get_user_data(resource);
1235         wl_list_for_each(output, &ivi->outputs, link) {
1236                 /* reset the active surf if there's one present */
1237                 if (output->active) {
1238                         output->active->view->is_mapped = false;
1239                         output->active->view->surface->is_mapped = false;
1240
1241                         weston_layer_entry_remove(&output->active->view->layer_link);
1242                         output->active = NULL;
1243                 }
1244
1245                 insert_black_surface(output);
1246         }
1247
1248         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
1249                 wl_list_remove(&surf->link);
1250                 wl_list_init(&surf->link);
1251         }
1252
1253         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
1254                 wl_list_remove(&surf->link);
1255                 wl_list_init(&surf->link);
1256         }
1257
1258         wl_list_init(&ivi->surfaces);
1259         wl_list_init(&ivi->pending_surfaces);
1260
1261         ivi->shell_client.ready = false;
1262         ivi->shell_client.resource = NULL;
1263         ivi->shell_client.client = NULL;
1264 }
1265
1266 static void
1267 bind_agl_shell(struct wl_client *client,
1268                void *data, uint32_t version, uint32_t id)
1269 {
1270         struct ivi_compositor *ivi = data;
1271         struct wl_resource *resource;
1272         struct ivi_policy *policy;
1273         void *interface;
1274
1275         policy = ivi->policy;
1276         interface = (void *) &agl_shell_interface;
1277         if (policy && policy->api.shell_bind_interface &&
1278             !policy->api.shell_bind_interface(client, interface)) {
1279                 wl_client_post_implementation_error(client,
1280                                        "client not authorized to use agl_shell");
1281                 return;
1282         }
1283
1284         resource = wl_resource_create(client, &agl_shell_interface,
1285                                       1, id);
1286         if (!resource) {
1287                 wl_client_post_no_memory(client);
1288                 return;
1289         }
1290
1291 #if 0
1292         if (ivi->shell_client.client != client) {
1293                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1294                                        "client not authorized to use agl_shell");
1295                 return;
1296         }
1297 #endif
1298
1299         if (ivi->shell_client.resource) {
1300                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1301                                        "agl_shell has already been bound");
1302                 return;
1303         }
1304
1305         wl_resource_set_implementation(resource, &agl_shell_implementation,
1306                                        ivi, unbind_agl_shell);
1307         ivi->shell_client.resource = resource;
1308 }
1309
1310 static void
1311 unbind_agl_shell_desktop(struct wl_resource *resource)
1312 {
1313         struct desktop_client *dclient = wl_resource_get_user_data(resource);
1314
1315         wl_list_remove(&dclient->link);
1316         free(dclient);
1317 }
1318
1319 static void
1320 bind_agl_shell_desktop(struct wl_client *client,
1321                        void *data, uint32_t version, uint32_t id)
1322 {
1323         struct ivi_compositor *ivi = data;
1324         struct wl_resource *resource;
1325         struct ivi_policy *policy;
1326         struct desktop_client *dclient;
1327         void *interface;
1328
1329         policy = ivi->policy;
1330         interface  = (void *) &agl_shell_desktop_interface;
1331         if (policy && policy->api.shell_bind_interface &&
1332             !policy->api.shell_bind_interface(client, interface)) {
1333                 wl_client_post_implementation_error(client,
1334                                 "client not authorized to use agl_shell_desktop");
1335                 return;
1336         }
1337
1338         dclient = zalloc(sizeof(*dclient));
1339         if (!dclient) {
1340                 wl_client_post_no_memory(client);
1341                 return;
1342         }
1343
1344         resource = wl_resource_create(client, &agl_shell_desktop_interface,
1345                                       version, id);
1346         dclient->ivi = ivi;
1347         if (!resource) {
1348                 wl_client_post_no_memory(client);
1349                 return;
1350         }
1351
1352         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
1353                                        dclient, unbind_agl_shell_desktop);
1354
1355         dclient->resource = resource;
1356         wl_list_insert(&ivi->desktop_clients, &dclient->link);
1357
1358         /* advertise xdg surfaces */
1359         ivi_shell_advertise_xdg_surfaces(ivi, resource);
1360 }
1361
1362 int
1363 ivi_shell_create_global(struct ivi_compositor *ivi)
1364 {
1365         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
1366                                           &agl_shell_interface, 1,
1367                                           ivi, bind_agl_shell);
1368         if (!ivi->agl_shell) {
1369                 weston_log("Failed to create wayland global.\n");
1370                 return -1;
1371         }
1372
1373         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
1374                                                   &agl_shell_desktop_interface, 2,
1375                                                   ivi, bind_agl_shell_desktop);
1376         if (!ivi->agl_shell_desktop) {
1377                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
1378                 return -1;
1379         }
1380
1381         return 0;
1382 }