Skip to content

Commit f35d51b

Browse files
committed
plugins: add a crop filter module
This should simply drop input events for x/y values ouside of the framebuffer (and thus ts_uinput) Min/Max values.
1 parent 900d54f commit f35d51b

File tree

10 files changed

+274
-5
lines changed

10 files changed

+274
-5
lines changed

Android.mk

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,24 @@ LOCAL_MODULE_TAGS := optional
178178
include $(BUILD_SHARED_LIBRARY)
179179

180180

181+
# plugin: crop
182+
include $(CLEAR_VARS)
183+
184+
LOCAL_PRELINK_MODULE := false
185+
186+
LOCAL_SRC_FILES := plugins/crop.c
187+
188+
LOCAL_C_INCLUDES += $(LOCAL_PATH)/src/
189+
190+
LOCAL_SHARED_LIBRARIES := libdl \
191+
libts
192+
193+
LOCAL_MODULE := ts/plugins/crop
194+
LOCAL_MODULE_TAGS := optional
195+
196+
include $(BUILD_SHARED_LIBRARY)
197+
198+
181199
# plugin: evthres
182200
include $(CLEAR_VARS)
183201

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ through the library:
6161
module module module module
6262

6363
### calibrate the touch screen
64-
Calibration is done by the `linear` plugin, which uses its own config file
65-
`/etc/pointercal`. Don't edit this file manually. It is created by the
64+
Calibration is applied by the `linear` plugin, which uses its own config file
65+
`TSLIB_CALIBFILE`. Don't edit this file manually. It is created by the
6666
[`ts_calibrate`](https://manpages.debian.org/unstable/libts0/ts_calibrate.1.en.html) program:
6767

6868
# ts_calibrate
@@ -197,8 +197,7 @@ for example (yet).
197197
### module: linear
198198
Linear scaling - calibration - module, primerily used for conversion of touch
199199
screen co-ordinates to screen co-ordinates. It applies the corrections as
200-
recorded and saved by the [`ts_calibrate`](https://manpages.debian.org/unstable/libts0/ts_calibrate.1.en.html) tool. It's the only module that reads
201-
a configuration file.
200+
recorded and saved by the [`ts_calibrate`](https://manpages.debian.org/unstable/libts0/ts_calibrate.1.en.html) tool.
202201

203202
Parameters (usually not needed):
204203
* `rot`
@@ -382,6 +381,20 @@ Parameters:
382381
Example: `module evthres N=5`
383382

384383

384+
### module: crop
385+
The goal of this filter is to drop input touch events that lie outside
386+
of the Min/Max values for the x/y event codes, i.e. touch points that
387+
might (due to applied filters) lie ouside of the visible framebuffer.
388+
389+
It sends "pen/finger up" for a given slot when said values are outside of
390+
the framebuffer.
391+
392+
It read the framebuffer dimensions from `TSLIB_CALIBFILE` which is
393+
generated and saved by the [`ts_calibrate`](https://manpages.debian.org/unstable/libts0/ts_calibrate.1.en.html) tool.
394+
395+
Example: `module crop`
396+
397+
385398
### module: variance
386399
Variance filter. Tries to do its best in order to filter out random noise
387400
coming from touchscreen ADC's. This is achieved by limiting the sample
@@ -649,7 +662,7 @@ For testing purposes there are tools like [ts_test_mt](#test-the-filtered-input-
649662
too.
650663

651664
#### shipped as part of tslib
652-
* [ts_calibrate](#filter-modules) - graphical calibration tool. Configures the `linear` filter module.
665+
* [ts_calibrate](#filter-modules) - graphical calibration tool. Configures the `linear` and `crop` filter modules.
653666
* [ts_uinput](#use-the-filtered-result-in-your-system-ts_uinput-method) - userspace **evdev** driver for the tslib-filtered samples.
654667

655668
#### third party applications

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ TSLIB_CHECK_MODULE([skip], [yes], [Enable building of skip filter])
106106
TSLIB_CHECK_MODULE([lowpass], [yes], [Enable building of lowpass filter])
107107
TSLIB_CHECK_MODULE([invert], [yes], [Enable building of invert filter])
108108
TSLIB_CHECK_MODULE([variance], [yes], [Enable building of variance filter])
109+
TSLIB_CHECK_MODULE([crop], [yes], [Enable building of crop filter])
109110

110111
# hardware access modules
111112
#########################

doc/ts.conf.5

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ numerator of the smoothing fraction. Default: 0.
227227
\fBD\fR
228228
.sp
229229
denominator of the smoothing fraction. Default: 1.
230+
230231
.RE
231232
.RE
232233
.PP
@@ -259,6 +260,15 @@ Minimum pressure value for a sample to be valid. Default: 1.
259260
.sp
260261
Maximum pressure value for a sample to be valid. Default: INT_MAX.
261262

263+
.RE
264+
.RE
265+
.PP
266+
\fBcrop\fR
267+
.RS 4
268+
Ignore touch points outside of the framebuffer.
269+
.sp
270+
.RS 4
271+
262272
.RE
263273
.RE
264274
.PP

etc/ts.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ module dejitter delta=100
3939

4040
# Uncomment to use ts_calibrate's settings
4141
module linear
42+
43+
# Uncomment to drop events outside of the framebuffer
44+
# module crop

plugins/Makefile.am

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ else
7272
IIR_MODULE =
7373
endif
7474

75+
if ENABLE_CROP_MODULE
76+
CROP_MODULE = crop.la
77+
else
78+
CROP_MODULE =
79+
endif
80+
7581
if ENABLE_EVTHRES_MODULE
7682
EVTHRES_MODULE = evthres.la
7783
else
@@ -197,6 +203,7 @@ pluginexec_LTLIBRARIES = \
197203
$(SKIP_MODULE) \
198204
$(INVERT_MODULE) \
199205
$(IIR_MODULE) \
206+
$(CROP_MODULE) \
200207
$(EVTHRES_MODULE) \
201208
$(LOWPASS_MODULE) \
202209
$(UCB1X00_MODULE) \
@@ -287,6 +294,13 @@ iir_la_LDFLAGS += -no-undefined
287294
endif
288295
iir_la_LIBADD = $(top_builddir)/src/libts.la
289296

297+
crop_la_SOURCES = crop.c
298+
crop_la_LDFLAGS = -module $(LTVSN)
299+
if WINDOWS
300+
crop_la_LDFLAGS += -no-undefined
301+
endif
302+
crop_la_LIBADD = $(top_builddir)/src/libts.la
303+
290304
evthres_la_SOURCES = evthres.c
291305
evthres_la_LDFLAGS = -module $(LTVSN)
292306
if WINDOWS

plugins/crop.c

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* tslib/plugins/crop.c
3+
*
4+
* Copyright (C) 2024 Martin Kepplinger-Novaković
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* SPDX-License-Identifier: LGPL-2.1
20+
*/
21+
#include <errno.h>
22+
#include <stdlib.h>
23+
#include <string.h>
24+
#include <limits.h>
25+
#include <sys/stat.h>
26+
#include <stdint.h>
27+
#include <stdio.h>
28+
29+
#include "config.h"
30+
#include "tslib-private.h"
31+
32+
struct tslib_crop {
33+
struct tslib_module_info module;
34+
int32_t *last_tid;
35+
uint32_t last_pressure;
36+
int a[7];
37+
/* fb res from calibration-time */
38+
uint32_t cal_res_x;
39+
uint32_t cal_res_y;
40+
uint32_t rot;
41+
};
42+
43+
static int crop_read(struct tslib_module_info *info, struct ts_sample *samp,
44+
int nr)
45+
{
46+
struct tslib_crop *crop = (struct tslib_crop *)info;
47+
int ret;
48+
int nread = 0;
49+
50+
while (nread < nr) {
51+
struct ts_sample cur;
52+
53+
ret = info->next->ops->read(info->next, &cur, 1);
54+
if (ret < 0)
55+
return ret;
56+
57+
if (cur.x >= crop->cal_res_x ||
58+
cur.x < 0 ||
59+
cur.y >= crop->cal_res_y ||
60+
cur.y < 0) {
61+
if (cur.pressure == 0) {
62+
if (crop->last_pressure == 0)
63+
continue;
64+
} else {
65+
continue;
66+
}
67+
}
68+
69+
samp[nread++] = cur;
70+
crop->last_pressure = cur.pressure;
71+
}
72+
73+
return nread;
74+
}
75+
76+
static int crop_read_mt(struct tslib_module_info *info,
77+
struct ts_sample_mt **samp, int max_slots, int nr)
78+
{
79+
struct tslib_crop *crop = (struct tslib_crop *)info;
80+
int32_t ret;
81+
int32_t i, j;
82+
83+
if (!info->next->ops->read_mt)
84+
return -ENOSYS;
85+
86+
ret = info->next->ops->read_mt(info->next, samp, max_slots, nr);
87+
if (ret < 0)
88+
return ret;
89+
90+
if (!crop->last_tid) {
91+
free(crop->last_tid);
92+
93+
crop->last_tid = calloc(max_slots, sizeof(int32_t));
94+
if (!crop->last_tid)
95+
return -ENOMEM;
96+
}
97+
98+
for (i = 0; i < ret; i++) {
99+
for (j = 0; j < max_slots; j++) {
100+
if (!(samp[i][j].valid & TSLIB_MT_VALID))
101+
continue;
102+
103+
/* assume the input device uses 0..(fb-1) value. */
104+
105+
if (samp[i][j].x >= crop->cal_res_x ||
106+
samp[i][j].x < 0 ||
107+
samp[i][j].y >= crop->cal_res_y ||
108+
samp[i][j].y < 0) {
109+
if (samp[i][j].tracking_id == -1) {
110+
/*
111+
* don't drop except last seen tid also -1
112+
* otherwise, tid would get filtered
113+
* out and new x/y would reach the app
114+
*/
115+
if (crop->last_tid[j] == -1) {
116+
samp[i][j].valid &= ~TSLIB_MT_VALID;
117+
}
118+
} else {
119+
/* drop */
120+
samp[i][j].valid &= ~TSLIB_MT_VALID;
121+
}
122+
}
123+
124+
/* save the last not-dropped tid value */
125+
if (samp[i][j].valid & TSLIB_MT_VALID)
126+
crop->last_tid[j] = samp[i][j].tracking_id;
127+
}
128+
}
129+
130+
return ret;
131+
}
132+
133+
static int crop_fini(struct tslib_module_info *info)
134+
{
135+
struct tslib_crop *crop = (struct tslib_crop *)info;
136+
137+
free(crop->last_tid);
138+
free(info);
139+
140+
return 0;
141+
}
142+
143+
static const struct tslib_ops crop_ops = {
144+
.read = crop_read,
145+
.read_mt = crop_read_mt,
146+
.fini = crop_fini,
147+
};
148+
149+
TSAPI struct tslib_module_info *crop_mod_init(__attribute__ ((unused)) struct tsdev *dev,
150+
const char *params)
151+
{
152+
struct tslib_crop *crop;
153+
struct stat sbuf;
154+
FILE *pcal_fd;
155+
int index;
156+
char *calfile;
157+
158+
crop = malloc(sizeof(struct tslib_crop));
159+
if (crop == NULL)
160+
return NULL;
161+
162+
memset(crop, 0, sizeof(struct tslib_crop));
163+
crop->module.ops = &crop_ops;
164+
165+
crop->last_tid = NULL;
166+
167+
/*
168+
* Get resolution from calibration file
169+
*/
170+
if ((calfile = getenv("TSLIB_CALIBFILE")) == NULL)
171+
calfile = TS_POINTERCAL;
172+
173+
if (stat(calfile, &sbuf) == 0) {
174+
pcal_fd = fopen(calfile, "r");
175+
if (!pcal_fd) {
176+
free(crop);
177+
perror("fopen");
178+
return NULL;
179+
}
180+
181+
for (index = 0; index < 7; index++)
182+
if (fscanf(pcal_fd, "%d", &crop->a[index]) != 1)
183+
break;
184+
185+
if (!fscanf(pcal_fd, "%d %d",
186+
&crop->cal_res_x, &crop->cal_res_y)) {
187+
fprintf(stderr,
188+
"CROP: Couldn't read resolution values\n");
189+
}
190+
191+
if (!fscanf(pcal_fd, "%d", &crop->rot)) {
192+
fprintf(stderr, "CROP: Couldn't read rotation value\n");
193+
}
194+
195+
fclose(pcal_fd);
196+
}
197+
return &crop->module;
198+
}
199+
200+
#ifndef TSLIB_STATIC_CROP_MODULE
201+
TSLIB_MODULE_INIT(crop_mod_init);
202+
#endif

plugins/plugins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TSLIB_DECLARE_MODULE(invert);
1616
TSLIB_DECLARE_MODULE(iir);
1717
TSLIB_DECLARE_MODULE(evthres);
1818
TSLIB_DECLARE_MODULE(lowpass);
19+
TSLIB_DECLARE_MODULE(crop);
1920

2021
TSLIB_DECLARE_MODULE(arctic2);
2122
TSLIB_DECLARE_MODULE(collie);

src/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ if ENABLE_STATIC_IIR_MODULE
5050
libts_la_SOURCES += $(top_srcdir)/plugins/iir.c
5151
endif
5252

53+
if ENABLE_STATIC_CROP_MODULE
54+
libts_la_SOURCES += $(top_srcdir)/plugins/crop.c
55+
endif
56+
5357
if ENABLE_STATIC_LINEAR_MODULE
5458
libts_la_SOURCES += $(top_srcdir)/plugins/linear.c
5559
endif

src/ts_load_module.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ static const struct tslib_module_desc tslib_modules[] = {
4545
#ifdef TSLIB_STATIC_CORGI_MODULE
4646
{ "corgi", corgi_mod_init },
4747
#endif
48+
#ifdef TSLIB_STATIC_CROP_MODULE
49+
{ "crop", crop_mod_init },
50+
#endif
4851
#ifdef TSLIB_STATIC_CY8MRLN_PALMPRE_MODULE
4952
{ "cy8mrln_palmpre", cy8mrln_palmpre_mod_init },
5053
#endif

0 commit comments

Comments
 (0)