Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins: add a crop filter module #213

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)


# plugin: crop
include $(CLEAR_VARS)

LOCAL_PRELINK_MODULE := false

LOCAL_SRC_FILES := plugins/crop.c

LOCAL_C_INCLUDES += $(LOCAL_PATH)/src/

LOCAL_SHARED_LIBRARIES := libdl \
libts

LOCAL_MODULE := ts/plugins/crop
LOCAL_MODULE_TAGS := optional

include $(BUILD_SHARED_LIBRARY)


# plugin: evthres
include $(CLEAR_VARS)

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ option(BUILD_SHARED_LIBS "ON: tslib is build as shared;
option(ENABLE_TOOLS "build additional tools" ON)

set(LIBTS_VERSION_CURRENT 10)
set(LIBTS_VERSION_REVISION 4)
set(LIBTS_VERSION_REVISION 5)
set(LIBTS_VERSION_AGE 10)

set(PACKAGE_VERSION 1.22+)
Expand Down
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

tslib 1.23 - unreleased
================================
This release includes libts version 0.10.4 and the following changes:
* some build and security fixes
* new filter module: `module crop`

tslib 1.22 - released 2020-07-01
================================
This release includes libts version 0.10.4 and the following changes:
Expand Down
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ through the library:
module module module module

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

# ts_calibrate
Expand Down Expand Up @@ -197,8 +197,7 @@ for example (yet).
### module: linear
Linear scaling - calibration - module, primerily used for conversion of touch
screen co-ordinates to screen co-ordinates. It applies the corrections as
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
a configuration file.
recorded and saved by the [`ts_calibrate`](https://manpages.debian.org/unstable/libts0/ts_calibrate.1.en.html) tool.

Parameters (usually not needed):
* `rot`
Expand Down Expand Up @@ -382,6 +381,20 @@ Parameters:
Example: `module evthres N=5`


### module: crop
The goal of this filter is to drop input touch events that lie outside
of the Min/Max values for the x/y event codes, i.e. touch points that
might (due to applied filters) lie ouside of the visible framebuffer.

It sends "pen/finger up" for a given slot when said values are outside of
the framebuffer.

It read the framebuffer dimensions from `TSLIB_CALIBFILE` which is
generated and saved by the [`ts_calibrate`](https://manpages.debian.org/unstable/libts0/ts_calibrate.1.en.html) tool.

Example: `module crop`


### module: variance
Variance filter. Tries to do its best in order to filter out random noise
coming from touchscreen ADC's. This is achieved by limiting the sample
Expand Down Expand Up @@ -649,7 +662,7 @@ For testing purposes there are tools like [ts_test_mt](#test-the-filtered-input-
too.

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

#### third party applications
Expand Down
3 changes: 2 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ TSLIB_CHECK_MODULE([skip], [yes], [Enable building of skip filter])
TSLIB_CHECK_MODULE([lowpass], [yes], [Enable building of lowpass filter])
TSLIB_CHECK_MODULE([invert], [yes], [Enable building of invert filter])
TSLIB_CHECK_MODULE([variance], [yes], [Enable building of variance filter])
TSLIB_CHECK_MODULE([crop], [yes], [Enable building of crop filter])

# hardware access modules
#########################
Expand Down Expand Up @@ -189,7 +190,7 @@ AS_IF([test "x$with_sdl2" = "xyes"], [
# increment if the interface changed
LT_CURRENT=10
# increment if anything changed. set to 0 if current was incremented!
LT_REVISION=4
LT_REVISION=5
# increment if the interface change is backwards compatible (an addition). set to 0 if not.
LT_AGE=10
AC_SUBST(LT_CURRENT)
Expand Down
10 changes: 10 additions & 0 deletions doc/ts.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ numerator of the smoothing fraction. Default: 0.
\fBD\fR
.sp
denominator of the smoothing fraction. Default: 1.

.RE
.RE
.PP
Expand Down Expand Up @@ -259,6 +260,15 @@ Minimum pressure value for a sample to be valid. Default: 1.
.sp
Maximum pressure value for a sample to be valid. Default: INT_MAX.

.RE
.RE
.PP
\fBcrop\fR
.RS 4
Ignore touch points outside of the framebuffer.
.sp
.RS 4

.RE
.RE
.PP
Expand Down
3 changes: 3 additions & 0 deletions etc/ts.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ module dejitter delta=100

# Uncomment to use ts_calibrate's settings
module linear

# Uncomment to drop events outside of the framebuffer
# module crop
14 changes: 14 additions & 0 deletions plugins/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ else
IIR_MODULE =
endif

if ENABLE_CROP_MODULE
CROP_MODULE = crop.la
else
CROP_MODULE =
endif

if ENABLE_EVTHRES_MODULE
EVTHRES_MODULE = evthres.la
else
Expand Down Expand Up @@ -197,6 +203,7 @@ pluginexec_LTLIBRARIES = \
$(SKIP_MODULE) \
$(INVERT_MODULE) \
$(IIR_MODULE) \
$(CROP_MODULE) \
$(EVTHRES_MODULE) \
$(LOWPASS_MODULE) \
$(UCB1X00_MODULE) \
Expand Down Expand Up @@ -287,6 +294,13 @@ iir_la_LDFLAGS += -no-undefined
endif
iir_la_LIBADD = $(top_builddir)/src/libts.la

crop_la_SOURCES = crop.c
crop_la_LDFLAGS = -module $(LTVSN)
if WINDOWS
crop_la_LDFLAGS += -no-undefined
endif
crop_la_LIBADD = $(top_builddir)/src/libts.la

evthres_la_SOURCES = evthres.c
evthres_la_LDFLAGS = -module $(LTVSN)
if WINDOWS
Expand Down
202 changes: 202 additions & 0 deletions plugins/crop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* tslib/plugins/crop.c
*
* Copyright (C) 2024 Martin Kepplinger-Novaković
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdio.h>

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

struct tslib_crop {
struct tslib_module_info module;
int32_t *last_tid;
uint32_t last_pressure;
int a[7];
/* fb res from calibration-time */
uint32_t cal_res_x;
uint32_t cal_res_y;
uint32_t rot;
};

static int crop_read(struct tslib_module_info *info, struct ts_sample *samp,
int nr)
{
struct tslib_crop *crop = (struct tslib_crop *)info;
int ret;
int nread = 0;

while (nread < nr) {
struct ts_sample cur;

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

if (cur.x >= crop->cal_res_x ||
cur.x < 0 ||
cur.y >= crop->cal_res_y ||
cur.y < 0) {
if (cur.pressure == 0) {
if (crop->last_pressure == 0)
continue;
} else {
continue;
}
}

samp[nread++] = cur;
crop->last_pressure = cur.pressure;
}

return nread;
}

static int crop_read_mt(struct tslib_module_info *info,
struct ts_sample_mt **samp, int max_slots, int nr)
{
struct tslib_crop *crop = (struct tslib_crop *)info;
int32_t ret;
int32_t i, j;

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;

if (!crop->last_tid) {
free(crop->last_tid);

crop->last_tid = calloc(max_slots, sizeof(int32_t));
if (!crop->last_tid)
return -ENOMEM;
}

for (i = 0; i < ret; i++) {
for (j = 0; j < max_slots; j++) {
if (!(samp[i][j].valid & TSLIB_MT_VALID))
continue;

/* assume the input device uses 0..(fb-1) value. */

if (samp[i][j].x >= crop->cal_res_x ||
samp[i][j].x < 0 ||
samp[i][j].y >= crop->cal_res_y ||
samp[i][j].y < 0) {
if (samp[i][j].tracking_id == -1) {
/*
* don't drop except last seen tid also -1
* otherwise, tid would get filtered
* out and new x/y would reach the app
*/
if (crop->last_tid[j] == -1) {
samp[i][j].valid &= ~TSLIB_MT_VALID;
}
} else {
/* drop */
samp[i][j].valid &= ~TSLIB_MT_VALID;
}
}

/* save the last not-dropped tid value */
if (samp[i][j].valid & TSLIB_MT_VALID)
crop->last_tid[j] = samp[i][j].tracking_id;
}
}

return ret;
}

static int crop_fini(struct tslib_module_info *info)
{
struct tslib_crop *crop = (struct tslib_crop *)info;

free(crop->last_tid);
free(info);

return 0;
}

static const struct tslib_ops crop_ops = {
.read = crop_read,
.read_mt = crop_read_mt,
.fini = crop_fini,
};

TSAPI struct tslib_module_info *crop_mod_init(__attribute__ ((unused)) struct tsdev *dev,
const char *params)
{
struct tslib_crop *crop;
struct stat sbuf;
FILE *pcal_fd;
int index;
char *calfile;

crop = malloc(sizeof(struct tslib_crop));
if (crop == NULL)
return NULL;

memset(crop, 0, sizeof(struct tslib_crop));
crop->module.ops = &crop_ops;

crop->last_tid = NULL;

/*
* Get resolution from calibration file
*/
if ((calfile = getenv("TSLIB_CALIBFILE")) == NULL)
calfile = TS_POINTERCAL;

if (stat(calfile, &sbuf) == 0) {
pcal_fd = fopen(calfile, "r");
if (!pcal_fd) {
free(crop);
perror("fopen");
return NULL;
}

for (index = 0; index < 7; index++)
if (fscanf(pcal_fd, "%d", &crop->a[index]) != 1)
break;

if (!fscanf(pcal_fd, "%d %d",
&crop->cal_res_x, &crop->cal_res_y)) {
fprintf(stderr,
"CROP: Couldn't read resolution values\n");
}

if (!fscanf(pcal_fd, "%d", &crop->rot)) {
fprintf(stderr, "CROP: Couldn't read rotation value\n");
}

fclose(pcal_fd);
}
return &crop->module;
}

#ifndef TSLIB_STATIC_CROP_MODULE
TSLIB_MODULE_INIT(crop_mod_init);
#endif
1 change: 1 addition & 0 deletions plugins/plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TSLIB_DECLARE_MODULE(invert);
TSLIB_DECLARE_MODULE(iir);
TSLIB_DECLARE_MODULE(evthres);
TSLIB_DECLARE_MODULE(lowpass);
TSLIB_DECLARE_MODULE(crop);

TSLIB_DECLARE_MODULE(arctic2);
TSLIB_DECLARE_MODULE(collie);
Expand Down
Loading