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