parent
48fa59c22e
commit
e3d47376dc
@ -0,0 +1,81 @@
|
|||||||
|
#ifndef _WLR_TYPES_CURSOR_H
|
||||||
|
#define _WLR_TYPES_CURSOR_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>
|
||||||
|
#include <wlr/xcursor.h>
|
||||||
|
|
||||||
|
struct wlr_cursor_state;
|
||||||
|
//struct wlr_cursor_impl *;
|
||||||
|
|
||||||
|
struct wlr_cursor {
|
||||||
|
struct wlr_cursor_state *state;
|
||||||
|
//struct wlr_cursor_impl *impl;
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
struct wl_signal motion;
|
||||||
|
struct wl_signal motion_absolute;
|
||||||
|
struct wl_signal button;
|
||||||
|
struct wl_signal axis;
|
||||||
|
} events;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_cursor *wlr_cursor_init();
|
||||||
|
|
||||||
|
void wlr_cursor_destroy(struct wlr_cursor *cur);
|
||||||
|
|
||||||
|
void wlr_cursor_set_xcursor(struct wlr_cursor *cur, struct wlr_xcursor *xcur);
|
||||||
|
|
||||||
|
void wlr_cursor_warp(struct wlr_cursor *cur, double x, double y);
|
||||||
|
|
||||||
|
void wlr_cursor_move(struct wlr_cursor *cur, double delta_x, double delta_y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attaches this input device to this cursor. The input device must be one of:
|
||||||
|
*
|
||||||
|
* - WLR_INPUT_DEVICE_POINTER
|
||||||
|
* - WLR_INPUT_DEVICE_TOUCH
|
||||||
|
* - WLR_INPUT_DEVICE_TABLET_TOOL
|
||||||
|
*/
|
||||||
|
void wlr_cursor_attach_input_device(struct wlr_cursor *cur,
|
||||||
|
struct wlr_input_device *dev);
|
||||||
|
|
||||||
|
void wlr_cursor_detach_input_device(struct wlr_cursor *cur,
|
||||||
|
struct wlr_input_device *dev);
|
||||||
|
/**
|
||||||
|
* Uses the given layout to establish the boundaries and movement semantics of
|
||||||
|
* this cursor. Cursors without an output layout allow infinite movement in any
|
||||||
|
* direction and do not support absolute input events.
|
||||||
|
*/
|
||||||
|
void wlr_cursor_attach_output_layout(struct wlr_cursor *cur,
|
||||||
|
struct wlr_output_layout *l);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attaches this cursor to the given output, which must be among the outputs in
|
||||||
|
* the current output_layout for this cursor. This call is invalid for a cursor
|
||||||
|
* without an associated output layout.
|
||||||
|
*/
|
||||||
|
void wlr_cursor_map_to_output(struct wlr_cursor *cur, struct wlr_output *output);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps all input from a specific input device to a given output. The input
|
||||||
|
* device must be attached to this cursor and the output must be among the
|
||||||
|
* outputs in the attached output layout.
|
||||||
|
*/
|
||||||
|
void wlr_cursor_map_input_to_output(struct wlr_cursor *cur,
|
||||||
|
struct wlr_input_device *dev, struct wlr_output *output);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps this cursor to an arbitrary region on the associated wlr_output_layout.
|
||||||
|
*/
|
||||||
|
//void wlr_cursor_map_to_region(struct wlr_cursor *cur, struct wlr_geometry *geo);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps inputs from this input device to an arbitrary region on the associated
|
||||||
|
* wlr_output_layout.
|
||||||
|
*/
|
||||||
|
//void wlr_cursor_map_input_to_region(struct wlr_cursor *cur, struct wlr_input_device *dev, struct wlr_geometry *geo);
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,206 @@
|
|||||||
|
#include <wlr/types/wlr_cursor.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#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;
|
||||||
|
|
||||||
|
struct wl_listener motion;
|
||||||
|
struct wl_listener motion_absolute;
|
||||||
|
struct wl_listener button;
|
||||||
|
struct wl_listener axis;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_cursor_state {
|
||||||
|
struct wl_list devices;
|
||||||
|
struct wlr_output_layout *layout;
|
||||||
|
struct wlr_xcursor *xcursor;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wlr_cursor *wlr_cursor_init() {
|
||||||
|
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");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_list_init(&cur->state->devices);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
cur->x = 100;
|
||||||
|
cur->y = 100;
|
||||||
|
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_destroy(struct wlr_cursor *cur) {
|
||||||
|
struct wlr_cursor_device *device;
|
||||||
|
wl_list_for_each(device, &cur->state->devices, link) {
|
||||||
|
wl_list_remove(&device->link);
|
||||||
|
free(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_set_xcursor(struct wlr_cursor *cur, struct wlr_xcursor *xcur) {
|
||||||
|
cur->state->xcursor = xcur;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_warp(struct wlr_cursor *cur, double x, double y) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_move(struct wlr_cursor *cur, double delta_x, double delta_y) {
|
||||||
|
//struct wlr_output *current_output;
|
||||||
|
//current_output = wlr_output_layout_output_at(cur->state->layout, cur->x, cur->y);
|
||||||
|
|
||||||
|
// TODO handle no layout
|
||||||
|
assert(cur->state->layout);
|
||||||
|
// TODO handle layout boundaries
|
||||||
|
double new_x = cur->x + delta_x;
|
||||||
|
double new_y = cur->y + delta_y;
|
||||||
|
int hotspot_x = 0;
|
||||||
|
int hotspot_y = 0;
|
||||||
|
|
||||||
|
if (cur->state->xcursor && cur->state->xcursor->image_count > 0) {
|
||||||
|
struct wlr_xcursor_image *image = cur->state->xcursor->images[0];
|
||||||
|
hotspot_x = image->hotspot_x;
|
||||||
|
hotspot_y = image->hotspot_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_output *output;
|
||||||
|
output = wlr_output_layout_output_at(cur->state->layout, new_x, new_y);
|
||||||
|
|
||||||
|
if (output) {
|
||||||
|
int output_x = new_x;
|
||||||
|
int output_y = new_y;
|
||||||
|
|
||||||
|
// TODO fix double to int rounding issues
|
||||||
|
wlr_output_layout_output_coords(cur->state->layout, output, &output_x, &output_y);
|
||||||
|
wlr_output_move_cursor(output, output_x - hotspot_x, output_y - hotspot_y);
|
||||||
|
|
||||||
|
cur->x = new_x;
|
||||||
|
cur->y = new_y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_pointer_motion(struct wl_listener *listener, void *data) {
|
||||||
|
struct wlr_event_pointer_motion *event = data;
|
||||||
|
struct wlr_cursor_device *device = wl_container_of(listener, device, motion);
|
||||||
|
wl_signal_emit(&device->cursor->events.motion, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_pointer_motion_absolute(struct wl_listener *listener, void *data) {
|
||||||
|
struct wlr_event_pointer_motion_absolute *event = data;
|
||||||
|
struct wlr_cursor_device *device = wl_container_of(listener, device, motion_absolute);
|
||||||
|
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;
|
||||||
|
struct wlr_cursor_device *device = wl_container_of(listener, device, button);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
wlr_log(L_ERROR, "only device types of pointer, touch or tablet tool are"
|
||||||
|
"supported");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO support other device types
|
||||||
|
if (dev->type != WLR_INPUT_DEVICE_POINTER) {
|
||||||
|
wlr_log(L_ERROR, "TODO: support touch and tablet tool devices");
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct wlr_cursor_device *device = calloc(1, sizeof(struct wlr_cursor_device));
|
||||||
|
if (!device) {
|
||||||
|
wlr_log(L_ERROR, "Failed to allocate wlr_cursor_device");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
device->cursor = cur;
|
||||||
|
device->device = dev;
|
||||||
|
|
||||||
|
// listen to events
|
||||||
|
wl_signal_add(&dev->pointer->events.motion, &device->motion);
|
||||||
|
device->motion.notify = handle_pointer_motion;
|
||||||
|
|
||||||
|
wl_signal_add(&dev->pointer->events.motion_absolute, &device->motion_absolute);
|
||||||
|
device->motion_absolute.notify = handle_pointer_motion_absolute;
|
||||||
|
|
||||||
|
wl_signal_add(&dev->pointer->events.button, &device->button);
|
||||||
|
device->button.notify = handle_pointer_button;
|
||||||
|
|
||||||
|
wl_signal_add(&dev->pointer->events.axis, &device->axis);
|
||||||
|
device->axis.notify = handle_pointer_axis;
|
||||||
|
|
||||||
|
wl_list_insert(&cur->state->devices, &device->link);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_detach_input_device(struct wlr_cursor *cur,
|
||||||
|
struct wlr_input_device *dev) {
|
||||||
|
struct wlr_cursor_device *target_device = NULL, *_device = NULL;
|
||||||
|
wl_list_for_each(_device, &cur->state->devices, link) {
|
||||||
|
if (_device->device == dev) {
|
||||||
|
target_device = _device;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target_device) {
|
||||||
|
wl_list_remove(&target_device->link);
|
||||||
|
free(target_device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_attach_output_layout(struct wlr_cursor *cur,
|
||||||
|
struct wlr_output_layout *l) {
|
||||||
|
cur->state->layout = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_map_to_output(struct wlr_cursor *cur,
|
||||||
|
struct wlr_output *output) {
|
||||||
|
wlr_log(L_DEBUG, "TODO: map to output");
|
||||||
|
}
|
||||||
|
|
||||||
|
void wlr_cursor_map_input_to_output(struct wlr_cursor *cur,
|
||||||
|
struct wlr_input_device *dev, struct wlr_output *output) {
|
||||||
|
wlr_log(L_DEBUG, "TODO map input to output");
|
||||||
|
}
|
Loading…
Reference in new issue