src: Send out the output name for the remote role
[src/agl-compositor.git] / src / shell.c
1 /*
2  * Copyright © 2019, 2022 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 <sys/wait.h>
37 #include <unistd.h>
38 #include <libweston/libweston.h>
39 #include <libweston/config-parser.h>
40
41 #include <weston.h>
42 #include "shared/os-compatibility.h"
43 #include "shared/helpers.h"
44 #include "shared/process-util.h"
45
46 #include "agl-shell-server-protocol.h"
47 #include "agl-shell-desktop-server-protocol.h"
48
49 #ifdef HAVE_WALTHAM
50 #include <waltham-transmitter/transmitter_api.h>
51 #endif
52
53 static void
54 create_black_curtain_view(struct ivi_output *output);
55
56 void
57 agl_shell_desktop_advertise_application_id(struct ivi_compositor *ivi,
58                                            struct ivi_surface *surface)
59 {
60         struct desktop_client *dclient;
61         static bool display_adv = false;
62
63         if (surface->advertised_on_launch)
64                 return;
65
66         /* advertise to all desktop clients the new surface */
67         wl_list_for_each(dclient, &ivi->desktop_clients, link) {
68                 const char *app_id =
69                         weston_desktop_surface_get_app_id(surface->dsurface);
70                 if (app_id == NULL) {
71                         if (!display_adv) {
72                                 weston_log("WARNING app_is is null, unable to advertise\n");
73                                 display_adv = true;
74                         }
75                         return;
76                 }
77                 agl_shell_desktop_send_application(dclient->resource, app_id);
78                 surface->advertised_on_launch = true;
79         }
80 }
81
82 void
83 ivi_set_desktop_surface(struct ivi_surface *surface)
84 {
85         struct ivi_compositor *ivi = surface->ivi;
86         assert(surface->role == IVI_SURFACE_ROLE_NONE);
87
88         surface->role = IVI_SURFACE_ROLE_DESKTOP;
89         wl_list_insert(&surface->ivi->surfaces, &surface->link);
90
91         agl_shell_desktop_advertise_application_id(ivi, surface);
92 }
93
94 static void
95 ivi_set_background_surface(struct ivi_surface *surface)
96 {
97         struct ivi_compositor *ivi = surface->ivi;
98         assert(surface->role == IVI_SURFACE_ROLE_BACKGROUND);
99
100         wl_list_insert(&surface->ivi->surfaces, &surface->link);
101         agl_shell_desktop_advertise_application_id(ivi, surface);
102 }
103
104 static void
105 ivi_set_desktop_surface_popup(struct ivi_surface *surface)
106 {
107         struct ivi_compositor *ivi = surface->ivi;
108         assert(surface->role == IVI_SURFACE_ROLE_NONE);
109
110         surface->role = IVI_SURFACE_ROLE_POPUP;
111         wl_list_insert(&ivi->surfaces, &surface->link);
112
113         agl_shell_desktop_advertise_application_id(ivi, surface);
114 }
115
116 static void
117 ivi_set_desktop_surface_fullscreen(struct ivi_surface *surface)
118 {
119         struct ivi_compositor *ivi = surface->ivi;
120         assert(surface->role == IVI_SURFACE_ROLE_NONE);
121
122         surface->role = IVI_SURFACE_ROLE_FULLSCREEN;
123         wl_list_insert(&ivi->surfaces, &surface->link);
124
125         agl_shell_desktop_advertise_application_id(ivi, surface);
126 }
127
128 #ifdef HAVE_WALTHAM
129 void
130 ivi_destroy_waltham_destroy(struct ivi_surface *surface)
131 {
132         struct ivi_compositor *ivi = surface->ivi;
133         const struct weston_transmitter_api *api =
134                 ivi->waltham_transmitter_api;
135
136         if (!api)
137                 return;
138
139         if (surface->waltham_surface.transmitter_surface)
140                 api->surface_destroy(surface->waltham_surface.transmitter_surface);
141 }
142
143 static void
144 ivi_output_notify_waltham_plugin(struct ivi_surface *surface)
145 {
146         struct ivi_compositor *ivi = surface->ivi;
147         const struct weston_transmitter_api *api = ivi->waltham_transmitter_api;
148         struct weston_transmitter *transmitter;
149         struct weston_transmitter_remote *trans_remote;
150         struct weston_surface *weston_surface;
151         struct weston_output *woutput = surface->remote.output->output;
152         const char *app_id;
153
154         if (!api)
155                 return;
156
157         transmitter = api->transmitter_get(ivi->compositor);
158         if (!transmitter)
159                 return;
160
161         trans_remote = api->get_transmitter_remote(woutput->name, transmitter);
162         if (!trans_remote) {
163                 weston_log("Could not find a valie weston_transmitter_remote "
164                                 "that matches the output %s\n", woutput->name);
165                 return;
166         }
167
168         app_id = weston_desktop_surface_get_app_id(surface->dsurface);
169         weston_surface =
170                 weston_desktop_surface_get_surface(surface->dsurface);
171
172         weston_log("Forwarding app_id %s to remote %s\n", app_id, woutput->name);
173
174         /* this will have the effect of informing the remote side to create a
175          * surface with the name app_id. W/ xdg-shell the following happens:
176          *
177          * compositor (server):
178          * surface_push_to_remote():
179          *      waltham-transmitter plug-in
180          *              -> wthp_ivi_app_id_surface_create()
181          *
182          * client -- on the receiver side:
183          *      -> wthp_ivi_app_id_surface_create()
184          *              -> wth_receiver_weston_main()
185          *                      -> wl_compositor_create_surface()
186          *                      -> xdg_wm_base_get_xdg_surface
187          *                      -> xdg_toplevel_set_app_id()
188          *                      -> gst_init()
189          *                      -> gst_parse_launch()
190          *
191          * wth_receiver_weston_main() will be invoked from the handler of
192          * wthp_ivi_app_id_surface_create() and is responsible for setting-up
193          * the gstreamer pipeline as well.
194          */
195         surface->waltham_surface.transmitter_surface =
196             api->surface_push_to_remote(weston_surface, app_id, trans_remote, NULL);
197 }
198
199 #else
200 void
201 ivi_destroy_waltham_destroy(struct ivi_surface *surface)
202 {
203 }
204 static void
205 ivi_output_notify_waltham_plugin(struct ivi_surface *surface)
206 {
207 }
208 #endif
209
210 static void
211 ivi_set_desktop_surface_remote(struct ivi_surface *surface)
212 {
213         struct ivi_compositor *ivi = surface->ivi;
214         struct weston_view *view;
215         struct ivi_output *output = surface->remote.output;
216
217         assert(surface->role == IVI_SURFACE_ROLE_NONE);
218
219         /* remote type are the same as desktop just that client can tell
220          * the compositor to start on another output */
221         surface->role = IVI_SURFACE_ROLE_REMOTE;
222
223         /* if thew black surface view is mapped on the mean we need
224          * to remove it in order to start showing the 'remote' surface
225          * just being added */
226         view = output->fullscreen_view.fs->view;
227         if (view->is_mapped || view->surface->is_mapped)
228                 remove_black_curtain(output);
229
230         if (output->type == OUTPUT_WALTHAM)
231                 ivi_output_notify_waltham_plugin(surface);
232
233         wl_list_insert(&ivi->surfaces, &surface->link);
234 }
235
236
237 static void
238 ivi_set_desktop_surface_split(struct ivi_surface *surface)
239 {
240         struct ivi_compositor *ivi = surface->ivi;
241         assert(surface->role == IVI_SURFACE_ROLE_NONE);
242
243         if (surface->split.orientation == AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL)
244                 surface->role = IVI_SURFACE_ROLE_SPLIT_V;
245         else
246                 surface->role = IVI_SURFACE_ROLE_SPLIT_H;
247
248         wl_list_insert(&ivi->surfaces, &surface->link);
249
250         agl_shell_desktop_advertise_application_id(ivi, surface);
251 }
252
253 static struct pending_popup *
254 ivi_ensure_popup(struct ivi_output *ioutput, int x, int y, int bx, int by,
255                  int width, int height, const char *app_id)
256 {
257         struct pending_popup *p_popup = zalloc(sizeof(*p_popup));
258         size_t len_app_id = strlen(app_id);
259
260         if (!p_popup)
261                 return NULL;
262         p_popup->app_id = zalloc(sizeof(char) * (len_app_id + 1));
263         if (!p_popup->app_id) {
264                 free(p_popup);
265                 return NULL;
266         }
267         memcpy(p_popup->app_id, app_id, len_app_id);
268         p_popup->ioutput = ioutput;
269         p_popup->x = x;
270         p_popup->y = y;
271
272         p_popup->bb.x = bx;
273         p_popup->bb.y = by;
274         p_popup->bb.width = width;
275         p_popup->bb.height = height;
276
277         return p_popup;
278 }
279
280 static void
281 ivi_update_popup(struct ivi_output *ioutput, int x, int y, int bx, int by,
282                  int width, int height, const char *app_id, struct pending_popup *p_popup)
283 {
284         size_t len_app_id = strlen(app_id);
285
286         wl_list_remove(&p_popup->link);
287         wl_list_init(&p_popup->link);
288
289         memset(p_popup->app_id, 0, strlen(app_id) + 1);
290         free(p_popup->app_id);
291
292         p_popup->app_id = zalloc(sizeof(char) * (len_app_id + 1));
293         if (!p_popup->app_id)
294                 return;
295         memcpy(p_popup->app_id, app_id, len_app_id);
296
297         p_popup->ioutput = ioutput;
298         p_popup->x = x;
299         p_popup->y = y;
300
301         p_popup->bb.x = bx;
302         p_popup->bb.y = by;
303         p_popup->bb.width = width;
304         p_popup->bb.height = height;
305 }
306
307 static struct pending_fullscreen *
308 ivi_ensure_fullscreen(struct ivi_output *ioutput, const char *app_id)
309 {
310         struct pending_fullscreen *p_fullscreen = zalloc(sizeof(*p_fullscreen));
311         size_t len_app_id = strlen(app_id);
312
313         if (!p_fullscreen)
314                 return NULL;
315         p_fullscreen->app_id = zalloc(sizeof(char) * (len_app_id + 1));
316         if (!p_fullscreen->app_id) {
317                 free(p_fullscreen);
318                 return NULL;
319         }
320         memcpy(p_fullscreen->app_id, app_id, len_app_id);
321
322         p_fullscreen->ioutput = ioutput;
323         return p_fullscreen;
324 }
325
326 static void
327 ivi_update_fullscreen(struct ivi_output *ioutput, const char *app_id,
328                       struct pending_fullscreen *p_fullscreen)
329 {
330         size_t len_app_id = strlen(app_id);
331
332         wl_list_remove(&p_fullscreen->link);
333         wl_list_init(&p_fullscreen->link);
334
335         memset(p_fullscreen->app_id, 0, strlen(app_id) + 1);
336         free(p_fullscreen->app_id);
337
338         p_fullscreen->app_id = zalloc(sizeof(char) * (len_app_id + 1));
339         if (!p_fullscreen->app_id)
340                 return;
341         memcpy(p_fullscreen->app_id, app_id, len_app_id);
342
343         p_fullscreen->ioutput = ioutput;
344 }
345
346 static struct pending_remote *
347 ivi_ensure_remote(struct ivi_output *ioutput, const char *app_id)
348 {
349         struct pending_remote *p_remote = zalloc(sizeof(*p_remote));
350         size_t len_app_id = strlen(app_id);
351
352         if (!p_remote)
353                 return NULL;
354         p_remote->app_id = zalloc(sizeof(char) * (len_app_id + 1));
355         if (!p_remote->app_id) {
356                 free(p_remote);
357                 return NULL;
358         }
359         memcpy(p_remote->app_id, app_id, len_app_id);
360
361         p_remote->ioutput = ioutput;
362         return p_remote;
363 }
364
365 static void
366 ivi_update_remote(struct ivi_output *ioutput, const char *app_id,
367                       struct pending_remote *p_remote)
368 {
369         size_t len_app_id = strlen(app_id);
370
371         wl_list_remove(&p_remote->link);
372         wl_list_init(&p_remote->link);
373
374         memset(p_remote->app_id, 0, strlen(app_id) + 1);
375         free(p_remote->app_id);
376
377         p_remote->app_id = zalloc(sizeof(char) * (len_app_id + 1));
378         if (!p_remote->app_id)
379                 return;
380         memcpy(p_remote->app_id, app_id, len_app_id);
381
382         p_remote->ioutput = ioutput;
383 }
384
385 static void
386 ivi_set_pending_desktop_surface_popup(struct ivi_output *ioutput, int x, int y, int bx,
387                                       int by, int width, int height, const char *app_id)
388 {
389         struct ivi_compositor *ivi = ioutput->ivi;
390         struct pending_popup *p_popup = NULL;
391         struct pending_popup *popup;
392
393         wl_list_for_each(popup, &ivi->popup_pending_apps, link)
394                 if (!strcmp(app_id, popup->app_id))
395                         p_popup = popup;
396
397         if (!p_popup)
398                 p_popup = ivi_ensure_popup(ioutput, x, y, bx, by, width, height, app_id);
399         else
400                 ivi_update_popup(ioutput, x, y, bx, by, width, height, app_id, p_popup);
401         if (!p_popup)
402                 return;
403
404         wl_list_insert(&ivi->popup_pending_apps, &p_popup->link);
405 }
406
407 static void
408 ivi_set_pending_desktop_surface_fullscreen(struct ivi_output *ioutput,
409                                            const char *app_id)
410 {
411         struct ivi_compositor *ivi = ioutput->ivi;
412         struct pending_fullscreen *p_fullscreen = NULL;
413         struct pending_fullscreen *fullscreen;
414
415         wl_list_for_each(fullscreen, &ivi->fullscreen_pending_apps, link)
416                 if (!strcmp(app_id, fullscreen->app_id))
417                         p_fullscreen = fullscreen;
418
419         if (!p_fullscreen)
420                 p_fullscreen = ivi_ensure_fullscreen(ioutput, app_id);
421         else
422                 ivi_update_fullscreen(ioutput, app_id, p_fullscreen);
423
424         if (!p_fullscreen)
425                 return;
426         wl_list_insert(&ivi->fullscreen_pending_apps, &p_fullscreen->link);
427 }
428
429 static void
430 ivi_set_pending_desktop_surface_split(struct ivi_output *ioutput,
431                                       const char *app_id, uint32_t orientation)
432 {
433         struct ivi_compositor *ivi = ioutput->ivi;
434         struct ivi_surface *surf;
435         size_t len_app_id = strlen(app_id);
436         struct pending_split *split;
437
438         if (orientation != AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL &&
439             orientation != AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL)
440                 return;
441
442         /* more than one is un-supported, do note we need to do
443          * conversion for surface roles instead of using the protocol ones */
444         wl_list_for_each(surf, &ivi->surfaces, link)
445                 if (surf->role == IVI_SURFACE_ROLE_SPLIT_V ||
446                     surf->role == IVI_SURFACE_ROLE_SPLIT_H)
447                         return;
448
449         split = zalloc(sizeof(*split));
450         if (!split)
451                 return;
452         split->app_id = zalloc(sizeof(char) * (len_app_id + 1));
453         if (!split->app_id) {
454                 free(split);
455                 return;
456         }
457         memcpy(split->app_id, app_id, len_app_id);
458
459         split->ioutput = ioutput;
460         split->orientation = orientation;
461
462         wl_list_insert(&ivi->split_pending_apps, &split->link);
463 }
464
465 void
466 ivi_set_pending_desktop_surface_remote(struct ivi_output *ioutput,
467                 const char *app_id)
468 {
469         struct ivi_compositor *ivi = ioutput->ivi;
470         struct pending_remote *remote;
471         struct pending_remote *p_remote = NULL;
472
473         wl_list_for_each(remote, &ivi->remote_pending_apps, link)
474                 if (!strcmp(app_id, remote->app_id))
475                         p_remote = remote;
476
477         if (!p_remote)
478                 p_remote = ivi_ensure_remote(ioutput, app_id);
479         else
480                 ivi_update_remote(ioutput, app_id, p_remote);
481         if (!p_remote)
482                 return;
483
484         wl_list_insert(&ivi->remote_pending_apps, &p_remote->link);
485 }
486
487
488 static void
489 ivi_remove_pending_desktop_surface_split(struct pending_split *split)
490 {
491         free(split->app_id);
492         wl_list_remove(&split->link);
493         free(split);
494 }
495
496 static void
497 ivi_remove_pending_desktop_surface_fullscreen(struct pending_fullscreen *fs)
498 {
499         free(fs->app_id);
500         wl_list_remove(&fs->link);
501         free(fs);
502 }
503
504 static void
505 ivi_remove_pending_desktop_surface_popup(struct pending_popup *p_popup)
506 {
507         free(p_popup->app_id);
508         wl_list_remove(&p_popup->link);
509         free(p_popup);
510 }
511
512 static void
513 ivi_remove_pending_desktop_surface_remote(struct pending_remote *remote)
514 {
515         free(remote->app_id);
516         wl_list_remove(&remote->link);
517         free(remote);
518 }
519
520 static bool
521 ivi_compositor_keep_pending_surfaces(struct ivi_surface *surface)
522 {
523         return surface->ivi->keep_pending_surfaces;
524 }
525
526 static bool
527 ivi_check_pending_desktop_surface_popup(struct ivi_surface *surface)
528 {
529         struct ivi_compositor *ivi = surface->ivi;
530         struct pending_popup *p_popup, *next_p_popup;
531         const char *_app_id =
532                         weston_desktop_surface_get_app_id(surface->dsurface);
533
534         if (wl_list_empty(&ivi->popup_pending_apps) || !_app_id)
535                 return false;
536
537         wl_list_for_each_safe(p_popup, next_p_popup,
538                               &ivi->popup_pending_apps, link) {
539                 if (!strcmp(_app_id, p_popup->app_id)) {
540                         surface->popup.output = p_popup->ioutput;
541                         surface->popup.x = p_popup->x;
542                         surface->popup.y = p_popup->y;
543
544                         surface->popup.bb.x = p_popup->bb.x;
545                         surface->popup.bb.y = p_popup->bb.y;
546                         surface->popup.bb.width = p_popup->bb.width;
547                         surface->popup.bb.height = p_popup->bb.height;
548
549                         if (!ivi_compositor_keep_pending_surfaces(surface))
550                                 ivi_remove_pending_desktop_surface_popup(p_popup);
551                         return true;
552                 }
553         }
554
555         return false;
556 }
557
558 static bool
559 ivi_check_pending_desktop_surface_split(struct ivi_surface *surface)
560 {
561         struct pending_split *split_surf, *next_split_surf;
562         struct ivi_compositor *ivi = surface->ivi;
563         const char *_app_id =
564                         weston_desktop_surface_get_app_id(surface->dsurface);
565
566         if (wl_list_empty(&ivi->split_pending_apps) || !_app_id)
567                 return false;
568
569         wl_list_for_each_safe(split_surf, next_split_surf,
570                               &ivi->split_pending_apps, link) {
571                 if (!strcmp(_app_id, split_surf->app_id)) {
572                         surface->split.output = split_surf->ioutput;
573                         surface->split.orientation = split_surf->orientation;
574                         if (!ivi_compositor_keep_pending_surfaces(surface))
575                                 ivi_remove_pending_desktop_surface_split(split_surf);
576                         return true;
577                 }
578         }
579
580         return false;
581 }
582
583 static bool
584 ivi_check_pending_desktop_surface_fullscreen(struct ivi_surface *surface)
585 {
586         struct pending_fullscreen *fs_surf, *next_fs_surf;
587         struct ivi_compositor *ivi = surface->ivi;
588         const char *_app_id =
589                         weston_desktop_surface_get_app_id(surface->dsurface);
590
591         if (wl_list_empty(&ivi->fullscreen_pending_apps) || !_app_id)
592                 return false;
593
594         wl_list_for_each_safe(fs_surf, next_fs_surf,
595                               &ivi->fullscreen_pending_apps, link) {
596                 if (!strcmp(_app_id, fs_surf->app_id)) {
597                         surface->fullscreen.output = fs_surf->ioutput;
598                         if (!ivi_compositor_keep_pending_surfaces(surface))
599                                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
600                         return true;
601                 }
602         }
603
604         return false;
605 }
606
607 static bool
608 ivi_check_pending_desktop_surface_remote(struct ivi_surface *surface)
609 {
610         struct pending_remote *remote_surf, *next_remote_surf;
611         struct ivi_compositor *ivi = surface->ivi;
612         const char *_app_id =
613                 weston_desktop_surface_get_app_id(surface->dsurface);
614
615         if (wl_list_empty(&ivi->remote_pending_apps) || !_app_id)
616                 return false;
617
618         wl_list_for_each_safe(remote_surf, next_remote_surf,
619                               &ivi->remote_pending_apps, link) {
620                 if (!strcmp(_app_id, remote_surf->app_id)) {
621                         surface->remote.output = remote_surf->ioutput;
622                         if (!ivi_compositor_keep_pending_surfaces(surface))
623                                 ivi_remove_pending_desktop_surface_remote(remote_surf);
624                         return true;
625                 }
626         }
627
628         return false;
629 }
630 void
631 ivi_check_pending_surface_desktop(struct ivi_surface *surface,
632                                   enum ivi_surface_role *role)
633 {
634         struct ivi_compositor *ivi = surface->ivi;
635         struct wl_list *role_pending_list;
636         struct pending_popup *p_popup;
637         struct pending_split *p_split;
638         struct pending_fullscreen *p_fullscreen;
639         struct pending_remote *p_remote;
640         const char *app_id =
641                 weston_desktop_surface_get_app_id(surface->dsurface);
642
643         role_pending_list = &ivi->popup_pending_apps;
644         wl_list_for_each(p_popup, role_pending_list, link) {
645                 if (app_id && !strcmp(app_id, p_popup->app_id)) {
646                         *role = IVI_SURFACE_ROLE_POPUP;
647                         return;
648                 }
649         }
650
651         role_pending_list = &ivi->split_pending_apps;
652         wl_list_for_each(p_split, role_pending_list, link) {
653                 if (app_id && !strcmp(app_id, p_split->app_id)) {
654                         *role = IVI_SURFACE_ROLE_SPLIT_V;
655                         return;
656                 }
657         }
658
659         role_pending_list = &ivi->fullscreen_pending_apps;
660         wl_list_for_each(p_fullscreen, role_pending_list, link) {
661                 if (app_id && !strcmp(app_id, p_fullscreen->app_id)) {
662                         *role = IVI_SURFACE_ROLE_FULLSCREEN;
663                         return;
664                 }
665         }
666
667         role_pending_list = &ivi->remote_pending_apps;
668         wl_list_for_each(p_remote, role_pending_list, link) {
669                 if (app_id && !strcmp(app_id, p_remote->app_id)) {
670                         *role = IVI_SURFACE_ROLE_REMOTE;
671                         return;
672                 }
673         }
674
675         /* else, we are a regular desktop surface */
676         *role = IVI_SURFACE_ROLE_DESKTOP;
677 }
678
679
680 void
681 ivi_check_pending_desktop_surface(struct ivi_surface *surface)
682 {
683         bool ret = false;
684
685         ret = ivi_check_pending_desktop_surface_popup(surface);
686         if (ret) {
687                 ivi_set_desktop_surface_popup(surface);
688                 ivi_layout_popup_committed(surface);
689                 return;
690         }
691
692         ret = ivi_check_pending_desktop_surface_split(surface);
693         if (ret) {
694                 ivi_set_desktop_surface_split(surface);
695                 ivi_layout_split_committed(surface);
696                 return;
697         }
698
699         ret = ivi_check_pending_desktop_surface_fullscreen(surface);
700         if (ret) {
701                 ivi_set_desktop_surface_fullscreen(surface);
702                 ivi_layout_fullscreen_committed(surface);
703                 return;
704         }
705
706         ret = ivi_check_pending_desktop_surface_remote(surface);
707         if (ret) {
708                 ivi_set_desktop_surface_remote(surface);
709                 ivi_layout_remote_committed(surface);
710                 return;
711         }
712
713         /* if we end up here means we have a regular desktop app and
714          * try to activate it */
715         ivi_set_desktop_surface(surface);
716         ivi_layout_desktop_committed(surface);
717 }
718
719 void
720 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
721 {
722         struct ivi_output *out;
723
724         wl_list_for_each(out, &ivi->outputs, link) {
725                 create_black_curtain_view(out);
726                 insert_black_curtain(out);
727         }
728 }
729
730 int
731 ivi_shell_init(struct ivi_compositor *ivi)
732 {
733         weston_layer_init(&ivi->hidden, ivi->compositor);
734         weston_layer_init(&ivi->background, ivi->compositor);
735         weston_layer_init(&ivi->normal, ivi->compositor);
736         weston_layer_init(&ivi->panel, ivi->compositor);
737         weston_layer_init(&ivi->popup, ivi->compositor);
738         weston_layer_init(&ivi->fullscreen, ivi->compositor);
739
740         weston_layer_set_position(&ivi->hidden,
741                                   WESTON_LAYER_POSITION_HIDDEN);
742         weston_layer_set_position(&ivi->background,
743                                   WESTON_LAYER_POSITION_BACKGROUND);
744         weston_layer_set_position(&ivi->normal,
745                                   WESTON_LAYER_POSITION_NORMAL);
746         weston_layer_set_position(&ivi->panel,
747                                   WESTON_LAYER_POSITION_UI);
748         weston_layer_set_position(&ivi->popup,
749                                   WESTON_LAYER_POSITION_TOP_UI);
750         weston_layer_set_position(&ivi->fullscreen,
751                                   WESTON_LAYER_POSITION_FULLSCREEN);
752
753         return 0;
754 }
755
756
757 static void
758 ivi_surf_destroy(struct ivi_surface *surf)
759 {
760         struct weston_surface *wsurface = surf->view->surface;
761
762         if (weston_surface_is_mapped(wsurface)) {
763                 weston_desktop_surface_unlink_view(surf->view);
764                 weston_view_destroy(surf->view);
765         }
766
767         wl_list_remove(&surf->link);
768         free(surf);
769 }
770
771 static void
772 ivi_shell_destroy_views_on_layer(struct weston_layer *layer)
773 {
774         struct weston_view *view, *view_next;
775
776         wl_list_for_each_safe(view, view_next, &layer->view_list.link, layer_link.link) {
777                 struct ivi_surface *ivi_surf =
778                         get_ivi_shell_surface(view->surface);
779                 if (ivi_surf)
780                         ivi_surf_destroy(ivi_surf);
781         }
782 }
783
784 void
785 ivi_shell_finalize(struct ivi_compositor *ivi)
786 {
787         struct ivi_output *output;
788
789         ivi_shell_destroy_views_on_layer(&ivi->hidden);
790         weston_layer_fini(&ivi->hidden);
791
792         ivi_shell_destroy_views_on_layer(&ivi->background);
793         weston_layer_fini(&ivi->background);
794
795         ivi_shell_destroy_views_on_layer(&ivi->normal);
796         weston_layer_fini(&ivi->normal);
797
798         ivi_shell_destroy_views_on_layer(&ivi->panel);
799         weston_layer_fini(&ivi->panel);
800
801         ivi_shell_destroy_views_on_layer(&ivi->popup);
802         weston_layer_fini(&ivi->popup);
803
804         wl_list_for_each(output, &ivi->outputs, link) {
805                 if (output->fullscreen_view.fs &&
806                     output->fullscreen_view.fs->view) {
807                         weston_surface_destroy(output->fullscreen_view.fs->view->surface);
808                         output->fullscreen_view.fs->view = NULL;
809                 }
810         }
811         weston_layer_fini(&ivi->fullscreen);
812 }
813
814 static void
815 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
816 {
817         struct ivi_surface *surface;
818
819         wl_list_for_each(surface, &ivi->surfaces, link) {
820                 const char *app_id =
821                         weston_desktop_surface_get_app_id(surface->dsurface);
822                 if (app_id == NULL) {
823                         weston_log("WARNING app_is is null, unable to advertise\n");
824                         return;
825                 }
826                 agl_shell_desktop_send_application(resource, app_id);
827         }
828 }
829
830 static struct wl_client *
831 client_launch(struct weston_compositor *compositor,
832                      struct weston_process *proc,
833                      const char *path,
834                      weston_process_cleanup_func_t cleanup)
835 {
836         struct wl_client *client = NULL;
837         struct custom_env child_env;
838         struct fdstr wayland_socket;
839         const char *fail_cloexec = "Couldn't unset CLOEXEC on client socket";
840         const char *fail_seteuid = "Couldn't call seteuid";
841         char *fail_exec;
842         char * const *argp;
843         char * const *envp;
844         sigset_t allsigs;
845         pid_t pid;
846         bool ret;
847         struct ivi_compositor *ivi;
848         size_t written __attribute__((unused));
849
850         weston_log("launching '%s'\n", path);
851         str_printf(&fail_exec, "Error: Couldn't launch client '%s'\n", path);
852
853         custom_env_init_from_environ(&child_env);
854         custom_env_add_from_exec_string(&child_env, path);
855
856         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0,
857                                   wayland_socket.fds) < 0) {
858                 weston_log("client_launch: "
859                            "socketpair failed while launching '%s': %s\n",
860                            path, strerror(errno));
861                 custom_env_fini(&child_env);
862                 return NULL;
863         }
864         fdstr_update_str1(&wayland_socket);
865         custom_env_set_env_var(&child_env, "WAYLAND_SOCKET",
866                                wayland_socket.str1);
867
868         argp = custom_env_get_argp(&child_env);
869         envp = custom_env_get_envp(&child_env);
870
871         pid = fork();
872         switch (pid) {
873         case 0:
874                 /* Put the client in a new session so it won't catch signals
875                  * intended for the parent. Sharing a session can be
876                  * confusing when launching weston under gdb, as the ctrl-c
877                  * intended for gdb will pass to the child, and weston
878                  * will cleanly shut down when the child exits.
879                  */
880                 setsid();
881
882                 /* do not give our signal mask to the new process */
883                 sigfillset(&allsigs);
884                 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
885
886                 /* Launch clients as the user. Do not launch clients with wrong euid. */
887                 if (seteuid(getuid()) == -1) {
888                         written = write(STDERR_FILENO, fail_seteuid, strlen(fail_seteuid));
889                         _exit(EXIT_FAILURE);
890                 }
891
892                 ret = fdstr_clear_cloexec_fd1(&wayland_socket);
893                 if (!ret) {
894                         written = write(STDERR_FILENO, fail_cloexec, strlen(fail_cloexec));
895                         _exit(EXIT_FAILURE);
896                 }
897
898                 execve(argp[0], argp, envp);
899
900                 if (fail_exec)
901                         written = write(STDERR_FILENO, fail_exec, strlen(fail_exec));
902                 _exit(EXIT_FAILURE);
903
904         default:
905                 close(wayland_socket.fds[1]);
906                 ivi = weston_compositor_get_user_data(compositor);
907                 client = wl_client_create(compositor->wl_display,
908                                           wayland_socket.fds[0]);
909                 if (!client) {
910                         custom_env_fini(&child_env);
911                         close(wayland_socket.fds[0]);
912                         free(fail_exec);
913                         weston_log("client_launch: "
914                                 "wl_client_create failed while launching '%s'.\n",
915                                 path);
916                         return NULL;
917                 }
918
919                 proc->pid = pid;
920                 proc->cleanup = cleanup;
921                 wl_list_insert(&ivi->child_process_list, &proc->link);
922                 break;
923
924         case -1:
925                 fdstr_close_all(&wayland_socket);
926                 weston_log("client_launch: "
927                            "fork failed while launching '%s': %s\n", path,
928                            strerror(errno));
929                 break;
930         }
931
932         custom_env_fini(&child_env);
933         free(fail_exec);
934
935         return client;
936 }
937
938 struct process_info {
939         struct weston_process proc;
940         char *path;
941 };
942
943 int
944 sigchld_handler(int signal_number, void *data)
945 {
946         struct weston_process *p;
947         struct ivi_compositor *ivi = data;
948         int status;
949         pid_t pid;
950
951         while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
952                 wl_list_for_each(p, &ivi->child_process_list, link) {
953                         if (p->pid == pid)
954                                 break;
955                 }
956
957                 if (&p->link == &ivi->child_process_list) {
958                         weston_log("unknown child process exited\n");
959                         continue;
960                 }
961
962                 wl_list_remove(&p->link);
963                 wl_list_init(&p->link);
964                 p->cleanup(p, status);
965         }
966
967         if (pid < 0 && errno != ECHILD)
968                 weston_log("waitpid error %s\n", strerror(errno));
969
970         return 1;
971 }
972
973
974 static void
975 process_handle_sigchld(struct weston_process *process, int status)
976 {
977         struct process_info *pinfo =
978                 container_of(process, struct process_info, proc);
979
980         /*
981          * There are no guarantees whether this runs before or after
982          * the wl_client destructor.
983          */
984
985         if (WIFEXITED(status)) {
986                 weston_log("%s exited with status %d\n", pinfo->path,
987                            WEXITSTATUS(status));
988         } else if (WIFSIGNALED(status)) {
989                 weston_log("%s died on signal %d\n", pinfo->path,
990                            WTERMSIG(status));
991         } else {
992                 weston_log("%s disappeared\n", pinfo->path);
993         }
994
995         free(pinfo->path);
996         free(pinfo);
997 }
998
999 int
1000 ivi_launch_shell_client(struct ivi_compositor *ivi, const char *cmd_section,
1001                         struct wl_client **client)
1002 {
1003         struct process_info *pinfo;
1004         struct weston_config_section *section;
1005         char *command = NULL;
1006
1007         section = weston_config_get_section(ivi->config, cmd_section, NULL, NULL);
1008         if (section)
1009                 weston_config_section_get_string(section, "command", &command, NULL);
1010
1011         if (!command)
1012                 return -1;
1013
1014         pinfo = zalloc(sizeof *pinfo);
1015         if (!pinfo)
1016                 return -1;
1017
1018         pinfo->path = strdup(command);
1019         if (!pinfo->path)
1020                 goto out_free;
1021
1022         *client = client_launch(ivi->compositor, &pinfo->proc, command, process_handle_sigchld);
1023         if (!*client)
1024                 goto out_str;
1025
1026         return 0;
1027
1028 out_str:
1029         free(pinfo->path);
1030
1031 out_free:
1032         free(pinfo);
1033
1034         return -1;
1035 }
1036
1037 static void
1038 destroy_black_curtain_view(struct wl_listener *listener, void *data)
1039 {
1040         struct fullscreen_view *fs =
1041                 wl_container_of(listener, fs, fs_destroy);
1042
1043
1044         if (fs && fs->fs) {
1045                 wl_list_remove(&fs->fs_destroy.link);
1046                 free(fs->fs);
1047         }
1048 }
1049
1050
1051 static void
1052 create_black_curtain_view(struct ivi_output *output)
1053 {
1054         struct weston_surface *surface = NULL;
1055         struct weston_view *view;
1056         struct ivi_compositor *ivi = output->ivi;
1057         struct weston_compositor *wc= ivi->compositor;
1058         struct weston_output *woutput = output->output;
1059
1060         if (!woutput)
1061                 return;
1062
1063         surface = weston_surface_create(wc);
1064         if (!surface)
1065                 return;
1066         view = weston_view_create(surface);
1067         if (!view) {
1068                 weston_surface_destroy(surface);
1069                 return;
1070         }
1071
1072         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1073         weston_surface_set_size(surface, woutput->width, woutput->height);
1074         weston_view_set_position(view, woutput->x, woutput->y);
1075
1076         output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
1077         if (!output->fullscreen_view.fs) {
1078                 weston_surface_destroy(surface);
1079                 return;
1080         }
1081         output->fullscreen_view.fs->view = view;
1082
1083         output->fullscreen_view.fs_destroy.notify = destroy_black_curtain_view;
1084         wl_signal_add(&woutput->destroy_signal,
1085                       &output->fullscreen_view.fs_destroy);
1086 }
1087
1088 bool
1089 output_has_black_curtain(struct ivi_output *output)
1090 {
1091         return (output->fullscreen_view.fs &&
1092                 output->fullscreen_view.fs->view &&
1093                 output->fullscreen_view.fs->view->is_mapped &&
1094                 output->fullscreen_view.fs->view->surface->is_mapped);
1095 }
1096
1097 void
1098 remove_black_curtain(struct ivi_output *output)
1099 {
1100         struct weston_view *view;
1101
1102         if ((!output &&
1103             !output->fullscreen_view.fs &&
1104             !output->fullscreen_view.fs->view) ||
1105             !output->fullscreen_view.fs) {
1106                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
1107                 return;
1108         }
1109
1110         view = output->fullscreen_view.fs->view;
1111         assert(view->is_mapped == true ||
1112                view->surface->is_mapped == true);
1113
1114         view->is_mapped = false;
1115         view->surface->is_mapped = false;
1116
1117         weston_layer_entry_remove(&view->layer_link);
1118         weston_view_update_transform(view);
1119
1120         weston_view_damage_below(view);
1121
1122         weston_log("Removed black curtain from output %s\n", output->output->name);
1123 }
1124
1125 void
1126 insert_black_curtain(struct ivi_output *output)
1127 {
1128         struct weston_view *view;
1129
1130         if ((!output &&
1131             !output->fullscreen_view.fs &&
1132             !output->fullscreen_view.fs->view) || !output->output ||
1133             !output->fullscreen_view.fs) {
1134                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
1135                 return;
1136         }
1137
1138         view = output->fullscreen_view.fs->view;
1139         if (view->is_mapped || view->surface->is_mapped)
1140                 return;
1141
1142         weston_layer_entry_remove(&view->layer_link);
1143         weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
1144                                   &view->layer_link);
1145
1146         view->is_mapped = true;
1147         view->surface->is_mapped = true;
1148
1149         weston_view_update_transform(view);
1150         weston_view_damage_below(view);
1151
1152         weston_log("Added black curtain to output %s\n", output->output->name);
1153 }
1154
1155 void
1156 shell_send_app_state(struct ivi_compositor *ivi, const char *app_id,
1157                      enum agl_shell_app_state state)
1158 {
1159         if (app_id && wl_resource_get_version(ivi->shell_client.resource) >=
1160             AGL_SHELL_APP_STATE_SINCE_VERSION) {
1161
1162                 agl_shell_send_app_state(ivi->shell_client.resource,
1163                                          app_id, state);
1164
1165                 if (ivi->shell_client.resource_ext)
1166                         agl_shell_send_app_state(ivi->shell_client.resource_ext,
1167                                                  app_id, state);
1168         }
1169 }
1170
1171 void
1172 shell_send_app_on_output(struct ivi_compositor *ivi, const char *app_id,
1173                          const char *output_name)
1174 {
1175         if (app_id && ivi->shell_client.resource &&
1176             wl_resource_get_version(ivi->shell_client.resource) >=
1177             AGL_SHELL_APP_ON_OUTPUT_SINCE_VERSION) {
1178
1179                 agl_shell_send_app_on_output(ivi->shell_client.resource,
1180                                          app_id, output_name);
1181
1182                 if (ivi->shell_client.resource_ext)
1183                         agl_shell_send_app_on_output(ivi->shell_client.resource_ext,
1184                                                  app_id, output_name);
1185         }
1186 }
1187
1188 static void
1189 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
1190 {
1191         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1192         struct ivi_output *output;
1193         struct ivi_surface *surface, *tmp;
1194
1195         if (wl_resource_get_version(shell_res) >=
1196             AGL_SHELL_BOUND_OK_SINCE_VERSION &&
1197             ivi->shell_client.status == BOUND_FAILED) {
1198                 wl_resource_post_error(shell_res,
1199                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1200                                        "agl_shell has already been bound. "
1201                                        "Check out bound_fail event");
1202                 return;
1203         }
1204
1205         /* Init already finished. Do nothing */
1206         if (ivi->shell_client.ready)
1207                 return;
1208
1209
1210         ivi->shell_client.ready = true;
1211
1212         wl_list_for_each(output, &ivi->outputs, link) {
1213                 if (output->background &&
1214                     output->background->role == IVI_SURFACE_ROLE_BACKGROUND) {
1215                         /* track the background surface role as a "regular"
1216                          * surface so we can activate it */
1217                         ivi_set_background_surface(output->background);
1218                         remove_black_curtain(output);
1219                 }
1220
1221                 ivi_layout_init(ivi, output);
1222         }
1223
1224         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
1225                 const char *app_id;
1226
1227                 wl_list_remove(&surface->link);
1228                 wl_list_init(&surface->link);
1229                 ivi_check_pending_desktop_surface(surface);
1230                 surface->checked_pending = true;
1231                 app_id = weston_desktop_surface_get_app_id(surface->dsurface);
1232
1233                 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
1234         }
1235 }
1236
1237 static void
1238 shell_set_background(struct wl_client *client,
1239                      struct wl_resource *shell_res,
1240                      struct wl_resource *surface_res,
1241                      struct wl_resource *output_res)
1242 {
1243         struct weston_head *head = weston_head_from_resource(output_res);
1244         struct weston_output *woutput = weston_head_get_output(head);
1245         struct ivi_output *output = to_ivi_output(woutput);
1246         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
1247         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1248         struct weston_desktop_surface *dsurface;
1249         struct ivi_surface *surface;
1250
1251         if ((wl_resource_get_version(shell_res) >=
1252             AGL_SHELL_BOUND_OK_SINCE_VERSION &&
1253             ivi->shell_client.status == BOUND_FAILED) ||
1254             ivi->shell_client.resource_ext == shell_res) {
1255                 wl_resource_post_error(shell_res,
1256                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1257                                        "agl_shell has already been bound. "
1258                                        "Check out bound_fail event");
1259                 return;
1260         }
1261
1262         dsurface = weston_surface_get_desktop_surface(wsurface);
1263         if (!dsurface) {
1264                 wl_resource_post_error(shell_res,
1265                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1266                                        "surface must be a desktop surface");
1267                 return;
1268         }
1269
1270         surface = weston_desktop_surface_get_user_data(dsurface);
1271         if (surface->role != IVI_SURFACE_ROLE_NONE) {
1272                 wl_resource_post_error(shell_res,
1273                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1274                                        "surface already has another ivi role");
1275                 return;
1276         }
1277
1278         if (output->background) {
1279                 wl_resource_post_error(shell_res,
1280                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
1281                                        "output already has background");
1282                 return;
1283         }
1284
1285         surface->checked_pending = true;
1286         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
1287         surface->bg.output = output;
1288         wl_list_remove(&surface->link);
1289         wl_list_init(&surface->link);
1290
1291         output->background = surface;
1292
1293         weston_desktop_surface_set_maximized(dsurface, true);
1294         weston_desktop_surface_set_size(dsurface,
1295                                         output->output->width,
1296                                         output->output->height);
1297 }
1298
1299 static void
1300 shell_set_panel(struct wl_client *client,
1301                 struct wl_resource *shell_res,
1302                 struct wl_resource *surface_res,
1303                 struct wl_resource *output_res,
1304                 uint32_t edge)
1305 {
1306         struct weston_head *head = weston_head_from_resource(output_res);
1307         struct weston_output *woutput = weston_head_get_output(head);
1308         struct ivi_output *output = to_ivi_output(woutput);
1309         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
1310         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1311         struct weston_desktop_surface *dsurface;
1312         struct ivi_surface *surface;
1313         struct ivi_surface **member;
1314         int32_t width = 0, height = 0;
1315
1316         if ((wl_resource_get_version(shell_res) >=
1317              AGL_SHELL_BOUND_OK_SINCE_VERSION &&
1318              ivi->shell_client.status == BOUND_FAILED) ||
1319             ivi->shell_client.resource_ext == shell_res) {
1320                 wl_resource_post_error(shell_res,
1321                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1322                                        "agl_shell has already been bound. "
1323                                        "Check out bound_fail event");
1324                 return;
1325         }
1326
1327         dsurface = weston_surface_get_desktop_surface(wsurface);
1328         if (!dsurface) {
1329                 wl_resource_post_error(shell_res,
1330                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1331                                        "surface must be a desktop surface");
1332                 return;
1333         }
1334
1335         surface = weston_desktop_surface_get_user_data(dsurface);
1336         if (surface->role != IVI_SURFACE_ROLE_NONE) {
1337                 wl_resource_post_error(shell_res,
1338                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1339                                        "surface already has another ivi role");
1340                 return;
1341         }
1342
1343         switch (edge) {
1344         case AGL_SHELL_EDGE_TOP:
1345                 member = &output->top;
1346                 break;
1347         case AGL_SHELL_EDGE_BOTTOM:
1348                 member = &output->bottom;
1349                 break;
1350         case AGL_SHELL_EDGE_LEFT:
1351                 member = &output->left;
1352                 break;
1353         case AGL_SHELL_EDGE_RIGHT:
1354                 member = &output->right;
1355                 break;
1356         default:
1357                 wl_resource_post_error(shell_res,
1358                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1359                                        "invalid edge for panel");
1360                 return;
1361         }
1362
1363         if (*member) {
1364                 wl_resource_post_error(shell_res,
1365                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
1366                                        "output already has panel on this edge");
1367                 return;
1368         }
1369
1370         surface->checked_pending = true;
1371         surface->role = IVI_SURFACE_ROLE_PANEL;
1372         surface->panel.output = output;
1373         surface->panel.edge = edge;
1374         wl_list_remove(&surface->link);
1375         wl_list_init(&surface->link);
1376
1377         *member = surface;
1378
1379         switch (surface->panel.edge) {
1380         case AGL_SHELL_EDGE_TOP:
1381         case AGL_SHELL_EDGE_BOTTOM:
1382                 width = woutput->width;
1383                 break;
1384         case AGL_SHELL_EDGE_LEFT:
1385         case AGL_SHELL_EDGE_RIGHT:
1386                 height = woutput->height;
1387                 break;
1388         }
1389
1390         weston_desktop_surface_set_size(dsurface, width, height);
1391 }
1392
1393 void
1394 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
1395                           const char *data, uint32_t app_state)
1396 {
1397         struct desktop_client *dclient;
1398         uint32_t app_role;
1399         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1400         struct ivi_policy *policy = ivi->policy;
1401
1402         /* FIXME: should queue it here and see when binding agl-shell-desktop
1403          * if there are any to be sent */
1404         if (!surf)
1405                 return;
1406
1407         if (!app_id)
1408                 return;
1409
1410         if (policy && policy->api.surface_advertise_state_change &&
1411             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
1412                 return;
1413         }
1414
1415         app_role = surf->role;
1416         if (app_role == IVI_SURFACE_ROLE_POPUP)
1417                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
1418
1419         wl_list_for_each(dclient, &ivi->desktop_clients, link)
1420                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
1421                                                  data, app_state, app_role);
1422 }
1423
1424 static void
1425 shell_activate_app(struct wl_client *client,
1426                    struct wl_resource *shell_res,
1427                    const char *app_id,
1428                    struct wl_resource *output_res)
1429 {
1430         struct weston_head *head;
1431         struct weston_output *woutput;
1432         struct ivi_compositor *ivi;
1433         struct ivi_output *output;
1434
1435         head = weston_head_from_resource(output_res);
1436         if (!head) {
1437                 weston_log("Invalid output to activate '%s' on\n", app_id);
1438                 return;
1439         }
1440
1441         woutput = weston_head_get_output(head);
1442         ivi = wl_resource_get_user_data(shell_res);
1443         output = to_ivi_output(woutput);
1444
1445         if (wl_resource_get_version(shell_res) >=
1446             AGL_SHELL_BOUND_OK_SINCE_VERSION &&
1447             ivi->shell_client.status == BOUND_FAILED) {
1448                 wl_resource_post_error(shell_res,
1449                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1450                                        "agl_shell has already been bound. "
1451                                        "Check out bound_fail event");
1452                 return;
1453         }
1454
1455         ivi_layout_activate(output, app_id);
1456 }
1457
1458 static void
1459 shell_new_deactivate_app(struct wl_client *client, struct wl_resource *shell_res,
1460                          const char *app_id)
1461 {
1462         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1463
1464         ivi_layout_deactivate(ivi, app_id);
1465 }
1466
1467 static void
1468 shell_set_app_float(struct wl_client *client, struct wl_resource *shell_res,
1469                    const char *app_id, int32_t x_pos, int32_t y_pos)
1470 {
1471         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1472         struct weston_output *output = get_focused_output(ivi->compositor);
1473         struct ivi_output *ivi_output;
1474         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1475
1476         if (!output)
1477                 output = get_default_output(ivi->compositor);
1478
1479         ivi_output = to_ivi_output(output);
1480
1481         /* verify if already mapped as desktop role, regular, unmap it, so we
1482          * can make it float */
1483         if (surf && surf->role == IVI_SURFACE_ROLE_DESKTOP) {
1484                 struct weston_view *ev = surf->view;
1485                 struct weston_desktop_surface *dsurf = surf->dsurface;
1486                 struct ivi_bounding_box bb = {};
1487                 const char *prev_activate_app_id = NULL;
1488
1489                 /* XXX: if this is useful we could bring it as active then make
1490                  * it float, but avoid doing it for the moment */
1491                 if (surf != ivi_output->active)
1492                         return;
1493
1494                 if (ivi_output->previous_active)
1495                         prev_activate_app_id =
1496                                 weston_desktop_surface_get_app_id(ivi_output->previous_active->dsurface);
1497
1498                 /* only deactivate if previous_active is different, otherwise
1499                  * this will blow up, because we're trying to activate the same
1500                  * app_id as the this one! */
1501                 if (prev_activate_app_id && strcmp(app_id, prev_activate_app_id)) {
1502                         ivi_layout_deactivate(ivi, app_id);
1503                 } else if (prev_activate_app_id) {
1504                         weston_layer_entry_remove(&ev->layer_link);
1505                         weston_view_geometry_dirty(ev);
1506                         weston_surface_damage(ev->surface);
1507                 }
1508
1509                 surf->hidden_layer_output = ivi_output;
1510
1511                 /* set attributes */
1512                 surf->popup.output = ivi_output;
1513
1514                 surf->popup.x = x_pos;
1515                 surf->popup.y = y_pos;
1516                 surf->popup.bb = bb;
1517
1518
1519                 /* change the role */
1520                 surf->role = IVI_SURFACE_ROLE_NONE;
1521
1522                 wl_list_remove(&surf->link);
1523                 wl_list_init(&surf->link);
1524
1525                 ivi_set_desktop_surface_popup(surf);
1526
1527                 weston_desktop_surface_set_maximized(dsurf, false);
1528                 weston_desktop_surface_set_size(dsurf, 0, 0);
1529
1530                 /* add to hidden layer */
1531                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
1532                 weston_compositor_schedule_repaint(ivi->compositor);
1533
1534         } else if (!surf || (surf && surf->role != IVI_SURFACE_ROLE_POPUP)) {
1535                 ivi_set_pending_desktop_surface_popup(ivi_output, x_pos, y_pos, 
1536                                                       0, 0, 0, 0, app_id);
1537         }
1538 }
1539
1540 static void
1541 shell_set_app_fullscreen(struct wl_client *client,
1542                          struct wl_resource *shell_res, const char *app_id)
1543 {
1544         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1545         struct weston_output *output = get_focused_output(ivi->compositor);
1546         struct ivi_output *ivi_output;
1547         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1548
1549         if (!output)
1550                 output = get_default_output(ivi->compositor);
1551
1552         ivi_output = to_ivi_output(output);
1553
1554         if (surf && surf->role == IVI_SURFACE_ROLE_DESKTOP) {
1555                 struct weston_view *ev = surf->view;
1556                 struct weston_desktop_surface *dsurf = surf->dsurface;
1557
1558                 if (surf != ivi_output->active)
1559                         return;
1560
1561                 weston_layer_entry_remove(&surf->view->layer_link);
1562                 weston_view_geometry_dirty(surf->view);
1563                 weston_surface_damage(surf->view->surface);
1564
1565                 surf->hidden_layer_output = ivi_output;
1566
1567                 /* set attributes */
1568                 surf->fullscreen.output = ivi_output;
1569
1570                 /* change the role */
1571                 surf->role = IVI_SURFACE_ROLE_NONE;
1572
1573                 wl_list_remove(&surf->link);
1574                 wl_list_init(&surf->link);
1575
1576                 ivi_set_desktop_surface_fullscreen(surf);
1577
1578                 weston_desktop_surface_set_maximized(dsurf, false);
1579                 weston_desktop_surface_set_fullscreen(dsurf, true);
1580                 weston_desktop_surface_set_size(dsurf,
1581                                                 ivi_output->output->width,
1582                                                 ivi_output->output->height);
1583
1584                 /* add to hidden layer */
1585                 weston_layer_entry_insert(&ivi->hidden.view_list, &ev->layer_link);
1586                 weston_compositor_schedule_repaint(ivi->compositor);
1587         } else if (!surf || (surf && surf->role != IVI_SURFACE_ROLE_FULLSCREEN)) {
1588                 ivi_set_pending_desktop_surface_fullscreen(ivi_output, app_id);
1589         }
1590 }
1591
1592
1593 static void
1594 shell_set_app_normal(struct wl_client *client, struct wl_resource *shell_res,
1595                      const char *app_id)
1596 {
1597         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1598         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1599         struct weston_output *output = get_focused_output(ivi->compositor);
1600         struct ivi_output *ivi_output;
1601         struct weston_desktop_surface *dsurf;
1602         struct weston_geometry area = {};
1603
1604
1605         if (!surf || (surf && surf->role == IVI_SURFACE_ROLE_DESKTOP))
1606                 return;
1607
1608         if (!output)
1609                 output = get_default_output(ivi->compositor);
1610
1611         dsurf = surf->dsurface;
1612         ivi_output = to_ivi_output(output);
1613
1614         weston_layer_entry_remove(&surf->view->layer_link);
1615         weston_view_geometry_dirty(surf->view);
1616         weston_surface_damage(surf->view->surface);
1617
1618         /* change the role */
1619         surf->role = IVI_SURFACE_ROLE_NONE;
1620         surf->state = NORMAL;
1621         surf->desktop.pending_output = ivi_output;
1622
1623         wl_list_remove(&surf->link);
1624         wl_list_init(&surf->link);
1625
1626         ivi_set_desktop_surface(surf);
1627
1628         if (ivi_output->area_activation.width ||
1629             ivi_output->area_activation.height)
1630                 area = ivi_output->area_activation;
1631         else
1632                 area = ivi_output->area;
1633
1634         if (weston_desktop_surface_get_fullscreen(dsurf))
1635                 weston_desktop_surface_set_fullscreen(dsurf, false);
1636
1637         weston_desktop_surface_set_maximized(dsurf, true);
1638         weston_desktop_surface_set_size(dsurf, area.width, area.height);
1639
1640         /* add to hidden layer */
1641         weston_layer_entry_insert(&ivi->hidden.view_list,
1642                                   &surf->view->layer_link);
1643         weston_compositor_schedule_repaint(ivi->compositor);
1644
1645 }
1646
1647 static void
1648 shell_desktop_activate_app(struct wl_client *client,
1649                            struct wl_resource *shell_res,
1650                            const char *app_id, const char *data,
1651                            struct wl_resource *output_res)
1652 {
1653         struct weston_head *head = weston_head_from_resource(output_res);
1654         struct weston_output *woutput = weston_head_get_output(head);
1655         struct ivi_output *output = to_ivi_output(woutput);
1656
1657         ivi_layout_activate(output, app_id);
1658         shell_advertise_app_state(output->ivi, app_id,
1659                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
1660 }
1661
1662 static void
1663 shell_deactivate_app(struct wl_client *client,
1664                    struct wl_resource *shell_res,
1665                    const char *app_id)
1666 {
1667         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1668         struct ivi_compositor *ivi = dclient->ivi;
1669
1670         ivi_layout_deactivate(ivi, app_id);
1671         shell_advertise_app_state(ivi, app_id,
1672                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
1673 }
1674
1675 static void
1676 shell_destroy(struct wl_client *client, struct wl_resource *res)
1677 {
1678         struct  ivi_compositor *ivi = wl_resource_get_user_data(res);
1679
1680         /* reset status in case bind_fail was sent */
1681         if (wl_resource_get_version(res) >= AGL_SHELL_BOUND_OK_SINCE_VERSION &&
1682             ivi->shell_client.status == BOUND_FAILED)
1683                 ivi->shell_client.status = BOUND_OK;
1684 }
1685
1686 static void
1687 shell_set_activate_region(struct wl_client *client, struct wl_resource *res,
1688                          struct wl_resource *output, int x, int y,
1689                          int width, int height)
1690 {
1691        struct ivi_compositor *ivi = wl_resource_get_user_data(res);
1692        struct weston_head *head = weston_head_from_resource(output);
1693        struct weston_output *woutput = weston_head_get_output(head);
1694        struct ivi_output *ioutput = to_ivi_output(woutput);
1695
1696        struct weston_geometry area = { .x = x, .y = y,
1697                                        .width = width,
1698                                        .height = height };
1699        if (ivi->shell_client.ready)
1700                return;
1701
1702        ioutput->area_activation = area;
1703 }
1704
1705 static void
1706 shell_set_app_output(struct wl_client *client, struct wl_resource *res,
1707                     const char *app_id, struct wl_resource *output)
1708 {
1709         struct ivi_compositor *ivi = wl_resource_get_user_data(res);
1710         struct weston_head *head = weston_head_from_resource(output);
1711         struct weston_output *woutput = weston_head_get_output(head);
1712         struct ivi_output *ioutput = to_ivi_output(woutput);
1713         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1714
1715         if (!app_id || !ioutput)
1716                 return;
1717
1718         if (!surf || (surf && surf->role != IVI_SURFACE_ROLE_REMOTE)) {
1719                 ivi_set_pending_desktop_surface_remote(ioutput, app_id);
1720                 shell_send_app_on_output(ivi, app_id, woutput->name);
1721                 return;
1722         }
1723 }
1724
1725 static void
1726 shell_ext_destroy(struct wl_client *client, struct wl_resource *res)
1727 {
1728         wl_resource_destroy(res);
1729 }
1730
1731 static void
1732 shell_ext_doas(struct wl_client *client, struct wl_resource *res)
1733 {
1734         struct  ivi_compositor *ivi = wl_resource_get_user_data(res);
1735
1736         ivi->shell_client_ext.doas_requested = true;
1737         agl_shell_ext_send_doas_done(ivi->shell_client_ext.resource,
1738                                      AGL_SHELL_EXT_DOAS_SHELL_CLIENT_STATUS_SUCCESS);
1739 }
1740
1741 static const struct agl_shell_interface agl_shell_implementation = {
1742         .ready = shell_ready,
1743         .set_background = shell_set_background,
1744         .set_panel = shell_set_panel,
1745         .activate_app = shell_activate_app,
1746         .destroy = shell_destroy,
1747         .set_activate_region = shell_set_activate_region,
1748         .deactivate_app = shell_new_deactivate_app,
1749         .set_app_float = shell_set_app_float,
1750         .set_app_normal = shell_set_app_normal,
1751         .set_app_fullscreen = shell_set_app_fullscreen,
1752         .set_app_output = shell_set_app_output,
1753 };
1754
1755 static const struct agl_shell_ext_interface agl_shell_ext_implementation = {
1756         .destroy = shell_ext_destroy,
1757         .doas_shell_client = shell_ext_doas,
1758 };
1759
1760 static void
1761 shell_desktop_set_app_property(struct wl_client *client,
1762                                struct wl_resource *shell_res,
1763                                const char *app_id, uint32_t role,
1764                                int x, int y, int bx, int by,
1765                                int width, int height,
1766                                struct wl_resource *output_res)
1767 {
1768         struct weston_head *head = weston_head_from_resource(output_res);
1769         struct weston_output *woutput = weston_head_get_output(head);
1770         struct ivi_output *output = to_ivi_output(woutput);
1771
1772         switch (role) {
1773         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
1774                 ivi_set_pending_desktop_surface_popup(output, x, y, bx, by,
1775                                                       width, height, app_id);
1776                 break;
1777         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
1778                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
1779                 break;
1780         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
1781         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
1782                 ivi_set_pending_desktop_surface_split(output, app_id, role);
1783                 break;
1784         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
1785                 ivi_set_pending_desktop_surface_remote(output, app_id);
1786                 break;
1787         default:
1788                 break;
1789         }
1790 }
1791
1792 void
1793 ivi_compositor_destroy_pending_surfaces(struct ivi_compositor *ivi)
1794 {
1795         struct pending_popup *p_popup, *next_p_popup;
1796         struct pending_split *split_surf, *next_split_surf;
1797         struct pending_fullscreen *fs_surf, *next_fs_surf;
1798         struct pending_remote *remote_surf, *next_remote_surf;
1799
1800         wl_list_for_each_safe(p_popup, next_p_popup,
1801                               &ivi->popup_pending_apps, link)
1802                 ivi_remove_pending_desktop_surface_popup(p_popup);
1803
1804         wl_list_for_each_safe(split_surf, next_split_surf,
1805                               &ivi->split_pending_apps, link)
1806                 ivi_remove_pending_desktop_surface_split(split_surf);
1807
1808         wl_list_for_each_safe(fs_surf, next_fs_surf,
1809                               &ivi->fullscreen_pending_apps, link)
1810                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
1811
1812         wl_list_for_each_safe(remote_surf, next_remote_surf,
1813                               &ivi->remote_pending_apps, link)
1814                 ivi_remove_pending_desktop_surface_remote(remote_surf);
1815 }
1816
1817 static void
1818 shell_desktop_set_app_property_mode(struct wl_client *client,
1819                                     struct wl_resource *shell_res, uint32_t perm)
1820 {
1821         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1822         if (perm) {
1823                 dclient->ivi->keep_pending_surfaces = true;
1824         } else {
1825                 dclient->ivi->keep_pending_surfaces = false;
1826                 /* remove any previous pending surfaces */
1827                 ivi_compositor_destroy_pending_surfaces(dclient->ivi);
1828         }
1829 }
1830
1831 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
1832         .activate_app = shell_desktop_activate_app,
1833         .set_app_property = shell_desktop_set_app_property,
1834         .deactivate_app = shell_deactivate_app,
1835         .set_app_property_mode = shell_desktop_set_app_property_mode,
1836 };
1837
1838 static void
1839 unbind_agl_shell(struct wl_resource *resource)
1840 {
1841         struct ivi_compositor *ivi;
1842         struct ivi_output *output;
1843         struct ivi_surface *surf, *surf_tmp;
1844
1845         ivi = wl_resource_get_user_data(resource);
1846
1847         /* reset status to allow other clients issue legit requests */
1848         if (wl_resource_get_version(resource) >=
1849             AGL_SHELL_BOUND_OK_SINCE_VERSION &&
1850             ivi->shell_client.status == BOUND_FAILED) {
1851                 ivi->shell_client.status = BOUND_OK;
1852                 return;
1853         }
1854
1855         wl_list_for_each(output, &ivi->outputs, link) {
1856                 struct weston_geometry area = {};
1857
1858                 /* reset the active surf if there's one present */
1859                 if (output->active) {
1860                         output->active->view->is_mapped = false;
1861                         output->active->view->surface->is_mapped = false;
1862
1863                         weston_layer_entry_remove(&output->active->view->layer_link);
1864                         output->active = NULL;
1865                 }
1866
1867                 output->area_activation = area;
1868                 insert_black_curtain(output);
1869         }
1870
1871         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
1872                 wl_list_remove(&surf->link);
1873                 wl_list_init(&surf->link);
1874         }
1875
1876         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
1877                 wl_list_remove(&surf->link);
1878                 wl_list_init(&surf->link);
1879         }
1880
1881         wl_list_init(&ivi->surfaces);
1882         wl_list_init(&ivi->pending_surfaces);
1883
1884         ivi->shell_client.ready = false;
1885         ivi->shell_client.resource = NULL;
1886         ivi->shell_client.client = NULL;
1887 }
1888
1889 static void
1890 unbind_agl_shell_ext(struct wl_resource *resource)
1891 {
1892         struct ivi_compositor *ivi = wl_resource_get_user_data(resource);
1893
1894         ivi->shell_client_ext.resource = NULL;
1895 }
1896
1897 static void
1898 bind_agl_shell(struct wl_client *client,
1899                void *data, uint32_t version, uint32_t id)
1900 {
1901         struct ivi_compositor *ivi = data;
1902         struct wl_resource *resource;
1903         struct ivi_policy *policy;
1904         void *interface;
1905
1906         policy = ivi->policy;
1907         interface = (void *) &agl_shell_interface;
1908         if (policy && policy->api.shell_bind_interface &&
1909             !policy->api.shell_bind_interface(client, interface)) {
1910                 wl_client_post_implementation_error(client,
1911                                        "client not authorized to use agl_shell");
1912                 return;
1913         }
1914
1915         resource = wl_resource_create(client, &agl_shell_interface, version, id);
1916         if (!resource) {
1917                 wl_client_post_no_memory(client);
1918                 return;
1919         }
1920
1921         if (ivi->shell_client.resource) {
1922                 if (wl_resource_get_version(resource) == 1) {
1923                         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1924                                                "agl_shell has already been bound");
1925                         return;
1926                 }
1927
1928                 if (ivi->shell_client_ext.resource && 
1929                     ivi->shell_client_ext.doas_requested) {
1930
1931                         /* reset status in case client-ext doesn't send an
1932                          * explicit agl_shell_destroy request, see
1933                          * shell_destroy() */
1934                         if (ivi->shell_client.status == BOUND_FAILED)
1935                                 ivi->shell_client.status = BOUND_OK;
1936
1937                         wl_resource_set_implementation(resource, &agl_shell_implementation,
1938                                                        ivi, NULL);
1939                         ivi->shell_client.resource_ext = resource;
1940
1941                         ivi->shell_client_ext.status = BOUND_OK;
1942                         agl_shell_send_bound_ok(ivi->shell_client.resource_ext);
1943
1944                         return;
1945                 } else {
1946                         wl_resource_set_implementation(resource, &agl_shell_implementation,
1947                                                        ivi, NULL);
1948                         agl_shell_send_bound_fail(resource);
1949                         ivi->shell_client.status = BOUND_FAILED;
1950                         return;
1951                 }
1952         }
1953
1954
1955         if (wl_resource_get_version(resource) >=
1956             AGL_SHELL_BOUND_OK_SINCE_VERSION) {
1957                 wl_resource_set_implementation(resource, &agl_shell_implementation,
1958                                                ivi, unbind_agl_shell);
1959                 ivi->shell_client.resource = resource;
1960                 /* if we land here we'll have BOUND_OK by default,
1961                    but still do the assignment */
1962                 ivi->shell_client.status = BOUND_OK;
1963                 agl_shell_send_bound_ok(ivi->shell_client.resource);
1964         } else {
1965                 /* fallback for just version 1 of the protocol */
1966                 wl_resource_set_implementation(resource, &agl_shell_implementation,
1967                                                ivi, unbind_agl_shell);
1968                 ivi->shell_client.resource = resource;
1969         }
1970 }
1971
1972 static void
1973 bind_agl_shell_ext(struct wl_client *client,
1974                    void *data, uint32_t version, uint32_t id)
1975 {
1976         struct ivi_compositor *ivi = data;
1977         struct wl_resource *resource;
1978
1979         resource = wl_resource_create(client, &agl_shell_ext_interface, version, id);
1980         if (!resource) {
1981                 wl_client_post_no_memory(client);
1982                 return;
1983         }
1984
1985         if (ivi->shell_client_ext.resource) {
1986                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1987                                        "agl_shell_ext has already been bound");
1988                 return;
1989         }
1990
1991         wl_resource_set_implementation(resource, &agl_shell_ext_implementation,
1992                                        ivi, unbind_agl_shell_ext);
1993         ivi->shell_client_ext.resource = resource;
1994 }
1995
1996 static void
1997 unbind_agl_shell_desktop(struct wl_resource *resource)
1998 {
1999         struct desktop_client *dclient = wl_resource_get_user_data(resource);
2000
2001         wl_list_remove(&dclient->link);
2002         free(dclient);
2003 }
2004
2005 static void
2006 bind_agl_shell_desktop(struct wl_client *client,
2007                        void *data, uint32_t version, uint32_t id)
2008 {
2009         struct ivi_compositor *ivi = data;
2010         struct wl_resource *resource;
2011         struct ivi_policy *policy;
2012         struct desktop_client *dclient;
2013         void *interface;
2014
2015         policy = ivi->policy;
2016         interface  = (void *) &agl_shell_desktop_interface;
2017         if (policy && policy->api.shell_bind_interface &&
2018             !policy->api.shell_bind_interface(client, interface)) {
2019                 wl_client_post_implementation_error(client,
2020                                 "client not authorized to use agl_shell_desktop");
2021                 return;
2022         }
2023
2024         dclient = zalloc(sizeof(*dclient));
2025         if (!dclient) {
2026                 wl_client_post_no_memory(client);
2027                 return;
2028         }
2029
2030         resource = wl_resource_create(client, &agl_shell_desktop_interface,
2031                                       version, id);
2032         dclient->ivi = ivi;
2033         if (!resource) {
2034                 wl_client_post_no_memory(client);
2035                 return;
2036         }
2037
2038         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
2039                                        dclient, unbind_agl_shell_desktop);
2040
2041         dclient->resource = resource;
2042         wl_list_insert(&ivi->desktop_clients, &dclient->link);
2043
2044         /* advertise xdg surfaces */
2045         ivi_shell_advertise_xdg_surfaces(ivi, resource);
2046 }
2047
2048 int
2049 ivi_shell_create_global(struct ivi_compositor *ivi)
2050 {
2051         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
2052                                           &agl_shell_interface, 8,
2053                                           ivi, bind_agl_shell);
2054         if (!ivi->agl_shell) {
2055                 weston_log("Failed to create wayland global.\n");
2056                 return -1;
2057         }
2058
2059         ivi->agl_shell_ext = wl_global_create(ivi->compositor->wl_display,
2060                                               &agl_shell_ext_interface, 1,
2061                                               ivi, bind_agl_shell_ext);
2062         if (!ivi->agl_shell_ext) {
2063                 weston_log("Failed to create agl_shell_ext global.\n");
2064                 return -1;
2065         }
2066
2067         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
2068                                                   &agl_shell_desktop_interface, 2,
2069                                                   ivi, bind_agl_shell_desktop);
2070         if (!ivi->agl_shell_desktop) {
2071                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
2072                 return -1;
2073         }
2074
2075         return 0;
2076 }