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