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