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"
44 create_black_surface_view(struct ivi_output *output);
47 insert_black_surface(struct ivi_output *output);
50 ivi_set_desktop_surface(struct ivi_surface *surface)
52 assert(surface->role == IVI_SURFACE_ROLE_NONE);
54 surface->role = IVI_SURFACE_ROLE_DESKTOP;
55 wl_list_insert(&surface->ivi->surfaces, &surface->link);
59 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
61 struct ivi_output *out;
63 wl_list_for_each(out, &ivi->outputs, link) {
64 create_black_surface_view(out);
65 insert_black_surface(out);
70 ivi_shell_init(struct ivi_compositor *ivi)
72 weston_layer_init(&ivi->hidden, ivi->compositor);
73 weston_layer_init(&ivi->background, ivi->compositor);
74 weston_layer_init(&ivi->normal, ivi->compositor);
75 weston_layer_init(&ivi->panel, ivi->compositor);
76 weston_layer_init(&ivi->fullscreen, ivi->compositor);
78 weston_layer_set_position(&ivi->hidden,
79 WESTON_LAYER_POSITION_HIDDEN);
80 weston_layer_set_position(&ivi->background,
81 WESTON_LAYER_POSITION_BACKGROUND);
82 weston_layer_set_position(&ivi->normal,
83 WESTON_LAYER_POSITION_NORMAL);
84 weston_layer_set_position(&ivi->panel,
85 WESTON_LAYER_POSITION_UI);
86 weston_layer_set_position(&ivi->fullscreen,
87 WESTON_LAYER_POSITION_FULLSCREEN);
93 client_exec(const char *command, int fd)
98 /* Don't give the child our signal mask */
100 sigprocmask(SIG_UNBLOCK, &sig, NULL);
102 /* Launch clients as the user; don't give them the wrong euid */
103 if (seteuid(getuid()) == -1) {
104 weston_log("seteuid failed: %s\n", strerror(errno));
108 /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
109 * clobbering fd, as we'll exit/exec either way.
113 weston_log("dup failed: %s\n", strerror(errno));
117 snprintf(s, sizeof s, "%d", fd);
118 setenv("WAYLAND_SOCKET", s, 1);
120 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
121 weston_log("executing '%s' failed: %s", command, strerror(errno));
124 static struct wl_client *
125 launch_shell_client(struct ivi_compositor *ivi, const char *command)
127 struct wl_client *client;
131 weston_log("launching' %s'\n", command);
133 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
134 weston_log("socketpair failed while launching '%s': %s\n",
135 command, strerror(errno));
143 weston_log("fork failed while launching '%s': %s\n",
144 command, strerror(errno));
149 client_exec(command, sock[1]);
154 client = wl_client_create(ivi->compositor->wl_display, sock[0]);
157 weston_log("Failed to create wayland client for '%s'",
166 ivi_launch_shell_client(struct ivi_compositor *ivi)
168 struct weston_config_section *section;
169 char *command = NULL;
171 section = weston_config_get_section(ivi->config, "shell-client",
174 weston_config_section_get_string(section, "command",
180 ivi->shell_client.client = launch_shell_client(ivi, command);
181 if (!ivi->shell_client.client)
188 destroy_black_view(struct wl_listener *listener, void *data)
190 struct fullscreen_view *fs =
191 wl_container_of(listener, fs, fs_destroy);
195 if (fs->fs->view && fs->fs->view->surface) {
196 weston_surface_destroy(fs->fs->view->surface);
201 wl_list_remove(&fs->fs_destroy.link);
207 create_black_surface_view(struct ivi_output *output)
209 struct weston_surface *surface = NULL;
210 struct weston_view *view;
211 struct ivi_compositor *ivi = output->ivi;
212 struct weston_compositor *wc= ivi->compositor;
213 struct weston_output *woutput = output->output;
215 surface = weston_surface_create(wc);
216 view = weston_view_create(surface);
218 assert(view || surface);
220 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
221 weston_surface_set_size(surface, woutput->width, woutput->height);
222 weston_view_set_position(view, woutput->x, woutput->y);
224 output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
225 output->fullscreen_view.fs->view = view;
227 output->fullscreen_view.fs_destroy.notify = destroy_black_view;
228 wl_signal_add(&woutput->destroy_signal,
229 &output->fullscreen_view.fs_destroy);
233 remove_black_surface(struct ivi_output *output)
235 struct weston_view *view = output->fullscreen_view.fs->view;
237 assert(view->is_mapped == true ||
238 view->surface->is_mapped == true);
240 view->is_mapped = false;
241 view->surface->is_mapped = false;
243 weston_layer_entry_remove(&view->layer_link);
244 weston_view_update_transform(view);
246 weston_output_damage(output->output);
250 insert_black_surface(struct ivi_output *output)
252 struct weston_view *view = output->fullscreen_view.fs->view;
254 if (view->is_mapped || view->surface->is_mapped)
257 weston_layer_entry_remove(&view->layer_link);
258 weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
261 view->is_mapped = true;
262 view->surface->is_mapped = true;
264 weston_view_update_transform(view);
265 weston_output_damage(output->output);
269 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
271 struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
272 struct ivi_output *output;
273 struct ivi_surface *surface, *tmp;
275 /* Init already finished. Do nothing */
276 if (ivi->shell_client.ready)
279 ivi->shell_client.ready = true;
281 wl_list_for_each(output, &ivi->outputs, link) {
282 remove_black_surface(output);
283 ivi_layout_init(ivi, output);
286 wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
287 wl_list_remove(&surface->link);
288 ivi_set_desktop_surface(surface);
293 shell_set_background(struct wl_client *client,
294 struct wl_resource *shell_res,
295 struct wl_resource *surface_res,
296 struct wl_resource *output_res)
298 struct weston_head *head = weston_head_from_resource(output_res);
299 struct weston_output *woutput = weston_head_get_output(head);
300 struct ivi_output *output = to_ivi_output(woutput);
301 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
302 struct weston_desktop_surface *dsurface;
303 struct ivi_surface *surface;
305 dsurface = weston_surface_get_desktop_surface(wsurface);
307 wl_resource_post_error(shell_res,
308 AGL_SHELL_ERROR_INVALID_ARGUMENT,
309 "surface must be a desktop surface");
313 surface = weston_desktop_surface_get_user_data(dsurface);
314 if (surface->role != IVI_SURFACE_ROLE_NONE) {
315 wl_resource_post_error(shell_res,
316 AGL_SHELL_ERROR_INVALID_ARGUMENT,
317 "surface already has another ivi role");
321 if (output->background) {
322 wl_resource_post_error(shell_res,
323 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
324 "output already has background");
328 surface->role = IVI_SURFACE_ROLE_BACKGROUND;
329 surface->bg.output = output;
330 wl_list_remove(&surface->link);
331 wl_list_init(&surface->link);
333 output->background = surface;
335 weston_desktop_surface_set_maximized(dsurface, true);
336 weston_desktop_surface_set_size(dsurface,
337 output->output->width,
338 output->output->height);
342 shell_set_panel(struct wl_client *client,
343 struct wl_resource *shell_res,
344 struct wl_resource *surface_res,
345 struct wl_resource *output_res,
348 struct weston_head *head = weston_head_from_resource(output_res);
349 struct weston_output *woutput = weston_head_get_output(head);
350 struct ivi_output *output = to_ivi_output(woutput);
351 struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
352 struct weston_desktop_surface *dsurface;
353 struct ivi_surface *surface;
354 struct ivi_surface **member;
355 int32_t width = 0, height = 0;
357 dsurface = weston_surface_get_desktop_surface(wsurface);
359 wl_resource_post_error(shell_res,
360 AGL_SHELL_ERROR_INVALID_ARGUMENT,
361 "surface must be a desktop surface");
365 surface = weston_desktop_surface_get_user_data(dsurface);
366 if (surface->role != IVI_SURFACE_ROLE_NONE) {
367 wl_resource_post_error(shell_res,
368 AGL_SHELL_ERROR_INVALID_ARGUMENT,
369 "surface already has another ivi role");
374 case AGL_SHELL_EDGE_TOP:
375 member = &output->top;
377 case AGL_SHELL_EDGE_BOTTOM:
378 member = &output->bottom;
380 case AGL_SHELL_EDGE_LEFT:
381 member = &output->left;
383 case AGL_SHELL_EDGE_RIGHT:
384 member = &output->right;
387 wl_resource_post_error(shell_res,
388 AGL_SHELL_ERROR_INVALID_ARGUMENT,
389 "invalid edge for panel");
394 wl_resource_post_error(shell_res,
395 AGL_SHELL_ERROR_BACKGROUND_EXISTS,
396 "output already has panel on this edge");
400 surface->role = IVI_SURFACE_ROLE_PANEL;
401 surface->panel.output = output;
402 surface->panel.edge = edge;
403 wl_list_remove(&surface->link);
404 wl_list_init(&surface->link);
408 switch (surface->panel.edge) {
409 case AGL_SHELL_EDGE_TOP:
410 case AGL_SHELL_EDGE_BOTTOM:
411 width = woutput->width;
413 case AGL_SHELL_EDGE_LEFT:
414 case AGL_SHELL_EDGE_RIGHT:
415 height = woutput->height;
419 weston_desktop_surface_set_size(dsurface, width, height);
423 shell_activate_app(struct wl_client *client,
424 struct wl_resource *shell_res,
426 struct wl_resource *output_res)
428 struct weston_head *head = weston_head_from_resource(output_res);
429 struct weston_output *woutput = weston_head_get_output(head);
430 struct ivi_output *output = to_ivi_output(woutput);
432 ivi_layout_activate(output, app_id);
435 static const struct agl_shell_interface agl_shell_implementation = {
436 .ready = shell_ready,
437 .set_background = shell_set_background,
438 .set_panel = shell_set_panel,
439 .activate_app = shell_activate_app,
443 unbind_agl_shell(struct wl_resource *resource)
445 struct ivi_compositor *ivi;
446 struct ivi_output *output;
447 struct ivi_surface *surf, *surf_tmp;
449 ivi = wl_resource_get_user_data(resource);
450 wl_list_for_each(output, &ivi->outputs, link) {
451 free(output->background);
452 output->background = NULL;
457 free(output->bottom);
458 output->bottom = NULL;
464 output->right = NULL;
466 /* reset the active surf if there's one present */
467 if (output->active) {
468 output->active->view->is_mapped = false;
469 output->active->view->surface->is_mapped = false;
471 weston_layer_entry_remove(&output->active->view->layer_link);
472 output->active = NULL;
475 insert_black_surface(output);
478 wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
479 wl_list_remove(&surf->link);
480 wl_list_init(&surf->link);
483 wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
484 wl_list_remove(&surf->link);
485 wl_list_init(&surf->link);
488 wl_list_init(&ivi->surfaces);
489 wl_list_init(&ivi->pending_surfaces);
491 ivi->shell_client.ready = false;
492 ivi->shell_client.resource = NULL;
493 ivi->shell_client.client = NULL;
497 bind_agl_shell(struct wl_client *client,
498 void *data, uint32_t version, uint32_t id)
500 struct ivi_compositor *ivi = data;
501 struct wl_resource *resource;
503 resource = wl_resource_create(client, &agl_shell_interface,
506 wl_client_post_no_memory(client);
511 if (ivi->shell_client.client != client) {
512 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
513 "client not authorized to use agl_shell");
518 if (ivi->shell_client.resource) {
519 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
520 "agl_shell has already been bound");
524 wl_resource_set_implementation(resource, &agl_shell_implementation,
525 ivi, unbind_agl_shell);
526 ivi->shell_client.resource = resource;
530 ivi_shell_create_global(struct ivi_compositor *ivi)
532 ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
533 &agl_shell_interface, 1,
534 ivi, bind_agl_shell);
535 if (!ivi->agl_shell) {
536 weston_log("Failed to create wayland global.\n");