shell: Add some extra checks to make sure we don't trip ourselves
[src/agl-compositor.git] / src / shell.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <libweston/libweston.h>
38 #include <libweston/config-parser.h>
39
40 #include "shared/os-compatibility.h"
41
42 #include "agl-shell-server-protocol.h"
43 #include "agl-shell-desktop-server-protocol.h"
44
45 static void
46 create_black_surface_view(struct ivi_output *output);
47
48 void
49 ivi_set_desktop_surface(struct ivi_surface *surface)
50 {
51         struct desktop_client *dclient;
52         struct ivi_compositor *ivi = surface->ivi;
53         assert(surface->role == IVI_SURFACE_ROLE_NONE);
54
55         surface->role = IVI_SURFACE_ROLE_DESKTOP;
56         wl_list_insert(&surface->ivi->surfaces, &surface->link);
57
58         /* advertise to all desktop clients the new surface */
59         wl_list_for_each(dclient, &ivi->desktop_clients, link) {
60                 const char *app_id =
61                         weston_desktop_surface_get_app_id(surface->dsurface);
62                 agl_shell_desktop_send_application(dclient->resource, app_id);
63         }
64 }
65
66 static void
67 ivi_set_desktop_surface_popup(struct ivi_surface *surface)
68 {
69         struct ivi_compositor *ivi = surface->ivi;
70         assert(surface->role == IVI_SURFACE_ROLE_NONE);
71
72         surface->role = IVI_SURFACE_ROLE_POPUP;
73         wl_list_insert(&ivi->surfaces, &surface->link);
74 }
75
76 static void
77 ivi_set_desktop_surface_fullscreen(struct ivi_surface *surface)
78 {
79         struct ivi_compositor *ivi = surface->ivi;
80         assert(surface->role == IVI_SURFACE_ROLE_NONE);
81
82         surface->role = IVI_SURFACE_ROLE_FULLSCREEN;
83         wl_list_insert(&ivi->surfaces, &surface->link);
84 }
85
86 static void
87 ivi_set_desktop_surface_remote(struct ivi_surface *surface)
88 {
89         struct ivi_compositor *ivi = surface->ivi;
90         struct weston_view *view;
91         struct ivi_output *output = surface->remote.output;
92
93         assert(surface->role == IVI_SURFACE_ROLE_NONE);
94
95         /* remote type are the same as desktop just that client can tell
96          * the compositor to start on another output */
97         surface->role = IVI_SURFACE_ROLE_REMOTE;
98
99         /* if thew black surface view is mapped on the mean we need
100          * to remove it in order to start showing the 'remote' surface
101          * just being added */
102         view = output->fullscreen_view.fs->view;
103         if (view->is_mapped || view->surface->is_mapped)
104                 remove_black_surface(output);
105
106         wl_list_insert(&ivi->surfaces, &surface->link);
107 }
108
109
110 static void
111 ivi_set_desktop_surface_split(struct ivi_surface *surface)
112 {
113         struct ivi_compositor *ivi = surface->ivi;
114         assert(surface->role == IVI_SURFACE_ROLE_NONE);
115
116         if (surface->split.orientation == AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL)
117                 surface->role = IVI_SURFACE_ROLE_SPLIT_V;
118         else
119                 surface->role = IVI_SURFACE_ROLE_SPLIT_H;
120
121         wl_list_insert(&ivi->surfaces, &surface->link);
122 }
123
124 static void
125 ivi_set_pending_desktop_surface_popup(struct ivi_output *ioutput,
126                                       int x, int y, const char *app_id)
127 {
128         struct ivi_compositor *ivi = ioutput->ivi;
129         size_t len_app_id = strlen(app_id);
130
131         struct pending_popup *p_popup = zalloc(sizeof(*p_popup));
132
133         p_popup->app_id = zalloc(sizeof(char) * (len_app_id + 1));
134         memcpy(p_popup->app_id, app_id, len_app_id);
135         p_popup->ioutput = ioutput;
136         p_popup->x = x;
137         p_popup->y = y;
138
139         wl_list_insert(&ivi->popup_pending_apps, &p_popup->link);
140 }
141
142 static void
143 ivi_set_pending_desktop_surface_fullscreen(struct ivi_output *ioutput,
144                                            const char *app_id)
145 {
146         struct ivi_compositor *ivi = ioutput->ivi;
147         size_t len_app_id = strlen(app_id);
148
149         struct pending_fullscreen *fs = zalloc(sizeof(*fs));
150
151         fs->app_id = zalloc(sizeof(char) * (len_app_id + 1));
152         memcpy(fs->app_id, app_id, len_app_id);
153
154         fs->ioutput = ioutput;
155
156         wl_list_insert(&ivi->fullscreen_pending_apps, &fs->link);
157 }
158
159 static void
160 ivi_set_pending_desktop_surface_split(struct ivi_output *ioutput,
161                                       const char *app_id, uint32_t orientation)
162 {
163         struct ivi_compositor *ivi = ioutput->ivi;
164         struct ivi_surface *surf;
165         size_t len_app_id = strlen(app_id);
166         struct pending_split *split;
167
168         if (orientation != AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL &&
169             orientation != AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL)
170                 return;
171
172         /* more than one is un-supported, do note we need to do
173          * conversion for surface roles instead of using the protocol ones */
174         wl_list_for_each(surf, &ivi->surfaces, link)
175                 if (surf->role == IVI_SURFACE_ROLE_SPLIT_V ||
176                     surf->role == IVI_SURFACE_ROLE_SPLIT_H)
177                         return;
178
179         split = zalloc(sizeof(*split));
180         split->app_id = zalloc(sizeof(char) * (len_app_id + 1));
181         memcpy(split->app_id, app_id, len_app_id);
182
183         split->ioutput = ioutput;
184         split->orientation = orientation;
185
186         wl_list_insert(&ivi->split_pending_apps, &split->link);
187 }
188
189 static void
190 ivi_set_pending_desktop_surface_remote(struct ivi_output *ioutput,
191                 const char *app_id)
192 {
193         struct ivi_compositor *ivi = ioutput->ivi;
194         size_t len_app_id = strlen(app_id);
195
196         struct pending_remote *remote = zalloc(sizeof(*remote));
197
198         remote->app_id = zalloc(sizeof(char) * (len_app_id + 1));
199         memcpy(remote->app_id, app_id, len_app_id);
200
201         remote->ioutput = ioutput;
202
203         wl_list_insert(&ivi->remote_pending_apps, &remote->link);
204 }
205
206
207 static void
208 ivi_remove_pending_desktop_surface_split(struct pending_split *split)
209 {
210         free(split->app_id);
211         wl_list_remove(&split->link);
212         free(split);
213 }
214
215 static void
216 ivi_remove_pending_desktop_surface_fullscreen(struct pending_fullscreen *fs)
217 {
218         free(fs->app_id);
219         wl_list_remove(&fs->link);
220         free(fs);
221 }
222
223 static void
224 ivi_remove_pending_desktop_surface_popup(struct pending_popup *p_popup)
225 {
226         free(p_popup->app_id);
227         wl_list_remove(&p_popup->link);
228         free(p_popup);
229 }
230
231 static void
232 ivi_remove_pending_desktop_surface_remote(struct pending_remote *remote)
233 {
234         free(remote->app_id);
235         wl_list_remove(&remote->link);
236         free(remote);
237 }
238
239 static bool
240 ivi_check_pending_desktop_surface_popup(struct ivi_surface *surface)
241 {
242         struct ivi_compositor *ivi = surface->ivi;
243         struct pending_popup *p_popup, *next_p_popup;
244         const char *_app_id =
245                         weston_desktop_surface_get_app_id(surface->dsurface);
246
247         if (wl_list_empty(&ivi->popup_pending_apps))
248                 return false;
249
250         wl_list_for_each_safe(p_popup, next_p_popup,
251                               &ivi->popup_pending_apps, link) {
252                 if (!strcmp(_app_id, p_popup->app_id)) {
253                         surface->popup.output = p_popup->ioutput;
254                         surface->popup.x = p_popup->x;
255                         surface->popup.y = p_popup->y;
256                         ivi_remove_pending_desktop_surface_popup(p_popup);
257                         return true;
258                 }
259         }
260
261         return false;
262 }
263
264 static bool
265 ivi_check_pending_desktop_surface_split(struct ivi_surface *surface)
266 {
267         struct pending_split *split_surf, *next_split_surf;
268         struct ivi_compositor *ivi = surface->ivi;
269         const char *_app_id =
270                         weston_desktop_surface_get_app_id(surface->dsurface);
271
272         if (wl_list_empty(&ivi->split_pending_apps))
273                 return false;
274
275         wl_list_for_each_safe(split_surf, next_split_surf,
276                               &ivi->split_pending_apps, link) {
277                 if (!strcmp(_app_id, split_surf->app_id)) {
278                         surface->split.output = split_surf->ioutput;
279                         surface->split.orientation = split_surf->orientation;
280                         ivi_remove_pending_desktop_surface_split(split_surf);
281                         return true;
282                 }
283         }
284
285         return false;
286 }
287
288 static bool
289 ivi_check_pending_desktop_surface_fullscreen(struct ivi_surface *surface)
290 {
291         struct pending_fullscreen *fs_surf, *next_fs_surf;
292         struct ivi_compositor *ivi = surface->ivi;
293         const char *_app_id =
294                         weston_desktop_surface_get_app_id(surface->dsurface);
295
296         if (wl_list_empty(&ivi->fullscreen_pending_apps))
297                 return false;
298
299         wl_list_for_each_safe(fs_surf, next_fs_surf,
300                               &ivi->fullscreen_pending_apps, link) {
301                 if (!strcmp(_app_id, fs_surf->app_id)) {
302                         surface->fullscreen.output = fs_surf->ioutput;
303                         ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
304                         return true;
305                 }
306         }
307
308         return false;
309 }
310
311 static bool
312 ivi_check_pending_desktop_surface_remote(struct ivi_surface *surface)
313 {
314         struct pending_remote *remote_surf, *next_remote_surf;
315         struct ivi_compositor *ivi = surface->ivi;
316         const char *_app_id =
317                 weston_desktop_surface_get_app_id(surface->dsurface);
318
319         if (wl_list_empty(&ivi->remote_pending_apps))
320                 return false;
321
322         wl_list_for_each_safe(remote_surf, next_remote_surf,
323                               &ivi->remote_pending_apps, link) {
324                 if (!strcmp(_app_id, remote_surf->app_id)) {
325                         surface->remote.output = remote_surf->ioutput;
326                         ivi_remove_pending_desktop_surface_remote(remote_surf);
327                         return true;
328                 }
329         }
330
331         return false;
332 }
333
334
335 void
336 ivi_check_pending_desktop_surface(struct ivi_surface *surface)
337 {
338         bool ret = false;
339
340         ret = ivi_check_pending_desktop_surface_popup(surface);
341         if (ret) {
342                 ivi_set_desktop_surface_popup(surface);
343                 return;
344         }
345
346         ret = ivi_check_pending_desktop_surface_split(surface);
347         if (ret) {
348                 ivi_set_desktop_surface_split(surface);
349                 return;
350         }
351
352         ret = ivi_check_pending_desktop_surface_fullscreen(surface);
353         if (ret) {
354                 ivi_set_desktop_surface_fullscreen(surface);
355                 return;
356         }
357
358         ret = ivi_check_pending_desktop_surface_remote(surface);
359         if (ret) {
360                 ivi_set_desktop_surface_remote(surface);
361                 return;
362         }
363
364         /* if we end up here means we have a regular desktop app and
365          * try to activate it */
366         ivi_set_desktop_surface(surface);
367         ivi_layout_desktop_committed(surface);
368 }
369
370 void
371 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
372 {
373         struct ivi_output *out;
374
375         wl_list_for_each(out, &ivi->outputs, link) {
376                 create_black_surface_view(out);
377                 insert_black_surface(out);
378         }
379 }
380
381 int
382 ivi_shell_init(struct ivi_compositor *ivi)
383 {
384         weston_layer_init(&ivi->hidden, ivi->compositor);
385         weston_layer_init(&ivi->background, ivi->compositor);
386         weston_layer_init(&ivi->normal, ivi->compositor);
387         weston_layer_init(&ivi->panel, ivi->compositor);
388         weston_layer_init(&ivi->popup, ivi->compositor);
389         weston_layer_init(&ivi->fullscreen, ivi->compositor);
390
391         weston_layer_set_position(&ivi->hidden,
392                                   WESTON_LAYER_POSITION_HIDDEN);
393         weston_layer_set_position(&ivi->background,
394                                   WESTON_LAYER_POSITION_BACKGROUND);
395         weston_layer_set_position(&ivi->normal,
396                                   WESTON_LAYER_POSITION_NORMAL);
397         weston_layer_set_position(&ivi->panel,
398                                   WESTON_LAYER_POSITION_UI);
399         weston_layer_set_position(&ivi->popup,
400                                   WESTON_LAYER_POSITION_TOP_UI);
401         weston_layer_set_position(&ivi->fullscreen,
402                                   WESTON_LAYER_POSITION_FULLSCREEN);
403
404         return 0;
405 }
406
407 static void
408 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
409 {
410         struct ivi_surface *surface;
411
412         wl_list_for_each(surface, &ivi->surfaces, link) {
413                 const char *app_id =
414                         weston_desktop_surface_get_app_id(surface->dsurface);
415                 agl_shell_desktop_send_application(resource, app_id);
416         }
417 }
418
419 static void
420 client_exec(const char *command, int fd)
421 {
422         sigset_t sig;
423         char s[32];
424
425         /* Don't give the child our signal mask */
426         sigfillset(&sig);
427         sigprocmask(SIG_UNBLOCK, &sig, NULL);
428
429         /* Launch clients as the user; don't give them the wrong euid */
430         if (seteuid(getuid()) == -1) {
431                 weston_log("seteuid failed: %s\n", strerror(errno));
432                 return;
433         }
434
435         /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
436          * clobbering fd, as we'll exit/exec either way.
437          */
438         fd = dup(fd);
439         if (fd == -1) {
440                 weston_log("dup failed: %s\n", strerror(errno));
441                 return;
442         }
443
444         snprintf(s, sizeof s, "%d", fd);
445         setenv("WAYLAND_SOCKET", s, 1);
446
447         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
448         weston_log("executing '%s' failed: %s", command, strerror(errno));
449 }
450
451 static struct wl_client *
452 launch_shell_client(struct ivi_compositor *ivi, const char *command)
453 {
454         struct wl_client *client;
455         int sock[2];
456         pid_t pid;
457
458         weston_log("launching' %s'\n", command);
459
460         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
461                 weston_log("socketpair failed while launching '%s': %s\n",
462                            command, strerror(errno));
463                 return NULL;
464         }
465
466         pid = fork();
467         if (pid == -1) {
468                 close(sock[0]);
469                 close(sock[1]);
470                 weston_log("fork failed while launching '%s': %s\n",
471                            command, strerror(errno));
472                 return NULL;
473         }
474
475         if (pid == 0) {
476                 client_exec(command, sock[1]);
477                 _Exit(EXIT_FAILURE);
478         }
479         close(sock[1]);
480
481         client = wl_client_create(ivi->compositor->wl_display, sock[0]);
482         if (!client) {
483                 close(sock[0]);
484                 weston_log("Failed to create wayland client for '%s'",
485                            command);
486                 return NULL;
487         }
488
489         return client;
490 }
491
492 int
493 ivi_launch_shell_client(struct ivi_compositor *ivi)
494 {
495         struct weston_config_section *section;
496         char *command = NULL;
497
498         section = weston_config_get_section(ivi->config, "shell-client",
499                                             NULL, NULL);
500         if (section)
501                 weston_config_section_get_string(section, "command",
502                                                  &command, NULL);
503
504         if (!command)
505                 return -1;
506
507         ivi->shell_client.client = launch_shell_client(ivi, command);
508         if (!ivi->shell_client.client)
509                 return -1;
510
511         return 0;
512 }
513
514 static void
515 destroy_black_view(struct wl_listener *listener, void *data)
516 {
517         struct fullscreen_view *fs =
518                 wl_container_of(listener, fs, fs_destroy);
519
520
521         if (fs && fs->fs) {
522                 if (fs->fs->view && fs->fs->view->surface) {
523                         weston_surface_destroy(fs->fs->view->surface);
524                         fs->fs->view = NULL;
525                 }
526
527                 free(fs->fs);
528                 wl_list_remove(&fs->fs_destroy.link);
529         }
530 }
531
532
533 static void
534 create_black_surface_view(struct ivi_output *output)
535 {
536         struct weston_surface *surface = NULL;
537         struct weston_view *view;
538         struct ivi_compositor *ivi = output->ivi;
539         struct weston_compositor *wc= ivi->compositor;
540         struct weston_output *woutput = output->output;
541
542         surface = weston_surface_create(wc);
543         view = weston_view_create(surface);
544
545         assert(view || surface);
546
547         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
548         weston_surface_set_size(surface, woutput->width, woutput->height);
549         weston_view_set_position(view, woutput->x, woutput->y);
550
551         output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
552         output->fullscreen_view.fs->view = view;
553
554         output->fullscreen_view.fs_destroy.notify = destroy_black_view;
555         wl_signal_add(&woutput->destroy_signal,
556                       &output->fullscreen_view.fs_destroy);
557 }
558
559 void
560 remove_black_surface(struct ivi_output *output)
561 {
562         struct weston_view *view;
563
564         if (!output &&
565             !output->fullscreen_view.fs &&
566             !output->fullscreen_view.fs->view) {
567                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
568                 return;
569         }
570
571         view = output->fullscreen_view.fs->view;
572         assert(view->is_mapped == true ||
573                view->surface->is_mapped == true);
574
575         view->is_mapped = false;
576         view->surface->is_mapped = false;
577
578         weston_layer_entry_remove(&view->layer_link);
579         weston_view_update_transform(view);
580
581         weston_output_damage(output->output);
582 }
583
584 void
585 insert_black_surface(struct ivi_output *output)
586 {
587         struct weston_view *view;
588
589         if (!output &&
590             !output->fullscreen_view.fs &&
591             !output->fullscreen_view.fs->view) {
592                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
593                 return;
594         }
595
596         view = output->fullscreen_view.fs->view;
597         if (view->is_mapped || view->surface->is_mapped)
598                 return;
599
600         weston_layer_entry_remove(&view->layer_link);
601         weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
602                                   &view->layer_link);
603
604         view->is_mapped = true;
605         view->surface->is_mapped = true;
606
607         weston_view_update_transform(view);
608         weston_output_damage(output->output);
609 }
610
611 static void
612 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
613 {
614         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
615         struct ivi_output *output;
616         struct ivi_surface *surface, *tmp;
617
618         /* Init already finished. Do nothing */
619         if (ivi->shell_client.ready)
620                 return;
621
622         ivi->shell_client.ready = true;
623
624         wl_list_for_each(output, &ivi->outputs, link) {
625                 if (output->background)
626                         remove_black_surface(output);
627                 ivi_layout_init(ivi, output);
628         }
629
630         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
631                 wl_list_remove(&surface->link);
632                 ivi_check_pending_desktop_surface(surface);
633         }
634 }
635
636 static void
637 shell_set_background(struct wl_client *client,
638                      struct wl_resource *shell_res,
639                      struct wl_resource *surface_res,
640                      struct wl_resource *output_res)
641 {
642         struct weston_head *head = weston_head_from_resource(output_res);
643         struct weston_output *woutput = weston_head_get_output(head);
644         struct ivi_output *output = to_ivi_output(woutput);
645         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
646         struct weston_desktop_surface *dsurface;
647         struct ivi_surface *surface;
648
649         dsurface = weston_surface_get_desktop_surface(wsurface);
650         if (!dsurface) {
651                 wl_resource_post_error(shell_res,
652                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
653                                        "surface must be a desktop surface");
654                 return;
655         }
656
657         surface = weston_desktop_surface_get_user_data(dsurface);
658         if (surface->role != IVI_SURFACE_ROLE_NONE) {
659                 wl_resource_post_error(shell_res,
660                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
661                                        "surface already has another ivi role");
662                 return;
663         }
664
665         if (output->background) {
666                 wl_resource_post_error(shell_res,
667                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
668                                        "output already has background");
669                 return;
670         }
671
672         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
673         surface->bg.output = output;
674         wl_list_remove(&surface->link);
675         wl_list_init(&surface->link);
676
677         output->background = surface;
678
679         weston_desktop_surface_set_maximized(dsurface, true);
680         weston_desktop_surface_set_size(dsurface,
681                                         output->output->width,
682                                         output->output->height);
683 }
684
685 static void
686 shell_set_panel(struct wl_client *client,
687                 struct wl_resource *shell_res,
688                 struct wl_resource *surface_res,
689                 struct wl_resource *output_res,
690                 uint32_t edge)
691 {
692         struct weston_head *head = weston_head_from_resource(output_res);
693         struct weston_output *woutput = weston_head_get_output(head);
694         struct ivi_output *output = to_ivi_output(woutput);
695         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
696         struct weston_desktop_surface *dsurface;
697         struct ivi_surface *surface;
698         struct ivi_surface **member;
699         int32_t width = 0, height = 0;
700
701         dsurface = weston_surface_get_desktop_surface(wsurface);
702         if (!dsurface) {
703                 wl_resource_post_error(shell_res,
704                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
705                                        "surface must be a desktop surface");
706                 return;
707         }
708
709         surface = weston_desktop_surface_get_user_data(dsurface);
710         if (surface->role != IVI_SURFACE_ROLE_NONE) {
711                 wl_resource_post_error(shell_res,
712                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
713                                        "surface already has another ivi role");
714                 return;
715         }
716
717         switch (edge) {
718         case AGL_SHELL_EDGE_TOP:
719                 member = &output->top;
720                 break;
721         case AGL_SHELL_EDGE_BOTTOM:
722                 member = &output->bottom;
723                 break;
724         case AGL_SHELL_EDGE_LEFT:
725                 member = &output->left;
726                 break;
727         case AGL_SHELL_EDGE_RIGHT:
728                 member = &output->right;
729                 break;
730         default:
731                 wl_resource_post_error(shell_res,
732                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
733                                        "invalid edge for panel");
734                 return;
735         }
736
737         if (*member) {
738                 wl_resource_post_error(shell_res,
739                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
740                                        "output already has panel on this edge");
741                 return;
742         }
743
744         surface->role = IVI_SURFACE_ROLE_PANEL;
745         surface->panel.output = output;
746         surface->panel.edge = edge;
747         wl_list_remove(&surface->link);
748         wl_list_init(&surface->link);
749
750         *member = surface;
751
752         switch (surface->panel.edge) {
753         case AGL_SHELL_EDGE_TOP:
754         case AGL_SHELL_EDGE_BOTTOM:
755                 width = woutput->width;
756                 break;
757         case AGL_SHELL_EDGE_LEFT:
758         case AGL_SHELL_EDGE_RIGHT:
759                 height = woutput->height;
760                 break;
761         }
762
763         weston_desktop_surface_set_size(dsurface, width, height);
764 }
765
766
767 static void
768 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
769                           const char *data, uint32_t app_state)
770 {
771         struct desktop_client *dclient;
772         uint32_t app_role;
773         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
774         struct ivi_policy *policy = ivi->policy;
775
776         /* FIXME: should queue it here and see when binding agl-shell-desktop
777          * if there are any to be sent */
778         if (!surf)
779                 return;
780
781         if (policy && policy->api.surface_advertise_state_change &&
782             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
783                 return;
784         }
785
786         app_role = surf->role;
787         if (app_role == IVI_SURFACE_ROLE_POPUP)
788                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
789
790         wl_list_for_each(dclient, &ivi->desktop_clients, link)
791                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
792                                                  data, app_state, app_role);
793 }
794
795 static void
796 shell_activate_app(struct wl_client *client,
797                    struct wl_resource *shell_res,
798                    const char *app_id,
799                    struct wl_resource *output_res)
800 {
801         struct weston_head *head = weston_head_from_resource(output_res);
802         struct weston_output *woutput = weston_head_get_output(head);
803         struct ivi_output *output = to_ivi_output(woutput);
804
805         ivi_layout_activate(output, app_id);
806 }
807
808 static void
809 shell_desktop_activate_app(struct wl_client *client,
810                            struct wl_resource *shell_res,
811                            const char *app_id, const char *data,
812                            struct wl_resource *output_res)
813 {
814         struct weston_head *head = weston_head_from_resource(output_res);
815         struct weston_output *woutput = weston_head_get_output(head);
816         struct ivi_output *output = to_ivi_output(woutput);
817
818         ivi_layout_activate(output, app_id);
819         shell_advertise_app_state(output->ivi, app_id,
820                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
821 }
822
823 static void
824 shell_deactivate_app(struct wl_client *client,
825                    struct wl_resource *shell_res,
826                    const char *app_id)
827 {
828         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
829         struct ivi_compositor *ivi = dclient->ivi;
830
831         ivi_layout_deactivate(ivi, app_id);
832         shell_advertise_app_state(ivi, app_id,
833                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
834 }
835
836 static const struct agl_shell_interface agl_shell_implementation = {
837         .ready = shell_ready,
838         .set_background = shell_set_background,
839         .set_panel = shell_set_panel,
840         .activate_app = shell_activate_app,
841 };
842
843 static void
844 shell_desktop_set_app_property(struct wl_client *client,
845                                struct wl_resource *shell_res,
846                                const char *app_id, uint32_t role,
847                                int x, int y, struct wl_resource *output_res)
848 {
849         struct weston_head *head = weston_head_from_resource(output_res);
850         struct weston_output *woutput = weston_head_get_output(head);
851         struct ivi_output *output = to_ivi_output(woutput);
852
853         switch (role) {
854         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
855                 ivi_set_pending_desktop_surface_popup(output, x, y, app_id);
856                 break;
857         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
858                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
859                 break;
860         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
861         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
862                 ivi_set_pending_desktop_surface_split(output, app_id, role);
863                 break;
864         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
865                 ivi_set_pending_desktop_surface_remote(output, app_id);
866                 break;
867         default:
868                 break;
869         }
870 }
871
872 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
873         .activate_app = shell_desktop_activate_app,
874         .set_app_property = shell_desktop_set_app_property,
875         .deactivate_app = shell_deactivate_app,
876 };
877
878 static void
879 unbind_agl_shell(struct wl_resource *resource)
880 {
881         struct ivi_compositor *ivi;
882         struct ivi_output *output;
883         struct ivi_surface *surf, *surf_tmp;
884
885         ivi = wl_resource_get_user_data(resource);
886         wl_list_for_each(output, &ivi->outputs, link) {
887                 /* reset the active surf if there's one present */
888                 if (output->active) {
889                         output->active->view->is_mapped = false;
890                         output->active->view->surface->is_mapped = false;
891
892                         weston_layer_entry_remove(&output->active->view->layer_link);
893                         output->active = NULL;
894                 }
895
896                 insert_black_surface(output);
897         }
898
899         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
900                 wl_list_remove(&surf->link);
901                 wl_list_init(&surf->link);
902         }
903
904         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
905                 wl_list_remove(&surf->link);
906                 wl_list_init(&surf->link);
907         }
908
909         wl_list_init(&ivi->surfaces);
910         wl_list_init(&ivi->pending_surfaces);
911
912         ivi->shell_client.ready = false;
913         ivi->shell_client.resource = NULL;
914         ivi->shell_client.client = NULL;
915 }
916
917 static void
918 bind_agl_shell(struct wl_client *client,
919                void *data, uint32_t version, uint32_t id)
920 {
921         struct ivi_compositor *ivi = data;
922         struct wl_resource *resource;
923
924         resource = wl_resource_create(client, &agl_shell_interface,
925                                       1, id);
926         if (!resource) {
927                 wl_client_post_no_memory(client);
928                 return;
929         }
930
931 #if 0
932         if (ivi->shell_client.client != client) {
933                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
934                                        "client not authorized to use agl_shell");
935                 return;
936         }
937 #endif
938
939         if (ivi->shell_client.resource) {
940                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
941                                        "agl_shell has already been bound");
942                 return;
943         }
944
945         wl_resource_set_implementation(resource, &agl_shell_implementation,
946                                        ivi, unbind_agl_shell);
947         ivi->shell_client.resource = resource;
948 }
949
950 static void
951 unbind_agl_shell_desktop(struct wl_resource *resource)
952 {
953         struct desktop_client *dclient = wl_resource_get_user_data(resource);
954
955         wl_list_remove(&dclient->link);
956         free(dclient);
957 }
958
959 static void
960 bind_agl_shell_desktop(struct wl_client *client,
961                        void *data, uint32_t version, uint32_t id)
962 {
963         struct ivi_compositor *ivi = data;
964         struct wl_resource *resource;
965         struct desktop_client *dclient = zalloc(sizeof(*dclient));
966
967         if (!dclient) {
968                 wl_client_post_no_memory(client);
969                 return;
970         }
971
972         resource = wl_resource_create(client, &agl_shell_desktop_interface,
973                                       version, id);
974         dclient->ivi = ivi;
975         if (!resource) {
976                 wl_client_post_no_memory(client);
977                 return;
978         }
979
980         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
981                                        dclient, unbind_agl_shell_desktop);
982
983         dclient->resource = resource;
984         wl_list_insert(&ivi->desktop_clients, &dclient->link);
985
986         /* advertise xdg surfaces */
987         ivi_shell_advertise_xdg_surfaces(ivi, resource);
988 }
989
990 int
991 ivi_shell_create_global(struct ivi_compositor *ivi)
992 {
993         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
994                                           &agl_shell_interface, 1,
995                                           ivi, bind_agl_shell);
996         if (!ivi->agl_shell) {
997                 weston_log("Failed to create wayland global.\n");
998                 return -1;
999         }
1000
1001         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
1002                                                   &agl_shell_desktop_interface, 1,
1003                                                   ivi, bind_agl_shell_desktop);
1004         if (!ivi->agl_shell_desktop) {
1005                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
1006                 return -1;
1007         }
1008
1009         return 0;
1010 }