Skip to content

Commit cab6218

Browse files
author
Robin Müller
committed
Ensure that each mt_slot point was initialized correct before start the gesture detection
1 parent f506e78 commit cab6218

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

src/gesture_detection.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#define PI 3.14159265358979323846264338327
4242
#define PI_1_2 PI / 2
43+
#define PI_3_2 3 * PI / 2
4344

4445

4546
typedef struct point {
@@ -97,15 +98,24 @@ static double calculate_distance(point_t p1, point_t p2) {
9798
return sqrt((x_distance * x_distance) + (y_distance * y_distance));
9899
}
99100

101+
static void reset_point(point_t *p) {
102+
p->x = -1;
103+
p->y = -1;
104+
}
105+
100106
static void init_gesture() {
101107
gesture_start.point = mt_slots.points[0];
102108
current_gesture = NO_GESTURE;
103109

104110
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;
106112
scroll.width = 0;
107113
scroll.x_velocity = 0;
108114
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]);
109119
}
110120
}
111121

@@ -259,7 +269,8 @@ static point_t create_vector(point_t p1, point_t p2) {
259269
/* if scrolling is enable, the finger_count matches SCROLL_FINGER_COUNT and\
260270
the direction of the direction vectors for both fingers is equal the current_gesture\
261271
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)) { \
263274
current_gesture = SCROLL; \
264275
/* if no scrolling and zooming is enabled or the finger_count does not match\
265276
SCROLL_FINGER_COUNT the current_gesture will be SWIPE*/\
@@ -295,11 +306,27 @@ static double get_vector_direction(point_t v) {
295306
}
296307
}
297308

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+
298321
static input_event_array_t *process_syn_event(struct input_event event,
299322
configuration_t config,
300323
point_t thresholds) {
301324
input_event_array_t *result = NULL;
302325
if (finger_count > 0 && event.code == SYN_REPORT) {
326+
if (!check_mt_slots()) {
327+
return new_input_event_array(0);
328+
}
329+
303330
direction_t direction = NONE;
304331
double vector_direction_difference;
305332
if (current_gesture == NO_GESTURE) {
@@ -308,14 +335,17 @@ static input_event_array_t *process_syn_event(struct input_event event,
308335
vector_direction_difference = fabs(v1_direction - v2_direction);
309336
// if zooming is enable, the finger_count matches SCROLL_FINGER_COUNT and the direction
310337
// 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) {
312340
current_gesture = ZOOM;
313341
}
314342
}
315343

316344
if (current_gesture == ZOOM) {
317345
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+
}
319349
last_zoom_distance = finger_distance;
320350
} else {
321351
int32_t x_distance, y_distance;
@@ -426,8 +456,6 @@ void process_events(int fd, configuration_t config, void (*callback)(input_event
426456

427457
pthread_t scroll_thread = NULL;
428458

429-
uint8_t syn_event_skip_counter = 0;
430-
431459
if (thresholds.x < 0 || thresholds.y < 0) {
432460
return;
433461
}
@@ -453,7 +481,6 @@ void process_events(int fd, configuration_t config, void (*callback)(input_event
453481
pthread_cancel(scroll_thread);
454482
}
455483
init_gesture();
456-
syn_event_skip_counter = 0;
457484
} else if (current_gesture == SCROLL && (scroll.x_velocity != 0 || scroll.y_velocity != 0)) {
458485
scroll_thread_params_t params = {
459486
.callback = callback
@@ -473,12 +500,7 @@ void process_events(int fd, configuration_t config, void (*callback)(input_event
473500
case EV_ABS:
474501
process_abs_event(ev[i]);
475502
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: {
482504
input_event_array_t *input_events = process_syn_event(ev[i], config, thresholds);
483505
callback(input_events);
484506
free(input_events);

0 commit comments

Comments
 (0)