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"
33 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <libweston/libweston.h>
37 #include <libweston/config-parser.h>
39 #include "shared/os-compatibility.h"
41 #include "agl-shell-server-protocol.h"
42 #include "agl-shell-desktop-server-protocol.h"
45 create_black_surface_view(struct ivi_output *output);
48 insert_black_surface(struct ivi_output *output);
51 ivi_set_desktop_surface(struct ivi_surface *surface)
53 struct desktop_client *dclient;
54 struct ivi_compositor *ivi = surface->ivi;
55 assert(surface->role == IVI_SURFACE_ROLE_NONE);
57 surface->role = IVI_SURFACE_ROLE_DESKTOP;
58 wl_list_insert(&surface->ivi->surfaces, &surface->link);
60 /* advertise to all desktop clients the new surface */
61 wl_list_for_each(dclient, &ivi->desktop_clients, link) {
63 weston_desktop_surface_get_app_id(surface->dsurface);
64 agl_shell_desktop_send_application(dclient->resource, app_id);
69 ivi_set_desktop_surface_popup(struct ivi_surface *surface)
71 struct ivi_compositor *ivi = surface->ivi;
72 assert(surface->role == IVI_SURFACE_ROLE_NONE);
74 surface->role = IVI_SURFACE_ROLE_POPUP;
75 wl_list_insert(&ivi->surfaces, &surface->link);
79 ivi_set_pending_desktop_surface_popup(struct ivi_output *ioutput,
80 int x, int y, const char *app_id)
82 struct ivi_compositor *ivi = ioutput->ivi;
83 size_t len_app_id = strlen(app_id);
85 struct pending_popup *p_popup = zalloc(sizeof(*p_popup));
87 p_popup->app_id = zalloc(sizeof(char) * (len_app_id + 1));
88 memcpy(p_popup->app_id, app_id, len_app_id);
89 p_popup->ioutput = ioutput;
93 wl_list_insert(&ivi->popup_pending_apps, &p_popup->link);
97 ivi_remove_pending_desktop_surface_popup(struct pending_popup *p_popup)
99 free(p_popup->app_id);
100 wl_list_remove(&p_popup->link);
105 ivi_check_pending_desktop_surface_popup(struct ivi_surface *surface)
107 struct ivi_compositor *ivi = surface->ivi;
108 struct pending_popup *p_popup, *next_p_popup;
109 const char *_app_id =
110 weston_desktop_surface_get_app_id(surface->dsurface);
112 wl_list_for_each_safe(p_popup, next_p_popup,
113 &ivi->popup_pending_apps, link) {
114 if (!strcmp(_app_id, p_popup->app_id)) {
115 surface->popup.output = p_popup->ioutput;
116 surface->popup.x = p_popup->x;
117 surface->popup.y = p_popup->y;
119 ivi_remove_pending_desktop_surface_popup(p_popup);
128 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
130 struct ivi_output *out;
132 wl_list_for_each(out, &ivi->outputs, link) {
133 create_black_surface_view(out);
134 insert_black_surface(out);
139 ivi_shell_init(struct ivi_compositor *ivi)
141 weston_layer_init(&ivi->hidden, ivi->compositor);
142 weston_layer_init(&ivi->background, ivi->compositor);
143 weston_layer_init(&ivi->normal, ivi->compositor);
144 weston_layer_init(&ivi->panel, ivi->compositor);
145 weston_layer_init(&ivi->popup, ivi->compositor);
146 weston_layer_init(&ivi->fullscreen, ivi->compositor);
148 weston_layer_set_position(&ivi->hidden,
149 WESTON_LAYER_POSITION_HIDDEN);
150 weston_layer_set_position(&ivi->background,
151 WESTON_LAYER_POSITION_BACKGROUND);
152 weston_layer_set_position(&ivi->normal,
153 WESTON_LAYER_POSITION_NORMAL);
154 weston_layer_set_position(&ivi->panel,
155 WESTON_LAYER_POSITION_UI);
156 weston_layer_set_position(&ivi->popup,
157 WESTON_LAYER_POSITION_TOP_UI);
158 weston_layer_set_position(&ivi->fullscreen,
159 WESTON_LAYER_POSITION_FULLSCREEN);
165 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
167 struct ivi_surface *surface;
169 wl_list_for_each(surface, &ivi->surfaces, link) {
171 weston_desktop_surface_get_app_id(surface->dsurface);
172 agl_shell_desktop_send_application(resource, app_id);
177 client_exec(const char *command, int fd)
182 /* Don't give the child our signal mask */
184 sigprocmask(SIG_UNBLOCK, &sig, NULL);
186 /* Launch clients as the user; don't give them the wrong euid */
187 if (seteuid(getuid()) == -1) {
188 weston_log("seteuid failed: %s\n", strerror(errno));
192 /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
193 * clobbering fd, as we'll exit/exec either way.
197 weston_log("dup failed: %s\n", strerror(errno));
201 snprintf(s, sizeof s, "%d", fd);
202 setenv("WAYLAND_SOCKET", s, 1);
204 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
205 weston_log("executing '%s' failed: %s", command, strerror(errno));
208 static struct wl_client *
209 launch_shell_client(struct ivi_compositor *ivi, const char *command)
211 struct wl_client *client;
215 weston_log("launching' %s'\n", command);
217 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
218 weston_log("socketpair failed while launching '%s': %s\n",
219 command, strerror(errno));
227 weston_log("fork failed while launching '%s': %s\n",
228 command, strerror(errno));
233 client_exec(command, sock[1]);
238 client = wl_client_create(ivi->compositor->wl_display, sock[0]);
241 weston_log("Failed to create wayland client for '%s'",
250 ivi_launch_shell_client(struct ivi_compositor *ivi)
252 struct weston_config_section *section;
253 char *command = NULL;
255 section = weston_config_get_section(ivi->config, "shell-client",
258 weston_config_section_get_string(section, "command",
264 ivi->shell_client.client = launch_shell_client(ivi, command);
265 if (!ivi->shell_client.client)
272 destroy_black_view(struct wl_listener *listener, void *data)
274 struct fullscreen_view *fs =
275 wl_container_of(listener, fs, fs_destroy);
279 if (fs->fs->view && fs->fs->view->surface) {
280 weston_surface_destroy(fs->fs->view->surface);
285 wl_list_remove(&fs->fs_destroy.link);
291 create_black_surface_view(struct ivi_output *output)
293 struct weston_surface *surface = NULL;
294 struct weston_view *view;
295 struct ivi_compositor *ivi = output->ivi;
296 struct weston_compositor *wc= ivi->compositor;
297 struct weston_output *woutput = output->output;
299 surface = weston_surface_create(wc);
300 view = weston_view_create(surface);
302 assert(view || surface);
304 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
305 weston_surface_set_size(surface, woutput->width, woutput->height);
306 weston_view_set_position(view, woutput->x, woutput->y);
308 output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
309 output->fullscreen_view.fs->view = view;
311 output->fullscreen_view.fs_destroy.notify = destroy_black_view;
312 wl_signal_add(&woutput->destroy_signal,
313 &output->fullscreen_view.fs_destroy);
317 remove_black_surface(struct ivi_output *output)
319 struct weston_view *view = output->fullscreen_view.fs->view;
321 assert(view->is_mapped == true ||
322 view->surface->is_mapped == true);
324 view->is_mapped = false;
325 view->surface->is_mapped = false;
327 weston_layer_entry_remove(&view->layer_link);
328 weston_view_update_transform(view);
330 weston_output_damage(output->output);
334 insert_black_surface(struct ivi_output *output)
336 struct weston_view *view = output->fullscreen_view.fs->view;
338 if (view->is_mapped || view->surface->is_mapped)
341 weston_layer_entry_remove(&view->layer_link);
342 weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
345 view->is_mapped = true;
346 view->surface->is_mapped = true;
348 weston_view_update_transform(view);
349 weston_output_damage(output->output);
353 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
355 struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
356 struct ivi_output *output;
357 struct ivi_surface *surface, *tmp;
359 /* Init already finished. Do nothing */
360 if (ivi->shell_client.ready)
363 ivi->shell_client.ready = true;
365 wl_list_for_each(output, &ivi->outputs, link) {
366 remove_black_surface(output);
367 ivi_layout_init(ivi, output);
370 wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
371 wl_list_remove(&surface->link);
373 if (ivi_check_pending_desktop_surface_popup(surface)) {
374 ivi_set_desktop_surface_popup(surface);
376 ivi_set_desktop_surface(surface);
377 ivi_layout_desktop_committed(surface);
383 shell_set_background(struct wl_client *client,
384 struct wl_resource *shell_res,
385 struct wl_resource *surface_res,
386 struct wl_resource *output_res)
388 struct weston_head *head = weston_head_from_resource(output_res);
389 struct weston_output *woutput = weston_head_get_output(head);
390 struct ivi_output *output = to_ivi_output(woutput);
391 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
392 struct weston_desktop_surface *dsurface;
393 struct ivi_surface *surface;
395 dsurface = weston_surface_get_desktop_surface(wsurface);
397 wl_resource_post_error(shell_res,
398 AGL_SHELL_ERROR_INVALID_ARGUMENT,
399 "surface must be a desktop surface");
403 surface = weston_desktop_surface_get_user_data(dsurface);
404 if (surface->role != IVI_SURFACE_ROLE_NONE) {
405 wl_resource_post_error(shell_res,
406 AGL_SHELL_ERROR_INVALID_ARGUMENT,
407 "surface already has another ivi role");
411 if (output->background) {
412 wl_resource_post_error(shell_res,
413 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
414 "output already has background");
418 surface->role = IVI_SURFACE_ROLE_BACKGROUND;
419 surface->bg.output = output;
420 wl_list_remove(&surface->link);
421 wl_list_init(&surface->link);
423 output->background = surface;
425 weston_desktop_surface_set_maximized(dsurface, true);
426 weston_desktop_surface_set_size(dsurface,
427 output->output->width,
428 output->output->height);
432 shell_set_panel(struct wl_client *client,
433 struct wl_resource *shell_res,
434 struct wl_resource *surface_res,
435 struct wl_resource *output_res,
438 struct weston_head *head = weston_head_from_resource(output_res);
439 struct weston_output *woutput = weston_head_get_output(head);
440 struct ivi_output *output = to_ivi_output(woutput);
441 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
442 struct weston_desktop_surface *dsurface;
443 struct ivi_surface *surface;
444 struct ivi_surface **member;
445 int32_t width = 0, height = 0;
447 dsurface = weston_surface_get_desktop_surface(wsurface);
449 wl_resource_post_error(shell_res,
450 AGL_SHELL_ERROR_INVALID_ARGUMENT,
451 "surface must be a desktop surface");
455 surface = weston_desktop_surface_get_user_data(dsurface);
456 if (surface->role != IVI_SURFACE_ROLE_NONE) {
457 wl_resource_post_error(shell_res,
458 AGL_SHELL_ERROR_INVALID_ARGUMENT,
459 "surface already has another ivi role");
464 case AGL_SHELL_EDGE_TOP:
465 member = &output->top;
467 case AGL_SHELL_EDGE_BOTTOM:
468 member = &output->bottom;
470 case AGL_SHELL_EDGE_LEFT:
471 member = &output->left;
473 case AGL_SHELL_EDGE_RIGHT:
474 member = &output->right;
477 wl_resource_post_error(shell_res,
478 AGL_SHELL_ERROR_INVALID_ARGUMENT,
479 "invalid edge for panel");
484 wl_resource_post_error(shell_res,
485 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
486 "output already has panel on this edge");
490 surface->role = IVI_SURFACE_ROLE_PANEL;
491 surface->panel.output = output;
492 surface->panel.edge = edge;
493 wl_list_remove(&surface->link);
494 wl_list_init(&surface->link);
498 switch (surface->panel.edge) {
499 case AGL_SHELL_EDGE_TOP:
500 case AGL_SHELL_EDGE_BOTTOM:
501 width = woutput->width;
503 case AGL_SHELL_EDGE_LEFT:
504 case AGL_SHELL_EDGE_RIGHT:
505 height = woutput->height;
509 weston_desktop_surface_set_size(dsurface, width, height);
513 shell_activate_app(struct wl_client *client,
514 struct wl_resource *shell_res,
516 struct wl_resource *output_res)
518 struct weston_head *head = weston_head_from_resource(output_res);
519 struct weston_output *woutput = weston_head_get_output(head);
520 struct ivi_output *output = to_ivi_output(woutput);
522 ivi_layout_activate(output, app_id);
525 static const struct agl_shell_interface agl_shell_implementation = {
526 .ready = shell_ready,
527 .set_background = shell_set_background,
528 .set_panel = shell_set_panel,
529 .activate_app = shell_activate_app,
533 shell_desktop_set_app_property(struct wl_client *client,
534 struct wl_resource *shell_res,
535 const char *app_id, uint32_t role,
536 int x, int y, struct wl_resource *output_res)
538 struct weston_head *head = weston_head_from_resource(output_res);
539 struct weston_output *woutput = weston_head_get_output(head);
540 struct ivi_output *output = to_ivi_output(woutput);
542 /* temporary store the app_id such that, when created to be check against */
543 if (role == AGL_SHELL_DESKTOP_APP_ROLE_POPUP)
544 ivi_set_pending_desktop_surface_popup(output, x, y, app_id);
547 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
548 .activate_app = shell_activate_app,
549 .set_app_property = shell_desktop_set_app_property
553 unbind_agl_shell(struct wl_resource *resource)
555 struct ivi_compositor *ivi;
556 struct ivi_output *output;
557 struct ivi_surface *surf, *surf_tmp;
559 ivi = wl_resource_get_user_data(resource);
560 wl_list_for_each(output, &ivi->outputs, link) {
561 free(output->background);
562 output->background = NULL;
567 free(output->bottom);
568 output->bottom = NULL;
574 output->right = NULL;
576 /* reset the active surf if there's one present */
577 if (output->active) {
578 output->active->view->is_mapped = false;
579 output->active->view->surface->is_mapped = false;
581 weston_layer_entry_remove(&output->active->view->layer_link);
582 output->active = NULL;
585 insert_black_surface(output);
588 wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
589 wl_list_remove(&surf->link);
590 wl_list_init(&surf->link);
593 wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
594 wl_list_remove(&surf->link);
595 wl_list_init(&surf->link);
598 wl_list_init(&ivi->surfaces);
599 wl_list_init(&ivi->pending_surfaces);
601 ivi->shell_client.ready = false;
602 ivi->shell_client.resource = NULL;
603 ivi->shell_client.client = NULL;
607 bind_agl_shell(struct wl_client *client,
608 void *data, uint32_t version, uint32_t id)
610 struct ivi_compositor *ivi = data;
611 struct wl_resource *resource;
613 resource = wl_resource_create(client, &agl_shell_interface,
616 wl_client_post_no_memory(client);
621 if (ivi->shell_client.client != client) {
622 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
623 "client not authorized to use agl_shell");
628 if (ivi->shell_client.resource) {
629 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
630 "agl_shell has already been bound");
634 wl_resource_set_implementation(resource, &agl_shell_implementation,
635 ivi, unbind_agl_shell);
636 ivi->shell_client.resource = resource;
640 unbind_agl_shell_desktop(struct wl_resource *resource)
642 struct desktop_client *dclient = wl_resource_get_user_data(resource);
644 wl_list_remove(&dclient->link);
649 bind_agl_shell_desktop(struct wl_client *client,
650 void *data, uint32_t version, uint32_t id)
652 struct ivi_compositor *ivi = data;
653 struct wl_resource *resource;
654 struct desktop_client *dclient = zalloc(sizeof(*dclient));
657 wl_client_post_no_memory(client);
661 resource = wl_resource_create(client, &agl_shell_desktop_interface,
665 wl_client_post_no_memory(client);
669 wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
670 dclient, unbind_agl_shell_desktop);
672 dclient->resource = resource;
673 wl_list_insert(&ivi->desktop_clients, &dclient->link);
675 /* advertise xdg surfaces */
676 ivi_shell_advertise_xdg_surfaces(ivi, resource);
680 ivi_shell_create_global(struct ivi_compositor *ivi)
682 ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
683 &agl_shell_interface, 1,
684 ivi, bind_agl_shell);
685 if (!ivi->agl_shell) {
686 weston_log("Failed to create wayland global.\n");
690 ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
691 &agl_shell_desktop_interface, 1,
692 ivi, bind_agl_shell_desktop);
693 if (!ivi->agl_shell_desktop) {
694 weston_log("Failed to create wayland global (agl_shell_desktop).\n");