summaryrefslogtreecommitdiff
path: root/src/wcmTouchFilter.c
blob: 088106476d75971a48f1b58d110c4ae1e8053c28 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright 2009 by Ping Cheng, Wacom Technology. <pingc@wacom.com>
 *
 * 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 Lesser 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "xf86Wacom.h"
#include <math.h>

/* Defines for 2FC Gesture */
#define WACOM_DIST_IN_POINT         300
#define WACOM_APART_IN_POINT        350
#define WACOM_MOTION_IN_POINT        50
#define WACOM_PARA_MOTION_IN_POINT   50
#define WACOM_DOWN_TIME_IN_MS       800
#define WACOM_TIME_BETWEEN_IN_MS    400
#define WACOM_HORIZ_ALLOWED           1
#define WACOM_VERT_ALLOWED            2

#define GESTURE_TAP_MODE              1
#define GESTURE_SCROLL_MODE           2
#define GESTURE_ZOOM_MODE             4

#define WCM_SCROLL_UP                 5	/* vertical up */
#define WCM_SCROLL_DOWN               4	/* vertical down */


/* Defines for Tap Add-a-Finger to Click */
#define WACOM_TAP_TIME_IN_MS        150

void xf86WcmFingerTapToClick(WacomCommonPtr common);

extern void xf86WcmRotateCoordinates(LocalDevicePtr local, int x, int y);
extern void emitKeysym (DeviceIntPtr keydev, int keysym, int state);

static void xf86WcmFingerScroll(WacomDevicePtr priv);
static void xf86WcmFingerZoom(WacomDevicePtr priv);

static double touchDistance(WacomDeviceState ds0, WacomDeviceState ds1)
{
	int xDelta = ds0.x - ds1.x;
	int yDelta = ds0.y - ds1.y;
	double distance = sqrt((double)(xDelta*xDelta + yDelta*yDelta));
	return distance;
}

static Bool pointsInLine(WacomDeviceState ds0, WacomDeviceState ds1,
				int *direction)
{
	Bool ret = FALSE;

	if (*direction == 0)
	{
		if (abs(ds0.x - ds1.x) < WACOM_PARA_MOTION_IN_POINT)
		{
			*direction = WACOM_VERT_ALLOWED;
			ret = TRUE;
		}
		else if (abs(ds0.y - ds1.y) < WACOM_PARA_MOTION_IN_POINT)
		{
			*direction = WACOM_HORIZ_ALLOWED;
			ret = TRUE;
		}
	}
	else if (*direction == WACOM_HORIZ_ALLOWED)
	{
		if (abs(ds0.y - ds1.y) < WACOM_PARA_MOTION_IN_POINT)
			ret = TRUE;
	}
	else if (*direction == WACOM_VERT_ALLOWED)
	{
		if (abs(ds0.x - ds1.x) < WACOM_PARA_MOTION_IN_POINT)
			ret = TRUE;
	}
	return ret;
}

static Bool pointsInLineAfter(int p1, int p2)
{
	Bool ret = FALSE;

	if (abs(p1 - p2) < WACOM_PARA_MOTION_IN_POINT)
		ret = TRUE;

	return ret;
}

static void xf86WcmSwitchLeftClick(WacomDevicePtr priv)
{
	WacomCommonPtr common = priv->common;

	if (common->wcmGestureMode)
	{
		/* send button one up */
		xf86PostButtonEvent(priv->local->dev,
					priv->flags & ABSOLUTE_FLAG,
					1,0,0,priv->naxes, priv->oldX,
					priv->oldY,0,0,0,0);
		priv->oldButtons = 0;
	}
}

/*****************************************************************************
 *   translate second finger tap to right click
 ****************************************************************************/

