src: Allow to start other clients
[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 static void
1142 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
1143 {
1144         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1145         struct ivi_output *output;
1146         struct ivi_surface *surface, *tmp;
1147
1148         if (ivi->shell_client.resource &&
1149             ivi->shell_client.status == BOUND_FAILED) {
1150                 wl_resource_post_error(shell_res,
1151                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1152                                        "agl_shell has already been bound. "
1153                                        "Check out bound_fail event");
1154                 return;
1155         }
1156
1157         /* Init already finished. Do nothing */
1158         if (ivi->shell_client.ready)
1159                 return;
1160
1161
1162         ivi->shell_client.ready = true;
1163
1164         wl_list_for_each(output, &ivi->outputs, link) {
1165                 if (output->background)
1166                         remove_black_curtain(output);
1167                 ivi_layout_init(ivi, output);
1168         }
1169
1170         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
1171                 const char *app_id;
1172
1173                 wl_list_remove(&surface->link);
1174                 wl_list_init(&surface->link);
1175                 ivi_check_pending_desktop_surface(surface);
1176                 surface->checked_pending = true;
1177                 app_id = weston_desktop_surface_get_app_id(surface->dsurface);
1178
1179                 if (app_id &&
1180                     wl_resource_get_version(ivi->shell_client.resource) >=
1181                     AGL_SHELL_APP_STATE_SINCE_VERSION)
1182                         agl_shell_send_app_state(ivi->shell_client.resource,
1183                                                  app_id, AGL_SHELL_APP_STATE_STARTED);
1184         }
1185 }
1186
1187 static void
1188 shell_set_background(struct wl_client *client,
1189                      struct wl_resource *shell_res,
1190                      struct wl_resource *surface_res,
1191                      struct wl_resource *output_res)
1192 {
1193         struct weston_head *head = weston_head_from_resource(output_res);
1194         struct weston_output *woutput = weston_head_get_output(head);
1195         struct ivi_output *output = to_ivi_output(woutput);
1196         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
1197         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1198         struct weston_desktop_surface *dsurface;
1199         struct ivi_surface *surface;
1200
1201         if (ivi->shell_client.resource &&
1202             ivi->shell_client.status == BOUND_FAILED) {
1203                 wl_resource_post_error(shell_res,
1204                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1205                                        "agl_shell has already been bound. "
1206                                        "Check out bound_fail event");
1207                 return;
1208         }
1209
1210         dsurface = weston_surface_get_desktop_surface(wsurface);
1211         if (!dsurface) {
1212                 wl_resource_post_error(shell_res,
1213                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1214                                        "surface must be a desktop surface");
1215                 return;
1216         }
1217
1218         surface = weston_desktop_surface_get_user_data(dsurface);
1219         if (surface->role != IVI_SURFACE_ROLE_NONE) {
1220                 wl_resource_post_error(shell_res,
1221                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1222                                        "surface already has another ivi role");
1223                 return;
1224         }
1225
1226         if (output->background) {
1227                 wl_resource_post_error(shell_res,
1228                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
1229                                        "output already has background");
1230                 return;
1231         }
1232
1233         surface->checked_pending = true;
1234         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
1235         surface->bg.output = output;
1236         wl_list_remove(&surface->link);
1237         wl_list_init(&surface->link);
1238
1239         output->background = surface;
1240
1241         weston_desktop_surface_set_maximized(dsurface, true);
1242         weston_desktop_surface_set_size(dsurface,
1243                                         output->output->width,
1244                                         output->output->height);
1245 }
1246
1247 static void
1248 shell_set_panel(struct wl_client *client,
1249                 struct wl_resource *shell_res,
1250                 struct wl_resource *surface_res,
1251                 struct wl_resource *output_res,
1252                 uint32_t edge)
1253 {
1254         struct weston_head *head = weston_head_from_resource(output_res);
1255         struct weston_output *woutput = weston_head_get_output(head);
1256         struct ivi_output *output = to_ivi_output(woutput);
1257         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
1258         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1259         struct weston_desktop_surface *dsurface;
1260         struct ivi_surface *surface;
1261         struct ivi_surface **member;
1262         int32_t width = 0, height = 0;
1263
1264         if (ivi->shell_client.resource &&
1265             ivi->shell_client.status == BOUND_FAILED) {
1266                 wl_resource_post_error(shell_res,
1267                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1268                                        "agl_shell has already been bound. "
1269                                        "Check out bound_fail event");
1270                 return;
1271         }
1272
1273         dsurface = weston_surface_get_desktop_surface(wsurface);
1274         if (!dsurface) {
1275                 wl_resource_post_error(shell_res,
1276                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1277                                        "surface must be a desktop surface");
1278                 return;
1279         }
1280
1281         surface = weston_desktop_surface_get_user_data(dsurface);
1282         if (surface->role != IVI_SURFACE_ROLE_NONE) {
1283                 wl_resource_post_error(shell_res,
1284                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1285                                        "surface already has another ivi role");
1286                 return;
1287         }
1288
1289         switch (edge) {
1290         case AGL_SHELL_EDGE_TOP:
1291                 member = &output->top;
1292                 break;
1293         case AGL_SHELL_EDGE_BOTTOM:
1294                 member = &output->bottom;
1295                 break;
1296         case AGL_SHELL_EDGE_LEFT:
1297                 member = &output->left;
1298                 break;
1299         case AGL_SHELL_EDGE_RIGHT:
1300                 member = &output->right;
1301                 break;
1302         default:
1303                 wl_resource_post_error(shell_res,
1304                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1305                                        "invalid edge for panel");
1306                 return;
1307         }
1308
1309         if (*member) {
1310                 wl_resource_post_error(shell_res,
1311                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
1312                                        "output already has panel on this edge");
1313                 return;
1314         }
1315
1316         surface->checked_pending = true;
1317         surface->role = IVI_SURFACE_ROLE_PANEL;
1318         surface->panel.output = output;
1319         surface->panel.edge = edge;
1320         wl_list_remove(&surface->link);
1321         wl_list_init(&surface->link);
1322
1323         *member = surface;
1324
1325         switch (surface->panel.edge) {
1326         case AGL_SHELL_EDGE_TOP:
1327         case AGL_SHELL_EDGE_BOTTOM:
1328                 width = woutput->width;
1329                 break;
1330         case AGL_SHELL_EDGE_LEFT:
1331         case AGL_SHELL_EDGE_RIGHT:
1332                 height = woutput->height;
1333                 break;
1334         }
1335
1336         weston_desktop_surface_set_size(dsurface, width, height);
1337 }
1338
1339 void
1340 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
1341                           const char *data, uint32_t app_state)
1342 {
1343         struct desktop_client *dclient;
1344         uint32_t app_role;
1345         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1346         struct ivi_policy *policy = ivi->policy;
1347
1348         /* FIXME: should queue it here and see when binding agl-shell-desktop
1349          * if there are any to be sent */
1350         if (!surf)
1351                 return;
1352
1353         if (!app_id)
1354                 return;
1355
1356         if (policy && policy->api.surface_advertise_state_change &&
1357             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
1358                 return;
1359         }
1360
1361         app_role = surf->role;
1362         if (app_role == IVI_SURFACE_ROLE_POPUP)
1363                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
1364
1365         wl_list_for_each(dclient, &ivi->desktop_clients, link)
1366                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
1367                                                  data, app_state, app_role);
1368 }
1369
1370 static void
1371 shell_activate_app(struct wl_client *client,
1372                    struct wl_resource *shell_res,
1373                    const char *app_id,
1374                    struct wl_resource *output_res)
1375 {
1376         struct weston_head *head = weston_head_from_resource(output_res);
1377         struct weston_output *woutput = weston_head_get_output(head);
1378         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1379         struct ivi_output *output = to_ivi_output(woutput);
1380
1381         if (ivi->shell_client.resource &&
1382             ivi->shell_client.status == BOUND_FAILED) {
1383                 wl_resource_post_error(shell_res,
1384                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1385                                        "agl_shell has already been bound. "
1386                                        "Check out bound_fail event");
1387                 return;
1388         }
1389
1390         ivi_layout_activate(output, app_id);
1391 }
1392
1393 static void
1394 shell_desktop_activate_app(struct wl_client *client,
1395                            struct wl_resource *shell_res,
1396                            const char *app_id, const char *data,
1397                            struct wl_resource *output_res)
1398 {
1399         struct weston_head *head = weston_head_from_resource(output_res);
1400         struct weston_output *woutput = weston_head_get_output(head);
1401         struct ivi_output *output = to_ivi_output(woutput);
1402
1403         ivi_layout_activate(output, app_id);
1404         shell_advertise_app_state(output->ivi, app_id,
1405                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
1406 }
1407
1408 static void
1409 shell_deactivate_app(struct wl_client *client,
1410                    struct wl_resource *shell_res,
1411                    const char *app_id)
1412 {
1413         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1414         struct ivi_compositor *ivi = dclient->ivi;
1415
1416         ivi_layout_deactivate(ivi, app_id);
1417         shell_advertise_app_state(ivi, app_id,
1418                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
1419 }
1420
1421 /* stub, no usage for the time being */
1422 static void
1423 shell_destroy(struct wl_client *client, struct wl_resource *res)
1424 {
1425 }
1426
1427 static void
1428 shell_ext_destroy(struct wl_client *client, struct wl_resource *res)
1429 {
1430         wl_resource_destroy(res);
1431 }
1432
1433 static void
1434 shell_ext_doas(struct wl_client *client, struct wl_resource *res)
1435 {
1436         struct  ivi_compositor *ivi = wl_resource_get_user_data(res);
1437
1438         ivi->shell_client_ext.doas_requested = true;
1439         agl_shell_ext_send_doas_done(ivi->shell_client_ext.resource,
1440                                      AGL_SHELL_EXT_DOAS_SHELL_CLIENT_STATUS_SUCCESS);
1441 }
1442
1443 static const struct agl_shell_interface agl_shell_implementation = {
1444         .destroy = shell_destroy,
1445         .ready = shell_ready,
1446         .set_background = shell_set_background,
1447         .set_panel = shell_set_panel,
1448         .activate_app = shell_activate_app,
1449 };
1450
1451 static const struct agl_shell_ext_interface agl_shell_ext_implementation = {
1452         .destroy = shell_ext_destroy,
1453         .doas_shell_client = shell_ext_doas,
1454 };
1455
1456 static void
1457 shell_desktop_set_app_property(struct wl_client *client,
1458                                struct wl_resource *shell_res,
1459                                const char *app_id, uint32_t role,
1460                                int x, int y, int bx, int by,
1461                                int width, int height,
1462                                struct wl_resource *output_res)
1463 {
1464         struct weston_head *head = weston_head_from_resource(output_res);
1465         struct weston_output *woutput = weston_head_get_output(head);
1466         struct ivi_output *output = to_ivi_output(woutput);
1467
1468         switch (role) {
1469         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
1470                 ivi_set_pending_desktop_surface_popup(output, x, y, bx, by,
1471                                                       width, height, app_id);
1472                 break;
1473         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
1474                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
1475                 break;
1476         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
1477         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
1478                 ivi_set_pending_desktop_surface_split(output, app_id, role);
1479                 break;
1480         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
1481                 ivi_set_pending_desktop_surface_remote(output, app_id);
1482                 break;
1483         default:
1484                 break;
1485         }
1486 }
1487
1488 void
1489 ivi_compositor_destroy_pending_surfaces(struct ivi_compositor *ivi)
1490 {
1491         struct pending_popup *p_popup, *next_p_popup;
1492         struct pending_split *split_surf, *next_split_surf;
1493         struct pending_fullscreen *fs_surf, *next_fs_surf;
1494         struct pending_remote *remote_surf, *next_remote_surf;
1495
1496         wl_list_for_each_safe(p_popup, next_p_popup,
1497                               &ivi->popup_pending_apps, link)
1498                 ivi_remove_pending_desktop_surface_popup(p_popup);
1499
1500         wl_list_for_each_safe(split_surf, next_split_surf,
1501                               &ivi->split_pending_apps, link)
1502                 ivi_remove_pending_desktop_surface_split(split_surf);
1503
1504         wl_list_for_each_safe(fs_surf, next_fs_surf,
1505                               &ivi->fullscreen_pending_apps, link)
1506                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
1507
1508         wl_list_for_each_safe(remote_surf, next_remote_surf,
1509                               &ivi->remote_pending_apps, link)
1510                 ivi_remove_pending_desktop_surface_remote(remote_surf);
1511 }
1512
1513 static void
1514 shell_desktop_set_app_property_mode(struct wl_client *client,
1515                                     struct wl_resource *shell_res, uint32_t perm)
1516 {
1517         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1518         if (perm) {
1519                 dclient->ivi->keep_pending_surfaces = true;
1520         } else {
1521                 dclient->ivi->keep_pending_surfaces = false;
1522                 /* remove any previous pending surfaces */
1523                 ivi_compositor_destroy_pending_surfaces(dclient->ivi);
1524         }
1525 }
1526
1527 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
1528         .activate_app = shell_desktop_activate_app,
1529         .set_app_property = shell_desktop_set_app_property,
1530         .deactivate_app = shell_deactivate_app,
1531         .set_app_property_mode = shell_desktop_set_app_property_mode,
1532 };
1533
1534 static void
1535 unbind_agl_shell(struct wl_resource *resource)
1536 {
1537         struct ivi_compositor *ivi;
1538         struct ivi_output *output;
1539         struct ivi_surface *surf, *surf_tmp;
1540
1541         ivi = wl_resource_get_user_data(resource);
1542
1543         /* reset status to allow other clients issue legit requests */
1544         if (ivi->shell_client.resource &&
1545             ivi->shell_client.status == BOUND_FAILED) {
1546                 ivi->shell_client.status = BOUND_OK;
1547                 return;
1548         }
1549
1550         wl_list_for_each(output, &ivi->outputs, link) {
1551                 /* reset the active surf if there's one present */
1552                 if (output->active) {
1553                         output->active->view->is_mapped = false;
1554                         output->active->view->surface->is_mapped = false;
1555
1556                         weston_layer_entry_remove(&output->active->view->layer_link);
1557                         output->active = NULL;
1558                 }
1559
1560                 insert_black_curtain(output);
1561         }
1562
1563         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
1564                 wl_list_remove(&surf->link);
1565                 wl_list_init(&surf->link);
1566         }
1567
1568         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
1569                 wl_list_remove(&surf->link);
1570                 wl_list_init(&surf->link);
1571         }
1572
1573         wl_list_init(&ivi->surfaces);
1574         wl_list_init(&ivi->pending_surfaces);
1575
1576         ivi->shell_client.ready = false;
1577         ivi->shell_client.resource = NULL;
1578         ivi->shell_client.client = NULL;
1579 }
1580
1581 static void
1582 unbind_agl_shell_ext(struct wl_resource *resource)
1583 {
1584         struct ivi_compositor *ivi = wl_resource_get_user_data(resource);
1585
1586         ivi->shell_client_ext.resource = NULL;
1587 }
1588
1589 static void
1590 bind_agl_shell(struct wl_client *client,
1591                void *data, uint32_t version, uint32_t id)
1592 {
1593         struct ivi_compositor *ivi = data;
1594         struct wl_resource *resource;
1595         struct ivi_policy *policy;
1596         void *interface;
1597
1598         policy = ivi->policy;
1599         interface = (void *) &agl_shell_interface;
1600         if (policy && policy->api.shell_bind_interface &&
1601             !policy->api.shell_bind_interface(client, interface)) {
1602                 wl_client_post_implementation_error(client,
1603                                        "client not authorized to use agl_shell");
1604                 return;
1605         }
1606
1607         resource = wl_resource_create(client, &agl_shell_interface, version, id);
1608         if (!resource) {
1609                 wl_client_post_no_memory(client);
1610                 return;
1611         }
1612
1613         if (ivi->shell_client.resource) {
1614                 if (wl_resource_get_version(resource) == 1) {
1615                         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1616                                                "agl_shell has already been bound");
1617                         return;
1618                 }
1619
1620                 if (ivi->shell_client_ext.resource && 
1621                     ivi->shell_client_ext.doas_requested) {
1622
1623                         wl_resource_set_implementation(resource, &agl_shell_implementation,
1624                                                        ivi, NULL);
1625                         ivi->shell_client_ext.resource = resource;
1626
1627                         if (ivi->shell_client.status == BOUND_OK &&
1628                             wl_resource_get_version(resource) >= AGL_SHELL_BOUND_OK_SINCE_VERSION) {
1629                                 weston_log("Sent agl_shell_send_bound_ok to client ext\n");
1630                                 ivi->shell_client_ext.status = BOUND_OK;
1631                                 agl_shell_send_bound_ok(ivi->shell_client_ext.resource);
1632                         }
1633
1634                         return;
1635                 } else {
1636                         agl_shell_send_bound_fail(resource);
1637                         ivi->shell_client.status = BOUND_FAILED;
1638                 }
1639         }
1640
1641         wl_resource_set_implementation(resource, &agl_shell_implementation,
1642                                        ivi, unbind_agl_shell);
1643         ivi->shell_client.resource = resource;
1644
1645         if (ivi->shell_client.status == BOUND_OK &&
1646             wl_resource_get_version(resource) >= AGL_SHELL_BOUND_OK_SINCE_VERSION)
1647                 agl_shell_send_bound_ok(ivi->shell_client.resource);
1648 }
1649
1650 static void
1651 bind_agl_shell_ext(struct wl_client *client,
1652                    void *data, uint32_t version, uint32_t id)
1653 {
1654         struct ivi_compositor *ivi = data;
1655         struct wl_resource *resource;
1656
1657         resource = wl_resource_create(client, &agl_shell_ext_interface, version, id);
1658         if (!resource) {
1659                 wl_client_post_no_memory(client);
1660                 return;
1661         }
1662
1663         if (ivi->shell_client_ext.resource) {
1664                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1665                                        "agl_shell_ext has already been bound");
1666                 return;
1667         }
1668
1669         wl_resource_set_implementation(resource, &agl_shell_ext_implementation,
1670                                        ivi, unbind_agl_shell_ext);
1671         ivi->shell_client_ext.resource = resource;
1672 }
1673
1674 static void
1675 unbind_agl_shell_desktop(struct wl_resource *resource)
1676 {
1677         struct desktop_client *dclient = wl_resource_get_user_data(resource);
1678
1679         wl_list_remove(&dclient->link);
1680         free(dclient);
1681 }
1682
1683 static void
1684 bind_agl_shell_desktop(struct wl_client *client,
1685                        void *data, uint32_t version, uint32_t id)
1686 {
1687         struct ivi_compositor *ivi = data;
1688         struct wl_resource *resource;
1689         struct ivi_policy *policy;
1690         struct desktop_client *dclient;
1691         void *interface;
1692
1693         policy = ivi->policy;
1694         interface  = (void *) &agl_shell_desktop_interface;
1695         if (policy && policy->api.shell_bind_interface &&
1696             !policy->api.shell_bind_interface(client, interface)) {
1697                 wl_client_post_implementation_error(client,
1698                                 "client not authorized to use agl_shell_desktop");
1699                 return;
1700         }
1701
1702         dclient = zalloc(sizeof(*dclient));
1703         if (!dclient) {
1704                 wl_client_post_no_memory(client);
1705                 return;
1706         }
1707
1708         resource = wl_resource_create(client, &agl_shell_desktop_interface,
1709                                       version, id);
1710         dclient->ivi = ivi;
1711         if (!resource) {
1712                 wl_client_post_no_memory(client);
1713                 return;
1714         }
1715
1716         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
1717                                        dclient, unbind_agl_shell_desktop);
1718
1719         dclient->resource = resource;
1720         wl_list_insert(&ivi->desktop_clients, &dclient->link);
1721
1722         /* advertise xdg surfaces */
1723         ivi_shell_advertise_xdg_surfaces(ivi, resource);
1724 }
1725
1726 int
1727 ivi_shell_create_global(struct ivi_compositor *ivi)
1728 {
1729         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
1730                                           &agl_shell_interface, 3,
1731                                           ivi, bind_agl_shell);
1732         if (!ivi->agl_shell) {
1733                 weston_log("Failed to create wayland global.\n");
1734                 return -1;
1735         }
1736
1737         ivi->agl_shell_ext = wl_global_create(ivi->compositor->wl_display,
1738                                               &agl_shell_ext_interface, 1,
1739                                               ivi, bind_agl_shell_ext);
1740         if (!ivi->agl_shell_ext) {
1741                 weston_log("Failed to create agl_shell_ext global.\n");
1742                 return -1;
1743         }
1744
1745         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
1746                                                   &agl_shell_desktop_interface, 2,
1747                                                   ivi, bind_agl_shell_desktop);
1748         if (!ivi->agl_shell_desktop) {
1749                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
1750                 return -1;
1751         }
1752
1753         return 0;
1754 }