2017-08-20 22:02:39 +02:00
|
|
|
#include <wlr/types/wlr_cursor.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2017-08-27 23:35:12 +02:00
|
|
|
#include <limits.h>
|
2017-08-20 22:02:39 +02:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include <wayland-server.h>
|
|
|
|
#include <wlr/types/wlr_output.h>
|
|
|
|
#include <wlr/types/wlr_output_layout.h>
|
|
|
|
#include <wlr/types/wlr_input_device.h>
|
|
|
|
|
|
|
|
struct wlr_cursor_device {
|
|
|
|
struct wlr_cursor *cursor;
|
|
|
|
struct wlr_input_device *device;
|
|
|
|
struct wl_list link;
|
2017-08-24 20:35:55 +02:00
|
|
|
struct wlr_output *mapped_output;
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_box *mapped_box;
|
2017-08-20 22:02:39 +02:00
|
|
|
|
|
|
|
struct wl_listener motion;
|
|
|
|
struct wl_listener motion_absolute;
|
|
|
|
struct wl_listener button;
|
|
|
|
struct wl_listener axis;
|
2017-08-27 17:34:25 +02:00
|
|
|
|
|
|
|
struct wl_listener touch_down;
|
|
|
|
struct wl_listener touch_up;
|
|
|
|
struct wl_listener touch_motion;
|
|
|
|
struct wl_listener touch_cancel;
|
2017-08-28 14:42:39 +02:00
|
|
|
|
|
|
|
struct wl_listener tablet_tool_axis;
|
|
|
|
struct wl_listener tablet_tool_proximity;
|
|
|
|
struct wl_listener tablet_tool_tip;
|
|
|
|
struct wl_listener tablet_tool_button;
|
2017-08-28 16:29:53 +02:00
|
|
|
|
|
|
|
struct wl_listener destroy;
|
2017-08-20 22:02:39 +02:00
|
|
|
};
|
|
|
|
|
2017-10-29 09:09:21 +01:00
|
|
|
struct wlr_cursor_output_cursor {
|
|
|
|
struct wlr_cursor *cursor;
|
|
|
|
struct wlr_output_cursor *output_cursor;
|
|
|
|
struct wl_list link;
|
2017-10-29 11:20:11 +01:00
|
|
|
|
|
|
|
struct wl_listener layout_output_destroy;
|
2017-10-29 09:09:21 +01:00
|
|
|
};
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
struct wlr_cursor_state {
|
2017-09-05 13:48:28 +02:00
|
|
|
struct wlr_cursor *cursor;
|
2017-10-29 09:09:21 +01:00
|
|
|
struct wl_list devices; // wlr_cursor_device::link
|
|
|
|
struct wl_list output_cursors; // wlr_cursor_output_cursor::link
|
2017-08-20 22:02:39 +02:00
|
|
|
struct wlr_output_layout *layout;
|
2017-08-24 18:30:34 +02:00
|
|
|
struct wlr_output *mapped_output;
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_box *mapped_box;
|
2017-09-05 13:48:28 +02:00
|
|
|
|
2017-10-29 11:20:11 +01:00
|
|
|
struct wl_listener layout_add;
|
2017-09-05 13:48:28 +02:00
|
|
|
struct wl_listener layout_change;
|
|
|
|
struct wl_listener layout_destroy;
|
2017-08-20 22:02:39 +02:00
|
|
|
};
|
|
|
|
|
2017-08-29 16:42:23 +02:00
|
|
|
struct wlr_cursor *wlr_cursor_create() {
|
2017-08-20 22:02:39 +02:00
|
|
|
struct wlr_cursor *cur = calloc(1, sizeof(struct wlr_cursor));
|
|
|
|
if (!cur) {
|
|
|
|
wlr_log(L_ERROR, "Failed to allocate wlr_cursor");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->state = calloc(1, sizeof(struct wlr_cursor_state));
|
|
|
|
if (!cur->state) {
|
|
|
|
wlr_log(L_ERROR, "Failed to allocate wlr_cursor_state");
|
2017-08-28 17:07:54 +02:00
|
|
|
free(cur);
|
2017-08-20 22:02:39 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-05 13:48:28 +02:00
|
|
|
cur->state->cursor = cur;
|
2017-08-24 18:30:34 +02:00
|
|
|
cur->state->mapped_output = NULL;
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
wl_list_init(&cur->state->devices);
|
2017-10-29 11:20:11 +01:00
|
|
|
wl_list_init(&cur->state->output_cursors);
|
2017-08-20 22:02:39 +02:00
|
|
|
|
2017-08-27 17:34:25 +02:00
|
|
|
// pointer signals
|
2017-08-20 22:02:39 +02:00
|
|
|
wl_signal_init(&cur->events.motion);
|
|
|
|
wl_signal_init(&cur->events.motion_absolute);
|
|
|
|
wl_signal_init(&cur->events.button);
|
|
|
|
wl_signal_init(&cur->events.axis);
|
|
|
|
|
2017-08-27 17:34:25 +02:00
|
|
|
// touch signals
|
|
|
|
wl_signal_init(&cur->events.touch_up);
|
|
|
|
wl_signal_init(&cur->events.touch_down);
|
|
|
|
wl_signal_init(&cur->events.touch_motion);
|
|
|
|
wl_signal_init(&cur->events.touch_cancel);
|
|
|
|
|
2017-08-28 14:42:39 +02:00
|
|
|
// tablet tool signals
|
|
|
|
wl_signal_init(&cur->events.tablet_tool_tip);
|
|
|
|
wl_signal_init(&cur->events.tablet_tool_axis);
|
|
|
|
wl_signal_init(&cur->events.tablet_tool_button);
|
|
|
|
wl_signal_init(&cur->events.tablet_tool_proximity);
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
cur->x = 100;
|
|
|
|
cur->y = 100;
|
|
|
|
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:58:58 +01:00
|
|
|
static void output_cursor_destroy(
|
2017-10-29 20:03:56 +01:00
|
|
|
struct wlr_cursor_output_cursor *output_cursor) {
|
2017-10-31 14:21:12 +01:00
|
|
|
wl_list_remove(&output_cursor->layout_output_destroy.link);
|
2017-10-29 20:03:56 +01:00
|
|
|
wl_list_remove(&output_cursor->link);
|
|
|
|
wlr_output_cursor_destroy(output_cursor->output_cursor);
|
|
|
|
free(output_cursor);
|
|
|
|
}
|
|
|
|
|
2017-09-05 13:48:28 +02:00
|
|
|
static void wlr_cursor_detach_output_layout(struct wlr_cursor *cur) {
|
|
|
|
if (!cur->state->layout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:58:58 +01:00
|
|
|
struct wlr_cursor_output_cursor *output_cursor, *tmp;
|
|
|
|
wl_list_for_each_safe(output_cursor, tmp, &cur->state->output_cursors,
|
|
|
|
link) {
|
|
|
|
output_cursor_destroy(output_cursor);
|
2017-10-29 20:03:56 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 13:48:28 +02:00
|
|
|
wl_list_remove(&cur->state->layout_destroy.link);
|
|
|
|
wl_list_remove(&cur->state->layout_change.link);
|
2017-10-29 11:20:11 +01:00
|
|
|
wl_list_remove(&cur->state->layout_add.link);
|
2017-09-05 13:48:28 +02:00
|
|
|
|
|
|
|
cur->state->layout = NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-29 20:58:58 +01:00
|
|
|
static void wlr_cursor_device_destroy(struct wlr_cursor_device *c_device) {
|
|
|
|
struct wlr_input_device *dev = c_device->device;
|
|
|
|
if (dev->type == WLR_INPUT_DEVICE_POINTER) {
|
|
|
|
wl_list_remove(&c_device->motion.link);
|
|
|
|
wl_list_remove(&c_device->motion_absolute.link);
|
|
|
|
wl_list_remove(&c_device->button.link);
|
|
|
|
wl_list_remove(&c_device->axis.link);
|
|
|
|
} else if (dev->type == WLR_INPUT_DEVICE_TOUCH) {
|
|
|
|
wl_list_remove(&c_device->touch_down.link);
|
|
|
|
wl_list_remove(&c_device->touch_up.link);
|
|
|
|
wl_list_remove(&c_device->touch_motion.link);
|
|
|
|
wl_list_remove(&c_device->touch_cancel.link);
|
|
|
|
} else if (dev->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
|
|
|
|
wl_list_remove(&c_device->tablet_tool_axis.link);
|
|
|
|
wl_list_remove(&c_device->tablet_tool_proximity.link);
|
|
|
|
wl_list_remove(&c_device->tablet_tool_tip.link);
|
|
|
|
wl_list_remove(&c_device->tablet_tool_button.link);
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_remove(&c_device->link);
|
|
|
|
wl_list_remove(&c_device->destroy.link);
|
|
|
|
free(c_device);
|
|
|
|
}
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
void wlr_cursor_destroy(struct wlr_cursor *cur) {
|
2017-09-05 13:48:28 +02:00
|
|
|
wlr_cursor_detach_output_layout(cur);
|
|
|
|
|
2017-08-24 20:35:55 +02:00
|
|
|
struct wlr_cursor_device *device, *device_tmp = NULL;
|
|
|
|
wl_list_for_each_safe(device, device_tmp, &cur->state->devices, link) {
|
2017-10-29 20:58:58 +01:00
|
|
|
wlr_cursor_device_destroy(device);
|
2017-08-20 22:02:39 +02:00
|
|
|
}
|
|
|
|
|
2017-10-31 14:58:58 +01:00
|
|
|
free(cur->state);
|
2017-08-20 22:02:39 +02:00
|
|
|
free(cur);
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:35:55 +02:00
|
|
|
static struct wlr_cursor_device *get_cursor_device(struct wlr_cursor *cur,
|
|
|
|
struct wlr_input_device *device) {
|
|
|
|
struct wlr_cursor_device *c_device, *ret = NULL;
|
|
|
|
wl_list_for_each(c_device, &cur->state->devices, link) {
|
|
|
|
if (c_device->device == device) {
|
|
|
|
ret = c_device;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:31:39 +02:00
|
|
|
static void wlr_cursor_warp_unchecked(struct wlr_cursor *cur,
|
|
|
|
double x, double y) {
|
2017-08-28 02:10:46 +02:00
|
|
|
assert(cur->state->layout);
|
2017-08-27 23:35:12 +02:00
|
|
|
|
2017-10-29 09:09:21 +01:00
|
|
|
struct wlr_cursor_output_cursor *output_cursor;
|
|
|
|
wl_list_for_each(output_cursor, &cur->state->output_cursors, link) {
|
2017-08-27 23:35:12 +02:00
|
|
|
double output_x = x;
|
|
|
|
double output_y = y;
|
|
|
|
wlr_output_layout_output_coords(cur->state->layout,
|
2017-10-29 09:09:21 +01:00
|
|
|
output_cursor->output_cursor->output, &output_x, &output_y);
|
2017-10-29 17:43:26 +01:00
|
|
|
wlr_output_cursor_move(output_cursor->output_cursor, output_x,
|
|
|
|
output_y);
|
2017-08-27 23:35:12 +02:00
|
|
|
}
|
2017-08-29 18:48:45 +02:00
|
|
|
|
|
|
|
cur->x = x;
|
|
|
|
cur->y = y;
|
2017-08-27 23:35:12 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
/**
|
|
|
|
* Get the most specific mapping box for the device in this order:
|
|
|
|
*
|
|
|
|
* 1. device geometry mapping
|
|
|
|
* 2. device output mapping
|
|
|
|
* 3. cursor geometry mapping
|
|
|
|
* 4. cursor output mapping
|
|
|
|
*
|
|
|
|
* Absolute movement for touch and pen devices will be relative to this box and
|
|
|
|
* pointer movement will be constrained to this box.
|
|
|
|
*
|
|
|
|
* If none of these are set, returns NULL and absolute movement should be
|
|
|
|
* relative to the extents of the layout.
|
|
|
|
*/
|
2017-08-29 18:08:49 +02:00
|
|
|
static struct wlr_box *get_mapping(struct wlr_cursor *cur,
|
2017-08-28 02:10:46 +02:00
|
|
|
struct wlr_input_device *dev) {
|
2017-08-24 16:53:11 +02:00
|
|
|
assert(cur->state->layout);
|
2017-08-24 20:35:55 +02:00
|
|
|
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
|
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
if (c_device) {
|
2017-08-29 18:08:49 +02:00
|
|
|
if (c_device->mapped_box) {
|
|
|
|
return c_device->mapped_box;
|
2017-08-28 02:10:46 +02:00
|
|
|
}
|
|
|
|
if (c_device->mapped_output) {
|
2017-08-29 18:08:49 +02:00
|
|
|
return wlr_output_layout_get_box(cur->state->layout,
|
2017-08-28 02:10:46 +02:00
|
|
|
c_device->mapped_output);
|
|
|
|
}
|
2017-08-24 20:35:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-29 18:08:49 +02:00
|
|
|
if (cur->state->mapped_box) {
|
|
|
|
return cur->state->mapped_box;
|
2017-08-24 18:30:34 +02:00
|
|
|
}
|
2017-08-29 16:31:39 +02:00
|
|
|
if (cur->state->mapped_output) {
|
2017-08-29 18:08:49 +02:00
|
|
|
return wlr_output_layout_get_box(cur->state->layout,
|
2017-08-28 02:10:46 +02:00
|
|
|
cur->state->mapped_output);
|
2017-08-24 16:42:05 +02:00
|
|
|
}
|
2017-08-24 16:11:57 +02:00
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
return NULL;
|
2017-08-27 23:35:12 +02:00
|
|
|
}
|
2017-08-20 22:02:39 +02:00
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
bool wlr_cursor_warp(struct wlr_cursor *cur, struct wlr_input_device *dev,
|
|
|
|
double x, double y) {
|
2017-08-27 23:35:12 +02:00
|
|
|
assert(cur->state->layout);
|
2017-08-28 02:10:46 +02:00
|
|
|
bool result = false;
|
2017-08-27 23:35:12 +02:00
|
|
|
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_box *mapping = get_mapping(cur, dev);
|
2017-08-28 02:10:46 +02:00
|
|
|
|
|
|
|
if (mapping) {
|
2017-08-29 18:08:49 +02:00
|
|
|
if (wlr_box_contains_point(mapping, x, y)) {
|
2017-08-28 02:10:46 +02:00
|
|
|
wlr_cursor_warp_unchecked(cur, x, y);
|
|
|
|
result = true;
|
|
|
|
}
|
2017-08-29 16:31:39 +02:00
|
|
|
} else if (wlr_output_layout_contains_point(cur->state->layout, NULL,
|
|
|
|
x, y)) {
|
2017-08-28 02:10:46 +02:00
|
|
|
wlr_cursor_warp_unchecked(cur, x, y);
|
|
|
|
result = true;
|
2017-08-20 22:02:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
return result;
|
|
|
|
}
|
2017-08-24 16:11:57 +02:00
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
void wlr_cursor_warp_absolute(struct wlr_cursor *cur,
|
|
|
|
struct wlr_input_device *dev, double x_mm, double y_mm) {
|
|
|
|
assert(cur->state->layout);
|
2017-08-24 15:18:42 +02:00
|
|
|
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_box *mapping = get_mapping(cur, dev);
|
2017-08-28 02:10:46 +02:00
|
|
|
if (!mapping) {
|
2017-08-29 18:08:49 +02:00
|
|
|
mapping = wlr_output_layout_get_box(cur->state->layout, NULL);
|
2017-08-24 16:42:05 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
double x = mapping->width * x_mm + mapping->x;
|
|
|
|
double y = mapping->height * y_mm + mapping->y;
|
2017-08-27 23:35:12 +02:00
|
|
|
|
|
|
|
wlr_cursor_warp_unchecked(cur, x, y);
|
2017-08-24 16:42:05 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 20:35:55 +02:00
|
|
|
void wlr_cursor_move(struct wlr_cursor *cur, struct wlr_input_device *dev,
|
|
|
|
double delta_x, double delta_y) {
|
2017-08-24 16:42:05 +02:00
|
|
|
assert(cur->state->layout);
|
|
|
|
|
2017-08-24 18:30:34 +02:00
|
|
|
double x = cur->x + delta_x;
|
|
|
|
double y = cur->y + delta_y;
|
|
|
|
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_box *mapping = get_mapping(cur, dev);
|
2017-08-26 17:55:24 +02:00
|
|
|
|
2017-08-28 02:10:46 +02:00
|
|
|
if (mapping) {
|
2017-08-29 18:08:49 +02:00
|
|
|
double closest_x, closest_y;
|
|
|
|
if (!wlr_box_contains_point(mapping, x, y)) {
|
|
|
|
wlr_box_closest_point(mapping, x, y, &closest_x,
|
|
|
|
&closest_y);
|
|
|
|
x = closest_x;
|
|
|
|
y = closest_y;
|
2017-08-28 02:10:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!wlr_output_layout_contains_point(cur->state->layout, NULL, x, y)) {
|
2017-08-29 18:32:17 +02:00
|
|
|
double layout_x, layout_y;
|
|
|
|
wlr_output_layout_closest_point(cur->state->layout, NULL, x, y,
|
|
|
|
&layout_x, &layout_y);
|
|
|
|
x = layout_x;
|
|
|
|
y = layout_y;
|
2017-08-26 17:55:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-27 23:35:12 +02:00
|
|
|
wlr_cursor_warp_unchecked(cur, x, y);
|
2017-08-20 22:02:39 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 09:09:21 +01:00
|
|
|
void wlr_cursor_set_image(struct wlr_cursor *cur, const uint8_t *pixels,
|
|
|
|
int32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x,
|
|
|
|
int32_t hotspot_y) {
|
2017-10-29 11:20:11 +01:00
|
|
|
struct wlr_cursor_output_cursor *output_cursor;
|
|
|
|
wl_list_for_each(output_cursor, &cur->state->output_cursors, link) {
|
|
|
|
wlr_output_cursor_set_image(output_cursor->output_cursor, pixels,
|
|
|
|
stride, width, height, hotspot_x, hotspot_y);
|
|
|
|
}
|
2017-10-29 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_cursor_set_surface(struct wlr_cursor *cur, struct wlr_surface *surface,
|
|
|
|
int32_t hotspot_x, int32_t hotspot_y) {
|
2017-10-29 11:20:11 +01:00
|
|
|
struct wlr_cursor_output_cursor *output_cursor;
|
|
|
|
wl_list_for_each(output_cursor, &cur->state->output_cursors, link) {
|
|
|
|
wlr_output_cursor_set_surface(output_cursor->output_cursor, surface,
|
|
|
|
hotspot_x, hotspot_y);
|
|
|
|
}
|
2017-10-29 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
static void handle_pointer_motion(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_pointer_motion *event = data;
|
2017-08-29 16:31:39 +02:00
|
|
|
struct wlr_cursor_device *device =
|
|
|
|
wl_container_of(listener, device, motion);
|
2017-08-20 22:02:39 +02:00
|
|
|
wl_signal_emit(&device->cursor->events.motion, event);
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:31:39 +02:00
|
|
|
static void handle_pointer_motion_absolute(struct wl_listener *listener,
|
|
|
|
void *data) {
|
2017-08-20 22:02:39 +02:00
|
|
|
struct wlr_event_pointer_motion_absolute *event = data;
|
2017-08-29 16:31:39 +02:00
|
|
|
struct wlr_cursor_device *device =
|
|
|
|
wl_container_of(listener, device, motion_absolute);
|
2017-08-20 22:02:39 +02:00
|
|
|
wl_signal_emit(&device->cursor->events.motion_absolute, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_pointer_button(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_pointer_button *event = data;
|
2017-08-29 16:31:39 +02:00
|
|
|
struct wlr_cursor_device *device =
|
|
|
|
wl_container_of(listener, device, button);
|
2017-08-20 22:02:39 +02:00
|
|
|
wl_signal_emit(&device->cursor->events.button, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_pointer_axis(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_pointer_axis *event = data;
|
|
|
|
struct wlr_cursor_device *device = wl_container_of(listener, device, axis);
|
|
|
|
wl_signal_emit(&device->cursor->events.axis, event);
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:34:25 +02:00
|
|
|
static void handle_touch_up(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_touch_up *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, touch_up);
|
|
|
|
wl_signal_emit(&device->cursor->events.touch_up, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_touch_down(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_touch_down *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, touch_down);
|
|
|
|
wl_signal_emit(&device->cursor->events.touch_down, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_touch_motion(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_touch_motion *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, touch_motion);
|
|
|
|
wl_signal_emit(&device->cursor->events.touch_motion, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_touch_cancel(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_touch_cancel *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, touch_cancel);
|
|
|
|
wl_signal_emit(&device->cursor->events.touch_cancel, event);
|
|
|
|
}
|
|
|
|
|
2017-08-28 14:42:39 +02:00
|
|
|
static void handle_tablet_tool_tip(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_tablet_tool_tip *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, tablet_tool_tip);
|
|
|
|
wl_signal_emit(&device->cursor->events.tablet_tool_tip, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_tablet_tool_axis(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_event_tablet_tool_axis *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, tablet_tool_axis);
|
|
|
|
wl_signal_emit(&device->cursor->events.tablet_tool_axis, event);
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:31:39 +02:00
|
|
|
static void handle_tablet_tool_button(struct wl_listener *listener,
|
|
|
|
void *data) {
|
2017-08-28 14:42:39 +02:00
|
|
|
struct wlr_event_tablet_tool_button *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, tablet_tool_button);
|
|
|
|
wl_signal_emit(&device->cursor->events.tablet_tool_button, event);
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:31:39 +02:00
|
|
|
static void handle_tablet_tool_proximity(struct wl_listener *listener,
|
|
|
|
void *data) {
|
2017-08-28 14:42:39 +02:00
|
|
|
struct wlr_event_tablet_tool_proximity *event = data;
|
|
|
|
struct wlr_cursor_device *device;
|
|
|
|
device = wl_container_of(listener, device, tablet_tool_proximity);
|
|
|
|
wl_signal_emit(&device->cursor->events.tablet_tool_proximity, event);
|
|
|
|
}
|
|
|
|
|
2017-08-28 16:29:53 +02:00
|
|
|
static void handle_device_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_cursor_device *c_device;
|
|
|
|
c_device = wl_container_of(listener, c_device, destroy);
|
|
|
|
wlr_cursor_detach_input_device(c_device->cursor, c_device->device);
|
|
|
|
}
|
|
|
|
|
2017-09-05 23:45:05 +02:00
|
|
|
static struct wlr_cursor_device *wlr_cursor_device_create(
|
|
|
|
struct wlr_cursor *cursor, struct wlr_input_device *device) {
|
|
|
|
struct wlr_cursor_device *c_device =
|
|
|
|
calloc(1, sizeof(struct wlr_cursor_device));
|
|
|
|
if (!c_device) {
|
|
|
|
wlr_log(L_ERROR, "Failed to allocate wlr_cursor_device");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c_device->cursor = cursor;
|
|
|
|
c_device->device = device;
|
|
|
|
|
|
|
|
// listen to events
|
|
|
|
wl_signal_add(&device->events.destroy, &c_device->destroy);
|
|
|
|
c_device->destroy.notify = handle_device_destroy;
|
|
|
|
|
|
|
|
if (device->type == WLR_INPUT_DEVICE_POINTER) {
|
|
|
|
wl_signal_add(&device->pointer->events.motion, &c_device->motion);
|
|
|
|
c_device->motion.notify = handle_pointer_motion;
|
|
|
|
|
|
|
|
wl_signal_add(&device->pointer->events.motion_absolute,
|
|
|
|
&c_device->motion_absolute);
|
|
|
|
c_device->motion_absolute.notify = handle_pointer_motion_absolute;
|
|
|
|
|
|
|
|
wl_signal_add(&device->pointer->events.button, &c_device->button);
|
|
|
|
c_device->button.notify = handle_pointer_button;
|
|
|
|
|
|
|
|
wl_signal_add(&device->pointer->events.axis, &c_device->axis);
|
|
|
|
c_device->axis.notify = handle_pointer_axis;
|
|
|
|
} else if (device->type == WLR_INPUT_DEVICE_TOUCH) {
|
|
|
|
wl_signal_add(&device->touch->events.motion, &c_device->touch_motion);
|
|
|
|
c_device->touch_motion.notify = handle_touch_motion;
|
|
|
|
|
|
|
|
wl_signal_add(&device->touch->events.down, &c_device->touch_down);
|
|
|
|
c_device->touch_down.notify = handle_touch_down;
|
|
|
|
|
|
|
|
wl_signal_add(&device->touch->events.up, &c_device->touch_up);
|
|
|
|
c_device->touch_up.notify = handle_touch_up;
|
|
|
|
|
|
|
|
wl_signal_add(&device->touch->events.cancel, &c_device->touch_cancel);
|
|
|
|
c_device->touch_cancel.notify = handle_touch_cancel;
|
|
|
|
} else if (device->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
|
|
|
|
wl_signal_add(&device->tablet_tool->events.tip,
|
|
|
|
&c_device->tablet_tool_tip);
|
|
|
|
c_device->tablet_tool_tip.notify = handle_tablet_tool_tip;
|
|
|
|
|
|
|
|
wl_signal_add(&device->tablet_tool->events.proximity,
|
|
|
|
&c_device->tablet_tool_proximity);
|
|
|
|
c_device->tablet_tool_proximity.notify = handle_tablet_tool_proximity;
|
|
|
|
|
|
|
|
wl_signal_add(&device->tablet_tool->events.axis,
|
|
|
|
&c_device->tablet_tool_axis);
|
|
|
|
c_device->tablet_tool_axis.notify = handle_tablet_tool_axis;
|
|
|
|
|
|
|
|
wl_signal_add(&device->tablet_tool->events.button,
|
|
|
|
&c_device->tablet_tool_button);
|
|
|
|
c_device->tablet_tool_button.notify = handle_tablet_tool_button;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_insert(&cursor->state->devices, &c_device->link);
|
|
|
|
|
|
|
|
return c_device;
|
|
|
|
}
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
void wlr_cursor_attach_input_device(struct wlr_cursor *cur,
|
|
|
|
struct wlr_input_device *dev) {
|
|
|
|
if (dev->type != WLR_INPUT_DEVICE_POINTER &&
|
|
|
|
dev->type != WLR_INPUT_DEVICE_TOUCH &&
|
|
|
|
dev->type != WLR_INPUT_DEVICE_TABLET_TOOL) {
|
2017-08-27 17:34:25 +02:00
|
|
|
wlr_log(L_ERROR, "only device types of pointer, touch or tablet tool"
|
|
|
|
"are supported");
|
2017-08-20 22:02:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure it is not already attached
|
|
|
|
struct wlr_cursor_device *_dev;
|
|
|
|
wl_list_for_each(_dev, &cur->state->devices, link) {
|
|
|
|
if (_dev->device == dev) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 23:45:05 +02:00
|
|
|
wlr_cursor_device_create(cur, dev);
|
|
|
|
}
|
2017-08-28 16:29:53 +02:00
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
void wlr_cursor_detach_input_device(struct wlr_cursor *cur,
|
|
|
|
struct wlr_input_device *dev) {
|
2017-09-05 23:45:05 +02:00
|
|
|
struct wlr_cursor_device *c_device, *tmp = NULL;
|
|
|
|
wl_list_for_each_safe(c_device, tmp, &cur->state->devices, link) {
|
|
|
|
if (c_device->device == dev) {
|
|
|
|
wlr_cursor_device_destroy(c_device);
|
2017-08-20 22:02:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 13:48:28 +02:00
|
|
|
static void handle_layout_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_cursor_state *state =
|
|
|
|
wl_container_of(listener, state, layout_change);
|
|
|
|
wlr_cursor_detach_output_layout(state->cursor);
|
|
|
|
}
|
|
|
|
|
2017-10-29 11:20:11 +01:00
|
|
|
static void handle_layout_output_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct wlr_cursor_output_cursor *output_cursor =
|
|
|
|
wl_container_of(listener, output_cursor, layout_output_destroy);
|
|
|
|
//struct wlr_output_layout_output *l_output = data;
|
2017-10-31 14:58:58 +01:00
|
|
|
output_cursor_destroy(output_cursor);
|
2017-10-29 11:20:11 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 20:03:56 +01:00
|
|
|
static void layout_add(struct wlr_cursor_state *state,
|
|
|
|
struct wlr_output_layout_output *l_output) {
|
2017-10-29 11:20:11 +01:00
|
|
|
struct wlr_cursor_output_cursor *output_cursor =
|
|
|
|
calloc(1, sizeof(struct wlr_cursor_output_cursor));
|
|
|
|
if (output_cursor == NULL) {
|
|
|
|
wlr_log(L_ERROR, "Failed to allocate wlr_cursor_output_cursor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
output_cursor->cursor = state->cursor;
|
|
|
|
|
|
|
|
output_cursor->output_cursor = wlr_output_cursor_create(l_output->output);
|
|
|
|
if (output_cursor->output_cursor == NULL) {
|
|
|
|
wlr_log(L_ERROR, "Failed to create wlr_output_cursor");
|
|
|
|
free(output_cursor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
output_cursor->layout_output_destroy.notify = handle_layout_output_destroy;
|
|
|
|
wl_signal_add(&l_output->events.destroy,
|
|
|
|
&output_cursor->layout_output_destroy);
|
|
|
|
|
2017-10-31 14:58:58 +01:00
|
|
|
wl_list_insert(&state->output_cursors, &output_cursor->link);
|
2017-10-29 11:20:11 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 20:03:56 +01:00
|
|
|
static void handle_layout_add(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_cursor_state *state =
|
|
|
|
wl_container_of(listener, state, layout_add);
|
|
|
|
struct wlr_output_layout_output *l_output = data;
|
|
|
|
layout_add(state, l_output);
|
|
|
|
}
|
|
|
|
|
2017-09-05 13:48:28 +02:00
|
|
|
static void handle_layout_change(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_cursor_state *state =
|
|
|
|
wl_container_of(listener, state, layout_change);
|
|
|
|
struct wlr_output_layout *layout = data;
|
2017-10-29 11:20:11 +01:00
|
|
|
|
2017-09-05 13:48:28 +02:00
|
|
|
if (!wlr_output_layout_contains_point(layout, NULL, state->cursor->x,
|
|
|
|
state->cursor->y)) {
|
|
|
|
// the output we were on has gone away so go to the closest boundary
|
|
|
|
// point
|
|
|
|
double x, y;
|
|
|
|
wlr_output_layout_closest_point(layout, NULL, state->cursor->x,
|
|
|
|
state->cursor->y, &x, &y);
|
|
|
|
|
|
|
|
wlr_cursor_warp_unchecked(state->cursor, x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
void wlr_cursor_attach_output_layout(struct wlr_cursor *cur,
|
|
|
|
struct wlr_output_layout *l) {
|
2017-09-05 13:48:28 +02:00
|
|
|
wlr_cursor_detach_output_layout(cur);
|
|
|
|
|
|
|
|
if (l == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-29 11:20:11 +01:00
|
|
|
wl_signal_add(&l->events.add, &cur->state->layout_add);
|
|
|
|
cur->state->layout_add.notify = handle_layout_add;
|
2017-09-05 13:48:28 +02:00
|
|
|
wl_signal_add(&l->events.change, &cur->state->layout_change);
|
|
|
|
cur->state->layout_change.notify = handle_layout_change;
|
|
|
|
wl_signal_add(&l->events.destroy, &cur->state->layout_destroy);
|
|
|
|
cur->state->layout_destroy.notify = handle_layout_destroy;
|
|
|
|
|
2017-08-20 22:02:39 +02:00
|
|
|
cur->state->layout = l;
|
2017-10-29 20:03:56 +01:00
|
|
|
|
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &l->outputs, link) {
|
|
|
|
layout_add(cur->state, l_output);
|
|
|
|
}
|
2017-08-20 22:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_cursor_map_to_output(struct wlr_cursor *cur,
|
|
|
|
struct wlr_output *output) {
|
2017-08-24 18:30:34 +02:00
|
|
|
cur->state->mapped_output = output;
|
2017-08-20 22:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_cursor_map_input_to_output(struct wlr_cursor *cur,
|
|
|
|
struct wlr_input_device *dev, struct wlr_output *output) {
|
2017-08-24 20:35:55 +02:00
|
|
|
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
|
|
|
|
if (!c_device) {
|
2017-08-29 16:31:39 +02:00
|
|
|
wlr_log(L_ERROR, "Cannot map device \"%s\" to output"
|
|
|
|
"(not found in this cursor)", dev->name);
|
2017-08-24 20:35:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c_device->mapped_output = output;
|
2017-08-20 22:02:39 +02:00
|
|
|
}
|
2017-08-25 19:26:13 +02:00
|
|
|
|
2017-08-29 16:31:39 +02:00
|
|
|
void wlr_cursor_map_to_region(struct wlr_cursor *cur,
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_box *box) {
|
|
|
|
if (box && wlr_box_empty(box)) {
|
2017-08-28 02:10:46 +02:00
|
|
|
wlr_log(L_ERROR, "cannot map cursor to an empty region");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-29 18:08:49 +02:00
|
|
|
cur->state->mapped_box = box;
|
2017-08-25 19:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_cursor_map_input_to_region(struct wlr_cursor *cur,
|
2017-08-29 18:08:49 +02:00
|
|
|
struct wlr_input_device *dev, struct wlr_box *box) {
|
|
|
|
if (box && wlr_box_empty(box)) {
|
2017-08-28 02:10:46 +02:00
|
|
|
wlr_log(L_ERROR, "cannot map device \"%s\" input to an empty region",
|
|
|
|
dev->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:26:13 +02:00
|
|
|
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
|
|
|
|
if (!c_device) {
|
2017-08-28 02:10:46 +02:00
|
|
|
wlr_log(L_ERROR, "Cannot map device \"%s\" to geometry (not found in"
|
|
|
|
"this cursor)", dev->name);
|
2017-08-25 19:26:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-08-28 02:10:46 +02:00
|
|
|
|
2017-08-29 18:08:49 +02:00
|
|
|
c_device->mapped_box = box;
|
2017-08-25 19:26:13 +02:00
|
|
|
}
|