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