wlroots-hyprland/rootston/cursor.c

352 lines
10 KiB
C
Raw Normal View History

2017-10-10 00:23:43 +02:00
#define _XOPEN_SOURCE 700
2017-10-15 17:06:03 +02:00
#include <stdlib.h>
2017-10-03 21:06:32 +02:00
#include <math.h>
2017-10-10 00:23:43 +02:00
#ifdef __linux__
2017-09-24 00:18:19 +02:00
#include <linux/input-event-codes.h>
2017-10-10 00:23:43 +02:00
#elif __FreeBSD__
#include <dev/evdev/input-event-codes.h>
#endif
2017-11-12 11:10:56 +01:00
#include <wlr/types/wlr_xcursor_manager.h>
2017-09-23 23:48:13 +02:00
#include <wlr/util/log.h>
#include "rootston/xcursor.h"
#include "rootston/cursor.h"
struct roots_cursor *roots_cursor_create(struct roots_seat *seat) {
struct roots_cursor *cursor = calloc(1, sizeof(struct roots_cursor));
if (!cursor) {
return NULL;
}
cursor->cursor = wlr_cursor_create();
if (!cursor->cursor) {
free(cursor);
return NULL;
}
return cursor;
2017-09-29 01:48:55 +02:00
}
void roots_cursor_destroy(struct roots_cursor *cursor) {
// TODO
2017-09-30 10:39:06 +02:00
}
2017-11-18 20:03:31 +01:00
static void roots_cursor_update_position(struct roots_cursor *cursor,
uint32_t time) {
struct roots_desktop *desktop = cursor->seat->input->server->desktop;
struct roots_seat *seat = cursor->seat;
2017-09-23 23:52:21 +02:00
struct roots_view *view;
2017-09-30 18:33:39 +02:00
struct wlr_surface *surface;
double sx, sy;
switch (cursor->mode) {
2017-09-23 23:52:21 +02:00
case ROOTS_CURSOR_PASSTHROUGH:
view = view_at(desktop, cursor->cursor->x, cursor->cursor->y,
&surface, &sx, &sy);
bool set_compositor_cursor = !view && cursor->cursor_client;
if (view) {
struct wl_client *view_client =
wl_resource_get_client(view->wlr_surface->resource);
set_compositor_cursor = view_client != cursor->cursor_client;
}
if (set_compositor_cursor) {
2017-11-12 11:10:56 +01:00
wlr_xcursor_manager_set_cursor_image(cursor->xcursor_manager,
ROOTS_XCURSOR_DEFAULT, cursor->cursor);
cursor->cursor_client = NULL;
}
2017-09-23 23:52:21 +02:00
if (view) {
wlr_seat_pointer_notify_enter(seat->seat, surface, sx, sy);
wlr_seat_pointer_notify_motion(seat->seat, time, sx, sy);
2017-09-23 23:52:21 +02:00
} else {
wlr_seat_pointer_clear_focus(seat->seat);
2017-09-23 23:52:21 +02:00
}
break;
case ROOTS_CURSOR_MOVE:
view = roots_seat_get_focus(seat);
2017-11-19 18:15:11 +01:00
if (view != NULL) {
double dx = cursor->cursor->x - cursor->offs_x;
double dy = cursor->cursor->y - cursor->offs_y;
2017-11-19 18:15:11 +01:00
view_move(view, cursor->view_x + dx,
cursor->view_y + dy);
2017-09-24 00:18:19 +02:00
}
2017-09-23 23:52:21 +02:00
break;
case ROOTS_CURSOR_RESIZE:
view = roots_seat_get_focus(seat);
2017-11-19 18:15:11 +01:00
if (view != NULL) {
double dx = cursor->cursor->x - cursor->offs_x;
double dy = cursor->cursor->y - cursor->offs_y;
double x = view->x;
double y = view->y;
int width = cursor->view_width;
int height = cursor->view_height;
if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_TOP) {
2017-11-20 11:10:43 +01:00
y = cursor->view_y + dy;
height -= dy;
2017-11-20 11:10:43 +01:00
if (height < 1) {
y += height;
}
} else if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_BOTTOM) {
height += dy;
}
if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_LEFT) {
2017-11-20 11:10:43 +01:00
x = cursor->view_x + dx;
width -= dx;
2017-11-20 11:10:43 +01:00
if (width < 1) {
x += width;
}
} else if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_RIGHT) {
width += dx;
}
2017-10-19 18:33:02 +02:00
2017-11-20 11:10:43 +01:00
if (width < 1) {
width = 1;
2017-10-28 11:58:34 +02:00
}
2017-11-20 11:10:43 +01:00
if (height < 1) {
height = 1;
2017-10-28 11:58:34 +02:00
}
view_move_resize(view, x, y, width, height);
2017-09-30 10:39:06 +02:00
}
2017-09-23 23:52:21 +02:00
break;
case ROOTS_CURSOR_ROTATE:
view = roots_seat_get_focus(seat);
2017-11-19 18:15:11 +01:00
if (view != NULL) {
2017-10-03 21:06:32 +02:00
int ox = view->x + view->wlr_surface->current->width/2,
oy = view->y + view->wlr_surface->current->height/2;
int ux = cursor->offs_x - ox,
uy = cursor->offs_y - oy;
int vx = cursor->cursor->x - ox,
vy = cursor->cursor->y - oy;
2017-10-03 21:06:32 +02:00
float angle = atan2(vx*uy - vy*ux, vx*ux + vy*uy);
2017-10-04 22:01:54 +02:00
int steps = 12;
angle = round(angle/M_PI*steps) / (steps/M_PI);
view->rotation = cursor->view_rotation + angle;
2017-10-03 21:06:32 +02:00
}
2017-09-23 23:52:21 +02:00
break;
}
2017-10-03 19:14:36 +02:00
}
static void roots_cursor_press_button(struct roots_cursor *cursor,
struct wlr_input_device *device, uint32_t time, uint32_t button,
2017-11-15 14:34:35 +01:00
uint32_t state, double lx, double ly) {
struct roots_seat *seat = cursor->seat;
struct roots_desktop *desktop = seat->input->server->desktop;
2017-11-15 14:34:35 +01:00
bool is_touch = device->type == WLR_INPUT_DEVICE_TOUCH;
2017-09-30 18:33:39 +02:00
struct wlr_surface *surface;
double sx, sy;
2017-11-15 14:34:35 +01:00
struct roots_view *view = view_at(desktop, lx, ly, &surface, &sx, &sy);
2017-10-03 19:14:36 +02:00
2017-11-18 20:03:31 +01:00
if (state == WLR_BUTTON_PRESSED &&
view &&
roots_seat_has_meta_pressed(seat)) {
2017-11-19 19:21:18 +01:00
roots_seat_set_focus(seat, view);
2017-10-03 19:14:36 +02:00
2017-10-27 20:36:25 +02:00
uint32_t edges;
2017-10-03 19:14:36 +02:00
switch (button) {
case BTN_LEFT:
roots_seat_begin_move(seat, view);
2017-10-03 19:14:36 +02:00
break;
case BTN_RIGHT:
2017-10-27 20:36:25 +02:00
edges = 0;
if (sx < view->wlr_surface->current->width/2) {
edges |= ROOTS_CURSOR_RESIZE_EDGE_LEFT;
} else {
edges |= ROOTS_CURSOR_RESIZE_EDGE_RIGHT;
}
if (sy < view->wlr_surface->current->height/2) {
edges |= ROOTS_CURSOR_RESIZE_EDGE_TOP;
} else {
edges |= ROOTS_CURSOR_RESIZE_EDGE_BOTTOM;
}
roots_seat_begin_resize(seat, view, edges);
2017-10-03 19:14:36 +02:00
break;
2017-10-03 21:06:32 +02:00
case BTN_MIDDLE:
roots_seat_begin_rotate(seat, view);
2017-10-27 20:36:25 +02:00
break;
2017-10-03 19:14:36 +02:00
}
return;
}
2017-11-15 14:34:35 +01:00
uint32_t serial;
if (is_touch) {
serial = wl_display_get_serial(desktop->server->wl_display);
} else {
2017-11-18 20:03:31 +01:00
serial =
wlr_seat_pointer_notify_button(seat->seat, time, button, state);
2017-11-15 14:34:35 +01:00
}
2017-10-03 19:14:36 +02:00
2017-09-23 20:53:15 +02:00
int i;
2017-09-24 00:18:19 +02:00
switch (state) {
2017-09-23 20:53:15 +02:00
case WLR_BUTTON_RELEASED:
seat->cursor->mode = ROOTS_CURSOR_PASSTHROUGH;
2017-11-15 14:34:35 +01:00
if (!is_touch) {
roots_cursor_update_position(cursor, time);
}
2017-09-23 20:53:15 +02:00
break;
case WLR_BUTTON_PRESSED:
i = cursor->input_events_idx;
cursor->input_events[i].serial = serial;
cursor->input_events[i].cursor = cursor->cursor;
cursor->input_events[i].device = device;
cursor->input_events_idx = (i + 1)
% (sizeof(cursor->input_events) / sizeof(cursor->input_events[0]));
2017-11-19 19:21:18 +01:00
roots_seat_set_focus(seat, view);
2017-09-23 20:53:15 +02:00
break;
}
}
void roots_cursor_handle_motion(struct roots_cursor *cursor,
struct wlr_event_pointer_motion *event) {
wlr_cursor_move(cursor->cursor, event->device,
event->delta_x, event->delta_y);
roots_cursor_update_position(cursor, event->time_msec);
}
void roots_cursor_handle_motion_absolute(struct roots_cursor *cursor,
struct wlr_event_pointer_motion_absolute *event) {
wlr_cursor_warp_absolute(cursor->cursor, event->device,
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
roots_cursor_update_position(cursor, event->time_msec);
}
void roots_cursor_handle_button(struct roots_cursor *cursor,
struct wlr_event_pointer_button *event) {
roots_cursor_press_button(cursor, event->device, event->time_msec,
2017-11-15 14:34:35 +01:00
event->button, event->state, cursor->cursor->x, cursor->cursor->y);
2017-09-24 00:18:19 +02:00
}
void roots_cursor_handle_axis(struct roots_cursor *cursor,
struct wlr_event_pointer_axis *event) {
wlr_seat_pointer_notify_axis(cursor->seat->seat, event->time_msec,
event->orientation, event->delta);
}
void roots_cursor_handle_touch_down(struct roots_cursor *cursor,
struct wlr_event_touch_down *event) {
2017-11-12 17:43:50 +01:00
struct roots_desktop *desktop = cursor->seat->input->server->desktop;
struct wlr_surface *surface = NULL;
double lx, ly;
bool result =
wlr_cursor_absolute_to_layout_coords(cursor->cursor,
event->device, event->x_mm, event->y_mm, event->width_mm,
event->height_mm, &lx, &ly);
if (!result) {
return;
}
2017-11-12 17:43:50 +01:00
double sx, sy;
view_at(desktop, lx, ly, &surface, &sx, &sy);
uint32_t serial = 0;
2017-11-12 17:43:50 +01:00
if (surface) {
serial = wlr_seat_touch_notify_down(cursor->seat->seat, surface,
2017-11-16 23:53:52 +01:00
event->time_msec, event->touch_id, sx, sy);
2017-11-12 17:43:50 +01:00
}
2017-11-15 14:34:35 +01:00
if (serial && wlr_seat_touch_num_points(cursor->seat->seat) == 1) {
2017-11-16 23:53:52 +01:00
cursor->seat->touch_id = event->touch_id;
2017-11-15 14:34:35 +01:00
cursor->seat->touch_x = lx;
cursor->seat->touch_y = ly;
roots_cursor_press_button(cursor, event->device, event->time_msec,
BTN_LEFT, 1, lx, ly);
}
}
void roots_cursor_handle_touch_up(struct roots_cursor *cursor,
struct wlr_event_touch_up *event) {
2017-11-18 20:03:31 +01:00
struct wlr_touch_point *point =
wlr_seat_touch_get_point(cursor->seat->seat, event->touch_id);
2017-11-15 14:34:35 +01:00
if (!point) {
return;
}
2017-11-15 14:34:35 +01:00
if (wlr_seat_touch_num_points(cursor->seat->seat) == 1) {
roots_cursor_press_button(cursor, event->device, event->time_msec,
BTN_LEFT, 0, cursor->seat->touch_x, cursor->seat->touch_y);
}
2017-11-18 20:03:31 +01:00
wlr_seat_touch_notify_up(cursor->seat->seat, event->time_msec,
event->touch_id);
}
void roots_cursor_handle_touch_motion(struct roots_cursor *cursor,
struct wlr_event_touch_motion *event) {
2017-11-12 17:43:50 +01:00
struct roots_desktop *desktop = cursor->seat->input->server->desktop;
struct wlr_touch_point *point =
2017-11-16 23:53:52 +01:00
wlr_seat_touch_get_point(cursor->seat->seat, event->touch_id);
2017-11-12 17:43:50 +01:00
if (!point) {
return;
}
struct wlr_surface *surface = NULL;
double lx, ly;
bool result =
wlr_cursor_absolute_to_layout_coords(cursor->cursor,
event->device, event->x_mm, event->y_mm, event->width_mm,
event->height_mm, &lx, &ly);
if (!result) {
return;
}
double sx, sy;
view_at(desktop, lx, ly, &surface, &sx, &sy);
if (surface) {
2017-11-14 14:51:37 +01:00
wlr_seat_touch_point_focus(cursor->seat->seat, surface,
2017-11-16 23:53:52 +01:00
event->time_msec, event->touch_id, sx, sy);
2017-11-14 14:51:37 +01:00
wlr_seat_touch_notify_motion(cursor->seat->seat, event->time_msec,
2017-11-16 23:53:52 +01:00
event->touch_id, sx, sy);
2017-11-14 14:51:37 +01:00
} else {
wlr_seat_touch_point_clear_focus(cursor->seat->seat, event->time_msec,
2017-11-16 23:53:52 +01:00
event->touch_id);
}
2017-11-15 12:43:30 +01:00
2017-11-16 23:53:52 +01:00
if (event->touch_id == cursor->seat->touch_id) {
2017-11-15 14:34:35 +01:00
cursor->seat->touch_x = lx;
cursor->seat->touch_y = ly;
}
}
void roots_cursor_handle_tool_axis(struct roots_cursor *cursor,
struct wlr_event_tablet_tool_axis *event) {
2017-09-23 20:53:15 +02:00
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X) &&
(event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) {
wlr_cursor_warp_absolute(cursor->cursor, event->device,
2017-09-23 20:53:15 +02:00
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
roots_cursor_update_position(cursor, event->time_msec);
} else if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X)) {
wlr_cursor_warp_absolute(cursor->cursor, event->device,
event->x_mm / event->width_mm, -1);
roots_cursor_update_position(cursor, event->time_msec);
} else if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) {
wlr_cursor_warp_absolute(cursor->cursor, event->device,
-1, event->y_mm / event->height_mm);
roots_cursor_update_position(cursor, event->time_msec);
2017-09-23 20:53:15 +02:00
}
}
void roots_cursor_handle_tool_tip(struct roots_cursor *cursor,
struct wlr_event_tablet_tool_tip *event) {
roots_cursor_press_button(cursor, event->device,
2017-11-15 14:34:35 +01:00
event->time_msec, BTN_LEFT, event->state, cursor->cursor->x,
cursor->cursor->y);
2017-09-23 20:53:15 +02:00
}
void roots_cursor_handle_request_set_cursor(struct roots_cursor *cursor,
struct wlr_seat_pointer_request_set_cursor_event *event) {
struct wlr_surface *focused_surface =
event->seat_client->seat->pointer_state.focused_surface;
2017-11-18 20:03:31 +01:00
bool has_focused =
focused_surface != NULL && focused_surface->resource != NULL;
struct wl_client *focused_client = NULL;
if (has_focused) {
focused_client = wl_resource_get_client(focused_surface->resource);
}
if (event->seat_client->client != focused_client ||
cursor->mode != ROOTS_CURSOR_PASSTHROUGH) {
wlr_log(L_DEBUG, "Denying request to set cursor from unfocused client");
return;
}
wlr_log(L_DEBUG, "Setting client cursor");
wlr_cursor_set_surface(cursor->cursor, event->surface, event->hotspot_x,
event->hotspot_y);
cursor->cursor_client = event->seat_client->client;
2017-10-15 17:06:03 +02:00
}