File: skip.c

package info (click to toggle)
tslib 1.22-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: ansic: 18,859; sh: 4,167; makefile: 550
file content (505 lines) | stat: -rw-r--r-- 10,555 bytes parent folder | download | duplicates (3)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 *  tslib/plugins/skip.c
 *
 * (C) 2017 Martin Kepplinger <martin.kepplinger@ginzinger.com> (read_mt())
 * (C) 2008 by Openmoko, Inc.
 * Author: Nelson Castillo
 *
 * This file is placed under the LGPL.  Please see the file
 * COPYING for more details.
 *
 * SPDX-License-Identifier: LGPL-2.1
 *
 *
 * Skip filter for touchscreen values.
 *
 * Problem: With some drivers the first and the last sample is reliable.
 *
 * Solution:
 *
 *  - Skip N points after pressure != 0
 *  - Skip M points before pressure == 0
 *  - Ignore a click if it has less than N + M + 1 points
 *
 * Parameters:
 *
 *  - nhead (means N)
 *  - ntail (means M)
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>

#include "config.h"
#include "tslib.h"
#include "tslib-filter.h"

struct tslib_skip {
	struct tslib_module_info module;

	int nhead;
	int N;
	int *N_mt;

	int ntail;
	int M;
	int *M_mt;
	struct ts_sample *buf;
	struct ts_sample_mt **buf_mt;
	struct ts_sample_mt **cur_mt;
	int slots;
	int sent;
	int *sent_mt;
};

static void reset_skip(struct tslib_skip *s)
{
	s->N = 0;
	s->M = 0;
	s->sent = 0;
}

static void reset_skip_mt(struct tslib_skip *s, int slot)
{
	s->N_mt[slot] = 0;
	s->M_mt[slot] = 0;
	s->sent_mt[slot] = 0;
}

static int skip_read(struct tslib_module_info *info, struct ts_sample *samp,
		     int nr)
{
	struct tslib_skip *skip = (struct tslib_skip *)info;
	int nread = 0;

	while (nread < nr) {
		struct ts_sample cur;

		if (info->next->ops->read(info->next, &cur, 1) < 1)
			return nread;

		/* skip the first N samples */
		if (skip->N < skip->nhead) {
			skip->N++;
			if (cur.pressure == 0)
				reset_skip(skip);
			continue;
		}

		/* We didn't send DOWN -- Ignore UP */
		if (cur.pressure == 0 && skip->sent == 0) {
			reset_skip(skip);
			continue;
		}

		/* Just accept the sample if ntail is zero */
		if (skip->ntail == 0) {
			samp[nread++] = cur;
			skip->sent = 1;
			if (cur.pressure == 0)
				reset_skip(skip);
			continue;
		}

		/* ntail > 0,  Queue current point if we need to */
		if (skip->sent == 0 && skip->M < skip->ntail) {
			skip->buf[skip->M++] = cur;
			continue;
		}

		/* queue full, accept one, queue one */

		if (skip->M >= skip->ntail)
			skip->M = 0;

		if (cur.pressure == 0)
			skip->buf[skip->M].pressure = 0;

		samp[nread++] = skip->buf[skip->M];

#ifdef DEBUG
		fprintf(stderr, "SKIP---> (X:%d Y:%d) pressure:%d\n",
			skip->buf[skip->M].x, skip->buf[skip->M].y,
			skip->buf[skip->M].pressure);
#endif

		if (cur.pressure == 0) {
			reset_skip(skip);
		} else {
			skip->buf[skip->M++] = cur;
			skip->sent = 1;
		}
	}
	return nread;
}

static int skip_read_mt(struct tslib_module_info *info,
			struct ts_sample_mt **samp, int max_slots, int nr)
{
	struct tslib_skip *skip = (struct tslib_skip *)info;
	int nread = 0;
	int i, j;
	int ret = 0;
	int count = 0;
	int count_current = 0;

	if (skip->cur_mt == NULL || max_slots > skip->slots) {
		if (skip->cur_mt) {
			if (skip->cur_mt[0])
				free(skip->cur_mt[0]);

			free(skip->cur_mt);
		}

		skip->cur_mt = malloc(sizeof(struct ts_sample_mt *));
		if (!skip->cur_mt)
			return -ENOMEM;

		skip->cur_mt[0] = calloc(max_slots,
					 sizeof(struct ts_sample_mt));
		if (!skip->cur_mt[0]) {
			if (skip->cur_mt)
				free(skip->cur_mt);

			return -ENOMEM;
		}

		skip->slots = max_slots;
	}
	memset(skip->cur_mt[0], 0, max_slots * sizeof(struct ts_sample_mt));

