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