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