aboutsummaryrefslogtreecommitdiffstats
path: root/src/gestures.c
blob: 44b96132a3ef23cb8dca3b5928d983dcae4b7dc1 (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
/***************************************************************************
 *
 * Multitouch X driver
 * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
 *
 * Gestures
 * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
 * Copyright (C) 2010 Arturo Castro <mail@arturocastro.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **************************************************************************/

#include "gestures.h"

/* timer for cursor stability on finger touch/release */
static const int AFTER_FINGER_CHANGE_MS = 70;

static void extract_movement(struct Gestures *gs, struct MTouch* mt)
{
	const struct FingerState *prev[DIM_FINGER];
	const struct FingerState *f = mt->state.finger;
	int same_fingers, i, x = 0, y = 0;

	if (mt->state.nfinger == 0)
		return;

	same_fingers = mt->state.nfinger == mt->prev_state.nfinger;
	for (i = 0; i < mt->state.nfinger; i++) {
		prev[i] = find_finger(&mt->prev_state, mt->state.finger[i].id);
		same_fingers = same_fingers && prev[i];
	}

	for (i = 0; i < mt->state.nfinger; i++) {
		x += f[i].hw.position_x;
		y += f[i].hw.position_y;
	}
	x /= mt->state.nfinger;
	y /= mt->state.nfinger;

	if (!same_fingers) {
		mt->mem.move_time = mt->state.evtime + AFTER_FINGER_CHANGE_MS;
	} else if (mt->state.evtime >= mt->mem.move_time) {
		gs->dx = x - mt->mem.move_x;
		gs->dy = y - mt->mem.move_y;
	} else {
		/* accumulate all movement during delay */
		return;
	}

	mt->mem.move_x = x;
	mt->mem.move_y = y;
}

static void extract_buttons(struct Gestures *gs, struct MTouch* mt)
{
	unsigned btdata = mt->state.button & BITONES(DIM_BUTTON);
	if (mt->state.button == BITMASK(MT_BUTTON_LEFT)) {
		if (mt->state.nfinger == 2)
			btdata = BITMASK(MT_BUTTON_RIGHT);
		if (mt->state.nfinger == 3)
			btdata = BITMASK(MT_BUTTON_MIDDLE);
	}
	gs->btmask = (btdata ^ mt->mem.btdata) & BITONES(DIM_BUTTON);
	gs->btdata = btdata;
	mt->mem.btdata = btdata;
}

/******************************************************/

static void extract_type(struct Gestures *gs, struct MTouch* mt)
{
	if (gs->dx || gs->dy) {
		if (mt->state.nfinger == 1)
			SETBIT(gs->type, GS_MOVE);
		if (mt->state.nfinger == 2)
			SETBIT(gs->type, GS_VSCROLL);
		if (mt->state.nfinger == 3)
			SETBIT(gs->type, GS_HSCROLL);
	}
}

/******************************************************/

void extract_gestures(struct Gestures *gs, struct MTouch* mt)
{
	memset(gs, 0, sizeof(struct Gestures));
	extract_movement(gs, mt);
	extract_buttons(gs, mt);
	extract_type(gs, mt);
	mt->prev_state = mt->state;
}