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_shell_init_black_fs(struct ivi_compositor *ivi)
71 struct ivi_output *out;
73 wl_list_for_each(out, &ivi->outputs, link) {
74 create_black_surface_view(out);
75 insert_black_surface(out);
80 ivi_shell_init(struct ivi_compositor *ivi)
82 weston_layer_init(&ivi->hidden, ivi->compositor);
83 weston_layer_init(&ivi->background, ivi->compositor);
84 weston_layer_init(&ivi->normal, ivi->compositor);
85 weston_layer_init(&ivi->panel, ivi->compositor);
86 weston_layer_init(&ivi->fullscreen, ivi->compositor);
88 weston_layer_set_position(&ivi->hidden,
89 WESTON_LAYER_POSITION_HIDDEN);
90 weston_layer_set_position(&ivi->background,
91 WESTON_LAYER_POSITION_BACKGROUND);
92 weston_layer_set_position(&ivi->normal,
93 WESTON_LAYER_POSITION_NORMAL);
94 weston_layer_set_position(&ivi->panel,
95 WESTON_LAYER_POSITION_UI);
96 weston_layer_set_position(&ivi->fullscreen,
97 WESTON_LAYER_POSITION_FULLSCREEN);
103 ivi_shell_advertise_xdg_surfaces(struct ivi_compositor *ivi, struct wl_resource *resource)
105 struct ivi_surface *surface;
107 wl_list_for_each(surface, &ivi->surfaces, link) {
109 weston_desktop_surface_get_app_id(surface->dsurface);
110 agl_shell_desktop_send_application(resource, app_id);
115 client_exec(const char *command, int fd)
120 /* Don't give the child our signal mask */
122 sigprocmask(SIG_UNBLOCK, &sig, NULL);
124 /* Launch clients as the user; don't give them the wrong euid */
125 if (seteuid(getuid()) == -1) {
126 weston_log("seteuid failed: %s\n", strerror(errno));
130 /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
131 * clobbering fd, as we'll exit/exec either way.
135 weston_log("dup failed: %s\n", strerror(errno));
139 snprintf(s, sizeof s, "%d", fd);
140 setenv("WAYLAND_SOCKET", s, 1);
142 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
143 weston_log("executing '%s' failed: %s", command, strerror(errno));
146 static struct wl_client *
147 launch_shell_client(struct ivi_compositor *ivi, const char *command)
149 struct wl_client *client;
153 weston_log("launching' %s'\n", command);
155 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
156 weston_log("socketpair failed while launching '%s': %s\n",
157 command, strerror(errno));
165 weston_log("fork failed while launching '%s': %s\n",
166 command, strerror(errno));
171 client_exec(command, sock[1]);
176 client = wl_client_create(ivi->compositor->wl_display, sock[0]);
179 weston_log("Failed to create wayland client for '%s'",
188 ivi_launch_shell_client(struct ivi_compositor *ivi)
190 struct weston_config_section *section;
191 char *command = NULL;
193 section = weston_config_get_section(ivi->config, "shell-client",
196 weston_config_section_get_string(section, "command",
202 ivi->shell_client.client = launch_shell_client(ivi, command);
203 if (!ivi->shell_client.client)
210 destroy_black_view(struct wl_listener *listener, void *data)
212 struct fullscreen_view *fs =
213 wl_container_of(listener, fs, fs_destroy);
217 if (fs->fs->view && fs->fs->view->surface) {
218 weston_surface_destroy(fs->fs->view->surface);
223 wl_list_remove(&fs->fs_destroy.link);
229 create_black_surface_view(struct ivi_output *output)
231 struct weston_surface *surface = NULL;
232 struct weston_view *view;
233 struct ivi_compositor *ivi = output->ivi;
234 struct weston_compositor *wc= ivi->compositor;
235 struct weston_output *woutput = output->output;
237 surface = weston_surface_create(wc);
238 view = weston_view_create(surface);
240 assert(view || surface);
242 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
243 weston_surface_set_size(surface, woutput->width, woutput->height);
244 weston_view_set_position(view, woutput->x, woutput->y);
246 output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
247 output->fullscreen_view.fs->view = view;
249 output->fullscreen_view.fs_destroy.notify = destroy_black_view;
250 wl_signal_add(&woutput->destroy_signal,
251 &output->fullscreen_view.fs_destroy);
255 remove_black_surface(struct ivi_output *output)
257 struct weston_view *view = output->fullscreen_view.fs->view;
259 assert(view->is_mapped == true ||
260 view->surface->is_mapped == true);
262 view->is_mapped = false;
263 view->surface->is_mapped = false;
265 weston_layer_entry_remove(&view->layer_link);
266 weston_view_update_transform(view);
268 weston_output_damage(output->output);
272 insert_black_surface(struct ivi_output *output)
274 struct weston_view *view = output->fullscreen_view.fs->view;
276 if (view->is_mapped || view->surface->is_mapped)
279 weston_layer_entry_remove(&view->layer_link);
280 weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
283 view->is_mapped = true;
284 view->surface->is_mapped = true;
286 weston_view_update_transform(view);
287 weston_output_damage(output->output);
291 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
293 struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
294 struct ivi_output *output;
295 struct ivi_surface *surface, *tmp;
297 /* Init already finished. Do nothing */
298 if (ivi->shell_client.ready)
301 ivi->shell_client.ready = true;
303 wl_list_for_each(output, &ivi->outputs, link) {
304 remove_black_surface(output);
305 ivi_layout_init(ivi, output);
308 wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
309 wl_list_remove(&surface->link);
310 ivi_set_desktop_surface(surface);
315 shell_set_background(struct wl_client *client,
316 struct wl_resource *shell_res,
317 struct wl_resource *surface_res,
318 struct wl_resource *output_res)
320 struct weston_head *head = weston_head_from_resource(output_res);
321 struct weston_output *woutput = weston_head_get_output(head);
322 struct ivi_output *output = to_ivi_output(woutput);
323 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
324 struct weston_desktop_surface *dsurface;
325 struct ivi_surface *surface;
327 dsurface = weston_surface_get_desktop_surface(wsurface);
329 wl_resource_post_error(shell_res,
330 AGL_SHELL_ERROR_INVALID_ARGUMENT,
331 "surface must be a desktop surface");
335 surface = weston_desktop_surface_get_user_data(dsurface);
336 if (surface->role != IVI_SURFACE_ROLE_NONE) {
337 wl_resource_post_error(shell_res,
338 AGL_SHELL_ERROR_INVALID_ARGUMENT,
339 "surface already has another ivi role");
343 if (output->background) {
344 wl_resource_post_error(shell_res,
345 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
346 "output already has background");
350 surface->role = IVI_SURFACE_ROLE_BACKGROUND;
351 surface->bg.output = output;
352 wl_list_remove(&surface->link);
353 wl_list_init(&surface->link);
355 output->background = surface;
357 weston_desktop_surface_set_maximized(dsurface, true);
358 weston_desktop_surface_set_size(dsurface,
359 output->output->width,
360 output->output->height);
364 shell_set_panel(struct wl_client *client,
365 struct wl_resource *shell_res,
366 struct wl_resource *surface_res,
367 struct wl_resource *output_res,
370 struct weston_head *head = weston_head_from_resource(output_res);
371 struct weston_output *woutput = weston_head_get_output(head);
372 struct ivi_output *output = to_ivi_output(woutput);
373 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
374 struct weston_desktop_surface *dsurface;
375 struct ivi_surface *surface;
376 struct ivi_surface **member;
377 int32_t width = 0, height = 0;
379 dsurface = weston_surface_get_desktop_surface(wsurface);
381 wl_resource_post_error(shell_res,
382 AGL_SHELL_ERROR_INVALID_ARGUMENT,
383 "surface must be a desktop surface");
387 surface = weston_desktop_surface_get_user_data(dsurface);
388 if (surface->role != IVI_SURFACE_ROLE_NONE) {
389 wl_resource_post_error(shell_res,
390 AGL_SHELL_ERROR_INVALID_ARGUMENT,
391 "surface already has another ivi role");
396 case AGL_SHELL_EDGE_TOP:
397 member = &output->top;
399 case AGL_SHELL_EDGE_BOTTOM:
400 member = &output->bottom;
402 case AGL_SHELL_EDGE_LEFT:
403 member = &output->left;
405 case AGL_SHELL_EDGE_RIGHT:
406 member = &output->right;
409 wl_resource_post_error(shell_res,
410 AGL_SHELL_ERROR_INVALID_ARGUMENT,
411 "invalid edge for panel");
416 wl_resource_post_error(shell_res,
417 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
418 "output already has panel on this edge");
422 surface->role = IVI_SURFACE_ROLE_PANEL;
423 surface->panel.output = output;
424 surface->panel.edge = edge;
425 wl_list_remove(&surface->link);
426 wl_list_init(&surface->link);
430 switch (surface->panel.edge) {
431 case AGL_SHELL_EDGE_TOP:
432 case AGL_SHELL_EDGE_BOTTOM:
433 width = woutput->width;
435 case AGL_SHELL_EDGE_LEFT:
436 case AGL_SHELL_EDGE_RIGHT:
437 height = woutput->height;
441 weston_desktop_surface_set_size(dsurface, width, height);
445 shell_activate_app(struct wl_client *client,
446 struct wl_resource *shell_res,
448 struct wl_resource *output_res)
450 struct weston_head *head = weston_head_from_resource(output_res);
451 struct weston_output *woutput = weston_head_get_output(head);
452 struct ivi_output *output = to_ivi_output(woutput);
454 ivi_layout_activate(output, app_id);
457 static const struct agl_shell_interface agl_shell_implementation = {
458 .ready = shell_ready,
459 .set_background = shell_set_background,
460 .set_panel = shell_set_panel,
461 .activate_app = shell_activate_app,
464 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
465 .activate_app = shell_activate_app,
469 unbind_agl_shell(struct wl_resource *resource)
471 struct ivi_compositor *ivi;
472 struct ivi_output *output;
473 struct ivi_surface *surf, *surf_tmp;
475 ivi = wl_resource_get_user_data(resource);
476 wl_list_for_each(output, &ivi->outputs, link) {
477 free(output->background);
478 output->background = NULL;
483 free(output->bottom);
484 output->bottom = NULL;
490 output->right = NULL;
492 /* reset the active surf if there's one present */
493 if (output->active) {
494 output->active->view->is_mapped = false;
495 output->active->view->surface->is_mapped = false;
497 weston_layer_entry_remove(&output->active->view->layer_link);
498 output->active = NULL;
501 insert_black_surface(output);
504 wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
505 wl_list_remove(&surf->link);
506 wl_list_init(&surf->link);
509 wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
510 wl_list_remove(&surf->link);
511 wl_list_init(&surf->link);
514 wl_list_init(&ivi->surfaces);
515 wl_list_init(&ivi->pending_surfaces);
517 ivi->shell_client.ready = false;
518 ivi->shell_client.resource = NULL;
519 ivi->shell_client.client = NULL;
523 bind_agl_shell(struct wl_client *client,
524 void *data, uint32_t version, uint32_t id)
526 struct ivi_compositor *ivi = data;
527 struct wl_resource *resource;
529 resource = wl_resource_create(client, &agl_shell_interface,
532 wl_client_post_no_memory(client);
537 if (ivi->shell_client.client != client) {
538 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
539 "client not authorized to use agl_shell");
544 if (ivi->shell_client.resource) {
545 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
546 "agl_shell has already been bound");
550 wl_resource_set_implementation(resource, &agl_shell_implementation,
551 ivi, unbind_agl_shell);
552 ivi->shell_client.resource = resource;
556 unbind_agl_shell_desktop(struct wl_resource *resource)
558 struct desktop_client *dclient = wl_resource_get_user_data(resource);
560 wl_list_remove(&dclient->link);
565 bind_agl_shell_desktop(struct wl_client *client,
566 void *data, uint32_t version, uint32_t id)
568 struct ivi_compositor *ivi = data;
569 struct wl_resource *resource;
570 struct desktop_client *dclient = zalloc(sizeof(*dclient));
573 wl_client_post_no_memory(client);
577 resource = wl_resource_create(client, &agl_shell_desktop_interface,
580 wl_client_post_no_memory(client);
584 wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
585 dclient, unbind_agl_shell_desktop);
587 dclient->resource = resource;
588 wl_list_insert(&ivi->desktop_clients, &dclient->link);
590 /* advertise xdg surfaces */
591 ivi_shell_advertise_xdg_surfaces(ivi, resource);
595 ivi_shell_create_global(struct ivi_compositor *ivi)
597 ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
598 &agl_shell_interface, 1,
599 ivi, bind_agl_shell);
600 if (!ivi->agl_shell) {
601 weston_log("Failed to create wayland global.\n");
605 ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
606 &agl_shell_desktop_interface, 1,
607 ivi, bind_agl_shell_desktop);
608 if (!ivi->agl_shell_desktop) {
609 weston_log("Failed to create wayland global (agl_shell_desktop).\n");