wlroots-hyprland/types/wlr_compositor.c

91 lines
2.7 KiB
C
Raw Normal View History

#include <assert.h>
#include <stdlib.h>
#include <wayland-server-core.h>
2018-02-12 21:29:23 +01:00
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_surface.h>
#include "types/wlr_region.h"
#include "types/wlr_surface.h"
2018-02-12 19:45:58 +01:00
#include "util/signal.h"
#define COMPOSITOR_VERSION 4
2018-04-21 20:02:39 +02:00
static const struct wl_compositor_interface compositor_impl;
2018-04-21 20:02:39 +02:00
static struct wlr_compositor *compositor_from_resource(
struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_compositor_interface,
&compositor_impl));
return wl_resource_get_user_data(resource);
}
static void compositor_create_surface(struct wl_client *client,
struct wl_resource *resource, uint32_t id) {
struct wlr_compositor *compositor = compositor_from_resource(resource);
2017-11-02 23:14:24 +01:00
struct wlr_surface *surface = surface_create(client,
wl_resource_get_version(resource), id, compositor->renderer);
2017-11-02 23:14:24 +01:00
if (surface == NULL) {
wl_client_post_no_memory(client);
2017-11-02 23:14:24 +01:00
return;
}
2017-08-10 12:42:35 +02:00
2018-02-12 09:12:31 +01:00
wlr_signal_emit_safe(&compositor->events.new_surface, surface);
}
static void compositor_create_region(struct wl_client *client,
struct wl_resource *resource, uint32_t id) {
region_create(client, wl_resource_get_version(resource), id);
}
static const struct wl_compositor_interface compositor_impl = {
.create_surface = compositor_create_surface,
.create_region = compositor_create_region,
};
static void compositor_bind(struct wl_client *wl_client, void *data,
uint32_t version, uint32_t id) {
2017-11-02 23:14:24 +01:00
struct wlr_compositor *compositor = data;
2017-10-22 11:18:27 +02:00
2018-04-21 20:02:39 +02:00
struct wl_resource *resource =
2017-09-23 22:21:18 +02:00
wl_resource_create(wl_client, &wl_compositor_interface, version, id);
2018-04-21 20:02:39 +02:00
if (resource == NULL) {
2017-11-02 23:14:24 +01:00
wl_client_post_no_memory(wl_client);
return;
}
wl_resource_set_implementation(resource, &compositor_impl, compositor, NULL);
}
static void compositor_handle_display_destroy(
struct wl_listener *listener, void *data) {
struct wlr_compositor *compositor =
wl_container_of(listener, compositor, display_destroy);
wlr_signal_emit_safe(&compositor->events.destroy, NULL);
wl_list_remove(&compositor->display_destroy.link);
wl_global_destroy(compositor->global);
free(compositor);
}
struct wlr_compositor *wlr_compositor_create(struct wl_display *display,
struct wlr_renderer *renderer) {
struct wlr_compositor *compositor = calloc(1, sizeof(*compositor));
if (!compositor) {
return NULL;
}
2017-09-24 12:04:29 +02:00
compositor->global = wl_global_create(display, &wl_compositor_interface,
COMPOSITOR_VERSION, compositor, compositor_bind);
if (!compositor->global) {
free(compositor);
return NULL;
}
compositor->renderer = renderer;
2017-09-24 12:04:29 +02:00
wl_signal_init(&compositor->events.new_surface);
wl_signal_init(&compositor->events.destroy);
2017-09-24 12:04:29 +02:00
compositor->display_destroy.notify = compositor_handle_display_destroy;
wl_display_add_destroy_listener(display, &compositor->display_destroy);
return compositor;
}