layout: Advertise state change for other types of surface roles
[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) || !_app_id)
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) || !_app_id)
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) || !_app_id)
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) || !_app_id)
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                 ivi_layout_popup_committed(surface);
370                 return;
371         }
372
373         ret = ivi_check_pending_desktop_surface_split(surface);
374         if (ret) {
375                 ivi_set_desktop_surface_split(surface);
376                 ivi_layout_split_committed(surface);
377                 return;
378         }
379
380         ret = ivi_check_pending_desktop_surface_fullscreen(surface);
381         if (ret) {
382                 ivi_set_desktop_surface_fullscreen(surface);
383                 ivi_layout_fullscreen_committed(surface);
384                 return;
385         }
386
387         ret = ivi_check_pending_desktop_surface_remote(surface);
388         if (ret) {
389                 ivi_set_desktop_surface_remote(surface);
390                 ivi_layout_desktop_committed(surface);
391                 return;
392         }
393
394         /* if we end up here means we have a regular desktop app and
395          * try to activate it */
396         ivi_set_desktop_surface(surface);
397         ivi_layout_desktop_committed(surface);
398 }
399
400 void
401 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
402 {
403         struct ivi_output *out;
404
405         wl_list_for_each(out, &ivi->outputs, link) {
406                 create_black_surface_view(out);
407                 insert_black_surface(out);
408         }
409 }
410
411 int
412 ivi_shell_init(struct ivi_compositor *ivi)
413 {
414         weston_layer_init(&ivi->hidden, ivi->compositor);
415         weston_layer_init(&ivi->background, ivi->compositor);
416         weston_layer_init(&ivi->normal, ivi->compositor);
417         weston_layer_init(&ivi->panel, ivi->compositor);
418         weston_layer_init(&ivi->popup, ivi->compositor);
419         weston_layer_init(&ivi->fullscreen, ivi->compositor);
420
421         weston_layer_set_position(&ivi->hidden,
422                                   WESTON_LAYER_POSITION_HIDDEN);
423         weston_layer_set_position(&ivi->background,
424                                   WESTON_LAYER_POSITION_BACKGROUND);
425         weston_layer_set_position(&ivi->normal,
426                                   WESTON_LAYER_POSITION_NORMAL);
427         weston_layer_set_position(&ivi->panel,
428                                   WESTON_LAYER_POSITION_UI);
429         weston_layer_set_position(&ivi->popup,
430                                   WESTON_LAYER_POSITION_TOP_UI);
431         weston_layer_set_position(&ivi->fullscreen,
432                                   WESTON_LAYER_POSITION_FULLSCREEN);
433
434         return 0;
435 }
436
437 static void
438 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
439 {
440         struct ivi_surface *surface;
441
442         wl_list_for_each(surface, &ivi->surfaces, link) {
443                 const char *app_id =
444                         weston_desktop_surface_get_app_id(surface->dsurface);
445                 agl_shell_desktop_send_application(resource, app_id);
446         }
447 }
448
449 static void
450 client_exec(const char *command, int fd)
451 {
452         sigset_t sig;
453         char s[32];
454
455         /* Don't give the child our signal mask */
456         sigfillset(&sig);
457         sigprocmask(SIG_UNBLOCK, &sig, NULL);
458
459         /* Launch clients as the user; don't give them the wrong euid */
460         if (seteuid(getuid()) == -1) {
461                 weston_log("seteuid failed: %s\n", strerror(errno));
462                 return;
463         }
464
465         /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
466          * clobbering fd, as we'll exit/exec either way.
467          */
468         fd = dup(fd);
469         if (fd == -1) {
470                 weston_log("dup failed: %s\n", strerror(errno));
471                 return;
472         }
473
474         snprintf(s, sizeof s, "%d", fd);
475         setenv("WAYLAND_SOCKET", s, 1);
476
477         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
478         weston_log("executing '%s' failed: %s", command, strerror(errno));
479 }
480
481 static struct wl_client *
482 launch_shell_client(struct ivi_compositor *ivi, const char *command)
483 {
484         struct wl_client *client;
485         int sock[2];
486         pid_t pid;
487
488         weston_log("launching' %s'\n", command);
489
490         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
491                 weston_log("socketpair failed while launching '%s': %s\n",
492                            command, strerror(errno));
493                 return NULL;
494         }
495
496         pid = fork();
497         if (pid == -1) {
498                 close(sock[0]);
499                 close(sock[1]);
500                 weston_log("fork failed while launching '%s': %s\n",
501                            command, strerror(errno));
502                 return NULL;
503         }
504
505         if (pid == 0) {
506                 client_exec(command, sock[1]);
507                 _Exit(EXIT_FAILURE);
508         }
509         close(sock[1]);
510
511         client = wl_client_create(ivi->compositor->wl_display, sock[0]);
512         if (!client) {
513                 close(sock[0]);
514                 weston_log("Failed to create wayland client for '%s'",
515                            command);
516                 return NULL;
517         }
518
519         return client;
520 }
521
522 int
523 ivi_launch_shell_client(struct ivi_compositor *ivi)
524 {
525         struct weston_config_section *section;
526         char *command = NULL;
527
528         section = weston_config_get_section(ivi->config, "shell-client",
529                                             NULL, NULL);
530         if (section)
531                 weston_config_section_get_string(section, "command",
532                                                  &command, NULL);
533
534         if (!command)
535                 return -1;
536
537         ivi->shell_client.client = launch_shell_client(ivi, command);
538         if (!ivi->shell_client.client)
539                 return -1;
540
541         return 0;
542 }
543
544 static void
545 destroy_black_view(struct wl_listener *listener, void *data)
546 {
547         struct fullscreen_view *fs =
548                 wl_container_of(listener, fs, fs_destroy);
549
550
551         if (fs && fs->fs) {
552                 if (fs->fs->view && fs->fs->view->surface) {
553                         weston_surface_destroy(fs->fs->view->surface);
554                         fs->fs->view = NULL;
555                 }
556
557                 free(fs->fs);
558                 wl_list_remove(&fs->fs_destroy.link);
559         }
560 }
561
562
563 static void
564 create_black_surface_view(struct ivi_output *output)
565 {
566         struct weston_surface *surface = NULL;
567         struct weston_view *view;
568         struct ivi_compositor *ivi = output->ivi;
569         struct weston_compositor *wc= ivi->compositor;
570         struct weston_output *woutput = output->output;
571
572         surface = weston_surface_create(wc);
573         view = weston_view_create(surface);
574
575         assert(view || surface);
576
577         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
578         weston_surface_set_size(surface, woutput->width, woutput->height);
579         weston_view_set_position(view, woutput->x, woutput->y);
580
581         output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
582         output->fullscreen_view.fs->view = view;
583
584         output->fullscreen_view.fs_destroy.notify = destroy_black_view;
585         wl_signal_add(&woutput->destroy_signal,
586                       &output->fullscreen_view.fs_destroy);
587 }
588
589 void
590 remove_black_surface(struct ivi_output *output)
591 {
592         struct weston_view *view;
593
594         if (!output &&
595             !output->fullscreen_view.fs &&
596             !output->fullscreen_view.fs->view) {
597                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
598                 return;
599         }
600
601         view = output->fullscreen_view.fs->view;
602         assert(view->is_mapped == true ||
603                view->surface->is_mapped == true);
604
605         view->is_mapped = false;
606         view->surface->is_mapped = false;
607
608         weston_layer_entry_remove(&view->layer_link);
609         weston_view_update_transform(view);
610
611         weston_output_damage(output->output);
612 }
613
614 void
615 insert_black_surface(struct ivi_output *output)
616 {
617         struct weston_view *view;
618
619         if (!output &&
620             !output->fullscreen_view.fs &&
621             !output->fullscreen_view.fs->view) {
622                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
623                 return;
624         }
625
626         view = output->fullscreen_view.fs->view;
627         if (view->is_mapped || view->surface->is_mapped)
628                 return;
629
630         weston_layer_entry_remove(&view->layer_link);
631         weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
632                                   &view->layer_link);
633
634         view->is_mapped = true;
635         view->surface->is_mapped = true;
636
637         weston_view_update_transform(view);
638         weston_output_damage(output->output);
639 }
640
641 static void
642 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
643 {
644         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
645         struct ivi_output *output;
646         struct ivi_surface *surface, *tmp;
647
648         /* Init already finished. Do nothing */
649         if (ivi->shell_client.ready)
650                 return;
651
652         ivi->shell_client.ready = true;
653
654         wl_list_for_each(output, &ivi->outputs, link) {
655                 if (output->background)
656                         remove_black_surface(output);
657                 ivi_layout_init(ivi, output);
658         }
659
660         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
661                 wl_list_remove(&surface->link);
662                 ivi_check_pending_desktop_surface(surface);
663         }
664 }
665
666 static void
667 shell_set_background(struct wl_client *client,
668                      struct wl_resource *shell_res,
669                      struct wl_resource *surface_res,
670                      struct wl_resource *output_res)
671 {
672         struct weston_head *head = weston_head_from_resource(output_res);
673         struct weston_output *woutput = weston_head_get_output(head);
674         struct ivi_output *output = to_ivi_output(woutput);
675         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
676         struct weston_desktop_surface *dsurface;
677         struct ivi_surface *surface;
678
679         dsurface = weston_surface_get_desktop_surface(wsurface);
680         if (!dsurface) {
681                 wl_resource_post_error(shell_res,
682                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
683                                        "surface must be a desktop surface");
684                 return;
685         }
686
687         surface = weston_desktop_surface_get_user_data(dsurface);
688         if (surface->role != IVI_SURFACE_ROLE_NONE) {
689                 wl_resource_post_error(shell_res,
690                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
691                                        "surface already has another ivi role");
692                 return;
693         }
694
695         if (output->background) {
696                 wl_resource_post_error(shell_res,
697                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
698                                        "output already has background");
699                 return;
700         }
701
702         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
703         surface->bg.output = output;
704         wl_list_remove(&surface->link);
705         wl_list_init(&surface->link);
706
707         output->background = surface;
708
709         weston_desktop_surface_set_maximized(dsurface, true);
710         weston_desktop_surface_set_size(dsurface,
711                                         output->output->width,
712                                         output->output->height);
713 }
714
715 static void
716 shell_set_panel(struct wl_client *client,
717                 struct wl_resource *shell_res,
718                 struct wl_resource *surface_res,
719                 struct wl_resource *output_res,
720                 uint32_t edge)
721 {
722         struct weston_head *head = weston_head_from_resource(output_res);
723         struct weston_output *woutput = weston_head_get_output(head);
724         struct ivi_output *output = to_ivi_output(woutput);
725         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
726         struct weston_desktop_surface *dsurface;
727         struct ivi_surface *surface;
728         struct ivi_surface **member;
729         int32_t width = 0, height = 0;
730
731         dsurface = weston_surface_get_desktop_surface(wsurface);
732         if (!dsurface) {
733                 wl_resource_post_error(shell_res,
734                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
735                                        "surface must be a desktop surface");
736                 return;
737         }
738
739         surface = weston_desktop_surface_get_user_data(dsurface);
740         if (surface->role != IVI_SURFACE_ROLE_NONE) {
741                 wl_resource_post_error(shell_res,
742                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
743                                        "surface already has another ivi role");
744                 return;
745         }
746
747         switch (edge) {
748         case AGL_SHELL_EDGE_TOP:
749                 member = &output->top;
750                 break;
751         case AGL_SHELL_EDGE_BOTTOM:
752                 member = &output->bottom;
753                 break;
754         case AGL_SHELL_EDGE_LEFT:
755                 member = &output->left;
756                 break;
757         case AGL_SHELL_EDGE_RIGHT:
758                 member = &output->right;
759                 break;
760         default:
761                 wl_resource_post_error(shell_res,
762                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
763                                        "invalid edge for panel");
764                 return;
765         }
766
767         if (*member) {
768                 wl_resource_post_error(shell_res,
769                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
770                                        "output already has panel on this edge");
771                 return;
772         }
773
774         surface->role = IVI_SURFACE_ROLE_PANEL;
775         surface->panel.output = output;
776         surface->panel.edge = edge;
777         wl_list_remove(&surface->link);
778         wl_list_init(&surface->link);
779
780         *member = surface;
781
782         switch (surface->panel.edge) {
783         case AGL_SHELL_EDGE_TOP:
784         case AGL_SHELL_EDGE_BOTTOM:
785                 width = woutput->width;
786                 break;
787         case AGL_SHELL_EDGE_LEFT:
788         case AGL_SHELL_EDGE_RIGHT:
789                 height = woutput->height;
790                 break;
791         }
792
793         weston_desktop_surface_set_size(dsurface, width, height);
794 }
795
796 void
797 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
798                           const char *data, uint32_t app_state)
799 {
800         struct desktop_client *dclient;
801         uint32_t app_role;
802         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
803         struct ivi_policy *policy = ivi->policy;
804
805         /* FIXME: should queue it here and see when binding agl-shell-desktop
806          * if there are any to be sent */
807         if (!surf)
808                 return;
809
810         if (policy && policy->api.surface_advertise_state_change &&
811             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
812                 return;
813         }
814
815         app_role = surf->role;
816         if (app_role == IVI_SURFACE_ROLE_POPUP)
817                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
818
819         wl_list_for_each(dclient, &ivi->desktop_clients, link)
820                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
821                                                  data, app_state, app_role);
822 }
823
824 static void
825 shell_activate_app(struct wl_client *client,
826                    struct wl_resource *shell_res,
827                    const char *app_id,
828                    struct wl_resource *output_res)
829 {
830         struct weston_head *head = weston_head_from_resource(output_res);
831         struct weston_output *woutput = weston_head_get_output(head);
832         struct ivi_output *output = to_ivi_output(woutput);
833
834         ivi_layout_activate(output, app_id);
835 }
836
837 static void
838 shell_desktop_activate_app(struct wl_client *client,
839                            struct wl_resource *shell_res,
840                            const char *app_id, const char *data,
841                            struct wl_resource *output_res)
842 {
843         struct weston_head *head = weston_head_from_resource(output_res);
844         struct weston_output *woutput = weston_head_get_output(head);
845         struct ivi_output *output = to_ivi_output(woutput);
846
847         ivi_layout_activate(output, app_id);
848         shell_advertise_app_state(output->ivi, app_id,
849                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
850 }
851
852 static void
853 shell_deactivate_app(struct wl_client *client,
854                    struct wl_resource *shell_res,
855                    const char *app_id)
856 {
857         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
858         struct ivi_compositor *ivi = dclient->ivi;
859
860         ivi_layout_deactivate(ivi, app_id);
861         shell_advertise_app_state(ivi, app_id,
862                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
863 }
864
865 static const struct agl_shell_interface agl_shell_implementation = {
866         .ready = shell_ready,
867         .set_background = shell_set_background,
868         .set_panel = shell_set_panel,
869         .activate_app = shell_activate_app,
870 };
871
872 static void
873 shell_desktop_set_app_property(struct wl_client *client,
874                                struct wl_resource *shell_res,
875                                const char *app_id, uint32_t role,
876                                int x, int y, int bx, int by,
877                                int width, int height,
878                                struct wl_resource *output_res)
879 {
880         struct weston_head *head = weston_head_from_resource(output_res);
881         struct weston_output *woutput = weston_head_get_output(head);
882         struct ivi_output *output = to_ivi_output(woutput);
883
884         switch (role) {
885         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
886                 ivi_set_pending_desktop_surface_popup(output, x, y, bx, by,
887                                                       width, height, app_id);
888                 break;
889         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
890                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
891                 break;
892         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
893         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
894                 ivi_set_pending_desktop_surface_split(output, app_id, role);
895                 break;
896         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
897                 ivi_set_pending_desktop_surface_remote(output, app_id);
898                 break;
899         default:
900                 break;
901         }
902 }
903
904 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
905         .activate_app = shell_desktop_activate_app,
906         .set_app_property = shell_desktop_set_app_property,
907         .deactivate_app = shell_deactivate_app,
908 };
909
910 static void
911 unbind_agl_shell(struct wl_resource *resource)
912 {
913         struct ivi_compositor *ivi;
914         struct ivi_output *output;
915         struct ivi_surface *surf, *surf_tmp;
916
917         ivi = wl_resource_get_user_data(resource);
918         wl_list_for_each(output, &ivi->outputs, link) {
919                 /* reset the active surf if there's one present */
920                 if (output->active) {
921                         output->active->view->is_mapped = false;
922                         output->active->view->surface->is_mapped = false;
923
924                         weston_layer_entry_remove(&output->active->view->layer_link);
925                         output->active = NULL;
926                 }
927
928                 insert_black_surface(output);
929         }
930
931         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
932                 wl_list_remove(&surf->link);
933                 wl_list_init(&surf->link);
934         }
935
936         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
937                 wl_list_remove(&surf->link);
938                 wl_list_init(&surf->link);
939         }
940
941         wl_list_init(&ivi->surfaces);
942         wl_list_init(&ivi->pending_surfaces);
943
944         ivi->shell_client.ready = false;
945         ivi->shell_client.resource = NULL;
946         ivi->shell_client.client = NULL;
947 }
948
949 static void
950 bind_agl_shell(struct wl_client *client,
951                void *data, uint32_t version, uint32_t id)
952 {
953         struct ivi_compositor *ivi = data;
954         struct wl_resource *resource;
955         struct ivi_policy *policy;
956         void *interface;
957
958         policy = ivi->policy;
959         interface = (void *) &agl_shell_interface;
960         if (policy && policy->api.shell_bind_interface &&
961             !policy->api.shell_bind_interface(client, interface)) {
962                 wl_client_post_implementation_error(client,
963                                        "client not authorized to use agl_shell");
964                 return;
965         }
966
967         resource = wl_resource_create(client, &agl_shell_interface,
968                                       1, id);
969         if (!resource) {
970                 wl_client_post_no_memory(client);
971                 return;
972         }
973
974 #if 0
975         if (ivi->shell_client.client != client) {
976                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
977                                        "client not authorized to use agl_shell");
978                 return;
979         }
980 #endif
981
982         if (ivi->shell_client.resource) {
983                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
984                                        "agl_shell has already been bound");
985                 return;
986         }
987
988         wl_resource_set_implementation(resource, &agl_shell_implementation,
989                                        ivi, unbind_agl_shell);
990         ivi->shell_client.resource = resource;
991 }
992
993 static void
994 unbind_agl_shell_desktop(struct wl_resource *resource)
995 {
996         struct desktop_client *dclient = wl_resource_get_user_data(resource);
997
998         wl_list_remove(&dclient->link);
999         free(dclient);
1000 }
1001
1002 static void
1003 bind_agl_shell_desktop(struct wl_client *client,
1004                        void *data, uint32_t version, uint32_t id)
1005 {
1006         struct ivi_compositor *ivi = data;
1007         struct wl_resource *resource;
1008         struct ivi_policy *policy;
1009         struct desktop_client *dclient;
1010         void *interface;
1011
1012         policy = ivi->policy;
1013         interface  = (void *) &agl_shell_desktop_interface;
1014         if (policy && policy->api.shell_bind_interface &&
1015             !policy->api.shell_bind_interface(client, interface)) {
1016                 wl_client_post_implementation_error(client,
1017                                 "client not authorized to use agl_shell_desktop");
1018                 return;
1019         }
1020
1021         dclient = zalloc(sizeof(*dclient));
1022         if (!dclient) {
1023                 wl_client_post_no_memory(client);
1024                 return;
1025         }
1026
1027         resource = wl_resource_create(client, &agl_shell_desktop_interface,
1028                                       version, id);
1029         dclient->ivi = ivi;
1030         if (!resource) {
1031                 wl_client_post_no_memory(client);
1032                 return;
1033         }
1034
1035         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
1036                                        dclient, unbind_agl_shell_desktop);
1037
1038         dclient->resource = resource;
1039         wl_list_insert(&ivi->desktop_clients, &dclient->link);
1040
1041         /* advertise xdg surfaces */
1042         ivi_shell_advertise_xdg_surfaces(ivi, resource);
1043 }
1044
1045 int
1046 ivi_shell_create_global(struct ivi_compositor *ivi)
1047 {
1048         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
1049                                           &agl_shell_interface, 1,
1050                                           ivi, bind_agl_shell);
1051         if (!ivi->agl_shell) {
1052                 weston_log("Failed to create wayland global.\n");
1053                 return -1;
1054         }
1055
1056         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
1057                                                   &agl_shell_desktop_interface, 1,
1058                                                   ivi, bind_agl_shell_desktop);
1059         if (!ivi->agl_shell_desktop) {
1060                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
1061                 return -1;
1062         }
1063
1064         return 0;
1065 }