40
40
41
41
#define PI 3.14159265358979323846264338327
42
42
#define PI_1_2 PI / 2
43
+ #define PI_3_2 3 * PI / 2
43
44
44
45
45
46
typedef struct point {
@@ -97,15 +98,24 @@ static double calculate_distance(point_t p1, point_t p2) {
97
98
return sqrt ((x_distance * x_distance ) + (y_distance * y_distance ));
98
99
}
99
100
101
+ static void reset_point (point_t * p ) {
102
+ p -> x = -1 ;
103
+ p -> y = -1 ;
104
+ }
105
+
100
106
static void init_gesture () {
101
107
gesture_start .point = mt_slots .points [0 ];
102
108
current_gesture = NO_GESTURE ;
103
109
104
110
if (finger_count == SCROLL_FINGER_COUNT ) {
105
- last_zoom_distance = calculate_distance ( mt_slots . points [ 0 ], mt_slots . points [ 1 ]) ;
111
+ last_zoom_distance = -1 ;
106
112
scroll .width = 0 ;
107
113
scroll .x_velocity = 0 ;
108
114
scroll .y_velocity = 0 ;
115
+ reset_point (& mt_slots .points [0 ]);
116
+ reset_point (& mt_slots .points [1 ]);
117
+ reset_point (& mt_slots .last_points [0 ]);
118
+ reset_point (& mt_slots .last_points [1 ]);
109
119
}
110
120
}
111
121
@@ -259,7 +269,8 @@ static point_t create_vector(point_t p1, point_t p2) {
259
269
/* if scrolling is enable, the finger_count matches SCROLL_FINGER_COUNT and\
260
270
the direction of the direction vectors for both fingers is equal the current_gesture\
261
271
will be SCROLL*/ \
262
- if (scroll_enabled && finger_count == SCROLL_FINGER_COUNT && vector_direction_difference < PI_1_2) { \
272
+ if (scroll_enabled && finger_count == SCROLL_FINGER_COUNT && \
273
+ (vector_direction_difference < PI_1_2 || vector_direction_difference > PI_3_2)) { \
263
274
current_gesture = SCROLL; \
264
275
/* if no scrolling and zooming is enabled or the finger_count does not match\
265
276
SCROLL_FINGER_COUNT the current_gesture will be SWIPE*/ \
@@ -295,11 +306,27 @@ static double get_vector_direction(point_t v) {
295
306
}
296
307
}
297
308
309
+ static bool is_valid_point (point_t p ) {
310
+ return p .x > -1 && p .y > -1 ;
311
+ }
312
+
313
+ static bool check_mt_slots () {
314
+ bool result = is_valid_point (mt_slots .last_points [0 ]) && is_valid_point (mt_slots .points [0 ]);
315
+ if (result && finger_count > 1 ) {
316
+ result = is_valid_point (mt_slots .last_points [1 ]) && is_valid_point (mt_slots .points [1 ]);
317
+ }
318
+ return result ;
319
+ }
320
+
298
321
static input_event_array_t * process_syn_event (struct input_event event ,
299
322
configuration_t config ,
300
323
point_t thresholds ) {
301
324
input_event_array_t * result = NULL ;
302
325
if (finger_count > 0 && event .code == SYN_REPORT ) {
326
+ if (!check_mt_slots ()) {
327
+ return new_input_event_array (0 );
328
+ }
329
+
303
330
direction_t direction = NONE ;
304
331
double vector_direction_difference ;
305
332
if (current_gesture == NO_GESTURE ) {
@@ -308,14 +335,17 @@ static input_event_array_t *process_syn_event(struct input_event event,
308
335
vector_direction_difference = fabs (v1_direction - v2_direction );
309
336
// if zooming is enable, the finger_count matches SCROLL_FINGER_COUNT and the direction
310
337
// vectors for both fingers are opposed to each other the current_gesture will be ZOOM
311
- if (config .zoom .enabled && finger_count == SCROLL_FINGER_COUNT && vector_direction_difference > PI_1_2 ) {
338
+ if (config .zoom .enabled && finger_count == SCROLL_FINGER_COUNT &&
339
+ vector_direction_difference > PI_1_2 && vector_direction_difference < PI_3_2 ) {
312
340
current_gesture = ZOOM ;
313
341
}
314
342
}
315
343
316
344
if (current_gesture == ZOOM ) {
317
345
double finger_distance = calculate_distance (mt_slots .points [0 ], mt_slots .points [1 ]);
318
- result = do_zoom (finger_distance - last_zoom_distance , config .zoom .delta );
346
+ if (last_zoom_distance > -1 ) {
347
+ result = do_zoom (finger_distance - last_zoom_distance , config .zoom .delta );
348
+ }
319
349
last_zoom_distance = finger_distance ;
320
350
} else {
321
351
int32_t x_distance , y_distance ;
@@ -426,8 +456,6 @@ void process_events(int fd, configuration_t config, void (*callback)(input_event
426
456
427
457
pthread_t scroll_thread = NULL ;
428
458
429
- uint8_t syn_event_skip_counter = 0 ;
430
-
431
459
if (thresholds .x < 0 || thresholds .y < 0 ) {
432
460
return ;
433
461
}
@@ -453,7 +481,6 @@ void process_events(int fd, configuration_t config, void (*callback)(input_event
453
481
pthread_cancel (scroll_thread );
454
482
}
455
483
init_gesture ();
456
- syn_event_skip_counter = 0 ;
457
484
} else if (current_gesture == SCROLL && (scroll .x_velocity != 0 || scroll .y_velocity != 0 )) {
458
485
scroll_thread_params_t params = {
459
486
.callback = callback
@@ -473,12 +500,7 @@ void process_events(int fd, configuration_t config, void (*callback)(input_event
473
500
case EV_ABS :
474
501
process_abs_event (ev [i ]);
475
502
break ;
476
- case EV_SYN :
477
- // skip the first 3 EV_SYN events to get enough data for mt_slots.last_points and .points
478
- // to calculate the direction vectors correctly
479
- if (syn_event_skip_counter < 3 ) {
480
- syn_event_skip_counter ++ ;
481
- } else {
503
+ case EV_SYN : {
482
504
input_event_array_t * input_events = process_syn_event (ev [i ], config , thresholds );
483
505
callback (input_events );
484
506
free (input_events );
0 commit comments