agl-shell: Add support for defining an activation area
[src/agl-compositor.git] / src / shell.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <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)
990 {
991         struct process_info *pinfo;
992         struct weston_config_section *section;
993         char *command = NULL;
994
995         section = weston_config_get_section(ivi->config, "shell-client",
996                                             NULL, NULL);
997         if (section)
998                 weston_config_section_get_string(section, "command",
999                                                  &command, NULL);
1000
1001         if (!command)
1002                 return -1;
1003
1004         pinfo = zalloc(sizeof *pinfo);
1005         if (!pinfo)
1006                 return -1;
1007
1008         pinfo->path = strdup(command);
1009         if (!pinfo->path)
1010                 goto out_free;
1011
1012         ivi->shell_client.client = client_launch(ivi->compositor, &pinfo->proc,
1013                                                  command, process_handle_sigchld);
1014         if (!ivi->shell_client.client)
1015                 goto out_str;
1016
1017         return 0;
1018
1019 out_str:
1020         free(pinfo->path);
1021
1022 out_free:
1023         free(pinfo);
1024
1025         return -1;
1026 }
1027
1028 static void
1029 destroy_black_curtain_view(struct wl_listener *listener, void *data)
1030 {
1031         struct fullscreen_view *fs =
1032                 wl_container_of(listener, fs, fs_destroy);
1033
1034
1035         if (fs && fs->fs) {
1036                 wl_list_remove(&fs->fs_destroy.link);
1037                 free(fs->fs);
1038         }
1039 }
1040
1041
1042 static void
1043 create_black_curtain_view(struct ivi_output *output)
1044 {
1045         struct weston_surface *surface = NULL;
1046         struct weston_view *view;
1047         struct ivi_compositor *ivi = output->ivi;
1048         struct weston_compositor *wc= ivi->compositor;
1049         struct weston_output *woutput = output->output;
1050
1051         if (!woutput)
1052                 return;
1053
1054         surface = weston_surface_create(wc);
1055         if (!surface)
1056                 return;
1057         view = weston_view_create(surface);
1058         if (!view) {
1059                 weston_surface_destroy(surface);
1060                 return;
1061         }
1062
1063         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1064         weston_surface_set_size(surface, woutput->width, woutput->height);
1065         weston_view_set_position(view, woutput->x, woutput->y);
1066
1067         output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
1068         if (!output->fullscreen_view.fs) {
1069                 weston_surface_destroy(surface);
1070                 return;
1071         }
1072         output->fullscreen_view.fs->view = view;
1073
1074         output->fullscreen_view.fs_destroy.notify = destroy_black_curtain_view;
1075         wl_signal_add(&woutput->destroy_signal,
1076                       &output->fullscreen_view.fs_destroy);
1077 }
1078
1079 bool
1080 output_has_black_curtain(struct ivi_output *output)
1081 {
1082         return (output->fullscreen_view.fs->view &&
1083                 output->fullscreen_view.fs->view->is_mapped &&
1084                 output->fullscreen_view.fs->view->surface->is_mapped);
1085 }
1086
1087 void
1088 remove_black_curtain(struct ivi_output *output)
1089 {
1090         struct weston_view *view;
1091
1092         if (!output &&
1093             !output->fullscreen_view.fs &&
1094             !output->fullscreen_view.fs->view) {
1095                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
1096                 return;
1097         }
1098
1099         view = output->fullscreen_view.fs->view;
1100         assert(view->is_mapped == true ||
1101                view->surface->is_mapped == true);
1102
1103         view->is_mapped = false;
1104         view->surface->is_mapped = false;
1105
1106         weston_layer_entry_remove(&view->layer_link);
1107         weston_view_update_transform(view);
1108
1109         weston_view_damage_below(view);
1110
1111         weston_log("Removed black curtain from output %s\n", output->output->name);
1112 }
1113
1114 void
1115 insert_black_curtain(struct ivi_output *output)
1116 {
1117         struct weston_view *view;
1118
1119         if ((!output &&
1120             !output->fullscreen_view.fs &&
1121             !output->fullscreen_view.fs->view) || !output->output) {
1122                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
1123                 return;
1124         }
1125
1126         view = output->fullscreen_view.fs->view;
1127         if (view->is_mapped || view->surface->is_mapped)
1128                 return;
1129
1130         weston_layer_entry_remove(&view->layer_link);
1131         weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
1132                                   &view->layer_link);
1133
1134         view->is_mapped = true;
1135         view->surface->is_mapped = true;
1136
1137         weston_view_update_transform(view);
1138         weston_view_damage_below(view);
1139
1140         weston_log("Added black curtain to output %s\n", output->output->name);
1141 }
1142
1143 static void
1144 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
1145 {
1146         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1147         struct ivi_output *output;
1148         struct ivi_surface *surface, *tmp;
1149
1150         if (ivi->shell_client.resource &&
1151             ivi->shell_client.status == BOUND_FAILED) {
1152                 wl_resource_post_error(shell_res,
1153                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1154                                        "agl_shell has already been bound. "
1155                                        "Check out bound_fail event");
1156                 return;
1157         }
1158
1159         /* Init already finished. Do nothing */
1160         if (ivi->shell_client.ready)
1161                 return;
1162
1163
1164         ivi->shell_client.ready = true;
1165
1166         wl_list_for_each(output, &ivi->outputs, link) {
1167                 if (output->background)
1168                         remove_black_curtain(output);
1169                 ivi_layout_init(ivi, output);
1170         }
1171
1172         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
1173                 const char *app_id;
1174
1175                 wl_list_remove(&surface->link);
1176                 wl_list_init(&surface->link);
1177                 ivi_check_pending_desktop_surface(surface);
1178                 surface->checked_pending = true;
1179                 app_id = weston_desktop_surface_get_app_id(surface->dsurface);
1180
1181                 if (app_id &&
1182                     wl_resource_get_version(ivi->shell_client.resource) >=
1183                     AGL_SHELL_APP_STATE_SINCE_VERSION)
1184                         agl_shell_send_app_state(ivi->shell_client.resource,
1185                                                  app_id, AGL_SHELL_APP_STATE_STARTED);
1186         }
1187 }
1188
1189 static void
1190 shell_set_background(struct wl_client *client,
1191                      struct wl_resource *shell_res,
1192                      struct wl_resource *surface_res,
1193                      struct wl_resource *output_res)
1194 {
1195         struct weston_head *head = weston_head_from_resource(output_res);
1196         struct weston_output *woutput = weston_head_get_output(head);
1197         struct ivi_output *output = to_ivi_output(woutput);
1198         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
1199         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1200         struct weston_desktop_surface *dsurface;
1201         struct ivi_surface *surface;
1202
1203         if (ivi->shell_client.resource &&
1204             ivi->shell_client.status == BOUND_FAILED) {
1205                 wl_resource_post_error(shell_res,
1206                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1207                                        "agl_shell has already been bound. "
1208                                        "Check out bound_fail event");
1209                 return;
1210         }
1211
1212         dsurface = weston_surface_get_desktop_surface(wsurface);
1213         if (!dsurface) {
1214                 wl_resource_post_error(shell_res,
1215                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1216                                        "surface must be a desktop surface");
1217                 return;
1218         }
1219
1220         surface = weston_desktop_surface_get_user_data(dsurface);
1221         if (surface->role != IVI_SURFACE_ROLE_NONE) {
1222                 wl_resource_post_error(shell_res,
1223                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1224                                        "surface already has another ivi role");
1225                 return;
1226         }
1227
1228         if (output->background) {
1229                 wl_resource_post_error(shell_res,
1230                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
1231                                        "output already has background");
1232                 return;
1233         }
1234
1235         surface->checked_pending = true;
1236         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
1237         surface->bg.output = output;
1238         wl_list_remove(&surface->link);
1239         wl_list_init(&surface->link);
1240
1241         output->background = surface;
1242
1243         weston_desktop_surface_set_maximized(dsurface, true);
1244         weston_desktop_surface_set_size(dsurface,
1245                                         output->output->width,
1246                                         output->output->height);
1247 }
1248
1249 static void
1250 shell_set_panel(struct wl_client *client,
1251                 struct wl_resource *shell_res,
1252                 struct wl_resource *surface_res,
1253                 struct wl_resource *output_res,
1254                 uint32_t edge)
1255 {
1256         struct weston_head *head = weston_head_from_resource(output_res);
1257         struct weston_output *woutput = weston_head_get_output(head);
1258         struct ivi_output *output = to_ivi_output(woutput);
1259         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
1260         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1261         struct weston_desktop_surface *dsurface;
1262         struct ivi_surface *surface;
1263         struct ivi_surface **member;
1264         int32_t width = 0, height = 0;
1265
1266         if (ivi->shell_client.resource &&
1267             ivi->shell_client.status == BOUND_FAILED) {
1268                 wl_resource_post_error(shell_res,
1269                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1270                                        "agl_shell has already been bound. "
1271                                        "Check out bound_fail event");
1272                 return;
1273         }
1274
1275         dsurface = weston_surface_get_desktop_surface(wsurface);
1276         if (!dsurface) {
1277                 wl_resource_post_error(shell_res,
1278                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1279                                        "surface must be a desktop surface");
1280                 return;
1281         }
1282
1283         surface = weston_desktop_surface_get_user_data(dsurface);
1284         if (surface->role != IVI_SURFACE_ROLE_NONE) {
1285                 wl_resource_post_error(shell_res,
1286                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1287                                        "surface already has another ivi role");
1288                 return;
1289         }
1290
1291         switch (edge) {
1292         case AGL_SHELL_EDGE_TOP:
1293                 member = &output->top;
1294                 break;
1295         case AGL_SHELL_EDGE_BOTTOM:
1296                 member = &output->bottom;
1297                 break;
1298         case AGL_SHELL_EDGE_LEFT:
1299                 member = &output->left;
1300                 break;
1301         case AGL_SHELL_EDGE_RIGHT:
1302                 member = &output->right;
1303                 break;
1304         default:
1305                 wl_resource_post_error(shell_res,
1306                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
1307                                        "invalid edge for panel");
1308                 return;
1309         }
1310
1311         if (*member) {
1312                 wl_resource_post_error(shell_res,
1313                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
1314                                        "output already has panel on this edge");
1315                 return;
1316         }
1317
1318         surface->checked_pending = true;
1319         surface->role = IVI_SURFACE_ROLE_PANEL;
1320         surface->panel.output = output;
1321         surface->panel.edge = edge;
1322         wl_list_remove(&surface->link);
1323         wl_list_init(&surface->link);
1324
1325         *member = surface;
1326
1327         switch (surface->panel.edge) {
1328         case AGL_SHELL_EDGE_TOP:
1329         case AGL_SHELL_EDGE_BOTTOM:
1330                 width = woutput->width;
1331                 break;
1332         case AGL_SHELL_EDGE_LEFT:
1333         case AGL_SHELL_EDGE_RIGHT:
1334                 height = woutput->height;
1335                 break;
1336         }
1337
1338         weston_desktop_surface_set_size(dsurface, width, height);
1339 }
1340
1341 void
1342 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
1343                           const char *data, uint32_t app_state)
1344 {
1345         struct desktop_client *dclient;
1346         uint32_t app_role;
1347         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
1348         struct ivi_policy *policy = ivi->policy;
1349
1350         /* FIXME: should queue it here and see when binding agl-shell-desktop
1351          * if there are any to be sent */
1352         if (!surf)
1353                 return;
1354
1355         if (!app_id)
1356                 return;
1357
1358         if (policy && policy->api.surface_advertise_state_change &&
1359             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
1360                 return;
1361         }
1362
1363         app_role = surf->role;
1364         if (app_role == IVI_SURFACE_ROLE_POPUP)
1365                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
1366
1367         wl_list_for_each(dclient, &ivi->desktop_clients, link)
1368                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
1369                                                  data, app_state, app_role);
1370 }
1371
1372 static void
1373 shell_activate_app(struct wl_client *client,
1374                    struct wl_resource *shell_res,
1375                    const char *app_id,
1376                    struct wl_resource *output_res)
1377 {
1378         struct weston_head *head = weston_head_from_resource(output_res);
1379         struct weston_output *woutput = weston_head_get_output(head);
1380         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
1381         struct ivi_output *output = to_ivi_output(woutput);
1382
1383         if (ivi->shell_client.resource &&
1384             ivi->shell_client.status == BOUND_FAILED) {
1385                 wl_resource_post_error(shell_res,
1386                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1387                                        "agl_shell has already been bound. "
1388                                        "Check out bound_fail event");
1389                 return;
1390         }
1391
1392         ivi_layout_activate(output, app_id);
1393 }
1394
1395 static void
1396 shell_desktop_activate_app(struct wl_client *client,
1397                            struct wl_resource *shell_res,
1398                            const char *app_id, const char *data,
1399                            struct wl_resource *output_res)
1400 {
1401         struct weston_head *head = weston_head_from_resource(output_res);
1402         struct weston_output *woutput = weston_head_get_output(head);
1403         struct ivi_output *output = to_ivi_output(woutput);
1404
1405         ivi_layout_activate(output, app_id);
1406         shell_advertise_app_state(output->ivi, app_id,
1407                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
1408 }
1409
1410 static void
1411 shell_deactivate_app(struct wl_client *client,
1412                    struct wl_resource *shell_res,
1413                    const char *app_id)
1414 {
1415         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1416         struct ivi_compositor *ivi = dclient->ivi;
1417
1418         ivi_layout_deactivate(ivi, app_id);
1419         shell_advertise_app_state(ivi, app_id,
1420                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
1421 }
1422
1423 /* stub, no usage for the time being */
1424 static void
1425 shell_destroy(struct wl_client *client, struct wl_resource *res)
1426 {
1427 }
1428
1429 static void
1430 shell_set_activate_region(struct wl_client *client, struct wl_resource *res,
1431                          struct wl_resource *output, int x, int y,
1432                          int width, int height)
1433 {
1434        struct ivi_compositor *ivi = wl_resource_get_user_data(res);
1435        struct weston_head *head = weston_head_from_resource(output);
1436        struct weston_output *woutput = weston_head_get_output(head);
1437        struct ivi_output *ioutput = to_ivi_output(woutput);
1438
1439        struct weston_geometry area = { .x = x, .y = y,
1440                                        .width = width,
1441                                        .height = height };
1442        if (ivi->shell_client.ready)
1443                return;
1444
1445        ioutput->area_activation = area;
1446 }
1447
1448
1449 static const struct agl_shell_interface agl_shell_implementation = {
1450         .ready = shell_ready,
1451         .set_background = shell_set_background,
1452         .set_panel = shell_set_panel,
1453         .activate_app = shell_activate_app,
1454         .destroy = shell_destroy,
1455         .set_activate_region = shell_set_activate_region
1456 };
1457
1458 static void
1459 shell_desktop_set_app_property(struct wl_client *client,
1460                                struct wl_resource *shell_res,
1461                                const char *app_id, uint32_t role,
1462                                int x, int y, int bx, int by,
1463                                int width, int height,
1464                                struct wl_resource *output_res)
1465 {
1466         struct weston_head *head = weston_head_from_resource(output_res);
1467         struct weston_output *woutput = weston_head_get_output(head);
1468         struct ivi_output *output = to_ivi_output(woutput);
1469
1470         switch (role) {
1471         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
1472                 ivi_set_pending_desktop_surface_popup(output, x, y, bx, by,
1473                                                       width, height, app_id);
1474                 break;
1475         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
1476                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
1477                 break;
1478         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
1479         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
1480                 ivi_set_pending_desktop_surface_split(output, app_id, role);
1481                 break;
1482         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
1483                 ivi_set_pending_desktop_surface_remote(output, app_id);
1484                 break;
1485         default:
1486                 break;
1487         }
1488 }
1489
1490 void
1491 ivi_compositor_destroy_pending_surfaces(struct ivi_compositor *ivi)
1492 {
1493         struct pending_popup *p_popup, *next_p_popup;
1494         struct pending_split *split_surf, *next_split_surf;
1495         struct pending_fullscreen *fs_surf, *next_fs_surf;
1496         struct pending_remote *remote_surf, *next_remote_surf;
1497
1498         wl_list_for_each_safe(p_popup, next_p_popup,
1499                               &ivi->popup_pending_apps, link)
1500                 ivi_remove_pending_desktop_surface_popup(p_popup);
1501
1502         wl_list_for_each_safe(split_surf, next_split_surf,
1503                               &ivi->split_pending_apps, link)
1504                 ivi_remove_pending_desktop_surface_split(split_surf);
1505
1506         wl_list_for_each_safe(fs_surf, next_fs_surf,
1507                               &ivi->fullscreen_pending_apps, link)
1508                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
1509
1510         wl_list_for_each_safe(remote_surf, next_remote_surf,
1511                               &ivi->remote_pending_apps, link)
1512                 ivi_remove_pending_desktop_surface_remote(remote_surf);
1513 }
1514
1515 static void
1516 shell_desktop_set_app_property_mode(struct wl_client *client,
1517                                     struct wl_resource *shell_res, uint32_t perm)
1518 {
1519         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1520         if (perm) {
1521                 dclient->ivi->keep_pending_surfaces = true;
1522         } else {
1523                 dclient->ivi->keep_pending_surfaces = false;
1524                 /* remove any previous pending surfaces */
1525                 ivi_compositor_destroy_pending_surfaces(dclient->ivi);
1526         }
1527 }
1528
1529 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
1530         .activate_app = shell_desktop_activate_app,
1531         .set_app_property = shell_desktop_set_app_property,
1532         .deactivate_app = shell_deactivate_app,
1533         .set_app_property_mode = shell_desktop_set_app_property_mode,
1534 };
1535
1536 static void
1537 unbind_agl_shell(struct wl_resource *resource)
1538 {
1539         struct ivi_compositor *ivi;
1540         struct ivi_output *output;
1541         struct ivi_surface *surf, *surf_tmp;
1542
1543         ivi = wl_resource_get_user_data(resource);
1544
1545         /* reset status to allow other clients issue legit requests */
1546         if (ivi->shell_client.resource &&
1547             ivi->shell_client.status == BOUND_FAILED) {
1548                 ivi->shell_client.status = BOUND_OK;
1549                 return;
1550         }
1551
1552         wl_list_for_each(output, &ivi->outputs, link) {
1553                 /* reset the active surf if there's one present */
1554                 if (output->active) {
1555                         output->active->view->is_mapped = false;
1556                         output->active->view->surface->is_mapped = false;
1557
1558                         weston_layer_entry_remove(&output->active->view->layer_link);
1559                         output->active = NULL;
1560                 }
1561
1562                 insert_black_curtain(output);
1563         }
1564
1565         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
1566                 wl_list_remove(&surf->link);
1567                 wl_list_init(&surf->link);
1568         }
1569
1570         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
1571                 wl_list_remove(&surf->link);
1572                 wl_list_init(&surf->link);
1573         }
1574
1575         wl_list_init(&ivi->surfaces);
1576         wl_list_init(&ivi->pending_surfaces);
1577
1578         ivi->shell_client.ready = false;
1579         ivi->shell_client.resource = NULL;
1580         ivi->shell_client.client = NULL;
1581 }
1582
1583 static void
1584 bind_agl_shell(struct wl_client *client,
1585                void *data, uint32_t version, uint32_t id)
1586 {
1587         struct ivi_compositor *ivi = data;
1588         struct wl_resource *resource;
1589         struct ivi_policy *policy;
1590         void *interface;
1591
1592         policy = ivi->policy;
1593         interface = (void *) &agl_shell_interface;
1594         if (policy && policy->api.shell_bind_interface &&
1595             !policy->api.shell_bind_interface(client, interface)) {
1596                 wl_client_post_implementation_error(client,
1597                                        "client not authorized to use agl_shell");
1598                 return;
1599         }
1600
1601         resource = wl_resource_create(client, &agl_shell_interface, version, id);
1602         if (!resource) {
1603                 wl_client_post_no_memory(client);
1604                 return;
1605         }
1606
1607         if (ivi->shell_client.resource) {
1608                 if (wl_resource_get_version(resource) == 1) {
1609                         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1610                                                "agl_shell has already been bound");
1611                         return;
1612                 }
1613
1614                 agl_shell_send_bound_fail(resource);
1615                 ivi->shell_client.status = BOUND_FAILED;
1616         }
1617
1618         wl_resource_set_implementation(resource, &agl_shell_implementation,
1619                                        ivi, unbind_agl_shell);
1620         ivi->shell_client.resource = resource;
1621
1622         if (ivi->shell_client.status == BOUND_OK &&
1623             wl_resource_get_version(resource) >= AGL_SHELL_BOUND_OK_SINCE_VERSION)
1624                 agl_shell_send_bound_ok(ivi->shell_client.resource);
1625 }
1626
1627 static void
1628 unbind_agl_shell_desktop(struct wl_resource *resource)
1629 {
1630         struct desktop_client *dclient = wl_resource_get_user_data(resource);
1631
1632         wl_list_remove(&dclient->link);
1633         free(dclient);
1634 }
1635
1636 static void
1637 bind_agl_shell_desktop(struct wl_client *client,
1638                        void *data, uint32_t version, uint32_t id)
1639 {
1640         struct ivi_compositor *ivi = data;
1641         struct wl_resource *resource;
1642         struct ivi_policy *policy;
1643         struct desktop_client *dclient;
1644         void *interface;
1645
1646         policy = ivi->policy;
1647         interface  = (void *) &agl_shell_desktop_interface;
1648         if (policy && policy->api.shell_bind_interface &&
1649             !policy->api.shell_bind_interface(client, interface)) {
1650                 wl_client_post_implementation_error(client,
1651                                 "client not authorized to use agl_shell_desktop");
1652                 return;
1653         }
1654
1655         dclient = zalloc(sizeof(*dclient));
1656         if (!dclient) {
1657                 wl_client_post_no_memory(client);
1658                 return;
1659         }
1660
1661         resource = wl_resource_create(client, &agl_shell_desktop_interface,
1662                                       version, id);
1663         dclient->ivi = ivi;
1664         if (!resource) {
1665                 wl_client_post_no_memory(client);
1666                 return;
1667         }
1668
1669         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
1670                                        dclient, unbind_agl_shell_desktop);
1671
1672         dclient->resource = resource;
1673         wl_list_insert(&ivi->desktop_clients, &dclient->link);
1674
1675         /* advertise xdg surfaces */
1676         ivi_shell_advertise_xdg_surfaces(ivi, resource);
1677 }
1678
1679 int
1680 ivi_shell_create_global(struct ivi_compositor *ivi)
1681 {
1682         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
1683                                           &agl_shell_interface, 4,
1684                                           ivi, bind_agl_shell);
1685         if (!ivi->agl_shell) {
1686                 weston_log("Failed to create wayland global.\n");
1687                 return -1;
1688         }
1689
1690         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
1691                                                   &agl_shell_desktop_interface, 2,
1692                                                   ivi, bind_agl_shell_desktop);
1693         if (!ivi->agl_shell_desktop) {
1694                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
1695                 return -1;
1696         }
1697
1698         return 0;
1699 }