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