	if (skip->slots < max_slots || skip->buf_mt == NULL) {
		if (skip->buf_mt) {
			for (i = 0; i < skip->ntail; i++) {
				if (skip->buf_mt[i])
					free(skip->buf_mt[i]);
			}
			free(skip->buf_mt);
		}

		skip->buf_mt = malloc(skip->ntail *
				      sizeof(struct ts_sample_mt *));
		if (!skip->buf_mt) {
			free(skip->cur_mt[0]);
			free(skip->cur_mt);
			return -ENOMEM;
		}

		for (i = 0; i < skip->ntail; i++) {
			skip->buf_mt[i] = calloc(max_slots,
						 sizeof(struct ts_sample_mt));
			if (!skip->buf_mt[i]) {
				for (j = 0; j < i; j++)
					free(skip->buf_mt[j]);
				free(skip->buf_mt);
				free(skip->cur_mt[0]);
				free(skip->cur_mt);
				return -ENOMEM;
			}
		}

		skip->N_mt = calloc(max_slots, sizeof(int));
		if (!skip->N_mt) {
			for (i = 0; i < skip->ntail; i++)
				free(skip->buf_mt[i]);
			free(skip->buf_mt);
			free(skip->cur_mt[0]);
			free(skip->cur_mt);
			return -ENOMEM;
		}

		skip->M_mt = calloc(max_slots, sizeof(int));
		if (!skip->M_mt) {
			free(skip->N_mt);
			for (i = 0; i < skip->ntail; i++)
				free(skip->buf_mt[i]);
			free(skip->buf_mt);
			free(skip->cur_mt[0]);
			free(skip->cur_mt);
			return -ENOMEM;
		}

		skip->sent_mt = calloc(max_slots, sizeof(int));
		if (!skip->sent_mt) {
			free(skip->N_mt);
			free(skip->M_mt);
			for (i = 0; i < skip->ntail; i++)
				free(skip->buf_mt[i]);
			free(skip->buf_mt);
			free(skip->cur_mt[0]);
			free(skip->cur_mt);
			return -ENOMEM;
		}

		skip->slots = max_slots;
	}

	if (!info->next->ops->read_mt)
		return -ENOSYS;

	ret = info->next->ops->read_mt(info->next, samp, max_slots, nr);
	if (ret < 0)
		return ret;

#ifdef DEBUG
	printf("SKIP: read %d samples (%d slots)\n",
	       ret, max_slots);
#endif
	while (nread < ret) {
		count_current = 0;
		memcpy(skip->cur_mt[0], samp[count],
		       max_slots * sizeof(struct ts_sample_mt));
		for (i = 0; i < max_slots; i++) {
			if (!(skip->cur_mt[0][i].valid & TSLIB_MT_VALID))
				continue;

			/* skip the first N samples */
			if (skip->N_mt[i] < skip->nhead) {
			#ifdef DEBUG
				printf("SKIP: (slot %d) skip %d of %d samples\n",
				       i, skip->N_mt[i] + 1, skip->nhead);
			#endif
				skip->N_mt[i]++;
				if (skip->cur_mt[0][i].pressure == 0) {
					reset_skip_mt(skip, i);
				}

				skip->cur_mt[0][i].valid = 0;
				samp[count][i].valid = 0;
				continue;
			}

			/* We didn't send DOWN -- Ignore UP */
			if (skip->cur_mt[0][i].pressure == 0 &&
			    skip->sent_mt[i] == 0) {
			#ifdef DEBUG
				fprintf(stderr, "SKIP: (Slot %d) ignore up\n", i);
			#endif
				reset_skip_mt(skip, i);
				continue;
			}

			/* Just accept the sample if ntail is zero */
			if (skip->ntail == 0) {

				if (skip->sent_mt[i] == 0) {
					skip->cur_mt[0][i].pen_down = 1;
					skip->cur_mt[0][i].valid |= TSLIB_MT_VALID;
				}

				memcpy(&samp[count][i], &skip->cur_mt[0][i],
				       sizeof(struct ts_sample_mt));

				if (count_current == 0) {
					nread++;
				}
				count_current++;

				skip->sent_mt[i] = 1;
				if (skip->cur_mt[0][i].pressure == 0)
					reset_skip_mt(skip, i);
			#ifdef DEBUG
				fprintf(stderr,
					"SKIP: ntail 0 - sample accepted\n");
			#endif
				continue;
			}

			/* ntail > 0,  Queue current point if we need to */
			if (skip->sent_mt[i] == 0 && skip->M_mt[i] < skip->ntail) {
				skip->cur_mt[0][i].pen_down = 1;
				skip->cur_mt[0][i].valid |= TSLIB_MT_VALID;
				samp[count][i].valid = 0;

			#ifdef DEBUG
				fprintf(stderr,
					"SKIP: queue one sample to %d\n",
					skip->M_mt[i]);
			#endif
				memcpy(&skip->buf_mt[skip->M_mt[i]][i],
				       &skip->cur_mt[0][i],
				       sizeof(struct ts_sample_mt));
				skip->M_mt[i]++;
				continue;
			}
			/* queue full, accept one, queue one */
			if (skip->M_mt[i] >= skip->ntail)
				skip->M_mt[i] = 0;


			if (skip->cur_mt[0][i].pressure == 0) {
				skip->buf_mt[skip->M_mt[i]][i].pressure = 0;
				skip->buf_mt[skip->M_mt[i]][i].pen_down = 0;
				skip->buf_mt[skip->M_mt[i]][i].tracking_id = -1;
				skip->buf_mt[skip->M_mt[i]][i].valid |= TSLIB_MT_VALID;
			}

			memcpy(&samp[count][i], &skip->buf_mt[skip->M_mt[i]][i],
			       sizeof(struct ts_sample_mt));

	#ifdef DEBUG
			fprintf(stderr,
				"SKIP: (Slot %d) X:%4d Y:%4d pressure:%d btn_touch:%d\n",
				skip->buf_mt[skip->M_mt[i]][i].slot,
				skip->buf_mt[skip->M_mt[i]][i].x,
				skip->buf_mt[skip->M_mt[i]][i].y,
				skip->buf_mt[skip->M_mt[i]][i].pressure,
				skip->buf_mt[skip->M_mt[i]][i].pen_down);

	#endif
			if (count_current == 0) {
				nread++;
			}
			count_current++;

			if (skip->cur_mt[0][i].pressure == 0) {
				reset_skip_mt(skip, i);
			} else {
				memcpy(&skip->buf_mt[skip->M_mt[i]][i],
				       &skip->cur_mt[0][i],
				       sizeof(struct ts_sample_mt));


			#ifdef DEBUG
				fprintf(stderr,
					"SKIP: accept and queue one sample (slot %d) to %d\n", i, skip->M_mt[i]);
			#endif
				skip->sent_mt[i] = 1;
				skip->M_mt[i]++;
			}

		}
		count++;
		if (count == ret)
			break;


	}

