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);
311 if (ivi_check_pending_desktop_surface_popup(surface)) {
312 ivi_set_desktop_surface_popup(surface);
314 ivi_set_desktop_surface(surface);
315 ivi_layout_desktop_committed(surface);
321 shell_set_background(struct wl_client *client,
322 struct wl_resource *shell_res,
323 struct wl_resource *surface_res,
324 struct wl_resource *output_res)
326 struct weston_head *head = weston_head_from_resource(output_res);
327 struct weston_output *woutput = weston_head_get_output(head);
328 struct ivi_output *output = to_ivi_output(woutput);
329 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
330 struct weston_desktop_surface *dsurface;
331 struct ivi_surface *surface;
333 dsurface = weston_surface_get_desktop_surface(wsurface);
335 wl_resource_post_error(shell_res,
336 AGL_SHELL_ERROR_INVALID_ARGUMENT,
337 "surface must be a desktop surface");
341 surface = weston_desktop_surface_get_user_data(dsurface);
342 if (surface->role != IVI_SURFACE_ROLE_NONE) {
343 wl_resource_post_error(shell_res,
344 AGL_SHELL_ERROR_INVALID_ARGUMENT,
345 "surface already has another ivi role");
349 if (output->background) {
350 wl_resource_post_error(shell_res,
351 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
352 "output already has background");
356 surface->role = IVI_SURFACE_ROLE_BACKGROUND;
357 surface->bg.output = output;
358 wl_list_remove(&surface->link);
359 wl_list_init(&surface->link);
361 output->background = surface;
363 weston_desktop_surface_set_maximized(dsurface, true);
364 weston_desktop_surface_set_size(dsurface,
365 output->output->width,
366 output->output->height);
370 shell_set_panel(struct wl_client *client,
371 struct wl_resource *shell_res,
372 struct wl_resource *surface_res,
373 struct wl_resource *output_res,
376 struct weston_head *head = weston_head_from_resource(output_res);
377 struct weston_output *woutput = weston_head_get_output(head);
378 struct ivi_output *output = to_ivi_output(woutput);
379 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
380 struct weston_desktop_surface *dsurface;
381 struct ivi_surface *surface;
382 struct ivi_surface **member;
383 int32_t width = 0, height = 0;
385 dsurface = weston_surface_get_desktop_surface(wsurface);
387 wl_resource_post_error(shell_res,
388 AGL_SHELL_ERROR_INVALID_ARGUMENT,
389 "surface must be a desktop surface");
393 surface = weston_desktop_surface_get_user_data(dsurface);
394 if (surface->role != IVI_SURFACE_ROLE_NONE) {
395 wl_resource_post_error(shell_res,
396 AGL_SHELL_ERROR_INVALID_ARGUMENT,
397 "surface already has another ivi role");
402 case AGL_SHELL_EDGE_TOP:
403 member = &output->top;
405 case AGL_SHELL_EDGE_BOTTOM:
406 member = &output->bottom;
408 case AGL_SHELL_EDGE_LEFT:
409 member = &output->left;
411 case AGL_SHELL_EDGE_RIGHT:
412 member = &output->right;
415 wl_resource_post_error(shell_res,
416 AGL_SHELL_ERROR_INVALID_ARGUMENT,
417 "invalid edge for panel");
422 wl_resource_post_error(shell_res,
423 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
424 "output already has panel on this edge");
428 surface->role = IVI_SURFACE_ROLE_PANEL;
429 surface->panel.output = output;
430 surface->panel.edge = edge;
431 wl_list_remove(&surface->link);
432 wl_list_init(&surface->link);
436 switch (surface->panel.edge) {
437 case AGL_SHELL_EDGE_TOP:
438 case AGL_SHELL_EDGE_BOTTOM:
439 width = woutput->width;
441 case AGL_SHELL_EDGE_LEFT:
442 case AGL_SHELL_EDGE_RIGHT:
443 height = woutput->height;
447 weston_desktop_surface_set_size(dsurface, width, height);
451 shell_activate_app(struct wl_client *client,
452 struct wl_resource *shell_res,
454 struct wl_resource *output_res)
456 struct weston_head *head = weston_head_from_resource(output_res);
457 struct weston_output *woutput = weston_head_get_output(head);
458 struct ivi_output *output = to_ivi_output(woutput);
460 ivi_layout_activate(output, app_id);
463 static const struct agl_shell_interface agl_shell_implementation = {
464 .ready = shell_ready,
465 .set_background = shell_set_background,
466 .set_panel = shell_set_panel,
467 .activate_app = shell_activate_app,
470 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
471 .activate_app = shell_activate_app,
475 unbind_agl_shell(struct wl_resource *resource)
477 struct ivi_compositor *ivi;
478 struct ivi_output *output;
479 struct ivi_surface *surf, *surf_tmp;
481 ivi = wl_resource_get_user_data(resource);
482 wl_list_for_each(output, &ivi->outputs, link) {
483 free(output->background);
484 output->background = NULL;
489 free(output->bottom);
490 output->bottom = NULL;
496 output->right = NULL;
498 /* reset the active surf if there's one present */
499 if (output->active) {
500 output->active->view->is_mapped = false;
501 output->active->view->surface->is_mapped = false;
503 weston_layer_entry_remove(&output->active->view->layer_link);
504 output->active = NULL;
507 insert_black_surface(output);
510 wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
511 wl_list_remove(&surf->link);
512 wl_list_init(&surf->link);
515 wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
516 wl_list_remove(&surf->link);
517 wl_list_init(&surf->link);
520 wl_list_init(&ivi->surfaces);
521 wl_list_init(&ivi->pending_surfaces);
523 ivi->shell_client.ready = false;
524 ivi->shell_client.resource = NULL;
525 ivi->shell_client.client = NULL;
529 bind_agl_shell(struct wl_client *client,
530 void *data, uint32_t version, uint32_t id)
532 struct ivi_compositor *ivi = data;
533 struct wl_resource *resource;
535 resource = wl_resource_create(client, &agl_shell_interface,
538 wl_client_post_no_memory(client);
543 if (ivi->shell_client.client != client) {
544 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
545 "client not authorized to use agl_shell");
550 if (ivi->shell_client.resource) {
551 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
552 "agl_shell has already been bound");
556 wl_resource_set_implementation(resource, &agl_shell_implementation,
557 ivi, unbind_agl_shell);
558 ivi->shell_client.resource = resource;
562 unbind_agl_shell_desktop(struct wl_resource *resource)
564 struct desktop_client *dclient = wl_resource_get_user_data(resource);
566 wl_list_remove(&dclient->link);
571 bind_agl_shell_desktop(struct wl_client *client,
572 void *data, uint32_t version, uint32_t id)
574 struct ivi_compositor *ivi = data;
575 struct wl_resource *resource;
576 struct desktop_client *dclient = zalloc(sizeof(*dclient));
579 wl_client_post_no_memory(client);
583 resource = wl_resource_create(client, &agl_shell_desktop_interface,
586 wl_client_post_no_memory(client);
590 wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
591 dclient, unbind_agl_shell_desktop);
593 dclient->resource = resource;
594 wl_list_insert(&ivi->desktop_clients, &dclient->link);
596 /* advertise xdg surfaces */
597 ivi_shell_advertise_xdg_surfaces(ivi, resource);
601 ivi_shell_create_global(struct ivi_compositor *ivi)
603 ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
604 &agl_shell_interface, 1,
605 ivi, bind_agl_shell);
606 if (!ivi->agl_shell) {
607 weston_log("Failed to create wayland global.\n");
611 ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
612 &agl_shell_desktop_interface, 1,
613 ivi, bind_agl_shell_desktop);
614 if (!ivi->agl_shell_desktop) {
615 weston_log("Failed to create wayland global (agl_shell_desktop).\n");