commit
5e9959daaa
@ -0,0 +1,260 @@
|
|||||||
|
#include <GLES2/gl2.h>
|
||||||
|
#include <linux/input-event-codes.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include <wayland-egl.h>
|
||||||
|
#include <wlr/render/egl.h>
|
||||||
|
#include "xdg-shell-client-protocol.h"
|
||||||
|
#include "pointer-constraints-unstable-v1-client-protocol.h"
|
||||||
|
|
||||||
|
static int width = 512, height = 512;
|
||||||
|
|
||||||
|
static struct wl_compositor *compositor = NULL;
|
||||||
|
static struct wl_seat *seat = NULL;
|
||||||
|
static struct xdg_wm_base *wm_base = NULL;
|
||||||
|
static struct zwp_pointer_constraints_v1 *pointer_constraints = NULL;
|
||||||
|
|
||||||
|
struct wlr_egl egl;
|
||||||
|
struct wl_egl_window *egl_window;
|
||||||
|
struct wlr_egl_surface *egl_surface;
|
||||||
|
struct zwp_locked_pointer_v1* locked_pointer;
|
||||||
|
struct zwp_confined_pointer_v1* confined_pointer;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
REGION_TYPE_NONE,
|
||||||
|
REGION_TYPE_DISJOINT,
|
||||||
|
REGION_TYPE_JOINT,
|
||||||
|
REGION_TYPE_MAX
|
||||||
|
} region_type = REGION_TYPE_NONE;
|
||||||
|
|
||||||
|
struct wl_region *regions[3];
|
||||||
|
|
||||||
|
static void draw(void) {
|
||||||
|
eglMakeCurrent(egl.display, egl_surface, egl_surface, egl.context);
|
||||||
|
|
||||||
|
float color[] = {1.0, 1.0, 0.0, 1.0};
|
||||||
|
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
glClearColor(color[0], color[1], color[2], 1.0);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
eglSwapBuffers(egl.display, egl_surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_button(void *data, struct wl_pointer *pointer,
|
||||||
|
uint32_t serial, uint32_t time, uint32_t button, uint32_t state_w) {
|
||||||
|
struct wl_surface *surface = data;
|
||||||
|
|
||||||
|
if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED) {
|
||||||
|
region_type = (region_type + 1) % REGION_TYPE_MAX;
|
||||||
|
|
||||||
|
if (locked_pointer) {
|
||||||
|
zwp_locked_pointer_v1_set_region(locked_pointer,
|
||||||
|
regions[region_type]);
|
||||||
|
} else if (confined_pointer) {
|
||||||
|
zwp_confined_pointer_v1_set_region(confined_pointer,
|
||||||
|
regions[region_type]);
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_surface_commit(surface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
|
||||||
|
uint32_t serial, struct wl_surface *surface,
|
||||||
|
wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
|
||||||
|
uint32_t serial, struct wl_surface *surface) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
|
||||||
|
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
|
||||||
|
uint32_t time, uint32_t axis, wl_fixed_t value) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_frame(void *data, struct wl_pointer *wl_pointer) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_axis_source(void *data,
|
||||||
|
struct wl_pointer *wl_pointer, uint32_t axis_source) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_axis_stop(void *data,
|
||||||
|
struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_handle_axis_discrete(void *data,
|
||||||
|
struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) {
|
||||||
|
// This space intentionally left blank
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct wl_pointer_listener pointer_listener = {
|
||||||
|
.enter = pointer_handle_enter,
|
||||||
|
.leave = pointer_handle_leave,
|
||||||
|
.motion = pointer_handle_motion,
|
||||||
|
.button = pointer_handle_button,
|
||||||
|
.axis = pointer_handle_axis,
|
||||||
|
.frame = pointer_handle_frame,
|
||||||
|
.axis_source = pointer_handle_axis_source,
|
||||||
|
.axis_stop = pointer_handle_axis_stop,
|
||||||
|
.axis_discrete = pointer_handle_axis_discrete,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void xdg_surface_handle_configure(void *data,
|
||||||
|
struct xdg_surface *xdg_surface, uint32_t serial) {
|
||||||
|
xdg_surface_ack_configure(xdg_surface, serial);
|
||||||
|
wl_egl_window_resize(egl_window, width, height, 0, 0);
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct xdg_surface_listener xdg_surface_listener = {
|
||||||
|
.configure = xdg_surface_handle_configure,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void xdg_toplevel_handle_configure(void *data,
|
||||||
|
struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h,
|
||||||
|
struct wl_array *states) {
|
||||||
|
width = w;
|
||||||
|
height = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xdg_toplevel_handle_close(void *data,
|
||||||
|
struct xdg_toplevel *xdg_toplevel) {
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
|
||||||
|
.configure = xdg_toplevel_handle_configure,
|
||||||
|
.close = xdg_toplevel_handle_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void handle_global(void *data, struct wl_registry *registry,
|
||||||
|
uint32_t name, const char *interface, uint32_t version) {
|
||||||
|
if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||||
|
compositor = wl_registry_bind(registry, name,
|
||||||
|
&wl_compositor_interface, 1);
|
||||||
|
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
||||||
|
wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
|
||||||
|
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
||||||
|
seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
|
||||||
|
} else if (strcmp(interface,
|
||||||
|
zwp_pointer_constraints_v1_interface.name) == 0) {
|
||||||
|
pointer_constraints = wl_registry_bind(registry, name,
|
||||||
|
&zwp_pointer_constraints_v1_interface, version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct wl_registry_listener registry_listener = {
|
||||||
|
.global = handle_global,
|
||||||
|
.global_remove = NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
if (argc != 4) {
|
||||||
|
goto invalid_args;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool lock;
|
||||||
|
if (strcmp(argv[1], "lock") == 0) {
|
||||||
|
lock = true;
|
||||||
|
} else if (strcmp(argv[1], "confine") == 0) {
|
||||||
|
lock = false;
|
||||||
|
} else {
|
||||||
|
goto invalid_args;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum zwp_pointer_constraints_v1_lifetime lifetime;
|
||||||
|
if (strcmp(argv[2], "oneshot") == 0) {
|
||||||
|
lifetime = ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT;
|
||||||
|
} else if (strcmp(argv[2], "persistent") == 0) {
|
||||||
|
lifetime = ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT;
|
||||||
|
} else {
|
||||||
|
goto invalid_args;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp(argv[3], "no-region") == 0) {
|
||||||
|
region_type = REGION_TYPE_NONE;
|
||||||
|
} else if (strcmp(argv[3], "disjoint-region") == 0) {
|
||||||
|
region_type = REGION_TYPE_DISJOINT;
|
||||||
|
} else if (strcmp(argv[3], "joint-region") == 0) {
|
||||||
|
region_type = REGION_TYPE_JOINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wl_display *display = wl_display_connect(NULL);
|
||||||
|
|
||||||
|
struct wl_registry *registry = wl_display_get_registry(display);
|
||||||
|
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
||||||
|
wl_display_dispatch(display);
|
||||||
|
wl_display_roundtrip(display);
|
||||||
|
|
||||||
|
struct wl_region *disjoint_region = wl_compositor_create_region(compositor);
|
||||||
|
wl_region_add(disjoint_region, 0, 0, 255, 256);
|
||||||
|
wl_region_add(disjoint_region, 257, 0, 255, 256);
|
||||||
|
regions[REGION_TYPE_DISJOINT] = disjoint_region;
|
||||||
|
|
||||||
|
struct wl_region *joint_region = wl_compositor_create_region(compositor);
|
||||||
|
wl_region_add(joint_region, 0, 0, 256, 256);
|
||||||
|
wl_region_add(joint_region, 256, 0, 256, 256);
|
||||||
|
wl_region_add(joint_region, 256, 256, 256, 256);
|
||||||
|
regions[REGION_TYPE_JOINT] = joint_region;
|
||||||
|
|
||||||
|
wlr_egl_init(&egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL,
|
||||||
|
WL_SHM_FORMAT_ARGB8888);
|
||||||
|
|
||||||
|
struct wl_surface *surface = wl_compositor_create_surface(compositor);
|
||||||
|
struct xdg_surface *xdg_surface =
|
||||||
|
xdg_wm_base_get_xdg_surface(wm_base, surface);
|
||||||
|
struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
|
||||||
|
|
||||||
|
xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);
|
||||||
|
xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);
|
||||||
|
|
||||||
|
struct wl_pointer *pointer = wl_seat_get_pointer(seat);
|
||||||
|
wl_pointer_add_listener(pointer, &pointer_listener, surface);
|
||||||
|
|
||||||
|
if (lock) {
|
||||||
|
locked_pointer = zwp_pointer_constraints_v1_lock_pointer(
|
||||||
|
pointer_constraints, surface, pointer,
|
||||||
|
regions[region_type], lifetime);
|
||||||
|
|
||||||
|
zwp_locked_pointer_v1_set_cursor_position_hint(locked_pointer,
|
||||||
|
wl_fixed_from_int(128), wl_fixed_from_int(128));
|
||||||
|
} else {
|
||||||
|
confined_pointer = zwp_pointer_constraints_v1_confine_pointer(
|
||||||
|
pointer_constraints, surface, pointer,
|
||||||
|
regions[region_type], lifetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_surface_commit(surface);
|
||||||
|
|
||||||
|
egl_window = wl_egl_window_create(surface, width, height);
|
||||||
|
egl_surface = wlr_egl_create_surface(&egl, egl_window);
|
||||||
|
|
||||||
|
wl_display_roundtrip(display);
|
||||||
|
|
||||||
|
draw();
|
||||||
|
|
||||||
|
while (wl_display_dispatch(display) != -1) {}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
|
invalid_args:
|
||||||
|
fprintf(stderr, "pointer-constraints <lock | confine> "
|
||||||
|
"<oneshot | persistent> "
|
||||||
|
"<no-region | disjoint-rejoin | joint-region>\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* This an unstable interface of wlroots. No guarantees are made regarding the
|
||||||
|
* future consistency of this API.
|
||||||
|
*/
|
||||||
|
#ifndef WLR_USE_UNSTABLE
|
||||||
|
#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WLR_TYPES_WLR_POINTER_CONSTRAINTS_V1_H
|
||||||
|
#define WLR_TYPES_WLR_POINTER_CONSTRAINTS_V1_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <wayland-server.h>
|
||||||
|
#include <pixman.h>
|
||||||
|
#include <wlr/types/wlr_box.h>
|
||||||
|
#include <wlr/types/wlr_seat.h>
|
||||||
|
#include "pointer-constraints-unstable-v1-protocol.h"
|
||||||
|
|
||||||
|
struct wlr_seat;
|
||||||
|
|
||||||
|
enum wlr_pointer_constraint_v1_type {
|
||||||
|
WLR_POINTER_CONSTRAINT_V1_LOCKED,
|
||||||
|
WLR_POINTER_CONSTRAINT_V1_CONFINED,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum wlr_pointer_constraint_v1_state_field {
|
||||||
|
WLR_POINTER_CONSTRAINT_V1_STATE_REGION = 1 << 0,
|
||||||
|
WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT = 1 << 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_pointer_constraint_v1_state {
|
||||||
|
uint32_t committed; // enum wlr_pointer_constraint_v1_state_field
|
||||||
|
pixman_region32_t region;
|
||||||
|
|
||||||
|
// only valid for locked_pointer
|
||||||
|
struct {
|
||||||
|
double x, y;
|
||||||
|
} cursor_hint;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_pointer_constraint_v1 {
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints;
|
||||||
|
|
||||||
|
struct wl_resource *resource;
|
||||||
|
struct wlr_surface *surface;
|
||||||
|
struct wlr_seat *seat;
|
||||||
|
enum zwp_pointer_constraints_v1_lifetime lifetime;
|
||||||
|
enum wlr_pointer_constraint_v1_type type;
|
||||||
|
pixman_region32_t region;
|
||||||
|
|
||||||
|
struct wlr_pointer_constraint_v1_state current, pending;
|
||||||
|
|
||||||
|
struct wl_listener surface_commit;
|
||||||
|
struct wl_listener surface_destroy;
|
||||||
|
struct wl_listener seat_destroy;
|
||||||
|
|
||||||
|
struct wl_list link; // wlr_pointer_constraints_v1::constraints
|
||||||
|
|
||||||
|
struct {
|
||||||
|
struct wl_signal destroy;
|
||||||
|
} events;
|
||||||
|
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_pointer_constraints_v1 {
|
||||||
|
struct wl_list resources; // wl_resource_get_link
|
||||||
|
struct wl_global *global;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
/**
|
||||||
|
* Called when a new pointer constraint is created.
|
||||||
|
*
|
||||||
|
* data: struct wlr_pointer_constraint_v1 *
|
||||||
|
*/
|
||||||
|
struct wl_signal new_constraint;
|
||||||
|
} events;
|
||||||
|
|
||||||
|
struct wl_list constraints; // wlr_pointer_constraint_v1::link
|
||||||
|
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_pointer_constraints_v1 *wlr_pointer_constraints_v1_create(
|
||||||
|
struct wl_display *display);
|
||||||
|
void wlr_pointer_constraints_v1_destroy(
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints);
|
||||||
|
|
||||||
|
struct wlr_pointer_constraint_v1 *
|
||||||
|
wlr_pointer_constraints_v1_constraint_for_surface(
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints,
|
||||||
|
struct wlr_surface *surface, struct wlr_seat *seat);
|
||||||
|
|
||||||
|
void wlr_pointer_constraint_v1_send_activated(
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint);
|
||||||
|
/**
|
||||||
|
* Deactivate the constraint. May destroy the constraint.
|
||||||
|
*/
|
||||||
|
void wlr_pointer_constraint_v1_send_deactivated(
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint);
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,372 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <pixman.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <wayland-server.h>
|
||||||
|
#include <wlr/types/wlr_box.h>
|
||||||
|
#include <wlr/types/wlr_pointer_constraints_v1.h>
|
||||||
|
#include <wlr/types/wlr_region.h>
|
||||||
|
#include <wlr/util/log.h>
|
||||||
|
#include "util/signal.h"
|
||||||
|
|
||||||
|
static const struct zwp_locked_pointer_v1_interface locked_pointer_impl;
|
||||||
|
static const struct zwp_confined_pointer_v1_interface confined_pointer_impl;
|
||||||
|
static const struct zwp_pointer_constraints_v1_interface pointer_constraints_impl;
|
||||||
|
static void pointer_constraint_destroy(struct wlr_pointer_constraint_v1 *constraint);
|
||||||
|
|
||||||
|
static struct wlr_pointer_constraint_v1 *pointer_constraint_from_resource(
|
||||||
|
struct wl_resource *resource) {
|
||||||
|
assert(
|
||||||
|
wl_resource_instance_of(
|
||||||
|
resource, &zwp_confined_pointer_v1_interface,
|
||||||
|
&confined_pointer_impl) ||
|
||||||
|
wl_resource_instance_of(
|
||||||
|
resource, &zwp_locked_pointer_v1_interface,
|
||||||
|
&locked_pointer_impl));
|
||||||
|
return wl_resource_get_user_data(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct wlr_pointer_constraints_v1 *pointer_constraints_from_resource(
|
||||||
|
struct wl_resource *resource) {
|
||||||
|
assert(wl_resource_instance_of(resource, &zwp_pointer_constraints_v1_interface,
|
||||||
|
&pointer_constraints_impl));
|
||||||
|
return wl_resource_get_user_data(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void resource_destroy(struct wl_client *client,
|
||||||
|
struct wl_resource *resource) {
|
||||||
|
wl_resource_destroy(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraint_destroy(struct wlr_pointer_constraint_v1 *constraint) {
|
||||||
|
if (constraint == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wlr_log(WLR_DEBUG, "destroying constraint %p", constraint);
|
||||||
|
|
||||||
|
wlr_signal_emit_safe(&constraint->events.destroy, constraint);
|
||||||
|
|
||||||
|
wl_resource_set_user_data(constraint->resource, NULL);
|
||||||
|
wl_list_remove(&constraint->link);
|
||||||
|
wl_list_remove(&constraint->surface_commit.link);
|
||||||
|
wl_list_remove(&constraint->surface_destroy.link);
|
||||||
|
wl_list_remove(&constraint->seat_destroy.link);
|
||||||
|
pixman_region32_fini(&constraint->current.region);
|
||||||
|
pixman_region32_fini(&constraint->pending.region);
|
||||||
|
pixman_region32_fini(&constraint->region);
|
||||||
|
free(constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraint_destroy_resource(struct wl_resource *resource) {
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint =
|
||||||
|
pointer_constraint_from_resource(resource);
|
||||||
|
|
||||||
|
pointer_constraint_destroy(constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraint_set_region(
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint,
|
||||||
|
struct wl_resource *region_resource) {
|
||||||
|
pixman_region32_clear(&constraint->pending.region);
|
||||||
|
|
||||||
|
if (region_resource) {
|
||||||
|
pixman_region32_t *region = wlr_region_from_resource(region_resource);
|
||||||
|
pixman_region32_copy(&constraint->pending.region, region);
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint->pending.committed |= WLR_POINTER_CONSTRAINT_V1_STATE_REGION;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraint_handle_set_region(struct wl_client *client,
|
||||||
|
struct wl_resource *resource, struct wl_resource *region_resource) {
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint =
|
||||||
|
pointer_constraint_from_resource(resource);
|
||||||
|
if (constraint == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pointer_constraint_set_region(constraint, region_resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraint_set_cursor_position_hint(struct wl_client *client,
|
||||||
|
struct wl_resource *resource, wl_fixed_t x, wl_fixed_t y) {
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint =
|
||||||
|
pointer_constraint_from_resource(resource);
|
||||||
|
if (constraint == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint->pending.cursor_hint.x = wl_fixed_to_double(x);
|
||||||
|
constraint->pending.cursor_hint.y = wl_fixed_to_double(y);
|
||||||
|
constraint->pending.committed |= WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraint_commit(
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint) {
|
||||||
|
if (constraint->pending.committed &
|
||||||
|
WLR_POINTER_CONSTRAINT_V1_STATE_REGION) {
|
||||||
|
pixman_region32_copy(&constraint->current.region,
|
||||||
|
&constraint->pending.region);
|
||||||
|
}
|
||||||
|
if (constraint->pending.committed &
|
||||||
|
WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT) {
|
||||||
|
constraint->current.cursor_hint = constraint->pending.cursor_hint;
|
||||||
|
}
|
||||||
|
constraint->current.committed |= constraint->pending.committed;
|
||||||
|
|
||||||
|
constraint->pending.committed = 0;
|
||||||
|
|
||||||
|
pixman_region32_clear(&constraint->region);
|
||||||
|
if (pixman_region32_not_empty(&constraint->current.region)) {
|
||||||
|
pixman_region32_intersect(&constraint->region,
|
||||||
|
&constraint->surface->input_region, &constraint->current.region);
|
||||||
|
} else {
|
||||||
|
pixman_region32_copy(&constraint->region,
|
||||||
|
&constraint->surface->input_region);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_surface_commit(struct wl_listener *listener, void *data) {
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint =
|
||||||
|
wl_container_of(listener, constraint, surface_commit);
|
||||||
|
|
||||||
|
pointer_constraint_commit(constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_surface_destroy(struct wl_listener *listener, void *data) {
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint =
|
||||||
|
wl_container_of(listener, constraint, surface_destroy);
|
||||||
|
|
||||||
|
pointer_constraint_destroy(constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_seat_destroy(struct wl_listener *listener, void *data) {
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint =
|
||||||
|
wl_container_of(listener, constraint, seat_destroy);
|
||||||
|
|
||||||
|
pointer_constraint_destroy(constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct zwp_confined_pointer_v1_interface confined_pointer_impl = {
|
||||||
|
.destroy = resource_destroy,
|
||||||
|
.set_region = pointer_constraint_handle_set_region,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct zwp_locked_pointer_v1_interface locked_pointer_impl = {
|
||||||
|
.destroy = resource_destroy,
|
||||||
|
.set_region = pointer_constraint_handle_set_region,
|
||||||
|
.set_cursor_position_hint = pointer_constraint_set_cursor_position_hint,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void pointer_constraint_create(struct wl_client *client,
|
||||||
|
struct wl_resource *pointer_constraints_resource, uint32_t id,
|
||||||
|
struct wl_resource *surface_resource,
|
||||||
|
struct wl_resource *pointer_resource,
|
||||||
|
struct wl_resource *region_resource,
|
||||||
|
enum zwp_pointer_constraints_v1_lifetime lifetime,
|
||||||
|
enum wlr_pointer_constraint_v1_type type) {
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints =
|
||||||
|
pointer_constraints_from_resource(pointer_constraints_resource);
|
||||||
|
|
||||||
|
struct wlr_surface *surface = wlr_surface_from_resource(surface_resource);
|
||||||
|
struct wlr_seat *seat =
|
||||||
|
wlr_seat_client_from_pointer_resource(pointer_resource)->seat;
|
||||||
|
|
||||||
|
if (wlr_pointer_constraints_v1_constraint_for_surface(pointer_constraints,
|
||||||
|
surface, seat)) {
|
||||||
|
wl_resource_post_error(pointer_constraints_resource,
|
||||||
|
ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED,
|
||||||
|
"a pointer constraint with a wl_pointer of the same wl_seat"
|
||||||
|
" is already on this surface");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t version = wl_resource_get_version(pointer_constraints_resource);
|
||||||
|
|
||||||
|
bool locked_pointer = type == WLR_POINTER_CONSTRAINT_V1_LOCKED;
|
||||||
|
|
||||||
|
struct wl_resource *resource = locked_pointer ?
|
||||||
|
wl_resource_create(client, &zwp_locked_pointer_v1_interface, version, id) :
|
||||||
|
wl_resource_create(client, &zwp_confined_pointer_v1_interface, version, id);
|
||||||
|
if (resource == NULL) {
|
||||||
|
wl_client_post_no_memory(client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint = calloc(1, sizeof(*constraint));
|
||||||
|
if (constraint == NULL) {
|
||||||
|
wl_resource_destroy(resource);
|
||||||
|
wl_client_post_no_memory(client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint->resource = resource;
|
||||||
|
constraint->surface = surface;
|
||||||
|
constraint->seat = seat;
|
||||||
|
constraint->lifetime = lifetime;
|
||||||
|
constraint->type = type;
|
||||||
|
constraint->pointer_constraints = pointer_constraints;
|
||||||
|
|
||||||
|
wl_signal_init(&constraint->events.destroy);
|
||||||
|
|
||||||
|
pixman_region32_init(&constraint->region);
|
||||||
|
|
||||||
|
pixman_region32_init(&constraint->pending.region);
|
||||||
|
pixman_region32_init(&constraint->current.region);
|
||||||
|
|
||||||
|
pointer_constraint_set_region(constraint, region_resource);
|
||||||
|
pointer_constraint_commit(constraint);
|
||||||
|
|
||||||
|
constraint->surface_commit.notify = handle_surface_commit;
|
||||||
|
wl_signal_add(&surface->events.commit, &constraint->surface_commit);
|
||||||
|
|
||||||
|
constraint->surface_destroy.notify = handle_surface_destroy;
|
||||||
|
wl_signal_add(&surface->events.destroy, &constraint->surface_destroy);
|
||||||
|
|
||||||
|
constraint->seat_destroy.notify = handle_seat_destroy;
|
||||||
|
wl_signal_add(&seat->events.destroy, &constraint->seat_destroy);
|
||||||
|
|
||||||
|
void *impl = locked_pointer ?
|
||||||
|
(void *)&locked_pointer_impl : (void *)&confined_pointer_impl;
|
||||||
|
wl_resource_set_implementation(constraint->resource, impl, constraint,
|
||||||
|
pointer_constraint_destroy_resource);
|
||||||
|
|
||||||
|
wlr_log(WLR_DEBUG, "new %s_pointer %p (res %p)",
|
||||||
|
locked_pointer ? "locked" : "confined",
|
||||||
|
constraint, constraint->resource);
|
||||||
|
|
||||||
|
wl_list_insert(&pointer_constraints->constraints, &constraint->link);
|
||||||
|
|
||||||
|
wlr_signal_emit_safe(&pointer_constraints->events.new_constraint,
|
||||||
|
constraint);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraints_lock_pointer(struct wl_client *client,
|
||||||
|
struct wl_resource *cons_resource, uint32_t id,
|
||||||
|
struct wl_resource *surface, struct wl_resource *pointer,
|
||||||
|
struct wl_resource *region, enum zwp_pointer_constraints_v1_lifetime lifetime) {
|
||||||
|
pointer_constraint_create(client, cons_resource, id, surface, pointer,
|
||||||
|
region, lifetime, WLR_POINTER_CONSTRAINT_V1_LOCKED);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraints_confine_pointer(struct wl_client *client,
|
||||||
|
struct wl_resource *cons_resource, uint32_t id,
|
||||||
|
struct wl_resource *surface, struct wl_resource *pointer,
|
||||||
|
struct wl_resource *region,
|
||||||
|
enum zwp_pointer_constraints_v1_lifetime lifetime) {
|
||||||
|
pointer_constraint_create(client, cons_resource, id, surface, pointer,
|
||||||
|
region, lifetime, WLR_POINTER_CONSTRAINT_V1_CONFINED);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct zwp_pointer_constraints_v1_interface
|
||||||
|
pointer_constraints_impl = {
|
||||||
|
.destroy = resource_destroy,
|
||||||
|
.lock_pointer = pointer_constraints_lock_pointer,
|
||||||
|
.confine_pointer = pointer_constraints_confine_pointer,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void pointer_constraints_destroy(struct wl_resource *resource) {
|
||||||
|
wl_list_remove(wl_resource_get_link(resource));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pointer_constraints_bind(struct wl_client *client, void *data,
|
||||||
|
uint32_t version, uint32_t id) {
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints = data;
|
||||||
|
assert(client && pointer_constraints);
|
||||||
|
|
||||||
|
struct wl_resource *resource = wl_resource_create(client,
|
||||||
|
&zwp_pointer_constraints_v1_interface, version, id);
|
||||||
|
if (resource == NULL) {
|
||||||
|
wl_client_post_no_memory(client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_list_insert(&pointer_constraints->resources,
|
||||||
|
wl_resource_get_link(resource));
|
||||||
|
wl_resource_set_implementation(resource, &pointer_constraints_impl,
|
||||||
|
pointer_constraints, pointer_constraints_destroy);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_pointer_constraints_v1 *wlr_pointer_constraints_v1_create(
|
||||||
|
struct wl_display *display) {
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints =
|
||||||
|
calloc(1, sizeof(*pointer_constraints));
|
||||||
|
|
||||||
|
if (!pointer_constraints) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wl_global *wl_global = wl_global_create(display,
|
||||||
|
&zwp_pointer_constraints_v1_interface, 1, pointer_constraints,
|
||||||
|
pointer_constraints_bind);
|
||||||
|
if (!wl_global) {
|
||||||
|
free(pointer_constraints);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
pointer_constraints->global = wl_global;
|
||||||
|
|
||||||
|
wl_list_init(&pointer_constraints->resources);
|
||||||
|
wl_list_init(&pointer_constraints->constraints);
|
||||||
|
wl_signal_init(&pointer_constraints->events.new_constraint);
|
||||||
|
|
||||||
|
return pointer_constraints;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_pointer_constraints_v1_destroy(
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints) {
|
||||||
|
struct wl_resource *resource, *_tmp_res;
|
||||||
|
wl_resource_for_each_safe(resource, _tmp_res,
|
||||||
|
&pointer_constraints->resources) {
|
||||||
|
wl_resource_destroy(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint, *_tmp_cons;
|
||||||
|
wl_list_for_each_safe(constraint, _tmp_cons,
|
||||||
|
&pointer_constraints->constraints, link) {
|
||||||
|
wl_resource_destroy(constraint->resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_global_destroy(pointer_constraints->global);
|
||||||
|
free(pointer_constraints);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_pointer_constraint_v1 *
|
||||||
|
wlr_pointer_constraints_v1_constraint_for_surface(
|
||||||
|
struct wlr_pointer_constraints_v1 *pointer_constraints,
|
||||||
|
struct wlr_surface *surface, struct wlr_seat *seat) {
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint;
|
||||||
|
wl_list_for_each(constraint, &pointer_constraints->constraints, link) {
|
||||||
|
if (constraint->surface == surface && constraint->seat == seat) {
|
||||||
|
return constraint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_pointer_constraint_v1_send_activated(
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint) {
|
||||||
|
wlr_log(WLR_DEBUG, "constrained %p", constraint);
|
||||||
|
if (constraint->type == WLR_POINTER_CONSTRAINT_V1_LOCKED) {
|
||||||
|
zwp_locked_pointer_v1_send_locked(constraint->resource);
|
||||||
|
} else {
|
||||||
|
zwp_confined_pointer_v1_send_confined(constraint->resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_pointer_constraint_v1_send_deactivated(
|
||||||
|
struct wlr_pointer_constraint_v1 *constraint) {
|
||||||
|
wlr_log(WLR_DEBUG, "unconstrained %p", constraint);
|
||||||
|
if (constraint->type == WLR_POINTER_CONSTRAINT_V1_LOCKED) {
|
||||||
|
zwp_locked_pointer_v1_send_unlocked(constraint->resource);
|
||||||
|
} else {
|
||||||
|
zwp_confined_pointer_v1_send_unconfined(constraint->resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (constraint->lifetime ==
|
||||||
|
ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT) {
|
||||||
|
pointer_constraint_destroy(constraint);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue