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