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