1 #define _POSIX_C_SOURCE 200809L
2 #include <float.h>
3 #include <wlr/types/wlr_cursor.h>
4 #include <wlr/types/wlr_tablet_v2.h>
5 #include "sway/input/cursor.h"
6 #include "sway/input/seat.h"
7 #include "sway/tree/view.h"
8 #include "log.h"
9 
10 struct seatop_down_event {
11 	struct sway_container *con;
12 	double ref_lx, ref_ly;         // cursor's x/y at start of op
13 	double ref_con_lx, ref_con_ly; // container's x/y at start of op
14 };
15 
handle_pointer_axis(struct sway_seat * seat,struct wlr_event_pointer_axis * event)16 static void handle_pointer_axis(struct sway_seat *seat,
17 		struct wlr_event_pointer_axis *event) {
18 	struct sway_input_device *input_device =
19 		event->device ? event->device->data : NULL;
20 	struct input_config *ic =
21 		input_device ? input_device_get_config(input_device) : NULL;
22 	float scroll_factor =
23 		(ic == NULL || ic->scroll_factor == FLT_MIN) ? 1.0f : ic->scroll_factor;
24 
25 	wlr_seat_pointer_notify_axis(seat->wlr_seat, event->time_msec,
26 		event->orientation, scroll_factor * event->delta,
27 		round(scroll_factor * event->delta_discrete), event->source);
28 }
29 
handle_button(struct sway_seat * seat,uint32_t time_msec,struct wlr_input_device * device,uint32_t button,enum wlr_button_state state)30 static void handle_button(struct sway_seat *seat, uint32_t time_msec,
31 		struct wlr_input_device *device, uint32_t button,
32 		enum wlr_button_state state) {
33 	seat_pointer_notify_button(seat, time_msec, button, state);
34 
35 	if (seat->cursor->pressed_button_count == 0) {
36 		seatop_begin_default(seat);
37 	}
38 }
39 
handle_pointer_motion(struct sway_seat * seat,uint32_t time_msec,double dx,double dy)40 static void handle_pointer_motion(struct sway_seat *seat, uint32_t time_msec,
41 		double dx, double dy) {
42 	struct seatop_down_event *e = seat->seatop_data;
43 	struct sway_container *con = e->con;
44 	if (seat_is_input_allowed(seat, con->view->surface)) {
45 		double moved_x = seat->cursor->cursor->x - e->ref_lx;
46 		double moved_y = seat->cursor->cursor->y - e->ref_ly;
47 		double sx = e->ref_con_lx + moved_x;
48 		double sy = e->ref_con_ly + moved_y;
49 		wlr_seat_pointer_notify_motion(seat->wlr_seat, time_msec, sx, sy);
50 	}
51 }
52 
handle_tablet_tool_tip(struct sway_seat * seat,struct sway_tablet_tool * tool,uint32_t time_msec,enum wlr_tablet_tool_tip_state state)53 static void handle_tablet_tool_tip(struct sway_seat *seat,
54 		struct sway_tablet_tool *tool, uint32_t time_msec,
55 		enum wlr_tablet_tool_tip_state state) {
56 	if (state == WLR_TABLET_TOOL_TIP_UP) {
57 		wlr_tablet_v2_tablet_tool_notify_up(tool->tablet_v2_tool);
58 		seatop_begin_default(seat);
59 	}
60 }
61 
handle_tablet_tool_motion(struct sway_seat * seat,struct sway_tablet_tool * tool,uint32_t time_msec,double dx,double dy)62 static void handle_tablet_tool_motion(struct sway_seat *seat,
63 		struct sway_tablet_tool *tool, uint32_t time_msec, double dx, double dy) {
64 	struct seatop_down_event *e = seat->seatop_data;
65 	struct sway_container *con = e->con;
66 	if (seat_is_input_allowed(seat, con->view->surface)) {
67 		double moved_x = seat->cursor->cursor->x - e->ref_lx;
68 		double moved_y = seat->cursor->cursor->y - e->ref_ly;
69 		double sx = e->ref_con_lx + moved_x;
70 		double sy = e->ref_con_ly + moved_y;
71 		wlr_tablet_v2_tablet_tool_notify_motion(tool->tablet_v2_tool, sx, sy);
72 	}
73 }
74 
handle_unref(struct sway_seat * seat,struct sway_container * con)75 static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
76 	struct seatop_down_event *e = seat->seatop_data;
77 	if (e->con == con) {
78 		seatop_begin_default(seat);
79 	}
80 }
81 
82 static const struct sway_seatop_impl seatop_impl = {
83 	.button = handle_button,
84 	.pointer_motion = handle_pointer_motion,
85 	.pointer_axis = handle_pointer_axis,
86 	.tablet_tool_tip = handle_tablet_tool_tip,
87 	.tablet_tool_motion = handle_tablet_tool_motion,
88 	.unref = handle_unref,
89 	.allow_set_cursor = true,
90 };
91 
seatop_begin_down(struct sway_seat * seat,struct sway_container * con,uint32_t time_msec,int sx,int sy)92 void seatop_begin_down(struct sway_seat *seat, struct sway_container *con,
93 		uint32_t time_msec, int sx, int sy) {
94 	seatop_end(seat);
95 
96 	struct seatop_down_event *e =
97 		calloc(1, sizeof(struct seatop_down_event));
98 	if (!e) {
99 		return;
100 	}
101 	e->con = con;
102 	e->ref_lx = seat->cursor->cursor->x;
103 	e->ref_ly = seat->cursor->cursor->y;
104 	e->ref_con_lx = sx;
105 	e->ref_con_ly = sy;
106 
107 	seat->seatop_impl = &seatop_impl;
108 	seat->seatop_data = e;
109 
110 	container_raise_floating(con);
111 }
112