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