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