chiark / gitweb /
22a94ac15e4271da5d57905261cda469bc3ccd3f
[xf86-input-mtrack.git] / src / gestures.c
1 /***************************************************************************
2  *
3  * Multitouch X driver
4  * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
5  * Copyright (C) 2011 Ryan Bourgeois <bluedragonx@gmail.com>
6  *
7  * Gestures
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>
11  *
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.
16  *
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.
21  *
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
25  *
26  **************************************************************************/
27
28 #include "gestures.h"
29 #include "mtouch.h"
30 #include "trig.h"
31
32 #define IS_VALID_BUTTON(x) (x >= 0 && x <= 31)
33
34 static void trigger_button_up(struct Gestures* gs, int button)
35 {
36         if (IS_VALID_BUTTON(button)) {
37                 if (button == 0 && gs->button_emulate > 0) {
38                         button = gs->button_emulate;
39                         gs->button_emulate = 0;
40                 }
41                 CLEARBIT(gs->buttons, button);
42 #ifdef DEBUG_GESTURES
43                 xf86Msg(X_INFO, "trigger_button_up: %d up\n", button);
44 #endif
45         }
46 }
47
48 static void trigger_button_down(struct Gestures* gs, int button)
49 {
50         struct timeval epoch;
51         timerclear(&epoch);
52
53         if (IS_VALID_BUTTON(button) && (button != gs->button_delayed || timercmp(&gs->button_delayed_time, &epoch, ==))) {
54                 SETBIT(gs->buttons, button);
55 #ifdef DEBUG_GESTURES
56                 xf86Msg(X_INFO, "trigger_button_down: %d down\n", button);
57 #endif
58         }
59 #ifdef DEBUG_GESTURES
60         else if (IS_VALID_BUTTON(button))
61                 xf86Msg(X_INFO, "trigger_button_down: %d down ignored, in delayed mode\n", button);
62 #endif
63 }
64
65 static void trigger_button_emulation(struct Gestures* gs, int button)
66 {
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;
71 #ifdef DEBUG_GESTURES
72                 xf86Msg(X_INFO, "trigger_button_emulation: %d emulated\n", button);
73 #endif
74         }
75 }
76
77 static void trigger_button_click(struct Gestures* gs,
78                         int button, struct timeval* trigger_up_time)
79 {
80         struct timeval epoch;
81         timerclear(&epoch);
82
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);
88 #ifdef DEBUG_GESTRUES
89                 xf86Msg(X_INFO, "trigger_button_click: %d placed in delayed mode\n");
90 #endif
91         }
92 #ifdef DEBUG_GESTURES
93         else if (IS_VALID_BUTTON(button))
94                 xf86Msg(X_INFO, "trigger_button_click: %d ignored, in delayed mode\n", button);
95 #endif
96 }
97
98 static void trigger_drag_ready(struct Gestures* gs,
99                         const struct MConfig* cfg)
100 {
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");
105 #endif
106 }
107
108 static int trigger_drag_start(struct Gestures* gs,
109                         const struct MConfig* cfg,
110                         int dx, int dy)
111 {
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");
119 #endif
120                 }
121                 else {
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");
128 #endif
129                 }
130         }
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");
139 #endif
140                 }
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");
145 #endif
146                 }
147         }
148         return gs->move_drag != GS_DRAG_WAIT;
149 }
150
151 static void trigger_drag_stop(struct Gestures* gs, int force)
152 {
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");
158 #endif
159         }
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");
166 #endif
167         }
168 }
169
170 static void buttons_update(struct Gestures* gs,
171                         const struct MConfig* cfg,
172                         const struct HWState* hs,
173                         struct MTState* ms)
174 {
175         if (!cfg->button_enable || cfg->trackpad_disable >= 3)
176                 return;
177
178         static bitmask_t button_prev = 0U;
179         int i, down, emulate, touching;
180         down = 0;
181         emulate = GETBIT(hs->button, 0) && !GETBIT(button_prev, 0);
182
183         for (i = 0; i < 32; i++) {
184                 if (GETBIT(hs->button, i) == GETBIT(button_prev, i))
185                         continue;
186                 if (GETBIT(hs->button, i)) {
187                         down++;
188                         trigger_button_down(gs, i);
189                 }
190                 else
191                         trigger_button_up(gs, i);
192         }
193         button_prev = hs->button;
194
195         if (down) {
196                 int earliest, latest, moving = 0;
197                 gs->move_type = GS_NONE;
198                 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
199                 earliest = -1;
200                 latest = -1;
201                 foreach_bit(i, ms->touch_used) {
202                         if (GETBIT(ms->touch[i].state, MT_INVALID))
203                                 continue;
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, <))
207                                 earliest = i;
208                         if (latest == -1 || timercmp(&ms->touch[i].down, &ms->touch[latest].down, >))
209                                 latest = i;
210                 }
211
212                 if (emulate) {
213                         int pos = -1;
214
215                         if (cfg->button_zones && earliest >= 0) {
216                                 pos = ms->touch[earliest].x;
217                         }
218                         if (cfg->bottom_edge_zones) {
219                                 int latest_bottom = -1;
220                                 foreach_bit(i, ms->touch_used) {
221                                         if (!GETBIT(ms->touch[i].state, MT_BOTTOM_EDGE))
222                                                 continue;
223                                         if (GETBIT(ms->touch[i].state, MT_PALM) && cfg->ignore_palm)
224                                                 continue;
225                                         /* we deliberately don't ignore thumbs for bottom button zones */
226
227                                         if (latest_bottom == -1 || timercmp(&ms->touch[i].down, &ms->touch[latest_bottom].down, >))
228                                                 latest_bottom = i;
229                                 }
230                                 if (latest_bottom >= 0)
231                                         pos = ms->touch[latest_bottom].x;
232                         }
233
234                         if (pos >= 0) {
235                                 int zones, left, right;
236                                 double width;
237
238                                 zones = 0;
239                                 if (cfg->button_1touch > 0)
240                                         zones++;
241                                 if (cfg->button_2touch > 0)
242                                         zones++;
243                                 if (cfg->button_3touch > 0)
244                                         zones++;
245
246                                 if (zones > 0) {
247                                         width = ((double)cfg->pad_width)/((double)zones);
248 #ifdef DEBUG_GESTURES
249                                         xf86Msg(X_INFO, "buttons_update: pad width %d, zones %d, zone width %f, x %d\n",
250                                                 cfg->pad_width, zones, width, pos);
251 #endif
252                                         for (i = 0; i < zones; i++) {
253                                                 left = width*i;
254                                                 right = width*(i+1);
255                                                 if (pos >= left && pos <= right) {
256 #ifdef DEBUG_GESTURES
257                                                         xf86Msg(X_INFO, "buttons_update: button %d, left %d, right %d (found)\n", i, left, right);
258 #endif
259                                                         break;
260                                                 }
261 #ifdef DEBUG_GESTURES
262                                                 else
263                                                         xf86Msg(X_INFO, "buttons_update: button %d, left %d, right %d\n", i, left, right);
264 #endif
265                                         }
266
267                                         if (i == 0)
268                                                 trigger_button_emulation(gs, cfg->button_1touch - 1);
269                                         else if (i == 1)
270                                                 trigger_button_emulation(gs, cfg->button_2touch - 1);
271                                         else
272                                                 trigger_button_emulation(gs, cfg->button_3touch - 1);
273                                 }
274                         }
275                         else if (latest >= 0) {
276                                 touching = 0;
277                                 struct timeval expire;
278                                 foreach_bit(i, ms->touch_used) {
279                                         timeraddms(&ms->touch[i].down, cfg->button_expire, &expire);
280                                         if (cfg->button_move || cfg->button_expire == 0 || timercmp(&ms->touch[latest].down, &expire, <))
281                                                 touching++;
282                                 }
283
284                                 if (cfg->button_integrated)
285                                         touching--;
286
287                                 if (touching == 1 && cfg->button_1touch > 0)
288                                         trigger_button_emulation(gs, cfg->button_1touch - 1);
289                                 else if (touching == 2 && cfg->button_2touch > 0)
290                                         trigger_button_emulation(gs, cfg->button_2touch - 1);
291                                 else if (touching == 3 && cfg->button_3touch > 0)
292                                         trigger_button_emulation(gs, cfg->button_3touch - 1);
293                         }
294                 }
295         }
296 }
297
298 static void tapping_update(struct Gestures* gs,
299                         const struct MConfig* cfg,
300                         struct MTState* ms)
301 {
302         int i, n, dist, released_max;
303         struct timeval tv_tmp;
304         struct timeval epoch;
305
306         if (cfg->trackpad_disable >= 1)
307                 return;
308
309         if (cfg->tap_4touch > 0)
310                 released_max = 4;
311         else if (cfg->tap_3touch > 0)
312                 released_max = 3;
313         else if (cfg->tap_2touch > 0)
314                 released_max = 2;
315         else if (cfg->tap_1touch > 0)
316                 released_max = 1;
317         else
318                 return;
319
320         timerclear(&epoch);
321         timeraddms(&gs->tap_time_down, cfg->tap_timeout, &tv_tmp);
322         if (!timercmp(&gs->tap_time_down, &epoch, ==) && !timercmp(&gs->time, &tv_tmp, <)) {
323                 gs->tap_touching = 0;
324                 gs->tap_released = 0;
325                 timerclear(&gs->tap_time_down);
326
327                 foreach_bit(i, ms->touch_used) {
328                         if (GETBIT(ms->touch[i].flags, GS_TAP))
329                                 CLEARBIT(ms->touch[i].flags, GS_TAP);
330                 }
331         }
332         else {
333                 foreach_bit(i, ms->touch_used) {
334                         if (GETBIT(ms->touch[i].state, MT_INVALID) || GETBIT(ms->touch[i].flags, GS_BUTTON)) {
335                                 if (GETBIT(ms->touch[i].flags, GS_TAP)) {
336                                         CLEARBIT(ms->touch[i].flags, GS_TAP);
337                                         gs->tap_touching--;
338 #ifdef DEBUG_GESTURES
339                                         xf86Msg(X_INFO, "tapping_update: tap_touching-- (%d): invalid or button\n", gs->tap_touching);
340 #endif
341                                 }
342                         }
343                         else {
344                                 if (GETBIT(ms->touch[i].state, MT_NEW)) {
345                                         SETBIT(ms->touch[i].flags, GS_TAP);
346                                         gs->tap_touching++;
347 #ifdef DEBUG_GESTURES
348                                         xf86Msg(X_INFO, "tapping_update: tap_touching++ (%d): new touch\n", gs->tap_touching);
349 #endif
350                                         timerclear(&tv_tmp);
351                                         if (timercmp(&gs->tap_time_down, &epoch, ==))
352                                                 timercp(&gs->tap_time_down, &gs->time);
353                                 }
354
355                                 if (GETBIT(ms->touch[i].flags, GS_TAP)) {
356                                         dist = dist2(ms->touch[i].total_dx, ms->touch[i].total_dy);
357                                         if (dist >= SQRVAL(cfg->tap_dist)) {
358                                                 CLEARBIT(ms->touch[i].flags, GS_TAP);
359                                                 gs->tap_touching--;
360 #ifdef DEBUG_GESTURES
361                                         xf86Msg(X_INFO, "tapping_update: tap_touching-- (%d): moved too far\n", gs->tap_touching);
362 #endif
363                                         }
364                                         else if (GETBIT(ms->touch[i].state, MT_RELEASED)) {
365                                                 gs->tap_touching--;
366                                                 gs->tap_released++;
367 #ifdef DEBUG_GESTURES
368                                         xf86Msg(X_INFO, "tapping_update: tap_touching-- (%d): released\n", gs->tap_touching);
369                                         xf86Msg(X_INFO, "tapping_update: tap_released++ (%d) (max %d): released\n", gs->tap_released, released_max);
370 #endif
371                                         }
372                                 }
373                         }
374                 }
375         }
376
377         if ((gs->tap_touching == 0 && gs->tap_released > 0) || gs->tap_released >= released_max) {
378                 foreach_bit(i, ms->touch_used) {
379                         if (GETBIT(ms->touch[i].flags, GS_TAP))
380                                 CLEARBIT(ms->touch[i].flags, GS_TAP);
381                 }
382
383                 if (gs->tap_released == 1)
384                         n = cfg->tap_1touch - 1;
385                 else if (gs->tap_released == 2)
386                         n = cfg->tap_2touch - 1;
387                 else if (gs->tap_released == 3)
388                         n = cfg->tap_3touch - 1;
389                 else
390                         n = cfg->tap_4touch - 1;
391
392                 trigger_button_click(gs, n, &tv_tmp);
393                 if (cfg->drag_enable && n == 0)
394                         trigger_drag_ready(gs, cfg);
395
396                 gs->move_type = GS_NONE;
397                 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
398
399                 gs->tap_touching = 0;
400                 gs->tap_released = 0;
401                 timerclear(&gs->tap_time_down);
402         }
403 }
404
405 static void trigger_move(struct Gestures* gs,
406                         const struct MConfig* cfg,
407                         int dx, int dy)
408 {
409         if ((gs->move_type == GS_MOVE || !timercmp(&gs->time, &gs->move_wait, <)) && (dx != 0 || dy != 0)) {
410                 if (trigger_drag_start(gs, cfg, dx, dy)) {
411                         gs->move_dx = (int)(dx*cfg->sensitivity);
412                         gs->move_dy = (int)(dy*cfg->sensitivity);
413                         gs->move_type = GS_MOVE;
414                         gs->move_dist = 0;
415                         gs->move_dir = TR_NONE;
416                         gs->move_speed = hypot(gs->move_dx, gs->move_dy)/timertomicro(&gs->dt);
417                         timerclear(&gs->move_wait);
418 #ifdef DEBUG_GESTURES
419                         xf86Msg(X_INFO, "trigger_move: %d, %d (speed %f)\n",
420                                 gs->move_dx, gs->move_dy, gs->move_speed);
421 #endif
422                 }
423         }
424 }
425
426 static void trigger_scroll(struct Gestures* gs,
427                         const struct MConfig* cfg,
428                         double dist, int dir)
429 {
430         if (gs->move_type == GS_SCROLL || !timercmp(&gs->time, &gs->move_wait, <)) {
431                 struct timeval tv_tmp;
432                 trigger_drag_stop(gs, 1);
433                 if (gs->move_type != GS_SCROLL || gs->move_dir != dir)
434                         gs->move_dist = 0;
435                 gs->move_dx = 0;
436                 gs->move_dy = 0;
437                 gs->move_type = GS_SCROLL;
438                 gs->move_dist += (int)ABSVAL(dist);
439                 gs->move_dir = dir;
440                 gs->move_speed = dist/timertomicro(&gs->dt);
441                 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
442
443                 if (gs->move_dist >= cfg->scroll_dist) {
444                         gs->move_dist = MODVAL(gs->move_dist, cfg->scroll_dist);
445                         timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
446                         if (dir == TR_DIR_UP)
447                                 trigger_button_click(gs, cfg->scroll_up_btn - 1, &tv_tmp);
448                         else if (dir == TR_DIR_DN)
449                                 trigger_button_click(gs, cfg->scroll_dn_btn - 1, &tv_tmp);
450                         else if (dir == TR_DIR_LT)
451                                 trigger_button_click(gs, cfg->scroll_lt_btn - 1, &tv_tmp);
452                         else if (dir == TR_DIR_RT)
453                                 trigger_button_click(gs, cfg->scroll_rt_btn - 1, &tv_tmp);
454                 }
455 #ifdef DEBUG_GESTURES
456                 xf86Msg(X_INFO, "trigger_scroll: scrolling %+f in direction %d (at %d of %d) (speed %f)\n",
457                         dist, dir, gs->move_dist, cfg->scroll_dist, gs->move_speed);
458 #endif
459         }
460 }
461
462 static void trigger_swipe(struct Gestures* gs,
463                         const struct MConfig* cfg,
464                         double dist, int dir, int isfour)
465 {
466         if (gs->move_type == GS_SWIPE || !timercmp(&gs->time, &gs->move_wait, <)) {
467                 struct timeval tv_tmp;
468                 trigger_drag_stop(gs, 1);
469                 if (gs->move_type != GS_SWIPE || gs->move_dir != dir)
470                         gs->move_dist = 0;
471                 gs->move_dx = 0;
472                 gs->move_dy = 0;
473                 gs->move_type = GS_SWIPE;
474                 gs->move_dist += (int)ABSVAL(dist);
475                 gs->move_dir = dir;
476                 gs->move_speed = dist/timertomicro(&gs->dt);
477                 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
478                 timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
479
480                 if (isfour) {
481                         if (cfg->swipe4_dist > 0 && gs->move_dist >= cfg->swipe4_dist) {
482                                 gs->move_dist = MODVAL(gs->move_dist, cfg->swipe4_dist);
483                                 if (dir == TR_DIR_UP)
484                                         trigger_button_click(gs, cfg->swipe4_up_btn - 1, &tv_tmp);
485                                 else if (dir == TR_DIR_DN)
486                                         trigger_button_click(gs, cfg->swipe4_dn_btn - 1, &tv_tmp);
487                                 else if (dir == TR_DIR_LT)
488                                         trigger_button_click(gs, cfg->swipe4_lt_btn - 1, &tv_tmp);
489                                 else if (dir == TR_DIR_RT)
490                                         trigger_button_click(gs, cfg->swipe4_rt_btn - 1, &tv_tmp);
491                         }
492 #ifdef DEBUG_GESTURES
493                         xf86Msg(X_INFO, "trigger_swipe4: swiping %+f in direction %d (at %d of %d) (speed %f)\n",
494                                 dist, dir, gs->move_dist, cfg->swipe_dist, gs->move_speed);
495 #endif
496                 }
497                 else {
498                         if (cfg->swipe_dist > 0 && gs->move_dist >= cfg->swipe_dist) {
499                                 gs->move_dist = MODVAL(gs->move_dist, cfg->swipe_dist);
500                                 if (dir == TR_DIR_UP)
501                                         trigger_button_click(gs, cfg->swipe_up_btn - 1, &tv_tmp);
502                                 else if (dir == TR_DIR_DN)
503                                         trigger_button_click(gs, cfg->swipe_dn_btn - 1, &tv_tmp);
504                                 else if (dir == TR_DIR_LT)
505                                         trigger_button_click(gs, cfg->swipe_lt_btn - 1, &tv_tmp);
506                                 else if (dir == TR_DIR_RT)
507                                         trigger_button_click(gs, cfg->swipe_rt_btn - 1, &tv_tmp);
508                         }
509 #ifdef DEBUG_GESTURES
510                         xf86Msg(X_INFO, "trigger_swipe: swiping %+f in direction %d (at %d of %d)\n", dist, dir, gs->move_dist, cfg->swipe_dist);
511 #endif
512                 }
513         }
514 }
515
516 static void trigger_scale(struct Gestures* gs,
517                         const struct MConfig* cfg,
518                         double dist, int dir)
519 {
520         if (gs->move_type == GS_SCALE || !timercmp(&gs->time, &gs->move_wait, <)) {
521                 struct timeval tv_tmp;
522                 trigger_drag_stop(gs, 1);
523                 if (gs->move_type != GS_SCALE || gs->move_dir != dir)
524                         gs->move_dist = 0;
525                 gs->move_dx = 0;
526                 gs->move_dy = 0;
527                 gs->move_type = GS_SCALE;
528                 gs->move_dist += (int)ABSVAL(dist);
529                 gs->move_dir = dir;
530                 gs->move_speed = dist/timertomicro(&gs->dt);
531                 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
532                 if (gs->move_dist >= cfg->scale_dist) {
533                         gs->move_dist = MODVAL(gs->move_dist, cfg->scale_dist);
534                         timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
535                         if (dir == TR_DIR_UP)
536                                 trigger_button_click(gs, cfg->scale_up_btn - 1, &tv_tmp);
537                         else if (dir == TR_DIR_DN)
538                                 trigger_button_click(gs, cfg->scale_dn_btn - 1, &tv_tmp);
539                 }
540 #ifdef DEBUG_GESTURES
541                 xf86Msg(X_INFO, "trigger_scale: scaling %+f in direction %d (at %d of %d) (speed %f)\n",
542                         dist, dir, gs->move_dist, cfg->scale_dist, gs->move_speed);
543 #endif
544         }
545 }
546
547 static void trigger_rotate(struct Gestures* gs,
548                         const struct MConfig* cfg,
549                         double dist, int dir)
550 {
551         if (gs->move_type == GS_ROTATE || !timercmp(&gs->time, &gs->move_wait, <)) {
552                 struct timeval tv_tmp;
553                 trigger_drag_stop(gs, 1);
554                 if (gs->move_type != GS_ROTATE || gs->move_dir != dir)
555                         gs->move_dist = 0;
556                 gs->move_dx = 0;
557                 gs->move_dy = 0;
558                 gs->move_type = GS_ROTATE;
559                 gs->move_dist += (int)ABSVAL(dist);
560                 gs->move_dir = dir;
561                 gs->move_speed = dist/timertomicro(&gs->dt);
562                 timeraddms(&gs->time, cfg->gesture_wait, &gs->move_wait);
563                 if (gs->move_dist >= cfg->rotate_dist) {
564                         gs->move_dist = MODVAL(gs->move_dist, cfg->rotate_dist);
565                         timeraddms(&gs->time, cfg->gesture_hold, &tv_tmp);
566                         if (dir == TR_DIR_LT)
567                                 trigger_button_click(gs, cfg->rotate_lt_btn - 1, &tv_tmp);
568                         else if (dir == TR_DIR_RT)
569                                 trigger_button_click(gs, cfg->rotate_rt_btn - 1, &tv_tmp);
570                 }
571 #ifdef DEBUG_GESTURES
572                 xf86Msg(X_INFO, "trigger_rotate: rotating %+f in direction %d (at %d of %d) (speed %f)\n",
573                         dist, dir, gs->move_dist, cfg->rotate_dist, gs->move_speed);
574 #endif
575         }
576 }
577
578 static void trigger_reset(struct Gestures* gs)
579 {
580         trigger_drag_stop(gs, 0);
581         gs->move_dx = 0;
582         gs->move_dy = 0;
583         gs->move_type = GS_NONE;
584         gs->move_dist = 0;
585         gs->move_dir = TR_NONE;
586         timerclear(&gs->move_wait);
587 }
588
589 static int get_scroll_dir(const struct Touch* t1,
590                         const struct Touch* t2)
591 {
592         if (trig_angles_acute(t1->direction, t2->direction) < 2.0) {
593                 double angles[2];
594                 angles[0] = t1->direction;
595                 angles[1] = t2->direction;
596                 return trig_generalize(trig_angles_avg(angles, 2));
597         }
598         return TR_NONE;
599 }
600
601 static int get_rotate_dir(const struct Touch* t1,
602                         const struct Touch* t2)
603 {
604         double v, d1, d2;
605         v = trig_direction(t2->x - t1->x, t2->y - t1->y);
606         d1 = trig_angles_add(v, 2);
607         d2 = trig_angles_sub(v, 2);
608         if (trig_angles_acute(t1->direction, d1) < 2 && trig_angles_acute(t2->direction, d2) < 2)
609                 return TR_DIR_RT;
610         else if (trig_angles_acute(t1->direction, d2) < 2 && trig_angles_acute(t2->direction, d1) < 2)
611                 return TR_DIR_LT;
612         return TR_NONE;
613 }
614
615 static int get_scale_dir(const struct Touch* t1,
616                         const struct Touch* t2)
617 {
618         double v;
619         if (trig_angles_acute(t1->direction, t2->direction) >= 2) {
620                 v = trig_direction(t2->x - t1->x, t2->y - t1->y);
621                 if (trig_angles_acute(v, t1->direction) < 2)
622                         return TR_DIR_DN;
623                 else
624                         return TR_DIR_UP;
625         }
626         return TR_NONE;
627 }
628
629 static int get_swipe_dir(const struct Touch* t1,
630                         const struct Touch* t2,
631                         const struct Touch* t3)
632 {
633         double angles[3];
634         angles[0] = t1->direction;
635         angles[1] = t2->direction;
636         angles[2] = t3->direction;
637         return trig_generalize(trig_angles_avg(angles, 3));
638 }
639
640 static int get_swipe4_dir(const struct Touch* t1,
641                         const struct Touch* t2,
642                         const struct Touch* t3,
643                         const struct Touch* t4)
644 {
645         double angles[4];
646         angles[0] = t1->direction;
647         angles[1] = t2->direction;
648         angles[2] = t3->direction;
649         angles[3] = t4->direction;
650         return trig_generalize(trig_angles_avg(angles, 4));
651 }
652
653 static void moving_update(struct Gestures* gs,
654                         const struct MConfig* cfg,
655                         struct MTState* ms)
656 {
657         int i, count, btn_count, dx, dy, dir;
658         double dist;
659         struct Touch* touches[4];
660         count = btn_count = 0;
661         dx = dy = 0;
662         dir = 0;
663
664         // Reset movement.
665         gs->move_dx = 0;
666         gs->move_dy = 0;
667
668         // Count touches and aggregate touch movements.
669         foreach_bit(i, ms->touch_used) {
670                 if (GETBIT(ms->touch[i].state, MT_INVALID))
671                         continue;
672                 else if (GETBIT(ms->touch[i].flags, GS_BUTTON)) {
673                         btn_count++;
674                         dx += ms->touch[i].dx;
675                         dy += ms->touch[i].dy;
676                 }
677                 else if (!GETBIT(ms->touch[i].flags, GS_TAP)) {
678                         if (count < 4)
679                                 touches[count++] = &ms->touch[i];
680                 }
681         }
682
683         // Determine gesture type.
684         if (count == 0) {
685                 if (btn_count >= 1 && cfg->trackpad_disable < 2)
686                         trigger_move(gs, cfg, dx, dy);
687                 else if (btn_count < 1)
688                         trigger_reset(gs);
689         }
690         else if (count == 1 && cfg->trackpad_disable < 2) {
691                 dx += touches[0]->dx;
692                 dy += touches[0]->dy;
693                 trigger_move(gs, cfg, dx, dy);
694         }
695         else if (count == 2 && cfg->trackpad_disable < 1) {
696                 // scroll, scale, or rotate
697                 if ((dir = get_scroll_dir(touches[0], touches[1])) != TR_NONE) {
698                         dist = hypot(
699                                 touches[0]->dx + touches[1]->dx,
700                                 touches[0]->dy + touches[1]->dy);
701                         trigger_scroll(gs, cfg, dist/2, dir);
702                 }
703                 else if ((dir = get_rotate_dir(touches[0], touches[1])) != TR_NONE) {
704                         dist = ABSVAL(hypot(touches[0]->dx, touches[0]->dy)) +
705                                 ABSVAL(hypot(touches[1]->dx, touches[1]->dy));
706                         trigger_rotate(gs, cfg, dist/2, dir);
707                 }
708                 else if ((dir = get_scale_dir(touches[0], touches[1])) != TR_NONE) {
709                         dist = ABSVAL(hypot(touches[0]->dx, touches[0]->dy)) +
710                                 ABSVAL(hypot(touches[1]->dx, touches[1]->dy));
711                         trigger_scale(gs, cfg, dist/2, dir);
712                 }
713         }
714         else if (count == 3 && cfg->trackpad_disable < 1) {
715                 if ((dir = get_swipe_dir(touches[0], touches[1], touches[2])) != TR_NONE) {
716                         dist = hypot(
717                                 touches[0]->dx + touches[1]->dx + touches[2]->dx,
718                                 touches[0]->dy + touches[1]->dy + touches[2]->dy);
719                         trigger_swipe(gs, cfg, dist/3, dir, 0);
720                 }
721         }
722         else if (count == 4 && cfg->trackpad_disable < 1) {
723                 if ((dir = get_swipe4_dir(touches[0], touches[1], touches[2], touches[3])) != TR_NONE) {
724                         dist = hypot(
725                                 touches[0]->dx + touches[1]->dx + touches[2]->dx + touches[3]->dx,
726                                 touches[0]->dy + touches[1]->dy + touches[2]->dy + touches[3]->dy);
727                         trigger_swipe(gs, cfg, dist/4, dir, 1);
728                 }
729         }
730 }
731
732 static void dragging_update(struct Gestures* gs)
733 {
734         if (gs->move_drag == GS_DRAG_READY && timercmp(&gs->time, &gs->move_drag_expire, >)) {
735 #ifdef DEBUG_GESTURES
736                 xf86Msg(X_INFO, "dragging_update: drag expired\n");
737 #endif
738                 trigger_drag_stop(gs, 1);
739         }
740 }
741
742 static void delayed_update(struct Gestures* gs)
743 {
744         struct timeval epoch;
745         timerclear(&epoch);
746
747         if (timercmp(&gs->button_delayed_time, &epoch, ==))
748                 return;
749
750         if (!timercmp(&gs->time, &gs->button_delayed_time, <)) {
751 #ifdef DEBUG_GESTURES
752                 xf86Msg(X_INFO, "delayed_update: %d delay expired, triggering up\n", gs->button_delayed);
753 #endif
754                 trigger_button_up(gs, gs->button_delayed);
755                 gs->button_delayed = 0;
756                 timerclear(&gs->button_delayed_time);
757                 timerclear(&gs->button_delayed_delta);
758         }
759         else {
760                 timersub(&gs->button_delayed_time, &gs->time, &gs->button_delayed_delta);
761         }
762 }
763
764 void gestures_init(struct MTouch* mt)
765 {
766         memset(&mt->gs, 0, sizeof(struct Gestures));
767 }
768
769 void gestures_extract(struct MTouch* mt)
770 {
771         timersub(&mt->hs.evtime, &mt->gs.time, &mt->gs.dt);
772         timercp(&mt->gs.time, &mt->hs.evtime);
773
774         dragging_update(&mt->gs);
775         buttons_update(&mt->gs, &mt->cfg, &mt->hs, &mt->state);
776         tapping_update(&mt->gs, &mt->cfg, &mt->state);
777         moving_update(&mt->gs, &mt->cfg, &mt->state);
778         delayed_update(&mt->gs);
779 }
780
781 static int gestures_sleep(struct MTouch* mt, const struct timeval* sleep)
782 {
783         if (mtdev_empty(&mt->dev)) {
784                 struct timeval now;
785                 mtdev_idle(&mt->dev, mt->fd, timertoms(sleep));
786                 microtime(&now);
787                 timersub(&now, &mt->gs.time, &mt->gs.dt);
788                 timercp(&mt->gs.time, &now);
789                 return 1;
790         }
791         return 0;
792 }
793
794 int gestures_delayed(struct MTouch* mt)
795 {
796         struct Gestures* gs = &mt->gs;
797         struct timeval epoch;
798         timerclear(&epoch);
799
800         if (timercmp(&gs->button_delayed_time, &epoch, >)) {
801                 if (gestures_sleep(mt, &gs->button_delayed_delta)) {
802 #ifdef DEBUG_GESTURES
803                         xf86Msg(X_INFO, "gestures_delayed: %d up, timer expired\n", gs->button_delayed);
804 #endif
805                         trigger_button_up(gs, gs->button_delayed);
806                         gs->move_dx = 0;
807                         gs->move_dy = 0;
808                         gs->button_delayed = 0;
809                         timerclear(&gs->button_delayed_time);
810                         timerclear(&gs->button_delayed_delta);
811                         return 1;
812                 }
813         }
814         return 0;
815 }
816