ClaylandSeat * clayland_seat_new (struct wl_display *display) { ClaylandSeat *seat = g_new0 (ClaylandSeat, 1); wl_signal_init (&seat->destroy_signal); seat->selection_data_source = NULL; wl_list_init (&seat->base_resource_list); wl_signal_init (&seat->selection_signal); wl_list_init (&seat->drag_resource_list); wl_signal_init (&seat->drag_icon_signal); clayland_pointer_init (&seat->pointer); clayland_keyboard_init (&seat->keyboard, display); seat->display = display; seat->current_stage = 0; seat->sprite = NULL; seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy; seat->hotspot_x = 16; seat->hotspot_y = 16; wl_display_add_global (display, &wl_seat_interface, seat, bind_seat); return seat; }
static void cogland_compositor_create_surface (struct wl_client *wayland_client, struct wl_resource *wayland_compositor_resource, uint32_t id) { CoglandCompositor *compositor = wl_resource_get_user_data (wayland_compositor_resource); CoglandSurface *surface = g_slice_new0 (CoglandSurface); surface->compositor = compositor; wl_signal_init (&surface->destroy_signal); surface->resource = wl_client_add_object (wayland_client, &wl_surface_interface, &cogland_surface_interface, id, surface); wl_resource_set_destructor (surface->resource, cogland_surface_resource_destroy_cb); surface->pending.buffer_destroy_listener.notify = surface_handle_pending_buffer_destroy; wl_list_init (&surface->pending.frame_callback_list); region_init (&surface->pending.damage); compositor->surfaces = g_list_prepend (compositor->surfaces, surface); }
static CoglandBuffer * cogland_buffer_from_resource (struct wl_resource *resource) { CoglandBuffer *buffer; struct wl_listener *listener; listener = wl_resource_get_destroy_listener (resource, cogland_buffer_destroy_handler); if (listener) { buffer = wl_container_of (listener, buffer, destroy_listener); } else { buffer = g_slice_new0 (CoglandBuffer); buffer->resource = resource; wl_signal_init (&buffer->destroy_signal); buffer->destroy_listener.notify = cogland_buffer_destroy_handler; wl_resource_add_destroy_listener (resource, &buffer->destroy_listener); } return buffer; }
static struct wl_resource * wl_data_source_send_offer(struct wl_data_source *source, struct wl_resource *target) { struct wl_data_offer *offer; char **p; offer = malloc(sizeof *offer); if (offer == NULL) return NULL; offer->resource.destroy = destroy_data_offer; offer->resource.object.id = 0; offer->resource.object.interface = &wl_data_offer_interface; offer->resource.object.implementation = (void (**)(void)) &data_offer_interface; offer->resource.data = offer; wl_signal_init(&offer->resource.destroy_signal); offer->source = source; offer->source_destroy_listener.notify = destroy_offer_data_source; wl_signal_add(&source->resource.destroy_signal, &offer->source_destroy_listener); wl_client_add_resource(target->client, &offer->resource); wl_data_device_send_data_offer(target, &offer->resource); wl_array_for_each(p, &source->mime_types) wl_data_offer_send_offer(&offer->resource, *p); return &offer->resource; }
static void create_data_source(struct wl_client *client, struct wl_resource *resource, uint32_t id) { struct wl_data_source *source; source = malloc(sizeof *source); if (source == NULL) { wl_resource_post_no_memory(resource); return; } source->resource.destroy = destroy_data_source; source->resource.object.id = id; source->resource.object.interface = &wl_data_source_interface; source->resource.object.implementation = (void (**)(void)) &data_source_interface; source->resource.data = source; wl_signal_init(&source->resource.destroy_signal); source->accept = client_source_accept; source->send = client_source_send; source->cancel = client_source_cancel; wl_array_init(&source->mime_types); wl_client_add_resource(client, &source->resource); }
WL_EXPORT int pixman_renderer_init(struct weston_compositor *ec) { struct pixman_renderer *renderer; renderer = calloc(1, sizeof *renderer); if (renderer == NULL) return -1; renderer->repaint_debug = 0; renderer->debug_color = NULL; renderer->base.read_pixels = pixman_renderer_read_pixels; renderer->base.repaint_output = pixman_renderer_repaint_output; renderer->base.flush_damage = pixman_renderer_flush_damage; renderer->base.attach = pixman_renderer_attach; renderer->base.surface_set_color = pixman_renderer_surface_set_color; renderer->base.destroy = pixman_renderer_destroy; ec->renderer = &renderer->base; ec->capabilities |= WESTON_CAP_ROTATION_ANY; ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP; renderer->debug_binding = weston_compositor_add_debug_binding(ec, KEY_R, debug_binding, ec); wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565); wl_signal_init(&renderer->destroy_signal); return 0; }
static struct clipboard_source * clipboard_source_create(struct clipboard *clipboard, const char *mime_type, uint32_t serial, int fd) { struct wl_display *display = clipboard->seat->compositor->wl_display; struct wl_event_loop *loop = wl_display_get_event_loop(display); struct clipboard_source *source; char **s; source = malloc(sizeof *source); wl_array_init(&source->contents); wl_array_init(&source->base.mime_types); source->base.accept = clipboard_source_accept; source->base.send = clipboard_source_send; source->base.cancel = clipboard_source_cancel; source->base.resource.data = &source->base; wl_signal_init(&source->base.resource.destroy_signal); source->refcount = 1; source->clipboard = clipboard; source->serial = serial; s = wl_array_add(&source->base.mime_types, sizeof *s); *s = strdup(mime_type); source->event_source = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, clipboard_source_data, source); return source; }
static void weston_wm_get_selection_targets(struct weston_wm *wm) { struct x11_data_source *source; struct weston_compositor *compositor; struct weston_seat *seat = weston_wm_pick_seat(wm); xcb_get_property_cookie_t cookie; xcb_get_property_reply_t *reply; xcb_atom_t *value; char **p; uint32_t i; cookie = xcb_get_property(wm->conn, 1, /* delete */ wm->selection_window, wm->atom.wl_selection, XCB_GET_PROPERTY_TYPE_ANY, 0, /* offset */ 4096 /* length */); reply = xcb_get_property_reply(wm->conn, cookie, NULL); if (reply == NULL) return; dump_property(wm, wm->atom.wl_selection, reply); if (reply->type != XCB_ATOM_ATOM) { free(reply); return; } source = zalloc(sizeof *source); if (source == NULL) { free(reply); return; } wl_signal_init(&source->base.destroy_signal); source->base.accept = data_source_accept; source->base.send = data_source_send; source->base.cancel = data_source_cancel; source->wm = wm; wl_array_init(&source->base.mime_types); value = xcb_get_property_value(reply); for (i = 0; i < reply->value_len; i++) { if (value[i] == wm->atom.utf8_string) { p = wl_array_add(&source->base.mime_types, sizeof *p); if (p) *p = strdup("text/plain;charset=utf-8"); } } compositor = wm->server->compositor; weston_seat_set_selection(seat, &source->base, wl_display_next_serial(compositor->wl_display)); free(reply); }
bool data_device_initialize(struct data_device * data_device) { data_device->selection_destroy_listener.notify = &handle_selection_destroy; wl_signal_init(&data_device->event_signal); wl_list_init(&data_device->resources); return true; }
WL_EXPORT struct wl_event_loop * wl_event_loop_create(void) { struct wl_event_loop *loop; loop = malloc(sizeof *loop); if (loop == NULL) return NULL; loop->epoll_fd = wl_os_epoll_create_cloexec(); if (loop->epoll_fd < 0) { free(loop); return NULL; } wl_list_init(&loop->check_list); wl_list_init(&loop->idle_list); wl_list_init(&loop->destroy_list); wl_signal_init(&loop->destroy_signal); return loop; }
static void create_data_source(struct wl_client *client, struct wl_resource *resource, uint32_t id) { struct weston_data_source *source; source = malloc(sizeof *source); if (source == NULL) { wl_resource_post_no_memory(resource); return; } wl_signal_init(&source->destroy_signal); source->accept = client_source_accept; source->send = client_source_send; source->cancel = client_source_cancel; wl_array_init(&source->mime_types); source->resource = wl_resource_create(client, &wl_data_source_interface, 1, id); wl_resource_set_implementation(source->resource, &data_source_interface, source, destroy_data_source); }
static void handle_enter(struct weston_wm *wm, xcb_client_message_event_t *client_message) { struct dnd_data_source *source; struct weston_seat *seat = weston_wm_pick_seat(wm); char **p; const char *name; uint32_t *types; int i, length, has_text; xcb_get_property_cookie_t cookie; xcb_get_property_reply_t *reply; source = malloc(sizeof *source); if (source == NULL) return; wl_signal_init(&source->base.destroy_signal); source->base.accept = data_source_accept; source->base.send = data_source_send; source->base.cancel = data_source_cancel; source->wm = wm; source->window = client_message->data.data32[0]; source->version = client_message->data.data32[1] >> 24; if (client_message->data.data32[1] & 1) { cookie = xcb_get_property(wm->conn, 0, /* delete */ source->window, wm->atom.xdnd_type_list, XCB_ATOM_ANY, 0, 2048); reply = xcb_get_property_reply(wm->conn, cookie, NULL); types = xcb_get_property_value(reply); length = reply->value_len; } else { reply = NULL; types = &client_message->data.data32[2]; length = 3; } wl_array_init(&source->base.mime_types); has_text = 0; for (i = 0; i < length; i++) { if (types[i] == XCB_ATOM_NONE) continue; name = get_atom_name(wm->conn, types[i]); if (types[i] == wm->atom.utf8_string || types[i] == wm->atom.text_plain_utf8 || types[i] == wm->atom.text_plain) { if (has_text) continue; has_text = 1; p = wl_array_add(&source->base.mime_types, sizeof *p); if (p) *p = strdup("text/plain;charset=utf-8"); } else if (strchr(name, '/')) { p = wl_array_add(&source->base.mime_types, sizeof *p); if (p) *p = strdup(name); } } free(reply); weston_seat_start_drag(seat, &source->base, NULL, NULL); }
WLC_API bool wlc_init(const struct wlc_interface *interface, int argc, char *argv[]) { assert(interface); if (!interface) die("no wlc_interface was given"); if (wlc.display) return true; memset(&wlc, 0, sizeof(wlc)); wl_log_set_handler_server(wl_cb_log); for (int i = 1; i < argc; ++i) { if (chck_cstreq(argv[i], "--log")) { if (i + 1 >= argc) die("--log takes an argument (filename)"); wlc_set_log_file(fopen(argv[++i], "a")); } } unsetenv("TERM"); const char *x11display = getenv("DISPLAY"); bool privilidged = false; const bool has_logind = wlc_logind_available(); if (getuid() != geteuid() || getgid() != getegid()) { wlc_log(WLC_LOG_INFO, "Doing work on SUID/SGID side and dropping permissions"); privilidged = true; } else if (getuid() == 0) { die("Do not run wlc compositor as root"); } else if (!x11display && !has_logind && access("/dev/input/event0", R_OK | W_OK) != 0) { die("Not running from X11 and no access to /dev/input/event0 or logind available"); } #ifndef NDEBUG { struct sigaction action = { .sa_handler = backtrace }; sigaction(SIGABRT, &action, NULL); sigaction(SIGSEGV, &action, NULL); // XXX: Some weird sigfpes seems to come when running // wlc compositor inside wlc compositor (X11 backend). // Seems to be caused by resolution changes and mouse clicks. // Gather more information about this later and see what's going on. if (!getenv("WAYLAND_DISPLAY")) fpesetup(&action); } #endif int vt = 0; #ifdef HAS_LOGIND // Init logind if we are not running as SUID. // We need event loop for logind to work, and thus we won't allow it on SUID process. if (!privilidged && !x11display && has_logind) { if (!(wlc.display = wl_display_create())) die("Failed to create wayland display"); if (!(vt = wlc_logind_init("seat0"))) die("Failed to init logind"); } #else (void)privilidged; #endif if (!x11display) wlc_tty_init(vt); // -- we open tty before dropping permissions // so the fd process can also handle cleanup in case of crash // if logind initialized correctly, fd process does nothing but handle crash. { struct wl_display *display = wlc.display; wlc.display = NULL; wlc_fd_init(argc, argv, (vt != 0)); wlc.display = display; } // -- permissions are now dropped wl_signal_init(&wlc.signals.terminate); wl_signal_init(&wlc.signals.activate); wl_signal_init(&wlc.signals.compositor); wl_signal_init(&wlc.signals.focus); wl_signal_init(&wlc.signals.surface); wl_signal_init(&wlc.signals.input); wl_signal_init(&wlc.signals.output); wl_signal_init(&wlc.signals.render); wl_signal_init(&wlc.signals.xwayland); wl_signal_add(&wlc.signals.compositor, &compositor_listener); if (!wlc_resources_init()) die("Failed to init resource manager"); if (!wlc.display && !(wlc.display = wl_display_create())) die("Failed to create wayland display"); const char *socket_name; if (!(socket_name = wl_display_add_socket_auto(wlc.display))) die("Failed to add socket to wayland display"); if (socket_name) // shut up static analyze setenv("WAYLAND_DISPLAY", socket_name, true); if (wl_display_init_shm(wlc.display) != 0) die("Failed to init shm"); if (!wlc_udev_init()) die("Failed to init udev"); const char *libinput = getenv("WLC_LIBINPUT"); if (!x11display || (libinput && !chck_cstreq(libinput, "0"))) { if (!wlc_input_init()) die("Failed to init input"); } memcpy(&wlc.interface, interface, sizeof(wlc.interface)); if (!wlc_compositor(&wlc.compositor)) die("Failed to init compositor"); const char *xwayland = getenv("WLC_XWAYLAND"); if (!xwayland || !chck_cstreq(xwayland, "0")) { if (!(wlc_xwayland_init())) die("Failed to init xwayland"); } else { wlc.set_ready_on_run = true; } wlc_set_active(true); return wlc_compositor_is_good(&wlc.compositor); }
WLC_API bool wlc_init(const struct wlc_interface *interface, int argc, char *argv[]) { assert(interface); if (!interface) die("no wlc_interface was given"); if (wlc.display) return true; // reset wlc state, but keep log function void *log_fun = wlc.log_fun; memset(&wlc, 0, sizeof(wlc)); wlc.log_fun = log_fun; wl_log_set_handler_server(wl_cb_log); unsetenv("TERM"); const char *x11display = getenv("DISPLAY"); bool privileged = false; const bool has_logind = wlc_logind_available(); if (getuid() != geteuid() || getgid() != getegid()) { wlc_log(WLC_LOG_INFO, "Doing work on SUID/SGID side and dropping permissions"); privileged = true; } else if (getuid() == 0) { die("Do not run wlc compositor as root"); } else if (!x11display && !has_logind && access("/dev/input/event0", R_OK | W_OK) != 0) { die("Not running from X11 and no access to /dev/input/event0 or logind available"); } int vt = 0; #ifdef HAS_LOGIND // Init logind if we are not running as SUID. // We need event loop for logind to work, and thus we won't allow it on SUID process. if (!privileged && !x11display && has_logind) { if (!(wlc.display = wl_display_create())) die("Failed to create wayland display"); if (!(vt = wlc_logind_init("seat0"))) die("Failed to init logind"); } #else (void)privileged; #endif if (!x11display) wlc_tty_init(vt); // -- we open tty before dropping permissions // so the fd process can also handle cleanup in case of crash // if logind initialized correctly, fd process does nothing but handle crash. { struct wl_display *display = wlc.display; wlc.display = NULL; wlc_fd_init(argc, argv, (vt != 0)); wlc.display = display; } // -- permissions are now dropped wl_signal_init(&wlc.signals.terminate); wl_signal_init(&wlc.signals.activate); wl_signal_init(&wlc.signals.compositor); wl_signal_init(&wlc.signals.focus); wl_signal_init(&wlc.signals.surface); wl_signal_init(&wlc.signals.input); wl_signal_init(&wlc.signals.output); wl_signal_init(&wlc.signals.render); wl_signal_init(&wlc.signals.xwayland); wl_signal_add(&wlc.signals.compositor, &compositor_listener); if (!wlc_resources_init()) die("Failed to init resource manager"); if (!wlc.display && !(wlc.display = wl_display_create())) die("Failed to create wayland display"); const char *socket_name; if (!(socket_name = wl_display_add_socket_auto(wlc.display))) die("Failed to add socket to wayland display"); if (socket_name) // shut up static analyze setenv("WAYLAND_DISPLAY", socket_name, true); if (wl_display_init_shm(wlc.display) != 0) die("Failed to init shm"); if (!wlc_udev_init()) die("Failed to init udev"); const char *libinput = getenv("WLC_LIBINPUT"); if (!x11display || (libinput && !chck_cstreq(libinput, "0"))) { if (!wlc_input_init()) die("Failed to init input"); } if (!wlc_compositor(&wlc.compositor)) die("Failed to init compositor"); memcpy(&wlc.interface, interface, sizeof(wlc.interface)); return true; }