aboutsummaryrefslogtreecommitdiffstats
path: root/sway/input/seatop_down.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/input/seatop_down.c')
-rw-r--r--sway/input/seatop_down.c181
1 files changed, 167 insertions, 14 deletions
diff --git a/sway/input/seatop_down.c b/sway/input/seatop_down.c
index 17f619e3..340e334b 100644
--- a/sway/input/seatop_down.c
+++ b/sway/input/seatop_down.c
@@ -1,22 +1,138 @@
1#define _POSIX_C_SOURCE 200809L
2#include <float.h> 1#include <float.h>
3#include <wlr/types/wlr_cursor.h> 2#include <wlr/types/wlr_cursor.h>
4#include <wlr/types/wlr_tablet_v2.h> 3#include <wlr/types/wlr_tablet_v2.h>
4#include <wlr/types/wlr_touch.h>
5#include "sway/input/cursor.h" 5#include "sway/input/cursor.h"
6#include "sway/input/seat.h" 6#include "sway/input/seat.h"
7#include "sway/tree/view.h" 7#include "sway/tree/view.h"
8#include "sway/desktop/transaction.h"
8#include "log.h" 9#include "log.h"
9 10
11struct seatop_touch_point_event {
12 double ref_lx, ref_ly; // touch's x/y at start of op
13 double ref_con_lx, ref_con_ly; // container's x/y at start of op
14 int32_t touch_id;
15 struct wl_list link;
16};
17
10struct seatop_down_event { 18struct seatop_down_event {
11 struct sway_container *con; 19 struct sway_container *con;
20 struct sway_seat *seat;
21 struct wl_listener surface_destroy;
22 struct wlr_surface *surface;
12 double ref_lx, ref_ly; // cursor's x/y at start of op 23 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 24 double ref_con_lx, ref_con_ly; // container's x/y at start of op
25 struct wl_list point_events; // seatop_touch_point_event::link
14}; 26};
15 27
28static void handle_touch_motion(struct sway_seat *seat,
29 struct wlr_touch_motion_event *event, double lx, double ly) {
30 struct seatop_down_event *e = seat->seatop_data;
31
32 struct seatop_touch_point_event *point_event;
33 bool found = false;
34 wl_list_for_each(point_event, &e->point_events, link) {
35 if (point_event->touch_id == event->touch_id) {
36 found = true;
37 break;
38 }
39 }
40 if (!found) {
41 return; // Probably not a point_event from this seatop_down
42 }
43
44 double moved_x = lx - point_event->ref_lx;
45 double moved_y = ly - point_event->ref_ly;
46 double sx = point_event->ref_con_lx + moved_x;
47 double sy = point_event->ref_con_ly + moved_y;
48
49 wlr_seat_touch_notify_motion(seat->wlr_seat, event->time_msec,
50 event->touch_id, sx, sy);
51}
52
53static void handle_touch_up(struct sway_seat *seat,
54 struct wlr_touch_up_event *event) {
55 struct seatop_down_event *e = seat->seatop_data;
56 struct seatop_touch_point_event *point_event, *tmp;
57
58 wl_list_for_each_safe(point_event, tmp, &e->point_events, link) {
59 if (point_event->touch_id == event->touch_id) {
60 wl_list_remove(&point_event->link);
61 free(point_event);
62 break;
63 }
64 }
65
66 wlr_seat_touch_notify_up(seat->wlr_seat, event->time_msec, event->touch_id);
67
68 if (wl_list_empty(&e->point_events)) {
69 seatop_begin_default(seat);
70 }
71}
72
73static void handle_touch_down(struct sway_seat *seat,
74 struct wlr_touch_down_event *event, double lx, double ly) {
75 struct seatop_down_event *e = seat->seatop_data;
76 double sx, sy;
77 struct wlr_surface *surface = NULL;
78 struct sway_node *focused_node = node_at_coords(seat, seat->touch_x,
79 seat->touch_y, &surface, &sx, &sy);
80
81 if (!surface || surface != e->surface) { // Must start from the initial surface
82 return;
83 }
84
85 struct seatop_touch_point_event *point_event =
86 calloc(1, sizeof(struct seatop_touch_point_event));
87 if (!sway_assert(point_event, "Unable to allocate point_event")) {
88 return;
89 }
90 point_event->touch_id = event->touch_id;
91 point_event->ref_lx = lx;
92 point_event->ref_ly = ly;
93 point_event->ref_con_lx = sx;
94 point_event->ref_con_ly = sy;
95
96 wl_list_insert(&e->point_events, &point_event->link);
97
98 wlr_seat_touch_notify_down(seat->wlr_seat, surface, event->time_msec,
99 event->touch_id, sx, sy);
100
101 if (focused_node) {
102 seat_set_focus(seat, focused_node);
103 }
104}
105
106static void handle_touch_cancel(struct sway_seat *seat,
107 struct wlr_touch_cancel_event *event) {
108 struct seatop_down_event *e = seat->seatop_data;
109 struct seatop_touch_point_event *point_event, *tmp;
110
111 wl_list_for_each_safe(point_event, tmp, &e->point_events, link) {
112 if (point_event->touch_id == event->touch_id) {
113 wl_list_remove(&point_event->link);
114 free(point_event);
115 break;
116 }
117 }
118
119 if (e->surface) {
120 struct wl_client *client = wl_resource_get_client(e->surface->resource);
121 struct wlr_seat_client *seat_client = wlr_seat_client_for_wl_client(seat->wlr_seat, client);
122 if (seat_client != NULL) {
123 wlr_seat_touch_notify_cancel(seat->wlr_seat, seat_client);
124 }
125 }
126
127 if (wl_list_empty(&e->point_events)) {
128 seatop_begin_default(seat);
129 }
130}
131
16static void handle_pointer_axis(struct sway_seat *seat, 132static void handle_pointer_axis(struct sway_seat *seat,
17 struct wlr_event_pointer_axis *event) { 133 struct wlr_pointer_axis_event *event) {
18 struct sway_input_device *input_device = 134 struct sway_input_device *input_device =
19 event->device ? event->device->data : NULL; 135 event->pointer ? event->pointer->base.data : NULL;
20 struct input_config *ic = 136 struct input_config *ic =
21 input_device ? input_device_get_config(input_device) : NULL; 137 input_device ? input_device_get_config(input_device) : NULL;
22 float scroll_factor = 138 float scroll_factor =
@@ -24,12 +140,13 @@ static void handle_pointer_axis(struct sway_seat *seat,
24 140
25 wlr_seat_pointer_notify_axis(seat->wlr_seat, event->time_msec, 141 wlr_seat_pointer_notify_axis(seat->wlr_seat, event->time_msec,
26 event->orientation, scroll_factor * event->delta, 142 event->orientation, scroll_factor * event->delta,
27 round(scroll_factor * event->delta_discrete), event->source); 143 roundf(scroll_factor * event->delta_discrete), event->source,
144 event->relative_direction);
28} 145}
29 146
30static void handle_button(struct sway_seat *seat, uint32_t time_msec, 147static void handle_button(struct sway_seat *seat, uint32_t time_msec,
31 struct wlr_input_device *device, uint32_t button, 148 struct wlr_input_device *device, uint32_t button,
32 enum wlr_button_state state) { 149 enum wl_pointer_button_state state) {
33 seat_pointer_notify_button(seat, time_msec, button, state); 150 seat_pointer_notify_button(seat, time_msec, button, state);
34 151
35 if (seat->cursor->pressed_button_count == 0) { 152 if (seat->cursor->pressed_button_count == 0) {
@@ -39,8 +156,7 @@ static void handle_button(struct sway_seat *seat, uint32_t time_msec,
39 156
40static void handle_pointer_motion(struct sway_seat *seat, uint32_t time_msec) { 157static void handle_pointer_motion(struct sway_seat *seat, uint32_t time_msec) {
41 struct seatop_down_event *e = seat->seatop_data; 158 struct seatop_down_event *e = seat->seatop_data;
42 struct sway_container *con = e->con; 159 if (seat_is_input_allowed(seat, e->surface)) {
43 if (seat_is_input_allowed(seat, con->view->surface)) {
44 double moved_x = seat->cursor->cursor->x - e->ref_lx; 160 double moved_x = seat->cursor->cursor->x - e->ref_lx;
45 double moved_y = seat->cursor->cursor->y - e->ref_ly; 161 double moved_y = seat->cursor->cursor->y - e->ref_ly;
46 double sx = e->ref_con_lx + moved_x; 162 double sx = e->ref_con_lx + moved_x;
@@ -61,8 +177,7 @@ static void handle_tablet_tool_tip(struct sway_seat *seat,
61static void handle_tablet_tool_motion(struct sway_seat *seat, 177static void handle_tablet_tool_motion(struct sway_seat *seat,
62 struct sway_tablet_tool *tool, uint32_t time_msec) { 178 struct sway_tablet_tool *tool, uint32_t time_msec) {
63 struct seatop_down_event *e = seat->seatop_data; 179 struct seatop_down_event *e = seat->seatop_data;
64 struct sway_container *con = e->con; 180 if (seat_is_input_allowed(seat, e->surface)) {
65 if (seat_is_input_allowed(seat, con->view->surface)) {
66 double moved_x = seat->cursor->cursor->x - e->ref_lx; 181 double moved_x = seat->cursor->cursor->x - e->ref_lx;
67 double moved_y = seat->cursor->cursor->y - e->ref_ly; 182 double moved_y = seat->cursor->cursor->y - e->ref_ly;
68 double sx = e->ref_con_lx + moved_x; 183 double sx = e->ref_con_lx + moved_x;
@@ -71,6 +186,14 @@ static void handle_tablet_tool_motion(struct sway_seat *seat,
71 } 186 }
72} 187}
73 188
189static void handle_destroy(struct wl_listener *listener, void *data) {
190 struct seatop_down_event *e =
191 wl_container_of(listener, e, surface_destroy);
192 if (e) {
193 seatop_begin_default(e->seat);
194 }
195}
196
74static void handle_unref(struct sway_seat *seat, struct sway_container *con) { 197static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
75 struct seatop_down_event *e = seat->seatop_data; 198 struct seatop_down_event *e = seat->seatop_data;
76 if (e->con == con) { 199 if (e->con == con) {
@@ -78,33 +201,63 @@ static void handle_unref(struct sway_seat *seat, struct sway_container *con) {
78 } 201 }
79} 202}
80 203
204static void handle_end(struct sway_seat *seat) {
205 struct seatop_down_event *e = seat->seatop_data;
206 wl_list_remove(&e->surface_destroy.link);
207}
208
81static const struct sway_seatop_impl seatop_impl = { 209static const struct sway_seatop_impl seatop_impl = {
82 .button = handle_button, 210 .button = handle_button,
83 .pointer_motion = handle_pointer_motion, 211 .pointer_motion = handle_pointer_motion,
84 .pointer_axis = handle_pointer_axis, 212 .pointer_axis = handle_pointer_axis,
85 .tablet_tool_tip = handle_tablet_tool_tip, 213 .tablet_tool_tip = handle_tablet_tool_tip,
86 .tablet_tool_motion = handle_tablet_tool_motion, 214 .tablet_tool_motion = handle_tablet_tool_motion,
215 .touch_motion = handle_touch_motion,
216 .touch_up = handle_touch_up,
217 .touch_down = handle_touch_down,
218 .touch_cancel = handle_touch_cancel,
87 .unref = handle_unref, 219 .unref = handle_unref,
220 .end = handle_end,
88 .allow_set_cursor = true, 221 .allow_set_cursor = true,
89}; 222};
90 223
91void seatop_begin_down(struct sway_seat *seat, struct sway_container *con, 224void seatop_begin_down(struct sway_seat *seat, struct sway_container *con,
92 uint32_t time_msec, int sx, int sy) { 225 double sx, double sy) {
226 seatop_begin_down_on_surface(seat, con->view->surface, sx, sy);
227 struct seatop_down_event *e = seat->seatop_data;
228 e->con = con;
229
230 container_raise_floating(con);
231 transaction_commit_dirty();
232}
233
234void seatop_begin_touch_down(struct sway_seat *seat,
235 struct wlr_surface *surface, struct wlr_touch_down_event *event,
236 double sx, double sy, double lx, double ly) {
237 seatop_begin_down_on_surface(seat, surface, sx, sy);
238 handle_touch_down(seat, event, lx, ly);
239}
240
241void seatop_begin_down_on_surface(struct sway_seat *seat,
242 struct wlr_surface *surface, double sx, double sy) {
93 seatop_end(seat); 243 seatop_end(seat);
94 244
95 struct seatop_down_event *e = 245 struct seatop_down_event *e =
96 calloc(1, sizeof(struct seatop_down_event)); 246 calloc(1, sizeof(struct seatop_down_event));
97 if (!e) { 247 if (!sway_assert(e, "Unable to allocate e")) {
98 return; 248 return;
99 } 249 }
100 e->con = con; 250 e->con = NULL;
251 e->seat = seat;
252 e->surface = surface;
253 wl_signal_add(&e->surface->events.destroy, &e->surface_destroy);
254 e->surface_destroy.notify = handle_destroy;
101 e->ref_lx = seat->cursor->cursor->x; 255 e->ref_lx = seat->cursor->cursor->x;
102 e->ref_ly = seat->cursor->cursor->y; 256 e->ref_ly = seat->cursor->cursor->y;
103 e->ref_con_lx = sx; 257 e->ref_con_lx = sx;
104 e->ref_con_ly = sy; 258 e->ref_con_ly = sy;
259 wl_list_init(&e->point_events);
105 260
106 seat->seatop_impl = &seatop_impl; 261 seat->seatop_impl = &seatop_impl;
107 seat->seatop_data = e; 262 seat->seatop_data = e;
108
109 container_raise_floating(con);
110} 263}