1 /***************************************************************************
4 * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
5 * Copyright (C) 2011 Ryan Bourgeois <bluedragonx@gmail.com>
8 * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
9 * Copyright (C) 2010 Arturo Castro <mail@arturocastro.net>
10 * Copyright (C) 2011 Ryan Bourgeois <bluedragonx@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 **************************************************************************/
32 #define IS_VALID_BUTTON(x) (x >= 0 && x <= 31)
34 static void trigger_button_up(struct Gestures* gs, int button)
36 if (IS_VALID_BUTTON(button)) {
37 if (button == 0 && gs->button_emulate > 0) {
38 button = gs->button_emulate;
39 gs->button_emulate = 0;
41 CLEARBIT(gs->buttons, button);
43 xf86Msg(X_INFO, "trigger_button_up: %d up\n", button);
48 static void trigger_button_down(struct Gestures* gs, int button)
53 if (IS_VALID_BUTTON(button) && (button != gs->button_delayed || timercmp(&gs->button_delayed_time, &epoch, ==))) {
54 SETBIT(gs->buttons, button);
56 xf86Msg(X_INFO, "trigger_button_down: %d down\n", button);
60 else if (IS_VALID_BUTTON(button))
61 xf86Msg(X_INFO, "trigger_button_down: %d down ignored, in delayed mode\n", button);
65 static void trigger_button_emulation(struct Gestures* gs, int button)
67 if (IS_VALID_BUTTON(button) && GETBIT(gs->buttons, 0)) {
68 CLEARBIT(gs->buttons, 0);
69 SETBIT(gs->buttons, button);
70 gs->button_emulate = button;
72 xf86Msg(X_INFO, "trigger_button_emulation: %d emulated\n", button);
77 static void trigger_button_click(struct Gestures* gs,
78 int button, struct timeval* trigger_up_time)
83 if (IS_VALID_BUTTON(button) && timercmp(&gs->button_delayed_time, &epoch, ==)) {
84 trigger_button_down(gs, button);
85 gs->button_delayed = button;
86 timercp(&gs->button_delayed_time, trigger_up_time);
87 timerclear(&gs->button_delayed_delta);
89 xf86Msg(X_INFO, "trigger_button_click: %d placed in delayed mode\n");
93 else if (IS_VALID_BUTTON(button))
94 xf86Msg(X_INFO, "trigger_button_click: %d ignored, in delayed mode\n", button);
98 static void trigger_drag_ready(struct Gestures* gs,
99 const struct MConfig* cfg)
101 gs->move_drag = GS_DRAG_READY;
102 timeraddms(&gs->time, cfg->drag_timeout, &gs->move_drag_expire);
103 #ifdef DEBUG_GESTURES
104 xf86Msg(X_INFO, "trigger_drag_ready: drag is ready\n");
108 static int trigger_drag_start(struct Gestures* gs,
109 const struct MConfig* cfg,
112 if (gs->move_drag == GS_DRAG_READY) {
113 timerclear(&gs->move_drag_expire);
114 if (cfg->drag_wait == 0) {
115 gs->move_drag = GS_DRAG_ACTIVE;
116 trigger_button_down(gs, 0);
117 #ifdef DEBUG_GESTURES
118 xf86Msg(X_INFO, "trigger_drag_start: drag is active\n");
122 gs->move_drag = GS_DRAG_WAIT;
123 gs->move_drag_dx = dx;
124 gs->move_drag_dy = dy;
125 timeraddms(&gs->time, cfg->drag_wait, &gs->move_drag_wait);
126 #ifdef DEBUG_GESTURES
127 xf86Msg(X_INFO, "trigger_drag_start: drag in wait\n");
131 else if (gs->move_drag == GS_DRAG_WAIT) {
132 gs->move_drag_dx += dx;
133 gs->move_drag_dy += dy;
134 if (!timercmp(&gs->time, &gs->move_drag_wait, <)) {
135 gs->move_drag = GS_DRAG_ACTIVE;
136 trigger_button_down(gs, 0);
137 #ifdef DEBUG_GESTURES
138 xf86Msg(X_INFO, "trigger_drag_start: drag is active\n");
141 else if (dist2(gs->move_drag_dx, gs->move_drag_dy) > SQRVAL(cfg->drag_dist)) {
142 gs->move_drag = GS_NONE;
143 #ifdef DEBUG_GESTURES
144 xf86Msg(X_INFO, "trigger_drag_start: drag canceled, moved too far\n");
148 return gs->move_drag != GS_DRAG_WAIT;
151 static void trigger_drag_stop(struct Gestures* gs, int force)
153 if (gs->move_drag == GS_DRAG_READY && force) {
154 gs->move_drag = GS_NONE;
155 timerclear(&gs->move_drag_expire);
156 #ifdef DEBUG_GESTURES
157 xf86Msg(X_INFO, "trigger_drag_stop: drag canceled\n");
160 else if (gs->move_drag == GS_DRAG_ACTIVE) {
161 gs->move_drag = GS_NONE;
162 timerclear(&gs->move_drag_expire);
163 trigger_button_up(gs, 0);
164 #ifdef DEBUG_GESTURES
165 xf86Msg(X_INFO, "trigger_drag_stop: drag stopped\n");
170 static void buttons_update(struct Gestures* gs,
171 const struct MConfig* cfg,
172 const struct HWState* hs,
175 if (!cfg->button_enable || cfg->trackpad_disable >= 3)
178 static bitmask_t button_prev = 0U;
179 int i, down, emulate, touching;
181 emulate = GETBIT(hs->button, 0) && !GETBIT(button_prev, 0);
183 for (i = 0; i < 32; i++) {
184 if (GETBIT(hs->button, i) == GETBIT(button_prev, i))
186 if (GETBIT(hs->button, i)) {
188 trigger_button_down(gs, i);
191 trigger_button_up(gs, i);
193 button_prev = hs->button;
196 int earliest, latest, moving = 0;
197 gs->move_type = GS_NONE;
198 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
201 foreach_bit(i, ms->touch_used) {
202 if (GETBIT(ms->touch[i].state, MT_INVALID))
204 if (cfg->button_integrated && !GETBIT(ms->touch[i].flags, GS_BUTTON))
205 SETBIT(ms->touch[i].flags, GS_BUTTON);
206 if (earliest == -1 || timercmp(&ms->touch[i].down, &ms->touch[earliest].down, <))
208 if (latest == -1 || timercmp(&ms->touch[i].down, &ms->touch[latest].down, >))
216 if (cfg->button_zones && earliest >= 0) {
217 pos = ms->touch[earliest].x;
220 if (cfg->bottom_edge_zones) {
221 int latest_bottom = -1;
222 foreach_bit(i, ms->touch_used) {
223 if (!GETBIT(ms->touch[i].state, MT_BOTTOM_EDGE))
225 if (GETBIT(ms->touch[i].state, MT_PALM) && cfg->ignore_palm)
227 /* we deliberately don't ignore thumbs for bottom button zones */
229 if (latest_bottom == -1 || timercmp(&ms->touch[i].down, &ms->touch[latest_bottom].down, >))
232 if (latest_bottom >= 0) {
233 pos = ms->touch[latest_bottom].x;
239 int zones, left, right;
243 if (cfg->button_1touch > 0)
245 if (cfg->button_2touch > 0)
247 if (cfg->button_3touch > 0)
251 width = ((double)cfg->pad_width)/((double)zones);
252 #ifdef DEBUG_GESTURES
253 xf86Msg(X_INFO, "buttons_update: pad width %d, zones %d, zone width %f, x %d\n",
254 cfg->pad_width, zones, width, pos);
256 for (i = 0; i < zones; i++) {
259 if (pos >= left && pos <= right) {
260 #ifdef DEBUG_GESTURES
261 xf86Msg(X_INFO, "buttons_update: button %d, left %d, right %d (found)\n", i, left, right);
265 #ifdef DEBUG_GESTURES
267 xf86Msg(X_INFO, "buttons_update: button %d, left %d, right %d\n", i, left, right);
272 trigger_button_emulation(gs, cfg->button_1touch - 1);
274 trigger_button_emulation(gs, cfg->button_2touch - 1);
276 trigger_button_emulation(gs, cfg->button_3touch - 1);
279 else if (latest >= 0) {
281 struct timeval expire;
282 foreach_bit(i, ms->touch_used) {
283 timeraddms(&ms->touch[i].down, cfg->button_expire, &expire);
284 if (cfg->button_move || cfg->button_expire == 0 || timercmp(&ms->touch[latest].down, &expire, <))
288 if (cfg->button_integrated)
291 if (touching == 1 && cfg->button_1touch > 0)
292 trigger_button_emulation(gs, cfg->button_1touch - 1);
293 else if (touching == 2 && cfg->button_2touch > 0)
294 trigger_button_emulation(gs, cfg->button_2touch - 1);
295 else if (touching == 3 && cfg->button_3touch > 0)
296 trigger_button_emulation(gs, cfg->button_3touch - 1);
302 static void tapping_update(struct Gestures* gs,
303 const struct MConfig* cfg,
306 int i, n, dist, released_max;
307 struct timeval tv_tmp;
308 struct timeval epoch;
310 if (cfg->trackpad_disable >= 1)
313 if (cfg->tap_4touch > 0)
315 else if (cfg->tap_3touch > 0)
317 else if (cfg->tap_2touch > 0)
319 else if (cfg->tap_1touch > 0)
325 timeraddms(&gs->tap_time_down, cfg->tap_timeout, &tv_tmp);
326 if (!timercmp(&gs->tap_time_down, &epoch, ==) && !timercmp(&gs->time, &tv_tmp, <)) {
327 gs->tap_touching = 0;
328 gs->tap_released = 0;
329 timerclear(&gs->tap_time_down);
331 foreach_bit(i, ms->touch_used) {
332 if (GETBIT(ms->touch[i].flags, GS_TAP))
333 CLEARBIT(ms->touch[i].flags, GS_TAP);
337 foreach_bit(i, ms->touch_used) {
338 if (GETBIT(ms->touch[i].state, MT_INVALID) || GETBIT(ms->touch[i].flags, GS_BUTTON)) {
339 if (GETBIT(ms->touch[i].flags, GS_TAP)) {
340 CLEARBIT(ms->touch[i].flags, GS_TAP);
342 #ifdef DEBUG_GESTURES
343 xf86Msg(X_INFO, "tapping_update: tap_touching-- (%d): invalid or button\n", gs->tap_touching);
348 if (GETBIT(ms->touch[i].state, MT_NEW)) {
349 SETBIT(ms->touch[i].flags, GS_TAP);
351 #ifdef DEBUG_GESTURES
352 xf86Msg(X_INFO, "tapping_update: tap_touching++ (%d): new touch\n", gs->tap_touching);
355 if (timercmp(&gs->tap_time_down, &epoch, ==))
356 timercp(&gs->tap_time_down, &gs->time);
359 if (GETBIT(ms->touch[i].flags, GS_TAP)) {
360 dist = dist2(ms->touch[i].total_dx, ms->touch[i].total_dy);
361 if (dist >= SQRVAL(cfg->tap_dist)) {
362 CLEARBIT(ms->touch[i].flags, GS_TAP);
364 #ifdef DEBUG_GESTURES
365 xf86Msg(X_INFO, "tapping_update: tap_touching-- (%d): moved too far\n", gs->tap_touching);
368 else if (GETBIT(ms->touch[i].state, MT_RELEASED)) {
371 #ifdef DEBUG_GESTURES
372 xf86Msg(X_INFO, "tapping_update: tap_touching-- (%d): released\n", gs->tap_touching);
373 xf86Msg(X_INFO, "tapping_update: tap_released++ (%d) (max %d): released\n", gs->tap_released, released_max);
381 if ((gs->tap_touching == 0 && gs->tap_released > 0) || gs->tap_released >= released_max) {
382 foreach_bit(i, ms->touch_used) {
383 if (GETBIT(ms->touch[i].flags, GS_TAP))
384 CLEARBIT(ms->touch[i].flags, GS_TAP);
387 if (gs->tap_released == 1)
388 n = cfg->tap_1touch - 1;
389 else if (gs->tap_released == 2)
390 n = cfg->tap_2touch - 1;
391 else if (gs->tap_released == 3)
392 n = cfg->tap_3touch - 1;
394 n = cfg->tap_4touch - 1;
396 trigger_button_click(gs, n, &tv_tmp);
397 if (cfg->drag_enable && n == 0)
398 trigger_drag_ready(gs, cfg);
400 gs->move_type = GS_NONE;
401 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
403 gs->tap_touching = 0;
404 gs->tap_released = 0;
405 timerclear(&gs->tap_time_down);
409 static void trigger_move(struct Gestures* gs,
410 const struct MConfig* cfg,
413 if ((gs->move_type == GS_MOVE || !timercmp(&gs->time, &gs->move_wait, <)) && (dx != 0 || dy != 0)) {
414 if (trigger_drag_start(gs, cfg, dx, dy)) {
415 gs->move_dx = (int)(dx*cfg->sensitivity);
416 gs->move_dy = (int)(dy*cfg->sensitivity);
417 gs->move_type = GS_MOVE;
419 gs->move_dir = TR_NONE;
420 gs->move_speed = hypot(gs->move_dx, gs->move_dy)/timertomicro(&gs->dt);
421 timerclear(&gs->move_wait);
422 #ifdef DEBUG_GESTURES
423 xf86Msg(X_INFO, "trigger_move: %d, %d (speed %f)\n",
424 gs->move_dx, gs->move_dy, gs->move_speed);
430 static void trigger_scroll(struct Gestures* gs,
431 const struct MConfig* cfg,
432 double dist, int dir)
434 if (gs->move_type == GS_SCROLL || !timercmp(&gs->time, &gs->move_wait, <)) {
435 struct timeval tv_tmp;
436 trigger_drag_stop(gs, 1);
437 if (gs->move_type != GS_SCROLL || gs->move_dir != dir)
441 gs->move_type = GS_SCROLL;
442 gs->move_dist += (int)ABSVAL(dist);
444 gs->move_speed = dist/timertomicro(&gs->dt);
445 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
447 if (gs->move_dist >= cfg->scroll_dist) {
448 gs->move_dist = MODVAL(gs->move_dist, cfg->scroll_dist);
449 timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
450 if (dir == TR_DIR_UP)
451 trigger_button_click(gs, cfg->scroll_up_btn - 1, &tv_tmp);
452 else if (dir == TR_DIR_DN)
453 trigger_button_click(gs, cfg->scroll_dn_btn - 1, &tv_tmp);
454 else if (dir == TR_DIR_LT)
455 trigger_button_click(gs, cfg->scroll_lt_btn - 1, &tv_tmp);
456 else if (dir == TR_DIR_RT)
457 trigger_button_click(gs, cfg->scroll_rt_btn - 1, &tv_tmp);
459 #ifdef DEBUG_GESTURES
460 xf86Msg(X_INFO, "trigger_scroll: scrolling %+f in direction %d (at %d of %d) (speed %f)\n",
461 dist, dir, gs->move_dist, cfg->scroll_dist, gs->move_speed);
466 static void trigger_swipe(struct Gestures* gs,
467 const struct MConfig* cfg,
468 double dist, int dir, int isfour)
470 if (gs->move_type == GS_SWIPE || !timercmp(&gs->time, &gs->move_wait, <)) {
471 struct timeval tv_tmp;
472 trigger_drag_stop(gs, 1);
473 if (gs->move_type != GS_SWIPE || gs->move_dir != dir)
477 gs->move_type = GS_SWIPE;
478 gs->move_dist += (int)ABSVAL(dist);
480 gs->move_speed = dist/timertomicro(&gs->dt);
481 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
482 timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
485 if (cfg->swipe4_dist > 0 && gs->move_dist >= cfg->swipe4_dist) {
486 gs->move_dist = MODVAL(gs->move_dist, cfg->swipe4_dist);
487 if (dir == TR_DIR_UP)
488 trigger_button_click(gs, cfg->swipe4_up_btn - 1, &tv_tmp);
489 else if (dir == TR_DIR_DN)
490 trigger_button_click(gs, cfg->swipe4_dn_btn - 1, &tv_tmp);
491 else if (dir == TR_DIR_LT)
492 trigger_button_click(gs, cfg->swipe4_lt_btn - 1, &tv_tmp);
493 else if (dir == TR_DIR_RT)
494 trigger_button_click(gs, cfg->swipe4_rt_btn - 1, &tv_tmp);
496 #ifdef DEBUG_GESTURES
497 xf86Msg(X_INFO, "trigger_swipe4: swiping %+f in direction %d (at %d of %d) (speed %f)\n",
498 dist, dir, gs->move_dist, cfg->swipe_dist, gs->move_speed);
502 if (cfg->swipe_dist > 0 && gs->move_dist >= cfg->swipe_dist) {
503 gs->move_dist = MODVAL(gs->move_dist, cfg->swipe_dist);
504 if (dir == TR_DIR_UP)
505 trigger_button_click(gs, cfg->swipe_up_btn - 1, &tv_tmp);
506 else if (dir == TR_DIR_DN)
507 trigger_button_click(gs, cfg->swipe_dn_btn - 1, &tv_tmp);
508 else if (dir == TR_DIR_LT)
509 trigger_button_click(gs, cfg->swipe_lt_btn - 1, &tv_tmp);
510 else if (dir == TR_DIR_RT)
511 trigger_button_click(gs, cfg->swipe_rt_btn - 1, &tv_tmp);
513 #ifdef DEBUG_GESTURES
514 xf86Msg(X_INFO, "trigger_swipe: swiping %+f in direction %d (at %d of %d)\n", dist, dir, gs->move_dist, cfg->swipe_dist);
520 static void trigger_scale(struct Gestures* gs,
521 const struct MConfig* cfg,
522 double dist, int dir)
524 if (gs->move_type == GS_SCALE || !timercmp(&gs->time, &gs->move_wait, <)) {
525 struct timeval tv_tmp;
526 trigger_drag_stop(gs, 1);
527 if (gs->move_type != GS_SCALE || gs->move_dir != dir)
531 gs->move_type = GS_SCALE;
532 gs->move_dist += (int)ABSVAL(dist);
534 gs->move_speed = dist/timertomicro(&gs->dt);
535 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
536 if (gs->move_dist >= cfg->scale_dist) {
537 gs->move_dist = MODVAL(gs->move_dist, cfg->scale_dist);
538 timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
539 if (dir == TR_DIR_UP)
540 trigger_button_click(gs, cfg->scale_up_btn - 1, &tv_tmp);
541 else if (dir == TR_DIR_DN)
542 trigger_button_click(gs, cfg->scale_dn_btn - 1, &tv_tmp);
544 #ifdef DEBUG_GESTURES
545 xf86Msg(X_INFO, "trigger_scale: scaling %+f in direction %d (at %d of %d) (speed %f)\n",
546 dist, dir, gs->move_dist, cfg->scale_dist, gs->move_speed);
551 static void trigger_rotate(struct Gestures* gs,
552 const struct MConfig* cfg,
553 double dist, int dir)
555 if (gs->move_type == GS_ROTATE || !timercmp(&gs->time, &gs->move_wait, <)) {
556 struct timeval tv_tmp;
557 trigger_drag_stop(gs, 1);
558 if (gs->move_type != GS_ROTATE || gs->move_dir != dir)
562 gs->move_type = GS_ROTATE;
563 gs->move_dist += (int)ABSVAL(dist);
565 gs->move_speed = dist/timertomicro(&gs->dt);
566 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
567 if (gs->move_dist >= cfg->rotate_dist) {
568 gs->move_dist = MODVAL(gs->move_dist, cfg->rotate_dist);
569 timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
570 if (dir == TR_DIR_LT)
571 trigger_button_click(gs, cfg->rotate_lt_btn - 1, &tv_tmp);
572 else if (dir == TR_DIR_RT)
573 trigger_button_click(gs, cfg->rotate_rt_btn - 1, &tv_tmp);
575 #ifdef DEBUG_GESTURES
576 xf86Msg(X_INFO, "trigger_rotate: rotating %+f in direction %d (at %d of %d) (speed %f)\n",
577 dist, dir, gs->move_dist, cfg->rotate_dist, gs->move_speed);
582 static void trigger_reset(struct Gestures* gs)
584 trigger_drag_stop(gs, 0);
587 gs->move_type = GS_NONE;
589 gs->move_dir = TR_NONE;
590 timerclear(&gs->move_wait);
593 static int get_scroll_dir(const struct Touch* t1,
594 const struct Touch* t2)
596 if (trig_angles_acute(t1->direction, t2->direction) < 2.0) {
598 angles[0] = t1->direction;
599 angles[1] = t2->direction;
600 return trig_generalize(trig_angles_avg(angles, 2));
605 static int get_rotate_dir(const struct Touch* t1,
606 const struct Touch* t2)
609 v = trig_direction(t2->x - t1->x, t2->y - t1->y);
610 d1 = trig_angles_add(v, 2);
611 d2 = trig_angles_sub(v, 2);
612 if (trig_angles_acute(t1->direction, d1) < 2 && trig_angles_acute(t2->direction, d2) < 2)
614 else if (trig_angles_acute(t1->direction, d2) < 2 && trig_angles_acute(t2->direction, d1) < 2)
619 static int get_scale_dir(const struct Touch* t1,
620 const struct Touch* t2)
623 if (trig_angles_acute(t1->direction, t2->direction) >= 2) {
624 v = trig_direction(t2->x - t1->x, t2->y - t1->y);
625 if (trig_angles_acute(v, t1->direction) < 2)
633 static int get_swipe_dir(const struct Touch* t1,
634 const struct Touch* t2,
635 const struct Touch* t3)
638 angles[0] = t1->direction;
639 angles[1] = t2->direction;
640 angles[2] = t3->direction;
641 return trig_generalize(trig_angles_avg(angles, 3));
644 static int get_swipe4_dir(const struct Touch* t1,
645 const struct Touch* t2,
646 const struct Touch* t3,
647 const struct Touch* t4)
650 angles[0] = t1->direction;
651 angles[1] = t2->direction;
652 angles[2] = t3->direction;
653 angles[3] = t4->direction;
654 return trig_generalize(trig_angles_avg(angles, 4));
657 static void moving_update(struct Gestures* gs,
658 const struct MConfig* cfg,
661 int i, count, btn_count, dx, dy, dir;
663 struct Touch* touches[4];
664 count = btn_count = 0;
672 // Count touches and aggregate touch movements.
673 foreach_bit(i, ms->touch_used) {
674 if (GETBIT(ms->touch[i].state, MT_INVALID))
676 else if (GETBIT(ms->touch[i].flags, GS_BUTTON)) {
678 dx += ms->touch[i].dx;
679 dy += ms->touch[i].dy;
681 else if (!GETBIT(ms->touch[i].flags, GS_TAP)) {
683 touches[count++] = &ms->touch[i];
687 // Determine gesture type.
689 if (btn_count >= 1 && cfg->trackpad_disable < 2)
690 trigger_move(gs, cfg, dx, dy);
691 else if (btn_count < 1)
694 else if (count == 1 && cfg->trackpad_disable < 2) {
695 dx += touches[0]->dx;
696 dy += touches[0]->dy;
697 trigger_move(gs, cfg, dx, dy);
699 else if (count == 2 && cfg->trackpad_disable < 1) {
700 // scroll, scale, or rotate
701 if ((dir = get_scroll_dir(touches[0], touches[1])) != TR_NONE) {
703 touches[0]->dx + touches[1]->dx,
704 touches[0]->dy + touches[1]->dy);
705 trigger_scroll(gs, cfg, dist/2, dir);
707 else if ((dir = get_rotate_dir(touches[0], touches[1])) != TR_NONE) {
708 dist = ABSVAL(hypot(touches[0]->dx, touches[0]->dy)) +
709 ABSVAL(hypot(touches[1]->dx, touches[1]->dy));
710 trigger_rotate(gs, cfg, dist/2, dir);
712 else if ((dir = get_scale_dir(touches[0], touches[1])) != TR_NONE) {
713 dist = ABSVAL(hypot(touches[0]->dx, touches[0]->dy)) +
714 ABSVAL(hypot(touches[1]->dx, touches[1]->dy));
715 trigger_scale(gs, cfg, dist/2, dir);
718 else if (count == 3 && cfg->trackpad_disable < 1) {
719 if ((dir = get_swipe_dir(touches[0], touches[1], touches[2])) != TR_NONE) {
721 touches[0]->dx + touches[1]->dx + touches[2]->dx,
722 touches[0]->dy + touches[1]->dy + touches[2]->dy);
723 trigger_swipe(gs, cfg, dist/3, dir, 0);
726 else if (count == 4 && cfg->trackpad_disable < 1) {
727 if ((dir = get_swipe4_dir(touches[0], touches[1], touches[2], touches[3])) != TR_NONE) {
729 touches[0]->dx + touches[1]->dx + touches[2]->dx + touches[3]->dx,
730 touches[0]->dy + touches[1]->dy + touches[2]->dy + touches[3]->dy);
731 trigger_swipe(gs, cfg, dist/4, dir, 1);
736 static void dragging_update(struct Gestures* gs)
738 if (gs->move_drag == GS_DRAG_READY && timercmp(&gs->time, &gs->move_drag_expire, >)) {
739 #ifdef DEBUG_GESTURES
740 xf86Msg(X_INFO, "dragging_update: drag expired\n");
742 trigger_drag_stop(gs, 1);
746 static void delayed_update(struct Gestures* gs)
748 struct timeval epoch;
751 if (timercmp(&gs->button_delayed_time, &epoch, ==))
754 if (!timercmp(&gs->time, &gs->button_delayed_time, <)) {
755 #ifdef DEBUG_GESTURES
756 xf86Msg(X_INFO, "delayed_update: %d delay expired, triggering up\n", gs->button_delayed);
758 trigger_button_up(gs, gs->button_delayed);
759 gs->button_delayed = 0;
760 timerclear(&gs->button_delayed_time);
761 timerclear(&gs->button_delayed_delta);
764 timersub(&gs->button_delayed_time, &gs->time, &gs->button_delayed_delta);
768 void gestures_init(struct MTouch* mt)
770 memset(&mt->gs, 0, sizeof(struct Gestures));
773 void gestures_extract(struct MTouch* mt)
775 timersub(&mt->hs.evtime, &mt->gs.time, &mt->gs.dt);
776 timercp(&mt->gs.time, &mt->hs.evtime);
778 dragging_update(&mt->gs);
779 buttons_update(&mt->gs, &mt->cfg, &mt->hs, &mt->state);
780 tapping_update(&mt->gs, &mt->cfg, &mt->state);
781 moving_update(&mt->gs, &mt->cfg, &mt->state);
782 delayed_update(&mt->gs);
785 static int gestures_sleep(struct MTouch* mt, const struct timeval* sleep)
787 if (mtdev_empty(&mt->dev)) {
789 mtdev_idle(&mt->dev, mt->fd, timertoms(sleep));
791 timersub(&now, &mt->gs.time, &mt->gs.dt);
792 timercp(&mt->gs.time, &now);
798 int gestures_delayed(struct MTouch* mt)
800 struct Gestures* gs = &mt->gs;
801 struct timeval epoch;
804 if (timercmp(&gs->button_delayed_time, &epoch, >)) {
805 if (gestures_sleep(mt, &gs->button_delayed_delta)) {
806 #ifdef DEBUG_GESTURES
807 xf86Msg(X_INFO, "gestures_delayed: %d up, timer expired\n", gs->button_delayed);
809 trigger_button_up(gs, gs->button_delayed);
812 gs->button_delayed = 0;
813 timerclear(&gs->button_delayed_time);
814 timerclear(&gs->button_delayed_delta);