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