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