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