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