6fcdfe84ce7c8936996c03e8a816b37e6bcc51a8
[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_compositor_keep_pending_surfaces(struct ivi_surface *surface)
362 {
363         return surface->ivi->keep_pending_surfaces;
364 }
365
366 static bool
367 ivi_check_pending_desktop_surface_popup(struct ivi_surface *surface)
368 {
369         struct ivi_compositor *ivi = surface->ivi;
370         struct pending_popup *p_popup, *next_p_popup;
371         const char *_app_id =
372                         weston_desktop_surface_get_app_id(surface->dsurface);
373
374         if (wl_list_empty(&ivi->popup_pending_apps) || !_app_id)
375                 return false;
376
377         wl_list_for_each_safe(p_popup, next_p_popup,
378                               &ivi->popup_pending_apps, link) {
379                 if (!strcmp(_app_id, p_popup->app_id)) {
380                         surface->popup.output = p_popup->ioutput;
381                         surface->popup.x = p_popup->x;
382                         surface->popup.y = p_popup->y;
383
384                         surface->popup.bb.x = p_popup->bb.x;
385                         surface->popup.bb.y = p_popup->bb.y;
386                         surface->popup.bb.width = p_popup->bb.width;
387                         surface->popup.bb.height = p_popup->bb.height;
388
389                         if (!ivi_compositor_keep_pending_surfaces(surface))
390                                 ivi_remove_pending_desktop_surface_popup(p_popup);
391                         return true;
392                 }
393         }
394
395         return false;
396 }
397
398 static bool
399 ivi_check_pending_desktop_surface_split(struct ivi_surface *surface)
400 {
401         struct pending_split *split_surf, *next_split_surf;
402         struct ivi_compositor *ivi = surface->ivi;
403         const char *_app_id =
404                         weston_desktop_surface_get_app_id(surface->dsurface);
405
406         if (wl_list_empty(&ivi->split_pending_apps) || !_app_id)
407                 return false;
408
409         wl_list_for_each_safe(split_surf, next_split_surf,
410                               &ivi->split_pending_apps, link) {
411                 if (!strcmp(_app_id, split_surf->app_id)) {
412                         surface->split.output = split_surf->ioutput;
413                         surface->split.orientation = split_surf->orientation;
414                         if (!ivi_compositor_keep_pending_surfaces(surface))
415                                 ivi_remove_pending_desktop_surface_split(split_surf);
416                         return true;
417                 }
418         }
419
420         return false;
421 }
422
423 static bool
424 ivi_check_pending_desktop_surface_fullscreen(struct ivi_surface *surface)
425 {
426         struct pending_fullscreen *fs_surf, *next_fs_surf;
427         struct ivi_compositor *ivi = surface->ivi;
428         const char *_app_id =
429                         weston_desktop_surface_get_app_id(surface->dsurface);
430
431         if (wl_list_empty(&ivi->fullscreen_pending_apps) || !_app_id)
432                 return false;
433
434         wl_list_for_each_safe(fs_surf, next_fs_surf,
435                               &ivi->fullscreen_pending_apps, link) {
436                 if (!strcmp(_app_id, fs_surf->app_id)) {
437                         surface->fullscreen.output = fs_surf->ioutput;
438                         if (!ivi_compositor_keep_pending_surfaces(surface))
439                                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
440                         return true;
441                 }
442         }
443
444         return false;
445 }
446
447 static bool
448 ivi_check_pending_desktop_surface_remote(struct ivi_surface *surface)
449 {
450         struct pending_remote *remote_surf, *next_remote_surf;
451         struct ivi_compositor *ivi = surface->ivi;
452         const char *_app_id =
453                 weston_desktop_surface_get_app_id(surface->dsurface);
454
455         if (wl_list_empty(&ivi->remote_pending_apps) || !_app_id)
456                 return false;
457
458         wl_list_for_each_safe(remote_surf, next_remote_surf,
459                               &ivi->remote_pending_apps, link) {
460                 if (!strcmp(_app_id, remote_surf->app_id)) {
461                         surface->remote.output = remote_surf->ioutput;
462                         if (!ivi_compositor_keep_pending_surfaces(surface))
463                                 ivi_remove_pending_desktop_surface_remote(remote_surf);
464                         return true;
465                 }
466         }
467
468         return false;
469 }
470
471 bool
472 ivi_check_pending_surface(struct ivi_surface *surface)
473 {
474         struct ivi_compositor *ivi = surface->ivi;
475         struct wl_list *role_pending_list;
476         struct pending_popup *p_popup;
477         struct pending_split *p_split;
478         struct pending_fullscreen *p_fullscreen;
479         struct pending_remote *p_remote;
480         const char *app_id =
481                 weston_desktop_surface_get_app_id(surface->dsurface);
482
483         if (!app_id)
484                 return false;
485
486         role_pending_list = &ivi->popup_pending_apps;
487         wl_list_for_each(p_popup, role_pending_list, link) {
488                 if (!strcmp(app_id, p_popup->app_id)) {
489                         return true;
490                 }
491         }
492
493         role_pending_list = &ivi->split_pending_apps;
494         wl_list_for_each(p_split, role_pending_list, link) {
495                 if (!strcmp(app_id, p_split->app_id)) {
496                         return true;
497                 }
498         }
499
500         role_pending_list = &ivi->fullscreen_pending_apps;
501         wl_list_for_each(p_fullscreen, role_pending_list, link) {
502                 if (!strcmp(app_id, p_fullscreen->app_id)) {
503                         return true;
504                 }
505         }
506
507         role_pending_list = &ivi->remote_pending_apps;
508         wl_list_for_each(p_remote, role_pending_list, link) {
509                 if (!strcmp(app_id, p_remote->app_id)) {
510                         return true;
511                 }
512         }
513
514         /* else, we are a regular desktop surface */
515         return false;
516 }
517
518
519 void
520 ivi_check_pending_desktop_surface(struct ivi_surface *surface)
521 {
522         bool ret = false;
523
524         ret = ivi_check_pending_desktop_surface_popup(surface);
525         if (ret) {
526                 ivi_set_desktop_surface_popup(surface);
527                 ivi_layout_popup_committed(surface);
528                 return;
529         }
530
531         ret = ivi_check_pending_desktop_surface_split(surface);
532         if (ret) {
533                 ivi_set_desktop_surface_split(surface);
534                 ivi_layout_split_committed(surface);
535                 return;
536         }
537
538         ret = ivi_check_pending_desktop_surface_fullscreen(surface);
539         if (ret) {
540                 ivi_set_desktop_surface_fullscreen(surface);
541                 ivi_layout_fullscreen_committed(surface);
542                 return;
543         }
544
545         ret = ivi_check_pending_desktop_surface_remote(surface);
546         if (ret) {
547                 ivi_set_desktop_surface_remote(surface);
548                 ivi_layout_desktop_committed(surface);
549                 return;
550         }
551
552         /* if we end up here means we have a regular desktop app and
553          * try to activate it */
554         ivi_set_desktop_surface(surface);
555         ivi_layout_desktop_committed(surface);
556 }
557
558 void
559 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
560 {
561         struct ivi_output *out;
562
563         wl_list_for_each(out, &ivi->outputs, link) {
564                 create_black_surface_view(out);
565                 insert_black_surface(out);
566         }
567 }
568
569 int
570 ivi_shell_init(struct ivi_compositor *ivi)
571 {
572         weston_layer_init(&ivi->hidden, ivi->compositor);
573         weston_layer_init(&ivi->background, ivi->compositor);
574         weston_layer_init(&ivi->normal, ivi->compositor);
575         weston_layer_init(&ivi->panel, ivi->compositor);
576         weston_layer_init(&ivi->popup, ivi->compositor);
577         weston_layer_init(&ivi->fullscreen, ivi->compositor);
578
579         weston_layer_set_position(&ivi->hidden,
580                                   WESTON_LAYER_POSITION_HIDDEN);
581         weston_layer_set_position(&ivi->background,
582                                   WESTON_LAYER_POSITION_BACKGROUND);
583         weston_layer_set_position(&ivi->normal,
584                                   WESTON_LAYER_POSITION_NORMAL);
585         weston_layer_set_position(&ivi->panel,
586                                   WESTON_LAYER_POSITION_UI);
587         weston_layer_set_position(&ivi->popup,
588                                   WESTON_LAYER_POSITION_TOP_UI);
589         weston_layer_set_position(&ivi->fullscreen,
590                                   WESTON_LAYER_POSITION_FULLSCREEN);
591
592         return 0;
593 }
594
595 static void
596 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
597 {
598         struct ivi_surface *surface;
599
600         wl_list_for_each(surface, &ivi->surfaces, link) {
601                 const char *app_id =
602                         weston_desktop_surface_get_app_id(surface->dsurface);
603                 if (app_id == NULL) {
604                         weston_log("WARNING app_is is null, unable to advertise\n");
605                         return;
606                 }
607                 agl_shell_desktop_send_application(resource, app_id);
608         }
609 }
610
611 static void
612 client_exec(const char *command, int fd)
613 {
614         sigset_t sig;
615         char s[32];
616
617         /* Don't give the child our signal mask */
618         sigfillset(&sig);
619         sigprocmask(SIG_UNBLOCK, &sig, NULL);
620
621         /* Launch clients as the user; don't give them the wrong euid */
622         if (seteuid(getuid()) == -1) {
623                 weston_log("seteuid failed: %s\n", strerror(errno));
624                 return;
625         }
626
627         /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
628          * clobbering fd, as we'll exit/exec either way.
629          */
630         fd = dup(fd);
631         if (fd == -1) {
632                 weston_log("dup failed: %s\n", strerror(errno));
633                 return;
634         }
635
636         snprintf(s, sizeof s, "%d", fd);
637         setenv("WAYLAND_SOCKET", s, 1);
638
639         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
640         weston_log("executing '%s' failed: %s", command, strerror(errno));
641 }
642
643 static struct wl_client *
644 launch_shell_client(struct ivi_compositor *ivi, const char *command)
645 {
646         struct wl_client *client;
647         int sock[2];
648         pid_t pid;
649
650         weston_log("launching' %s'\n", command);
651
652         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
653                 weston_log("socketpair failed while launching '%s': %s\n",
654                            command, strerror(errno));
655                 return NULL;
656         }
657
658         pid = fork();
659         if (pid == -1) {
660                 close(sock[0]);
661                 close(sock[1]);
662                 weston_log("fork failed while launching '%s': %s\n",
663                            command, strerror(errno));
664                 return NULL;
665         }
666
667         if (pid == 0) {
668                 client_exec(command, sock[1]);
669                 _Exit(EXIT_FAILURE);
670         }
671         close(sock[1]);
672
673         client = wl_client_create(ivi->compositor->wl_display, sock[0]);
674         if (!client) {
675                 close(sock[0]);
676                 weston_log("Failed to create wayland client for '%s'",
677                            command);
678                 return NULL;
679         }
680
681         return client;
682 }
683
684 int
685 ivi_launch_shell_client(struct ivi_compositor *ivi)
686 {
687         struct weston_config_section *section;
688         char *command = NULL;
689
690         section = weston_config_get_section(ivi->config, "shell-client",
691                                             NULL, NULL);
692         if (section)
693                 weston_config_section_get_string(section, "command",
694                                                  &command, NULL);
695
696         if (!command)
697                 return -1;
698
699         ivi->shell_client.client = launch_shell_client(ivi, command);
700         if (!ivi->shell_client.client)
701                 return -1;
702
703         return 0;
704 }
705
706 static void
707 destroy_black_view(struct wl_listener *listener, void *data)
708 {
709         struct fullscreen_view *fs =
710                 wl_container_of(listener, fs, fs_destroy);
711
712
713         if (fs && fs->fs) {
714                 wl_list_remove(&fs->fs_destroy.link);
715                 free(fs->fs);
716         }
717 }
718
719
720 static void
721 create_black_surface_view(struct ivi_output *output)
722 {
723         struct weston_surface *surface = NULL;
724         struct weston_view *view;
725         struct ivi_compositor *ivi = output->ivi;
726         struct weston_compositor *wc= ivi->compositor;
727         struct weston_output *woutput = output->output;
728
729         if (!woutput)
730                 return;
731
732         surface = weston_surface_create(wc);
733         view = weston_view_create(surface);
734
735         assert(view || surface);
736
737         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
738         weston_surface_set_size(surface, woutput->width, woutput->height);
739         weston_view_set_position(view, woutput->x, woutput->y);
740
741         output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
742         output->fullscreen_view.fs->view = view;
743
744         output->fullscreen_view.fs_destroy.notify = destroy_black_view;
745         wl_signal_add(&woutput->destroy_signal,
746                       &output->fullscreen_view.fs_destroy);
747 }
748
749 void
750 remove_black_surface(struct ivi_output *output)
751 {
752         struct weston_view *view;
753
754         if (!output &&
755             !output->fullscreen_view.fs &&
756             !output->fullscreen_view.fs->view) {
757                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
758                 return;
759         }
760
761         view = output->fullscreen_view.fs->view;
762         assert(view->is_mapped == true ||
763                view->surface->is_mapped == true);
764
765         view->is_mapped = false;
766         view->surface->is_mapped = false;
767
768         weston_layer_entry_remove(&view->layer_link);
769         weston_view_update_transform(view);
770
771         weston_view_damage_below(view);
772 }
773
774 void
775 insert_black_surface(struct ivi_output *output)
776 {
777         struct weston_view *view;
778
779         if ((!output &&
780             !output->fullscreen_view.fs &&
781             !output->fullscreen_view.fs->view) || !output->output) {
782                 weston_log("Output %s doesn't have a surface installed!\n", output->name);
783                 return;
784         }
785
786         view = output->fullscreen_view.fs->view;
787         if (view->is_mapped || view->surface->is_mapped)
788                 return;
789
790         weston_layer_entry_remove(&view->layer_link);
791         weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
792                                   &view->layer_link);
793
794         view->is_mapped = true;
795         view->surface->is_mapped = true;
796
797         weston_view_update_transform(view);
798         weston_view_damage_below(view);
799 }
800
801 static void
802 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
803 {
804         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
805         struct ivi_output *output;
806         struct ivi_surface *surface, *tmp;
807
808         /* Init already finished. Do nothing */
809         if (ivi->shell_client.ready)
810                 return;
811
812         ivi->shell_client.ready = true;
813
814         wl_list_for_each(output, &ivi->outputs, link) {
815                 if (output->background)
816                         remove_black_surface(output);
817                 ivi_layout_init(ivi, output);
818         }
819
820         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
821                 wl_list_remove(&surface->link);
822                 wl_list_init(&surface->link);
823                 ivi_check_pending_desktop_surface(surface);
824                 surface->checked_pending = true;
825         }
826 }
827
828 static void
829 shell_set_background(struct wl_client *client,
830                      struct wl_resource *shell_res,
831                      struct wl_resource *surface_res,
832                      struct wl_resource *output_res)
833 {
834         struct weston_head *head = weston_head_from_resource(output_res);
835         struct weston_output *woutput = weston_head_get_output(head);
836         struct ivi_output *output = to_ivi_output(woutput);
837         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
838         struct weston_desktop_surface *dsurface;
839         struct ivi_surface *surface;
840
841         dsurface = weston_surface_get_desktop_surface(wsurface);
842         if (!dsurface) {
843                 wl_resource_post_error(shell_res,
844                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
845                                        "surface must be a desktop surface");
846                 return;
847         }
848
849         surface = weston_desktop_surface_get_user_data(dsurface);
850         if (surface->role != IVI_SURFACE_ROLE_NONE) {
851                 wl_resource_post_error(shell_res,
852                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
853                                        "surface already has another ivi role");
854                 return;
855         }
856
857         if (output->background) {
858                 wl_resource_post_error(shell_res,
859                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
860                                        "output already has background");
861                 return;
862         }
863
864         surface->checked_pending = true;
865         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
866         surface->bg.output = output;
867         wl_list_remove(&surface->link);
868         wl_list_init(&surface->link);
869
870         output->background = surface;
871
872         weston_desktop_surface_set_maximized(dsurface, true);
873         weston_desktop_surface_set_size(dsurface,
874                                         output->output->width,
875                                         output->output->height);
876 }
877
878 static void
879 shell_set_panel(struct wl_client *client,
880                 struct wl_resource *shell_res,
881                 struct wl_resource *surface_res,
882                 struct wl_resource *output_res,
883                 uint32_t edge)
884 {
885         struct weston_head *head = weston_head_from_resource(output_res);
886         struct weston_output *woutput = weston_head_get_output(head);
887         struct ivi_output *output = to_ivi_output(woutput);
888         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
889         struct weston_desktop_surface *dsurface;
890         struct ivi_surface *surface;
891         struct ivi_surface **member;
892         int32_t width = 0, height = 0;
893
894         dsurface = weston_surface_get_desktop_surface(wsurface);
895         if (!dsurface) {
896                 wl_resource_post_error(shell_res,
897                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
898                                        "surface must be a desktop surface");
899                 return;
900         }
901
902         surface = weston_desktop_surface_get_user_data(dsurface);
903         if (surface->role != IVI_SURFACE_ROLE_NONE) {
904                 wl_resource_post_error(shell_res,
905                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
906                                        "surface already has another ivi role");
907                 return;
908         }
909
910         switch (edge) {
911         case AGL_SHELL_EDGE_TOP:
912                 member = &output->top;
913                 break;
914         case AGL_SHELL_EDGE_BOTTOM:
915                 member = &output->bottom;
916                 break;
917         case AGL_SHELL_EDGE_LEFT:
918                 member = &output->left;
919                 break;
920         case AGL_SHELL_EDGE_RIGHT:
921                 member = &output->right;
922                 break;
923         default:
924                 wl_resource_post_error(shell_res,
925                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
926                                        "invalid edge for panel");
927                 return;
928         }
929
930         if (*member) {
931                 wl_resource_post_error(shell_res,
932                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
933                                        "output already has panel on this edge");
934                 return;
935         }
936
937         surface->checked_pending = true;
938         surface->role = IVI_SURFACE_ROLE_PANEL;
939         surface->panel.output = output;
940         surface->panel.edge = edge;
941         wl_list_remove(&surface->link);
942         wl_list_init(&surface->link);
943
944         *member = surface;
945
946         switch (surface->panel.edge) {
947         case AGL_SHELL_EDGE_TOP:
948         case AGL_SHELL_EDGE_BOTTOM:
949                 width = woutput->width;
950                 break;
951         case AGL_SHELL_EDGE_LEFT:
952         case AGL_SHELL_EDGE_RIGHT:
953                 height = woutput->height;
954                 break;
955         }
956
957         weston_desktop_surface_set_size(dsurface, width, height);
958 }
959
960 void
961 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
962                           const char *data, uint32_t app_state)
963 {
964         struct desktop_client *dclient;
965         uint32_t app_role;
966         struct ivi_surface *surf = ivi_find_app(ivi, app_id);
967         struct ivi_policy *policy = ivi->policy;
968
969         /* FIXME: should queue it here and see when binding agl-shell-desktop
970          * if there are any to be sent */
971         if (!surf)
972                 return;
973
974         if (!app_id)
975                 return;
976
977         if (policy && policy->api.surface_advertise_state_change &&
978             !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
979                 return;
980         }
981
982         app_role = surf->role;
983         if (app_role == IVI_SURFACE_ROLE_POPUP)
984                 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
985
986         wl_list_for_each(dclient, &ivi->desktop_clients, link)
987                 agl_shell_desktop_send_state_app(dclient->resource, app_id,
988                                                  data, app_state, app_role);
989 }
990
991 static void
992 shell_activate_app(struct wl_client *client,
993                    struct wl_resource *shell_res,
994                    const char *app_id,
995                    struct wl_resource *output_res)
996 {
997         struct weston_head *head = weston_head_from_resource(output_res);
998         struct weston_output *woutput = weston_head_get_output(head);
999         struct ivi_output *output = to_ivi_output(woutput);
1000
1001         ivi_layout_activate(output, app_id);
1002 }
1003
1004 static void
1005 shell_desktop_activate_app(struct wl_client *client,
1006                            struct wl_resource *shell_res,
1007                            const char *app_id, const char *data,
1008                            struct wl_resource *output_res)
1009 {
1010         struct weston_head *head = weston_head_from_resource(output_res);
1011         struct weston_output *woutput = weston_head_get_output(head);
1012         struct ivi_output *output = to_ivi_output(woutput);
1013
1014         ivi_layout_activate(output, app_id);
1015         shell_advertise_app_state(output->ivi, app_id,
1016                                   data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
1017 }
1018
1019 static void
1020 shell_deactivate_app(struct wl_client *client,
1021                    struct wl_resource *shell_res,
1022                    const char *app_id)
1023 {
1024         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1025         struct ivi_compositor *ivi = dclient->ivi;
1026
1027         ivi_layout_deactivate(ivi, app_id);
1028         shell_advertise_app_state(ivi, app_id,
1029                                   NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
1030 }
1031
1032 static const struct agl_shell_interface agl_shell_implementation = {
1033         .ready = shell_ready,
1034         .set_background = shell_set_background,
1035         .set_panel = shell_set_panel,
1036         .activate_app = shell_activate_app,
1037 };
1038
1039 static void
1040 shell_desktop_set_app_property(struct wl_client *client,
1041                                struct wl_resource *shell_res,
1042                                const char *app_id, uint32_t role,
1043                                int x, int y, int bx, int by,
1044                                int width, int height,
1045                                struct wl_resource *output_res)
1046 {
1047         struct weston_head *head = weston_head_from_resource(output_res);
1048         struct weston_output *woutput = weston_head_get_output(head);
1049         struct ivi_output *output = to_ivi_output(woutput);
1050
1051         switch (role) {
1052         case AGL_SHELL_DESKTOP_APP_ROLE_POPUP:
1053                 ivi_set_pending_desktop_surface_popup(output, x, y, bx, by,
1054                                                       width, height, app_id);
1055                 break;
1056         case AGL_SHELL_DESKTOP_APP_ROLE_FULLSCREEN:
1057                 ivi_set_pending_desktop_surface_fullscreen(output, app_id);
1058                 break;
1059         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL:
1060         case AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL:
1061                 ivi_set_pending_desktop_surface_split(output, app_id, role);
1062                 break;
1063         case AGL_SHELL_DESKTOP_APP_ROLE_REMOTE:
1064                 ivi_set_pending_desktop_surface_remote(output, app_id);
1065                 break;
1066         default:
1067                 break;
1068         }
1069 }
1070
1071 void
1072 ivi_compositor_destroy_pending_surfaces(struct ivi_compositor *ivi)
1073 {
1074         struct pending_popup *p_popup, *next_p_popup;
1075         struct pending_split *split_surf, *next_split_surf;
1076         struct pending_fullscreen *fs_surf, *next_fs_surf;
1077         struct pending_remote *remote_surf, *next_remote_surf;
1078
1079         wl_list_for_each_safe(p_popup, next_p_popup,
1080                               &ivi->popup_pending_apps, link)
1081                 ivi_remove_pending_desktop_surface_popup(p_popup);
1082
1083         wl_list_for_each_safe(split_surf, next_split_surf,
1084                               &ivi->split_pending_apps, link)
1085                 ivi_remove_pending_desktop_surface_split(split_surf);
1086
1087         wl_list_for_each_safe(fs_surf, next_fs_surf,
1088                               &ivi->fullscreen_pending_apps, link)
1089                 ivi_remove_pending_desktop_surface_fullscreen(fs_surf);
1090
1091         wl_list_for_each_safe(remote_surf, next_remote_surf,
1092                               &ivi->remote_pending_apps, link)
1093                 ivi_remove_pending_desktop_surface_remote(remote_surf);
1094 }
1095
1096 static void
1097 shell_desktop_set_app_property_mode(struct wl_client *client,
1098                                     struct wl_resource *shell_res, uint32_t perm)
1099 {
1100         struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
1101         if (perm) {
1102                 dclient->ivi->keep_pending_surfaces = true;
1103         } else {
1104                 dclient->ivi->keep_pending_surfaces = false;
1105                 /* remove any previous pending surfaces */
1106                 ivi_compositor_destroy_pending_surfaces(dclient->ivi);
1107         }
1108 }
1109
1110 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
1111         .activate_app = shell_desktop_activate_app,
1112         .set_app_property = shell_desktop_set_app_property,
1113         .deactivate_app = shell_deactivate_app,
1114         .set_app_property_mode = shell_desktop_set_app_property_mode,
1115 };
1116
1117 static void
1118 unbind_agl_shell(struct wl_resource *resource)
1119 {
1120         struct ivi_compositor *ivi;
1121         struct ivi_output *output;
1122         struct ivi_surface *surf, *surf_tmp;
1123
1124         ivi = wl_resource_get_user_data(resource);
1125         wl_list_for_each(output, &ivi->outputs, link) {
1126                 /* reset the active surf if there's one present */
1127                 if (output->active) {
1128                         output->active->view->is_mapped = false;
1129                         output->active->view->surface->is_mapped = false;
1130
1131                         weston_layer_entry_remove(&output->active->view->layer_link);
1132                         output->active = NULL;
1133                 }
1134
1135                 insert_black_surface(output);
1136         }
1137
1138         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
1139                 wl_list_remove(&surf->link);
1140                 wl_list_init(&surf->link);
1141         }
1142
1143         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
1144                 wl_list_remove(&surf->link);
1145                 wl_list_init(&surf->link);
1146         }
1147
1148         wl_list_init(&ivi->surfaces);
1149         wl_list_init(&ivi->pending_surfaces);
1150
1151         ivi->shell_client.ready = false;
1152         ivi->shell_client.resource = NULL;
1153         ivi->shell_client.client = NULL;
1154 }
1155
1156 static void
1157 bind_agl_shell(struct wl_client *client,
1158                void *data, uint32_t version, uint32_t id)
1159 {
1160         struct ivi_compositor *ivi = data;
1161         struct wl_resource *resource;
1162         struct ivi_policy *policy;
1163         void *interface;
1164
1165         policy = ivi->policy;
1166         interface = (void *) &agl_shell_interface;
1167         if (policy && policy->api.shell_bind_interface &&
1168             !policy->api.shell_bind_interface(client, interface)) {
1169                 wl_client_post_implementation_error(client,
1170                                        "client not authorized to use agl_shell");
1171                 return;
1172         }
1173
1174         resource = wl_resource_create(client, &agl_shell_interface,
1175                                       1, id);
1176         if (!resource) {
1177                 wl_client_post_no_memory(client);
1178                 return;
1179         }
1180
1181 #if 0
1182         if (ivi->shell_client.client != client) {
1183                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1184                                        "client not authorized to use agl_shell");
1185                 return;
1186         }
1187 #endif
1188
1189         if (ivi->shell_client.resource) {
1190                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1191                                        "agl_shell has already been bound");
1192                 return;
1193         }
1194
1195         wl_resource_set_implementation(resource, &agl_shell_implementation,
1196                                        ivi, unbind_agl_shell);
1197         ivi->shell_client.resource = resource;
1198 }
1199
1200 static void
1201 unbind_agl_shell_desktop(struct wl_resource *resource)
1202 {
1203         struct desktop_client *dclient = wl_resource_get_user_data(resource);
1204
1205         wl_list_remove(&dclient->link);
1206         free(dclient);
1207 }
1208
1209 static void
1210 bind_agl_shell_desktop(struct wl_client *client,
1211                        void *data, uint32_t version, uint32_t id)
1212 {
1213         struct ivi_compositor *ivi = data;
1214         struct wl_resource *resource;
1215         struct ivi_policy *policy;
1216         struct desktop_client *dclient;
1217         void *interface;
1218
1219         policy = ivi->policy;
1220         interface  = (void *) &agl_shell_desktop_interface;
1221         if (policy && policy->api.shell_bind_interface &&
1222             !policy->api.shell_bind_interface(client, interface)) {
1223                 wl_client_post_implementation_error(client,
1224                                 "client not authorized to use agl_shell_desktop");
1225                 return;
1226         }
1227
1228         dclient = zalloc(sizeof(*dclient));
1229         if (!dclient) {
1230                 wl_client_post_no_memory(client);
1231                 return;
1232         }
1233
1234         resource = wl_resource_create(client, &agl_shell_desktop_interface,
1235                                       version, id);
1236         dclient->ivi = ivi;
1237         if (!resource) {
1238                 wl_client_post_no_memory(client);
1239                 return;
1240         }
1241
1242         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
1243                                        dclient, unbind_agl_shell_desktop);
1244
1245         dclient->resource = resource;
1246         wl_list_insert(&ivi->desktop_clients, &dclient->link);
1247
1248         /* advertise xdg surfaces */
1249         ivi_shell_advertise_xdg_surfaces(ivi, resource);
1250 }
1251
1252 int
1253 ivi_shell_create_global(struct ivi_compositor *ivi)
1254 {
1255         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
1256                                           &agl_shell_interface, 1,
1257                                           ivi, bind_agl_shell);
1258         if (!ivi->agl_shell) {
1259                 weston_log("Failed to create wayland global.\n");
1260                 return -1;
1261         }
1262
1263         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
1264                                                   &agl_shell_desktop_interface, 2,
1265                                                   ivi, bind_agl_shell_desktop);
1266         if (!ivi->agl_shell_desktop) {
1267                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
1268                 return -1;
1269         }
1270
1271         return 0;
1272 }