desktop: Delay surface creation until committed
[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                 surface->checked_pending = true;
751         }
752 }
753
754 static void
755 shell_set_background(struct wl_client *client,
756                      struct wl_resource *shell_res,
757                      struct wl_resource *surface_res,
758                      struct wl_resource *output_res)
759 {
760         struct weston_head *head = weston_head_from_resource(output_res);
761         struct weston_output *woutput = weston_head_get_output(head);
762         struct ivi_output *output = to_ivi_output(woutput);
763         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
764         struct weston_desktop_surface *dsurface;
765         struct ivi_surface *surface;
766
767         dsurface = weston_surface_get_desktop_surface(wsurface);
768         if (!dsurface) {
769                 wl_resource_post_error(shell_res,
770                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
771                                        "surface must be a desktop surface");
772                 return;
773         }
774
775         surface = weston_desktop_surface_get_user_data(dsurface);
776         if (surface->role != IVI_SURFACE_ROLE_NONE) {
777                 wl_resource_post_error(shell_res,
778                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
779                                        "surface already has another ivi role");
780                 return;
781         }
782
783         if (output->background) {
784                 wl_resource_post_error(shell_res,
785                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
786                                        "output already has background");
787                 return;
788         }
789
790         surface->checked_pending = true;
791         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
792         surface->bg.output = output;
793         wl_list_remove(&surface->link);
794         wl_list_init(&surface->link);
795
796         output->background = surface;
797
798         weston_desktop_surface_set_maximized(dsurface, true);
799         weston_desktop_surface_set_size(dsurface,
800                                         output->output->width,
801                                         output->output->height);
802 }
803
804 static void
805 shell_set_panel(struct wl_client *client,
806                 struct wl_resource *shell_res,
807                 struct wl_resource *surface_res,
808                 struct wl_resource *output_res,
809                 uint32_t edge)
810 {
811         struct weston_head *head = weston_head_from_resource(output_res);
812         struct weston_output *woutput = weston_head_get_output(head);
813         struct ivi_output *output = to_ivi_output(woutput);
814         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
815         struct weston_desktop_surface *dsurface;
816         struct ivi_surface *surface;
817         struct ivi_surface **member;
818         int32_t width = 0, height = 0;
819
820         dsurface = weston_surface_get_desktop_surface(wsurface);
821         if (!dsurface) {
822                 wl_resource_post_error(shell_res,
823                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
824                                        "surface must be a desktop surface");
825                 return;
826         }
827
828         surface = weston_desktop_surface_get_user_data(dsurface);
829         if (surface->role != IVI_SURFACE_ROLE_NONE) {
830                 wl_resource_post_error(shell_res,
831                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
832                                        "surface already has another ivi role");
833                 return;
834         }
835
836         switch (edge) {
837         case AGL_SHELL_EDGE_TOP:
838                 member = &output->top;
839                 break;
840         case AGL_SHELL_EDGE_BOTTOM:
841                 member = &output->bottom;
842                 break;
843         case AGL_SHELL_EDGE_LEFT:
844                 member = &output->left;
845                 break;
846         case AGL_SHELL_EDGE_RIGHT:
847                 member = &output->right;
848                 break;
849         default:
850                 wl_resource_post_error(shell_res,
851                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
852                                        "invalid edge for panel");
853                 return;
854         }
855
856         if (*member) {
857                 wl_resource_post_error(shell_res,
858                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
859                                        "output already has panel on this edge");
860                 return;
861         }
862
863         surface->checked_pending = true;
864         surface->role = IVI_SURFACE_ROLE_PANEL;
865         surface->panel.output = output;
866         surface->panel.edge = edge;
867         wl_list_remove(&surface->link);
868         wl_list_init(&surface->link);
869
870         *member = surface;
871
872         switch (surface->panel.edge) {
873         case AGL_SHELL_EDGE_TOP:
874         case AGL_SHELL_EDGE_BOTTOM:
875                 width = woutput->width;
876                 break;
877         case AGL_SHELL_EDGE_LEFT:
878         case AGL_SHELL_EDGE_RIGHT:
879                 height = woutput->height;
880                 break;
881         }
882
883         weston_desktop_surface_set_size(dsurface, width, height);
884 }
885
886 void
887 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
888                           const char *data, uint32_t app_state)
889 {
890         struct desktop_client *dclient;
891         uint32_t app_role;
892         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
893         struct ivi_policy *policy = ivi->policy;
894
895         /* FIXME: should queue it here and see when binding agl-shell-desktop
896          * if there are any to be sent */
897         if (!surf)
898                 return;
899
900         if (!app_id)
901                 return;
902
903         if (policy && policy->api.surface_advertise_state_change &&
904             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
905                 return;
906         }
907
908         app_role = surf->role;
909         if (app_role == IVI_SURFACE_ROLE_POPUP)
910                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
911
912         wl_list_for_each(dclient, &ivi->desktop_clients, link)
913                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
914                                                  data, app_state, app_role);
915 }
916
917 static void
918 shell_activate_app(struct wl_client *client,
919                    struct wl_resource *shell_res,
920                    const char *app_id,
921                    struct wl_resource *output_res)
922 {
923         struct weston_head *head = weston_head_from_resource(output_res);
924         struct weston_output *woutput = weston_head_get_output(head);
925         struct ivi_output *output = to_ivi_output(woutput);
926
927         ivi_layout_activate(output, app_id);
928 }
929
930 static void
931 shell_desktop_activate_app(struct wl_client *client,
932                            struct wl_resource *shell_res,
933                            const char *app_id, const char *data,
934                            struct wl_resource *output_res)
935 {
936         struct weston_head *head = weston_head_from_resource(output_res);
937         struct weston_output *woutput = weston_head_get_output(head);
938         struct ivi_output *output = to_ivi_output(woutput);
939
940         ivi_layout_activate(output, app_id);
941         shell_advertise_app_state(output->ivi, app_id,
942                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
943 }
944
945 static void
946 shell_deactivate_app(struct wl_client *client,
947                    struct wl_resource *shell_res,
948                    const char *app_id)
949 {
950         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
951         struct ivi_compositor *ivi = dclient->ivi;
952
953         ivi_layout_deactivate(ivi, app_id);
954         shell_advertise_app_state(ivi, app_id,
955                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
956 }
957
958 static const struct agl_shell_interface agl_shell_implementation = {
959         .ready = shell_ready,
960         .set_background = shell_set_background,
961         .set_panel = shell_set_panel,
962         .activate_app = shell_activate_app,
963 };
964
965 static void
966 shell_desktop_set_app_property(struct wl_client *client,
967                                struct wl_resource *shell_res,
968                                const char *app_id, uint32_t role,
969                                int x, int y, int bx, int by,
970                                int width, int height,
971                                struct wl_resource *output_res)
972 {
973         struct weston_head *head = weston_head_from_resource(output_res);
974         struct weston_output *woutput = weston_head_get_output(head);
975         struct ivi_output *output = to_ivi_output(woutput);
976
977         switch (role) {
978         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
979                 ivi_set_pending_desktop_surface_popup(output, x, y, bx, by,
980                                                       width, height, app_id);
981                 break;
982         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
983                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
984                 break;
985         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
986         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
987                 ivi_set_pending_desktop_surface_split(output, app_id, role);
988                 break;
989         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
990                 ivi_set_pending_desktop_surface_remote(output, app_id);
991                 break;
992         default:
993                 break;
994         }
995 }
996
997 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
998         .activate_app = shell_desktop_activate_app,
999         .set_app_property = shell_desktop_set_app_property,
1000         .deactivate_app = shell_deactivate_app,
1001 };
1002
1003 static void
1004 unbind_agl_shell(struct wl_resource *resource)
1005 {
1006         struct ivi_compositor *ivi;
1007         struct ivi_output *output;
1008         struct ivi_surface *surf, *surf_tmp;
1009
1010         ivi = wl_resource_get_user_data(resource);
1011         wl_list_for_each(output, &ivi->outputs, link) {
1012                 /* reset the active surf if there's one present */
1013                 if (output->active) {
1014                         output->active->view->is_mapped = false;
1015                         output->active->view->surface->is_mapped = false;
1016
1017                         weston_layer_entry_remove(&output->active->view->layer_link);
1018                         output->active = NULL;
1019                 }
1020
1021                 insert_black_surface(output);
1022         }
1023
1024         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
1025                 wl_list_remove(&surf->link);
1026                 wl_list_init(&surf->link);
1027         }
1028
1029         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
1030                 wl_list_remove(&surf->link);
1031                 wl_list_init(&surf->link);
1032         }
1033
1034         wl_list_init(&ivi->surfaces);
1035         wl_list_init(&ivi->pending_surfaces);
1036
1037         ivi->shell_client.ready = false;
1038         ivi->shell_client.resource = NULL;
1039         ivi->shell_client.client = NULL;
1040 }
1041
1042 static void
1043 bind_agl_shell(struct wl_client *client,
1044                void *data, uint32_t version, uint32_t id)
1045 {
1046         struct ivi_compositor *ivi = data;
1047         struct wl_resource *resource;
1048         struct ivi_policy *policy;
1049         void *interface;
1050
1051         policy = ivi->policy;
1052         interface = (void *) &agl_shell_interface;
1053         if (policy && policy->api.shell_bind_interface &&
1054             !policy->api.shell_bind_interface(client, interface)) {
1055                 wl_client_post_implementation_error(client,
1056                                        "client not authorized to use agl_shell");
1057                 return;
1058         }
1059
1060         resource = wl_resource_create(client, &agl_shell_interface,
1061                                       1, id);
1062         if (!resource) {
1063                 wl_client_post_no_memory(client);
1064                 return;
1065         }
1066
1067 #if 0
1068         if (ivi->shell_client.client != client) {
1069                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1070                                        "client not authorized to use agl_shell");
1071                 return;
1072         }
1073 #endif
1074
1075         if (ivi->shell_client.resource) {
1076                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1077                                        "agl_shell has already been bound");
1078                 return;
1079         }
1080
1081         wl_resource_set_implementation(resource, &agl_shell_implementation,
1082                                        ivi, unbind_agl_shell);
1083         ivi->shell_client.resource = resource;
1084 }
1085
1086 static void
1087 unbind_agl_shell_desktop(struct wl_resource *resource)
1088 {
1089         struct desktop_client *dclient = wl_resource_get_user_data(resource);
1090
1091         wl_list_remove(&dclient->link);
1092         free(dclient);
1093 }
1094
1095 static void
1096 bind_agl_shell_desktop(struct wl_client *client,
1097                        void *data, uint32_t version, uint32_t id)
1098 {
1099         struct ivi_compositor *ivi = data;
1100         struct wl_resource *resource;
1101         struct ivi_policy *policy;
1102         struct desktop_client *dclient;
1103         void *interface;
1104
1105         policy = ivi->policy;
1106         interface  = (void *) &agl_shell_desktop_interface;
1107         if (policy && policy->api.shell_bind_interface &&
1108             !policy->api.shell_bind_interface(client, interface)) {
1109                 wl_client_post_implementation_error(client,
1110                                 "client not authorized to use agl_shell_desktop");
1111                 return;
1112         }
1113
1114         dclient = zalloc(sizeof(*dclient));
1115         if (!dclient) {
1116                 wl_client_post_no_memory(client);
1117                 return;
1118         }
1119
1120         resource = wl_resource_create(client, &agl_shell_desktop_interface,
1121                                       version, id);
1122         dclient->ivi = ivi;
1123         if (!resource) {
1124                 wl_client_post_no_memory(client);
1125                 return;
1126         }
1127
1128         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
1129                                        dclient, unbind_agl_shell_desktop);
1130
1131         dclient->resource = resource;
1132         wl_list_insert(&ivi->desktop_clients, &dclient->link);
1133
1134         /* advertise xdg surfaces */
1135         ivi_shell_advertise_xdg_surfaces(ivi, resource);
1136 }
1137
1138 int
1139 ivi_shell_create_global(struct ivi_compositor *ivi)
1140 {
1141         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
1142                                           &agl_shell_interface, 1,
1143                                           ivi, bind_agl_shell);
1144         if (!ivi->agl_shell) {
1145                 weston_log("Failed to create wayland global.\n");
1146                 return -1;
1147         }
1148
1149         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
1150                                                   &agl_shell_desktop_interface, 1,
1151                                                   ivi, bind_agl_shell_desktop);
1152         if (!ivi->agl_shell_desktop) {
1153                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
1154                 return -1;
1155         }
1156
1157         return 0;
1158 }