aboutsummaryrefslogtreecommitdiff
path: root/backend/libinput/pointer.c
blob: 711474fe21b92cf0d5443c4bbc99c8ac010326f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <assert.h>
#include <libinput.h>
#include <wlr/interfaces/wlr_pointer.h>
#include "backend/libinput.h"

const struct wlr_pointer_impl libinput_pointer_impl = {
	.name = "libinput-pointer",
};

void init_device_pointer(struct wlr_libinput_input_device *dev) {
	const char *name = libinput_device_get_name(dev->handle);
	struct wlr_pointer *wlr_pointer = &dev->pointer;
	wlr_pointer_init(wlr_pointer, &libinput_pointer_impl, name);
	wlr_pointer->base.vendor = libinput_device_get_id_vendor(dev->handle);
	wlr_pointer->base.product = libinput_device_get_id_product(dev->handle);
}

struct wlr_libinput_input_device *device_from_pointer(
		struct wlr_pointer *wlr_pointer) {
	assert(wlr_pointer->impl == &libinput_pointer_impl);

	struct wlr_libinput_input_device *dev =
		wl_container_of(wlr_pointer, dev, pointer);
	return dev;
}

void handle_pointer_motion(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_pointer *pevent =
		libinput_event_get_pointer_event(event);
	struct wlr_pointer_motion_event wlr_event = {
		.pointer = pointer,
		.time_msec = usec_to_msec(libinput_event_pointer_get_time_usec(pevent)),
		.delta_x = libinput_event_pointer_get_dx(pevent),
		.delta_y = libinput_event_pointer_get_dy(pevent),
		.unaccel_dx = libinput_event_pointer_get_dx_unaccelerated(pevent),
		.unaccel_dy = libinput_event_pointer_get_dy_unaccelerated(pevent),
	};
	wl_signal_emit_mutable(&pointer->events.motion, &wlr_event);
	wl_signal_emit_mutable(&pointer->events.frame, pointer);
}

void handle_pointer_motion_abs(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_pointer *pevent =
		libinput_event_get_pointer_event(event);
	struct wlr_pointer_motion_absolute_event wlr_event = {
		.pointer = pointer,
		.time_msec = usec_to_msec(libinput_event_pointer_get_time_usec(pevent)),
		.x = libinput_event_pointer_get_absolute_x_transformed(pevent, 1),
		.y = libinput_event_pointer_get_absolute_y_transformed(pevent, 1),
	};
	wl_signal_emit_mutable(&pointer->events.motion_absolute, &wlr_event);
	wl_signal_emit_mutable(&pointer->events.frame, pointer);
}

void handle_pointer_button(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_pointer *pevent =
		libinput_event_get_pointer_event(event);
	struct wlr_pointer_button_event wlr_event = {
		.pointer = pointer,
		.time_msec = usec_to_msec(libinput_event_pointer_get_time_usec(pevent)),
		.button = libinput_event_pointer_get_button(pevent),
	};
	// Ignore events which aren't a seat-wide state change. For instance, if
	// the same button is pressed twice on the same seat, ignore the second
	// press.
	uint32_t seat_count = libinput_event_pointer_get_seat_button_count(pevent);
	switch (libinput_event_pointer_get_button_state(pevent)) {
	case LIBINPUT_BUTTON_STATE_PRESSED:
		wlr_event.state = WLR_BUTTON_PRESSED;
		if (seat_count != 1) {
			return;
		}
		break;
	case LIBINPUT_BUTTON_STATE_RELEASED:
		wlr_event.state = WLR_BUTTON_RELEASED;
		if (seat_count != 0) {
			return;
		}
		break;
	}
	wl_signal_emit_mutable(&pointer->events.button, &wlr_event);
	wl_signal_emit_mutable(&pointer->events.frame, pointer);
}

