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