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