void xf86WcmFingerTapToClick(WacomCommonPtr common)
{
	WacomDevicePtr priv = common->wcmDevices;
	WacomChannelPtr firstChannel = common->wcmChannel;
	WacomChannelPtr secondChannel = common->wcmChannel + 1;
	WacomDeviceState ds[2] = { firstChannel->valid.states[0],
				   secondChannel->valid.states[0] };
	WacomDeviceState dsLast[2] = { firstChannel->valid.states[1],
					secondChannel->valid.states[1] };
	int direction = 0;

	DBG(10, priv->debugLevel, xf86Msg(X_INFO, "xf86WcmFingerTapToClick \n"));

	/* skip initial second finger event */
	if (!dsLast[1].proximity)
		goto skipGesture;

	if (!IsTouch(priv))
	{
		/* go through the shared port */
		for (; priv; priv = priv->next)
			if (IsTouch(priv))
				break;
	}

	if (priv)  /* found the first finger */
	{
		/* allow only second finger tap */
		if ((dsLast[0].sample < dsLast[1].sample) && ((GetTimeInMillis() -
						dsLast[1].sample) <= WACOM_TAP_TIME_IN_MS))
		{
			/* send right click when second finger taps within WACOM_TAP_TIMEms
			 * and both fingers stay within WACOM_DIST */
			if (!ds[1].proximity && dsLast[1].proximity)
			{
				if (touchDistance(ds[0], dsLast[1]) <= WACOM_DIST_IN_POINT)
				{
					/* send left up before sending right down */
					if (!common->wcmGestureMode)
					{
						common->wcmGestureMode = GESTURE_TAP_MODE;
						xf86WcmSwitchLeftClick(priv);
					}

					/* right button down */
					xf86PostButtonEvent(priv->local->dev,
							priv->flags & ABSOLUTE_FLAG,
							3,1,0,priv->naxes, priv->oldX,
							priv->oldY,0,0,0,0);
					/* right button up */
					xf86PostButtonEvent(priv->local->dev,
							priv->flags & ABSOLUTE_FLAG,
							3,0,0,priv->naxes, priv->oldX,
							priv->oldY,0,0,0,0);
				}
			}
		}
		else if ((WACOM_TAP_TIME_IN_MS < (GetTimeInMillis() - dsLast[0].sample))
				&& (WACOM_TAP_TIME_IN_MS < (GetTimeInMillis() - dsLast[1].sample))
				&& ds[0].proximity && ds[1].proximity)
		{
			if (abs(touchDistance(ds[0], ds[1])) >= WACOM_APART_IN_POINT &&
					common->wcmGestureMode != GESTURE_TAP_MODE &&
					common->wcmGestureMode != GESTURE_SCROLL_MODE)
			{
				/* fingers moved apart more than WACOM_APART_IN_POINT
				 * zoom mode is entered */
				if (!common->wcmGestureMode)
				{
					common->wcmGestureMode = GESTURE_ZOOM_MODE;
					xf86WcmSwitchLeftClick(priv);
				}
				xf86WcmFingerZoom(priv);
			}

			if ( pointsInLine(ds[0], dsLast[0], &direction) &&
					pointsInLine(ds[1], dsLast[1], &direction) &&
					common->wcmGestureMode != GESTURE_ZOOM_MODE &&
					common->wcmGestureMode != GESTURE_TAP_MODE)
			{
				/* send scroll event when both fingers move in
				 * the same direction */
				if (!common->wcmGestureMode)
				{
					common->wcmGestureMode = GESTURE_SCROLL_MODE;
					xf86WcmSwitchLeftClick(priv);
				}
				xf86WcmFingerScroll(priv);
			}
		}
	}
	else
		/* this should never happen */
		xf86Msg(X_ERROR, "WACOM: No touch device found for %s \n", common->wcmDevice);

skipGesture:
	/* keep the initial in-prox time */
	ds[0].sample = dsLast[0].sample;
	ds[1].sample = dsLast[1].sample;

	/* keep the initial states for both fingers */
	if ( !(common->wcmGestureMode && (GESTURE_SCROLL_MODE | GESTURE_ZOOM_MODE))
			&& ds[0].proximity && ds[1].proximity)
	{
		common->wcmGestureState[0] = ds[0];
		common->wcmGestureState[1] = ds[1];
	}
}

static void xf86WcmSendVerticalScrollEvent(WacomDevicePtr priv,
		int dist, int up, int dn)
{
	int i = 0;

	for (i=0; i<(int)(((double)abs(dist)/
			  (double)WACOM_MOTION_IN_POINT) + 0.5); i++)
	{
		if (dist > 0)
		{
			/* button down */
			xf86PostButtonEvent(priv->local->dev,
					priv->flags & ABSOLUTE_FLAG,
					up,1,0,priv->naxes, priv->oldX,
					priv->oldY,0,0,0,0);
			/* button up */
			xf86PostButtonEvent(priv->local->dev,
					priv->flags & ABSOLUTE_FLAG,
					up,0,0,priv->naxes, priv->oldX,
					priv->oldY,0,0,0,0);
		}
		else
		{
			/* button down */
			xf86PostButtonEvent(priv->local->dev,
					priv->flags & ABSOLUTE_FLAG,
					dn,1,0,priv->naxes, priv->oldX,
					priv->oldY,0,0,0,0);
			/* button up */
			xf86PostButtonEvent(priv->local->dev,
					priv->flags & ABSOLUTE_FLAG,
					dn,0,0,priv->naxes, priv->oldX,
					priv->oldY,0,0,0,0);
		}
	}
}

static void xf86WcmSendHorizontalScrollEvent(WacomDevicePtr priv,
					     int dist, int left, int right)
{
	int i = 0;

	for (i=0; i<(int)(((double)abs(dist)/
			   (double)WACOM_MOTION_IN_POINT) + 0.5); i++)
	{
		if (dist > 0)
		{
			emitKeysym (priv->local->dev, left, 1);
			emitKeysym (priv->local->dev, left, 0);
		}
		else
		{
			emitKeysym (priv->local->dev, right, 1);
			emitKeysym (priv->local->dev, right, 0);
		}
	}
}

