summaryrefslogtreecommitdiff
path: root/src/evdev-wheel.c
blob: acdbf8617386a86da647bc427bbe90535df57b5f (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright © 2010 Intel Corporation
 * Copyright © 2013 Jonas Ådahl
 * Copyright © 2013-2017 Red Hat, Inc.
 * Copyright © 2017 James Ye <jye836@gmail.com>
 * Copyright © 2021 José Expósito
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "config.h"

#include "evdev-fallback.h"
#include "util-input-event.h"

enum wheel_event {
	WHEEL_EVENT_PRESS,
	WHEEL_EVENT_RELEASE,
	WHEEL_EVENT_SCROLL,
};

static inline const char *
wheel_state_to_str(enum wheel_state state)
{
	switch(state) {
	CASE_RETURN_STRING(WHEEL_STATE_NONE);
	CASE_RETURN_STRING(WHEEL_STATE_PRESSED);
	CASE_RETURN_STRING(WHEEL_STATE_SCROLLING);
	}
	return NULL;
}

static inline const char*
wheel_event_to_str(enum wheel_event event)
{
	switch(event) {
	CASE_RETURN_STRING(WHEEL_EVENT_PRESS);
	CASE_RETURN_STRING(WHEEL_EVENT_RELEASE);
	CASE_RETURN_STRING(WHEEL_EVENT_SCROLL);
	}
	return NULL;
}

static inline void
log_wheel_bug(struct fallback_dispatch *dispatch, enum wheel_event event)
{
	evdev_log_bug_libinput(dispatch->device,
			       "invalid wheel event %s in state %s\n",
			       wheel_event_to_str(event),
			       wheel_state_to_str(dispatch->wheel.state));
}

static void
wheel_handle_event_on_state_none(struct fallback_dispatch *dispatch,
				 enum wheel_event event,
				 uint64_t time)
{
	switch (event) {
	case WHEEL_EVENT_PRESS:
		dispatch->wheel.state = WHEEL_STATE_PRESSED;
		break;
	case WHEEL_EVENT_SCROLL:
		dispatch->wheel.state = WHEEL_STATE_SCROLLING;
		break;
	case WHEEL_EVENT_RELEASE:
		log_wheel_bug(dispatch, event);
		break;
	}
}

static void
wheel_handle_event_on_state_pressed(struct fallback_dispatch *dispatch,
				    enum wheel_event event,
				    uint64_t time)
{
	switch (event) {
	case WHEEL_EVENT_RELEASE:
		dispatch->wheel.state = WHEEL_STATE_NONE;
		break;
	case WHEEL_EVENT_SCROLL:
		/* Ignore scroll while the wheel is pressed */
		break;
	case WHEEL_EVENT_PRESS:
		log_wheel_bug(dispatch, event);
		break;
	}
}

static void
wheel_handle_event_on_state_scrolling(struct fallback_dispatch *dispatch,
				      enum wheel_event event,
				      uint64_t time)
{
	switch (event) {
	case WHEEL_EVENT_PRESS:
		dispatch->wheel.state = WHEEL_STATE_PRESSED;
		break;
	case WHEEL_EVENT_SCROLL:
		break;
	case WHEEL_EVENT_RELEASE:
		log_wheel_bug(dispatch, event);
		break;
	}
}

static void
wheel_handle_event(struct fallback_dispatch *dispatch,
		   enum wheel_event event,
		   uint64_t time)
{
	enum wheel_state oldstate = dispatch->wheel.state;

	switch (oldstate) {
	case WHEEL_STATE_NONE:
		wheel_handle_event_on_state_none(dispatch, event, time);
		break;
	case WHEEL_STATE_PRESSED:
		wheel_handle_event_on_state_pressed(dispatch, event, time);
		break;
	case WHEEL_STATE_SCROLLING:
		wheel_handle_event_on_state_scrolling(dispatch, event, time);
		break;
	}

	if (oldstate != dispatch->wheel.state) {
		evdev_log_debug(dispatch->device,
				"wheel state %s → %s → %s\n",
				wheel_state_to_str(oldstate),
				wheel_event_to_str(event),
				wheel_state_to_str(dispatch->wheel.state));
	}
}

static void
wheel_flush_scroll(struct fallback_dispatch *dispatch,
		   struct evdev_device *device,
		   uint64_t time)
{
	struct normalized_coords wheel_degrees = { 0.0, 0.0 };
	struct discrete_coords discrete = { 0.0, 0.0 };
	struct wheel_v120 v120 = { 0.0, 0.0 };

	if (device->model_flags & EVDEV_MODEL_LENOVO_SCROLLPOINT) {
		struct normalized_coords unaccel = { 0.0, 0.0 };

		dispatch->wheel.hi_res.y *= -1;
		fallback_normalize_delta(device, &dispatch->wheel.hi_res, &unaccel);
		evdev_post_scroll(device,
				  time,
				  LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
				  &unaccel);
		dispatch->wheel.hi_res.x = 0;
		dispatch->wheel.hi_res.y = 0;

		return;
	}

	if (dispatch->wheel.hi_res.y != 0) {
		int value = dispatch->wheel.hi_res.y;

		v120.y = -1 * value;
		wheel_degrees.y = -1 * value/120.0 * device->scroll.wheel_click_angle.y;
		evdev_notify_axis_wheel(
			device,
			time,
			bit(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
			&wheel_degrees,
			&v120);
		dispatch->wheel.hi_res.y = 0;
	}

	if (dispatch->wheel.lo_res.y != 0) {
		int value = dispatch->wheel.lo_res.y;

		wheel_degrees.y = -1 * value * device->scroll.wheel_click_angle.y;
		discrete.y = -1 * value;
		evdev_notify_axis_legacy_wheel(
			device,
			time,
			bit(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
			&wheel_degrees,
			&discrete);
		dispatch->wheel.lo_res.y = 0;
	}

	if (dispatch->wheel.hi_res.x != 0) {
		int value = dispatch->wheel.hi_res.x;

		v120.x = value;
		wheel_degrees.x = value/120.0 * device->scroll.wheel_click_angle.x;
		evdev_notify_axis_wheel(
			device,
			time,
			bit(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
			&wheel_degrees,
			&v120);
		dispatch->wheel.hi_res.x = 0;
	}

	if (dispatch->wheel.lo_res.x != 0) {
		int value = dispatch->wheel.lo_res.x;

		wheel_degrees.x = value * device->scroll.wheel_click_angle.x;
		discrete.x = value;
		evdev_notify_axis_legacy_wheel(
			device,
			time,
			bit(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
			&wheel_degrees,
			&discrete);
		dispatch->wheel.lo_res.x = 0;
	}
}

static void
wheel_handle_state_none(struct fallback_dispatch *dispatch,
			struct evdev_device *device,
			uint64_t time)
{

}

static void
wheel_handle_state_pressed(struct fallback_dispatch *dispatch,
			   struct evdev_device *device,
			   uint64_t time)
{
	dispatch->wheel.hi_res.x = 0;
	dispatch->wheel.hi_res.y = 0;
	dispatch->wheel.lo_res.x = 0;
	dispatch->wheel.lo_res.y = 0;
}

static void
wheel_handle_state_scrolling(struct fallback_dispatch *dispatch,
			     struct evdev_device *device,
			     uint64_t time)
{
	wheel_flush_scroll(dispatch, device, time);
}

void
fallback_wheel_process_relative(struct fallback_dispatch *dispatch,
				struct evdev_device *device,
				struct input_event *e, uint64_t time)
{
	switch (e->code) {
	case REL_WHEEL:
		dispatch->wheel.lo_res.y += e->value;
		if (dispatch->wheel.emulate_hi_res_wheel)
			dispatch->wheel.hi_res.y += e->value * 120;
		dispatch->pending_event |= EVDEV_WHEEL;
		wheel_handle_event(dispatch, WHEEL_EVENT_SCROLL, time);
		break;
	case REL_HWHEEL:
		dispatch->wheel.lo_res.x += e->value;
		if (dispatch->wheel.emulate_hi_res_wheel)
			dispatch->wheel.hi_res.x += e->value * 120;
		dispatch->pending_event |= EVDEV_WHEEL;
		wheel_handle_event(dispatch, WHEEL_EVENT_SCROLL, time);
		break;
	case REL_WHEEL_HI_RES:
		dispatch->wheel.hi_res.y += e->value;
		dispatch->wheel.hi_res_event_received = true;
		dispatch->pending_event |= EVDEV_WHEEL;
		wheel_handle_event(dispatch, WHEEL_EVENT_SCROLL, time);
		break;
	case REL_HWHEEL_HI_RES:
		dispatch->wheel.hi_res.x += e->value;
		dispatch->wheel.hi_res_event_received = true;
		dispatch->pending_event |= EVDEV_WHEEL;
		wheel_handle_event(dispatch, WHEEL_EVENT_SCROLL, time);
		break;
	}
}

void
fallback_wheel_notify_physical_button(struct fallback_dispatch *dispatch,
				      struct evdev_device *device,
				      uint64_t time,
				      int button,
				      enum libinput_button_state state)
{
	/* Lenovo TrackPoint Keyboard II sends its own scroll events when its
	 * trackpoint is moved while the middle button is pressed.
	 * Do not inhibit the scroll events.
	 * https://gitlab.freedesktop.org/libinput/libinput/-/issues/651
	 */
	if (evdev_device_has_model_quirk(device,
					 QUIRK_MODEL_LENOVO_TRACKPOINT_KEYBOARD_2))
		return;

	if (button == BTN_MIDDLE) {
		if (state == LIBINPUT_BUTTON_STATE_PRESSED)
			wheel_handle_event(dispatch, WHEEL_EVENT_PRESS, time);
		else
			wheel_handle_event(dispatch, WHEEL_EVENT_RELEASE, time);
	}
}

void
fallback_wheel_handle_state(struct fallback_dispatch *dispatch,
			    struct evdev_device *device,
			    uint64_t time)
{
	if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
		return;

	if (!dispatch->wheel.emulate_hi_res_wheel &&
	    !dispatch->wheel.hi_res_event_received &&
	    (dispatch->wheel.lo_res.x != 0 || dispatch->wheel.lo_res.y != 0)) {
		evdev_log_bug_kernel(device,
				     "device supports high-resolution scroll but only low-resolution events have been received.\n"
				     "See %s/incorrectly-enabled-hires.html for details\n",
				     HTTP_DOC_LINK);
		dispatch->wheel.emulate_hi_res_wheel = true;
		dispatch->wheel.hi_res.x = dispatch->wheel.lo_res.x * 120;
		dispatch->wheel.hi_res.y = dispatch->wheel.lo_res.y * 120;
	}

	switch (dispatch->wheel.state) {
	case WHEEL_STATE_NONE:
		wheel_handle_state_none(dispatch, device, time);
		break;
	case WHEEL_STATE_PRESSED:
		wheel_handle_state_pressed(dispatch, device, time);
		break;
	case WHEEL_STATE_SCROLLING:
		wheel_handle_state_scrolling(dispatch, device, time);
		break;
	}
}

void
fallback_init_wheel(struct fallback_dispatch *dispatch,
		    struct evdev_device *device)
{
	dispatch->wheel.state = WHEEL_STATE_NONE;

	/* On kernel < 5.0 we need to emulate high-resolution
	   wheel scroll events */
	if ((libevdev_has_event_code(device->evdev,
				     EV_REL,
				     REL_WHEEL) &&
	     !libevdev_has_event_code(device->evdev,
				      EV_REL,
				      REL_WHEEL_HI_RES)) ||
	    (libevdev_has_event_code(device->evdev,
				     EV_REL,
				     REL_HWHEEL) &&
	     !libevdev_has_event_code(device->evdev,
				      EV_REL,
				      REL_HWHEEL_HI_RES)))
		dispatch->wheel.emulate_hi_res_wheel = true;
}