aboutsummaryrefslogtreecommitdiffstats
path: root/src/gestures.c
blob: 977655ca8f18154bfacaee7dde00e137d80fce1f (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***************************************************************************
 *
 * 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"

static const int FINGER_THUMB_MS = 400;
static const int BUTTON_HOLD_MS = 200;

/**
 * extract_buttons
 *
 * Set the button gesture.
 *
 * Reset memory after use.
 *
 */
static void extract_buttons(struct Gestures *gs, struct MTouch* mt)
{
	unsigned btdata = mt->state.button & BITONES(DIM_BUTTON);
	int npoint = bitcount(mt->mem.pointing);
	if (mt->state.button == BITMASK(MT_BUTTON_LEFT)) {
		if (npoint == 2)
			btdata = BITMASK(MT_BUTTON_RIGHT);
		if (npoint == 3)
			btdata = BITMASK(MT_BUTTON_MIDDLE);
	}
	if (mt->state.button != mt->prev_state.button) {
		gs->btmask = (btdata ^ mt->mem.btdata) & BITONES(DIM_BUTTON);
		gs->btdata = btdata;
		mt->mem.btdata = btdata;
	}
	if (gs->btmask) {
		mt_delay_movement(mt, BUTTON_HOLD_MS);
		SETBIT(gs->type, GS_BUTTON);
	}
}

/**
 * extract_movement
 *
 * Set the movement gesture.
 *
 * Reset memory after use.
 *
 */
static void extract_movement(struct Gestures *gs, struct MTouch* mt)
{
	int npoint = bitcount(mt->mem.pointing);
	int nmove = bitcount(mt->mem.moving);
	int i;
	float xm[DIM_FINGER], ym[DIM_FINGER];
	float xmove = 0, ymove = 0;

	if (!nmove || nmove != npoint)
		return;

	foreach_bit(i, mt->mem.moving) {
		xm[i] = mt->mem.dx[i];
		ym[i] = mt->mem.dy[i];
		mt->mem.dx[i] = 0;
		mt->mem.dy[i] = 0;
		xmove += xm[i];
		ymove += ym[i];
	}
	xmove /= nmove;
	ymove /= nmove;

	if (nmove == 1) {
		if (mt->mem.moving & mt->mem.thumb) {
			mt_skip_movement(mt, FINGER_THUMB_MS);
			return;
		}
		gs->dx = xmove;
		gs->dy = ymove;
		if (gs->dx || gs->dy)
			SETBIT(gs->type, GS_MOVE);
	} else {
		if (mt->mem.moving & mt->mem.thumb) {
			mt_skip_movement(mt, FINGER_THUMB_MS);
			return;
		}
		gs->dx = xmove;
		gs->dy = ymove;
		if (abs(gs->dx) > abs(gs->dy)) {
			if (nmove == 2)
				SETBIT(gs->type, GS_HSCROLL);
			if (nmove == 3)
				SETBIT(gs->type, GS_HSWIPE);
		}
		if (abs(gs->dy) > abs(gs->dx)) {
			if (nmove == 2)
				SETBIT(gs->type, GS_VSCROLL);
			if (nmove == 3)
				SETBIT(gs->type, GS_VSWIPE);
		}
	}
}

/**
 * extract_gestures
 *
 * Extract the gestures.
 *
 * Reset memory after use.
 *
 */
void extract_gestures(struct Gestures *gs, struct MTouch* mt)
{
	memset(gs, 0, sizeof(struct Gestures));
	gs->same_fingers = mt->mem.same;
	extract_buttons(gs, mt);
	extract_movement(gs, mt);
	mt->prev_state = mt->state;
}