static void xf86WcmFingerScroll(WacomDevicePtr priv)
{
	WacomCommonPtr common = priv->common;
	WacomChannelPtr firstChannel = common->wcmChannel;
	WacomChannelPtr secondChannel = common->wcmChannel + 1;
	WacomDeviceState ds[2] = { firstChannel->valid.states[0],
		secondChannel->valid.states[0] };
	WacomDeviceState dsLast[2] = { firstChannel->valid.states[1],
		secondChannel->valid.states[1] };
	int midPoint_new = 0;
	int midPoint_old = 0;
	int i = 0, dist =0;
	int gesture = 0;
	WacomFilterState filterd;  /* borrow this struct */

	DBG(10, priv->debugLevel, xf86Msg(X_INFO, "xf86WcmFingerScroll \n"));

	/* initialize the points so we can rotate them */
	filterd.x[0] = ds[0].x;
	filterd.y[0] = ds[0].y;
	filterd.x[1] = ds[1].x;
	filterd.y[1] = ds[1].y;
	filterd.x[2] = common->wcmGestureState[0].x;
	filterd.y[2] = common->wcmGestureState[0].y;
	filterd.x[3] = common->wcmGestureState[1].x;
	filterd.y[3] = common->wcmGestureState[1].y;
	filterd.x[4] = dsLast[0].x;
	filterd.y[4] = dsLast[0].y;
	filterd.x[5] = dsLast[1].x;
	filterd.y[5] = dsLast[1].y;

	/* rotate the coordinates first */
	for (i=0; i<6; i++)
		xf86WcmRotateCoordinates(priv->local, filterd.x[i], filterd.y[i]);

	/* check vertical direction */
	midPoint_old = (((double)filterd.x[4] + (double)filterd.x[5]) / 2.);
	midPoint_new = (((double)filterd.x[0] + (double)filterd.x[1]) / 2.);
	if (pointsInLineAfter(midPoint_old, midPoint_new))
	{
		midPoint_old = (((double)filterd.y[2] + (double)filterd.y[3]) / 2.);
		midPoint_new = (((double)filterd.y[0] + (double)filterd.y[1]) / 2.);
		dist = midPoint_old - midPoint_new;

		if (abs(dist) > WACOM_PARA_MOTION_IN_POINT)
		{
			gesture = 1;
			xf86WcmSendVerticalScrollEvent(priv,  dist,
					WCM_SCROLL_UP, WCM_SCROLL_DOWN);
		}

		/* check horizontal direction */
		if (!gesture)
		{
			midPoint_old = (((double)filterd.y[4] + (double)filterd.y[5]) / 2.);
			midPoint_new = (((double)filterd.y[0] + (double)filterd.y[1]) / 2.);
			if (pointsInLineAfter(midPoint_old, midPoint_new))
			{
				midPoint_old = (((double)filterd.x[2] + (double)filterd.x[3]) / 2.);
				midPoint_new = (((double)filterd.x[0] + (double)filterd.x[1]) / 2.);
				dist = midPoint_old - midPoint_new;

				if (abs(dist) > WACOM_PARA_MOTION_IN_POINT)
				{
					gesture = 1;
					xf86WcmSendHorizontalScrollEvent(priv, dist, XK_Left, XK_Right);
				}
			}
		}
		if (gesture)
		{
			/* reset initial states */
			common->wcmGestureState[0] = ds[0];
			common->wcmGestureState[1] = ds[1];
		}
	}
}

static void xf86WcmFingerZoom(WacomDevicePtr priv)
{
	WacomCommonPtr common = priv->common;
	WacomChannelPtr firstChannel = common->wcmChannel;
	WacomChannelPtr secondChannel = common->wcmChannel + 1;
	WacomDeviceState ds[2] = { firstChannel->valid.states[0],
		secondChannel->valid.states[0] };
	int i = 0;
	int dist = touchDistance(common->wcmGestureState[0],
			common->wcmGestureState[1]);

	DBG(10, priv->debugLevel, xf86Msg(X_INFO, "xf86WcmFingerZoom \n"));

	dist = touchDistance(ds[0], ds[1]) - dist;

	/* zooming? */
	if (abs(dist) > WACOM_MOTION_IN_POINT)
	{
		for (i=0; i<(int)(((double)abs(dist)/
				(double)WACOM_MOTION_IN_POINT) + 0.5); i++)
		{
			emitKeysym (priv->local->dev, XK_Control_L, 1);
			/* zooming in */
			if (dist > 0)
			{
				emitKeysym (priv->local->dev, XK_plus, 1);
				emitKeysym (priv->local->dev, XK_plus, 0);
			}
			else /* zooming out */
			{
				emitKeysym (priv->local->dev, XK_minus, 1);
				emitKeysym (priv->local->dev, XK_minus, 0);
			}
			emitKeysym (priv->local->dev, XK_Control_L, 0);
		}

		/* reset initial states */
		common->wcmGestureState[0] = ds[0];
		common->wcmGestureState[1] = ds[1];
	}
}

/* vim: set noexpandtab shiftwidth=8: */