	return nread;
}

static int skip_fini(struct tslib_module_info *info)
{
	struct tslib_skip *skip = (struct tslib_skip *)info;
	int i;

	if (skip->N_mt)
		free(skip->N_mt);

	if (skip->M_mt)
		free(skip->M_mt);

	if (skip->sent_mt)
		free(skip->sent_mt);

	if (skip->buf)
		free(skip->buf);

	if (skip->buf_mt) {
		for (i = 0; i < skip->ntail; i++) {
			if (skip->buf_mt[i])
				free(skip->buf_mt[i]);
		}
		free(skip->buf_mt);
	}

	if (skip->buf_mt)

	if (skip->cur_mt && skip->cur_mt[0])
		free(skip->cur_mt[0]);

	if (skip->cur_mt)
		free(skip->cur_mt);

	free(info);

	return 0;
}

static const struct tslib_ops skip_ops = {
	.read		= skip_read,
	.read_mt	= skip_read_mt,
	.fini		= skip_fini,
};

static int skip_opt(struct tslib_module_info *inf, char *str, void *data)
{
	struct tslib_skip *skip = (struct tslib_skip *)inf;
	unsigned long v;
	int err = errno;

	v = strtoul(str, NULL, 0);

	if (v == ULONG_MAX && errno == ERANGE)
		return -1;

	errno = err;

	switch ((int)(intptr_t)data) {
	case 1:
		skip->nhead = v;
		break;

	case 2:
		skip->ntail = v;
		break;

	default:
		return -1;
	}
	return 0;
}

static const struct tslib_vars skip_vars[] = {
	{ "nhead",      (void *)1, skip_opt },
	{ "ntail",      (void *)2, skip_opt },
};

#define NR_VARS (sizeof(skip_vars) / sizeof(skip_vars[0]))

TSAPI struct tslib_module_info *skip_mod_init(__attribute__ ((unused)) struct tsdev *dev,
					      const char *params)
{
	struct tslib_skip *skip;

	skip = malloc(sizeof(struct tslib_skip));
	if (skip == NULL)
		return NULL;

	memset(skip, 0, sizeof(struct tslib_skip));
	skip->module.ops = &skip_ops;

	skip->nhead = 1; /* by default remove the first */
	skip->ntail = 1; /* by default remove the last */
	skip->buf = NULL;
	skip->buf_mt = NULL;
	skip->cur_mt = NULL;
	skip->slots = 0;
	skip->N_mt = NULL;
	skip->M_mt = NULL;
	skip->sent_mt = NULL;

	reset_skip(skip);

	if (tslib_parse_vars(&skip->module, skip_vars, NR_VARS, params)) {
		free(skip);
		return NULL;
	}

	if (skip->ntail &&
	    !(skip->buf = malloc(sizeof(struct ts_sample) * skip->ntail))) {
		free(skip);
		return NULL;
	}

	return &skip->module;
}

#ifndef TSLIB_STATIC_SKIP_MODULE
	TSLIB_MODULE_INIT(skip_mod_init);
#endif