void handle_pointer_axis(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_pointer *pevent =
		libinput_event_get_pointer_event(event);
	struct wlr_pointer_axis_event wlr_event = {
		.pointer = pointer,
		.time_msec = usec_to_msec(libinput_event_pointer_get_time_usec(pevent)),
	};
	switch (libinput_event_pointer_get_axis_source(pevent)) {
	case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
		wlr_event.source = WLR_AXIS_SOURCE_WHEEL;
		break;
	case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
		wlr_event.source = WLR_AXIS_SOURCE_FINGER;
		break;
	case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
		wlr_event.source = WLR_AXIS_SOURCE_CONTINUOUS;
		break;
	case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL_TILT:
		wlr_event.source = WLR_AXIS_SOURCE_WHEEL_TILT;
		break;
	}
	const enum libinput_pointer_axis axes[] = {
		LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
		LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
	};
	for (size_t i = 0; i < sizeof(axes) / sizeof(axes[0]); ++i) {
		if (!libinput_event_pointer_has_axis(pevent, axes[i])) {
			continue;
		}

		switch (axes[i]) {
		case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL:
			wlr_event.orientation = WLR_AXIS_ORIENTATION_VERTICAL;
			break;
		case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL:
			wlr_event.orientation = WLR_AXIS_ORIENTATION_HORIZONTAL;
			break;
		}
		wlr_event.delta =
			libinput_event_pointer_get_axis_value(pevent, axes[i]);
		wlr_event.delta_discrete =
			libinput_event_pointer_get_axis_value_discrete(pevent, axes[i]);
		wlr_event.delta_discrete *= WLR_POINTER_AXIS_DISCRETE_STEP;
		wl_signal_emit_mutable(&pointer->events.axis, &wlr_event);
	}
	wl_signal_emit_mutable(&pointer->events.frame, pointer);
}

#if HAVE_LIBINPUT_SCROLL_VALUE120
void handle_pointer_axis_value120(struct libinput_event *event,
		struct wlr_pointer *pointer, enum wlr_axis_source source) {
	struct libinput_event_pointer *pevent =
		libinput_event_get_pointer_event(event);
	struct wlr_pointer_axis_event wlr_event = {
		.pointer = pointer,
		.time_msec = usec_to_msec(libinput_event_pointer_get_time_usec(pevent)),
		.source = source,
	};

	const enum libinput_pointer_axis axes[] = {
		LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
		LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
	};
	for (size_t i = 0; i < sizeof(axes) / sizeof(axes[0]); ++i) {
		if (!libinput_event_pointer_has_axis(pevent, axes[i])) {
			continue;
		}
		switch (axes[i]) {
		case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL:
			wlr_event.orientation = WLR_AXIS_ORIENTATION_VERTICAL;
			break;
		case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL:
			wlr_event.orientation = WLR_AXIS_ORIENTATION_HORIZONTAL;
			break;
		}
		wlr_event.delta =
			libinput_event_pointer_get_scroll_value(pevent, axes[i]);
		if (source == WLR_AXIS_SOURCE_WHEEL) {
			wlr_event.delta_discrete =
				libinput_event_pointer_get_scroll_value_v120(pevent, axes[i]);
		}
		wl_signal_emit_mutable(&pointer->events.axis, &wlr_event);
	}
	wl_signal_emit_mutable(&pointer->events.frame, pointer);
}
#endif

void handle_pointer_swipe_begin(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_swipe_begin_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.fingers = libinput_event_gesture_get_finger_count(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.swipe_begin, &wlr_event);
}

void handle_pointer_swipe_update(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_swipe_update_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.fingers = libinput_event_gesture_get_finger_count(gevent),
		.dx = libinput_event_gesture_get_dx(gevent),
		.dy = libinput_event_gesture_get_dy(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.swipe_update, &wlr_event);
}

void handle_pointer_swipe_end(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_swipe_end_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.cancelled = libinput_event_gesture_get_cancelled(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.swipe_end, &wlr_event);
}

void handle_pointer_pinch_begin(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_pinch_begin_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.fingers = libinput_event_gesture_get_finger_count(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.pinch_begin, &wlr_event);
}

void handle_pointer_pinch_update(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_pinch_update_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.fingers = libinput_event_gesture_get_finger_count(gevent),
		.dx = libinput_event_gesture_get_dx(gevent),
		.dy = libinput_event_gesture_get_dy(gevent),
		.scale = libinput_event_gesture_get_scale(gevent),
		.rotation = libinput_event_gesture_get_angle_delta(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.pinch_update, &wlr_event);
}

void handle_pointer_pinch_end(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_pinch_end_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.cancelled = libinput_event_gesture_get_cancelled(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.pinch_end, &wlr_event);
}

void handle_pointer_hold_begin(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_hold_begin_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.fingers = libinput_event_gesture_get_finger_count(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.hold_begin, &wlr_event);
}

void handle_pointer_hold_end(struct libinput_event *event,
		struct wlr_pointer *pointer) {
	struct libinput_event_gesture *gevent =
		libinput_event_get_gesture_event(event);
	struct wlr_pointer_hold_end_event wlr_event = {
		.pointer = pointer,
		.time_msec =
			usec_to_msec(libinput_event_gesture_get_time_usec(gevent)),
		.cancelled = libinput_event_gesture_get_cancelled(gevent),
	};
	wl_signal_emit_mutable(&pointer->events.hold_end, &wlr_event);
}