2 * Copyright © 2019 Collabora, Ltd.
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:
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.
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
26 #include "ivi-compositor.h"
34 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <libweston/libweston.h>
38 #include <libweston/config-parser.h>
40 #include "shared/os-compatibility.h"
42 #include "agl-shell-server-protocol.h"
43 #include "agl-shell-desktop-server-protocol.h"
46 create_black_surface_view(struct ivi_output *output);
49 insert_black_surface(struct ivi_output *output);
52 ivi_set_desktop_surface(struct ivi_surface *surface)
54 struct desktop_client *dclient;
55 struct ivi_compositor *ivi = surface->ivi;
56 assert(surface->role == IVI_SURFACE_ROLE_NONE);
58 surface->role = IVI_SURFACE_ROLE_DESKTOP;
59 wl_list_insert(&surface->ivi->surfaces, &surface->link);
61 /* advertise to all desktop clients the new surface */
62 wl_list_for_each(dclient, &ivi->desktop_clients, link) {
64 weston_desktop_surface_get_app_id(surface->dsurface);
65 agl_shell_desktop_send_application(dclient->resource, app_id);
70 ivi_set_desktop_surface_popup(struct ivi_surface *surface)
72 struct ivi_compositor *ivi = surface->ivi;
73 assert(surface->role == IVI_SURFACE_ROLE_NONE);
75 surface->role = IVI_SURFACE_ROLE_POPUP;
76 wl_list_insert(&ivi->surfaces, &surface->link);
80 ivi_set_pending_desktop_surface_popup(struct ivi_output *ioutput,
81 int x, int y, const char *app_id)
83 struct ivi_compositor *ivi = ioutput->ivi;
84 size_t len_app_id = strlen(app_id);
86 struct pending_popup *p_popup = zalloc(sizeof(*p_popup));
88 p_popup->app_id = zalloc(sizeof(char) * (len_app_id + 1));
89 memcpy(p_popup->app_id, app_id, len_app_id);
90 p_popup->ioutput = ioutput;
94 wl_list_insert(&ivi->popup_pending_apps, &p_popup->link);
98 ivi_remove_pending_desktop_surface_popup(struct pending_popup *p_popup)
100 free(p_popup->app_id);
101 wl_list_remove(&p_popup->link);
106 ivi_check_pending_desktop_surface_popup(struct ivi_surface *surface)
108 struct ivi_compositor *ivi = surface->ivi;
109 struct pending_popup *p_popup, *next_p_popup;
110 const char *_app_id =
111 weston_desktop_surface_get_app_id(surface->dsurface);
113 wl_list_for_each_safe(p_popup, next_p_popup,
114 &ivi->popup_pending_apps, link) {
115 if (!strcmp(_app_id, p_popup->app_id)) {
116 surface->popup.output = p_popup->ioutput;
117 surface->popup.x = p_popup->x;
118 surface->popup.y = p_popup->y;
120 ivi_remove_pending_desktop_surface_popup(p_popup);
129 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
131 struct ivi_output *out;
133 wl_list_for_each(out, &ivi->outputs, link) {
134 create_black_surface_view(out);
135 insert_black_surface(out);
140 ivi_shell_init(struct ivi_compositor *ivi)
142 weston_layer_init(&ivi->hidden, ivi->compositor);
143 weston_layer_init(&ivi->background, ivi->compositor);
144 weston_layer_init(&ivi->normal, ivi->compositor);
145 weston_layer_init(&ivi->panel, ivi->compositor);
146 weston_layer_init(&ivi->popup, ivi->compositor);
147 weston_layer_init(&ivi->fullscreen, ivi->compositor);
149 weston_layer_set_position(&ivi->hidden,
150 WESTON_LAYER_POSITION_HIDDEN);
151 weston_layer_set_position(&ivi->background,
152 WESTON_LAYER_POSITION_BACKGROUND);
153 weston_layer_set_position(&ivi->normal,
154 WESTON_LAYER_POSITION_NORMAL);
155 weston_layer_set_position(&ivi->panel,
156 WESTON_LAYER_POSITION_UI);
157 weston_layer_set_position(&ivi->popup,
158 WESTON_LAYER_POSITION_TOP_UI);
159 weston_layer_set_position(&ivi->fullscreen,
160 WESTON_LAYER_POSITION_FULLSCREEN);
166 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
168 struct ivi_surface *surface;
170 wl_list_for_each(surface, &ivi->surfaces, link) {
172 weston_desktop_surface_get_app_id(surface->dsurface);
173 agl_shell_desktop_send_application(resource, app_id);
178 client_exec(const char *command, int fd)
183 /* Don't give the child our signal mask */
185 sigprocmask(SIG_UNBLOCK, &sig, NULL);
187 /* Launch clients as the user; don't give them the wrong euid */
188 if (seteuid(getuid()) == -1) {
189 weston_log("seteuid failed: %s\n", strerror(errno));
193 /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
194 * clobbering fd, as we'll exit/exec either way.
198 weston_log("dup failed: %s\n", strerror(errno));
202 snprintf(s, sizeof s, "%d", fd);
203 setenv("WAYLAND_SOCKET", s, 1);
205 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
206 weston_log("executing '%s' failed: %s", command, strerror(errno));
209 static struct wl_client *
210 launch_shell_client(struct ivi_compositor *ivi, const char *command)
212 struct wl_client *client;
216 weston_log("launching' %s'\n", command);
218 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
219 weston_log("socketpair failed while launching '%s': %s\n",
220 command, strerror(errno));
228 weston_log("fork failed while launching '%s': %s\n",
229 command, strerror(errno));
234 client_exec(command, sock[1]);
239 client = wl_client_create(ivi->compositor->wl_display, sock[0]);
242 weston_log("Failed to create wayland client for '%s'",
251 ivi_launch_shell_client(struct ivi_compositor *ivi)
253 struct weston_config_section *section;
254 char *command = NULL;
256 section = weston_config_get_section(ivi->config, "shell-client",
259 weston_config_section_get_string(section, "command",
265 ivi->shell_client.client = launch_shell_client(ivi, command);
266 if (!ivi->shell_client.client)
273 destroy_black_view(struct wl_listener *listener, void *data)
275 struct fullscreen_view *fs =
276 wl_container_of(listener, fs, fs_destroy);
280 if (fs->fs->view && fs->fs->view->surface) {
281 weston_surface_destroy(fs->fs->view->surface);
286 wl_list_remove(&fs->fs_destroy.link);
292 create_black_surface_view(struct ivi_output *output)
294 struct weston_surface *surface = NULL;
295 struct weston_view *view;
296 struct ivi_compositor *ivi = output->ivi;
297 struct weston_compositor *wc= ivi->compositor;
298 struct weston_output *woutput = output->output;
300 surface = weston_surface_create(wc);
301 view = weston_view_create(surface);
303 assert(view || surface);
305 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
306 weston_surface_set_size(surface, woutput->width, woutput->height);
307 weston_view_set_position(view, woutput->x, woutput->y);
309 output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
310 output->fullscreen_view.fs->view = view;
312 output->fullscreen_view.fs_destroy.notify = destroy_black_view;
313 wl_signal_add(&woutput->destroy_signal,
314 &output->fullscreen_view.fs_destroy);
318 remove_black_surface(struct ivi_output *output)
320 struct weston_view *view = output->fullscreen_view.fs->view;
322 assert(view->is_mapped == true ||
323 view->surface->is_mapped == true);
325 view->is_mapped = false;
326 view->surface->is_mapped = false;
328 weston_layer_entry_remove(&view->layer_link);
329 weston_view_update_transform(view);
331 weston_output_damage(output->output);
335 insert_black_surface(struct ivi_output *output)
337 struct weston_view *view = output->fullscreen_view.fs->view;
339 if (view->is_mapped || view->surface->is_mapped)
342 weston_layer_entry_remove(&view->layer_link);
343 weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
346 view->is_mapped = true;
347 view->surface->is_mapped = true;
349 weston_view_update_transform(view);
350 weston_output_damage(output->output);
354 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
356 struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
357 struct ivi_output *output;
358 struct ivi_surface *surface, *tmp;
360 /* Init already finished. Do nothing */
361 if (ivi->shell_client.ready)
364 ivi->shell_client.ready = true;
366 wl_list_for_each(output, &ivi->outputs, link) {
367 remove_black_surface(output);
368 ivi_layout_init(ivi, output);
371 wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
372 wl_list_remove(&surface->link);
374 if (ivi_check_pending_desktop_surface_popup(surface)) {
375 ivi_set_desktop_surface_popup(surface);
377 ivi_set_desktop_surface(surface);
378 ivi_layout_desktop_committed(surface);
384 shell_set_background(struct wl_client *client,
385 struct wl_resource *shell_res,
386 struct wl_resource *surface_res,
387 struct wl_resource *output_res)
389 struct weston_head *head = weston_head_from_resource(output_res);
390 struct weston_output *woutput = weston_head_get_output(head);
391 struct ivi_output *output = to_ivi_output(woutput);
392 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
393 struct weston_desktop_surface *dsurface;
394 struct ivi_surface *surface;
396 dsurface = weston_surface_get_desktop_surface(wsurface);
398 wl_resource_post_error(shell_res,
399 AGL_SHELL_ERROR_INVALID_ARGUMENT,
400 "surface must be a desktop surface");
404 surface = weston_desktop_surface_get_user_data(dsurface);
405 if (surface->role != IVI_SURFACE_ROLE_NONE) {
406 wl_resource_post_error(shell_res,
407 AGL_SHELL_ERROR_INVALID_ARGUMENT,
408 "surface already has another ivi role");
412 if (output->background) {
413 wl_resource_post_error(shell_res,
414 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
415 "output already has background");
419 surface->role = IVI_SURFACE_ROLE_BACKGROUND;
420 surface->bg.output = output;
421 wl_list_remove(&surface->link);
422 wl_list_init(&surface->link);
424 output->background = surface;
426 weston_desktop_surface_set_maximized(dsurface, true);
427 weston_desktop_surface_set_size(dsurface,
428 output->output->width,
429 output->output->height);
433 shell_set_panel(struct wl_client *client,
434 struct wl_resource *shell_res,
435 struct wl_resource *surface_res,
436 struct wl_resource *output_res,
439 struct weston_head *head = weston_head_from_resource(output_res);
440 struct weston_output *woutput = weston_head_get_output(head);
441 struct ivi_output *output = to_ivi_output(woutput);
442 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
443 struct weston_desktop_surface *dsurface;
444 struct ivi_surface *surface;
445 struct ivi_surface **member;
446 int32_t width = 0, height = 0;
448 dsurface = weston_surface_get_desktop_surface(wsurface);
450 wl_resource_post_error(shell_res,
451 AGL_SHELL_ERROR_INVALID_ARGUMENT,
452 "surface must be a desktop surface");
456 surface = weston_desktop_surface_get_user_data(dsurface);
457 if (surface->role != IVI_SURFACE_ROLE_NONE) {
458 wl_resource_post_error(shell_res,
459 AGL_SHELL_ERROR_INVALID_ARGUMENT,
460 "surface already has another ivi role");
465 case AGL_SHELL_EDGE_TOP:
466 member = &output->top;
468 case AGL_SHELL_EDGE_BOTTOM:
469 member = &output->bottom;
471 case AGL_SHELL_EDGE_LEFT:
472 member = &output->left;
474 case AGL_SHELL_EDGE_RIGHT:
475 member = &output->right;
478 wl_resource_post_error(shell_res,
479 AGL_SHELL_ERROR_INVALID_ARGUMENT,
480 "invalid edge for panel");
485 wl_resource_post_error(shell_res,
486 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
487 "output already has panel on this edge");
491 surface->role = IVI_SURFACE_ROLE_PANEL;
492 surface->panel.output = output;
493 surface->panel.edge = edge;
494 wl_list_remove(&surface->link);
495 wl_list_init(&surface->link);
499 switch (surface->panel.edge) {
500 case AGL_SHELL_EDGE_TOP:
501 case AGL_SHELL_EDGE_BOTTOM:
502 width = woutput->width;
504 case AGL_SHELL_EDGE_LEFT:
505 case AGL_SHELL_EDGE_RIGHT:
506 height = woutput->height;
510 weston_desktop_surface_set_size(dsurface, width, height);
515 shell_advertise_app_state(struct ivi_compositor *ivi, const char *app_id,
516 const char *data, uint32_t app_state)
518 struct desktop_client *dclient;
520 struct ivi_surface *surf = ivi_find_app(ivi, app_id);
521 struct ivi_policy *policy = ivi->policy;
523 /* FIXME: should queue it here and see when binding agl-shell-desktop
524 * if there are any to be sent */
528 if (policy && policy->api.surface_advertise_state_change &&
529 !policy->api.surface_advertise_state_change(surf, surf->ivi)) {
533 app_role = surf->role;
534 if (app_role == IVI_SURFACE_ROLE_POPUP)
535 app_role = AGL_SHELL_DESKTOP_APP_ROLE_POPUP;
537 wl_list_for_each(dclient, &ivi->desktop_clients, link)
538 agl_shell_desktop_send_state_app(dclient->resource, app_id,
539 data, app_state, app_role);
543 shell_activate_app(struct wl_client *client,
544 struct wl_resource *shell_res,
546 struct wl_resource *output_res)
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);
552 ivi_layout_activate(output, app_id);
556 shell_desktop_activate_app(struct wl_client *client,
557 struct wl_resource *shell_res,
558 const char *app_id, const char *data,
559 struct wl_resource *output_res)
561 struct weston_head *head = weston_head_from_resource(output_res);
562 struct weston_output *woutput = weston_head_get_output(head);
563 struct ivi_output *output = to_ivi_output(woutput);
565 ivi_layout_activate(output, app_id);
566 shell_advertise_app_state(output->ivi, app_id,
567 data, AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED);
571 shell_deactivate_app(struct wl_client *client,
572 struct wl_resource *shell_res,
575 struct desktop_client *dclient = wl_resource_get_user_data(shell_res);
576 struct ivi_compositor *ivi = dclient->ivi;
578 ivi_layout_deactivate(ivi, app_id);
579 shell_advertise_app_state(ivi, app_id,
580 NULL, AGL_SHELL_DESKTOP_APP_STATE_DEACTIVATED);
583 static const struct agl_shell_interface agl_shell_implementation = {
584 .ready = shell_ready,
585 .set_background = shell_set_background,
586 .set_panel = shell_set_panel,
587 .activate_app = shell_activate_app,
591 shell_desktop_set_app_property(struct wl_client *client,
592 struct wl_resource *shell_res,
593 const char *app_id, uint32_t role,
594 int x, int y, struct wl_resource *output_res)
596 struct weston_head *head = weston_head_from_resource(output_res);
597 struct weston_output *woutput = weston_head_get_output(head);
598 struct ivi_output *output = to_ivi_output(woutput);
600 /* temporary store the app_id such that, when created to be check against */
601 if (role == AGL_SHELL_DESKTOP_APP_ROLE_POPUP)
602 ivi_set_pending_desktop_surface_popup(output, x, y, app_id);
605 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
606 .activate_app = shell_desktop_activate_app,
607 .set_app_property = shell_desktop_set_app_property,
608 .deactivate_app = shell_deactivate_app,
612 unbind_agl_shell(struct wl_resource *resource)
614 struct ivi_compositor *ivi;
615 struct ivi_output *output;
616 struct ivi_surface *surf, *surf_tmp;
618 ivi = wl_resource_get_user_data(resource);
619 wl_list_for_each(output, &ivi->outputs, link) {
620 /* reset the active surf if there's one present */
621 if (output->active) {
622 output->active->view->is_mapped = false;
623 output->active->view->surface->is_mapped = false;
625 weston_layer_entry_remove(&output->active->view->layer_link);
626 output->active = NULL;
629 insert_black_surface(output);
632 wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
633 wl_list_remove(&surf->link);
634 wl_list_init(&surf->link);
637 wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
638 wl_list_remove(&surf->link);
639 wl_list_init(&surf->link);
642 wl_list_init(&ivi->surfaces);
643 wl_list_init(&ivi->pending_surfaces);
645 ivi->shell_client.ready = false;
646 ivi->shell_client.resource = NULL;
647 ivi->shell_client.client = NULL;
651 bind_agl_shell(struct wl_client *client,
652 void *data, uint32_t version, uint32_t id)
654 struct ivi_compositor *ivi = data;
655 struct wl_resource *resource;
657 resource = wl_resource_create(client, &agl_shell_interface,
660 wl_client_post_no_memory(client);
665 if (ivi->shell_client.client != client) {
666 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
667 "client not authorized to use agl_shell");
672 if (ivi->shell_client.resource) {
673 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
674 "agl_shell has already been bound");
678 wl_resource_set_implementation(resource, &agl_shell_implementation,
679 ivi, unbind_agl_shell);
680 ivi->shell_client.resource = resource;
684 unbind_agl_shell_desktop(struct wl_resource *resource)
686 struct desktop_client *dclient = wl_resource_get_user_data(resource);
688 wl_list_remove(&dclient->link);
693 bind_agl_shell_desktop(struct wl_client *client,
694 void *data, uint32_t version, uint32_t id)
696 struct ivi_compositor *ivi = data;
697 struct wl_resource *resource;
698 struct desktop_client *dclient = zalloc(sizeof(*dclient));
701 wl_client_post_no_memory(client);
705 resource = wl_resource_create(client, &agl_shell_desktop_interface,
709 wl_client_post_no_memory(client);
713 wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
714 dclient, unbind_agl_shell_desktop);
716 dclient->resource = resource;
717 wl_list_insert(&ivi->desktop_clients, &dclient->link);
719 /* advertise xdg surfaces */
720 ivi_shell_advertise_xdg_surfaces(ivi, resource);
724 ivi_shell_create_global(struct ivi_compositor *ivi)
726 ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
727 &agl_shell_interface, 1,
728 ivi, bind_agl_shell);
729 if (!ivi->agl_shell) {
730 weston_log("Failed to create wayland global.\n");
734 ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
735 &agl_shell_desktop_interface, 1,
736 ivi, bind_agl_shell_desktop);
737 if (!ivi->agl_shell_desktop) {
738 weston_log("Failed to create wayland global (agl_shell_desktop).\n");